SpecService.php 6.0 KB

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