SpecService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\api\service;
  4. use app\common\model\ShopList;
  5. use app\common\model\StockConfig;
  6. use app\common\model\ProductConfig;
  7. use app\common\model\StockDetail;
  8. use app\common\model\ShopDelivery;
  9. /**
  10. * 录入发货
  11. */
  12. class SpecService{
  13. //判断价格为空
  14. public static function getIsZeroSpecsPrice(array $data): bool
  15. {
  16. $result = false;
  17. foreach ($data as $item) {
  18. if(empty($item['price'])) return false;
  19. }
  20. return $result;
  21. }
  22. //获取规格列表
  23. public static function getSpecsList(int $type_id, array $data): array
  24. {
  25. $result = [];
  26. foreach ($data as $item) {
  27. if($item['type_id'] == $type_id){
  28. $result[] = $item;
  29. }
  30. }
  31. return $result;
  32. }
  33. //录入规格列表
  34. public static function getDeliveryList(int $uid, array $data): array
  35. {
  36. $result = [];
  37. $stockData = [];
  38. //获取店铺客户信息
  39. $customer = ShopList::getCustomerByShopId((int)$data['shop_id']);
  40. if(!$customer) throw new \Exception('店铺客户信息不存在!');
  41. //结算周期
  42. $settlement_data = SpecService::getDeliverySettlement($customer, $data['ship_date']);
  43. foreach ($data['variety'] as $item) {
  44. if(count($item) != 6 || empty(floatval($item['num']))) throw new \Exception('参数有误!');
  45. $specs = ProductConfig::where('id', $item['spec_id'])->field('weight,box_id')->find();
  46. if(!$specs) throw new \Exception('参数有误!');
  47. $weight = bcmul((string)$specs->weight, $item['num'], 2);
  48. $result[] = [
  49. 'user_id' => $uid,
  50. 'customer_id' => $customer->id,
  51. 'shop_id' => $data['shop_id'],
  52. 'plat_id' => $data['plat_id'],
  53. 'variety_id' => $item['variety_id'],
  54. 'spec_id' => $item['spec_id'],
  55. 'num' => $item['num'],
  56. 'weigh' => $weight,
  57. 'price' => $item['price'],
  58. 'ship_date' => $data['ship_date'],
  59. 'settlement_data' => $settlement_data,
  60. 'total_price' => bcmul($item['price'], $item['num'], 2)
  61. ];
  62. //根据品种扣除库存
  63. $varietyNum = StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
  64. $stockData[] = [
  65. 'user_id' => $uid,
  66. 'type_id' => StockConfig::VarietyName,
  67. 'type' => 1,
  68. 'variety_id' => $item['variety_id'],
  69. 'change' => -$weight,
  70. 'after' => $varietyNum,
  71. 'remark' => '录入出库'
  72. ];
  73. //根据规格扣除包装箱
  74. $boxNum = StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
  75. $stockData[] = [
  76. 'user_id' => $uid,
  77. 'type_id' => StockConfig::PackingBox,
  78. 'type' => 1,
  79. 'variety_id' => $specs['box_id'],
  80. 'change' => -$item['num'],
  81. 'after' => $boxNum,
  82. 'remark' => '录入出库'
  83. ];
  84. }
  85. return [$result, $stockData];
  86. }
  87. public static function getDeliveryEdit(int $user_id, array $data)
  88. {
  89. //查询发货记录
  90. $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
  91. if(!$delivery) throw new \Exception('参数有误!');
  92. //判断是否当天
  93. if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
  94. //修改库存
  95. $specs = ProductConfig::where('id', $delivery->spec_id)->field('weight,box_id')->find();
  96. if(!$specs) throw new \Exception('参数有误!');
  97. //根据品种扣除库存
  98. $weight = bcmul((string)$specs->weight, $data['num'], 2); //总重
  99. //扣除品种
  100. StockDetail::setStockConfigNum($delivery->variety_id, bcsub($delivery->weigh, $weight, 2), StockConfig::VarietyName);
  101. //根据规格扣除包装箱
  102. StockDetail::setStockConfigNum($specs->box_id, bcsub((string)$delivery->num, $data['num'], 2), StockConfig::PackingBox);
  103. //修改数量
  104. $price = bcdiv($delivery->total_price, (string)$delivery->num, 2); //单价
  105. $delivery->weigh = $weight; //总重
  106. $delivery->num = $data['num'];
  107. $delivery->total_price = bcmul($price, $data['num'], 2);
  108. return $delivery->save();
  109. }
  110. //删除
  111. public static function getDeliveryDel(int $user_id, array $data)
  112. {
  113. //查询发货记录
  114. $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
  115. if(!$delivery) throw new \Exception('参数有误!');
  116. //判断是否当天
  117. if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
  118. //修改库存
  119. $box_id = ProductConfig::where('id', $delivery->spec_id)->value('box_id');
  120. if(!$box_id) throw new \Exception('参数有误!');
  121. //扣除品种
  122. StockDetail::setStockConfigNum($delivery->variety_id, $delivery->weigh, StockConfig::VarietyName);
  123. //根据规格扣除包装箱
  124. StockDetail::setStockConfigNum($box_id, (string)$delivery->num, StockConfig::PackingBox);
  125. //删除
  126. return $delivery->delete();
  127. }
  128. //结算周期
  129. public static function getDeliverySettlement(object $customer, string $ship_date)
  130. {
  131. $result = $ship_date;
  132. switch ($customer->account_term) {
  133. case 2:
  134. $result = getNextWeekDates($ship_date, $customer->cycle);
  135. break;
  136. case 3:
  137. $result = date('Y-m-d', strtotime($ship_date . ' +15 days'));
  138. break;
  139. }
  140. return $result;
  141. }
  142. }