feat(sales): add discount support with ledger accounting
- Add discount_type column to transactions table via migration - Update Sale model to use fillable instead of guarded for better security - Implement discount account ledger creation when discount is applied - Fix net amount calculation to include discount in CreateSaleAction - Remove unused "Exempt" column from sale transaction table - Make discount_type required when discount is enabled in form - Update form data mutation to properly handle discount calculations
This commit is contained in:
@@ -24,6 +24,10 @@ class CreateTransactionAction extends BaseAction
|
||||
|
||||
$this->cashAccountLedger($payload);
|
||||
|
||||
if ($payload->transaction->discount !== 0) {
|
||||
$this->discountAccountLedger($payload);
|
||||
}
|
||||
|
||||
return $next($payload);
|
||||
}
|
||||
|
||||
@@ -33,11 +37,13 @@ class CreateTransactionAction extends BaseAction
|
||||
$isExpense = $payload->transactionable instanceof \App\Models\Expense;
|
||||
$type = $isExpense ? 'debit' : 'credit';
|
||||
|
||||
$discount = $payload->transaction->discount ?? 0.00;
|
||||
|
||||
if ($branch->isClientVatable) {
|
||||
//create transaction account ledger
|
||||
$ledgerPayload = new CreateLedgerDTO(
|
||||
branch_id: $payload->transactionable->branch_id,
|
||||
amount: $payload->transaction->net_amount ?? 0.00,
|
||||
amount: $payload->transaction->net_amount + $discount ?? 0.00,
|
||||
transaction: $payload->transaction,
|
||||
account: $payload->transaction->account,
|
||||
type: $type,
|
||||
@@ -144,4 +150,28 @@ class CreateTransactionAction extends BaseAction
|
||||
$this->ledgerPipe($ledgerPayload);
|
||||
}
|
||||
}
|
||||
|
||||
public function discountAccountLedger($payload): void
|
||||
{
|
||||
$isExpense = $payload->transactionable instanceof \App\Models\Expense;
|
||||
$type = $isExpense ? 'credit' : 'debit';
|
||||
$amount = $payload->transaction->discount ?? 0.00;
|
||||
$clientId = $payload->transactionable->branch->client_id;
|
||||
|
||||
$discountAccount = Account::query()
|
||||
->where('account', 'Sales Discount')
|
||||
->where('client_id', $clientId)
|
||||
->first();
|
||||
|
||||
if ($discountAccount && $amount > 0) {
|
||||
$ledgerPayload = new CreateLedgerDTO(
|
||||
branch_id: $payload->transactionable->branch_id,
|
||||
amount: $amount,
|
||||
transaction: $payload->transaction,
|
||||
account: $discountAccount,
|
||||
type: $type,
|
||||
);
|
||||
$this->ledgerPipe($ledgerPayload);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user