Re-generation of Invoices
The process includes two main steps: clean-up of old invoices and invoice creation using the new API.
Step 1: Invoice Clean-up (Script Execution)
Objective:
This step removes invoices with the INVOICE_ORIGIN.CALL_FOR_RENT and INVOICE_ORIGIN.INSURANCE origins that have already been processed.
Script Execution:
The script performs the following tasks:
let invoices = db.getSiblingDB("adb-accounting").getCollection('invoices');
let events = db.getSiblingDB("adb-accounting").getCollection('accounting_event');
let entries = db.getSiblingDB("adb-accounting").getCollection('domain_object_accounting_entry');
var organisationId = "62d4ffdd0fa1798e82091168";
var invoiceOrigin = "INVOICE_ORIGIN.CALL_FOR_RENT"; //Available: INVOICE_ORIGIN.CALL_FOR_RENT, INVOICE_ORIGIN.INSURANCE
var removeProcessedInvoices = false;
// Find invoices that need to be cleaned up
invoices.find({
"organisationId": organisationId,
"invoiceOrigin": invoiceOrigin,
"invoiceNature": "INVOICE_NATURE.HEADER"
})
.forEach(header => {
// Check if the invoice is processed and has a transactionId
if (header.state == 'PROCESSED' && header.transactionId && removeProcessedInvoices === true) {
// Find related accounting events
events.find({
"$or": [
{"_id": ObjectId(header.transactionId)}, // Match events with the same transactionId
{"parent._id": ObjectId(header.transactionId)} // Match events with the parentId equal to transactionId
]
}).forEach(event => {
// If event is processed and has a transactionId, delete related accounting entries
if (event.state == 'PROCESSED' && event.transactionId) {
entries.deleteMany({"transactionId": event.transactionId});
}
// Delete the event from the events collection
events.deleteOne({"_id": event._id});
})
}
// Delete all child invoices related to the header
invoices.deleteMany({"parentId": header._id});
// Delete the invoice header itself
invoices.deleteOne({"_id": header._id});
});
Explanation of the Script
1. Identifying Invoices to Delete:
- The script identifies invoices from the
invoicescollection with the specifiedorganisationId,invoiceOrigin( eitherINVOICE_ORIGIN.CALL_FOR_RENTorINVOICE_ORIGIN.INSURANCE), andinvoiceNatureasINVOICE_NATURE.HEADER.
2. Processing Each Invoice Header:
- For each invoice header, the script checks:
- If the invoice state is
PROCESSEDand it has atransactionId.
- If the invoice state is
3. Removing Related Events and Entries:
- For each
PROCESSEDinvoice header with atransactionId:- The script deletes related events from the
accounting_eventcollection that match thetransactionIdor its parenttransactionId. - It also deletes accounting entries from the
domain_object_accounting_entrycollection linked to thetransactionId.
- The script deletes related events from the
4. Deleting Invoices:
- Deletes all child invoices (where the
parentIdmatches theheader._id). - Deletes the invoice header from the
invoicescollection.
Invoice Origins for Deletion:
INVOICE_ORIGIN.CALL_FOR_RENTINVOICE_ORIGIN.INSURANCE
Step 2: Create New Invoices via Technical API
Objective:
This step re-generates invoices using the new technical API endpoint.
API Endpoint:
POST /adb-accounting/accounting/invoices/creation
Request Body:
The API requires the following fields in the request body:
{
"origin": "CALL_FOR_RENT", // Available: INSURANCE, CALL_FOR_RENT
"callForRentPeriod": "2025-02", // Period for the call-for-rent invoice
"rentalDelegateContractId": "62d51bcf89483a34a515267b", // Optional, can be null if not relevant
"rentalContractId": null // Optional, can be null if not relevant
}
Execution
1. Open Postman and navigate to the following collection:
- ADB-2.0 E2E API tests [development]
- ADB-2.0-ACCOUNTING-E2E
- I can generate invoices
- I can generate invoices by a request