feat: Listing due sales invoices for specific customer.

This commit is contained in:
Ahmed Bouhuolia
2020-08-15 16:30:52 +02:00
parent 42569c89e4
commit 4e68a7db71

View File

@@ -1,6 +1,7 @@
import express from 'express'; import express from 'express';
import { check, param, query } from 'express-validator'; import { check, param, query } from 'express-validator';
import { difference } from 'lodash'; import { difference } from 'lodash';
import { raw } from 'objection';
import { ItemEntry } from '@/models'; import { ItemEntry } from '@/models';
import validateMiddleware from '@/http/middleware/validateMiddleware'; import validateMiddleware from '@/http/middleware/validateMiddleware';
import asyncMiddleware from '@/http/middleware/asyncMiddleware'; import asyncMiddleware from '@/http/middleware/asyncMiddleware';
@@ -10,6 +11,7 @@ import CustomersService from '@/services/Customers/CustomersService';
import DynamicListing from '@/services/DynamicListing/DynamicListing'; import DynamicListing from '@/services/DynamicListing/DynamicListing';
import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder'; import DynamicListingBuilder from '@/services/DynamicListing/DynamicListingBuilder';
import { dynamicListingErrorsToResponse } from '@/services/DynamicListing/hasDynamicListing'; import { dynamicListingErrorsToResponse } from '@/services/DynamicListing/hasDynamicListing';
import { Customer } from '../../../models';
export default class SaleInvoicesController { export default class SaleInvoicesController {
/** /**
@@ -49,6 +51,11 @@ export default class SaleInvoicesController {
asyncMiddleware(this.validateInvoiceExistance), asyncMiddleware(this.validateInvoiceExistance),
asyncMiddleware(this.deleteSaleInvoice) asyncMiddleware(this.deleteSaleInvoice)
); );
router.get(
'/due_invoices',
this.dueSalesInvoicesListValidationSchema,
asyncMiddleware(this.getDueSalesInvoice),
);
router.get( router.get(
'/:id', '/:id',
this.specificSaleInvoiceValidation, this.specificSaleInvoiceValidation,
@@ -60,7 +67,7 @@ export default class SaleInvoicesController {
'/', '/',
this.saleInvoiceListValidationSchema, this.saleInvoiceListValidationSchema,
asyncMiddleware(this.getSalesInvoices) asyncMiddleware(this.getSalesInvoices)
); )
return router; 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. * Validate whether sale invoice customer exists on the storage.
* @param {Request} req * @param {Request} req
@@ -316,6 +330,39 @@ export default class SaleInvoicesController {
return res.status(200).send({ sale_invoice: saleInvoice }); 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. * Retrieve paginated sales invoices with custom view metadata.
* @param {Request} req * @param {Request} req