diff --git a/src/app/Console/Kernel.php b/src/app/Console/Kernel.php index d49a0aca..db647891 100644 --- a/src/app/Console/Kernel.php +++ b/src/app/Console/Kernel.php @@ -21,7 +21,6 @@ class Kernel extends ConsoleKernel protected $commands = [ Commands\GenerateSitemapCommand::class, Commands\Payment\EripStatusUpdateCommand::class, - Commands\Payment\BelpostCODParseFromEmailCommand::class, ]; /** @@ -52,7 +51,7 @@ protected function schedule(Schedule $schedule): void $schedule->command('generate:sitemap')->dailyAt('00:30'); $schedule->command('erip:update-statuses')->everyTenMinutes(); - $schedule->command('belpost:cod-parse-from-email')->dailyAt('01:30'); + $schedule->command('belpost:cod-parse-from-email')->hourly()->between('8:00', '18:00'); } /** diff --git a/src/app/Services/Payment/BelpostCODService.php b/src/app/Services/Payment/BelpostCODService.php index 652162dc..a717b325 100644 --- a/src/app/Services/Payment/BelpostCODService.php +++ b/src/app/Services/Payment/BelpostCODService.php @@ -6,6 +6,7 @@ use App\Enums\Payment\OnlinePaymentStatusEnum; use App\Models\Orders\Order; use App\Models\Orders\OrderItem; +use App\Models\Payments\Installment; use App\Services\Imap\ImapParseEmailService; use DateInterval; use DatePeriod; @@ -45,7 +46,7 @@ public function importExcelCOD(UploadedFile $file): array $orders = Order::with([ 'onlinePayments', - 'data', + 'data' => fn ($query) => $query->with('installment'), 'track', ])->whereHas('track', fn ($query) => $query->whereIn('track_number', array_keys($parsedData))) ->get(); @@ -64,32 +65,54 @@ public function importExcelCOD(UploadedFile $file): array $payment->statuses()->create([ 'payment_status_enum_id' => OnlinePaymentStatusEnum::SUCCEEDED, ]); - $advancePayment = $order->onlinePayments->filter(function ($item) { - return stripos($item->comment, 'Аванс') !== false; - })->sum('amount'); - $codSum = $paymentSum + $advancePayment; - $itemCodSum = $paymentSum + ($advancePayment / count($order->data)); - if ($order->getItemsPrice() == $codSum) { - $order->update(['status_key' => 'complete']); - $payment->order->data->each(function (OrderItem $orderItem) { - $orderItem->update(['status_key' => 'complete']); - }); - } elseif ($order->data->where('current_price', $itemCodSum)->count() === 1) { - $order->update(['status_key' => 'complete']); - $payment->order->data->each(function (OrderItem $orderItem) use ($itemCodSum) { - if ($orderItem->current_price == $itemCodSum) { - $orderItem->update(['status_key' => 'complete']); - } else { - $orderItem->update(['status_key' => 'return_fitting']); + + if (in_array($order->status_key, ['fitting', 'sent'])) { + $partialBuybackItemsCount = 0; + $isInstallment = $order->payment_id === Installment::PAYMENT_METHOD_ID; + $successfulPaymentsSum = $order->onlinePayments->where('last_status_enum_id', OnlinePaymentStatusEnum::SUCCEEDED)->sum('amount'); + $itemCodSum = (float)($paymentSum + ($successfulPaymentsSum / count($order->data))); + $orderTotalPrice = (float)($order->getItemsPrice() - $successfulPaymentsSum); + $firstPaymentsSum = $isInstallment ? $order->data->map(function ($item) use ($isInstallment, $itemCodSum, &$partialBuybackItemsCount) { + $firstPaymentSum = $isInstallment ? $item->current_price - ($item->installment->monthly_fee * 2) : 0; + if ($item->current_price == $itemCodSum || ($isInstallment && $firstPaymentSum == $itemCodSum)) { + $partialBuybackItemsCount++; } - }); - } else { - $order->update(['status_key' => 'delivered']); - $order->adminComments()->create([ - 'comment' => "Получен наложенный платеж на сумму {$paymentSum}. Распределите сумму по товарам!", - ]); - } + return $firstPaymentSum; + })->sum() : 0; + $firstPaymentsSum = $firstPaymentsSum ? ($firstPaymentsSum - $successfulPaymentsSum) : 0; + + if ( + $orderTotalPrice == $paymentSum || + ($isInstallment && $firstPaymentsSum == $paymentSum) + ) { + $order->update(['status_key' => 'complete']); + $order->data->each(function (OrderItem $orderItem) { + $orderItem->update(['status_key' => 'complete']); + }); + } elseif ($partialBuybackItemsCount === 1) { + $order->update(['status_key' => 'delivered']); + $order->data->each(function (OrderItem $orderItem) use ($order, $itemCodSum, $isInstallment) { + $firstPaymentSum = $isInstallment ? $orderItem->current_price - ($orderItem->installment->monthly_fee * 2) : 0; + if ( + $orderItem->current_price == $itemCodSum || + $isInstallment && $firstPaymentSum == $itemCodSum + ) { + $orderItem->update(['status_key' => 'complete']); + } else { + $productFullName = $orderItem->product->getFullName(); + $order->adminComments()->create([ + 'comment' => "Товар {$productFullName} не выкуплен - ожидайте возврат", + ]); + } + }); + } else { + $order->update(['status_key' => 'delivered']); + $order->adminComments()->create([ + 'comment' => "Получен наложенный платеж на сумму {$paymentSum}. Распределите сумму по товарам!", + ]); + } + } $result['count']++; $result['sum'] += $paymentSum; } diff --git a/src/app/Services/Payment/Methods/PaymentEripService.php b/src/app/Services/Payment/Methods/PaymentEripService.php index 86d76c33..6c630236 100644 --- a/src/app/Services/Payment/Methods/PaymentEripService.php +++ b/src/app/Services/Payment/Methods/PaymentEripService.php @@ -114,6 +114,7 @@ private function updateHgroshStatuses(string $beginDate, string $endDate, int $s if ($invoicingList->isOk()) { $responseInvoicingList = $invoicingList->getBodyFormat(); $records = $responseInvoicingList['records'] ?? []; + \File::append(storage_path('logs/erip_statuses_' . date('Y-m-d') . '.log'), print_r($records, true) . PHP_EOL); if (count($records)) { $paymentNums = array_map(fn ($record) => $record['number'], array_filter($records, fn ($record) => isset($record['number']))); $paymentsByNum = OnlinePayment::whereIn('payment_num', $paymentNums)