I have table Contracts, Documents and Documents items.
I need to create new Contract (project) and new Document (offer, invoice, etc.) with specific items and prices.
I need to create everything from Table Contracts which is not problem for me.
Now I am looking for a solution how to Copy document with linked items.
Example: Lets say I will create an Offer with ID 1 and I’ll create three items in it - A, B, C and save it. Now how can I simply copy whole Offer with item A, B, C and change it into Invoice easily?
simple way is to create invoice header and invoice items (two tables) and use workflow automation to create a button in the Offers table (for example: CREATE INVOICE) with javascript model doo.table.
I am sending you an example of one of our application:
debugger;
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const isoCurrentDate = `${currentYear}-${(currentDate.getMonth() + 1).toString().padStart(2, '0')}-${currentDate.getDate().toString().padStart(2, '0')}T00:00:00.00Z`;
let dueDate= new Date(Date.now() + 14 * 24 * 60 * 60 * 1000).toISOString().substring(0,10);
doo.toast.info('Info','Creating an invoice');
let id = doo.model.id;
const OrdersBySh = await doo.table.getRecord('ordersByShoptet', id);
if (OrdersBySh.data.fields.paymentMethod ===' On-line bank transfer‘)
dueDate = isoCurrentDate;
let fields = {
invoiceDate: isoCurrentDate,
paymentMethod: OrdersBySh.data.fields.paymentMethod,
status: OrdersBySh.data.fields.statusName,
ordersByShoptet:{id: id},
ordersViaShoptet: OrdersBySh.data.fields.eshop,
dueDate: dueDate,
dateOfTaxableTransaction: isoCurrentDate,
};
let invoiceHeader= await doo.table.createRecord('issuedInvoices', fields);
let options = { filter: 'ordersByShoptet.id(eq)' +id };
const invoiceItems = await doo.table.getData('shoptetrow',options);
const invoicesRow= [];
for (const row of invoiceItems.data){
let fields = {
invoice: { id: invoiceHeader.data.id },
popis: row.fields.itemName,
amount: row.fields.itemAmount,
unitPraceWithVat: row.fields.itemUnitPriceWithVat,
UnitPriceWithoutVat: row.fields.itemUnitPriceWithoutVat,
vatRate: row.fields.itemVatRate,
TotalPriceWithVat: row.fields.itemTotalPriceWithVat,
TotalPriceWithoutVat: row.fields.itemTotalPriceWithoutVat
};
rowsOfIssuedInvoices.push(fields);
}
let invoiceRows=await doo.table.createRecordsBulk('rowsOfIssuedInvoices', rowsOfIssuedInvoices);
}