100 lines
2.9 KiB
JavaScript
100 lines
2.9 KiB
JavaScript
const { FinancePayment, FnF, Application, Resignation, User, Outlet } = require('../models');
|
|
|
|
exports.getOnboardingPayments = async (req, res) => {
|
|
try {
|
|
const payments = await FinancePayment.findAll({
|
|
include: [{
|
|
model: Application,
|
|
as: 'application',
|
|
attributes: ['applicantName', 'applicationId']
|
|
}],
|
|
order: [['createdAt', 'ASC']]
|
|
});
|
|
|
|
res.json({ success: true, payments });
|
|
} catch (error) {
|
|
console.error('Get onboarding payments error:', error);
|
|
res.status(500).json({ success: false, message: 'Error fetching payments' });
|
|
}
|
|
};
|
|
|
|
exports.getFnFSettlements = async (req, res) => {
|
|
try {
|
|
const settlements = await FnF.findAll({
|
|
include: [
|
|
{
|
|
model: Resignation,
|
|
as: 'resignation',
|
|
attributes: ['resignationId']
|
|
},
|
|
{
|
|
model: Outlet, // Need to ensure Outlet is imported or associated
|
|
as: 'outlet',
|
|
include: [{
|
|
model: User,
|
|
as: 'dealer',
|
|
attributes: ['name']
|
|
}]
|
|
}
|
|
],
|
|
order: [['createdAt', 'DESC']]
|
|
});
|
|
|
|
res.json({ success: true, settlements });
|
|
} catch (error) {
|
|
console.error('Get F&F settlements error:', error);
|
|
res.status(500).json({ success: false, message: 'Error fetching settlements' });
|
|
}
|
|
};
|
|
|
|
exports.updatePayment = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { paidDate, amount, paymentMode, transactionReference, status } = req.body;
|
|
|
|
const payment = await FinancePayment.findByPk(id);
|
|
if (!payment) {
|
|
return res.status(404).json({ success: false, message: 'Payment not found' });
|
|
}
|
|
|
|
await payment.update({
|
|
paymentDate: paidDate || payment.paymentDate,
|
|
amount: amount || payment.amount,
|
|
transactionId: transactionReference || payment.transactionId,
|
|
paymentStatus: status || payment.paymentStatus,
|
|
updatedAt: new Date()
|
|
});
|
|
|
|
res.json({ success: true, message: 'Payment updated successfully' });
|
|
} catch (error) {
|
|
console.error('Update payment error:', error);
|
|
res.status(500).json({ success: false, message: 'Error updating payment' });
|
|
}
|
|
};
|
|
|
|
exports.updateFnF = async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const {
|
|
inventoryClearance, sparesClearance, accountsClearance, legalClearance,
|
|
finalSettlementAmount, status
|
|
} = req.body;
|
|
|
|
const fnf = await FnF.findByPk(id);
|
|
if (!fnf) {
|
|
return res.status(404).json({ success: false, message: 'F&F settlement not found' });
|
|
}
|
|
|
|
await fnf.update({
|
|
status: status || fnf.status,
|
|
netAmount: finalSettlementAmount || fnf.netAmount,
|
|
updatedAt: new Date()
|
|
});
|
|
|
|
res.json({ success: true, message: 'F&F settlement updated successfully' });
|
|
} catch (error) {
|
|
console.error('Update F&F error:', error);
|
|
res.status(500).json({ success: false, message: 'Error updating F&F settlement' });
|
|
}
|
|
};
|