1234567891011121314151617181920212223242526272829303132333435363738394041 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\model;
- 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');
- foreach ($arr as $value){
- $temp=["send_num"=>0,"updatetime"=>date("Y-m-d H:i:s")];
- if($sendNum >$value['send_num']){
- $sendNum-=$value['send_num'];
- $stockup=["total_stock"=>$stock[$value['stockid']]['wait_out_stock']+$stock[$value['stockid']]['usable_stock'],"wait_out_stock"=>$stock[$value['stockid']]['wait_out_stock']-$value['send_num'],"updatetime"=>date("Y-m-d H:i:s")];
- }else{
- $temp['send_num'] =$value['send_num']-$sendNum;
- $stockup=["total_stock"=>$stock[$value['stockid']]['wait_out_stock']+$stock[$value['stockid']]['usable_stock'],"wait_out_stock"=>$stock[$value['stockid']]['wait_out_stock']-$sendNum,"updatetime"=>date("Y-m-d H:i:s")];
- $sendNum=0;
- }
- self::where($value)->update($temp);
- self::name("good_stock")->where($stock[$value['stockid']])->update($stockup);
- }
- return true;
- }
- }
|