InventoryCompareImport.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Imports;
  3. use App\Commodity;
  4. use App\OracleBasSKU;
  5. use App\OracleInvLotAtt;
  6. use App\Owner;
  7. use App\Services\InventoryCompareService;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Maatwebsite\Excel\Concerns\ToCollection;
  11. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  12. use Maatwebsite\Excel\Concerns\WithMultipleSheets;
  13. use Maatwebsite\Excel\Imports\HeadingRowFormatter;
  14. HeadingRowFormatter::default('none');
  15. class InventoryCompareImport implements ToCollection, WithHeadingRow, WithMultipleSheets
  16. {
  17. protected $owner_id=null;
  18. protected $ownerCode=null;
  19. protected $customLocationArrs=[];
  20. protected $ownerArr=[];
  21. public function __construct()
  22. {
  23. app()->singleton('InventoryCompareService',InventoryCompareService::class);
  24. }
  25. public function Collection(Collection $collection)
  26. {
  27. $array = $collection->toArray();
  28. $skuName=(function()use($array){
  29. if($array[0]['物料编号']??false) return '物料编号';
  30. if($array[0]['SKU']??false) return 'SKU';
  31. if($array[0]['产品编码']??false) return '产品编码';
  32. })();
  33. $amountName=(function()use($array){
  34. if($array[0]['物料库存量']??false) return '物料库存量';
  35. if($array[0]['库存量']??false) return '库存量';
  36. if($array[0]['数量']??false) return '数量';
  37. })();
  38. $customLocationName=(function()use($array){
  39. if($array[0]['仓库']??false) return '仓库';
  40. if($array[0]['属性仓']??false) return '属性仓';
  41. })();
  42. $sku=$array[0][$skuName] ?? null;
  43. $amount=$array[0][$amountName] ?? null;
  44. $customLocation=$array[0][$customLocationName] ?? null;
  45. $endIS = false;
  46. if (!isset($sku) || !isset($amount) || !isset($customLocation)) {
  47. Cache::put('error', '请检查您第一行标题是否存在产品编号,数量,属性仓');
  48. } else {
  49. $endIS = true;
  50. }
  51. foreach ($collection as $row) {
  52. $customLocationArr=[
  53. 'custom_location'=>$row[$customLocationName] ?? null,
  54. ];
  55. array_push($this->customLocationArrs,$customLocationArr);
  56. }
  57. $this->customLocationArrs=array_column($this->customLocationArrs,'custom_location');
  58. $this->customLocationArrs=array_unique($this->customLocationArrs);
  59. foreach ($this->customLocationArrs as $customLocation){
  60. $ownerCode=OracleInvLotAtt::query()->where('lotatt05',$customLocation)->value('customerid');
  61. if ($ownerCode){
  62. $owner_id=Owner::query()->where('code',$ownerCode)->value('id');
  63. }else{
  64. $owner_id=null;
  65. }
  66. $owners=[
  67. 'owner_id'=>$owner_id,
  68. 'owner_code'=>$ownerCode,
  69. 'custom_location'=>$customLocation,
  70. ];
  71. $this->ownerArr = array_merge($this->ownerArr,[$owners['custom_location']=>$owners]);
  72. }
  73. $skuAndWarehouseArr=[];
  74. $inventoryCompares=[];
  75. $exception = [];
  76. $sum = 2;
  77. if ($endIS) {
  78. foreach ($collection as $row) {
  79. if (!$row[$skuName]) {
  80. array_push($exception, ['第' . $sum . '行产品编号为空!']);
  81. $sum++;
  82. continue;
  83. } else if (!$row[$amountName]) {
  84. array_push($exception, ['第' . $sum . '行数量为空!']);
  85. $sum++;
  86. continue;
  87. } else if (!$row[$customLocationName]) {
  88. array_push($exception, ['第' . $sum . '行属性仓为空!']);
  89. $sum++;
  90. continue;
  91. }
  92. if ($this->ownerArr["$row[$customLocationName]"]['owner_id']){
  93. $this->owner_id=$this->ownerArr["$row[$customLocationName]"]['owner_id'];
  94. $this->ownerCode=$this->ownerArr["$row[$customLocationName]"]['owner_code'];
  95. }
  96. if (!$this->ownerArr["$row[$customLocationName]"]['owner_id']){
  97. $ownerCode=OracleInvLotAtt::query()->where('lotatt05',$row[$customLocationName])->value('customerid');
  98. if (!$ownerCode){
  99. array_push($exception, ['第' . $sum . '行货主不存在!']);
  100. $sum++;
  101. continue;
  102. }
  103. if (!$this->ownerCode) {
  104. $this->ownerCode=$ownerCode;
  105. $this->owner_id=Owner::query()->where('code',$this->ownerCode)->value('id');
  106. }elseif ($this->ownerCode!=$ownerCode){
  107. $this->owner_id=Owner::query()->where('code',$ownerCode)->value('id');
  108. }
  109. }
  110. $skuAndWarehouse=$row[$skuName].$row[$customLocationName];
  111. array_push($skuAndWarehouseArr,$skuAndWarehouse);
  112. $uniqueArr=array_unique($skuAndWarehouseArr);
  113. if (count($uniqueArr)!=count($skuAndWarehouseArr)){
  114. array_push($exception, ['第' . $sum . '行产品编号+属性仓不能同时重复!']);
  115. $sum++;
  116. continue;
  117. }
  118. $commodityInWMS=Commodity::where('owner_id',$this->owner_id)->where('sku',$row[$skuName])->first();
  119. $owner=Owner::find($this->owner_id);
  120. if(!$commodityInWMS) {
  121. $commodityInWMS = OracleBasSKU::query()->where('sku', "$row[$skuName]")->where('customerid', "$owner->code")->first();
  122. $commodity=Commodity::query()->firstOrCreate([
  123. 'owner_id'=>$this->owner_id,
  124. 'sku'=>$row[$skuName],
  125. 'name'=>$commodityInWMS['descr_c'],
  126. 'length'=>$commodityInWMS['skulength'],
  127. 'width'=>$commodityInWMS['skuwidth'],
  128. 'height'=>$commodityInWMS['skuhigh'],
  129. 'volumn'=>$commodityInWMS['skulength']*$commodityInWMS['skuwidth']*$commodityInWMS['skuhigh']]);
  130. $barcode = $commodityInWMS['alternate_sku1']??$commodityInWMS['alternate_sku2']??$commodityInWMS['alternate_sku3'];
  131. if($barcode)
  132. $commodity->newBarcode($barcode);
  133. }
  134. if (!$commodityInWMS){
  135. array_push($exception, ['该货主下不存在产品编号:'.$row[$skuName]]);
  136. $sum++;
  137. continue;
  138. }
  139. $inventoryCompare=[
  140. 'owner_name'=>$owner['name'],
  141. 'owner_code'=>$owner['code'],
  142. 'owner_id'=>$this->owner_id,
  143. 'sku'=>"$row[$skuName]",
  144. 'custom_location'=>$row[$customLocationName],
  145. 'amount'=>$row[$amountName],
  146. ];
  147. array_push($inventoryCompares,$inventoryCompare);
  148. $sum++;
  149. }
  150. /** @var InventoryCompareService $service */
  151. $service = app('InventoryCompareService');
  152. $inventoryCompare= $service->createInventoryCompares($inventoryCompares);
  153. if (!$inventoryCompare) array_push($exception, ['创建库存对比失败!']);
  154. }
  155. Cache::put('exception', $exception, 86400);
  156. }
  157. /**
  158. * 该方法是实现上传文件只选中 第一个表
  159. * ExcelImprot 默认是有多少个分表就执行多少次的分表
  160. * @return OrderIssueImport[]|array
  161. */
  162. public function sheets(): array
  163. {
  164. return [0 => new InventoryCompareImport()];
  165. }
  166. }