StoreCheckingReceiveImport.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. namespace App\Imports;
  3. use App\Http\Controllers\CommodityController;
  4. use App\Services\CommodityBarcodeService;
  5. use App\Services\CommodityService;
  6. use Carbon\Carbon;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use Maatwebsite\Excel\Concerns\ToCollection;
  10. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  11. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  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')->first(["code"=>$owner_name,"name"=>$owner_name],["name"=>"or"]);
  109. if (!$owner) $owner = app('OwnerService')->create(["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->toArray())) 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. app('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()->toDateTimeString();
  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. app('LogService')->log(__METHOD__,"盘收录入临时商品",json_encode($createCommodities,JSON_UNESCAPED_UNICODE));
  205. //拿到商品ID录入条码
  206. $barcodes = [];
  207. $date = Carbon::now()->toDateTimeString();
  208. $commoditiesTem = $commodityService->get(['owner_id'=>$owner->id, 'sku'=>$skus]);
  209. $exist = [];
  210. foreach ($commoditiesTem as $item){
  211. foreach ($skuMap[$item->sku] as $index){
  212. if (!isset($exist[$item->id."_".$items[$index]["条码"]])){
  213. $barcodes[] = [
  214. 'commodity_id' => $item->id,
  215. 'code' => $items[$index]["条码"],
  216. 'created_at' => $date,
  217. ];
  218. $exist[$item->id."_".$items[$index]["条码"]] = true;
  219. }
  220. $items[$index]["commodity_id"] = $item->id;
  221. unset($items[$index]["SKU"]);
  222. unset($items[$index]["商品名称"]);
  223. unset($items[$index]["条码"]);
  224. }
  225. }
  226. if (count($barcodes) > 0){
  227. $commodityBarcodeService->insert($barcodes);
  228. app('LogService')->log(__METHOD__,"盘收导入录入条码",json_encode($barcodes));
  229. }
  230. }
  231. }
  232. }
  233. if (count($items) < 1){
  234. Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
  235. return false;
  236. }
  237. $storeCheckingReceive = app('StoreCheckingReceiveService')->create([
  238. "owner_id" => $owner->id,
  239. "created_at" => date('Y-m-d H:i:s'),
  240. 'status' => "已导入",
  241. ]);
  242. $storeCheckingReceive->owner_name = $owner->name;
  243. app('LogService')->log(__METHOD__,"导入盘收任务-录入盘收任务",json_encode($storeCheckingReceive,JSON_UNESCAPED_UNICODE));
  244. foreach ($items as &$it){
  245. $it["store_checking_receive_id"] = $storeCheckingReceive->id;
  246. }
  247. app('StoreCheckingReceiveItemService')->insert($items);
  248. app('LogService')->log(__METHOD__,"导入盘收任务-批量录入盘收记录",json_encode($items,JSON_UNESCAPED_UNICODE));
  249. Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
  250. return true;
  251. }
  252. }