StoreCheckingReceiveImport.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Imports;
  3. use App\Services\CommodityService;
  4. use Illuminate\Support\Collection;
  5. use Maatwebsite\Excel\Concerns\ToCollection;
  6. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  7. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  8. HeadingRowFormatter::default('none');
  9. class StoreCheckingReceiveImport implements ToCollection,WithHeadingRow
  10. {
  11. /** @var bool $isOverride
  12. * 是否覆盖
  13. */
  14. protected $isOverride;
  15. public function __construct($isOverride)
  16. {
  17. $this->isOverride=$isOverride ? true : false;
  18. }
  19. /**
  20. * @param Collection $collection
  21. * @return array
  22. */
  23. public function collection(Collection $collection)
  24. {
  25. $row = $collection->first();
  26. $header = [
  27. "货主","SKU","商品名称","条码","数量","生产日期","失效日期","批号","唯一码"
  28. ];
  29. foreach ($header as $str){
  30. if (!($row[$str] ?? false)) return ["success"=>false, "data"=>"表头不存在“".$str."”"];
  31. }
  32. $owner_name = null;
  33. $items = [];
  34. $errors = [];
  35. $commodities = [];
  36. $barcodes = [];
  37. //去重 筛选 错误
  38. foreach ($collection as $index => $item){
  39. if (!$owner_name && $item["货主"]) $owner_name = $item["货主"];
  40. if (!$item["条码"] || !$item["数量"] || !is_numeric($item["数量"])){
  41. array_push($errors,($index+2)." 行:条码或数量不存在");
  42. continue;
  43. }
  44. if ($item["生产日期"] && !(strtotime($item["生产日期"]) ? true : false)){
  45. array_push($errors,($index+2)." 行:生产日期格式错误");
  46. continue;
  47. }
  48. if ($item["失效日期"] && !(strtotime($item["失效日期"]) ? true : false)){
  49. array_push($errors,($index+2)." 行:失效日期格式错误");
  50. continue;
  51. }
  52. $isUniqueCommodity = isset($commodities[$item["条码"]]);
  53. if ($isUniqueCommodity){
  54. $line = &$items[$commodities[$item["条码"]]];
  55. if ($line["produced_at"] == $item["生产日期"] && $line["invalid_at"] == $item["失效日期"]
  56. && $line["batch_code"] == $item["批号"] && $line["unique_code"] == $item["唯一码"]) {
  57. $line["imported_amount"] += $item["数量"];
  58. array_push($errors, ($index + 2) . " 行:重复数据已合并数量");
  59. continue;
  60. }
  61. }
  62. array_push($items,[
  63. "imported_amount" => $item["数量"],
  64. "produced_at" => $item["生产日期"],
  65. "invalid_at" => $item["失效日期"],
  66. "batch_code" => $item["批号"],
  67. "unique_code" => $item["唯一码"],
  68. "SKU" => $item["SKU"],
  69. "商品名称" => $item["商品名称"],
  70. "条码" => $item["条码"],
  71. "lineno" => $index+2,
  72. ]);
  73. if ($isUniqueCommodity){
  74. if (is_array($commodities[$item["条码"]])) array_push($commodities[$item["条码"]], count($items)-1);
  75. else $commodities[$item["条码"]] = [$commodities[$item["条码"]], count($items)-1];
  76. }else{
  77. $commodities[$item["条码"]] = count($items)-1 ;
  78. array_push($barcodes,$item["条码"]);
  79. }
  80. }
  81. //不存在货主
  82. if (!$owner_name) return ["success"=>false, "data"=>"货主为空,不允许录入,至少为一行标明货主!"];
  83. //使用条码获取商品ID
  84. $commodityBarCodes = app('commodityBarcodeService')->getCommodities($barcodes);
  85. foreach ($commodityBarCodes as $barcode){
  86. if (!is_array($commodities[$barcode->code])){
  87. $commodities[$barcode->code] = [$commodities[$barcode->code]];
  88. }
  89. foreach ($commodities[$barcode->code] as $index){
  90. $items[$index]["commodity_id"] = $barcode->commodity_id;
  91. unset($items[$index]["SKU"]);
  92. unset($items[$index]["商品名称"]);
  93. unset($items[$index]["条码"]);
  94. unset($items[$index]["lineno"]);
  95. }
  96. unset($commodities[$barcode->code]);
  97. }
  98. $owner = app('ownerService')->firstOrCreate(["name"=>$owner_name],["name"=>$owner_name, "code"=>$owner_name]);
  99. //填充商品ID,未找到项流转下一步,空SKU删除该行并返回错误
  100. $skus = [];
  101. $skuParam = [];
  102. foreach ($commodities as $key => $value){
  103. if (!is_array($value)){
  104. $value = [$value];
  105. }
  106. foreach ($value as $i => $index){
  107. if (!$items[$index]["SKU"]){
  108. array_push($errors, ($items[$index]["lineno"]) . " 行:条码未找到商品时SKU不得为空");
  109. unset($items[$index]);
  110. if ($i == count($value)-1) unset($commodities[$key]);
  111. continue;
  112. }
  113. if (isset($skus[$items[$index]["SKU"]])) array_push($skus, $index);
  114. else{
  115. $skus[$items[$index]["SKU"]] = [$index];
  116. array_push($skuParam,$items[$index]["SKU"]);
  117. }
  118. }
  119. }
  120. //使用货主+SKU获取商品ID,将条码补录
  121. $barcodes = [];
  122. /** @var CommodityService $commodityService */
  123. $commodityService = app('commodityService');
  124. $commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  125. foreach ($commodities as $commodity){
  126. foreach ($skus[$commodity->sku] as $i => $index){
  127. $items[$index]["commodity_id"] = $commodity->id;
  128. if ($i == 0){
  129. array_push($barcodes,[
  130. "code" => $items[$index]["条码"],
  131. "commodity_id" => $commodity->id,
  132. "created_at" => date('Y-m-d H:i:s')
  133. ]);
  134. }
  135. unset($items[$index]["SKU"]);
  136. unset($items[$index]["商品名称"]);
  137. unset($items[$index]["条码"]);
  138. unset($items[$index]["lineno"]);
  139. }
  140. unset($skus[$commodity->sku]);
  141. }
  142. if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
  143. //未找到商品录入商品
  144. $commodities = [];
  145. $skuParam = [];
  146. foreach ($skus as $sku => $arr){
  147. $data = &$items[$arr[0]];
  148. array_push($commodities,[
  149. "name" => $data["商品名称"],
  150. "sku" => $data["SKU"],
  151. "owner_id" => $owner->id,
  152. "created_at" => date('Y-m-d H:i:s'),
  153. ]);
  154. array_push($skuParam, $sku);
  155. }
  156. $commodityService->insert($commodities);
  157. $commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  158. foreach ($commodities as $commodity){
  159. foreach ($skus[$commodity->sku] as $i => $index){
  160. $items[$index]["commodity_id"] = $commodity->id;
  161. if ($i == 0){
  162. array_push($barcodes,[
  163. "code" => $items[$index]["条码"],
  164. "commodity_id" => $commodity->id,
  165. "created_at" => date('Y-m-d H:i:s')
  166. ]);
  167. }
  168. unset($items[$index]["SKU"]);
  169. unset($items[$index]["商品名称"]);
  170. unset($items[$index]["条码"]);
  171. unset($items[$index]["lineno"]);
  172. }
  173. unset($skus[$commodity->sku]);
  174. }
  175. }
  176. }