@@ -34,8 +34,10 @@ type BuyerOrderProductItem struct {
3434 NonPurchaseReason string
3535 DistributedQuantity * int32
3636 ServiceFeePerUnitCents int32
37+ SubtotalCents int32
3738 EstimatedUnitPriceCents int32
3839 ErrandDemandItemID int64
40+ PackagingFeeShareCents int32
3941}
4042
4143type BuyerOrderDetail struct {
@@ -204,7 +206,23 @@ func GetBuyerOrderDetail(ctx context.Context, requesterID, demandID int64) (*Buy
204206 captainInfo := loadCaptain (ctx , task )
205207 billID := findBillID (assignByItem )
206208
207- productItems , originCents , serviceCents := buildProductItems (items , assignByItem , taskItemByProduct , productMap )
209+ packagingShareCents , err := loadBuyerPackagingShareCents (ctx , task , requesterID )
210+ if err != nil {
211+ log .Error ().Err (err ).Int64 ("demand_id" , demandID ).Msg ("calculate buyer packaging share failed" )
212+ return nil , ErrInternal
213+ }
214+
215+ productItems , originCents , actualCents , serviceCents , err := buildProductItems (
216+ items ,
217+ assignByItem ,
218+ taskItemByProduct ,
219+ productMap ,
220+ packagingShareCents ,
221+ )
222+ if err != nil {
223+ log .Error ().Err (err ).Int64 ("demand_id" , demandID ).Msg ("build buyer order product items failed" )
224+ return nil , ErrInternal
225+ }
208226
209227 return & BuyerOrderDetail {
210228 ErrandDemandID : demand .ID ,
@@ -213,6 +231,7 @@ func GetBuyerOrderDetail(ctx context.Context, requesterID, demandID int64) (*Buy
213231 Status : demand .Status ,
214232 ProductItems : productItems ,
215233 TotalOriginAmountCents : originCents ,
234+ TotalActualAmountCents : actualCents ,
216235 TotalServiceFeeCents : serviceCents ,
217236 StoreInfo : store ,
218237 CaptainInfo : captainInfo ,
@@ -279,35 +298,120 @@ func findBillID(assignByItem map[int64]*model.ErrandTaskAssignment) *int64 {
279298 return nil
280299}
281300
301+ func loadBuyerPackagingShareCents (ctx context.Context , task * model.ErrandTask , requesterID int64 ) (int32 , error ) {
302+ if task == nil || task .PackagingFeeCents <= 0 {
303+ return 0 , nil
304+ }
305+
306+ rows , err := repository .ListTaskPaymentBillAssignments (ctx , postgres .DB , task .ID )
307+ if err != nil {
308+ return 0 , err
309+ }
310+
311+ payerIDs := make (map [int64 ]struct {})
312+ requesterHasBillableItem := false
313+ for _ , row := range rows {
314+ payerIDs [row .PayerID ] = struct {}{}
315+ if row .PayerID == requesterID {
316+ requesterHasBillableItem = true
317+ }
318+ }
319+ if ! requesterHasBillableItem {
320+ return 0 , nil
321+ }
322+
323+ payerCount , err := safeInt32FromInt (len (payerIDs ))
324+ if err != nil {
325+ return 0 , err
326+ }
327+ return ceilDivide (task .PackagingFeeCents , payerCount ), nil
328+ }
329+
282330func buildProductItems (
283331 items []* model.ErrandDemandItem ,
284332 assignByItem map [int64 ]* model.ErrandTaskAssignment ,
285333 taskItemByProduct map [int64 ]* model.ErrandTaskItem ,
286334 productMap map [int64 ]* catalogv1.ProductTemplate ,
287- ) ([]* BuyerOrderProductItem , int32 , int32 ) {
335+ packagingShareCents int32 ,
336+ ) ([]* BuyerOrderProductItem , int32 , * int32 , int32 , error ) {
288337 productItems := make ([]* BuyerOrderProductItem , 0 , len (items ))
289- var originCents , serviceCents int32
338+ actualTotalsReady := len (items ) > 0
339+ packagingApplied := false
340+ var originCents , actualProductCents , serviceCents int64
290341
291342 for _ , item := range items {
292- originCents += item .EstimatedUnitPriceCents * item .Quantity
293- serviceCents += item .ServiceFeePerUnitCents * item .Quantity
343+ originCents += int64 (item .EstimatedUnitPriceCents ) * int64 (item .Quantity )
294344
295345 pi := & BuyerOrderProductItem {
296346 ProductTemplate : productMap [item .ProductTemplateID ],
297347 RequiredQuantity : item .Quantity ,
298348 ServiceFeePerUnitCents : item .ServiceFeePerUnitCents ,
299349 EstimatedUnitPriceCents : item .EstimatedUnitPriceCents ,
300350 ErrandDemandItemID : item .ID ,
351+ PackagingFeeShareCents : packagingShareCents ,
301352 }
353+
354+ quantity := item .Quantity
302355 if a , ok := assignByItem [item .ID ]; ok {
303356 pi .DistributedQuantity = a .DistributedQuantity
357+ if a .DistributedQuantity != nil {
358+ quantity = * a .DistributedQuantity
359+ } else {
360+ actualTotalsReady = false
361+ }
362+ } else {
363+ actualTotalsReady = false
304364 }
365+
366+ actualUnitPriceCents := item .EstimatedUnitPriceCents
305367 if ti , ok := taskItemByProduct [item .ProductTemplateID ]; ok {
306368 pi .ActualUnitPriceCents = ti .ActualUnitPriceCents
307369 pi .PurchasedQuantity = ti .PurchasedQuantity
308370 pi .NonPurchaseReason = ti .NonPurchaseReason
371+ if ti .ActualUnitPriceCents != nil {
372+ actualUnitPriceCents = * ti .ActualUnitPriceCents
373+ } else if quantity > 0 {
374+ actualTotalsReady = false
375+ }
376+ } else if quantity > 0 {
377+ actualTotalsReady = false
378+ }
379+
380+ productAmount := int64 (actualUnitPriceCents ) * int64 (quantity )
381+ serviceFeeAmount := int64 (item .ServiceFeePerUnitCents ) * int64 (quantity )
382+ subtotalCents := productAmount + serviceFeeAmount
383+ if ! packagingApplied && packagingShareCents > 0 {
384+ subtotalCents += int64 (packagingShareCents )
385+ packagingApplied = true
309386 }
387+
388+ subtotal , err := safeInt32FromInt64 (subtotalCents )
389+ if err != nil {
390+ return nil , 0 , nil , 0 , err
391+ }
392+ pi .SubtotalCents = subtotal
393+ actualProductCents += productAmount
394+ serviceCents += serviceFeeAmount
310395 productItems = append (productItems , pi )
311396 }
312- return productItems , originCents , serviceCents
397+
398+ origin , err := safeInt32FromInt64 (originCents )
399+ if err != nil {
400+ return nil , 0 , nil , 0 , err
401+ }
402+ service , err := safeInt32FromInt64 (serviceCents )
403+ if err != nil {
404+ return nil , 0 , nil , 0 , err
405+ }
406+
407+ var actual * int32
408+ if actualTotalsReady {
409+ actualSummary , err := safeInt32FromInt64 (actualProductCents )
410+ if err != nil {
411+ return nil , 0 , nil , 0 , err
412+ }
413+ actual = & actualSummary
414+ }
415+
416+ return productItems , origin , actual , service , nil
313417}
0 commit comments