ImportReclamationsService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Services;
  3. use App\Helpers\DateHelper;
  4. use App\Models\Import;
  5. use App\Models\Order;
  6. use App\Models\Product;
  7. use App\Models\ProductSKU;
  8. use App\Models\Reclamation;
  9. use Illuminate\Support\Str;
  10. class ImportReclamationsService extends ImportBaseService
  11. {
  12. const HEADERS = [
  13. 'Округ' => 'districts.name',
  14. 'Район' => 'areas.name',
  15. 'Адрес' => 'orders.object_address',
  16. 'Артикул' => 'products.article',
  17. 'Тип' => 'products.nomenclature_number',
  18. 'RFID' => 'products_sku.rfid',
  19. 'Гарантии' => 'reclamations.guarantee',
  20. 'Что сделано' => 'reclamations.whats_done',
  21. 'Дата создания' => 'reclamations.create_date',
  22. 'Дата начала работ' => 'reclamations.start_work_date',
  23. 'Дата завершения работ' => 'reclamations.finish_date',
  24. 'Год поставки МАФ' => 'orders.year',
  25. 'Причина' => 'reclamations.reason',
  26. 'Статус' => 'reclamation_statuses.name',
  27. 'Комментарий' => 'reclamations.comment',
  28. ];
  29. public function __construct(Import $import)
  30. {
  31. parent::__construct($import);
  32. $this->headers = self::HEADERS;
  33. }
  34. public function handle()
  35. {
  36. if(!$this->prepare()) {
  37. return false;
  38. }
  39. $strNumber = 0;
  40. $result = [
  41. 'reclamationsCreated' => 0,
  42. 'mafAttached' => 0,
  43. ];
  44. foreach ($this->rowIterator as $row) {
  45. $r = $this->rowToArray($row);
  46. $strNumber++;
  47. if(1 === $strNumber) {
  48. echo $this->import->log('Skip headers Row: ' . $strNumber);
  49. continue;
  50. }
  51. echo $this->import->log("Row $strNumber: " . $r['orders.object_address']);
  52. $year = (int) $r['orders.year'];
  53. // округ
  54. if(!($districtId = $this->findId('districts.shortname', $r['districts.name'] ?? ''))) {
  55. continue;
  56. }
  57. // район
  58. if(!($areaId = $this->findId('areas.name', $r['areas.name']))) {
  59. continue;
  60. }
  61. // manager
  62. $userId = config('app.default_maf_order_user_id');
  63. // status
  64. if(!($statusId = $this->findId('reclamation_statuses.name', $r['reclamation_statuses.name']))) {
  65. continue;
  66. }
  67. // order
  68. $order = Order::query()
  69. ->where('year', $year)
  70. ->where('object_address', $r['orders.object_address'])
  71. ->first();
  72. if(!$order) {
  73. echo $this->import->log('Order NOT FOUND: ' . $r['orders.object_address'], 'WARNING');
  74. continue;
  75. } else {
  76. echo $this->import->log('Found order: ' . $order->object_address);
  77. }
  78. // product
  79. $product = Product::query()
  80. ->where('year', $year)
  81. ->where('nomenclature_number', $r['products.nomenclature_number'])
  82. ->first();
  83. if(!$product) {
  84. echo $this->import->log('Product not found: ' . $r['products.nomenclature_number'], 'WARNING');
  85. continue;
  86. }
  87. $rfid = Str::replace(' ', '', $r['products_sku.rfid']);
  88. // check maf with this nomenclature number in order
  89. $productSKU = ProductSKU::query()
  90. ->where('year', $year)
  91. ->where('product_id', $product->id)
  92. ->where('order_id', $order->id)
  93. ->where('rfid', $rfid)
  94. ->first();
  95. if(!$productSKU) {
  96. $productSKU = ProductSKU::query()
  97. ->where('year', $year)
  98. ->where('product_id', $product->id)
  99. ->where('order_id', $order->id)
  100. ->first();
  101. }
  102. if(!$productSKU) {
  103. echo $this->import->log('SKU not found: ' . $r['products.nomenclature_number'], 'WARNING');
  104. continue;
  105. }
  106. $createDate = ($r['reclamations.create_date']) ? DateHelper::excelDateToISODate($r['reclamations.create_date']) : null;
  107. $finishDate = ($r['reclamations.finish_date']) ? DateHelper::excelDateToISODate($r['reclamations.finish_date']) : null;
  108. $startWorkDate = ($r['reclamations.start_work_date']) ? DateHelper::excelDateToISODate($r['reclamations.start_work_date']) : null;
  109. // reclamation
  110. $reclamation = Reclamation::query()
  111. ->where('order_id', $order->id)
  112. ->where('status_id', $statusId)
  113. ->where('create_date', $createDate)
  114. ->where('finish_date', $finishDate)
  115. ->where('start_work_date', $startWorkDate)
  116. ->where('reason', $r['reclamations.reason'])
  117. ->where('whats_done', $r['reclamations.whats_done'])
  118. ->where('guarantee', $r['reclamations.guarantee'])
  119. ->first();
  120. if(!$reclamation) {
  121. $reclamation = Reclamation::query()
  122. ->create([
  123. 'order_id' => $order->id,
  124. 'user_id' => $userId,
  125. 'status_id' => $statusId,
  126. 'create_date' => $createDate,
  127. 'finish_date' => $finishDate,
  128. 'reason' => $r['reclamations.reason'],
  129. 'guarantee' => $r['reclamations.guarantee'],
  130. 'whats_done' => $r['reclamations.whats_done'],
  131. 'start_work_date' => $startWorkDate,
  132. 'comment' => $r['reclamations.comment'],
  133. ]);
  134. echo $this->import->log('Reclamation created: ' . $r['orders.object_address']);
  135. $result['reclamationsCreated']++;
  136. } else {
  137. echo $this->import->log('Reclamation found: ' . $r['orders.object_address']);
  138. }
  139. if(!$reclamation->skus->contains($productSKU)) {
  140. $reclamation->skus()->syncWithoutDetaching($productSKU->id);
  141. echo $this->import->log('Attached MAF to reclamation, maf_id: ' . $productSKU->id);
  142. $result['mafAttached']++;
  143. } else {
  144. echo $this->import->log('MAF already attached!');
  145. }
  146. }
  147. echo $this->import->log(print_r($result, true));
  148. $this->import->status = 'DONE';
  149. $this->import->save();
  150. return true;
  151. }
  152. }