Skip to content

Commit

Permalink
Merge pull request #763 from bigcapitalhq/fix-discount-gl-entries
Browse files Browse the repository at this point in the history
fix: discount transactions GL entries
  • Loading branch information
abouolia authored Dec 9, 2024
2 parents 14ae978 + c633fa8 commit baf4c69
Show file tree
Hide file tree
Showing 33 changed files with 872 additions and 111 deletions.
60 changes: 47 additions & 13 deletions packages/server/src/database/seeds/data/accounts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export const OtherExpensesAccount = {
name: 'Other Expenses',
slug: 'other-expenses',
account_type: 'other-expense',
code: '40011',
description: '',
active: 1,
index: 1,
predefined: 1,
};

export const TaxPayableAccount = {
name: 'Tax Payable',
slug: 'tax-payable',
Expand Down Expand Up @@ -39,8 +50,38 @@ export const StripeClearingAccount = {
code: '100020',
active: true,
index: 1,
predefined: true,
}
predefined: true,
};

export const DiscountExpenseAccount = {
name: 'Discount',
slug: 'discount',
account_type: 'other-income',
code: '40008',
active: true,
index: 1,
predefined: true,
};

export const PurchaseDiscountAccount = {
name: 'Purchase Discount',
slug: 'purchase-discount',
account_type: 'other-expense',
code: '40009',
active: true,
index: 1,
predefined: true,
};

export const OtherChargesAccount = {
name: 'Other Charges',
slug: 'other-charges',
account_type: 'other-income',
code: '40010',
active: true,
index: 1,
predefined: true,
};

export default [
{
Expand Down Expand Up @@ -231,17 +272,7 @@ export default [
},

// Expenses
{
name: 'Other Expenses',
slug: 'other-expenses',
account_type: 'other-expense',
parent_account_id: null,
code: '40001',
description: '',
active: 1,
index: 1,
predefined: 1,
},
OtherExpensesAccount,
{
name: 'Cost of Goods Sold',
slug: 'cost-of-goods-sold',
Expand Down Expand Up @@ -358,4 +389,7 @@ export default [
},
UnearnedRevenueAccount,
PrepardExpenses,
DiscountExpenseAccount,
PurchaseDiscountAccount,
OtherChargesAccount,
];
7 changes: 7 additions & 0 deletions packages/server/src/interfaces/SaleInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ export interface ISaleInvoice {
pdfTemplateId?: number;

paymentMethods?: Array<PaymentIntegrationTransactionLink>;

adjustment?: number;
adjustmentLocal?: number | null;

discount?: number;
discountAmount?: number;
discountAmountLocal?: number | null;
}

export enum DiscountType {
Expand Down
3 changes: 3 additions & 0 deletions packages/server/src/interfaces/SaleReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export interface ISaleReceipt {
discountPercentage?: number | null;

adjustment?: number;
adjustmentLocal?: number | null;

discountAmountLocal?: number | null;
}

export interface ISalesReceiptsFilter {
Expand Down
32 changes: 26 additions & 6 deletions packages/server/src/models/Bill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Model, raw, mixin } from 'objection';
import { castArray, defaultTo, difference } from 'lodash';
import moment from 'moment';
import * as R from 'ramda';
import TenantModel from 'models/TenantModel';
import BillSettings from './Bill.Settings';
import ModelSetting from './ModelSetting';
Expand Down Expand Up @@ -55,8 +56,11 @@ export default class Bill extends mixin(TenantModel, [
'amountLocal',

'discountAmount',
'discountAmountLocal',
'discountPercentage',

'adjustmentLocal',

'subtotal',
'subtotalLocal',
'subtotalExludingTax',
Expand Down Expand Up @@ -118,6 +122,15 @@ export default class Bill extends mixin(TenantModel, [
: this.subtotal * (this.discount / 100);
}

/**
* Discount amount in local currency.
* @returns {number | null}
*/
get discountAmountLocal() {
return this.discountAmount ? this.discountAmount * this.exchangeRate : null;
}

/**
/**
* Discount percentage.
* @returns {number | null}
Expand All @@ -126,19 +139,26 @@ export default class Bill extends mixin(TenantModel, [
return this.discountType === DiscountType.Percentage ? this.discount : null;
}

/**
* Adjustment amount in local currency.
* @returns {number | null}
*/
get adjustmentLocal() {
return this.adjustment ? this.adjustment * this.exchangeRate : null;
}

/**
* Invoice total. (Tax included)
* @returns {number}
*/
get total() {
const adjustmentAmount = defaultTo(this.adjustment, 0);

return this.isInclusiveTax
? this.subtotal - this.discountAmount - adjustmentAmount
: this.subtotal +
this.taxAmountWithheld -
this.discountAmount -
adjustmentAmount;
return R.compose(
R.add(adjustmentAmount),
R.subtract(R.__, this.discountAmount),
R.when(R.always(this.isInclusiveTax), R.add(this.taxAmountWithheld))
)(this.subtotal);
}

/**
Expand Down
25 changes: 21 additions & 4 deletions packages/server/src/models/CreditNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ export default class CreditNote extends mixin(TenantModel, [
'subtotalLocal',

'discountAmount',
'discountAmountLocal',
'discountPercentage',

'total',
'totalLocal',

'adjustmentLocal',
];
}

Expand Down Expand Up @@ -92,22 +95,36 @@ export default class CreditNote extends mixin(TenantModel, [
: this.subtotal * (this.discount / 100);
}

/**
* Discount amount in local currency.
* @returns {number}
*/
get discountAmountLocal() {
return this.discountAmount ? this.discountAmount * this.exchangeRate : null;
}

/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
return this.discountType === DiscountType.Percentage ? this.discount : null;
}

/**
* Adjustment amount in local currency.
* @returns {number}
*/
get adjustmentLocal() {
return this.adjustment ? this.adjustment * this.exchangeRate : null;
}

/**
* Credit note total.
* @returns {number}
*/
get total() {
return this.subtotal - this.discountAmount - this.adjustment;
return this.subtotal - this.discountAmount + this.adjustment;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/models/SaleEstimate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default class SaleEstimate extends mixin(TenantModel, [
get total() {
const adjustmentAmount = defaultTo(this.adjustment, 0);

return this.subtotal - this.discountAmount - adjustmentAmount;
return this.subtotal - this.discountAmount + adjustmentAmount;
}

/**
Expand Down
32 changes: 25 additions & 7 deletions packages/server/src/models/SaleInvoice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { mixin, Model, raw } from 'objection';
import * as R from 'ramda';
import { castArray, defaultTo, takeWhile } from 'lodash';
import moment from 'moment';
import TenantModel from 'models/TenantModel';
Expand Down Expand Up @@ -72,12 +73,14 @@ export default class SaleInvoice extends mixin(TenantModel, [

'taxAmountWithheldLocal',
'discountAmount',
'discountAmountLocal',
'discountPercentage',

'total',
'totalLocal',

'writtenoffAmountLocal',
'adjustmentLocal',
];
}

Expand Down Expand Up @@ -142,14 +145,28 @@ export default class SaleInvoice extends mixin(TenantModel, [
: this.subtotal * (this.discount / 100);
}

/**
* Local discount amount.
* @returns {number | null}
*/
get discountAmountLocal() {
return this.discountAmount ? this.discountAmount * this.exchangeRate : null;
}

/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
return this.discountType === DiscountType.Percentage ? this.discount : null;
}

/**
* Adjustment amount in local currency.
* @returns {number | null}
*/
get adjustmentLocal(): number | null {
return this.adjustment ? this.adjustment * this.exchangeRate : null;
}

/**
Expand All @@ -158,11 +175,12 @@ export default class SaleInvoice extends mixin(TenantModel, [
*/
get total() {
const adjustmentAmount = defaultTo(this.adjustment, 0);
const differencies = this.discountAmount + adjustmentAmount;

return this.isInclusiveTax
? this.subtotal - differencies
: this.subtotal + this.taxAmountWithheld - differencies;
return R.compose(
R.add(adjustmentAmount),
R.subtract(R.__, this.discountAmount),
R.when(R.always(this.isInclusiveTax), R.add(this.taxAmountWithheld))
)(this.subtotal);
}

/**
Expand Down
45 changes: 41 additions & 4 deletions packages/server/src/models/SaleReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,16 @@ export default class SaleReceipt extends mixin(TenantModel, [
'total',
'totalLocal',

'adjustment',
'adjustmentLocal',

'discountAmount',
'discountAmountLocal',
'discountPercentage',

'paid',
'paidLocal',

'isClosed',
'isDraft',
];
Expand Down Expand Up @@ -91,14 +98,20 @@ export default class SaleReceipt extends mixin(TenantModel, [
: this.subtotal * (this.discount / 100);
}

/**
* Discount amount in local currency.
* @returns {number | null}
*/
get discountAmountLocal() {
return this.discountAmount ? this.discountAmount * this.exchangeRate : null;
}

/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage
? this.discount
: null;
return this.discountType === DiscountType.Percentage ? this.discount : null;
}

/**
Expand All @@ -108,7 +121,7 @@ export default class SaleReceipt extends mixin(TenantModel, [
get total() {
const adjustmentAmount = defaultTo(this.adjustment, 0);

return this.subtotal - this.discountAmount - adjustmentAmount;
return this.subtotal - this.discountAmount + adjustmentAmount;
}

/**
Expand All @@ -119,6 +132,30 @@ export default class SaleReceipt extends mixin(TenantModel, [
return this.total * this.exchangeRate;
}

/**
* Adjustment amount in local currency.
* @returns {number}
*/
get adjustmentLocal() {
return this.adjustment * this.exchangeRate;
}

/**
* Receipt paid amount.
* @returns {number}
*/
get paid() {
return this.total;
}

/**
* Receipt paid amount in local currency.
* @returns {number}
*/
get paidLocal() {
return this.paid * this.exchangeRate;
}

/**
* Detarmine whether the sale receipt closed.
* @return {boolean}
Expand Down
Loading

0 comments on commit baf4c69

Please sign in to comment.