Skip to content

Commit

Permalink
Merge pull request #289 from bigcapitalhq/big-80-entering-decimal-amo…
Browse files Browse the repository at this point in the history
…unt-in-salepurchase-transactions

fix(server): allow decimal amount in sale/purchase transactions.
  • Loading branch information
abouolia authored Dec 4, 2023
2 parents 6091bc7 + 33afa3a commit c9d3a3c
Show file tree
Hide file tree
Showing 32 changed files with 239 additions and 70 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/api/controllers/Purchases/Bills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export default class BillsController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export default class BillsPayments extends BaseController {
check('entries').exists().isArray({ min: 1 }),
check('entries.*.index').optional().isNumeric().toInt(),
check('entries.*.bill_id').exists().isNumeric().toInt(),
check('entries.*.payment_amount').exists().isNumeric().toInt(),
check('entries.*.payment_amount').exists().isNumeric().toFloat(),
];
}

Expand Down
5 changes: 2 additions & 3 deletions packages/server/src/api/controllers/Purchases/VendorCredit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default class VendorCreditController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down Expand Up @@ -211,12 +211,11 @@ export default class VendorCreditController extends BaseController {
check('branch_id').optional({ nullable: true }).isNumeric().toInt(),

check('entries').isArray({ min: 1 }),

check('entries.*.id').optional().isNumeric().toInt(),
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/api/controllers/Sales/CreditNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export default class PaymentReceivesController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toFloat(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default class PaymentReceivesController extends BaseController {
check('entries.*.id').optional({ nullable: true }).isNumeric().toInt(),
check('entries.*.index').optional().isNumeric().toInt(),
check('entries.*.invoice_id').exists().isNumeric().toInt(),
check('entries.*.payment_amount').exists().isNumeric().toInt(),
check('entries.*.payment_amount').exists().isNumeric().toFloat(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/api/controllers/Sales/SalesReceipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class SalesReceiptsController extends BaseController {
check('entries.*.index').exists().isNumeric().toInt(),
check('entries.*.item_id').exists().isNumeric().toInt(),
check('entries.*.quantity').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toInt(),
check('entries.*.rate').exists().isNumeric().toFloat(),
check('entries.*.discount')
.optional({ nullable: true })
.isNumeric()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.up = function (knex) {
return knex.schema.alterTable('items_entries', (table) => {
table.decimal('rate', 15, 5).alter();
});
};

exports.down = function (knex) {
return knex.table('items_entries', (table) => {});
};
5 changes: 3 additions & 2 deletions packages/server/src/interfaces/Roles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Knex } from 'knex';
import { Ability, RawRuleOf, ForcedSubject } from '@casl/ability';
import Knex from 'knex';

export const actions = [
'manage',
Expand Down Expand Up @@ -96,7 +96,8 @@ export enum AbilitySubject {
Preferences = 'Preferences',
CreditNote = 'CreditNode',
VendorCredit = 'VendorCredit',
Project = 'Project'
Project = 'Project',
TaxRate = 'TaxRate'
}

export interface IRoleCreatedPayload {
Expand Down
18 changes: 16 additions & 2 deletions packages/server/src/services/CreditNotes/CreditNoteTransformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils';
import { ItemEntryTransformer } from '../Sales/Invoices/ItemEntryTransformer';
import { ICreditNote } from '@/interfaces';

export class CreditNoteTransformer extends Transformer {
/**
Expand All @@ -11,7 +13,8 @@ export class CreditNoteTransformer extends Transformer {
'formattedCreditsRemaining',
'formattedCreditNoteDate',
'formattedAmount',
'formattedCreditsUsed'
'formattedCreditsUsed',
'entries',
];
};

Expand Down Expand Up @@ -51,9 +54,20 @@ export class CreditNoteTransformer extends Transformer {
* @param {ICreditNote} credit
* @returns {string}
*/
protected formattedCreditsUsed = (credit) => {
protected formattedCreditsUsed = (credit) => {
return formatNumber(credit.creditsUsed, {
currencyCode: credit.currencyCode,
});
};

/**
* Retrieves the entries of the credit note.
* @param {ICreditNote} credit
* @returns {}
*/
protected entries = (credit) => {
return this.item(credit.entries, new ItemEntryTransformer(), {
currencyCode: credit.currencyCode,
});
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from '@/utils';

export class BillPaymentEntryTransformer extends Transformer {
/**
* Include these attributes to bill payment object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['paymentAmountFormatted'];
};

/**
* Retreives the payment amount formatted.
* @returns {string}
*/
protected paymentAmountFormatted(entry) {
return formatNumber(entry.paymentAmount, { money: false });
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { IBillPayment } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils';
import { BillPaymentEntryTransformer } from './BillPaymentEntryTransformer';

export class BillPaymentTransformer extends Transformer {
/**
* Include these attributes to sale invoice object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['formattedPaymentDate', 'formattedAmount'];
return ['formattedPaymentDate', 'formattedAmount', 'entries'];
};

/**
Expand All @@ -30,4 +31,11 @@ export class BillPaymentTransformer extends Transformer {
currencyCode: billPayment.currencyCode,
});
};

/**
* Retreives the bill payment entries.
*/
protected entries = (billPayment) => {
return this.item(billPayment.entries, new BillPaymentEntryTransformer());
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IBill } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { ItemEntryTransformer } from '@/services/Sales/Invoices/ItemEntryTransformer';
import { SaleInvoiceTaxEntryTransformer } from '@/services/Sales/Invoices/SaleInvoiceTaxEntryTransformer';
import { formatNumber } from 'utils';

Expand All @@ -23,6 +24,7 @@ export class PurchaseInvoiceTransformer extends Transformer {
'totalFormatted',
'totalLocalFormatted',
'taxes',
'entries',
];
};

Expand Down Expand Up @@ -178,4 +180,15 @@ export class PurchaseInvoiceTransformer extends Transformer {
currencyCode: bill.currencyCode,
});
};

/**
* Retrieves the entries of the bill.
* @param {Bill} credit
* @returns {}
*/
protected entries = (bill) => {
return this.item(bill.entries, new ItemEntryTransformer(), {
currencyCode: bill.currencyCode,
});
};
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IVendorCredit } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { ItemEntryTransformer } from '@/services/Sales/Invoices/ItemEntryTransformer';
import { formatNumber } from 'utils';

export class VendorCreditTransformer extends Transformer {
Expand All @@ -8,9 +10,10 @@ export class VendorCreditTransformer extends Transformer {
*/
public includeAttributes = (): string[] => {
return [
'formattedVendorCreditDate',
'formattedAmount',
'formattedVendorCreditDate',
'formattedCreditsRemaining',
'entries',
];
};

Expand Down Expand Up @@ -44,4 +47,15 @@ export class VendorCreditTransformer extends Transformer {
currencyCode: credit.currencyCode,
});
};

/**
* Retrieves the entries of the bill.
* @param {IVendorCredit} vendorCredit
* @returns {}
*/
protected entries = (vendorCredit) => {
return this.item(vendorCredit.entries, new ItemEntryTransformer(), {
currencyCode: vendorCredit.currencyCode,
});
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Service } from 'typedi';
import { ISaleEstimate } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils';
import { ItemEntryTransformer } from '../Invoices/ItemEntryTransformer';

export class SaleEstimateTransfromer extends Transformer {
/**
Expand All @@ -16,6 +16,7 @@ export class SaleEstimateTransfromer extends Transformer {
'formattedDeliveredAtDate',
'formattedApprovedAtDate',
'formattedRejectedAtDate',
'entries',
];
};

Expand Down Expand Up @@ -74,4 +75,15 @@ export class SaleEstimateTransfromer extends Transformer {
currencyCode: estimate.currencyCode,
});
};

/**
* Retrieves the entries of the sale estimate.
* @param {ISaleEstimate} estimate
* @returns {}
*/
protected entries = (estimate) => {
return this.item(estimate.entries, new ItemEntryTransformer(), {
currencyCode: estimate.currencyCode,
});
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { IItemEntry } from '@/interfaces';
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from '@/utils';

export class ItemEntryTransformer extends Transformer {
/**
* Include these attributes to item entry object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['rateFormatted', 'totalFormatted'];
};

/**
* Retrieves the formatted rate of item entry.
* @param {IItemEntry} itemEntry -
* @returns {string}
*/
protected rateFormatted = (entry: IItemEntry): string => {
return formatNumber(entry.rate, {
currencyCode: this.context.currencyCode,
money: false,
});
};

/**
* Retrieves the formatted total of item entry.
* @param {IItemEntry} entry
* @returns {string}
*/
protected totalFormatted = (entry: IItemEntry): string => {
return formatNumber(entry.total, {
currencyCode: this.context.currencyCode,
money: false,
});
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { formatNumber } from 'utils';
import { SaleInvoiceTaxEntryTransformer } from './SaleInvoiceTaxEntryTransformer';
import { ItemEntryTransformer } from './ItemEntryTransformer';

export class SaleInvoiceTransformer extends Transformer {
/**
Expand All @@ -23,6 +24,7 @@ export class SaleInvoiceTransformer extends Transformer {
'totalFormatted',
'totalLocalFormatted',
'taxes',
'entries',
];
};

Expand Down Expand Up @@ -95,6 +97,7 @@ export class SaleInvoiceTransformer extends Transformer {
protected subtotalFormatted = (invoice): string => {
return formatNumber(invoice.subtotal, {
currencyCode: this.context.organization.baseCurrency,
money: false,
});
};

Expand Down Expand Up @@ -176,4 +179,15 @@ export class SaleInvoiceTransformer extends Transformer {
currencyCode: invoice.currencyCode,
});
};

/**
* Retrieves the entries of the sale invoice.
* @param {ISaleInvoice} invoice
* @returns {}
*/
protected entries = (invoice) => {
return this.item(invoice.entries, new ItemEntryTransformer(), {
currencyCode: invoice.currencyCode,
});
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { Inject, Service } from 'typedi';
import HasTenancyService from '@/services/Tenancy/TenancyService';
import { PaymentReceiveValidators } from './PaymentReceiveValidators';

@Service()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Transformer } from '@/lib/Transformer/Transformer';
import { SaleInvoiceTransformer } from '../Invoices/SaleInvoiceTransformer';
import { formatNumber } from '@/utils';

export class PaymentReceiveEntryTransfromer extends Transformer {
/**
* Include these attributes to payment receive entry object.
* @returns {Array}
*/
public includeAttributes = (): string[] => {
return ['paymentAmountFormatted', 'entry'];
};

/**
* Retreives the payment amount formatted.
* @param entry
* @returns {string}
*/
protected paymentAmountFormatted(entry) {
return formatNumber(entry.paymentAmount, { money: false });
}

/**
* Retreives the transformed invoice.
*/
protected invoice(entry) {
return this.item(entry.invoice, new SaleInvoiceTransformer());
}
}
Loading

0 comments on commit c9d3a3c

Please sign in to comment.