StoreCheckingReceiveImport.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 (!isset($row[$str])){
  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. "imported_amount" => $item["数量"],
  88. "produced_at" => $item["生产日期"],
  89. "invalid_at" => $item["失效日期"],
  90. "batch_code" => $item["批号"],
  91. "unique_code" => $item["唯一码"],
  92. "SKU" => $item["SKU"],
  93. "商品名称" => $item["商品名称"],
  94. "条码" => $item["条码"],
  95. "lineno" => $index+2,
  96. ]);
  97. if ($isUniqueCommodity){
  98. if (is_array($commodities[$item["条码"]])) array_push($commodities[$item["条码"]], count($items)-1);
  99. else $commodities[$item["条码"]] = [$commodities[$item["条码"]], count($items)-1];
  100. }else{
  101. $commodities[$item["条码"]] = count($items)-1 ;
  102. array_push($barcodes,$item["条码"]);
  103. }
  104. }
  105. //不存在货主
  106. if (!$owner_name){
  107. Cache::put("storeCheckingReceive",["success"=>false, "data"=>"货主为空,不允许录入,至少为一行标明货主!"],86400);
  108. return false;
  109. }
  110. //使用条码获取商品ID
  111. $commodityBarCodes = app('commodityBarcodeService')->getCommodities($barcodes);
  112. foreach ($commodityBarCodes as $barcode){
  113. if (!is_array($commodities[$barcode->code])){
  114. $commodities[$barcode->code] = [$commodities[$barcode->code]];
  115. }
  116. foreach ($commodities[$barcode->code] as $index){
  117. $items[$index]["commodity_id"] = $barcode->commodity_id;
  118. unset($items[$index]["SKU"]);
  119. unset($items[$index]["商品名称"]);
  120. unset($items[$index]["条码"]);
  121. unset($items[$index]["lineno"]);
  122. }
  123. unset($commodities[$barcode->code]);
  124. }
  125. $owner = app('ownerService')->firstOrCreate(["name"=>$owner_name],["name"=>$owner_name, "code"=>$owner_name]);
  126. //填充商品ID,未找到项流转下一步,空SKU删除该行并返回错误
  127. $skus = [];
  128. $skuParam = [];
  129. foreach ($commodities as $key => $value){
  130. if (!is_array($value)){
  131. $value = [$value];
  132. }
  133. foreach ($value as $i => $index){
  134. if (!$items[$index]["SKU"]){
  135. array_push($errors, ($items[$index]["lineno"]) . " 行:条码未找到商品时SKU不得为空");
  136. unset($items[$index]);
  137. if ($i == count($value)-1) unset($commodities[$key]);
  138. continue;
  139. }
  140. if (isset($skus[$items[$index]["SKU"]])) array_push($skus, $index);
  141. else{
  142. $skus[$items[$index]["SKU"]] = [$index];
  143. array_push($skuParam,$items[$index]["SKU"]);
  144. }
  145. }
  146. }
  147. //使用货主+SKU获取商品ID,将条码补录
  148. $barcodes = [];
  149. /** @var CommodityService $commodityService */
  150. $commodityService = app('commodityService');
  151. if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  152. foreach ($commodities as $commodity){
  153. foreach ($skus[$commodity->sku] as $i => $index){
  154. $items[$index]["commodity_id"] = $commodity->id;
  155. if ($i == 0){
  156. array_push($barcodes,[
  157. "code" => $items[$index]["条码"],
  158. "commodity_id" => $commodity->id,
  159. "created_at" => date('Y-m-d H:i:s')
  160. ]);
  161. }
  162. unset($items[$index]["SKU"]);
  163. unset($items[$index]["商品名称"]);
  164. unset($items[$index]["条码"]);
  165. unset($items[$index]["lineno"]);
  166. }
  167. unset($skus[$commodity->sku]);
  168. }
  169. if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
  170. //未找到商品录入商品
  171. $commodities = [];
  172. $skuParam = [];
  173. $barcodes = [];
  174. foreach ($skus as $sku => $arr){
  175. $data = &$items[$arr[0]];
  176. array_push($commodities,[
  177. "name" => $data["商品名称"],
  178. "sku" => $data["SKU"],
  179. "owner_id" => $owner->id,
  180. "created_at" => date('Y-m-d H:i:s'),
  181. ]);
  182. array_push($skuParam, $sku);
  183. }
  184. if (count($commodities) > 0)$commodityService->insert($commodities);
  185. if (count($skuParam) > 0)$commodities = $commodityService->get(["owner_id"=>$owner->id,"sku"=>$skuParam]);
  186. foreach ($commodities as $commodity){
  187. foreach ($skus[$commodity->sku] as $i => $index){
  188. $items[$index]["commodity_id"] = $commodity->id;
  189. if ($i == 0){
  190. array_push($barcodes,[
  191. "code" => $items[$index]["条码"],
  192. "commodity_id" => $commodity->id,
  193. "created_at" => date('Y-m-d H:i:s')
  194. ]);
  195. }
  196. unset($items[$index]["SKU"]);
  197. unset($items[$index]["商品名称"]);
  198. unset($items[$index]["条码"]);
  199. unset($items[$index]["lineno"]);
  200. }
  201. unset($skus[$commodity->sku]);
  202. }
  203. if (count($barcodes) > 0) app('commodityBarcodeService')->insert($barcodes);
  204. if (count($items) < 1){
  205. Cache::put("storeCheckingReceive",["success"=>false, "errors"=>$errors],86400);
  206. return false;
  207. }
  208. $storeCheckingReceive = app('storeCheckingReceiveService')->create([
  209. "owner_id" => $owner->id,
  210. "created_at" => date('Y-m-d H:i:s'),
  211. 'status' => "已导入",
  212. ]);
  213. $storeCheckingReceive->owner_name = $owner_name;
  214. foreach ($items as &$item){
  215. $item["store_checking_receive_id"] = $storeCheckingReceive->id;
  216. }
  217. app('storeCheckingReceiveItemService')->insert($items);
  218. Cache::put("storeCheckingReceive",["success"=>true,"data"=>$storeCheckingReceive,"errors"=>$errors],86400);
  219. return true;
  220. }
  221. }