StoreCheckingReceiveImport.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Imports;
  3. use App\Services\CommodityService;
  4. use App\Services\LogService;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use Maatwebsite\Excel\Concerns\ToCollection;
  9. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  10. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  11. HeadingRowFormatter::default('none');
  12. class StoreCheckingReceiveImport implements ToCollection,WithHeadingRow
  13. {
  14. /** @var bool $isOverride
  15. * 是否覆盖
  16. */
  17. protected $isOverride;
  18. public function __construct($isOverride)
  19. {
  20. $this->isOverride=$isOverride ? true : false;
  21. }
  22. /**
  23. * @param Collection $collection
  24. * @return bool
  25. * @throws \Exception
  26. */
  27. public function collection(Collection $collection)
  28. {
  29. $row = $collection->first();
  30. $header = [
  31. "货主","SKU","商品名称","条码","数量","生产日期","失效日期","批号","唯一码"
  32. ];
  33. foreach ($header as $str){
  34. if (!isset($row[$str])){
  35. Cache::put("storeCheckingReceive",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  36. return false;
  37. }
  38. }
  39. $owner_name = null;
  40. $items = [];
  41. $errors = [];
  42. $commodities = [];
  43. $barcodes = [];
  44. //去重 筛选 错误
  45. foreach ($collection as $index => $item){
  46. if (!$owner_name && $item["货主"]) $owner_name = $item["货主"];
  47. if (!$item["条码"] || !$item["数量"] || !is_numeric($item["数量"])){
  48. array_push($errors,($index+2)." 行:条码或数量不存在");
  49. continue;
  50. }
  51. if ($item["生产日期"]){
  52. if (is_numeric($item["生产日期"])){
  53. $diff = intval($item["生产日期"]);
  54. $today=new Carbon('1900-01-01');
  55. $day = $today->addDays($diff-2);
  56. $item["生产日期"] = $day;
  57. }else{
  58. if (!(strtotime($item["生产日期"]) ? true : false)){
  59. array_push($errors,($index+2)." 行:生产日期格式错误");
  60. continue;
  61. }
  62. }
  63. }
  64. if ($item["失效日期"] && !(strtotime($item["失效日期"]) ? true : false)){
  65. if (is_numeric($item["失效日期"])){
  66. $diff = intval($item["失效日期"]);
  67. $today=new Carbon('1900-01-01');
  68. $day = $today->addDays($diff-2);
  69. $item["失效日期"] = $day;
  70. }else{
  71. if (!(strtotime($item["失效日期"]) ? true : false)){
  72. array_push($errors,($index+2)." 行:失效日期格式错误");
  73. continue;
  74. }
  75. }
  76. }
  77. $isUniqueCommodity = isset($commodities[$item["条码"]]);
  78. if ($isUniqueCommodity){
  79. $line = &$items[$commodities[$item["条码"]]];
  80. if ($line["produced_at"] == $item["生产日期"] && $line["invalid_at"] == $item["失效日期"]
  81. && $line["batch_code"] == $item["批号"] && $line["unique_code"] == $item["唯一码"]) {
  82. $line["imported_amount"] += $item["数量"];
  83. array_push($errors, ($index + 2) . " 行:重复数据已合并数量");
  84. continue;
  85. }
  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. "lineno" => $index+2,
  97. ]);
  98. if ($isUniqueCommodity){
  99. if (is_array($commodities[$item["条码"]])) array_push($commodities[$item["条码"]], count($items)-1);
  100. else $commodities[$item["条码"]] = [$commodities[$item["条码"]], count($items)-1];
  101. }else{
  102. $commodities[$item["条码"]] = count($items)-1 ;
  103. array_push($barcodes,$item["条码"]);
  104. }
  105. }
  106. //不存在货主
  107. if (!$owner_name){
  108. Cache::put("storeCheckingReceive",["success"=>false, "data"=>"货主为空,不允许录入,至少为一行标明货主!"],86400);
  109. return false;
  110. }
  111. $owner = app('ownerService')->firstOrCreate(["name"=>$owner_name],["name"=>$owner_name, "code"=>$owner_name]);
  112. //使用条码获取商品ID
  113. $commodityBarCodes = app('commodityBarcodeService')->getCommodities($barcodes, $owner->id);
  114. foreach ($commodityBarCodes as $barcode){
  115. if (!is_array($commodities[$barcode->code])){
  116. $commodities[$barcode->code] = [$commodities[$barcode->code]];
  117. }
  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. unset($items[$index]["lineno"]);
  124. }
  125. unset($commodities[$barcode->code]);
  126. }
  127. //填充商品ID,未找到项流转下一步,空SKU删除该行并返回错误
  128. $skus = [];
  129. $skuParam = [];
  130. foreach ($commodities as $key => $value){
  131. if (!is_array($value)){
  132. $value = [$value];
  133. }
  134. foreach ($value as $i => $index){
  135. if (!$items[$index]["SKU"]){
  136. array_push($errors, ($items[$index]["lineno"]) . " 行:条码未找到商品时SKU不得为空");
  137. unset($items[$index]);
  138. if ($i == count($value)-1) unset($commodities[$key]);
  139. continue;
  140. }
  141. if (isset($skus[$items[$index]["SKU"]])) array_push($skus, $index);
  142. else{
  143. $skus[$items[$index]["SKU"]] = [$index];
  144. array_push($skuParam,$items[$index]["SKU"]);
  145. }
  146. }
  147. }
  148. //使用货主+SKU获取商品ID,将条码补录
  149. $barcodes = [];
  150. /** @var CommodityService $commodityService */
  151. $commodityService = app('commodityService');
  152. if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  153. foreach ($commodities as $commodity){
  154. foreach ($skus[$commodity->sku] as $i => $index){
  155. $items[$index]["commodity_id"] = $commodity->id;
  156. if ($i == 0){
  157. array_push($barcodes,[
  158. "code" => $items[$index]["条码"],
  159. "commodity_id" => $commodity->id,
  160. "created_at" => date('Y-m-d H:i:s')
  161. ]);
  162. }
  163. unset($items[$index]["SKU"]);
  164. unset($items[$index]["商品名称"]);
  165. unset($items[$index]["条码"]);
  166. unset($items[$index]["lineno"]);
  167. }
  168. unset($skus[$commodity->sku]);
  169. }
  170. if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
  171. //未找到商品录入商品
  172. $commodities = [];
  173. $skuParam = [];
  174. $barcodes = [];
  175. foreach ($skus as $sku => $arr){
  176. $data = &$items[$arr[0]];
  177. array_push($commodities,[
  178. "name" => $data["商品名称"],
  179. "sku" => $data["SKU"],
  180. "owner_id" => $owner->id,
  181. "created_at" => date('Y-m-d H:i:s'),
  182. ]);
  183. array_push($skuParam, $sku);
  184. }
  185. if (count($commodities) > 0){
  186. $commodityService->insert($commodities);
  187. LogService::log(__METHOD__,"导入盘收任务-批量录入商品",json_encode($commodities,JSON_UNESCAPED_UNICODE));
  188. }
  189. if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  190. foreach ($commodities as $commodity){
  191. foreach ($skus[$commodity->sku] as $i => $index){
  192. $items[$index]["commodity_id"] = $commodity->id;
  193. if ($i == 0){
  194. array_push($barcodes,[
  195. "code" => $items[$index]["条码"],
  196. "commodity_id" => $commodity->id,
  197. "created_at" => date('Y-m-d H:i:s')
  198. ]);
  199. }
  200. unset($items[$index]["SKU"]);
  201. unset($items[$index]["商品名称"]);
  202. unset($items[$index]["条码"]);
  203. unset($items[$index]["lineno"]);
  204. }
  205. unset($skus[$commodity->sku]);
  206. }
  207. if (count($barcodes) > 0){
  208. app('commodityBarcodeService')->insert($barcodes);
  209. LogService::log(__METHOD__,"导入盘收任务-批量录入商品条码",json_encode($barcodes,JSON_UNESCAPED_UNICODE));
  210. }
  211. if (count($items) < 1){
  212. Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
  213. return false;
  214. }
  215. $storeCheckingReceive = app('storeCheckingReceiveService')->create([
  216. "owner_id" => $owner->id,
  217. "created_at" => date('Y-m-d H:i:s'),
  218. 'status' => "已导入",
  219. ]);
  220. LogService::log(__METHOD__,"导入盘收任务-录入盘收任务",json_encode($storeCheckingReceive,JSON_UNESCAPED_UNICODE));
  221. $storeCheckingReceive->owner_name = $owner_name;
  222. foreach ($items as &$item){
  223. $item["store_checking_receive_id"] = $storeCheckingReceive->id;
  224. }
  225. app('storeCheckingReceiveItemService')->insert($items);
  226. LogService::log(__METHOD__,"导入盘收任务-批量录入盘收记录",json_encode($items,JSON_UNESCAPED_UNICODE));
  227. Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
  228. return true;
  229. }
  230. }