@quantyapp/quanty-mcp-server 1.0.11 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -32,4 +32,32 @@ export declare function suggestMissingItems(auth: AuthContext, budgetId: string)
32
32
  * Calculates average cost per unit (e.g., R$/m³ de concreto)
33
33
  */
34
34
  export declare function calculateCostPerUnit(auth: AuthContext, category: string, driver: string): Promise<CostMetric>;
35
+ export interface AbcItem {
36
+ id: string;
37
+ description: string;
38
+ unit: string;
39
+ quantity: number;
40
+ unitCost: number;
41
+ total: number;
42
+ percentage: number;
43
+ accumulated: number;
44
+ class: 'A' | 'B' | 'C';
45
+ resourceType: string;
46
+ }
47
+ export interface AbcCurveResult {
48
+ totalValue: number;
49
+ classAThreshold: number;
50
+ classBThreshold: number;
51
+ classACount: number;
52
+ classBCount: number;
53
+ classCCount: number;
54
+ items: AbcItem[];
55
+ }
56
+ /**
57
+ * Generates ABC Curve analysis for a budget
58
+ * Class A: items representing ~80% of total value (high impact)
59
+ * Class B: items representing next ~15% of value (medium impact)
60
+ * Class C: remaining items representing ~5% of value (low impact)
61
+ */
62
+ export declare function generateAbcCurve(auth: AuthContext, budgetId: string, classAThreshold?: number, classBThreshold?: number): Promise<AbcCurveResult>;
35
63
  //# sourceMappingURL=analysisService.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"analysisService.d.ts","sourceRoot":"","sources":["../../src/core/analysisService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxC,MAAM,WAAW,aAAa;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CA+CtG;AAED;;GAEG;AACH,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC,CA0DvG;AAED;;GAEG;AACH,wBAAsB,oBAAoB,CACtC,IAAI,EAAE,WAAW,EACjB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACf,OAAO,CAAC,UAAU,CAAC,CAmDrB"}
1
+ {"version":3,"file":"analysisService.d.ts","sourceRoot":"","sources":["../../src/core/analysisService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAGxC,MAAM,WAAW,aAAa;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,UAAU;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,EAAE,MAAM,CAAC;IAC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,EAC5B,eAAe,GAAE,MAAW,GAC7B,OAAO,CAAC,cAAc,CAAC,CA0FzB"}
@@ -149,3 +149,89 @@ export async function calculateCostPerUnit(auth, category, driver) {
149
149
  p90Cost: Math.round(ratios[p90Index] * 100) / 100
150
150
  };
151
151
  }
152
+ /**
153
+ * Generates ABC Curve analysis for a budget
154
+ * Class A: items representing ~80% of total value (high impact)
155
+ * Class B: items representing next ~15% of value (medium impact)
156
+ * Class C: remaining items representing ~5% of value (low impact)
157
+ */
158
+ export async function generateAbcCurve(auth, budgetId, classAThreshold = 80, classBThreshold = 95) {
159
+ const { data, error } = await supabase
160
+ .from('budgets')
161
+ .select('data')
162
+ .eq('id', budgetId)
163
+ .single();
164
+ if (error)
165
+ throw new Error(`Failed to get budget: ${error.message}`);
166
+ const rows = data?.data?.rows || [];
167
+ const abcClassifications = data?.data?.abcClassifications || {};
168
+ // Filter only rows with actual values (non-empty rows)
169
+ const validRows = rows.filter(r => r.description && r.description.trim() !== '' &&
170
+ r.quantity > 0 && r.unitCost > 0);
171
+ // Calculate totals
172
+ const itemsWithTotal = validRows.map(r => ({
173
+ id: r.id,
174
+ description: r.description,
175
+ unit: r.unit,
176
+ quantity: r.quantity,
177
+ unitCost: r.unitCost,
178
+ total: r.quantity * r.unitCost
179
+ }));
180
+ // Sort by total value descending
181
+ itemsWithTotal.sort((a, b) => b.total - a.total);
182
+ const totalValue = itemsWithTotal.reduce((sum, item) => sum + item.total, 0);
183
+ if (totalValue === 0) {
184
+ return {
185
+ totalValue: 0,
186
+ classAThreshold,
187
+ classBThreshold,
188
+ classACount: 0,
189
+ classBCount: 0,
190
+ classCCount: 0,
191
+ items: []
192
+ };
193
+ }
194
+ // Calculate percentages and classifications
195
+ let accumulated = 0;
196
+ let classACount = 0;
197
+ let classBCount = 0;
198
+ let classCCount = 0;
199
+ const items = itemsWithTotal.map(item => {
200
+ const percentage = (item.total / totalValue) * 100;
201
+ accumulated += percentage;
202
+ let itemClass;
203
+ if (accumulated <= classAThreshold) {
204
+ itemClass = 'A';
205
+ classACount++;
206
+ }
207
+ else if (accumulated <= classBThreshold) {
208
+ itemClass = 'B';
209
+ classBCount++;
210
+ }
211
+ else {
212
+ itemClass = 'C';
213
+ classCCount++;
214
+ }
215
+ return {
216
+ id: item.id,
217
+ description: item.description,
218
+ unit: item.unit,
219
+ quantity: item.quantity,
220
+ unitCost: item.unitCost,
221
+ total: Math.round(item.total * 100) / 100,
222
+ percentage: Math.round(percentage * 100) / 100,
223
+ accumulated: Math.round(accumulated * 100) / 100,
224
+ class: itemClass,
225
+ resourceType: abcClassifications[item.id] || 'Custo'
226
+ };
227
+ });
228
+ return {
229
+ totalValue: Math.round(totalValue * 100) / 100,
230
+ classAThreshold,
231
+ classBThreshold,
232
+ classACount,
233
+ classBCount,
234
+ classCCount,
235
+ items
236
+ };
237
+ }
@@ -49,7 +49,7 @@ export declare function searchBankItems(bankId: string, query: string, limit?: n
49
49
  /**
50
50
  * Prepares importing an item from bank to budget
51
51
  */
52
- export declare function prepareImportItem(budgetId: string, bankItem: BankItem, quantity: number): {
52
+ export declare function prepareImportItem(budgetId: string, bankItem: BankItem, quantity: number, level?: number): {
53
53
  action: string;
54
54
  preview: string;
55
55
  data: any;
@@ -62,4 +62,29 @@ export declare function getBankItemById(bankId: string, itemId: string): Promise
62
62
  * NOVA FUNÇÃO: Lista todos os itens de uma categoria específica
63
63
  */
64
64
  export declare function getCategoryItems(bankId: string, categoryId: string): Promise<BankItem[]>;
65
+ /**
66
+ * Creates a private bank for the user
67
+ */
68
+ export declare function createPrivateBank(auth: AuthContext, name: string, description?: string): Promise<string>;
69
+ /**
70
+ * Edits a bank's metadata (title, description, image)
71
+ */
72
+ export declare function editBank(auth: AuthContext, bankId: string, updates: {
73
+ title?: string;
74
+ description?: string;
75
+ background_image?: string;
76
+ versionDate?: string;
77
+ }): Promise<void>;
78
+ /**
79
+ * Adds an item to a private bank
80
+ */
81
+ export declare function addItemToBank(auth: AuthContext, bankId: string, categoryId: string, item: {
82
+ code: string;
83
+ description: string;
84
+ unit: string;
85
+ unitCost: number;
86
+ itemType?: string;
87
+ isComposition?: boolean;
88
+ composition?: any[];
89
+ }): Promise<string>;
65
90
  //# sourceMappingURL=bankService.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bankService.d.ts","sourceRoot":"","sources":["../../src/core/bankService.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAExC,MAAM,WAAW,WAAW;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,QAAQ,GAAG,SAAS,GAAG,cAAc,CAAC;IAClD,aAAa,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,gBAAgB;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,QAAQ;IACrB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE,OAAO,CAAC;IACvB,WAAW,CAAC,EAAE,gBAAgB,EAAE,CAAC;CACpC;AAED,MAAM,WAAW,YAAY;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM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  /**
185
185
  * Prepares importing an item from bank to budget
186
186
  */
187
- export function prepareImportItem(budgetId, bankItem, quantity) {
187
+ export function prepareImportItem(budgetId, bankItem, quantity, level) {
188
188
  let compositionForBudget = undefined;
189
189
  if (bankItem.isComposition && bankItem.composition && bankItem.composition.length > 0) {
190
190
  compositionForBudget = bankItem.composition.map(input => ({
@@ -209,7 +209,8 @@ export function prepareImportItem(budgetId, bankItem, quantity) {
209
209
  quantity: quantity,
210
210
  unitCost: bankItem.unitCost,
211
211
  isComposition: bankItem.isComposition,
212
- composition: compositionForBudget
212
+ composition: compositionForBudget,
213
+ level: level ?? 0
213
214
  }
214
215
  }
215
216
  };
@@ -303,3 +304,111 @@ export async function getCategoryItems(bankId, categoryId) {
303
304
  }
304
305
  return items;
305
306
  }
307
+ // ================ BANK MANAGEMENT ================
308
+ /**
309
+ * Creates a private bank for the user
310
+ */
311
+ export async function createPrivateBank(auth, name, description) {
312
+ const now = new Date().toISOString();
313
+ const bankData = {
314
+ title: name,
315
+ description: description || '',
316
+ author: auth.userName,
317
+ visibility: 'private',
318
+ tenant_id: auth.tenantId,
319
+ user_id: auth.userId,
320
+ created_at: now,
321
+ updated_at: now,
322
+ data: {
323
+ categories: []
324
+ }
325
+ };
326
+ const { data, error } = await supabase
327
+ .from('composition_banks')
328
+ .insert(bankData)
329
+ .select('id')
330
+ .single();
331
+ if (error)
332
+ throw new Error(`Failed to create bank: ${error.message}`);
333
+ return data.id;
334
+ }
335
+ /**
336
+ * Edits a bank's metadata (title, description, image)
337
+ */
338
+ export async function editBank(auth, bankId, updates) {
339
+ const updatePayload = {
340
+ updated_at: new Date().toISOString()
341
+ };
342
+ if (updates.title !== undefined)
343
+ updatePayload.title = updates.title;
344
+ if (updates.description !== undefined)
345
+ updatePayload.description = updates.description;
346
+ if (updates.background_image !== undefined)
347
+ updatePayload.background_image = updates.background_image;
348
+ if (updates.versionDate !== undefined)
349
+ updatePayload.version_date = updates.versionDate;
350
+ const { error } = await supabase
351
+ .from('composition_banks')
352
+ .update(updatePayload)
353
+ .eq('id', bankId)
354
+ .or(`tenant_id.eq.${auth.tenantId},user_id.eq.${auth.userId}`);
355
+ if (error)
356
+ throw new Error(`Failed to edit bank: ${error.message}`);
357
+ }
358
+ /**
359
+ * Adds an item to a private bank
360
+ */
361
+ export async function addItemToBank(auth, bankId, categoryId, item) {
362
+ // Get current bank data
363
+ const { data: bank, error: fetchError } = await supabase
364
+ .from('composition_banks')
365
+ .select('data')
366
+ .eq('id', bankId)
367
+ .or(`tenant_id.eq.${auth.tenantId},user_id.eq.${auth.userId}`)
368
+ .single();
369
+ if (fetchError || !bank)
370
+ throw new Error('Bank not found or not accessible');
371
+ const categories = bank.data?.categories || [];
372
+ // Find category and add item
373
+ let found = false;
374
+ const findAndAddToCategory = (nodes) => {
375
+ for (const node of nodes) {
376
+ if (node.id === categoryId) {
377
+ if (!node.items)
378
+ node.items = [];
379
+ node.items.push({
380
+ id: crypto.randomUUID(),
381
+ ...item
382
+ });
383
+ found = true;
384
+ return true;
385
+ }
386
+ if (node.children && findAndAddToCategory(node.children)) {
387
+ return true;
388
+ }
389
+ }
390
+ return false;
391
+ };
392
+ findAndAddToCategory(categories);
393
+ if (!found) {
394
+ // Create a new category if not found
395
+ categories.push({
396
+ id: categoryId,
397
+ name: 'Itens Personalizados',
398
+ items: [{
399
+ id: crypto.randomUUID(),
400
+ ...item
401
+ }]
402
+ });
403
+ }
404
+ const { error } = await supabase
405
+ .from('composition_banks')
406
+ .update({
407
+ data: { categories },
408
+ updated_at: new Date().toISOString()
409
+ })
410
+ .eq('id', bankId);
411
+ if (error)
412
+ throw new Error(`Failed to add item to bank: ${error.message}`);
413
+ return 'Item added successfully';
414
+ }
@@ -14,6 +14,7 @@ export interface BudgetRow {
14
14
  export interface Budget {
15
15
  id: string;
16
16
  title: string;
17
+ baseDate?: string;
17
18
  createdAt: string;
18
19
  lastModified: string;
19
20
  creator: string;
@@ -22,6 +23,8 @@ export interface Budget {
22
23
  rows: BudgetRow[];
23
24
  bdiPrincipal: number;
24
25
  bdiDiferenciado: number;
26
+ bdiItemsPrincipal: any[];
27
+ bdiItemsDiferenciado: any[];
25
28
  }
26
29
  export interface BudgetSummary {
27
30
  id: string;
@@ -61,6 +64,33 @@ export declare function prepareAddItem(budgetId: string, item: {
61
64
  unit: string;
62
65
  quantity: number;
63
66
  unitCost: number;
67
+ level?: number;
68
+ }): {
69
+ action: string;
70
+ preview: string;
71
+ data: any;
72
+ };
73
+ /**
74
+ * Interface para insumos de uma composição
75
+ */
76
+ export interface CompositionInput {
77
+ code?: string;
78
+ description: string;
79
+ unit: string;
80
+ quantity: number;
81
+ unitCost: number;
82
+ itemType?: string;
83
+ }
84
+ /**
85
+ * Prepares adding a COMPOSITION (with inputs) to a budget
86
+ */
87
+ export declare function prepareAddComposition(budgetId: string, composition: {
88
+ code?: string;
89
+ description: string;
90
+ unit: string;
91
+ quantity: number;
92
+ level?: number;
93
+ insumos: CompositionInput[];
64
94
  }): {
65
95
  action: string;
66
96
  preview: string;
@@ -88,4 +118,330 @@ export declare function executeDeleteItem(auth: AuthContext, data: any): Promise
88
118
  * Gets composition details (inputs/children) for a specific item in a budget
89
119
  */
90
120
  export declare function getCompositionDetails(auth: AuthContext, budgetId: string, itemId: string): Promise<any[]>;
121
+ /**
122
+ * Prepares updating BDI values for a budget
123
+ */
124
+ export declare function prepareUpdateBdi(budgetId: string, bdiPrincipal?: number, bdiDiferenciado?: number): {
125
+ action: string;
126
+ preview: string;
127
+ data: any;
128
+ };
129
+ /**
130
+ * Executes BDI update
131
+ */
132
+ export declare function executeUpdateBdi(auth: AuthContext, data: any): Promise<void>;
133
+ export interface BdiItem {
134
+ id?: string;
135
+ description: string;
136
+ percentage: number;
137
+ incidence: string;
138
+ }
139
+ /**
140
+ * Calcula o BDI total baseado nos itens de composição
141
+ * Fórmula: BDI = ((1 + i_custo) / (1 - i_venda) - 1) * 100
142
+ *
143
+ * Onde:
144
+ * - i_custo = soma dos % que incidem sobre o Custo (ex: Administração, Risco)
145
+ * - i_venda = soma dos % que incidem sobre a Venda (ex: ISS, PIS, COFINS, Lucro)
146
+ */
147
+ export declare function calculateBdiTotal(items: BdiItem[]): number;
148
+ /**
149
+ * Prepares adding an item to BDI composition
150
+ */
151
+ export declare function prepareAddBdiItem(budgetId: string, bdiType: 'Principal' | 'Diferenciado', item: BdiItem): {
152
+ action: string;
153
+ preview: string;
154
+ data: any;
155
+ };
156
+ /**
157
+ * Executes adding item to BDI composition
158
+ * ATUALIZADO: Recalcula automaticamente o BDI total
159
+ */
160
+ export declare function executeAddBdiItem(auth: AuthContext, data: any): Promise<void>;
161
+ /**
162
+ * Prepares editing an existing BDI item
163
+ */
164
+ export declare function prepareEditBdiItem(budgetId: string, bdiType: 'Principal' | 'Diferenciado', itemId: string, updates: Partial<BdiItem>): {
165
+ action: string;
166
+ preview: string;
167
+ data: any;
168
+ };
169
+ /**
170
+ * Executes editing BDI item
171
+ * ATUALIZADO: Recalcula automaticamente o BDI total
172
+ */
173
+ export declare function executeEditBdiItem(auth: AuthContext, data: any): Promise<void>;
174
+ /**
175
+ * Prepares removing an item from BDI composition
176
+ */
177
+ export declare function prepareRemoveBdiItem(budgetId: string, bdiType: 'Principal' | 'Diferenciado', itemId: string, itemDescription: string): {
178
+ action: string;
179
+ preview: string;
180
+ data: any;
181
+ };
182
+ /**
183
+ * Executes removing BDI item
184
+ * ATUALIZADO: Recalcula automaticamente o BDI total
185
+ */
186
+ export declare function executeRemoveBdiItem(auth: AuthContext, data: any): Promise<void>;
187
+ /**
188
+ * Prepares editing an existing item
189
+ */
190
+ export declare function prepareEditItem(budgetId: string, itemId: string, updates: {
191
+ description?: string;
192
+ unit?: string;
193
+ quantity?: number;
194
+ unitCost?: number;
195
+ level?: number;
196
+ code?: string;
197
+ }): {
198
+ action: string;
199
+ preview: string;
200
+ data: any;
201
+ };
202
+ /**
203
+ * Executes item edit
204
+ */
205
+ export declare function executeEditItem(auth: AuthContext, data: any): Promise<void>;
206
+ /**
207
+ * Prepares duplicating a budget
208
+ */
209
+ export declare function prepareDuplicateBudget(auth: AuthContext, budgetId: string, newTitle: string): {
210
+ action: string;
211
+ preview: string;
212
+ data: any;
213
+ };
214
+ /**
215
+ * Executes budget duplication
216
+ */
217
+ export declare function executeDuplicateBudget(auth: AuthContext, data: any): Promise<string>;
218
+ /**
219
+ * Prepares moving an item to a different level
220
+ */
221
+ export declare function prepareMoveItem(budgetId: string, itemId: string, newLevel: number, itemDescription: string): {
222
+ action: string;
223
+ preview: string;
224
+ data: any;
225
+ };
226
+ /**
227
+ * Prepares editing budget metadata
228
+ */
229
+ export declare function prepareEditBudget(budgetId: string, updates: {
230
+ title?: string;
231
+ baseDate?: string;
232
+ bdiPrincipal?: number;
233
+ bdiDiferenciado?: number;
234
+ }): {
235
+ action: string;
236
+ preview: string;
237
+ data: any;
238
+ };
239
+ /**
240
+ * Executes budget metadata editing
241
+ */
242
+ export declare function executeEditBudget(auth: AuthContext, data: any): Promise<void>;
243
+ /**
244
+ * Prepares setting BDI type for an item
245
+ */
246
+ export declare function prepareSetBdiType(budgetId: string, itemId: string, bdiType: 'Principal' | 'Diferenciado', itemDescription: string): {
247
+ action: string;
248
+ preview: string;
249
+ data: any;
250
+ };
251
+ /**
252
+ * Prepares ABC classification for an item
253
+ */
254
+ export declare function prepareSetAbcClassification(budgetId: string, itemId: string, classification: 'Custo' | 'Material' | 'Mão de Obra' | 'Equipamento', itemDescription: string): {
255
+ action: string;
256
+ preview: string;
257
+ data: any;
258
+ };
259
+ /**
260
+ * Executes ABC classification setting
261
+ */
262
+ export declare function executeSetAbcClassification(auth: AuthContext, data: any): Promise<void>;
263
+ /**
264
+ * Prepares editing inputs of a composition
265
+ */
266
+ export declare function prepareEditCompositionInput(budgetId: string, compositionId: string, inputId: string, updates: {
267
+ code?: string;
268
+ description?: string;
269
+ unit?: string;
270
+ quantity?: number;
271
+ unitCost?: number;
272
+ itemType?: string;
273
+ }, inputDescription: string): {
274
+ action: string;
275
+ preview: string;
276
+ data: any;
277
+ };
278
+ /**
279
+ * Executes editing composition input
280
+ */
281
+ export declare function executeEditCompositionInput(auth: AuthContext, data: any): Promise<void>;
282
+ /**
283
+ * Prepares adding an input to a composition
284
+ */
285
+ export declare function prepareAddCompositionInput(budgetId: string, compositionId: string, input: {
286
+ code?: string;
287
+ description: string;
288
+ unit: string;
289
+ quantity: number;
290
+ unitCost: number;
291
+ itemType?: string;
292
+ }): {
293
+ action: string;
294
+ preview: string;
295
+ data: any;
296
+ };
297
+ /**
298
+ * Executes adding input to composition
299
+ */
300
+ export declare function executeAddCompositionInput(auth: AuthContext, data: any): Promise<void>;
301
+ /**
302
+ * Prepares removing an input from a composition
303
+ */
304
+ export declare function prepareRemoveCompositionInput(budgetId: string, compositionId: string, inputId: string, inputDescription: string): {
305
+ action: string;
306
+ preview: string;
307
+ data: any;
308
+ };
309
+ /**
310
+ * Executes removing input from composition
311
+ */
312
+ export declare function executeRemoveCompositionInput(auth: AuthContext, data: any): Promise<void>;
313
+ /**
314
+ * Prepares converting a simple cost item to a composition
315
+ */
316
+ export declare function prepareConvertToComposition(budgetId: string, itemId: string, itemDescription: string): {
317
+ action: string;
318
+ preview: string;
319
+ data: any;
320
+ };
321
+ /**
322
+ * Executes converting a cost item to a composition
323
+ */
324
+ export declare function executeConvertToComposition(auth: AuthContext, data: any): Promise<void>;
325
+ /**
326
+ * Prepares converting a composition back to simple cost
327
+ */
328
+ export declare function prepareConvertToCost(budgetId: string, itemId: string, itemDescription: string): {
329
+ action: string;
330
+ preview: string;
331
+ data: any;
332
+ };
333
+ /**
334
+ * Executes converting a composition back to cost
335
+ */
336
+ export declare function executeConvertToCost(auth: AuthContext, data: any): Promise<void>;
337
+ export interface Tag {
338
+ id: string;
339
+ budget_id: string;
340
+ name: string;
341
+ created_at: string;
342
+ }
343
+ /**
344
+ * Lists all tags for a specific budget
345
+ */
346
+ export declare function getTagsForBudget(budgetId: string): Promise<Tag[]>;
347
+ /**
348
+ * Prepares adding a tag to a budget
349
+ */
350
+ export declare function prepareAddTag(budgetId: string, name: string): {
351
+ action: string;
352
+ preview: string;
353
+ data: any;
354
+ };
355
+ /**
356
+ * Executes adding a tag
357
+ */
358
+ export declare function executeAddTag(auth: AuthContext, data: any): Promise<Tag>;
359
+ /**
360
+ * Prepares editing a tag
361
+ */
362
+ export declare function prepareEditTag(tagId: string, newName: string, currentName: string): {
363
+ action: string;
364
+ preview: string;
365
+ data: any;
366
+ };
367
+ /**
368
+ * Executes editing a tag
369
+ */
370
+ export declare function executeEditTag(auth: AuthContext, data: any): Promise<void>;
371
+ /**
372
+ * Prepares removing a tag
373
+ */
374
+ export declare function prepareRemoveTag(tagId: string, tagName: string): {
375
+ action: string;
376
+ preview: string;
377
+ data: any;
378
+ };
379
+ /**
380
+ * Executes removing a tag
381
+ */
382
+ export declare function executeRemoveTag(auth: AuthContext, data: any): Promise<void>;
383
+ export interface Project {
384
+ id: string;
385
+ code?: string;
386
+ name: string;
387
+ description?: string;
388
+ created_at: string;
389
+ updated_at?: string;
390
+ }
391
+ /**
392
+ * Lists all projects for the authenticated user's tenant
393
+ */
394
+ export declare function getProjects(auth: AuthContext): Promise<Project[]>;
395
+ /**
396
+ * Prepares creating a new project
397
+ */
398
+ export declare function prepareCreateProject(name: string, description?: string, code?: string): {
399
+ action: string;
400
+ preview: string;
401
+ data: any;
402
+ };
403
+ /**
404
+ * Executes creating a project
405
+ */
406
+ export declare function executeCreateProject(auth: AuthContext, data: any): Promise<Project>;
407
+ /**
408
+ * Prepares editing a project
409
+ */
410
+ export declare function prepareEditProject(projectId: string, updates: {
411
+ name?: string;
412
+ description?: string;
413
+ code?: string;
414
+ }, currentName: string): {
415
+ action: string;
416
+ preview: string;
417
+ data: any;
418
+ };
419
+ /**
420
+ * Executes editing a project
421
+ */
422
+ export declare function executeEditProject(auth: AuthContext, data: any): Promise<void>;
423
+ /**
424
+ * Prepares deleting a project (soft delete)
425
+ */
426
+ export declare function prepareDeleteProject(projectId: string, projectName: string): {
427
+ action: string;
428
+ preview: string;
429
+ data: any;
430
+ };
431
+ /**
432
+ * Executes deleting a project (soft delete)
433
+ */
434
+ export declare function executeDeleteProject(auth: AuthContext, data: any): Promise<void>;
435
+ /**
436
+ * Prepares linking a budget to a project
437
+ */
438
+ export declare function prepareLinkBudgetToProject(budgetId: string, projectId: string | null, budgetTitle: string, projectName: string | null): {
439
+ action: string;
440
+ preview: string;
441
+ data: any;
442
+ };
443
+ /**
444
+ * Executes linking a budget to a project
445
+ */
446
+ export declare function executeLinkBudgetToProject(auth: AuthContext, data: any): Promise<void>;
91
447
  //# sourceMappingURL=budgetService.d.ts.map