ImportReclamationsService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(): bool
  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. try {
  52. $logMessage = "Row $strNumber: " . $r['orders.object_address'] . ". ";
  53. $year = (int)$r['orders.year'];
  54. // округ
  55. if (!($districtId = $this->findId('districts.shortname', $r['districts.name'] ?? ''))) {
  56. continue;
  57. }
  58. // район
  59. if (!($areaId = $this->findId('areas.name', $r['areas.name']))) {
  60. continue;
  61. }
  62. // manager
  63. $userId = config('app.default_maf_order_user_id');
  64. // status
  65. if (!($statusId = $this->findId('reclamation_statuses.name', $r['reclamation_statuses.name']))) {
  66. continue;
  67. }
  68. // order
  69. $order = Order::query()
  70. ->where('year', $year)
  71. ->where('object_address', $r['orders.object_address'])
  72. ->first();
  73. if (!$order) {
  74. echo $this->import->log('Order NOT FOUND: ' . $r['orders.object_address'], 'WARNING');
  75. continue;
  76. } else {
  77. $logMessage .= 'Found order: ' . $order->object_address . ". ";
  78. }
  79. // product
  80. $product = Product::query()
  81. ->where('year', $year)
  82. ->where('nomenclature_number', $r['products.nomenclature_number'])
  83. ->first();
  84. if (!$product) {
  85. echo $this->import->log('Product not found: ' . $r['products.nomenclature_number'], 'WARNING');
  86. continue;
  87. }
  88. $rfid = Str::replace(' ', '', $r['products_sku.rfid']);
  89. // check maf with this nomenclature number in order
  90. $productSKU = ProductSKU::query()
  91. ->where('year', $year)
  92. ->where('product_id', $product->id)
  93. ->where('order_id', $order->id)
  94. ->where('rfid', $rfid)
  95. ->first();
  96. if (!$productSKU) {
  97. $productSKU = ProductSKU::query()
  98. ->where('year', $year)
  99. ->where('product_id', $product->id)
  100. ->where('order_id', $order->id)
  101. ->first();
  102. }
  103. if (!$productSKU) {
  104. echo $this->import->log('SKU not found: ' . $r['products.nomenclature_number'], 'WARNING');
  105. continue;
  106. }
  107. $createDate = ($r['reclamations.create_date']) ? DateHelper::excelDateToISODate($r['reclamations.create_date']) : null;
  108. $finishDate = ($r['reclamations.finish_date']) ? DateHelper::excelDateToISODate($r['reclamations.finish_date']) : null;
  109. $startWorkDate = ($r['reclamations.start_work_date']) ? DateHelper::excelDateToISODate($r['reclamations.start_work_date']) : null;
  110. // reclamation
  111. $reclamation = Reclamation::query()
  112. ->where('order_id', $order->id)
  113. ->where('status_id', $statusId)
  114. ->where('create_date', $createDate)
  115. ->where('finish_date', $finishDate)
  116. ->where('start_work_date', $startWorkDate)
  117. ->where('reason', $r['reclamations.reason'])
  118. ->where('whats_done', $r['reclamations.whats_done'])
  119. ->where('guarantee', $r['reclamations.guarantee'])
  120. ->first();
  121. if (!$reclamation) {
  122. $reclamation = Reclamation::query()
  123. ->create([
  124. 'order_id' => $order->id,
  125. 'user_id' => $userId,
  126. 'status_id' => $statusId,
  127. 'create_date' => $createDate,
  128. 'finish_date' => $finishDate,
  129. 'reason' => $r['reclamations.reason'],
  130. 'guarantee' => $r['reclamations.guarantee'],
  131. 'whats_done' => $r['reclamations.whats_done'],
  132. 'start_work_date' => $startWorkDate,
  133. 'comment' => $r['reclamations.comment'],
  134. ]);
  135. $logMessage .= 'Reclamation created: ' . $r['orders.object_address'] . ". ";
  136. $result['reclamationsCreated']++;
  137. } else {
  138. $logMessage .= 'Reclamation found: ' . $r['orders.object_address']. ". ";
  139. }
  140. if (!$reclamation->skus->contains($productSKU)) {
  141. $reclamation->skus()->syncWithoutDetaching($productSKU->id);
  142. $logMessage .= 'Attached MAF to reclamation, maf_id: ' . $productSKU->id . ". ";
  143. $result['mafAttached']++;
  144. } else {
  145. $logMessage .= 'MAF already attached!';
  146. }
  147. echo $this->import->log($logMessage);
  148. } catch(\Exception $e) {
  149. echo $this->import->log($e->getMessage(), 'WARNING');
  150. }
  151. }
  152. echo $this->import->log(print_r($result, true));
  153. $this->import->status = 'DONE';
  154. $this->import->save();
  155. return true;
  156. }
  157. }