InventoryCompareImport.php 7.7 KB

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