refactoring: payment receive form.

This commit is contained in:
a.bouhuolia
2021-02-17 19:45:18 +02:00
parent 95cccfd13b
commit e36817cb88
46 changed files with 775 additions and 325 deletions

View File

@@ -48,10 +48,10 @@ export default class PaymentReceivesController extends BaseController {
this.handleServiceErrors
);
router.get(
'/:id',
'/:id/edit-page',
this.paymentReceiveValidation,
this.validationResult,
asyncMiddleware(this.getPaymentReceive.bind(this)),
asyncMiddleware(this.getPaymentReceiveEditPage.bind(this)),
this.handleServiceErrors
);
router.get(
@@ -215,16 +215,15 @@ export default class PaymentReceivesController extends BaseController {
* @param {Request} req -
* @param {Response} res -
*/
async getPaymentReceive(req: Request, res: Response, next: NextFunction) {
async getPaymentReceiveEditPage(req: Request, res: Response, next: NextFunction) {
const { tenantId, user } = req;
const { id: paymentReceiveId } = req.params;
try {
const {
paymentReceive,
receivableInvoices,
paymentReceiveInvoices,
} = await this.paymentReceiveService.getPaymentReceive(
entries
} = await this.paymentReceiveService.getPaymentReceiveEditPage(
tenantId,
paymentReceiveId,
user
@@ -232,8 +231,7 @@ export default class PaymentReceivesController extends BaseController {
return res.status(200).send({
payment_receive: this.transfromToResponse({ ...paymentReceive }),
receivable_invoices: this.transfromToResponse([...receivableInvoices]),
payment_invoices: this.transfromToResponse([...paymentReceiveInvoices]),
entries: this.transfromToResponse([...entries]),
});
} catch (error) {
next(error);

View File

@@ -1,5 +1,5 @@
import { IDynamicListFilterDTO } from "./DynamicFilter";
import { IDynamicListFilterDTO } from "./DynamicFilter";
export interface IPaymentReceive {
id?: number,
@@ -50,4 +50,20 @@ export interface IPaymentReceiveEntryDTO {
export interface IPaymentReceivesFilter extends IDynamicListFilterDTO {
stringifiedFilterRoles?: string,
}
}
export interface IPaymentReceiveEditPageEntry {
invoiceId: number,
entryType: string,
invoiceNo: string,
dueAmount: number,
amount: number,
totalPaymentAmount: number,
paymentAmount: number,
date: Date|string,
};
export interface IPaymentReceiveEditPage {
paymentReceive: IPaymentReceive,
entries: IPaymentReceiveEditPageEntry[];
};

View File

@@ -1,5 +1,4 @@
import { omit, sumBy, difference } from 'lodash';
import moment from 'moment';
import { Service, Inject } from 'typedi';
import {
EventDispatcher,
@@ -19,6 +18,7 @@ import {
ISaleInvoice,
ISystemService,
ISystemUser,
IPaymentReceiveEditPageEntry,
} from 'interfaces';
import AccountsService from 'services/Accounts/AccountsService';
import JournalPoster from 'services/Accounting/JournalPoster';
@@ -466,46 +466,80 @@ export default class PaymentReceiveService {
});
}
/**
* Retrive edit page invoices entries from the given sale invoices models.
* @param {ISaleInvoice[]} invoices - Invoices.
* @return {IPaymentReceiveEditPageEntry}
*/
public invoicesToEditPageEntries(
invoice: ISaleInvoice
): IPaymentReceiveEditPageEntry {
return {
entryType: 'invoice',
invoiceId: invoice.id,
dueAmount: invoice.dueAmount + invoice.paymentAmount,
amount: invoice.balance,
invoiceNo: invoice.invoiceNo,
totalPaymentAmount: invoice.paymentAmount,
paymentAmount: invoice.paymentAmount,
date: invoice.invoiceDate,
};
}
/**
* Retrieve the payment receive details of the given id.
* @param {number} tenantId - Tenant id.
* @param {Integer} paymentReceiveId - Payment receive id.
*/
public async getPaymentReceive(
public async getPaymentReceiveEditPage(
tenantId: number,
paymentReceiveId: number,
authorizedUser: ISystemService
): Promise<{
paymentReceive: IPaymentReceive;
receivableInvoices: ISaleInvoice[];
paymentReceiveInvoices: ISaleInvoice[];
entries: IPaymentReceiveEditPageEntry[];
}> {
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
// Retrieve payment receive.
const paymentReceive = await PaymentReceive.query()
.findById(paymentReceiveId)
.withGraphFetched('entries.invoice')
.withGraphFetched('customer')
.withGraphFetched('depositAccount');
.withGraphFetched('entries.invoice');
// Throw not found the payment receive.
if (!paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
const invoicesIds = paymentReceive.entries.map((entry) => entry.invoiceId);
// Retrieves all receivable bills that associated to the payment receive transaction.
const receivableInvoices = await SaleInvoice.query()
.modify('dueInvoices')
.where('customer_id', paymentReceive.customerId)
.whereNotIn('id', invoicesIds)
.orderBy('invoice_date', 'ASC');
// Mapping the entries invoices.
const entriesInvoicesIds = paymentReceive.entries.map(
(entry) => entry.invoiceId
);
// Retrieve all payment receive associated invoices.
const paymentReceiveInvoices = paymentReceive.entries.map((entry) => ({
...entry.invoice,
dueAmount: entry.invoice.dueAmount + entry.paymentAmount,
const paymentEntries = paymentReceive.entries.map((entry) => ({
...this.invoicesToEditPageEntries(entry.invoice),
paymentAmount: entry.paymentAmount,
}));
return { paymentReceive, receivableInvoices, paymentReceiveInvoices };
// Retrieves all receivable bills that associated to the payment receive transaction.
const restReceivableInvoices = await SaleInvoice.query()
.modify('dueInvoices')
.where('customer_id', paymentReceive.customerId)
.whereNotIn('id', entriesInvoicesIds)
.orderBy('invoice_date', 'ASC');
const restReceivableEntries = restReceivableInvoices.map(
this.invoicesToEditPageEntries
);
const entries = [
...paymentEntries,
...restReceivableEntries,
];
return {
paymentReceive: omit(paymentReceive, ['entries']),
entries,
};
}
/**
@@ -664,7 +698,7 @@ export default class PaymentReceiveService {
*/
async revertPaymentReceiveJournalEntries(
tenantId: number,
paymentReceiveId: number,
paymentReceiveId: number
) {
const { accountRepository } = this.tenancy.repositories(tenantId);

View File

@@ -583,6 +583,8 @@ export default class SaleInvoicesService {
query.where('customer_id', customerId);
}
});
return salesInvoices;
}
}