ultimatepos/app/Jobs/Accounting/PostPaymentTransactionJob.php

85 lines
2.3 KiB
PHP

<?php
namespace App\Jobs\Accounting;
use App\BusinessLocation;
use App\Transaction;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\Accounting\Services\OperationalTransactionPostingService;
class PostPaymentTransactionJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $tries = 3;
public int $backoff = 30;
public function __construct(
public int $paymentId,
public int $transactionId,
public int $businessId,
public ?int $userId = null,
public bool $isDeleted = false
) {}
public function handle(OperationalTransactionPostingService $postingService): void
{
$transaction = Transaction::findForBusiness($this->transactionId, $this->businessId);
if (! $transaction) {
return;
}
$type = match ($transaction->type) {
'purchase' => 'purchase_payment',
'sell' => 'sell_payment',
default => null,
};
if ($type === null) {
return;
}
if ($this->isDeleted) {
$postingService->delete(
$type,
$this->transactionId,
$this->paymentId,
$this->businessId
);
return;
}
$businessLocation = BusinessLocation::where('business_id', $this->businessId)
->find($transaction->location_id);
if (! $businessLocation) {
return;
}
$accountingDefaultMap = json_decode($businessLocation->accounting_default_map ?? '{}', true);
$depositTo = $accountingDefaultMap[$type]['deposit_to'] ?? null;
$paymentAccount = $accountingDefaultMap[$type]['payment_account'] ?? null;
if (is_null($depositTo) || is_null($paymentAccount)) {
return;
}
$postingService->post(
$type,
$this->paymentId,
(int) ($this->userId ?? $transaction->created_by ?? 0),
$this->businessId,
(int) $depositTo,
(int) $paymentAccount,
(int) $transaction->location_id
);
}
}