Skip to content

Commit

Permalink
369. new notifications about offline orders
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrakovich committed Jun 2, 2024
1 parent 4badbcb commit 38b045f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function grid()
$grid->column('confirmed_at', 'Подтвержден');
$grid->column('collected_at', 'Собран');
$grid->column('picked_up_at', 'Упаковано');
$grid->column('moved_at', 'Перемещено на склад ИМ');
// $grid->column('moved_at', 'Перемещено на склад ИМ');
$grid->column('sended_at', 'Отправлено');
$grid->column('completed_at', 'Выкуплен');
$grid->column('returned_at', 'Возврат');
Expand Down
181 changes: 0 additions & 181 deletions src/app/Jobs/AvailableSizes/NotifyOfflineOrdersJob.php

This file was deleted.

5 changes: 2 additions & 3 deletions src/app/Jobs/AvailableSizes/UpdateAvailableSizesTableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ protected function updateAvailableSizesFromOrders(array &$availableSizes): int
$productsInOrdersDebug = [];
$sizesCount = OrderItem::query()
->whereIn('status_key', ['new', 'reserved', 'confirmed', 'collect', 'pickup'])
->whereHas('statusLog', fn (Builder $query) => $query->whereNull('moved_at'))
->with('statusLog:order_item_id,stock_id')
->get(['id', 'product_id', 'size_id', 'count'])
->each(function (OrderItem $orderItem) use (&$productsInOrders, &$productsInOrdersDebug) {
Expand All @@ -352,7 +351,7 @@ protected function updateAvailableSizesFromOrders(array &$availableSizes): int
})
->count();

$this->debug('productsInOrdersDebug:', $productsInOrdersDebug);
// $this->debug('productsInOrdersDebug:', $productsInOrdersDebug);

foreach ($availableSizes as &$stock) {
if (empty($stock['product_id'])) {
Expand All @@ -364,7 +363,7 @@ protected function updateAvailableSizesFromOrders(array &$availableSizes): int
$sizeField = AvailableSizes::convertSizeIdToField($sizeId);
$stockCount = $stock[$sizeField];

$this->debug('subtract to order:', compact('stockId', 'productId', 'sizeField', 'count', 'stockCount'));
// $this->debug('subtract to order:', compact('stockId', 'productId', 'sizeField', 'count', 'stockCount'));

$stock[$sizeField] -= $count;
$count -= $stockCount;
Expand Down
69 changes: 60 additions & 9 deletions src/app/Jobs/OneC/UpdateOfflineOrdersJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
namespace App\Jobs\OneC;

use App\Jobs\AbstractJob;
use App\Models\Bots\Telegram\TelegramChat;
use App\Models\Logs\OrderItemStatusLog;
use App\Models\OneC\OfflineOrder as OfflineOrder1C;
use App\Models\Orders\OfflineOrder;
use App\Models\Orders\OrderItem;
use App\Models\Stock;
use App\Models\User\User;
use App\Notifications\OrderItemInventoryNotification;
use function Sentry\captureMessage;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Context;
use Sentry\Severity;

use function Sentry\captureMessage;

class UpdateOfflineOrdersJob extends AbstractJob
{
const NEW_ORDERS_LIMIT = 100;
Expand All @@ -28,19 +32,28 @@ class UpdateOfflineOrdersJob extends AbstractJob
*/
protected $contextVars = ['usedMemory'];

/**
* Stock models collection
*
* @var Collection<Stock>
*/
private Collection $stocks;

/**
* Execute the job.
*/
public function handle(): void
{
$this->setStocks();

[$newReturnOrders, $newSaleOrders] = $this->getNewOrders()->partition(
fn (OfflineOrder1C $order) => $order->isReturn()
);

foreach ($newSaleOrders as $order) {
Context::add('1C sale order', $order->attributesToArray());

$offlineOrder = new OfflineOrder([
$offlineOrder = OfflineOrder::query()->create([
'one_c_id' => $order->CODE,
'receipt_number' => $order->SP6098,
'stock_id' => $order->stock->id,
Expand All @@ -55,7 +68,11 @@ public function handle(): void
'sold_at' => $order->getSoldAtDateTime(),
]);

$offlineOrder->save();
// todo: если продажа с ИМ, переводить статус соответствующего заказа

if (!$order->isOnline()) {
$this->notify($offlineOrder);
}
}
Context::forget('1C sale order');

Expand All @@ -67,12 +84,9 @@ public function handle(): void
if (isset($returnOrders[$orderItemKey])) {
$returnOrder = $returnOrders[$orderItemKey];
$returnOrder->update(['returned_at' => $order->getReturnedAtDateTime()]);

//! отправить сообщение с помощью бота в ТГ
continue;
} else {
captureMessage('Return 1C order without sold order in DB', Severity::warning());
}

captureMessage('Return 1C order without sold order in DB', Severity::warning());
}
}

Expand Down Expand Up @@ -152,4 +166,41 @@ private function generateKeyForCompare(OfflineOrder|OfflineOrder1C $offlineOrder
return "{$offlineOrder->SP6098}|{$offlineOrder->SP6092}|{$offlineOrder->getSizeId()}";
}
}

/**
* Notify the Telegram chat about a offline order.
*/
private function notify(OfflineOrder $offlineOrder): void
{
if (!$chat = $this->getChatByStockId($offlineOrder->stock_id)) {
return;
}

$notification = new OrderItemStatusLog(['stock_id' => $offlineOrder->stock_id]);
$orderItem = (new OrderItem([
'product_id' => $offlineOrder->product_id,
'size_id' => $offlineOrder->size_id,
'status_key' => 'complete',
]))->setRelation('inventoryNotification', $notification);

$chat->notifyNow(new OrderItemInventoryNotification($orderItem));
}

/**
* Set the stocks for the current context.
*/
private function setStocks(): void
{
$this->stocks = Stock::with(['groupChat'])
->get(['id', 'group_chat_id'])
->keyBy('id');
}

/**
* Get the Telegram chat model associated with the specified stock ID.
*/
private function getChatByStockId(int $stockId): ?TelegramChat
{
return $this->stocks[$stockId]?->groupChat;
}
}
13 changes: 13 additions & 0 deletions src/app/Models/OneC/OfflineOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class OfflineOrder extends AbstractOneCModel
*/
protected $table = 'SC6104';

/**
* The identifier for online orders stock.
*/
const ONLINE_STOCK_ID = 4;

/**
* The attributes that should be cast.
*
Expand Down Expand Up @@ -153,4 +158,12 @@ public function getSizeId(): int
{
return $this->size?->id ?? Size::ONE_SIZE_ID;
}

/**
* Check if the order is online.
*/
public function isOnline(): bool
{
return $this->SP6096 === self::ONLINE_STOCK_ID;
}
}

0 comments on commit 38b045f

Please sign in to comment.