SpecService.php 7.1 KB

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