StoreCheckingReceiveImport.php 10 KB

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