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