Skip to content

Commit

Permalink
✨ Improve Invoice Payment
Browse files Browse the repository at this point in the history
🎨 Refactor Code ERP Modules
🐛 Fix Bugs in all erp modules
  • Loading branch information
Salah Eddine Lalami committed Oct 25, 2023
1 parent ae8978d commit c2bc6cc
Show file tree
Hide file tree
Showing 57 changed files with 181 additions and 533 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require('mongoose');

const Model = mongoose.model('Invoice');
const ModalPaymentInvoice = mongoose.model('PaymentInvoice');
const ModalPayment = mongoose.model('Payment');

const remove = async (req, res) => {
try {
Expand All @@ -24,7 +24,7 @@ const remove = async (req, res) => {
message: 'Invoice not found',
});
}
const paymentsInvoices = await ModalPaymentInvoice.updateMany(
const paymentsInvoices = await ModalPayment.updateMany(
{ invoice: deletedInvoice._id },
{ $set: { removed: true } }
);
Expand Down
10 changes: 6 additions & 4 deletions backend/controllers/appControllers/offerController/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Model = mongoose.model('Offer');
const custom = require('@/controllers/middlewaresControllers/pdfController');

const { calculate } = require('@/helpers');
const { increaseBySettingKey } = require('@/middlewares/settings');

const create = async (req, res) => {
try {
Expand Down Expand Up @@ -37,7 +38,7 @@ const create = async (req, res) => {

// Creating a new document in the collection
const result = await new Model(body).save();
const fileId = 'invoice-' + result._id + '.pdf';
const fileId = 'offer-' + result._id + '.pdf';
const updateResult = await Model.findOneAndUpdate(
{ _id: result._id },
{ pdfPath: fileId },
Expand All @@ -47,13 +48,14 @@ const create = async (req, res) => {
).exec();
// Returning successfull response

custom.generatePdf('Offer', { filename: 'quote', format: 'A4' }, result);
increaseBySettingKey({ settingKey: 'last_offer_number' });
custom.generatePdf('Offer', { filename: 'offer', format: 'A4' }, result);

// Returning successfull response
return res.status(200).json({
success: true,
result: updateResult,
message: 'Quote created successfully',
message: 'Offer created successfully',
});
} catch (err) {
// If err is thrown by Mongoose due to required validations
Expand All @@ -68,7 +70,7 @@ const create = async (req, res) => {
return res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: err.message,
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions backend/controllers/appControllers/offerController/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const update = async (req, res) => {
body['taxTotal'] = taxTotal;
body['total'] = total;
body['items'] = items;
body['pdfPath'] = 'quote-' + req.params.id + '.pdf';
body['pdfPath'] = 'offer-' + req.params.id + '.pdf';
// Find document by id and updates with the required fields

const result = await Model.findOneAndUpdate({ _id: req.params.id, removed: false }, body, {
Expand All @@ -49,7 +49,7 @@ const update = async (req, res) => {

// Returning successfull response

custom.generatePdf('Offer', { filename: 'invoice', format: 'A4' }, result);
custom.generatePdf('Offer', { filename: 'offer', format: 'A4' }, result);
return res.status(200).json({
success: true,
result,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');

const Model = mongoose.model('PaymentInvoice');
const Model = mongoose.model('Payment');
const Invoice = mongoose.model('Invoice');
const custom = require('@/controllers/middlewaresControllers/pdfController');

Expand Down Expand Up @@ -40,7 +40,7 @@ const create = async (req, res) => {
req.body['createdBy'] = req.admin._id;
const result = await Model.create(req.body);

const fileId = 'payment-invoice-report-' + result._id + '.pdf';
const fileId = 'payment-' + result._id + '.pdf';
const updatePath = await Model.findOneAndUpdate(
{ _id: result._id.toString(), removed: false },
{ pdfPath: fileId },
Expand All @@ -50,7 +50,7 @@ const create = async (req, res) => {
).exec();
// Returning successfull response

const { _id: paymentInvoiceId, amount } = result;
const { _id: paymentId, amount } = result;
const { id: invoiceId, total, discount, credit } = currentInvoice;

let paymentStatus =
Expand All @@ -63,7 +63,7 @@ const create = async (req, res) => {
const invoiceUpdate = await Invoice.findOneAndUpdate(
{ _id: req.body.invoice },
{
$push: { paymentInvoice: paymentInvoiceId.toString() },
$push: { payment: paymentId.toString() },
$inc: { credit: amount },
$set: { paymentStatus: paymentStatus },
},
Expand All @@ -73,11 +73,7 @@ const create = async (req, res) => {
}
).exec();

await custom.generatePdf(
'PaymentInvoice',
{ filename: 'payment-invoice-report', format: 'A4' },
invoiceUpdate
);
await custom.generatePdf('Payment', { filename: 'payment', format: 'A4' }, result);

res.status(200).json({
success: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const createCRUDController = require('@/controllers/middlewaresControllers/createCRUDController');
const methods = createCRUDController('PaymentInvoice');
const methods = createCRUDController('Payment');

const create = require('./create');
const summary = require('./summary');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');

const Model = mongoose.model('PaymentInvoice');
const Model = mongoose.model('Payment');
const Invoice = mongoose.model('Invoice');

const remove = async (req, res) => {
Expand All @@ -19,7 +19,7 @@ const remove = async (req, res) => {
});
}

const { _id: paymentInvoiceId, amount: previousAmount } = previousPayment;
const { _id: paymentId, amount: previousAmount } = previousPayment;
const { id: invoiceId, total, discount, credit: previousCredit } = previousPayment.invoice;

// Find the document by id and delete it
Expand Down Expand Up @@ -47,7 +47,7 @@ const remove = async (req, res) => {
{ _id: invoiceId },
{
$pull: {
paymentInvoice: paymentInvoiceId,
payment: paymentId,
},
$inc: { credit: -previousAmount },
$set: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const mongoose = require('mongoose');
const moment = require('moment');

const Model = mongoose.model('PaymentInvoice');
const Model = mongoose.model('Payment');

const summary = async (req, res) => {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');

const Model = mongoose.model('PaymentInvoice');
const Model = mongoose.model('Payment');
const Invoice = mongoose.model('Invoice');
const custom = require('@/controllers/middlewaresControllers/pdfController');

Expand Down Expand Up @@ -77,11 +77,7 @@ const update = async (req, res) => {
}
).exec();

await custom.generatePdf(
'PaymentInvoice',
{ filename: 'payment-invoice-report', format: 'A4' },
updateInvoice
);
await custom.generatePdf('Payment', { filename: 'payment', format: 'A4' }, result);

res.status(200).json({
success: true,
Expand All @@ -102,7 +98,7 @@ const update = async (req, res) => {
res.status(500).json({
success: false,
result: null,
message: 'Oops there is an Error',
message: err.message,
error: err,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const create = async (req, res) => {

// Creating a new document in the collection
const result = await new Model(body).save();
const fileId = 'invoice-' + result._id + '.pdf';
const fileId = 'quote-' + result._id + '.pdf';
const updateResult = await Model.findOneAndUpdate(
{ _id: result._id },
{ pdfPath: fileId },
Expand Down
1 change: 0 additions & 1 deletion backend/models/appModels/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const clientSchema = new mongoose.Schema({
type: Boolean,
default: true,
},
createdBy: { type: mongoose.Schema.ObjectId, ref: 'Admin', autopopulate: true, required: true },
company: {
type: String,
trim: true,
Expand Down
4 changes: 2 additions & 2 deletions backend/models/appModels/Invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ const invoiceSchema = new mongoose.Schema({
type: Number,
default: 0,
},
paymentInvoice: [
payment: [
{
type: mongoose.Schema.ObjectId,
ref: 'PaymentInvoice',
ref: 'Payment',
},
],
paymentStatus: {
Expand Down
1 change: 0 additions & 1 deletion backend/models/appModels/Lead.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const leadSchema = new mongoose.Schema({
type: Boolean,
default: false,
},
createdBy: { type: mongoose.Schema.ObjectId, ref: 'Admin', autopopulate: true, required: true },
enabled: {
type: Boolean,
default: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mongoose = require('mongoose');
const AutoIncrement = require('mongoose-sequence')(mongoose);
mongoose.Promise = global.Promise;

const paymentInvoiceSchema = new mongoose.Schema({
const paymentSchema = new mongoose.Schema({
removed: {
type: Boolean,
default: false,
Expand Down Expand Up @@ -53,5 +53,5 @@ const paymentInvoiceSchema = new mongoose.Schema({
default: Date.now,
},
});
paymentInvoiceSchema.plugin(require('mongoose-autopopulate'));
module.exports = mongoose.model('PaymentInvoice', paymentInvoiceSchema);
paymentSchema.plugin(require('mongoose-autopopulate'));
module.exports = mongoose.model('Payment', paymentSchema);
40 changes: 15 additions & 25 deletions backend/routes/appRoutes/appApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const supplierController = require('@/controllers/appControllers/supplierControl
const supplierOrderController = require('@/controllers/appControllers/supplierOrderController');
const expenseController = require('@/controllers/appControllers/expenseController');
const expenseCategoryController = require('@/controllers/appControllers/expenseCategoryController');
const paymentInvoiceController = require('@/controllers/appControllers/paymentInvoiceController');
const paymentController = require('@/controllers/appControllers/paymentController');
const orderController = require('@/controllers/appControllers/orderController');
const offerController = require('@/controllers/appControllers/offerController');

Expand Down Expand Up @@ -235,34 +235,24 @@ router
// //_____________________________________________ API for client payments_________________

router
.route('/payment/invoice/create')
.post(hasPermission('create'), catchErrors(paymentInvoiceController.create));
.route('/payment/create')
.post(hasPermission('create'), catchErrors(paymentController.create));
router.route('/payment/read/:id').get(hasPermission('read'), catchErrors(paymentController.read));
router
.route('/payment/invoice/read/:id')
.get(hasPermission('read'), catchErrors(paymentInvoiceController.read));
.route('/payment/update/:id')
.patch(hasPermission('update'), catchErrors(paymentController.update));
router
.route('/payment/invoice/update/:id')
.patch(hasPermission('update'), catchErrors(paymentInvoiceController.update));
.route('/payment/delete/:id')
.delete(hasPermission('delete'), catchErrors(paymentController.delete));
router.route('/payment/search').get(hasPermission('read'), catchErrors(paymentController.search));
router.route('/payment/list').get(hasPermission('read'), catchErrors(paymentController.list));
router.route('/payment/filter').get(hasPermission('read'), catchErrors(paymentController.filter));
router
.route('/payment/invoice/delete/:id')
.delete(hasPermission('delete'), catchErrors(paymentInvoiceController.delete));
router
.route('/payment/invoice/search')
.get(hasPermission('read'), catchErrors(paymentInvoiceController.search));
router
.route('/payment/invoice/list')
.get(hasPermission('read'), catchErrors(paymentInvoiceController.list));
router
.route('/payment/invoice/filter')
.get(hasPermission('read'), catchErrors(paymentInvoiceController.filter));
router
.route('/payment/invoice/pdf/:id')
.get(hasPermission('read'), catchErrors(paymentInvoiceController.generatePDF));
router
.route('/payment/invoice/summary')
.get(hasPermission('read'), catchErrors(paymentInvoiceController.summary));
.route('/payment/pdf/:id')
.get(hasPermission('read'), catchErrors(paymentController.generatePDF));
router.route('/payment/summary').get(hasPermission('read'), catchErrors(paymentController.summary));

//router.route('/payment/invoice/mail).post( hasPermission('create'),catchErrors(paymentInvoiceController.sendMail));
//router.route('/payment/mail).post( hasPermission('create'),catchErrors(paymentController.sendMail));

// //_________________________________________________________________API for Offers_____________________

Expand Down
4 changes: 2 additions & 2 deletions backend/routes/coreRoutes/coreDownloadRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const { hasPermission } = require('@/middlewares/permission');
router.route('/:subPath/:directory/:id').get(function (req, res) {
const { subPath, directory, id } = req.params;

// Handle the /payment/invoice/* route
// Handle the /payment/* route
if (subPath == 'payment' && directory == 'invoice') {
downloadPdf(req, res, { directory: 'PaymentInvoice', id });
downloadPdf(req, res, { directory: 'Payment', id });
} else {
downloadPdf(req, res, { directory, id });
}
Expand Down
7 changes: 7 additions & 0 deletions backend/setup/config/financeConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
"valueType": "number",
"isCoreSetting": true
},
{
"settingCategory": "finance_settings",
"settingKey": "last_payment_number",
"settingValue": 1,
"valueType": "number",
"isCoreSetting": true
},
{
"settingCategory": "finance_settings",
"settingKey": "current_invoice_year",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,13 @@ html
.space
.left.col-12
h1.invoice
| Payment Invoice
| Payment Reciept
div
.col.col-8
p.strong Date :
p #{moment(model.date).format("DD/MM/YYYY")}
.col.col-8
p.strong Expired Date :
p #{moment(model.expiredDate).format("DD/MM/YYYY")}
.col.col-9
p.strong Invoice Number :
p.strong Reciept Number :
p # #{model.number}/#{model.year || ""}

.right.col-10
Expand All @@ -302,15 +299,12 @@ html
tbody
tr
td.strong TOTAL
td.total.price $ #{model.subTotal.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, " ")}
tr
td.grand.total.strong GRAND TOTAL
td.grand.total.price $#{model.total.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, " ")}
td.total.price $ #{model.amount.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, " ")}

.col.col-24
.space
footer.footer-invoice
| This Payment Invoice was created on a computer and is valid without the signature and seal.
| This Payment Reciept was created on a computer and is valid without the signature and seal.



Loading

0 comments on commit c2bc6cc

Please sign in to comment.