SpecService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. $customer_id = ShopList::where('id', $data['shop_id'])->value('customer_id');
  39. foreach ($data['variety'] as $item) {
  40. if(count($item) != 6 || empty(floatval($item['num']))) throw new \Exception('参数有误!');
  41. $specs = ProductConfig::where('id', $item['spec_id'])->field('weight,box_id')->find();
  42. if(!$specs) throw new \Exception('参数有误!');
  43. $weight = bcmul((string)$specs->weight, $item['num'], 2);
  44. $result[] = [
  45. 'user_id' => $uid,
  46. 'customer_id' => $customer_id,
  47. 'shop_id' => $data['shop_id'],
  48. 'plat_id' => $data['plat_id'],
  49. 'variety_id' => $item['variety_id'],
  50. 'spec_id' => $item['spec_id'],
  51. 'num' => $item['num'],
  52. 'weigh' => $weight,
  53. 'total_price' => bcmul($item['price'], $item['num'], 2)
  54. ];
  55. //根据品种扣除库存
  56. $varietyNum = StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
  57. $stockData[] = [
  58. 'user_id' => $uid,
  59. 'type_id' => StockConfig::VarietyName,
  60. 'type' => 1,
  61. 'variety_id' => $item['variety_id'],
  62. 'change' => -$weight,
  63. 'after' => $varietyNum,
  64. 'remark' => '录入出库'
  65. ];
  66. //根据规格扣除包装箱
  67. $boxNum = StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
  68. $stockData[] = [
  69. 'user_id' => $uid,
  70. 'type_id' => StockConfig::PackingBox,
  71. 'type' => 1,
  72. 'variety_id' => $item['variety_id'],
  73. 'change' => -$item['num'],
  74. 'after' => $boxNum,
  75. 'remark' => '录入出库'
  76. ];
  77. }
  78. return [$result, $stockData];
  79. }
  80. public static function getDeliveryEdit(int $user_id, array $data)
  81. {
  82. //查询发货记录
  83. $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
  84. if(!$delivery) throw new \Exception('参数有误!');
  85. //判断是否当天
  86. if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
  87. //修改库存
  88. $specs = ProductConfig::where('id', $delivery->spec_id)->field('weight,box_id')->find();
  89. if(!$specs) throw new \Exception('参数有误!');
  90. //根据品种扣除库存
  91. $weight = bcmul((string)$specs->weight, $data['num'], 2); //总重
  92. //扣除品种
  93. StockDetail::setStockConfigNum($delivery->variety_id, bcsub($delivery->weigh, $weight, 2), StockConfig::VarietyName);
  94. //根据规格扣除包装箱
  95. StockDetail::setStockConfigNum($specs->box_id, bcsub((string)$delivery->num, $data['num'], 2), StockConfig::PackingBox);
  96. //修改数量
  97. $price = bcdiv($delivery->total_price, (string)$delivery->num, 2); //单价
  98. $delivery->weigh = $weight; //总重
  99. $delivery->num = $data['num'];
  100. $delivery->total_price = bcmul($price, $data['num'], 2);
  101. return $delivery->save();
  102. }
  103. //删除
  104. public static function getDeliveryDel(int $user_id, array $data)
  105. {
  106. //查询发货记录
  107. $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
  108. if(!$delivery) throw new \Exception('参数有误!');
  109. //判断是否当天
  110. if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
  111. //修改库存
  112. $box_id = ProductConfig::where('id', $delivery->spec_id)->value('box_id');
  113. if(!$box_id) throw new \Exception('参数有误!');
  114. //扣除品种
  115. StockDetail::setStockConfigNum($delivery->variety_id, $delivery->weigh, StockConfig::VarietyName);
  116. //根据规格扣除包装箱
  117. StockDetail::setStockConfigNum($box_id, (string)$delivery->num, StockConfig::PackingBox);
  118. //删除
  119. return $delivery->delete();
  120. }
  121. }