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