Skip to content

Commit

Permalink
Merge pull request #201 from Oneami/master
Browse files Browse the repository at this point in the history
145. Fix. Automatic order statuses
  • Loading branch information
dmitrakovich authored Dec 8, 2023
2 parents b6dea77 + 6f327e5 commit bfc2b76
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class Kernel extends ConsoleKernel
protected $commands = [
Commands\GenerateSitemapCommand::class,
Commands\Payment\EripStatusUpdateCommand::class,
Commands\Payment\BelpostCODParseFromEmailCommand::class,
];

/**
Expand Down Expand Up @@ -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');
}

/**
Expand Down
73 changes: 48 additions & 25 deletions src/app/Services/Payment/BelpostCODService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/app/Services/Payment/Methods/PaymentEripService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bfc2b76

Please sign in to comment.