feat: payment receive of customers invoices.

This commit is contained in:
Ahmed Bouhuolia
2020-11-02 21:42:40 +02:00
parent 731b8fd119
commit e6cd921b94
24 changed files with 911 additions and 645 deletions

View File

@@ -34,8 +34,9 @@ export default class PaymentReceivesController extends BaseController {
this.handleServiceErrors,
);
router.post(
'/',
this.newPaymentReceiveValidation,
'/', [
...this.newPaymentReceiveValidation,
],
this.validationResult,
asyncMiddleware(this.newPaymentReceive.bind(this)),
this.handleServiceErrors,
@@ -87,6 +88,7 @@ export default class PaymentReceivesController extends BaseController {
check('entries').isArray({ min: 1 }),
check('entries.*.id').optional({ nullable: true }).isNumeric().toInt(),
check('entries.*.invoice_id').exists().isNumeric().toInt(),
check('entries.*.payment_amount').exists().isNumeric().toInt(),
];

View File

@@ -1,4 +1,4 @@
import { omit, sumBy, chain, difference } from 'lodash';
import { omit, sumBy, difference } from 'lodash';
import moment from 'moment';
import { Service, Inject } from 'typedi';
import {
@@ -26,7 +26,6 @@ import { formatDateFields, entriesAmountDiff } from 'utils';
import { ServiceError } from 'exceptions';
import CustomersService from 'services/Contacts/CustomersService';
import ItemsEntriesService from 'services/Items/ItemsEntriesService';
import { SaleInvoice } from 'models';
const ERRORS = {
PAYMENT_RECEIVE_NO_EXISTS: 'PAYMENT_RECEIVE_NO_EXISTS',
@@ -327,16 +326,30 @@ export default class PaymentReceiveService {
* @param {number} tenantId - Tenant id.
* @param {Integer} paymentReceiveId - Payment receive id.
*/
public async getPaymentReceive(tenantId: number, paymentReceiveId: number) {
const { PaymentReceive } = this.tenancy.models(tenantId);
public async getPaymentReceive(
tenantId: number,
paymentReceiveId: number
): Promise<{ paymentReceive: IPaymentReceive[], receivableInvoices: ISaleInvoice }> {
const { PaymentReceive, SaleInvoice } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.findById(paymentReceiveId)
.withGraphFetched('entries.invoice');
.withGraphFetched('entries')
.withGraphFetched('customer')
.withGraphFetched('depositAccount');
if (!paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
return paymentReceive;
// Receivable open invoices.
const receivableInvoices = await SaleInvoice.query().onBuild((builder) => {
const invoicesIds = paymentReceive.entries.map((entry) => entry.invoiceId);
builder.where('customer_id', paymentReceive.customerId);
builder.orWhereIn('id', invoicesIds);
builder.orderByRaw(`FIELD(id, ${invoicesIds.join(', ')}) DESC`);
builder.orderBy('invoice_date', 'ASC');
});
return { paymentReceive, receivableInvoices };
}
/**