ImportReclamationsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 App\Models\Setting;
  10. use Illuminate\Support\Str;
  11. class ImportReclamationsService extends ImportBaseService
  12. {
  13. const HEADERS = [
  14. 'Округ' => 'districts.name',
  15. 'Район' => 'areas.name',
  16. 'Адрес' => 'orders.object_address',
  17. 'Артикул' => 'products.article',
  18. 'Тип' => 'products.nomenclature_number',
  19. 'RFID' => 'products_sku.rfid',
  20. 'Гарантии' => 'reclamations.guarantee',
  21. 'Что сделано' => 'reclamations.whats_done',
  22. 'Дата создания' => 'reclamations.create_date',
  23. 'Дата начала работ' => 'reclamations.start_work_date',
  24. 'Дата завершения работ' => 'reclamations.finish_date',
  25. 'Год поставки МАФ' => 'orders.year',
  26. 'Причина' => 'reclamations.reason',
  27. 'Статус' => 'reclamation_statuses.name',
  28. 'Комментарий' => 'reclamations.comment',
  29. ];
  30. public function __construct(Import $import)
  31. {
  32. parent::__construct($import);
  33. $this->headers = self::HEADERS;
  34. }
  35. public function handle(): bool
  36. {
  37. if(!$this->prepare()) {
  38. return false;
  39. }
  40. $strNumber = 0;
  41. $result = [
  42. 'reclamationsCreated' => 0,
  43. 'mafAttached' => 0,
  44. ];
  45. $defaultMafOrderUserId = Setting::getInt(
  46. Setting::KEY_DEFAULT_MAF_ORDER_USER_ID,
  47. (int) config('app.default_maf_order_user_id')
  48. );
  49. $errors = [
  50. 'district_not_found' => [],
  51. 'area_not_found' => [],
  52. 'status_not_found' => [],
  53. 'order_not_found' => [],
  54. 'product_not_found' => [],
  55. 'sku_not_found' => [],
  56. 'exceptions' => [],
  57. ];
  58. foreach ($this->rowIterator as $row) {
  59. $r = $this->rowToArray($row);
  60. $strNumber++;
  61. if(1 === $strNumber) {
  62. echo $this->import->log('Skip headers Row: ' . $strNumber);
  63. continue;
  64. }
  65. try {
  66. $logMessage = "Row $strNumber: " . $r['orders.object_address'] . ". ";
  67. $year = (int)$r['orders.year'];
  68. // округ
  69. // if (!($districtId = $this->findId('districts.shortname', $r['districts.name'] ?? ''))) {
  70. // $errors['district_not_found'][] = $strNumber;
  71. // continue;
  72. // }
  73. // район
  74. // if (!($areaId = $this->findId('areas.name', $r['areas.name']))) {
  75. // $errors['area_not_found'][] = $strNumber;
  76. // continue;
  77. // }
  78. // manager
  79. $userId = $defaultMafOrderUserId;
  80. // status
  81. if (!($statusId = $this->findId('reclamation_statuses.name', $r['reclamation_statuses.name']))) {
  82. $errors['status_not_found'][] = $strNumber;
  83. continue;
  84. }
  85. // order
  86. // $order = Order::query()
  87. // ->withoutGlobalScopes()
  88. // ->where('year', $year)
  89. // ->where('object_address', $r['orders.object_address'])
  90. // ->first();
  91. // if (!$order) {
  92. // echo $this->import->log('Order NOT FOUND: ' . $r['orders.object_address'], 'WARNING');
  93. // $errors['order_not_found'][] = $strNumber;
  94. // continue;
  95. // } else {
  96. // $logMessage .= 'Found order: ' . $order->object_address . ". ";
  97. // }
  98. // product
  99. // $product = Product::query()
  100. // ->withoutGlobalScopes()
  101. // ->where('year', $year)
  102. // ->where('nomenclature_number', $r['products.nomenclature_number'])
  103. // ->first();
  104. // if (!$product) {
  105. // echo $this->import->log('Product not found: ' . $r['products.nomenclature_number'], 'WARNING');
  106. // $errors['product_not_found'][] = $strNumber;
  107. // continue;
  108. // }
  109. $rfid = Str::replace(' ', '', $r['products_sku.rfid']);
  110. // check maf with this nomenclature number in order
  111. $productSKU = ProductSKU::query()
  112. ->withoutGlobalScopes()
  113. // ->where('year', $year)
  114. // ->where('product_id', $product->id)
  115. // ->where('order_id', $order->id)
  116. ->where('rfid', $rfid)
  117. ->first();
  118. if (!$productSKU) {
  119. echo $this->import->log('SKU not found: ' . $r['products.nomenclature_number'] . '(RFID: ' . $r['products_sku.rfid'] . ')', 'WARNING');
  120. $errors['sku_not_found'][] = $strNumber;
  121. continue;
  122. }
  123. $order = Order::query()->withoutGlobalScopes()->where('id', $productSKU->order_id)->first();
  124. $createDate = ($r['reclamations.create_date']) ? DateHelper::excelDateToISODate($r['reclamations.create_date']) : null;
  125. $finishDate = ($r['reclamations.finish_date']) ? DateHelper::excelDateToISODate($r['reclamations.finish_date']) : null;
  126. $startWorkDate = ($r['reclamations.start_work_date']) ? DateHelper::excelDateToISODate($r['reclamations.start_work_date']) : null;
  127. // reclamation
  128. $reclamation = Reclamation::query()
  129. ->where('order_id', $order->id)
  130. ->where('status_id', $statusId)
  131. ->where('create_date', $createDate)
  132. ->where('finish_date', $finishDate)
  133. ->where('start_work_date', $startWorkDate)
  134. ->where('reason', $r['reclamations.reason'])
  135. ->where('whats_done', $r['reclamations.whats_done'])
  136. ->where('guarantee', $r['reclamations.guarantee'])
  137. ->first();
  138. if (!$reclamation) {
  139. $reclamation = Reclamation::query()
  140. ->create([
  141. 'order_id' => $order->id,
  142. 'user_id' => $userId,
  143. 'status_id' => $statusId,
  144. 'create_date' => $createDate,
  145. 'finish_date' => $finishDate,
  146. 'reason' => $r['reclamations.reason'],
  147. 'guarantee' => $r['reclamations.guarantee'],
  148. 'whats_done' => $r['reclamations.whats_done'],
  149. 'start_work_date' => $startWorkDate,
  150. 'comment' => $r['reclamations.comment'],
  151. ]);
  152. $logMessage .= 'Reclamation created: ' . $r['orders.object_address'] . ". ";
  153. $result['reclamationsCreated']++;
  154. } else {
  155. $logMessage .= 'Reclamation found: ' . $r['orders.object_address']. ". ";
  156. }
  157. if (!$reclamation->skus->contains($productSKU)) {
  158. $reclamation->skus()->syncWithoutDetaching($productSKU->id);
  159. $logMessage .= 'Attached MAF to reclamation, maf_id: ' . $productSKU->id . ". ";
  160. $result['mafAttached']++;
  161. } else {
  162. $logMessage .= 'MAF already attached!';
  163. }
  164. echo $this->import->log($logMessage);
  165. } catch(\Exception $e) {
  166. echo $this->import->log($e->getMessage(), 'WARNING');
  167. $errors['exceptions'][] = $strNumber;
  168. }
  169. }
  170. echo $this->import->log(print_r($result, true));
  171. // Резюме по ошибкам
  172. $totalErrors = array_sum(array_map('count', $errors));
  173. if ($totalErrors > 0) {
  174. echo $this->import->log('--- РЕЗЮМЕ ПО ОШИБКАМ ---');
  175. if (count($errors['district_not_found']) > 0) {
  176. $rows = implode(', ', $errors['district_not_found']);
  177. echo $this->import->log("Округ не найден: " . count($errors['district_not_found']) . " ({$rows})", 'WARNING');
  178. }
  179. if (count($errors['area_not_found']) > 0) {
  180. $rows = implode(', ', $errors['area_not_found']);
  181. echo $this->import->log("Район не найден: " . count($errors['area_not_found']) . " ({$rows})", 'WARNING');
  182. }
  183. if (count($errors['status_not_found']) > 0) {
  184. $rows = implode(', ', $errors['status_not_found']);
  185. echo $this->import->log("Статус не найден: " . count($errors['status_not_found']) . " ({$rows})", 'WARNING');
  186. }
  187. if (count($errors['order_not_found']) > 0) {
  188. $rows = implode(', ', $errors['order_not_found']);
  189. echo $this->import->log("Заказ не найден: " . count($errors['order_not_found']) . " ({$rows})", 'WARNING');
  190. }
  191. if (count($errors['product_not_found']) > 0) {
  192. $rows = implode(', ', $errors['product_not_found']);
  193. echo $this->import->log("Товар не найден: " . count($errors['product_not_found']) . " ({$rows})", 'WARNING');
  194. }
  195. if (count($errors['sku_not_found']) > 0) {
  196. $rows = implode(', ', $errors['sku_not_found']);
  197. echo $this->import->log("МАФ (SKU) не найден: " . count($errors['sku_not_found']) . " ({$rows})", 'WARNING');
  198. }
  199. if (count($errors['exceptions']) > 0) {
  200. $rows = implode(', ', $errors['exceptions']);
  201. echo $this->import->log("Исключения: " . count($errors['exceptions']) . " ({$rows})", 'WARNING');
  202. }
  203. echo $this->import->log("Всего ошибок: {$totalErrors}", 'WARNING');
  204. } else {
  205. echo $this->import->log('Импорт завершён без ошибок');
  206. }
  207. $this->import->status = 'DONE';
  208. $this->import->save();
  209. return true;
  210. }
  211. }