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