SaleInfo.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class SaleInfo extends Model
  9. {
  10. /**
  11. * @param string $orderCode
  12. * @param int $sendNum
  13. * @return bool
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\DbException
  16. * @throws \think\db\exception\ModelNotFoundException
  17. */
  18. static function SendStock(string $orderCode,int $sendNum){
  19. $arr=self::where([["orderCode","=",$orderCode],["send_num",">",0]])->select()->toArray();
  20. if(empty($arr))throw new \Exception("未找到可用的批次库存数据",1006);
  21. $stockid = array_unique(array_column($arr,"stockid"));
  22. $stock=self::name("good_stock")->where(["id"=>$stockid])->column("id,usable_stock,wait_out_stock",'id');
  23. foreach ($arr as $value){
  24. $temp=["send_num"=>0,"updatetime"=>date("Y-m-d H:i:s")];
  25. if($sendNum >$value['send_num']){
  26. $sendNum-=$value['send_num'];
  27. $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")];
  28. }else{
  29. $temp['send_num'] =$value['send_num']-$sendNum;
  30. $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")];
  31. $sendNum=0;
  32. }
  33. self::where($value)->update($temp);
  34. self::name("good_stock")->where($stock[$value['stockid']])->update($stockup);
  35. }
  36. return true;
  37. }
  38. }