SpecService.php 7.2 KB

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