@@ -176,6 +176,17 @@ def test_pack_unit_lock_ignores_pack_total_cx_on_unit():
176176 assert ce .pack_unit_locked_cx_demand (snap , unit ) == 0
177177 own = {"additionalDiscount" : 59500 , "cxDemand" : 790000 , "exShowroom" : 825000 }
178178 assert ce .pack_unit_locked_cx_demand (snap , own ) == 790000
179+ # Agreed price sits above today's GVC after a large RTO / insurance cut.
180+ # That is this unit's deal, not the pack total.
181+ reduced = {
182+ "model" : "Turbo Max" , "variant" : "Maxx (PV)" ,
183+ "exShowroom" : 785000 , "registrationRto" : 0 , "insurance" : 10000 ,
184+ "accessories" : 0 , "fastag" : 0 , "handlingCharges" : 0 , "trc" : 0 ,
185+ "extendedWarranty" : 0 , "rsaAmc" : 0 , "otherCharges" : 0 ,
186+ }
187+ agreed = {"additionalDiscount" : 4500 , "cxDemand" : 805000 , "exShowroom" : 785000 }
188+ assert ce .pack_unit_cx_is_pack_rollup (805000 , reduced , agreed ) is False
189+ assert ce .pack_unit_locked_cx_demand (reduced , agreed ) == 805000
179190
180191
181192def test_additional_discount_has_no_maximum ():
@@ -259,6 +270,106 @@ async def test_pack_rto_insurance_cut_shrinks_additional_not_payable(client):
259270 assert ce .num (u1 .get ("customerPayable" )) != 820074
260271
261272
273+ @pytest .mark .asyncio
274+ async def test_confirmed_rto_cut_leaves_additional_200 (client ):
275+ """Extra discount 4,500 on list RTO 5,500 / insurance 19,000.
276+
277+ RTO 2,200 and insurance 18,000 cut 4,300, so Additional becomes 200.
278+ Payable stays. Same on one vehicle, on pack unit 2, and when the cut
279+ is larger than the discount (Additional goes to 0, payable stays).
280+ """
281+ ex , list_rto , list_ins = 785000 , 5500 , 19000
282+ pay = ex + list_rto + list_ins - 4500 # 805000
283+
284+ async def make (lid , * , pack = False ):
285+ await server .db .leads .delete_many ({"leadId" : lid })
286+ await server .db .price_master .delete_many ({"priceId" : f"PM-{ lid } " })
287+ await server .db .price_master .insert_one ({
288+ "priceId" : f"PM-{ lid } " , "model" : "Turbo Max" , "variant" : "Maxx Confirmed" ,
289+ "exShowroom" : ex , "rto" : list_rto , "insurance" : list_ins ,
290+ "status" : "active" , "oemSyncedAt" : "2026-11-01T00:00:00" ,
291+ })
292+ unit = {
293+ "model" : "Turbo Max" , "variant" : "Maxx Confirmed" ,
294+ "exShowroom" : ex , "rto" : list_rto , "insuranceAmount" : list_ins , "insurance" : list_ins ,
295+ "additionalDiscount" : 4500 , "customerPayable" : pay , "cxDemand" : pay ,
296+ "useDealPrice" : True , "priceStructureSaved" : True ,
297+ "schemeAllocationExplicit" : True , "benefitPassedBreakup" : "{}" ,
298+ }
299+ doc = {
300+ "leadId" : lid , "customerName" : lid , "mobile" : "9000003344" ,
301+ "interestedModel" : "Turbo Max" , "variant" : "Maxx Confirmed" ,
302+ "executive" : "Amit" , "accountStatus" : "Active" , "currentStatus" : "Booked" ,
303+ "bookingDate" : "2026-11-06" , "priceStructureSaved" : True ,
304+ "schemeAllocationExplicit" : True , "benefitPassedBreakup" : "{}" ,
305+ "exShowroom" : ex , "rto" : list_rto , "insuranceAmount" : list_ins , "insurance" : list_ins ,
306+ "additionalDiscount" : 4500 , "customerPayable" : pay , "cxDemand" : pay , "useDealPrice" : True ,
307+ "units" : [dict (unit , sno = 1 )],
308+ }
309+ if pack :
310+ doc ["sameOrderMultiUnit" ] = True
311+ doc ["units" ] = [dict (unit , sno = 1 ), dict (unit , sno = 2 )]
312+ doc ["customerPayable" ] = pay * 2
313+ doc ["cxDemand" ] = pay * 2
314+ await server .db .leads .insert_one (doc )
315+
316+ await make ("LD-CONF-RTO" )
317+ saved = await client .put ("/api/leads/LD-CONF-RTO/price-structure" , json = {
318+ "exShowroom" : ex , "rto" : 2200 , "insuranceAmount" : 18000 ,
319+ })
320+ assert saved .status_code == 200 , saved .text
321+ one = saved .json ()
322+ assert ce .num (one ["customerPayable" ]) == pay
323+ assert ce .num (one ["additionalDiscount" ]) == 200
324+
325+ await make ("LD-CONF-RTO-PACK" , pack = True )
326+ pack_saved = await client .put ("/api/leads/LD-CONF-RTO-PACK/price-structure" , json = {
327+ "exShowroom" : ex , "rto" : 2200 , "insuranceAmount" : 18000 , "unitSno" : 2 ,
328+ })
329+ assert pack_saved .status_code == 200 , pack_saved .text
330+ after = await server .db .leads .find_one ({"leadId" : "LD-CONF-RTO-PACK" })
331+ u1 , u2 = after ["units" ][0 ], after ["units" ][1 ]
332+ assert ce .num (u2 ["customerPayable" ]) == pay
333+ assert ce .num (u2 ["additionalDiscount" ]) == 200
334+ assert ce .num (u1 ["additionalDiscount" ]) == 4500
335+ assert ce .num (u1 ["rto" ]) == list_rto
336+
337+ await make ("LD-CONF-RTO-OF" , pack = True )
338+ rto = await client .put ("/api/leads/LD-CONF-RTO-OF/owner-field" , json = {
339+ "field" : "rto" , "value" : 2200 , "unitSno" : 2 ,
340+ })
341+ assert rto .status_code == 200 , rto .text
342+ ins = await client .put ("/api/leads/LD-CONF-RTO-OF/owner-field" , json = {
343+ "field" : "insuranceAmount" , "value" : 18000 , "unitSno" : 2 ,
344+ })
345+ assert ins .status_code == 200 , ins .text
346+ edited = await server .db .leads .find_one ({"leadId" : "LD-CONF-RTO-OF" })
347+ edited_u1 , edited_u2 = edited ["units" ][0 ], edited ["units" ][1 ]
348+ assert ce .num (edited_u2 ["rto" ]) == 2200
349+ assert ce .num (edited_u2 ["insuranceAmount" ]) == 18000
350+ assert ce .num (edited_u2 ["customerPayable" ]) == pay
351+ assert ce .num (edited_u2 ["additionalDiscount" ]) == 200
352+ assert ce .num (edited_u1 ["additionalDiscount" ]) == 4500
353+ assert ce .num (edited ["rto" ]) == list_rto
354+
355+ # Cut 14,500 is larger than Additional 4,500. Discount goes to 0.
356+ # Payable stays; the rest is extra from the customer, not a cheaper price.
357+ await make ("LD-CONF-RTO-BIG" , pack = True )
358+ big = await client .put ("/api/leads/LD-CONF-RTO-BIG/price-structure" , json = {
359+ "exShowroom" : ex , "rto" : 0 , "insuranceAmount" : 10000 , "unitSno" : 2 ,
360+ })
361+ assert big .status_code == 200 , big .text
362+ big_doc = await server .db .leads .find_one ({"leadId" : "LD-CONF-RTO-BIG" })
363+ big_u2 = big_doc ["units" ][1 ]
364+ assert ce .num (big_u2 ["customerPayable" ]) == pay
365+ assert ce .num (big_u2 ["cxDemand" ]) == pay
366+ assert ce .num (big_u2 ["additionalDiscount" ]) == 0
367+ assert ce .num (big_u2 .get ("extraIncomeFromCustomer" )) == 10000
368+ base_margin = ce .compute_dealer_margin ({"exShowroom" : ex })["marginNetExGst" ]
369+ assert ce .num (big_doc .get ("extraIncomeFromCustomer" )) == 10000
370+ assert ce .num (big_doc .get ("dealerMarginNetExGst" )) == ce .round2 (base_margin + 10000 )
371+
372+
262373@pytest .mark .asyncio
263374async def test_actual_rto_and_insurance_cut_additional_not_payable (client ):
264375 """Lower real RTO and insurance shrink Additional. Payable and excess stay."""
0 commit comments