StoreCheckingReceiveImport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. "lineno" => $index+2,
  96. ]);
  97. if ($isUniqueCommodity){
  98. array_push($commodities[$item["条码"]], count($items)-1);
  99. }else{
  100. $commodities[$item["条码"]] = [count($items)-1] ;
  101. array_push($barcodes,$item["条码"]);
  102. }
  103. }
  104. //不存在货主
  105. if (!$owner_name){
  106. Cache::put("storeCheckingReceive",["success"=>false, "data"=>"货主为空,不允许录入,至少为一行标明货主!"],86400);
  107. return false;
  108. }
  109. $owner = app('ownerService')->firstOrCreate(["name"=>$owner_name],["name"=>$owner_name, "code"=>$owner_name]);
  110. //使用条码同步
  111. $commodityController = new CommodityController();
  112. $commodityController->syncOwnerCommodities($owner->id, $owner->code, null,$barcodes);
  113. //使用条码获取商品ID,筛选出需要录入条码项
  114. /** @var CommodityBarcodeService $commodityBarcodeService */
  115. $commodityBarcodeService = app('commodityBarcodeService');
  116. $commodityBarCodes = $commodityBarcodeService->getCommodities($barcodes, $owner->id);
  117. foreach ($commodityBarCodes as $barcode){
  118. foreach ($commodities[$barcode->code] as $index){
  119. $items[$index]["commodity_id"] = $barcode->commodity_id;
  120. unset($items[$index]["SKU"]);
  121. unset($items[$index]["商品名称"]);
  122. unset($items[$index]["条码"]);
  123. unset($items[$index]["lineno"]);
  124. }
  125. unset($commodities[$barcode->code]);
  126. }
  127. if (count($commodities) > 0){
  128. //获取SKU,将已存在的商品补录条码
  129. $skus = [];
  130. $skuMap = [];
  131. foreach ($commodities as $arr){
  132. foreach ($arr as $index){
  133. $item = $items[$index];
  134. if ($item['SKU']){
  135. $skus[] = $item['SKU'];
  136. //sku对应多个条码
  137. if (isset($skuMap[$item['SKU']]))$skuMap[$item['SKU']][] = $item['条码'];
  138. else $skuMap[$item['SKU']] = [$item['条码']];
  139. } else {
  140. $skus[] = $item['条码'];
  141. //sku对应多个条码
  142. if (isset($skuMap[$item['条码']]))$skuMap[$item['SKU']][] = $item['条码'];
  143. else $skuMap[$item['条码']] = [$item['条码']];
  144. }
  145. }
  146. }
  147. $commoditiesTem = app('commodityService')->get(['owner_id'=>$owner->id, 'sku'=>$skus]);
  148. if (count($commoditiesTem) > 0){
  149. $barcodes = [];
  150. $date = Carbon::now();
  151. foreach ($commoditiesTem as $item){
  152. //对比本地与导入的存在商品中条码差异 补充数据库
  153. foreach (array_unique(array_diff($skuMap[$item->sku], $item->barcodes)) as $code){
  154. $barcodes[] = [
  155. 'commodity_id' => $item->id,
  156. 'code' => $code,
  157. 'created_at' => $date,
  158. ];
  159. foreach ($commodities[$code] as $index){
  160. $items[$index]["commodity_id"] = $item->id;
  161. unset($items[$index]["SKU"]);
  162. unset($items[$index]["商品名称"]);
  163. unset($items[$index]["条码"]);
  164. unset($items[$index]["lineno"]);
  165. }
  166. unset($commodities[$code]);
  167. }
  168. }
  169. if (count($barcodes) > 0){
  170. $commodityBarcodeService->insert($barcodes);
  171. LogService::log(__METHOD__,"盘收导入补录条码",json_encode($barcodes));
  172. }
  173. }
  174. //WMS与本地皆不存在的商品 录入为临时商品
  175. if (count($commodities) > 0){
  176. $createCommodities = [];
  177. $skus = [];
  178. $skuMap = [];
  179. $date = Carbon::now();
  180. foreach ($commodities as $arr){
  181. foreach ($arr as $index){
  182. $item = $items[$index];
  183. if ($item["SKU"]) $sku = $item["SKU"];
  184. else $sku = $item["条码"];
  185. if (isset($skuMap[$sku])) $skuMap[$sku][] = $index;
  186. else {
  187. $skuMap[$sku] = [$index];
  188. $skus = $sku;
  189. $commodity = [
  190. "name" => $item["商品名称"],
  191. "sku" => $sku,
  192. "owner_id" => $owner->id,
  193. "type" => "临时",
  194. "created_at" => $date,
  195. ];
  196. $createCommodities[] = $commodity;
  197. }
  198. }
  199. }
  200. //录入商品
  201. if (count($createCommodities) > 0){
  202. /** @var CommodityService $commodityService */
  203. $commodityService = app('commodityService');
  204. $commodityService->insert($createCommodities);
  205. LogService::log(__METHOD__,"盘收录入临时商品",json_encode($createCommodities,JSON_UNESCAPED_UNICODE));
  206. //拿到商品ID录入条码
  207. $barcodes = [];
  208. $date = Carbon::now();
  209. $commoditiesTem = $commodityService->get(['owner_id'=>$owner->id, 'sku'=>$skus]);
  210. foreach ($commoditiesTem as $item){
  211. foreach ($skuMap[$item->sku] as $index){
  212. $barcodes[] = [
  213. 'commodity_id' => $item->id,
  214. 'code' => $items[$index]["条码"],
  215. 'created_at' => $date,
  216. ];
  217. $items[$index]["commodity_id"] = $item->id;
  218. unset($items[$index]["SKU"]);
  219. unset($items[$index]["商品名称"]);
  220. unset($items[$index]["条码"]);
  221. unset($items[$index]["lineno"]);
  222. }
  223. }
  224. if (count($barcodes) > 0){
  225. $commodityBarcodeService->insert(array_unique($barcodes));
  226. LogService::log(__METHOD__,"盘收导入录入条码",json_encode($barcodes));
  227. }
  228. }
  229. }
  230. }
  231. if (count($items) < 1){
  232. Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
  233. return false;
  234. }
  235. $storeCheckingReceive = app('storeCheckingReceiveService')->create([
  236. "owner_id" => $owner->id,
  237. "created_at" => date('Y-m-d H:i:s'),
  238. 'status' => "已导入",
  239. ]);
  240. LogService::log(__METHOD__,"导入盘收任务-录入盘收任务",json_encode($storeCheckingReceive,JSON_UNESCAPED_UNICODE));
  241. $storeCheckingReceive->owner_name = $owner_name;
  242. foreach ($items as &$item){
  243. $item["store_checking_receive_id"] = $storeCheckingReceive->id;
  244. }
  245. app('storeCheckingReceiveItemService')->insert($items);
  246. LogService::log(__METHOD__,"导入盘收任务-批量录入盘收记录",json_encode($items,JSON_UNESCAPED_UNICODE));
  247. Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
  248. return true;
  249. }
  250. }