StoreCheckingReceiveImport.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Imports;
  3. use App\Services\CommodityService;
  4. use Carbon\Carbon;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Cache;
  7. use Maatwebsite\Excel\Concerns\ToCollection;
  8. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  9. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  10. HeadingRowFormatter::default('none');
  11. class StoreCheckingReceiveImport implements ToCollection,WithHeadingRow
  12. {
  13. /** @var bool $isOverride
  14. * 是否覆盖
  15. */
  16. protected $isOverride;
  17. public function __construct($isOverride)
  18. {
  19. $this->isOverride=$isOverride ? true : false;
  20. }
  21. /**
  22. * @param Collection $collection
  23. * @return bool
  24. * @throws \Exception
  25. */
  26. public function collection(Collection $collection)
  27. {
  28. $row = $collection->first();
  29. $header = [
  30. "货主","SKU","商品名称","条码","数量","生产日期","失效日期","批号","唯一码"
  31. ];
  32. foreach ($header as $str){
  33. if (!($row[$str] ?? false)){
  34. Cache::put("storeCheckingReceive",["success"=>false, "data"=>"表头不存在“".$str."”"],86400);
  35. return false;
  36. }
  37. }
  38. $owner_name = null;
  39. $items = [];
  40. $errors = [];
  41. $commodities = [];
  42. $barcodes = [];
  43. //去重 筛选 错误
  44. foreach ($collection as $index => $item){
  45. if (!$owner_name && $item["货主"]) $owner_name = $item["货主"];
  46. if (!$item["条码"] || !$item["数量"] || !is_numeric($item["数量"])){
  47. array_push($errors,($index+2)." 行:条码或数量不存在");
  48. continue;
  49. }
  50. if ($item["生产日期"]){
  51. if (is_numeric($item["生产日期"])){
  52. $diff = intval($item["生产日期"]);
  53. $today=new Carbon('1900-01-01');
  54. $day = $today->addDays($diff-2);
  55. $item["生产日期"] = $day;
  56. }else{
  57. if (!(strtotime($item["生产日期"]) ? true : false)){
  58. array_push($errors,($index+2)." 行:生产日期格式错误");
  59. continue;
  60. }
  61. }
  62. }
  63. if ($item["失效日期"] && !(strtotime($item["失效日期"]) ? true : false)){
  64. if (is_numeric($item["失效日期"])){
  65. $diff = intval($item["失效日期"]);
  66. $today=new Carbon('1900-01-01');
  67. $day = $today->addDays($diff-2);
  68. $item["失效日期"] = $day;
  69. }else{
  70. if (!(strtotime($item["失效日期"]) ? true : false)){
  71. array_push($errors,($index+2)." 行:失效日期格式错误");
  72. continue;
  73. }
  74. }
  75. }
  76. $isUniqueCommodity = isset($commodities[$item["条码"]]);
  77. if ($isUniqueCommodity){
  78. $line = &$items[$commodities[$item["条码"]]];
  79. if ($line["produced_at"] == $item["生产日期"] && $line["invalid_at"] == $item["失效日期"]
  80. && $line["batch_code"] == $item["批号"] && $line["unique_code"] == $item["唯一码"]) {
  81. $line["imported_amount"] += $item["数量"];
  82. array_push($errors, ($index + 2) . " 行:重复数据已合并数量");
  83. continue;
  84. }
  85. }
  86. array_push($items,[
  87. "bin_number" => $item["格口号"],
  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. //使用条码获取商品ID
  112. $commodityBarCodes = app('commodityBarcodeService')->getCommodities($barcodes);
  113. foreach ($commodityBarCodes as $barcode){
  114. if (!is_array($commodities[$barcode->code])){
  115. $commodities[$barcode->code] = [$commodities[$barcode->code]];
  116. }
  117. foreach ($commodities[$barcode->code] as $index){
  118. $items[$index]["commodity_id"] = $barcode->commodity_id;
  119. unset($items[$index]["SKU"]);
  120. unset($items[$index]["商品名称"]);
  121. unset($items[$index]["条码"]);
  122. unset($items[$index]["lineno"]);
  123. }
  124. unset($commodities[$barcode->code]);
  125. }
  126. $owner = app('ownerService')->firstOrCreate(["name"=>$owner_name],["name"=>$owner_name, "code"=>$owner_name]);
  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)$commodityService->insert($commodities);
  186. if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  187. foreach ($commodities as $commodity){
  188. foreach ($skus[$commodity->sku] as $i => $index){
  189. $items[$index]["commodity_id"] = $commodity->id;
  190. if ($i == 0){
  191. array_push($barcodes,[
  192. "code" => $items[$index]["条码"],
  193. "commodity_id" => $commodity->id,
  194. "created_at" => date('Y-m-d H:i:s')
  195. ]);
  196. }
  197. unset($items[$index]["SKU"]);
  198. unset($items[$index]["商品名称"]);
  199. unset($items[$index]["条码"]);
  200. unset($items[$index]["lineno"]);
  201. }
  202. unset($skus[$commodity->sku]);
  203. }
  204. if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
  205. if (count($items) < 1){
  206. Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
  207. return false;
  208. }
  209. $storeCheckingReceive = app('storeCheckingReceiveService')->create([
  210. "owner_id" => $owner->id,
  211. "created_at" => date('Y-m-d H:i:s'),
  212. 'status' => "已导入",
  213. ]);
  214. $storeCheckingReceive->owner_name = $owner_name;
  215. foreach ($items as &$item){
  216. $item["store_checking_receive_id"] = $storeCheckingReceive->id;
  217. }
  218. app('storeCheckingReceiveItemService')->insert($items);
  219. Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
  220. return true;
  221. }
  222. }