From a457005fd8ed0b6b5f9f31ac5eeeed853c181c6e Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Wed, 28 Oct 2020 22:41:16 +0200 Subject: [PATCH] feat: retrieve bills that associated to payment made transaction. feat: retrieve sale invoices that associated to payment receive transactions. --- .../controllers/Purchases/BillsPayments.ts | 32 +++++++++++++++++-- .../api/controllers/Sales/PaymentReceives.ts | 27 ++++++++++++++++ server/src/services/Purchases/BillPayments.ts | 17 ++++++++++ server/src/services/Sales/PaymentsReceives.ts | 21 ++++++++++-- 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/server/src/api/controllers/Purchases/BillsPayments.ts b/server/src/api/controllers/Purchases/BillsPayments.ts index f175e09fd..b4a81285c 100644 --- a/server/src/api/controllers/Purchases/BillsPayments.ts +++ b/server/src/api/controllers/Purchases/BillsPayments.ts @@ -8,6 +8,7 @@ import BaseController from 'api/controllers/BaseController'; import BillPaymentsService from 'services/Purchases/BillPayments'; import DynamicListingService from 'services/DynamicListing/DynamicListService'; import AccountsService from 'services/Accounts/AccountsService'; +import ResourceController from '../Resources'; /** * Bills payments controller. @@ -52,6 +53,12 @@ export default class BillsPayments extends BaseController { asyncMiddleware(this.deleteBillPayment.bind(this)), this.handleServiceError, ); + router.get('/:id/bills', + this.specificBillPaymentValidateSchema, + this.validationResult, + asyncMiddleware(this.getPaymentBills.bind(this)), + this.handleServiceError, + ); router.get('/:id', this.specificBillPaymentValidateSchema, this.validationResult, @@ -190,9 +197,30 @@ export default class BillsPayments extends BaseController { const { tenantId } = req; const { id: billPaymentId } = req.params; - const billPayment = await this.billPaymentService.getBillPayment(tenantId, billPaymentId); + try { + const billPayment = await this.billPaymentService.getBillPayment(tenantId, billPaymentId); + return res.status(200).send({ bill_payment: billPayment }); + } catch (error) { + next(error); + } + } - return res.status(200).send({ bill_payment: billPayment }); + /** + * Retrieve associated bills for the given payment made. + * @param {Request} req + * @param {Response} res + * @param {NextFunction} next + */ + async getPaymentBills(req: Request, res: Response, next: NextFunction) { + const { tenantId } = req; + const { id: billPaymentId } = req.params; + + try { + const bills = await this.billPaymentService.getPaymentBills(tenantId, billPaymentId); + return res.status(200).send({ bills }); + } catch (error) { + next(error); + } } /** diff --git a/server/src/api/controllers/Sales/PaymentReceives.ts b/server/src/api/controllers/Sales/PaymentReceives.ts index a2faf945d..997ce1ee7 100644 --- a/server/src/api/controllers/Sales/PaymentReceives.ts +++ b/server/src/api/controllers/Sales/PaymentReceives.ts @@ -40,6 +40,13 @@ export default class PaymentReceivesController extends BaseController { asyncMiddleware(this.newPaymentReceive.bind(this)), this.handleServiceErrors, ); + router.get( + '/:id/invoices', + this.paymentReceiveValidation, + this.validationResult, + asyncMiddleware(this.getPaymentReceiveInvoices.bind(this)), + this.handleServiceErrors, + ); router.get( '/:id', this.paymentReceiveValidation, @@ -209,6 +216,26 @@ export default class PaymentReceivesController extends BaseController { } } + /** + * Retrieve sale invoices that associated with the given payment receive. + * @param {Request} req + * @param {Response} res + * @param {NextFunction} next + */ + async getPaymentReceiveInvoices(req: Request, res: Response, next: NextFunction) { + const { tenantId } = req; + const { id: paymentReceiveId } = req.params; + + try { + const invoices = await this.paymentReceiveService.getPaymentReceiveInvoices( + tenantId, paymentReceiveId, + ); + return res.status(200).send({ sale_invoices: invoices }); + } catch (error) { + next(error); + } + } + /** * Retrieve payment receive list with pagination metadata. * @param {Request} req diff --git a/server/src/services/Purchases/BillPayments.ts b/server/src/services/Purchases/BillPayments.ts index edbba8562..7b3b35194 100644 --- a/server/src/services/Purchases/BillPayments.ts +++ b/server/src/services/Purchases/BillPayments.ts @@ -25,6 +25,7 @@ import TenancyService from 'services/Tenancy/TenancyService'; import DynamicListingService from 'services/DynamicListing/DynamicListService'; import { entriesAmountDiff, formatDateFields } from 'utils'; import { ServiceError } from 'exceptions'; +import { Bill } from 'models'; const ERRORS = { BILL_VENDOR_NOT_FOUND: 'VENDOR_NOT_FOUND', @@ -366,6 +367,22 @@ export default class BillPaymentsService { this.logger.info('[bill_payment] deleted successfully.', { tenantId, billPaymentId }); } + /** + * Retrieve payment made associated bills. + * @param {number} tenantId - + * @param {number} billPaymentId - + */ + public async getPaymentBills(tenantId: number, billPaymentId: number) { + const { Bill } = this.tenancy.models(tenantId); + + const billPayment = await this.getPaymentMadeOrThrowError(tenantId, billPaymentId); + const paymentBillsIds = billPayment.entries.map((entry) => entry.id); + + const bills = await Bill.query().whereIn('id', paymentBillsIds); + + return bills; + } + /** * Records bill payment receive journal transactions. * @param {number} tenantId - diff --git a/server/src/services/Sales/PaymentsReceives.ts b/server/src/services/Sales/PaymentsReceives.ts index a77bd4163..ad9b61caa 100644 --- a/server/src/services/Sales/PaymentsReceives.ts +++ b/server/src/services/Sales/PaymentsReceives.ts @@ -26,6 +26,7 @@ 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', @@ -337,7 +338,24 @@ export default class PaymentReceiveService { } return paymentReceive; } - + + /** + * Retrieve sale invoices that assocaited to the given payment receive. + * @param {number} tenantId - Tenant id. + * @param {number} paymentReceiveId - Payment receive id. + * @return {Promise} + */ + public async getPaymentReceiveInvoices(tenantId: number, paymentReceiveId: number) { + const { SaleInvoice } = this.tenancy.models(tenantId); + + const paymentReceive = await this.getPaymentReceiveOrThrowError(tenantId, paymentReceiveId); + const paymentReceiveInvoicesIds = paymentReceive.entries.map(entry => entry.invoiceId); + + const saleInvoices = await SaleInvoice.query().whereIn('id', paymentReceiveInvoicesIds); + + return saleInvoices; + } + /** * Retrieve payment receives paginated and filterable list. * @param {number} tenantId @@ -442,7 +460,6 @@ export default class PaymentReceiveService { ]); } - /** * Saves difference changing between old and new invoice payment amount. * @async