StoreCheckingReceiveImport.php 10 KB

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