feat: get specifici payment receive/made details.

This commit is contained in:
a.bouhuolia
2021-03-06 11:04:24 +02:00
parent c9c749175e
commit 9c2dfc7bd2
4 changed files with 138 additions and 51 deletions

View File

@@ -31,20 +31,6 @@ export default class BillsPayments extends BaseController {
router() { router() {
const router = Router(); const router = Router();
router.get(
'/new-page/entries',
[query('vendor_id').exists()],
this.validationResult,
asyncMiddleware(this.getBillPaymentNewPageEntries.bind(this)),
this.handleServiceError
);
router.get(
'/:id/edit-page',
this.specificBillPaymentValidateSchema,
this.validationResult,
asyncMiddleware(this.getBillPaymentEditPage.bind(this)),
this.handleServiceError
);
router.post( router.post(
'/', '/',
[...this.billPaymentSchemaValidation], [...this.billPaymentSchemaValidation],
@@ -69,6 +55,20 @@ export default class BillsPayments extends BaseController {
asyncMiddleware(this.deleteBillPayment.bind(this)), asyncMiddleware(this.deleteBillPayment.bind(this)),
this.handleServiceError this.handleServiceError
); );
router.get(
'/new-page/entries',
[query('vendor_id').exists()],
this.validationResult,
asyncMiddleware(this.getBillPaymentNewPageEntries.bind(this)),
this.handleServiceError
);
router.get(
'/:id/edit-page',
this.specificBillPaymentValidateSchema,
this.validationResult,
asyncMiddleware(this.getBillPaymentEditPage.bind(this)),
this.handleServiceError
);
router.get( router.get(
'/:id/bills', '/:id/bills',
this.specificBillPaymentValidateSchema, this.specificBillPaymentValidateSchema,
@@ -159,7 +159,11 @@ export default class BillsPayments extends BaseController {
* @param {Request} req * @param {Request} req
* @param {Response} res * @param {Response} res
*/ */
async getBillPaymentEditPage(req: Request, res: Response, next: NextFunction) { async getBillPaymentEditPage(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req; const { tenantId } = req;
const { id: paymentReceiveId } = req.params; const { id: paymentReceiveId } = req.params;
@@ -169,7 +173,7 @@ export default class BillsPayments extends BaseController {
entries, entries,
} = await this.billPaymentService.getBillPaymentEditPage( } = await this.billPaymentService.getBillPaymentEditPage(
tenantId, tenantId,
paymentReceiveId, paymentReceiveId
); );
return res.status(200).send({ return res.status(200).send({
@@ -265,16 +269,13 @@ export default class BillsPayments extends BaseController {
const { id: billPaymentId } = req.params; const { id: billPaymentId } = req.params;
try { try {
const { const billPayment = await this.billPaymentService.getBillPayment(
billPayment, tenantId,
payableBills, billPaymentId
paymentMadeBills, );
} = await this.billPaymentService.getBillPayment(tenantId, billPaymentId);
return res.status(200).send({ return res.status(200).send({
bill_payment: this.transfromToResponse({ ...billPayment }), bill_payment: this.transfromToResponse(billPayment),
payable_bills: this.transfromToResponse([...payableBills]),
payment_bills: this.transfromToResponse([...paymentMadeBills]),
}); });
} catch (error) { } catch (error) {
next(error); next(error);

View File

@@ -40,13 +40,6 @@ export default class PaymentReceivesController extends BaseController {
asyncMiddleware(this.newPaymentReceive.bind(this)), asyncMiddleware(this.newPaymentReceive.bind(this)),
this.handleServiceErrors this.handleServiceErrors
); );
router.get(
'/:id/invoices',
this.paymentReceiveValidation,
this.validationResult,
asyncMiddleware(this.getPaymentReceiveInvoices.bind(this)),
this.handleServiceErrors
);
router.get( router.get(
'/:id/edit-page', '/:id/edit-page',
this.paymentReceiveValidation, this.paymentReceiveValidation,
@@ -56,13 +49,24 @@ export default class PaymentReceivesController extends BaseController {
); );
router.get( router.get(
'/new-page/entries', '/new-page/entries',
[ [query('customer_id').exists().isNumeric().toInt()],
query('customer_id').exists().isNumeric().toInt(),
],
this.validationResult, this.validationResult,
asyncMiddleware(this.getPaymentReceiveNewPageEntries.bind(this)), asyncMiddleware(this.getPaymentReceiveNewPageEntries.bind(this)),
this.getPaymentReceiveNewPageEntries.bind(this) this.getPaymentReceiveNewPageEntries.bind(this)
); );
router.get(
'/:id/invoices',
this.paymentReceiveValidation,
this.validationResult,
asyncMiddleware(this.getPaymentReceiveInvoices.bind(this)),
this.handleServiceErrors
);
router.get(
'/:id',
this.paymentReceiveValidation,
this.asyncMiddleware(this.getPaymentReceive.bind(this)),
this.handleServiceErrors
);
router.get( router.get(
'/', '/',
this.validatePaymentReceiveList, this.validatePaymentReceiveList,
@@ -224,14 +228,18 @@ export default class PaymentReceivesController extends BaseController {
* @param {Request} req - * @param {Request} req -
* @param {Response} res - * @param {Response} res -
*/ */
async getPaymentReceiveEditPage(req: Request, res: Response, next: NextFunction) { async getPaymentReceiveEditPage(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId, user } = req; const { tenantId, user } = req;
const { id: paymentReceiveId } = req.params; const { id: paymentReceiveId } = req.params;
try { try {
const { const {
paymentReceive, paymentReceive,
entries entries,
} = await this.paymentReceiveService.getPaymentReceiveEditPage( } = await this.paymentReceiveService.getPaymentReceiveEditPage(
tenantId, tenantId,
paymentReceiveId, paymentReceiveId,
@@ -317,7 +325,11 @@ export default class PaymentReceivesController extends BaseController {
* @param {Request} req - Request. * @param {Request} req - Request.
* @param {Response} res - Response. * @param {Response} res - Response.
*/ */
async getPaymentReceiveNewPageEntries(req, res) { async getPaymentReceiveNewPageEntries(
req: Request,
res: Response,
next: NextFunction
) {
const { tenantId } = req; const { tenantId } = req;
const { customerId } = this.matchedQueryData(req); const { customerId } = this.matchedQueryData(req);
@@ -329,7 +341,33 @@ export default class PaymentReceivesController extends BaseController {
return res.status(200).send({ return res.status(200).send({
entries: this.transfromToResponse(entries), entries: this.transfromToResponse(entries),
}); });
} catch (error) {} } catch (error) {
next(error);
}
}
/**
* Retrieve the payment receive details.
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
async getPaymentReceive(req: Request, res: Response, next: NextFunction) {
const { tenantId } = req;
const { id: paymentReceiveId } = req.params;
try {
const paymentReceive = await this.paymentReceiveService.getPaymentReceive(
tenantId,
paymentReceiveId
);
return res
.status(200)
.send({ payment_receive: this.transfromToResponse(paymentReceive) });
} catch (error) {
next(error);
}
} }
/** /**
@@ -363,9 +401,7 @@ export default class PaymentReceivesController extends BaseController {
} }
if (error.errorType === 'DEPOSIT_ACCOUNT_INVALID_TYPE') { if (error.errorType === 'DEPOSIT_ACCOUNT_INVALID_TYPE') {
return res.boom.badRequest(null, { return res.boom.badRequest(null, {
errors: [ errors: [{ type: 'DEPOSIT_ACCOUNT_INVALID_TYPE', code: 300 }],
{ type: 'DEPOSIT_ACCOUNT_INVALID_TYPE', code: 300 },
],
}); });
} }
if (error.errorType === 'INVALID_PAYMENT_AMOUNT_INVALID') { if (error.errorType === 'INVALID_PAYMENT_AMOUNT_INVALID') {

View File

@@ -482,6 +482,31 @@ export default class BillPaymentsService {
return bills; return bills;
} }
/**
* Retrieve bill payment.
* @param {number} tenantId
* @param {number} billPyamentId
* @return {Promise<IBillPayment>}
*/
public async getBillPayment(
tenantId: number,
billPyamentId: number
): Promise<IBillPayment> {
const { BillPayment } = this.tenancy.models(tenantId);
const billPayment = await BillPayment.query()
.withGraphFetched('entries')
.withGraphFetched('vendor')
.withGraphFetched('paymentAccount')
.withGraphFetched('transactions')
.findById(billPyamentId);
if (!billPayment) {
throw new ServiceError(ERRORS.PAYMENT_MADE_NOT_FOUND);
}
return billPayment;
}
/** /**
* Records bill payment receive journal transactions. * Records bill payment receive journal transactions.
* @param {number} tenantId - * @param {number} tenantId -
@@ -491,7 +516,7 @@ export default class BillPaymentsService {
public async recordJournalEntries( public async recordJournalEntries(
tenantId: number, tenantId: number,
billPayment: IBillPayment, billPayment: IBillPayment,
override: boolean = false, override: boolean = false
) { ) {
const { AccountTransaction } = this.tenancy.models(tenantId); const { AccountTransaction } = this.tenancy.models(tenantId);
const { accountRepository } = this.tenancy.repositories(tenantId); const { accountRepository } = this.tenancy.repositories(tenantId);
@@ -612,7 +637,7 @@ export default class BillPaymentsService {
tenantId: number, tenantId: number,
billPaymentId: number billPaymentId: number
): Promise<{ ): Promise<{
billPayment: Omit<IBillPayment, "entries">; billPayment: Omit<IBillPayment, 'entries'>;
entries: IBillReceivePageEntry[]; entries: IBillReceivePageEntry[];
}> { }> {
const { BillPayment, Bill } = this.tenancy.models(tenantId); const { BillPayment, Bill } = this.tenancy.models(tenantId);
@@ -634,7 +659,7 @@ export default class BillPaymentsService {
.where('vendor_id', billPayment.vendorId) .where('vendor_id', billPayment.vendorId)
.whereNotIn( .whereNotIn(
'id', 'id',
billPayment.entries.map((e) => e.billId), billPayment.entries.map((e) => e.billId)
) )
.orderBy('bill_date', 'ASC'); .orderBy('bill_date', 'ASC');
@@ -644,7 +669,7 @@ export default class BillPaymentsService {
return { return {
billPayment: omit(billPayment, ['entries']), billPayment: omit(billPayment, ['entries']),
entries entries,
}; };
} }
@@ -712,7 +737,7 @@ export default class BillPaymentsService {
date: bill.billDate, date: bill.billDate,
totalPaymentAmount: bill.paymentAmount, totalPaymentAmount: bill.paymentAmount,
paymentAmount: 0, paymentAmount: 0,
} };
} }
/** /**
@@ -722,7 +747,7 @@ export default class BillPaymentsService {
*/ */
async getNewPageEntries( async getNewPageEntries(
tenantId: number, tenantId: number,
vendorId: number, vendorId: number
): Promise<IBillReceivePageEntry[]> { ): Promise<IBillReceivePageEntry[]> {
const { Bill } = this.tenancy.models(tenantId); const { Bill } = this.tenancy.models(tenantId);

View File

@@ -465,6 +465,31 @@ export default class PaymentReceiveService {
}); });
} }
/**
* Retrieve payment receive details.
* @param {number} tenantId - Tenant id.
* @param {number} paymentReceiveId - Payment receive id.
* @return {Promise<IPaymentReceive>}
*/
async getPaymentReceive(
tenantId: number,
paymentReceiveId: number
): Promise<IPaymentReceive> {
const { PaymentReceive } = this.tenancy.models(tenantId);
const paymentReceive = await PaymentReceive.query()
.withGraphFetched('customer')
.withGraphFetched('depositAccount')
.withGraphFetched('entries')
.withGraphFetched('transactions')
.findById(paymentReceiveId);
if (!paymentReceive) {
throw new ServiceError(ERRORS.PAYMENT_RECEIVE_NOT_EXISTS);
}
return paymentReceive;
}
/** /**
* Retrive edit page invoices entries from the given sale invoices models. * Retrive edit page invoices entries from the given sale invoices models.
* @param {ISaleInvoice[]} invoices - Invoices. * @param {ISaleInvoice[]} invoices - Invoices.