OrderOutChild.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\model;
  4. use app\admin\common\YouZan;use think\Exception;use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class OrderOutChild extends Model
  9. { /**
  10. * @param string $outCode 发货单信息
  11. * @throws \Exception
  12. */
  13. static function makeChild(string $outCode=''){
  14. if($outCode=='')throw new \Exception("工单发货单单号不能为空");
  15. $out=self::name("order_out")->where(["outCode"=>$outCode,"is_del"=>0])->findOrEmpty();
  16. if(empty($out)) throw new \Exception("发货单信息未找到");
  17. if($out['send_status']!=0) throw new \Exception("发货单拆单状态有误");
  18. $sale = self::name("sale")->where(["orderCode"=>$out['orderCode'],"is_del"=>0])->findOrEmpty();
  19. if(empty($sale)) throw new \Exception("订单信息未找到");
  20. if($sale['wsend_num']<$out['send_num']) throw new \Exception("订单待发货数量不足");
  21. $num= $out['send_num'];
  22. $wsmlist = self::name("good_stock")->alias("a")
  23. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code and b.wsm_type in (2,5)")
  24. ->field("a.id,a.usable_stock,a.wait_out_stock,b.wsm_code,b.supplierNo,b.supplierName")
  25. ->where(["spuCode"=>$sale['good_code'],"a.is_del"=>0,"b.companyNo"=>$sale['companyNo']])
  26. ->where(["a.usable_stock",">=",$num])
  27. ->order("a.usable_stock asc")
  28. ->findOrEmpty();
  29. $child=[
  30. "outChildCode"=>'',
  31. "outCode"=>$outCode,
  32. "orderCode"=>$sale['orderCode'],
  33. "companyNo"=>$sale['supplierNo'],
  34. "companyName"=>$sale['supplierName'],
  35. "supplierNo"=>$sale['supNo'],
  36. "supplierName"=>$sale['supName'],
  37. "cutomer_code"=>$sale['cutomer_code'],
  38. "spuCode"=>$sale['good_code'],
  39. "skuCode"=>$sale['skuCode'],
  40. "good_name"=>$sale['good_name'],
  41. "order_type"=>$sale['order_type'],
  42. "order_source"=>$sale['order_source'],
  43. "num"=>$num,
  44. "wsm_code"=>$wsmlist['wsm_code']??'',
  45. "apply_id"=>$out['apply_id'],
  46. "apply_name"=>$out['apply_name'],
  47. "addrid"=>$out['addrid'],
  48. "addtime"=>date("Y-m-d H:i:s"),
  49. "updatetime"=>date("Y-m-d H:i:s")
  50. ];
  51. if(empty($wsmlist)) self::BratchChild($child);
  52. else self::SingleChild($child,$wsmlist['id']);
  53. $outup=self::name("order_out")->where(["outCode"=>$outCode,"is_del"=>0])->update(["send_status"=>2,"status"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  54. if($outup==false)throw new \Exception("发货单信息更新失败");
  55. return true;
  56. }
  57. /**单工单创建
  58. * @param array $child
  59. * @param int $wsm_id
  60. * @throws \think\Exception
  61. */
  62. public static function SingleChild(array $child=[],int $wsm_id=0){
  63. if(empty($child)) throw new Exception("发货工单信息不能为空");
  64. $child['outChildCode'] = makeNo("TCD");
  65. $childout=self::insert($child);
  66. if($childout==false)throw new \Exception("发货工单生成失败");
  67. $bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'],$wsm_id);
  68. if($bnadd==false)throw new \Exception("库存更新失败");
  69. }
  70. /**多工单创建
  71. * @param array $child
  72. * @throws \think\Exception
  73. */
  74. public static function BratchChild(array $child=[]){
  75. if(empty($child)) throw new Exception("工单信息不能为空");
  76. $stock = self::name("good_stock")->alias("a")
  77. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code and b.wsm_type in (2,5)")
  78. ->where(["spuCode"=>$child['good_code'],"a.is_del"=>0,"b.companyNo"=>$child['companyNo']])
  79. ->order("a.usable_stock desc")
  80. ->column("a.id,a.usable_stock,a.wait_out_stock,b.wsm_code,b.supplierNo,b.supplierName","a.id");
  81. if(empty($stock)) throw new Exception("商品库存信息未找到");
  82. $wsmArr =self::GetKeyBySum($stock,$child['num']);
  83. if(empty($wsmArr)) throw new Exception("库存数不足生成工单");
  84. $i=0;
  85. foreach ($wsmArr as $key=>$val){
  86. $i++;
  87. $child['outChildCode'] = substr(makeNo("TCD"), 0, -3) . str_pad($i, 3, '0', STR_PAD_LEFT);
  88. $child['num'] = $val['desc_num']??0;
  89. $child['wsm_code'] = $val['wsm_code'];
  90. $childout=self::insert($child);
  91. if($childout==false)throw new \Exception("工单生成失败");
  92. $bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'],$val['id']);
  93. if($bnadd==false)throw new \Exception("库存更新失败");
  94. }
  95. }
  96. /**仓库选择
  97. * @param array $Arr
  98. * @param int $num
  99. * @return array
  100. * @throws \Exception
  101. */
  102. private static function GetKeyBySum( array $Arr=[],int $num=0){
  103. $resp=[];
  104. $keys = array_keys($Arr);
  105. $stock =array_column($Arr,"usable_stock");
  106. $newArr=array_combine($keys,$stock);
  107. if(array_sum($stock)<$num) throw new \Exception("库存数不足生成工单");
  108. foreach ($Arr as $key=>$value){
  109. if($value['usable_stock']>=$num){
  110. $value['desc_num'] = $num;
  111. $num=0;
  112. }else{
  113. $value['desc_num'] = $value['usable_stock'];
  114. $num-=$value['usable_stock'];
  115. }
  116. $resp[]=$value;
  117. if($num==0)break;
  118. unset($newArr[$key]);
  119. $k=array_search($num,$newArr);
  120. if($k===false) continue;
  121. else{
  122. $Arr[$k]['desc_num'] = $num;
  123. $resp[]=$Arr[$k];
  124. break;
  125. }
  126. }
  127. return $resp;
  128. }
  129. }