Spaces:
Runtime error
Runtime error
Bansari Akhani commited on
Commit ·
fa3491c
1
Parent(s): 0949fd2
syn invoices to propertyware
Browse files
src/controllers/propertyware/bills.controller.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Request, Response } from 'express';
|
| 2 |
+
import { logger } from '../../utils/logger';
|
| 3 |
+
import Invoice from '../../models/invoice';
|
| 4 |
+
import InvoiceDetail from '../../models/invoicedetail';
|
| 5 |
+
import { createBill } from '../../shared/services/propertyware.service';
|
| 6 |
+
import { formatDate } from '../../utils/dataUtils';
|
| 7 |
+
import AuditLog from '../../models/auditLogs';
|
| 8 |
+
import ErrorLog from './../../models/errorLog';
|
| 9 |
+
import { AxiosError } from 'axios';
|
| 10 |
+
|
| 11 |
+
export const syncInvoices = async (req: Request, res: Response) => {
|
| 12 |
+
try {
|
| 13 |
+
const invoices = await Invoice.findAll({
|
| 14 |
+
where: { status: 'approved' },
|
| 15 |
+
include: [
|
| 16 |
+
{ model: InvoiceDetail }
|
| 17 |
+
]
|
| 18 |
+
});
|
| 19 |
+
|
| 20 |
+
if (invoices.length == 0) {
|
| 21 |
+
res.end("No invoice to sync");
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// Map the invoices to the bill request format
|
| 25 |
+
invoices.forEach(async (invoice) => {
|
| 26 |
+
const bill = {
|
| 27 |
+
billDate: formatDate(invoice.invoice_date), // FIXME: format invoice date
|
| 28 |
+
billSplits: invoice.InvoiceDetails.map(detail => ({
|
| 29 |
+
amount: detail.amount as number,
|
| 30 |
+
glAccountID: detail.pw_gl_account_id,
|
| 31 |
+
portfolioID: detail.pw_portfolio_id,
|
| 32 |
+
buildingID: detail.pw_building_id,
|
| 33 |
+
comments: detail.description,
|
| 34 |
+
paid: false,
|
| 35 |
+
unitID: detail.pw_unit_id
|
| 36 |
+
})),
|
| 37 |
+
dueDate: invoice.due_date ? formatDate(invoice.due_date) : null, // FIXME: format due date
|
| 38 |
+
vendorID: invoice.pw_vendor_id?.toString(),
|
| 39 |
+
billNumber: invoice.invoice_number,
|
| 40 |
+
comments: invoice.description,
|
| 41 |
+
refNo: invoice.reference_number,
|
| 42 |
+
terms: invoice.term,
|
| 43 |
+
workOrderID: invoice.pw_work_order_id, // FIXME: check if work order id is valid from pwoprtyware workorders
|
| 44 |
+
};
|
| 45 |
+
try {
|
| 46 |
+
const response = await createBill(bill);
|
| 47 |
+
console.log(response.id);
|
| 48 |
+
|
| 49 |
+
// add audit log for Update Invoice
|
| 50 |
+
// add entry to error log
|
| 51 |
+
const auditLogData = { invoice_id: invoice.id as number, action_by: 1, action: 'Invoice sync to PW', details: `sync Successfull. PW invoice id : ${response.id}`};
|
| 52 |
+
await AuditLog.create(auditLogData);
|
| 53 |
+
|
| 54 |
+
// add audit log
|
| 55 |
+
|
| 56 |
+
logger.info(`Bill for invoice ${bill.refNo} synced successfully`);
|
| 57 |
+
} catch (error) {
|
| 58 |
+
if (error instanceof Error ){
|
| 59 |
+
|
| 60 |
+
logger.error(`Error syncing bill for invoice ${bill.refNo}`);
|
| 61 |
+
|
| 62 |
+
// add entry to error log
|
| 63 |
+
const errorLogData = { invoice_id: invoice.id as number, error_type: 'PW Sync Failed', error_details: `Error syncing bill for invoice ${bill.refNo}. ${error.message}`};
|
| 64 |
+
await ErrorLog.create(errorLogData);
|
| 65 |
+
}
|
| 66 |
+
logger.error(error);
|
| 67 |
+
}
|
| 68 |
+
});
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
res.end("Invoice data synced successfully");
|
| 73 |
+
} catch (error) {
|
| 74 |
+
logger.error('Error fetching invoices:', error);
|
| 75 |
+
res.status(500).send('Error fetching invoices');
|
| 76 |
+
}
|
| 77 |
+
};
|