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'] != 0) 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['companyNo']])
  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['companyNo'],
  38. "companyName" => $sale['companyName'],
  39. "supplierNo" => $sale['supplierNo'],
  40. "supplierName" => $sale['supplierName'],
  41. "cutomer_code" => $sale['cutomer_code'],
  42. "spuCode" => $sale['good_code'],
  43. "skuCode" => $sale['skuCode'],
  44. "good_name" => $sale['good_name'],
  45. "order_type" => $sale['order_type'],
  46. "order_source" => $sale['order_source'],
  47. "num" => $num,
  48. "wsm_code" => $wsmlist['wsm_code'] ?? '',
  49. "apply_id" => $out['apply_id'],
  50. "apply_name" => $out['apply_name'],
  51. "addrid" => $out['addrid'],
  52. "addtime" => date("Y-m-d H:i:s"),
  53. "updatetime" => date("Y-m-d H:i:s")
  54. ];
  55. if ($wsmlist->isEmpty()) self::BratchChild($child);
  56. else self::SingleChild($child, $wsmlist['id']);
  57. $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")]);
  58. if ($outup == false) throw new \Exception("发货单信息更新失败");
  59. return true;
  60. }
  61. /**单工单创建
  62. * @param array $child
  63. * @param int $wsm_id
  64. * @throws \think\Exception
  65. */
  66. public static function SingleChild(array $child = [], int $wsm_id = 0)
  67. {
  68. if (empty($child)) throw new Exception("发货工单信息不能为空");
  69. $child['outChildCode'] = makeNo("TCD");
  70. $childout = self::insert($child);
  71. if ($childout == false) throw new \Exception("发货工单生成失败");
  72. $bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'], $wsm_id);
  73. if ($bnadd == false) throw new \Exception("库存更新失败");
  74. }
  75. /**多工单创建
  76. * @param array $child
  77. * @throws \think\Exception
  78. */
  79. public static function BratchChild(array $child = [])
  80. {
  81. if (empty($child)) throw new Exception("工单信息不能为空");
  82. $stock = self::name("good_stock")->alias("a")
  83. ->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code and b.wsm_type in (2,5)")
  84. ->where(["spuCode" => $child['spuCode'], "a.is_del" => 0, "b.companyNo" => $child['companyNo']])
  85. ->order("a.usable_stock desc")
  86. ->column("a.id,a.usable_stock,a.wait_out_stock,b.wsm_code,b.supplierNo,b.supplierName", "a.id");
  87. if (empty($stock)) throw new Exception("商品库存信息未找到");
  88. $wsmArr = self::GetKeyBySum($stock, $child['num']);
  89. if (empty($wsmArr)) throw new Exception("库存数不足生成工单");
  90. $i = 0;
  91. foreach ($wsmArr as $key => $val) {
  92. $i++;
  93. $child['outChildCode'] = substr(makeNo("TCD"), 0, -3) . str_pad($i, 3, '0', STR_PAD_LEFT);
  94. $child['num'] = $val['desc_num'] ?? 0;
  95. $child['wsm_code'] = $val['wsm_code'];
  96. $childout = self::insert($child);
  97. if ($childout == false) throw new \Exception("工单生成失败");
  98. $bnadd = GoodStockInfo::ChildAddBn($child['outChildCode'], $val['id']);
  99. if ($bnadd == false) throw new \Exception("库存更新失败");
  100. }
  101. }
  102. /**仓库选择
  103. * @param array $Arr
  104. * @param int $num
  105. * @return array
  106. * @throws \Exception
  107. */
  108. private static function GetKeyBySum(array $Arr = [], int $num = 0)
  109. {
  110. $resp = [];
  111. $keys = array_keys($Arr);
  112. $stock = array_column($Arr, "usable_stock");
  113. $newArr = array_combine($keys, $stock);
  114. if (array_sum($stock) < $num) throw new \Exception("库存数不足生成工单");
  115. foreach ($Arr as $key => $value) {
  116. if ($value['usable_stock'] >= $num) {
  117. $value['desc_num'] = $num;
  118. $num = 0;
  119. } else {
  120. $value['desc_num'] = $value['usable_stock'];
  121. $num -= $value['usable_stock'];
  122. }
  123. $resp[] = $value;
  124. if ($num == 0) break;
  125. unset($newArr[$key]);
  126. $k = array_search($num, $newArr);
  127. if ($k === false) continue;
  128. else {
  129. $Arr[$k]['desc_num'] = $num;
  130. $resp[] = $Arr[$k];
  131. break;
  132. }
  133. }
  134. return $resp;
  135. }
  136. }