SaleInfo.php 2.2 KB

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