OrderOutChild.php 6.8 KB

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