12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- use think\facade\Log;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class SaleInfo extends Model
- {
- /**
- * @param string $orderCode
- * @param int $sendNum
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- static function SendStock(string $orderCode,int $sendNum){
- $arr=self::where([["orderCode","=",$orderCode],["send_num",">",0]])->select()->toArray();
- if(empty($arr))throw new \Exception("未找到可用的批次库存数据",1006);
- $stockid = array_unique(array_column($arr,"stockid"));
- $stock=self::name("good_stock")->where(["id"=>$stockid])->column("id,usable_stock,wait_out_stock",'id');
- // Log::write("库存更新{$orderCode}:".json_encode($stock,JSON_UNESCAPED_UNICODE),"info",true);
- foreach ($arr as $value){
- $temp=["send_num"=>0,"updatetime"=>date("Y-m-d H:i:s")];
- if($sendNum==0) break;
- if($stock[$value['stockid']]['wait_out_stock']<$sendNum) throw new \Exception("待出库存数量不足");
- if($sendNum >$value['send_num']){
- $sendNum-=$value['send_num'];
- $stockup=["total_stock"=>$stock[$value['stockid']]['wait_out_stock']+$stock[$value['stockid']]['usable_stock']-$value['send_num'],"wait_out_stock"=>$stock[$value['stockid']]['wait_out_stock']-$value['send_num'],"updatetime"=>date("Y-m-d H:i:s")];
- $stock[$value['stockid']]['wait_out_stock'] =$stock[$value['stockid']]['wait_out_stock']-$value['send_num'];
- }else{
- $temp['send_num'] =$value['send_num']-$sendNum;
- $stockup=["total_stock"=>$stock[$value['stockid']]['wait_out_stock']+$stock[$value['stockid']]['usable_stock']-$sendNum,"wait_out_stock"=>$stock[$value['stockid']]['wait_out_stock']-$sendNum,"updatetime"=>date("Y-m-d H:i:s")];
- $stock[$value['stockid']]['wait_out_stock'] =$stock[$value['stockid']]['wait_out_stock']-$sendNum;
- $sendNum=0;
- }
- $ip = self::where($value)->update($temp);
- if($ip==false) throw new \Exception("库存更新失败");
- $sp=self::name("good_stock")->where(["id"=>$stock[$value['stockid']]['id']])->update($stockup);
- if($sp==false) throw new \Exception("库存更新失败");
- }
- return true;
- }
- }
|