SpecService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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) < 7 || empty(floatval($item['num']))){
  46. throw new \Exception('参数有误!' . json_encode($item));
  47. }
  48. if($item['box_id'] == 0) $item['box_name'] = '';
  49. $specsWeight = ProductConfig::where('id', $item['spec_id'])->value('weight');
  50. $weight = bcmul((string)$specsWeight, $item['num'], 2);
  51. $result[] = [
  52. 'user_id' => $uid,
  53. 'customer_id' => $customer->id,
  54. 'shop_id' => $data['shop_id'],
  55. 'plat_id' => $data['plat_id'],
  56. 'variety_id' => $item['variety_id'],
  57. 'variety_name' => $item['variety_name'],
  58. 'spec_id' => $item['spec_id'],
  59. 'box_id' => $item['box_id'],
  60. 'box_name' => $item['box_name'],
  61. 'spec_name' => $item['spec_name'],
  62. 'num' => $item['num'],
  63. 'weigh' => $weight,
  64. 'price' => $item['price'],
  65. 'ship_date' => $data['ship_date'],
  66. 'settlement_data' => $settlement_data,
  67. 'total_price' => bcmul($item['price'], $item['num'], 2)
  68. ];
  69. //根据品种扣除库存
  70. if($weight){
  71. $varietyNum = StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
  72. $stockData[] = [
  73. 'user_id' => $uid,
  74. 'type_id' => StockConfig::VarietyName,
  75. 'type' => 1,
  76. 'variety_id' => $item['variety_id'],
  77. 'change' => -$weight,
  78. 'after' => $varietyNum,
  79. 'remark' => '录入出库'
  80. ];
  81. }
  82. //根据规格扣除包装箱
  83. if($item['box_id']){
  84. $boxNum = StockDetail::setStockConfigNum((int)$item['box_id'], (string)-$item['num'], StockConfig::PackingBox);
  85. $stockData[] = [
  86. 'user_id' => $uid,
  87. 'type_id' => StockConfig::PackingBox,
  88. 'type' => 1,
  89. 'variety_id' => $item['box_id'],
  90. 'change' => -$item['num'],
  91. 'after' => $boxNum,
  92. 'remark' => '录入出库'
  93. ];
  94. }
  95. }
  96. return [$result, $stockData];
  97. }
  98. public static function getDeliveryEdit(int $user_id, array $data)
  99. {
  100. //查询发货记录
  101. $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
  102. if(!$delivery) throw new \Exception('参数有误!');
  103. //判断是否当天
  104. if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
  105. //修改库存
  106. $specs = ProductConfig::where('id', $delivery->spec_id)->value('weight');
  107. if(!$specs) throw new \Exception('参数有误!');
  108. //根据品种扣除库存
  109. $weight = bcmul((string)$specs, $data['num'], 2); //总重
  110. //扣除品种
  111. StockDetail::setStockConfigNum($delivery->variety_id, bcsub($delivery->weigh, $weight, 2), StockConfig::VarietyName);
  112. //根据规格扣除包装箱
  113. if($delivery->box_id){
  114. StockDetail::setStockConfigNum($delivery->box_id, bcsub((string)$delivery->num, $data['num'], 2), StockConfig::PackingBox);
  115. }
  116. //修改数量
  117. $price = bcdiv($delivery->total_price, (string)$delivery->num, 2); //单价
  118. $delivery->weigh = $weight; //总重
  119. $delivery->num = $data['num'];
  120. $delivery->total_price = bcmul($price, $data['num'], 2);
  121. return $delivery->save();
  122. }
  123. //删除
  124. public static function getDeliveryDel(int $user_id, array $data)
  125. {
  126. //查询发货记录
  127. $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
  128. if(!$delivery) throw new \Exception('参数有误!');
  129. //判断是否当天
  130. if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
  131. //修改库存
  132. $box_id = ProductConfig::where('id', $delivery->spec_id)->value('box_id');
  133. if(!$box_id) throw new \Exception('参数有误!');
  134. //扣除品种
  135. StockDetail::setStockConfigNum($delivery->variety_id, $delivery->weigh, StockConfig::VarietyName);
  136. //根据规格扣除包装箱
  137. StockDetail::setStockConfigNum($box_id, (string)$delivery->num, StockConfig::PackingBox);
  138. //删除
  139. return $delivery->delete();
  140. }
  141. //结算周期
  142. public static function getDeliverySettlement(object $customer, string $ship_date)
  143. {
  144. $result = $ship_date;
  145. switch ($customer->account_term) {
  146. case 2:
  147. $result = getNextWeekDates($ship_date, $customer->cycle);
  148. break;
  149. case 3:
  150. $result = date('Y-m-d', strtotime($ship_date . ' +15 days'));
  151. break;
  152. }
  153. return $result;
  154. }
  155. }