Last active
March 11, 2024 11:46
-
-
Save RussellBishop/289d80015165fed2cfbada0fc9388d59 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create the new budget items (↖️ connect to new estimate) ✅ | |
// create the new budget item specialism totals (↖️ connect to new budget items ) ✅ | |
// create the new role hours (↖️ connect to new budget items AND new budget item specialism totals) ✅ | |
// create the new estimate specialism totals (↖️ connect to new estimate) ✅ | |
// Start time | |
// let startTime = Date.now(); | |
let inputConfig = input.config(); | |
// –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– | |
// BUDGET ITEMS | |
// & EXPENSES | |
// & ROLE HOURS | |
// –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– | |
// * Budget items fields * | |
// [text] Budget item title | |
// [text] Description | |
// [text] Asset type | |
// [text] Deliverables | |
// [text] Inclusions | |
// [text] Exclusions | |
// [number] Quantity | |
// [linked] Budget item specialism totals | |
let budgetItemsTable = base.getTable("Budget items"); | |
let budgetItemsView = budgetItemsTable.getView("🔍 Select budget items to copy in saved version"); | |
let budgetItemsToCopy = await budgetItemsView.selectRecordsAsync( | |
{ | |
fields: [ | |
"Budget item title", | |
"Description", | |
"Asset type", | |
"Deliverables", | |
"Inclusions", | |
"Exclusions", | |
"Quantity", | |
"Manual sort" | |
], | |
recordIds: inputConfig.budgetItemsToCopyIDs | |
} | |
); | |
// endTime = Date.now(); | |
// console.log('Select budget items from table duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
// * Expenses fields * | |
// [text] Description | |
// [number] Unit Cost | |
// [number] Units | |
let expensesTable = base.getTable("Expenses"); | |
let expensesView = expensesTable.getView("🔍 Select expenses to copy in saved version"); | |
let allExpenses = await expensesView.selectRecordsAsync({ | |
fields: ["Budget item record ID", "Description", "Unit cost", "Units"] | |
}); | |
// endTime = Date.now(); | |
// console.log('Select expenses from table duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
// * Role hours fields * | |
// [number] Manual tier # | |
// [number] Manual specialist team | |
// [number] Units | |
let roleHoursTable = base.getTable("Role hours"); | |
let roleHoursView = roleHoursTable.getView("🔍 Select role hours to copy in saved version"); | |
let allRoleHours = await roleHoursView.selectRecordsAsync({ | |
fields: [ | |
"Budget item record ID", | |
"Budget item specialism total record ID", | |
"Hours", | |
"Role", | |
"ℹ️ Tier #", | |
"ℹ️ Hourly rate" | |
] | |
}); | |
// let roleHoursBist = roleHours.getCellValue("Budget item specialism total record ID"); | |
// let roleHoursHours = roleHours.getCellValue("Hours"); | |
// let roleHoursRole = roleHours.getCellValue("Role"); | |
// let roleHoursHourlyRate = roleHours.getCellValue("ℹ️ Hourly rate"); | |
// endTime = Date.now(); | |
// console.log('Select role hours from table duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
// * Budget item specialism totals fields * | |
// | |
// | |
// | |
let budgetItemSpecialismTotals = base.getTable("Budget item specialism totals"); | |
let budgetItemSpecialismTotalsView = budgetItemSpecialismTotals.getView("🔍 Select BIST to copy in saved version"); | |
let allBudgetItemSpecialismTotals = await budgetItemSpecialismTotalsView.selectRecordsAsync({ | |
fields: [ | |
"Budget item", | |
"Budget item record ID", | |
"Specialism", | |
"ℹ️ Fees" | |
// "Role hours", | |
// "Role", | |
] | |
}); | |
// endTime = Date.now(); | |
// console.log('Select budget item specialism totals duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
let expensesRecordsToCreate = []; | |
let roleHoursRecordsToCreate = []; | |
for (let budgetItem = 0; budgetItem < budgetItemsToCopy.records.length; budgetItem++) { | |
let budgetItemToCopyID = budgetItemsToCopy.records[budgetItem].id; | |
let newBudgetItem = await budgetItemsTable.createRecordAsync({ | |
["Estimate"]: [{id: inputConfig.newBudgetRecordID}], | |
["Budget item title"]: budgetItemsToCopy.records[budgetItem].getCellValue("Budget item title"), | |
["Description"]: budgetItemsToCopy.records[budgetItem].getCellValue("Description"), | |
["Asset type"]: budgetItemsToCopy.records[budgetItem].getCellValue("Asset type"), | |
["Deliverables"]: budgetItemsToCopy.records[budgetItem].getCellValue("Deliverables"), | |
["Inclusions"]: budgetItemsToCopy.records[budgetItem].getCellValue("Inclusions"), | |
["Exclusions"]: budgetItemsToCopy.records[budgetItem].getCellValue("Exclusions"), | |
["Quantity"]: budgetItemsToCopy.records[budgetItem].getCellValue("Quantity"), | |
["Manual sort (saved)"]: budgetItemsToCopy.records[budgetItem].getCellValue("Manual sort"), | |
["Saved"]: true | |
}); | |
// * Expenses * | |
// select expenses that were linked to the copied budget item | |
// create new expenses records linked to the newBudgetItem | |
for (let expenseRecord of allExpenses.records) { | |
let budgetItemId = expenseRecord.getCellValue("Budget item record ID"); | |
// Check if the current record's 'Budget item record ID' matches the target ID | |
if (budgetItemId === budgetItemToCopyID) { | |
expensesRecordsToCreate.push({ | |
fields: { | |
"Budget item": [{ id: newBudgetItem }], | |
"Description": expenseRecord.getCellValue("Description"), | |
"Unit cost": expenseRecord.getCellValue("Unit cost"), | |
"Units": expenseRecord.getCellValue("Units"), | |
"Saved": true | |
} | |
}); | |
} | |
} | |
for (let bistRecordToCopy of allBudgetItemSpecialismTotals.records) { | |
let bistBudgetItemId = bistRecordToCopy.getCellValue("Budget item record ID"); | |
let bistFees = bistRecordToCopy.getCellValue("ℹ️ Fees"); | |
let bistSpecialism = bistRecordToCopy.getCellValue("Specialism"); | |
if ( | |
// matches the budget item we're copying | |
// has hours | |
bistBudgetItemId === budgetItemToCopyID | |
&& | |
bistFees > 0 | |
&& | |
bistSpecialism.length > 0 | |
) | |
{ | |
let newBist = await budgetItemSpecialismTotals.createRecordAsync({ | |
["Budget item"]: [{id: newBudgetItem}], | |
["Specialism"]: bistSpecialism, | |
["Saved"]: true | |
}); | |
for (let roleHours of allRoleHours.records) { | |
let roleHoursBist = roleHours.getCellValue("Budget item specialism total record ID"); | |
let roleHoursHours = roleHours.getCellValue("Hours"); | |
let roleHoursRole = roleHours.getCellValue("Role"); | |
let roleHoursHourlyRate = roleHours.getCellValue("ℹ️ Hourly rate"); | |
let roleHoursTier = null; | |
if (roleHours.getCellValueAsString("ℹ️ Tier #") !== "") { | |
// console.log(roleHours.getCellValueAsString("ℹ️ Tier #")); | |
// console.log(Number(roleHours.getCellValueAsString("ℹ️ Tier #"))); | |
roleHoursTier = Number(roleHours.getCellValueAsString("ℹ️ Tier #").replace('#','')); | |
} | |
// console.log(roleHoursTier); | |
if ( | |
// matches the budget item we're copying | |
// has hours | |
roleHoursBist === bistRecordToCopy.id | |
&& | |
roleHoursHours > 0 | |
&& | |
roleHoursRole.length > 0 | |
&& | |
roleHoursHourlyRate > 0 | |
) | |
{ | |
roleHoursRecordsToCreate.push({ | |
fields: { | |
"Budget item": [{ id: newBudgetItem }], | |
"Budget item specialism total": [{ id: newBist }], | |
"Specialism": bistSpecialism, | |
"Hours": roleHoursHours, | |
"Role": roleHoursRole, | |
"Hourly rate (saved)": roleHoursHourlyRate, | |
"Manual tier #": roleHoursTier, | |
"Saved": true | |
} | |
}); | |
} | |
} | |
} | |
} | |
} | |
// endTime = Date.now(); | |
// console.log('Create budget items and prepare expenses, budget item specialism totals and role hours duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
while (expensesRecordsToCreate.length > 0) { | |
await expensesTable.createRecordsAsync(expensesRecordsToCreate.slice(0, 50)); | |
expensesRecordsToCreate = expensesRecordsToCreate.slice(50); | |
} | |
// endTime = Date.now(); | |
// console.log('Batch create expenses duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
while (roleHoursRecordsToCreate.length > 0) { | |
await roleHoursTable.createRecordsAsync(roleHoursRecordsToCreate.slice(0, 50)); | |
roleHoursRecordsToCreate = roleHoursRecordsToCreate.slice(50); | |
} | |
// endTime = Date.now(); | |
// console.log('Batch create role hours duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
// –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– | |
// ESTIMATE SPECIALISM TOTALS | |
// –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– –––––– | |
// * Estimate specialism totals fields * | |
// [text] ℹ️ Specialis team | |
// [number] ℹ️ Hours | |
// [number] ℹ️ Fees | |
let estimateSpecialismTotalsTable = base.getTable("Estimate specialism totals"); | |
let estimateSpecialismTotalsView = estimateSpecialismTotalsTable.getView("🔍 Select EST to copy in saved version"); | |
let estimateSpecialismTotalsToCopy = await estimateSpecialismTotalsView.selectRecordsAsync( | |
{ | |
fields: [ | |
"ℹ️ Specialist team", | |
"ℹ️ Hours", | |
"ℹ️ Fees", | |
"Specialist team" | |
], | |
recordIds: inputConfig.estimateSpecialismTotalsToCopyIDs | |
} | |
); | |
// endTime = Date.now(); | |
// console.log('Select estimate specialism totals duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
let estimateSpecialismTotalsToCreate = []; | |
for (let total = 0; total < estimateSpecialismTotalsToCopy.records.length; total++) { | |
estimateSpecialismTotalsToCreate.push({ | |
fields: { | |
["Estimate"]: [{id: inputConfig.newBudgetRecordID}], | |
["Specialism (saved)"]: estimateSpecialismTotalsToCopy.records[total].getCellValue("ℹ️ Specialist team"), | |
["Hours (saved)"]: estimateSpecialismTotalsToCopy.records[total].getCellValue("ℹ️ Hours"), | |
["Fees (saved)"]: estimateSpecialismTotalsToCopy.records[total].getCellValue("ℹ️ Fees"), | |
["Specialist team"]: estimateSpecialismTotalsToCopy.records[total].getCellValue("Specialist team"), | |
["Saved"]: true | |
} | |
}); | |
} | |
// endTime = Date.now(); | |
// console.log('Prepare estimate specialism totals duration:', (endTime - startTime) / 1000 + 's'); | |
// startTime = Date.now(); | |
while (estimateSpecialismTotalsToCreate.length > 0) { | |
await estimateSpecialismTotalsTable.createRecordsAsync(estimateSpecialismTotalsToCreate.slice(0, 50)); | |
estimateSpecialismTotalsToCreate = estimateSpecialismTotalsToCreate.slice(50); | |
} | |
// endTime = Date.now(); | |
// console.log('Batch create estimate specialism totals duration:', (endTime - startTime) / 1000 + 's'); | |
// … | |
// … ALL DONE? | |
// … | |
output.set("New budget record ID", inputConfig.newBudgetRecordID); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment