mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
fix: issue in edit payment receive with invoices payment amount chaning.
This commit is contained in:
@@ -8,62 +8,6 @@ export default class HasItemEntries {
|
||||
@Inject()
|
||||
tenancy: TenancyService;
|
||||
|
||||
/**
|
||||
* Patch items entries to the storage.
|
||||
*
|
||||
* @param {Array} newEntries -
|
||||
* @param {Array} oldEntries -
|
||||
* @param {String} referenceType -
|
||||
* @param {String|Number} referenceId -
|
||||
* @return {Promise}
|
||||
*/
|
||||
async patchItemsEntries(
|
||||
tenantId: number,
|
||||
newEntries: Array<any>,
|
||||
oldEntries: Array<any>,
|
||||
referenceType: string,
|
||||
referenceId: string|number
|
||||
) {
|
||||
const { ItemEntry } = this.tenancy.models(tenantId);
|
||||
|
||||
const entriesHasIds = newEntries.filter((entry) => entry.id);
|
||||
const entriesHasNoIds = newEntries.filter((entry) => !entry.id);
|
||||
const entriesIds = entriesHasIds.map(entry => entry.id);
|
||||
|
||||
const oldEntriesIds = oldEntries.map((e) => e.id);
|
||||
const excludeAttrs = ['id', 'amount'];
|
||||
const opers = [];
|
||||
|
||||
const entriesIdsShouldDelete = difference(
|
||||
oldEntriesIds,
|
||||
entriesIds,
|
||||
);
|
||||
if (entriesIdsShouldDelete.length > 0) {
|
||||
const deleteOper = ItemEntry.query()
|
||||
.whereIn('id', entriesIdsShouldDelete)
|
||||
.delete();
|
||||
opers.push(deleteOper);
|
||||
}
|
||||
entriesHasIds.forEach((entry) => {
|
||||
const updateOper = ItemEntry.query()
|
||||
.where('id', entry.id)
|
||||
.update({
|
||||
...omit(entry, excludeAttrs),
|
||||
});
|
||||
opers.push(updateOper);
|
||||
});
|
||||
entriesHasNoIds.forEach((entry) => {
|
||||
const insertOper = ItemEntry.query()
|
||||
.insert({
|
||||
reference_id: referenceId,
|
||||
reference_type: referenceType,
|
||||
...omit(entry, excludeAttrs),
|
||||
});
|
||||
opers.push(insertOper);
|
||||
});
|
||||
return Promise.all([...opers]);
|
||||
}
|
||||
|
||||
filterNonInventoryEntries(entries: [], items: []) {
|
||||
const nonInventoryItems = items.filter((item: any) => item.type !== 'inventory');
|
||||
const nonInventoryItemsIds = nonInventoryItems.map((i: any) => i.id);
|
||||
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
IPaginationMeta,
|
||||
IPaymentReceive,
|
||||
IPaymentReceiveDTO,
|
||||
IPaymentReceiveCreateDTO,
|
||||
IPaymentReceiveEditDTO,
|
||||
IPaymentReceiveEntry,
|
||||
IPaymentReceiveEntryDTO,
|
||||
IPaymentReceivesFilter,
|
||||
@@ -236,7 +238,7 @@ export default class PaymentReceiveService {
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {IPaymentReceive} paymentReceive
|
||||
*/
|
||||
public async createPaymentReceive(tenantId: number, paymentReceiveDTO: IPaymentReceiveDTO) {
|
||||
public async createPaymentReceive(tenantId: number, paymentReceiveDTO: IPaymentReceiveCreateDTO) {
|
||||
const { PaymentReceive } = this.tenancy.models(tenantId);
|
||||
const paymentAmount = sumBy(paymentReceiveDTO.entries, 'paymentAmount');
|
||||
|
||||
@@ -293,7 +295,7 @@ export default class PaymentReceiveService {
|
||||
public async editPaymentReceive(
|
||||
tenantId: number,
|
||||
paymentReceiveId: number,
|
||||
paymentReceiveDTO: any,
|
||||
paymentReceiveDTO: IPaymentReceiveEditDTO,
|
||||
) {
|
||||
const { PaymentReceive } = this.tenancy.models(tenantId);
|
||||
const paymentAmount = sumBy(paymentReceiveDTO.entries, 'paymentAmount');
|
||||
@@ -310,14 +312,11 @@ export default class PaymentReceiveService {
|
||||
// Validate the deposit account existance and type.
|
||||
this.getDepositAccountOrThrowError(tenantId, paymentReceiveDTO.depositAccountId);
|
||||
|
||||
// Validate customer existance.
|
||||
await this.customersService.getCustomerByIdOrThrowError(tenantId, paymentReceiveDTO.customerId);
|
||||
|
||||
// Validate the entries ids existance on payment receive type.
|
||||
await this.validateEntriesIdsExistance(tenantId, paymentReceiveId, paymentReceiveDTO.entries);
|
||||
|
||||
// Validate payment receive invoices IDs existance and associated to the given customer id.
|
||||
await this.validateInvoicesIDsExistance(tenantId, paymentReceiveDTO.customerId, paymentReceiveDTO.entries);
|
||||
await this.validateInvoicesIDsExistance(tenantId, oldPaymentReceive.customerId, paymentReceiveDTO.entries);
|
||||
|
||||
// Validate invoice payment amount.
|
||||
await this.validateInvoicesPaymentsAmount(tenantId, paymentReceiveDTO.entries, oldPaymentReceive.entries);
|
||||
@@ -536,7 +535,7 @@ export default class PaymentReceiveService {
|
||||
* @param {number} tenantId - Tenant id.
|
||||
* @param {Array} paymentReceiveEntries
|
||||
* @param {Array} newPaymentReceiveEntries
|
||||
* @return
|
||||
* @return {Promise<void>}
|
||||
*/
|
||||
public async saveChangeInvoicePaymentAmount(
|
||||
tenantId: number,
|
||||
|
||||
Reference in New Issue
Block a user