OrderOutChild.php 6.9 KB

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