-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdenis.html
225 lines (187 loc) · 9.08 KB
/
denis.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="ro">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profitul lui Denis</title>
<link rel="stylesheet" href="style2.css">
</head>
<body data-id="store1">
<h1>Bine ai revenit Denis!</h1>
<h2><span id="date"></span> - <span id="month"></span></h2>
<table id="profitTable"></table>
<h3>Calculeaza-ti profitul pe luna aceasta</h3>
<table id="weeklySummaryTable"></table><br>
<button onclick="saveProfitAsText()" class="custom-button">Descarca Profitul</button>
<script>
// Get a unique identifier for this page from the `data-id` attribute
const pageID = document.body.getAttribute('data-id'); // "store1", "store2", etc.
function createProfitTable() {
const date = new Date();
const month = date.getMonth(); // 0-indexed, so January is 0
const year = date.getFullYear();
// Get the number of days in the current month
const daysInMonth = new Date(year, month + 1, 0).getDate();
// Create table for profit display
const profitTable = document.getElementById('profitTable');
profitTable.innerHTML = ''; // Clear previous content
// Create table header for weekdays
const headerRow = document.createElement('tr');
const daysOfWeek = ['Luni', 'Marti', 'Miercuri', 'Joi', 'Vineri']; // Only weekdays, no 'Total' column
daysOfWeek.forEach(day => {
const th = document.createElement('th');
th.innerText = day;
headerRow.appendChild(th);
});
profitTable.appendChild(headerRow);
// Create the profit rows for 4 weeks, excluding weekends
let currentRow;
let dayCount = 1;
for (let week = 0; week < 4; week++) { // 4 weeks
currentRow = document.createElement('tr');
let weekdaysCount = 0;
while (weekdaysCount < 5 && dayCount <= daysInMonth) { // 5 weekdays per week
const currentDate = new Date(year, month, dayCount);
const dayOfWeek = currentDate.getDay(); // Get the day of the week (0 = Sunday, 6 = Saturday)
// Only add input fields for weekdays (Monday to Friday)
if (dayOfWeek >= 1 && dayOfWeek <= 5) {
const td = document.createElement('td');
// Create an input field for profit for each day
const input = document.createElement('input');
input.type = 'number';
input.className = 'input-cell';
input.placeholder = '0';
input.dataset.date = `${pageID}-${year}-${month + 1}-${dayCount}`; // Store date in data attribute
// Load saved profit from local storage
const savedProfit = localStorage.getItem(input.dataset.date);
if (savedProfit) {
input.value = savedProfit; // Set input value to saved profit
updateInputColor(input); // Update color based on loaded value
}
// Function to update background color based on value
input.oninput = function() {
localStorage.setItem(input.dataset.date, input.value); // Save profit to local storage
updateInputColor(input); // Update color based on current value
updateTotalProfit(); // Update total profit when input changes
};
td.appendChild(input);
currentRow.appendChild(td);
weekdaysCount++;
}
dayCount++;
}
// Append the row only if it has cells
if (weekdaysCount > 0) {
profitTable.appendChild(currentRow);
}
}
// After creating the profit table, update the weekly totals and the monthly total table
updateTotalProfit();
}
// Function to update input color based on value
function updateInputColor(input) {
const value = Number(input.value);
if (value > 0) {
input.style.backgroundColor = 'lightgreen'; // Positive profit
} else if (value < 0) {
input.style.backgroundColor = 'lightcoral'; // Negative profit
} else {
input.style.backgroundColor = 'lightyellow'; // Zero profit
}
}
// Function to update weekly and monthly totals
function updateTotalProfit() {
let totalProfit = 0;
const weeklySummaryTable = document.getElementById('weeklySummaryTable');
weeklySummaryTable.innerHTML = ''; // Clear previous weekly summary content
// Create header row for the summary table
const summaryHeader = document.createElement('tr');
const headers = ['Sapt #1', 'Sapt #2', 'Sapt #3', 'Sapt #4', 'Profit Lunar'];
headers.forEach(header => {
const th = document.createElement('th');
th.innerText = header;
summaryHeader.appendChild(th);
});
weeklySummaryTable.appendChild(summaryHeader);
// Array to hold the weekly sums
let weeklySums = [];
const rows = document.querySelectorAll('#profitTable tr');
// Calculate and update each week's total
for (let i = 1; i < rows.length; i++) { // Skip the header row
let weeklySum = 0;
const inputs = rows[i].querySelectorAll('.input-cell');
inputs.forEach(input => {
const value = parseFloat(input.value) || 0;
weeklySum += value;
});
totalProfit += weeklySum;
weeklySums.push(weeklySum.toFixed(2));
}
// Create a new row to display the weekly totals in the second table
const summaryRow = document.createElement('tr');
weeklySums.forEach(sum => {
const td = document.createElement('td');
td.innerText = sum;
summaryRow.appendChild(td);
});
// Add the total monthly profit
const totalTd = document.createElement('td');
totalTd.innerText = totalProfit.toFixed(2);
summaryRow.appendChild(totalTd);
weeklySummaryTable.appendChild(summaryRow);
}
function formatDate() {
const date = new Date();
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
function getCurrentMonth() {
const date = new Date();
const options = { month: 'long' }; // Specify that we want the full month name
return new Intl.DateTimeFormat('ro-RO', options).format(date);
}
function saveProfitAsText() {
// Get the current date and month
const date = new Date();
const day = String(date.getDate()).padStart(2, '0');
const month = new Intl.DateTimeFormat('ro-RO', { month: 'long' }).format(date); // Full month name in Romanian
const year = date.getFullYear();
// Start building the text for the file
let profitText = `Raport profit lunar pentru Denis | ${day} ${month} ${year}\n`;
profitText += "-------------------------\n";
// Get weekly profit data from the table
const weeklySummaryTable = document.getElementById('weeklySummaryTable');
const rows = weeklySummaryTable.getElementsByTagName('tr');
if (rows.length > 1) { // Ensure there's data in the table
const weeklySums = rows[1].getElementsByTagName('td');
// Add each week's profit to the text file
for (let i = 0; i < weeklySums.length - 1; i++) { // Skip the last cell (monthly profit)
profitText += `Săptămâna ${i + 1}: ${weeklySums[i].innerText}\n`;
}
// Add monthly profit
const monthlyProfit = weeklySums[weeklySums.length - 1].innerText;
profitText += "-------------------------\n";
profitText += `Profit Lunar: ${monthlyProfit}\n`;
} else {
profitText += "Nu există date pentru salvare.\n";
}
// Create a Blob with the profit text
const blob = new Blob([profitText], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = `profit_report_${month}_${year}_denis.txt`; // Include month and year in filename
// Simulate a click on the link to trigger the download
link.click();
// Clean up by revoking the object URL
URL.revokeObjectURL(link.href);
}
window.onload = function() {
document.getElementById('date').innerText = formatDate();
document.getElementById('month').innerText = getCurrentMonth();
createProfitTable()
};
</script>
</body>
</html>