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