OrderOutChild.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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::insert($child);
  72. if ($childout == false) throw new \Exception("发货工单生成失败");
  73. $bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'], $wsm_id);
  74. if ($bnadd == false) throw new \Exception("库存更新失败");
  75. }
  76. /**多工单创建
  77. * @param array $child
  78. * @throws \think\Exception
  79. */
  80. public static function BratchChild(array $child = [])
  81. {
  82. if (empty($child)) throw new Exception("工单信息不能为空");
  83. $stock = self::name("good_stock")->alias("a")
  84. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code and b.wsm_type in (2,5)")
  85. ->where(["spuCode" => $child['spuCode'], "a.is_del" => 0, "b.companyNo" => $child['companyNo']])
  86. ->order("a.usable_stock desc")
  87. ->column("a.id,a.usable_stock,a.wait_out_stock,b.wsm_code,b.supplierNo,b.supplierName", "a.id");
  88. if (empty($stock)) throw new Exception("商品库存信息未找到");
  89. $wsmArr = self::GetKeyBySum($stock, intval($child['num']));
  90. if (empty($wsmArr)) throw new Exception("库存数不足生成工单");
  91. $i = 0;
  92. foreach ($wsmArr as $key => $val) {
  93. $i++;
  94. $child['outChildCode'] = substr(makeNo("TCD"), 0, -3) . str_pad(strval($i), 3, '0', STR_PAD_LEFT);
  95. $child['num'] = $val['desc_num'] ?? 0;
  96. $child['wsm_code'] = $val['wsm_code'];
  97. $childout = self::insert($child);
  98. if ($childout == false) throw new \Exception("工单生成失败");
  99. $bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'], intval($val['id']));
  100. if ($bnadd == false) throw new \Exception("库存更新失败");
  101. }
  102. }
  103. /**仓库选择
  104. * @param array $Arr
  105. * @param int $num
  106. * @return array
  107. * @throws \Exception
  108. */
  109. private static function GetKeyBySum(array $Arr = [], int $num = 0)
  110. {
  111. $resp = [];
  112. $keys = array_keys($Arr);
  113. $stock = array_column($Arr, "usable_stock");
  114. $newArr = array_combine($keys, $stock);
  115. if (array_sum($stock) < $num) throw new \Exception("库存数不足生成工单");
  116. foreach ($Arr as $key => $value) {
  117. if ($value['usable_stock'] >= $num) {
  118. $value['desc_num'] = $num;
  119. $num = 0;
  120. } else {
  121. $value['desc_num'] = $value['usable_stock'];
  122. $num -= $value['usable_stock'];
  123. }
  124. $resp[] = $value;
  125. if ($num == 0) break;
  126. unset($newArr[$key]);
  127. $k = array_search($num, $newArr);
  128. if ($k === false) continue;
  129. else {
  130. $Arr[$k]['desc_num'] = $num;
  131. $resp[] = $Arr[$k];
  132. break;
  133. }
  134. }
  135. return $resp;
  136. }
  137. }