SpecService.php 7.2 KB

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