StoreCheckingReceiveImport.php 11 KB

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