fix: Add bank/cash name sorting to treasury register

- Add bancasa as third sorting criterion (date, number, bank name)
- Sort null-date rows (previous balances) alphabetically by bank name
- Sort bank names alphabetically in PDF export
- Improve window function for cumulative balance calculation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-05 14:13:30 +02:00
parent eb3dc195ed
commit 615593eb40
7 changed files with 1596 additions and 334 deletions

View File

@@ -15,6 +15,7 @@ export const useTreasuryStore = defineStore("treasury", () => {
total_incasari: 0,
total_plati: 0,
});
const accountingPeriod = ref({ an: null, luna: null });
const loadBankCashRegister = async (companyId, filters = {}) => {
isLoading.value = true;
@@ -39,6 +40,11 @@ export const useTreasuryStore = defineStore("treasury", () => {
total_plati: response.data.total_plati,
};
// Store accounting period if available
if (response.data.accounting_period) {
accountingPeriod.value = response.data.accounting_period;
}
return { success: true };
} catch (err) {
error.value = err.response?.data?.detail || "Failed to load register";
@@ -57,6 +63,7 @@ export const useTreasuryStore = defineStore("treasury", () => {
registers.value = [];
isLoading.value = false;
error.value = null;
accountingPeriod.value = { an: null, luna: null };
pagination.value = {
page: 0,
rows: 50,
@@ -70,6 +77,7 @@ export const useTreasuryStore = defineStore("treasury", () => {
error,
pagination,
totals,
accountingPeriod,
loadBankCashRegister,
setPagination,
reset,

View File

@@ -55,7 +55,7 @@ const formatNumberForPDF = (value) => {
* @param {Array} data - Array of objects to export
* @param {Array} columns - Column definitions [{field: 'key', header: 'Display Name', type: 'text|number|currency', width: 30}]
* @param {String} filename - Name of the file (without extension)
* @param {Object} header - Header configuration {companyName: '', title: '', period: ''}
* @param {Object} header - Header configuration {companyName: '', title: '', period: '', subtitle2: '', initialBalances: [], totalInitialBalance: 0}
*/
export const exportToPDF = (data, columns, filename, header) => {
try {
@@ -78,6 +78,9 @@ export const exportToPDF = (data, columns, filename, header) => {
const marginRight = 8;
const contentWidth = pageWidth - marginLeft - marginRight;
// Check if there are initial balances to display
const hasInitialBalances = header.initialBalances && header.initialBalances.length > 0;
// Function to add header (called for each page)
const addHeader = () => {
// Line 1: Company name (left aligned, bold, larger font)
@@ -100,12 +103,31 @@ export const exportToPDF = (data, columns, filename, header) => {
const periodWidth = doc.getTextWidth(periodText);
const periodX = marginLeft + (contentWidth - periodWidth) / 2;
doc.text(periodText, periodX, 32);
// Line 4: Subtitle2 - filters (left aligned, below period) - optional
let currentY = 32;
if (header.subtitle2) {
currentY = 39;
doc.setFontSize(10);
doc.setFont(undefined, "normal");
doc.text(header.subtitle2, marginLeft, currentY);
}
// Initial Balances section - rendered just before table, closer to it
// This is handled in didDrawPage for first page only
};
// Prepare table data
// Prepare table data and track total rows
const tableColumns = columns.map((col) => col.header);
const tableRows = data.map((row) =>
columns.map((col) => {
const totalRowIndices = new Set(); // Track which rows are totals
const tableRows = data.map((row, rowIndex) => {
// Track total rows for special styling
if (row._isTotal) {
totalRowIndices.add(rowIndex);
}
return columns.map((col) => {
const value = row[col.field];
if (col.type === "currency") {
return formatCurrency(value);
@@ -113,8 +135,8 @@ export const exportToPDF = (data, columns, filename, header) => {
return formatNumberForPDF(value);
}
return value || "-";
}),
);
});
});
// Function to add footer (called for each page)
const addFooter = (pageNum, totalPages) => {
@@ -183,7 +205,64 @@ export const exportToPDF = (data, columns, filename, header) => {
}
});
const tableStartY = 36;
// Start table lower based on header content
let tableStartY = 36;
if (header.subtitle2) tableStartY = 43;
if (hasInitialBalances) {
// Initial balances rendered close to table (just 3mm above table header)
const balancesCount = header.initialBalances.length;
const hasTotal = balancesCount > 1;
const balancesHeight = (balancesCount * 5) + (hasTotal ? 7 : 0);
// Base position after header content
const baseY = header.subtitle2 ? 43 : 36;
tableStartY = baseY + balancesHeight + 5; // balances + small gap before table
}
// Function to draw initial balances (called only on first page)
const drawInitialBalances = (tableY) => {
if (!hasInitialBalances) return;
const valueRightEdge = pageWidth - marginRight;
const balancesCount = header.initialBalances.length;
const hasTotal = balancesCount > 1;
const balancesHeight = (balancesCount * 5) + (hasTotal ? 7 : 0);
// Start position: just above table header (3mm gap)
let y = tableY - 3 - (hasTotal ? 7 : 0) - (balancesCount * 5);
doc.setFont(undefined, "normal");
doc.setFontSize(9);
// Draw each balance line: "AccountName sold precedent: VALUE"
header.initialBalances.forEach((item) => {
const value = formatNumberForPDF(item.sold);
const valueWidth = doc.getTextWidth(value);
const label = `${item.accountName} sold precedent:`;
doc.text(label, valueRightEdge - valueWidth - doc.getTextWidth(" sold precedent:") - doc.getTextWidth(item.accountName) - 2, y);
doc.text(value, valueRightEdge - valueWidth, y);
y += 5;
});
// Total only if multiple accounts
if (hasTotal) {
// Separator line
doc.setDrawColor(150, 150, 150);
doc.line(valueRightEdge - 40, y - 2, valueRightEdge, y - 2);
// Total line
doc.setFont(undefined, "bold");
const totalValue = formatNumberForPDF(header.totalInitialBalance || 0);
const totalValueWidth = doc.getTextWidth(totalValue);
const totalLabel = "TOTAL sold precedent:";
const totalLabelWidth = doc.getTextWidth(totalLabel);
doc.text(totalLabel, valueRightEdge - totalValueWidth - 3 - totalLabelWidth, y + 2);
doc.text(totalValue, valueRightEdge - totalValueWidth, y + 2);
}
};
let isFirstPage = true;
// Add table using autoTable (call as function, not method)
autoTable(doc, {
@@ -218,16 +297,28 @@ export const exportToPDF = (data, columns, filename, header) => {
},
tableWidth: pageWidth - marginLeft - marginRight, // Use full page width
theme: "grid",
didDrawPage: function (data) {
didDrawPage: function () {
// Add header to each page
addHeader();
// Draw initial balances only on first page
if (isFirstPage && hasInitialBalances) {
drawInitialBalances(tableStartY);
isFirstPage = false;
}
},
didParseCell: function (data) {
// Force alignment based on column type (body cells only)
if (data.section === "body") {
const rowIndex = data.row.index;
const colIndex = data.column.index;
const column = columns[colIndex];
// Style total rows differently (bold, light gray background)
if (totalRowIndices.has(rowIndex)) {
data.cell.styles.fontStyle = "bold";
data.cell.styles.fillColor = [230, 230, 230]; // Light gray
}
if (column) {
if (column.type === "number" || column.type === "currency") {
data.cell.styles.halign = "right";
@@ -351,6 +442,375 @@ export const exportSoldNetBreakdown = (summaryData) => {
return data;
};
/**
* Export Bank Cash Register to PDF with grouped format
* Matches the Romanian standard format with:
* - Bank name + Sold precedent on same line
* - Daily totals (Total zi)
* - Cumulative totals (Total cumulat)
*
* @param {Array} data - Array of register entries
* @param {Object} header - Header configuration
* @param {String} filename - Output filename
*/
export const exportBankCashRegisterPDF = (data, header, filename) => {
try {
if (!data || data.length === 0) {
console.error("No data to export");
return { success: false, error: "No data available" };
}
const doc = new jsPDF("landscape", "mm", "a4");
const pageWidth = doc.internal.pageSize.getWidth();
const pageHeight = doc.internal.pageSize.getHeight();
const marginLeft = 8;
const marginRight = 8;
const contentWidth = pageWidth - marginLeft - marginRight;
// Remove diacritics helper
const removeDiacritics = (text) => {
if (!text) return "";
return text
.replace(/[ăâ]/gi, (m) => (m === m.toLowerCase() ? "a" : "A"))
.replace(/[î]/gi, (m) => (m === m.toLowerCase() ? "i" : "I"))
.replace(/[ș]/gi, (m) => (m === m.toLowerCase() ? "s" : "S"))
.replace(/[ț]/gi, (m) => (m === m.toLowerCase() ? "t" : "T"));
};
// Group data by bank account (bancasa)
const groupedByBank = {};
const initialBalances = {};
data.forEach((row) => {
const bankName = row.nume_cont_bancar || "Necunoscut";
if (!groupedByBank[bankName]) {
groupedByBank[bankName] = [];
initialBalances[bankName] = 0;
}
if (!row.dataact) {
// Initial balance row (null date) - sold precedent
initialBalances[bankName] = parseFloat(row.sold) || 0;
} else {
// Transaction row with date
groupedByBank[bankName].push(row);
}
});
// Table columns definition
const tableColumns = [
"Data act",
"Nr.act",
"Explicatia",
"Incasari",
"Plati",
"Sold",
];
const columnWidths = {
0: contentWidth * 0.10, // Data act
1: contentWidth * 0.08, // Nr.act
2: contentWidth * 0.42, // Explicatia
3: contentWidth * 0.13, // Incasari
4: contentWidth * 0.13, // Plati
5: contentWidth * 0.14, // Sold
};
const columnStyles = {};
Object.keys(columnWidths).forEach((idx) => {
columnStyles[idx] = { cellWidth: columnWidths[idx] };
if (idx >= 3) {
columnStyles[idx].halign = "right";
}
});
let currentY = 15;
let pageNum = 1;
// Function to add page header
const addPageHeader = () => {
// Company name (left)
doc.setFontSize(12);
doc.setFont(undefined, "bold");
doc.text(removeDiacritics(header.companyName || ""), marginLeft, 12);
// Luna: MM / YYYY (right)
doc.setFontSize(10);
doc.setFont(undefined, "normal");
const lunaText = `Luna: ${header.luna || ""} / ${header.an || ""}`;
const lunaWidth = doc.getTextWidth(lunaText);
doc.text(lunaText, pageWidth - marginRight - lunaWidth, 12);
// Title centered
doc.setFontSize(13);
doc.setFont(undefined, "bold");
const titleWidth = doc.getTextWidth(header.title || "");
doc.text(header.title || "", marginLeft + (contentWidth - titleWidth) / 2, 20);
};
// Function to check if we need a new page (for tables spanning multiple pages within a bank)
const checkNewPage = (neededHeight = 20) => {
if (currentY + neededHeight > pageHeight - 15) {
doc.addPage();
pageNum++;
addPageHeader();
currentY = 28;
return true;
}
return false;
};
// Process each bank account - each on a new page (sorted alphabetically)
const bankNames = Object.keys(groupedByBank).sort((a, b) => a.localeCompare(b, 'ro'));
bankNames.forEach((bankName, bankIndex) => {
const bankRows = groupedByBank[bankName];
const soldPrecedent = initialBalances[bankName] || 0;
// Start each bank/casa on a new page (except first one which is already on page 1)
if (bankIndex > 0) {
doc.addPage();
pageNum++;
}
// Add full page header (company, title, luna/an)
addPageHeader();
currentY = 28;
// Bank/Casa header: "Banca: NAME" (left) + "Sold precedent: VALUE" (right)
doc.setFontSize(10);
doc.setFont(undefined, "bold");
const bankLabel = header.isBanca ? "Banca:" : "Casa:";
const bankHeaderText = `${bankLabel} ${removeDiacritics(bankName)}`;
doc.text(bankHeaderText, marginLeft, currentY);
const soldPrecedentText = `Sold precedent: ${formatNumberForPDF(soldPrecedent)}`;
const soldPrecedentWidth = doc.getTextWidth(soldPrecedentText);
doc.text(soldPrecedentText, pageWidth - marginRight - soldPrecedentWidth, currentY);
currentY += 6;
// Handle case when there are no transactions (only initial balance)
if (bankRows.length === 0) {
// Draw empty table with header only
autoTable(doc, {
head: [tableColumns],
body: [],
startY: currentY,
styles: {
fontSize: 8,
cellPadding: 1.5,
lineColor: [200, 200, 200],
lineWidth: 0.1,
},
headStyles: {
fillColor: [41, 128, 185],
textColor: 255,
fontStyle: "bold",
halign: "center",
fontSize: 8,
},
columnStyles: columnStyles,
margin: { left: marginLeft, right: marginRight },
tableWidth: contentWidth,
theme: "grid",
});
currentY = doc.lastAutoTable.finalY;
// Show total with sold precedent (no transactions)
const totalRows = [
[
"",
"",
"Total:",
formatNumberForPDF(0),
formatNumberForPDF(0),
formatNumberForPDF(soldPrecedent),
],
];
const totalsStartY = currentY;
autoTable(doc, {
body: totalRows,
startY: currentY,
styles: {
fontSize: 8,
cellPadding: 1.5,
fontStyle: "bold",
lineWidth: 0,
},
columnStyles: columnStyles,
margin: { left: marginLeft, right: marginRight },
tableWidth: contentWidth,
theme: "plain",
});
// Draw outer border for totals box
const totalsEndY = doc.lastAutoTable.finalY;
doc.setDrawColor(200, 200, 200);
doc.setLineWidth(0.1);
doc.rect(marginLeft, totalsStartY, contentWidth, totalsEndY - totalsStartY);
currentY = doc.lastAutoTable.finalY + 3;
} else {
// Group bank rows by date
const groupedByDate = {};
bankRows.forEach((row) => {
const dateKey = row.dataact;
if (!groupedByDate[dateKey]) {
groupedByDate[dateKey] = [];
}
groupedByDate[dateKey].push(row);
});
// Cumulative totals for the bank
let cumulativeIncasari = 0;
let cumulativePlati = 0;
let lastSold = soldPrecedent;
const dates = Object.keys(groupedByDate).sort();
dates.forEach((dateKey, dateIndex) => {
const dateRows = groupedByDate[dateKey];
const dateFormatted = dateKey
? new Date(dateKey).toLocaleDateString("ro-RO")
: "";
checkNewPage(30);
// Prepare rows for this date
const tableRows = [];
let dailyIncasari = 0;
let dailyPlati = 0;
dateRows.forEach((row) => {
const incasari = parseFloat(row.incasari) || 0;
const plati = parseFloat(row.plati) || 0;
dailyIncasari += incasari;
dailyPlati += plati;
lastSold = parseFloat(row.sold) || lastSold;
tableRows.push([
dateFormatted,
row.nract || "",
removeDiacritics(row.explicatia || row.nume || ""),
formatNumberForPDF(incasari),
formatNumberForPDF(plati),
formatNumberForPDF(row.sold),
]);
});
cumulativeIncasari += dailyIncasari;
cumulativePlati += dailyPlati;
// Draw table for this date group
autoTable(doc, {
head: dateIndex === 0 ? [tableColumns] : [],
body: tableRows,
startY: currentY,
styles: {
fontSize: 8,
cellPadding: 1.5,
lineColor: [200, 200, 200],
lineWidth: 0.1,
overflow: "linebreak",
},
headStyles: {
fillColor: [41, 128, 185],
textColor: 255,
fontStyle: "bold",
halign: "center",
fontSize: 8,
},
columnStyles: columnStyles,
margin: { left: marginLeft, right: marginRight },
tableWidth: contentWidth,
theme: "grid",
showHead: dateIndex === 0 ? "firstPage" : "never",
});
currentY = doc.lastAutoTable.finalY;
// Daily total + Cumulative total rows in same box
checkNewPage(16);
const totalRows = [
[
"",
"",
`Total zi: ${dateFormatted}`,
formatNumberForPDF(dailyIncasari),
formatNumberForPDF(dailyPlati),
"Sold",
],
[
"",
"",
"Total cumulat:",
formatNumberForPDF(cumulativeIncasari),
formatNumberForPDF(cumulativePlati),
formatNumberForPDF(lastSold),
],
];
const totalsStartY = currentY;
autoTable(doc, {
body: totalRows,
startY: currentY,
styles: {
fontSize: 8,
cellPadding: 1.5,
fontStyle: "bold",
lineWidth: 0,
},
columnStyles: columnStyles,
margin: { left: marginLeft, right: marginRight },
tableWidth: contentWidth,
theme: "plain",
});
// Draw outer border for totals box (no internal lines)
const totalsEndY = doc.lastAutoTable.finalY;
doc.setDrawColor(200, 200, 200);
doc.setLineWidth(0.1);
doc.rect(marginLeft, totalsStartY, contentWidth, totalsEndY - totalsStartY);
currentY = doc.lastAutoTable.finalY + 3;
});
}
});
// Add footer to all pages (Generat: DATE on left, Pagina X din Y on right)
const totalPages = doc.internal.getNumberOfPages();
const generatedText = `Generat: ${new Date().toLocaleString("ro-RO")}`;
for (let i = 1; i <= totalPages; i++) {
doc.setPage(i);
doc.setFontSize(8);
doc.setFont(undefined, "normal");
const footerY = pageHeight - 8;
// Left: Generated date
doc.text(generatedText, marginLeft, footerY);
// Right: Page number
const pageText = `Pagina ${i} din ${totalPages}`;
const pageTextWidth = doc.getTextWidth(pageText);
doc.text(pageText, pageWidth - marginRight - pageTextWidth, footerY);
}
doc.save(`${filename}_${new Date().toISOString().split("T")[0]}.pdf`);
return { success: true };
} catch (error) {
console.error("Bank Cash Register PDF export error:", error);
return { success: false, error: error.message || "PDF generation failed" };
}
};
/**
* Export Trend Data
*/

File diff suppressed because it is too large Load Diff

View File

@@ -797,7 +797,15 @@ const searchData = () => {
}
};
// Watchers - removed unused watchers
// Watch for company changes - reload dashboard when company changes
watch(
() => companyStore.selectedCompany,
async (newCompany) => {
if (newCompany) {
await loadDashboardData();
}
},
);
// Lifecycle
onMounted(async () => {