| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- declare(strict_types=1);
- namespace app\api\service;
- use app\common\model\ShopList;
- use app\common\model\StockConfig;
- use app\common\model\ProductConfig;
- use app\common\model\StockDetail;
- use app\common\model\ShopDelivery;
- /**
- * 录入发货
- */
- class SpecService{
-
- //判断价格为空
- public static function getIsZeroSpecsPrice(array $data): bool
- {
- $result = false;
- foreach ($data as $item) {
- if(empty($item['price'])) return false;
- }
- return $result;
- }
- //获取规格列表
- public static function getSpecsList(int $type_id, array $data): array
- {
- $result = [];
- foreach ($data as $item) {
-
- if($item['type_id'] == $type_id){
- $result[] = $item;
- }
- }
- return $result;
- }
-
-
- //录入规格列表
- public static function getDeliveryList(int $uid, array $data): array
- {
- $result = [];
- $customer_id = ShopList::where('id', $data['shop_id'])->value('customer_id');
- foreach ($data['variety'] as $item) {
- if(count($item) != 6 || empty(floatval($item['num']))) throw new \Exception('参数有误!');
- $specs = ProductConfig::where('id', $item['spec_id'])->field('weight,box_id')->find();
- if(!$specs) throw new \Exception('参数有误!');
- $weight = bcmul((string)$specs->weight, $item['num'], 2);
- $result[] = [
- 'user_id' => $uid,
- 'customer_id' => $customer_id,
- 'shop_id' => $data['shop_id'],
- 'plat_id' => $data['plat_id'],
- 'variety_id' => $item['variety_id'],
- 'spec_id' => $item['spec_id'],
- 'num' => $item['num'],
- 'weigh' => $weight,
- 'total_price' => bcmul($item['price'], $item['num'], 2)
- ];
-
- //根据品种扣除库存
- StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
- //根据规格扣除包装箱
- StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
- }
- return $result;
- }
- public static function getDeliveryEdit(int $user_id, array $data)
- {
- //查询发货记录
- $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
- if(!$delivery) throw new \Exception('参数有误!');
- //判断是否当天
- if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
-
- //修改库存
- $specs = ProductConfig::where('id', $delivery->spec_id)->field('weight,box_id')->find();
- if(!$specs) throw new \Exception('参数有误!');
-
- //根据品种扣除库存
- $weight = bcmul((string)$specs->weight, $data['num'], 2); //总重
-
- //扣除品种
- StockDetail::setStockConfigNum($delivery->variety_id, bcsub($delivery->weigh, $weight, 2), StockConfig::VarietyName);
- //根据规格扣除包装箱
- StockDetail::setStockConfigNum($specs->box_id, bcsub((string)$delivery->num, $data['num'], 2), StockConfig::PackingBox);
-
- //修改数量
- $price = bcdiv($delivery->total_price, (string)$delivery->num, 2); //单价
- $delivery->weigh = $weight; //总重
- $delivery->num = $data['num'];
- $delivery->total_price = bcmul($price, $data['num'], 2);
- return $delivery->save();
- }
-
- //删除
- public static function getDeliveryDel(int $user_id, array $data)
- {
- //查询发货记录
- $delivery = ShopDelivery::where('user_id', $user_id)->where('id', $data['ids'])->find();
- if(!$delivery) throw new \Exception('参数有误!');
- //判断是否当天
- if(substr($delivery->createtime, 0, 10) != date('Y-m-d')) throw new \Exception('只能修改当日发货记录!');
-
- //修改库存
- $box_id = ProductConfig::where('id', $delivery->spec_id)->value('box_id');
- if(!$box_id) throw new \Exception('参数有误!');
- //扣除品种
- StockDetail::setStockConfigNum($delivery->variety_id, $delivery->weigh, StockConfig::VarietyName);
- //根据规格扣除包装箱
- StockDetail::setStockConfigNum($box_id, (string)$delivery->num, StockConfig::PackingBox);
- //删除
- return $delivery->delete();
- }
-
- }
|