ImportReclamationsService.php 7.5 KB

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