UpdatePickZone.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Imports;
  3. use App\OracleDOCOrderDetail;
  4. use App\Services\LogService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  12. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  13. HeadingRowFormatter::default('none');
  14. class UpdatePickZone implements ToCollection,WithHeadingRow
  15. {
  16. /**
  17. * @param Collection $collection
  18. * @return bool
  19. * @throws
  20. */
  21. public function collection(Collection $collection)
  22. {
  23. $row = $collection->first();
  24. $headers = ["订单编号","商品条码","数量","生产日期","失效日期"];
  25. foreach ($headers as $header){
  26. if (!isset($row[$header])){
  27. Cache::put("commodityAssign",["success"=>false, "data"=>"表头不存在“".$header."”"],86400);
  28. return false;
  29. }
  30. }
  31. $errors = [];
  32. $ids = [];
  33. foreach ($collection as $index=>$item){
  34. if ($item["生产日期"]) $item["生产日期"] = formatExcelDate($item["生产日期"]);
  35. if ($item["失效日期"]) $item["失效日期"] = formatExcelDate($item["失效日期"]);
  36. if (!$item["订单编号"]){
  37. $errors[] = "第“" . ($index + 2) . "”行订单编号为空";
  38. continue;
  39. }
  40. if (!$item["商品条码"]){
  41. $errors[] = "第“" . ($index + 2) . "”行商品条码为空";
  42. continue;
  43. }
  44. if (!$item["生产日期"] && !$item["失效日期"]){
  45. $errors[] = "第“" . ($index + 2) . "”行不存在日期";
  46. continue;
  47. }
  48. $detail = OracleDOCOrderDetail::query()->select("customerid","sku")
  49. ->where("orderno",$item["订单编号"])
  50. ->whereHas("sku",function ($query)use($item){
  51. /** @var Builder $query */
  52. $query->where("alternate_sku1",$item["商品条码"]);
  53. })->first();
  54. if (!$detail){
  55. $errors[] = "第“" . ($index + 2) . "”行未知订单商品";
  56. continue;
  57. }
  58. $sql = "select INV_LOT_LOC_ID.LOCATIONID,INV_LOT_LOC_ID.LOTNUM,BAS_LOCATION.PICKZONE,INV_LOT_LOC_ID.QTY from INV_LOT_ATT LEFT JOIN
  59. INV_LOT_LOC_ID ON INV_LOT_ATT.LOTNUM = INV_LOT_LOC_ID.LOTNUM LEFT JOIN BAS_LOCATION ON INV_LOT_LOC_ID.LOCATIONID = BAS_LOCATION.LOCATIONID
  60. where INV_LOT_ATT.LOTNUM in (select LOTNUM from INV_LOT_LOC_ID where CUSTOMERID = ? AND SKU = ? GROUP BY LOTNUM)";
  61. $bindings = [$detail->customerid,$detail->sku];
  62. if ($item["生产日期"]){
  63. $sql .= " AND LOTATT01 = ?";
  64. $bindings[] = $item["生产日期"];
  65. }else $sql .= " AND LOTATT01 IS NULL";
  66. if ($item["失效日期"]){
  67. $sql .= " AND LOTATT02 = ?";
  68. $bindings[] = $item["失效日期"];
  69. }else $sql .= " AND LOTATT02 IS NULL";
  70. $lot = DB::connection("oracle")->select(DB::raw($sql),$bindings);
  71. if (!$lot){
  72. $errors[] = "第“" . ($index + 2) . "”行未找到库位";
  73. continue;
  74. }
  75. $result = null;
  76. if (count($lot)>1){
  77. $min = null;
  78. $max = null;
  79. $map = [];
  80. foreach ($lot as $i => $l){
  81. $qty = (int)$l->quty;
  82. $map[$qty] = $i;
  83. if ($qty >= (int)$item["数量"] && $qty<=$max)$max = $qty;
  84. if ($qty < (int)$item["数量"] && $qty>=$min)$min = $qty;
  85. }
  86. if ($max !== null)$result = $lot[$map[$max]];
  87. else $result = $lot[$map[$min]];
  88. }
  89. if (count($lot) == 1)$result = $lot[0];
  90. if ($result){dd($result);
  91. try{
  92. $detail->update([
  93. "lotnum" => $result->lotnum,
  94. "pickzone" => $result->pickzone,
  95. "kitreferenceno" => '0',
  96. "d_edi_09" => '0',
  97. "d_edi_10" => '0',
  98. ]);
  99. DB::connection("oracle")->commit();
  100. LogService::log(__METHOD__,"SUCCESS-指定效期分配修改库位",json_encode($detail)." | ".json_encode($result));
  101. $order = app("OrderService")->first(["code"=>$item["订单编号"]]);
  102. if (!$order){
  103. $errors[] = "第“" . ($index + 2) . "”行已成功修改FLUX库位但在本地未找到订单";
  104. continue;
  105. }
  106. $barcode = app("CommodityBarcodeService")->first(["code"=>$item["商品条码"]]);
  107. if (!$barcode){
  108. $errors[] = "第“" . ($index + 2) . "”行已成功修改FLUX库位但在本地未找到条码";
  109. continue;
  110. }
  111. $model = app("OrderCommodityAssignService")->create([
  112. "order_id" => 1,//$order->id,
  113. "commodity_id" => 1,//$barcode->commodity_id,
  114. "amount" => $item["数量"],
  115. "produced_at" => $item["生产日期"],
  116. "valid_at" => $item["失效日期"],
  117. "batch_number" => $result->lotnum,
  118. "location" => $result->locationid,
  119. "region" => $result->pickzone,
  120. "user_id" => Auth::id(),
  121. ]);
  122. LogService::log(__METHOD__,"SUCCESS-生成配置记录",json_encode($model));
  123. $ids[] = $model->id;
  124. }catch (\Exception $e){
  125. LogService::log(__METHOD__,"ERROR-指定效期分配修改库位",json_encode($detail)." | ".json_encode($result));
  126. }
  127. }else $errors[] = "第“" . ($index + 2) . "”行未找到可分配库位";
  128. }
  129. $assigns = app("OrderCommodityAssignService")->get(["id"=>$ids]);
  130. $models = [];
  131. foreach ($assigns as $assign){
  132. $models[] = [
  133. "orderNumber" => $assign->order ? $assign->order->code : '',
  134. "barcode" => $assign->commodity ? $assign->commodity->barcode : '',
  135. "amount" => $assign->amount,
  136. "producedAt" => $assign->produced_at,
  137. "validAt" => $assign->valid_at,
  138. "batchNumber" => $assign->batch_number,
  139. "location" => $assign->location,
  140. "region" => $assign->region,
  141. "createdAt" => $assign->created_at,
  142. "userName" => $assign->user ? $assign->user->name : 'system',
  143. ];
  144. }
  145. Cache::put("commodityAssign",["success"=>true,"data"=>$models,"errors"=>$errors]);
  146. }
  147. }