SpecService.php 4.9 KB

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