From 4e68a7db71cd4dc28fd56a385c162beb32e17650 Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Sat, 15 Aug 2020 16:30:52 +0200 Subject: [PATCH] feat: Listing due sales invoices for specific customer. --- .../http/controllers/Sales/SalesInvoices.js | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/server/src/http/controllers/Sales/SalesInvoices.js b/server/src/http/controllers/Sales/SalesInvoices.js index 23d129dbb..d15b8a032 100644 --- a/server/src/http/controllers/Sales/SalesInvoices.js +++ b/server/src/http/controllers/Sales/SalesInvoices.js @@ -1,6 +1,7 @@ import express from 'express'; import { check, param, query } from 'express-validator'; import { difference } from 'lodash'; +import { raw } from 'objection'; import { ItemEntry } from '@/models'; import validateMiddleware from '@/http/middleware/validateMiddleware'; import asyncMiddleware from '@/http/middleware/asyncMiddleware'; @@ -10,6 +11,7 @@ import CustomersService from '@/services/Customers/CustomersService'; import DynamicListing from '@/services/DynamicListing/DynamicListing'; import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder'; import { dynamicListingErrorsToResponse } from '@/services/DynamicListing/hasDynamicListing'; +import { Customer } from '../../../models'; export default class SaleInvoicesController { /** @@ -49,6 +51,11 @@ export default class SaleInvoicesController { asyncMiddleware(this.validateInvoiceExistance), asyncMiddleware(this.deleteSaleInvoice) ); + router.get( + '/due_invoices', + this.dueSalesInvoicesListValidationSchema, + asyncMiddleware(this.getDueSalesInvoice), + ); router.get( '/:id', this.specificSaleInvoiceValidation, @@ -60,7 +67,7 @@ export default class SaleInvoicesController { '/', this.saleInvoiceListValidationSchema, asyncMiddleware(this.getSalesInvoices) - ); + ) return router; } @@ -111,6 +118,13 @@ export default class SaleInvoicesController { ]; } + + static get dueSalesInvoicesListValidationSchema() { + return [ + query('customer_id').optional().isNumeric().toInt(), + ] + } + /** * Validate whether sale invoice customer exists on the storage. * @param {Request} req @@ -316,6 +330,39 @@ export default class SaleInvoicesController { return res.status(200).send({ sale_invoice: saleInvoice }); } + /** + * Retrieve the due sales invoices for the given customer. + * @param {Request} req + * @param {Response} res + */ + static async getDueSalesInvoice(req, res) { + const filter = { + customer_id: null, + ...req.query, + }; + const { Customer, SaleInvoice } = req.models; + + if (filter.customer_id) { + const foundCustomer = await Customer.query().findById(filter.customer_id); + + if (!foundCustomer) { + return res.status(200).send({ + errors: [{ type: 'CUSTOMER.NOT.FOUND', code: 200 }], + }); + } + } + const dueSalesInvoices = await SaleInvoice.query().onBuild((query) => { + query.where(raw('BALANCE - PAYMENT_AMOUNT > 0')); + + if (filter.customer_id) { + query.where('customer_id', filter.customer_id); + } + }); + return res.status(200).send({ + due_sales_invoices: dueSalesInvoices, + }); + } + /** * Retrieve paginated sales invoices with custom view metadata. * @param {Request} req