@@ -219,67 +219,87 @@ def test_use_cr_posting_date_config(cr):
219219
220220@pytest .mark .order (18 )
221221def test_calculate_payment_term_discount_function ():
222+ # Create precise dates to test edge cases
223+ today = datetime .date .today ()
224+ first_of_current_month = today .replace (day = 1 )
225+ last_month = first_of_current_month - datetime .timedelta (days = 1 )
226+ first_of_last_month = last_month .replace (day = 1 )
227+ last_day_of_last_month = last_month
228+
229+ # Simulate being on the 9th day of current month (within 10-day discount period)
230+ simulated_today_within = first_of_current_month + datetime .timedelta (days = 8 ) # day 9
231+ # Simulate being on the 12th day of current month (outside 10-day discount period)
232+ simulated_today_outside = first_of_current_month + datetime .timedelta (days = 11 ) # day 12
233+
222234 cases = [
223235 {
224236 "desc" : "2% 10 Net 30 - within the discount period" ,
225237 "payment_term" : "2% 10 Net 30" ,
226- "posting_date" : datetime .date .today () - datetime .timedelta (days = 5 ),
238+ "posting_date" : first_of_last_month , # Invoice on 1st of previous month
239+ "payment_date" : simulated_today_within , # Payment on 9th of current month
227240 "amount" : 1000.0 ,
228241 "expected_discount" : 1000.0 * 0.02 ,
229242 "should_have_discount" : True ,
230243 },
231244 {
232245 "desc" : "2% 10 Net 30 - outside the discount period" ,
233246 "payment_term" : "2% 10 Net 30" ,
234- "posting_date" : datetime .date .today () - datetime .timedelta (days = 20 ),
247+ "posting_date" : first_of_last_month , # Invoice on 1st of previous month
248+ "payment_date" : simulated_today_outside , # Payment on 12th of current month (outside period)
235249 "amount" : 1000.0 ,
236250 "expected_discount" : 0.0 ,
237251 "should_have_discount" : False ,
238252 },
239253 {
240- "desc" : "3% 10 Net 30 - within the discount period" ,
241- "payment_term" : "3% 10 Net 30" ,
242- "posting_date" : datetime .date .today () - datetime .timedelta (days = 8 ),
254+ "desc" : "$20 10 Net 30 - within the discount period" ,
255+ "payment_term" : "$20 10 Net 30" ,
256+ "posting_date" : last_day_of_last_month , # Invoice on last day of previous month
257+ "payment_date" : simulated_today_within , # Payment on 9th of current month
243258 "amount" : 500.0 ,
244259 "expected_discount" : min (20.0 , 500.0 ),
245260 "should_have_discount" : True ,
246261 },
247262 {
248- "desc" : "3% 10 Net 30 - outside the discount period" ,
249- "payment_term" : "3% 10 Net 30" ,
250- "posting_date" : datetime .date .today () - datetime .timedelta (days = 25 ),
263+ "desc" : "$20 10 Net 30 - outside the discount period" ,
264+ "payment_term" : "$20 10 Net 30" ,
265+ "posting_date" : last_day_of_last_month , # Invoice on last day of previous month
266+ "payment_date" : simulated_today_outside , # Payment on 12th of current month (outside period)
251267 "amount" : 500.0 ,
252268 "expected_discount" : 0.0 ,
253269 "should_have_discount" : False ,
254270 },
255271 {
256272 "desc" : "2% 10 Net 30 - Invoice Date - last valid day" ,
257273 "payment_term" : "2% 10 Net 30 - Invoice Date" ,
258- "posting_date" : datetime .date .today () - datetime .timedelta (days = 10 ),
274+ "posting_date" : today - datetime .timedelta (days = 10 ), # Exactly 10 days ago
275+ "payment_date" : today ,
259276 "amount" : 800.0 ,
260277 "expected_discount" : 800.0 * 0.02 ,
261278 "should_have_discount" : True ,
262279 },
263280 {
264281 "desc" : "2% 10 Net 30 - Invoice Date - outside the discount period" ,
265282 "payment_term" : "2% 10 Net 30 - Invoice Date" ,
266- "posting_date" : datetime .date .today () - datetime .timedelta (days = 11 ),
283+ "posting_date" : today - datetime .timedelta (days = 11 ), # 11 days ago (outside period)
284+ "payment_date" : today ,
267285 "amount" : 800.0 ,
268286 "expected_discount" : 0.0 ,
269287 "should_have_discount" : False ,
270288 },
271289 {
272- "desc" : "3% 10 Net 30 - Month End - within valid month" ,
273- "payment_term" : "3% 10 Net 30 - Month End" ,
274- "posting_date" : (datetime .date .today ().replace (day = 1 ) - datetime .timedelta (days = 5 )),
290+ "desc" : "3% Net 30 - within valid month" ,
291+ "payment_term" : "3% Net 30" ,
292+ "posting_date" : first_of_last_month , # Invoice from previous month
293+ "payment_date" : today , # Payment today (within validity month)
275294 "amount" : 600.0 ,
276295 "expected_discount" : 600.0 * 0.03 ,
277296 "should_have_discount" : True ,
278297 },
279298 {
280- "desc" : "3% 10 Net 30 - Month End - outside valid month" ,
281- "payment_term" : "3% 10 Net 30 - Month End" ,
282- "posting_date" : (datetime .date .today ().replace (day = 1 ) - datetime .timedelta (days = 40 )),
299+ "desc" : "3% Net 30 - outside valid month" ,
300+ "payment_term" : "3% Net 30" ,
301+ "posting_date" : first_of_last_month - datetime .timedelta (days = 32 ), # Invoice from 2 months ago
302+ "payment_date" : today , # Payment today (outside validity month)
283303 "amount" : 600.0 ,
284304 "expected_discount" : 0.0 ,
285305 "should_have_discount" : False ,
@@ -295,7 +315,7 @@ def test_calculate_payment_term_discount_function():
295315 "posting_date" : case ["posting_date" ],
296316 }
297317 )
298- payment_date = datetime . date . today ()
318+ payment_date = case [ "payment_date" ]
299319 discount_amount , has_discount = calculate_payment_term_discount (transaction , payment_date )
300320
301321 assert (
@@ -351,9 +371,9 @@ def test_cr_payment_term_discounts(cr):
351371
352372 if payment_term in [
353373 "2% 10 Net 30" ,
354- "3% 10 Net 30" ,
374+ "$20 10 Net 30" ,
355375 "2% 10 Net 30 - Invoice Date" ,
356- "3% 10 Net 30 - Month End " ,
376+ "3% Net 30" ,
357377 ]:
358378 row ["pay" ] = True
359379 row ["mode_of_payment" ] = "Credit Card"
@@ -372,10 +392,10 @@ def test_cr_payment_term_discounts(cr):
372392 ), f"Percentage discount mismatch for { case ['invoice' ]} : expected { expected_discount } , got { case ['discount_amount' ]} "
373393
374394 # Test Case 2: Invoices with amount-based discounts (fixed amount)
375- amount_discount_cases = [tc for tc in test_cases if "3% 10 Net 30" in tc ["payment_term" ]]
395+ amount_discount_cases = [tc for tc in test_cases if "$20 10 Net 30" in tc ["payment_term" ]]
376396 for case in amount_discount_cases :
377397 if case ["has_discount" ]:
378- if case ["payment_term" ] == "3% 10 Net 30 - Month End " :
398+ if case ["payment_term" ] == "3% Net 30" :
379399 expected_discount = case ["amount" ] * 0.03
380400 else :
381401 expected_discount = min (20.0 , case ["amount" ])
@@ -403,8 +423,8 @@ def test_cr_process_with_discounts(cr):
403423 for row in cr .transactions :
404424 if row .get ("doctype" ) == "Purchase Invoice" and row .get ("payment_term" ) in [
405425 "2% 10 Net 30" ,
406- "3% 10 Net 30" ,
407- "3% 10 Net 30 - Month End " ,
426+ "$20 10 Net 30" ,
427+ "3% Net 30" ,
408428 "2% 10 Net 30 - Invoice Date" ,
409429 ]:
410430 row ["pay" ] = True
0 commit comments