mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-15 20:30:33 +00:00
feat: wip advanced payment
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.table('payment_receives', (table) => {
|
||||
table.decimal('unapplied_amount', 13, 3).defaultTo(0);
|
||||
table.decimal('used_amount', 13, 3).defaultTo(0);
|
||||
table.decimal('applied_amount', 13, 3).defaultTo(0);
|
||||
table
|
||||
.integer('unearned_revenue_account_id')
|
||||
.unsigned()
|
||||
@@ -12,8 +11,7 @@ exports.up = function (knex) {
|
||||
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.table('payment_receives', (table) => {
|
||||
table.dropColumn('unapplied_amount');
|
||||
table.dropColumn('used_amount');
|
||||
table.dropColumn('applied_amount');
|
||||
table.dropColumn('unearned_revenue_account_id');
|
||||
});
|
||||
};
|
||||
|
||||
@@ -31,26 +31,27 @@ export class AutoApplyUnearnedRevenue {
|
||||
): Promise<void> {
|
||||
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
|
||||
|
||||
const unappliedPayments = await PaymentReceive.query(trx).where(
|
||||
'unappliedAmount',
|
||||
'>',
|
||||
0
|
||||
);
|
||||
const invoice = await SaleInvoice.query(trx)
|
||||
.findById(saleInvoiceId)
|
||||
.throwIfNotFound();
|
||||
|
||||
const unappliedPayments = await PaymentReceive.query(trx)
|
||||
.where('customerId', invoice.customerId)
|
||||
.whereRaw('amount - applied_amount > 0')
|
||||
.whereNotNull('unearnedRevenueAccountId');
|
||||
|
||||
let unappliedAmount = invoice.total;
|
||||
let appliedTotalAmount = 0;
|
||||
let appliedTotalAmount = 0; // Total applied amount after applying.
|
||||
|
||||
const processHandler: ProcessHandler<
|
||||
IPaymentReceive,
|
||||
Promise<void>
|
||||
> = async (unappliedPayment: IPaymentReceive, index: number, pool) => {
|
||||
const appliedAmount = Math.min(unappliedAmount, unappliedAmount);
|
||||
const appliedAmount = Math.min(unappliedAmount, unappliedPayment.amount);
|
||||
unappliedAmount = unappliedAmount - appliedAmount;
|
||||
appliedTotalAmount += appliedAmount;
|
||||
|
||||
// Stop applying once the unapplied amount reache zero or less.
|
||||
if (appliedAmount <= 0) {
|
||||
pool.stop();
|
||||
return;
|
||||
@@ -108,7 +109,7 @@ export class AutoApplyUnearnedRevenue {
|
||||
invoiceId,
|
||||
paymentAmount: appliedAmount,
|
||||
});
|
||||
await PaymentReceive.query(trx).increment('usedAmount', appliedAmount);
|
||||
await PaymentReceive.query(trx).increment('appliedAmount', appliedAmount);
|
||||
|
||||
// Triggers the event `onPaymentReceivedUnearnedRevenue`.
|
||||
await this.eventPublisher.emitAsync(
|
||||
|
||||
@@ -37,7 +37,6 @@ export class PaymentReceiveDTOTransformer {
|
||||
oldPaymentReceive?: IPaymentReceive
|
||||
): Promise<IPaymentReceive> {
|
||||
const appliedAmount = sumBy(paymentReceiveDTO.entries, 'paymentAmount');
|
||||
const unappliedAmount = paymentReceiveDTO.amount - appliedAmount;
|
||||
|
||||
// Retreive the next invoice number.
|
||||
const autoNextNumber =
|
||||
@@ -55,7 +54,7 @@ export class PaymentReceiveDTOTransformer {
|
||||
...formatDateFields(omit(paymentReceiveDTO, ['entries', 'attachments']), [
|
||||
'paymentDate',
|
||||
]),
|
||||
unappliedAmount,
|
||||
appliedAmount,
|
||||
currencyCode: customer.currencyCode,
|
||||
...(paymentReceiveNo ? { paymentReceiveNo } : {}),
|
||||
exchangeRate: paymentReceiveDTO.exchangeRate || 1,
|
||||
|
||||
Reference in New Issue
Block a user