StoreCheckingReceiveImport.php 12 KB

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