DeliverySettlement.php 1.9 KB

12345678910111213141516171819202122232425262728293031
  1. <?php
  2. declare(strict_types=1);
  3. namespace plugin\saiadmin\app\service\fulfillment;
  4. use plugin\saiadmin\exception\ApiException;
  5. /** Pure balance calculations; the application service owns locks and persistence. */
  6. final class DeliverySettlement
  7. {
  8. public static function receive(array $task,array $sku,array $account,int $accepted,int $empty): array
  9. {
  10. $quantity=(int)$task['quantity'];
  11. if ($accepted>$quantity) throw new ApiException('实收桶数不能超过配送桶数');
  12. if ($empty>(int)$account['empty_qty']) throw new ApiException('回收空桶超过企业账面空桶,请先核对现场库存');
  13. if ((int)$sku['transit']<$quantity || (int)$account['frozen']<$quantity || (int)$account['entitlement']<$quantity) throw new ApiException('在途库存或冻结权益异常,请先核账');
  14. return [
  15. 'sku'=>['transit'=>(int)$sku['transit']-$accepted],
  16. 'account'=>['entitlement'=>(int)$account['entitlement']-$accepted,'frozen'=>(int)$account['frozen']-$accepted,
  17. 'full_qty'=>(int)$account['full_qty']+$accepted,'empty_qty'=>(int)$account['empty_qty']-$empty],
  18. ];
  19. }
  20. public static function returned(array $task,array $sku,array $account,int $good,int $damaged): array
  21. {
  22. $remaining=(int)$task['quantity']-(int)$task['delivered_qty'];
  23. if ($good+$damaged!==$remaining) throw new ApiException('可用退回与破损隔离桶数合计必须等于待退回桶数');
  24. if ((int)$sku['transit']<$remaining || (int)$account['frozen']<$remaining) throw new ApiException('在途库存或冻结权益异常,请先核账');
  25. return [
  26. 'sku'=>['transit'=>(int)$sku['transit']-$remaining,'available'=>(int)$sku['available']+$good,'damaged_qty'=>(int)$sku['damaged_qty']+$damaged],
  27. 'account'=>['frozen'=>(int)$account['frozen']-$remaining],
  28. ];
  29. }
  30. }