StoreCheckingReceiveImport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\Arr;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Cache;
  11. use Maatwebsite\Excel\Concerns\ToCollection;
  12. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  13. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  14. HeadingRowFormatter::default('none');
  15. class StoreCheckingReceiveImport implements ToCollection,WithHeadingRow
  16. {
  17. /**
  18. * @param Collection $collection
  19. * @return bool
  20. * @throws \Exception
  21. */
  22. public function collection(Collection $collection)
  23. {
  24. $row = $collection->first();
  25. $header = [
  26. "货主","SKU","商品名称","条码","数量","生产日期","失效日期","批号","唯一码"
  27. ];
  28. foreach ($header as $str){
  29. if (!isset($row[$str])){
  30. Cache::put("storeCheckingReceive",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  31. return false;
  32. }
  33. }
  34. $owner_name = null;
  35. $items = [];
  36. $errors = [];
  37. $commodities = [];
  38. $barcodes = [];
  39. //去重 筛选 错误
  40. foreach ($collection as $index => $item){
  41. if (!$owner_name && $item["货主"]) $owner_name = $item["货主"];
  42. if (!$item["条码"] || !is_numeric($item["数量"])){
  43. array_push($errors,($index+2)." 行:条码或数量不存在");
  44. continue;
  45. }
  46. if ($item["生产日期"]){
  47. if (is_numeric($item["生产日期"])){
  48. $diff = intval($item["生产日期"]);
  49. $today=new Carbon('1900-01-01');
  50. $day = $today->addDays($diff-2);
  51. $item["生产日期"] = $day;
  52. }else{
  53. if (!(strtotime($item["生产日期"]) ? true : false)){
  54. array_push($errors,($index+2)." 行:生产日期格式错误");
  55. continue;
  56. }
  57. }
  58. }
  59. if ($item["失效日期"] && !(strtotime($item["失效日期"]) ? true : false)){
  60. if (is_numeric($item["失效日期"])){
  61. $diff = intval($item["失效日期"]);
  62. $today=new Carbon('1900-01-01');
  63. $day = $today->addDays($diff-2);
  64. $item["失效日期"] = $day;
  65. }else{
  66. if (!(strtotime($item["失效日期"]) ? true : false)){
  67. array_push($errors,($index+2)." 行:失效日期格式错误");
  68. continue;
  69. }
  70. }
  71. }
  72. $isUniqueCommodity = isset($commodities[$item["条码"]]);
  73. if ($isUniqueCommodity){
  74. $sign = false;
  75. foreach ($commodities[$item["条码"]] as $i){
  76. $line = &$items[$i];
  77. if ($line["produced_at"] == $item["生产日期"] && $line["invalid_at"] == $item["失效日期"]
  78. && $line["batch_code"] == $item["批号"] && $line["unique_code"] == $item["唯一码"]) {
  79. $line["imported_amount"] += $item["数量"];
  80. array_push($errors, ($index + 2) . " 行:重复数据已合并数量");
  81. $sign = true;
  82. break;
  83. }
  84. }
  85. if ($sign) continue;
  86. }
  87. array_push($items,[
  88. "imported_amount" => $item["数量"],
  89. "produced_at" => $item["生产日期"],
  90. "invalid_at" => $item["失效日期"],
  91. "batch_code" => $item["批号"],
  92. "unique_code" => $item["唯一码"],
  93. "SKU" => $item["SKU"],
  94. "商品名称" => $item["商品名称"],
  95. "条码" => $item["条码"],
  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. if(!isset($commodities[$barcode->code]))continue;
  119. foreach ($commodities[$barcode->code] as $index){
  120. $items[$index]["commodity_id"] = $barcode->commodity_id;
  121. unset($items[$index]["SKU"]);
  122. unset($items[$index]["商品名称"]);
  123. unset($items[$index]["条码"]);
  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. }
  165. unset($commodities[$code]);
  166. }
  167. }
  168. if (count($barcodes) > 0){
  169. $commodityBarcodeService->insert($barcodes);
  170. LogService::log(__METHOD__,"盘收导入补录条码",json_encode($barcodes));
  171. }
  172. }
  173. //WMS与本地皆不存在的商品 录入为临时商品
  174. if (count($commodities) > 0){
  175. $createCommodities = [];
  176. $skus = [];
  177. $skuMap = [];
  178. $date = Carbon::now();
  179. foreach ($commodities as $arr){
  180. foreach ($arr as $index){
  181. $item = $items[$index];
  182. if ($item["SKU"]) $sku = $item["SKU"];
  183. else $sku = $item["条码"];
  184. if (isset($skuMap[$sku])) $skuMap[$sku][] = $index;
  185. else {
  186. $skuMap[$sku] = [$index];
  187. $skus = $sku;
  188. $commodity = [
  189. "name" => $item["商品名称"],
  190. "sku" => $sku,
  191. "owner_id" => $owner->id,
  192. "type" => "临时",
  193. "created_at" => $date,
  194. ];
  195. $createCommodities[] = $commodity;
  196. }
  197. }
  198. }
  199. //录入商品
  200. if (count($createCommodities) > 0){
  201. /** @var CommodityService $commodityService */
  202. $commodityService = app('CommodityService');
  203. $commodityService->insert($createCommodities);
  204. LogService::log(__METHOD__,"盘收录入临时商品",json_encode($createCommodities,JSON_UNESCAPED_UNICODE));
  205. //拿到商品ID录入条码
  206. $barcodes = [];
  207. $date = Carbon::now();
  208. $commoditiesTem = $commodityService->get(['owner_id'=>$owner->id, 'sku'=>$skus]);
  209. foreach ($commoditiesTem as $item){
  210. foreach ($skuMap[$item->sku] as $index){
  211. $barcodes[] = [
  212. 'commodity_id' => $item->id,
  213. 'code' => $items[$index]["条码"],
  214. 'created_at' => $date,
  215. ];
  216. $items[$index]["commodity_id"] = $item->id;
  217. unset($items[$index]["SKU"]);
  218. unset($items[$index]["商品名称"]);
  219. unset($items[$index]["条码"]);
  220. }
  221. }
  222. if (count($barcodes) > 0){
  223. $commodityBarcodeService->insert(array_unique($barcodes));
  224. LogService::log(__METHOD__,"盘收导入录入条码",json_encode($barcodes));
  225. }
  226. }
  227. }
  228. }
  229. if (count($items) < 1){
  230. Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
  231. return false;
  232. }
  233. $storeCheckingReceive = app('StoreCheckingReceiveService')->create([
  234. "owner_id" => $owner->id,
  235. "created_at" => date('Y-m-d H:i:s'),
  236. 'status' => "已导入",
  237. ]);
  238. LogService::log(__METHOD__,"导入盘收任务-录入盘收任务",json_encode($storeCheckingReceive,JSON_UNESCAPED_UNICODE));
  239. $storeCheckingReceive->owner_name = $owner_name;
  240. foreach ($items as &$item){
  241. $item["store_checking_receive_id"] = $storeCheckingReceive->id;
  242. }
  243. app('StoreCheckingReceiveItemService')->insert($items);
  244. LogService::log(__METHOD__,"导入盘收任务-批量录入盘收记录",json_encode($items,JSON_UNESCAPED_UNICODE));
  245. Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
  246. return true;
  247. }
  248. }