-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
469 lines (404 loc) · 17.9 KB
/
main.js
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
let uygulama_sayisi = 1;
let uygulama_sayisi_tab2 = 1;
//* Retrieve the options for msInput
fetch("./data/bolge-katsayileri.json").then((response) => response.json()).then((tablo8_data) => {
retrive_msInput_options(tablo8_data);
});
//* Retrieve the options for ygInput
fetch("./data/birim-maliyet.json").then((response) => response.json()).then((bm_data) => {
for (let index = 0; index < Object.keys(bm_data).length; index++) {
const key_name = Object.keys(bm_data)[index];
let key_name_sliced = key_name.slice(0,1);
for (let index = 0; index < Object.keys(bm_data[key_name]).length; index++) {
const second_key_name = Object.keys(bm_data[key_name])[index];
document.getElementById("ygInput").innerHTML += `
<option value="`+key_name_sliced + second_key_name+`">`+key_name_sliced + '-' + second_key_name+`</option>
`;
document.getElementById("ygInput_tab2").innerHTML += `
<option value="`+key_name_sliced + second_key_name+`">`+key_name_sliced + '-' + second_key_name+`</option>
`;
}
}
});
//* Retrieve the options for tsInput
fetch("./data/yapi-sinifi-puani.json").then((response) => response.json()).then((ysp_data) => {
// ysp_data = yapı sınıfı puanı data
const tsy_data = ysp_data["tasiyici sistem yapisi"]; // tsy_data = tasiyici sistem yapisi data
for (let index = 0; index < Object.keys(tsy_data).length; index++) {
const key_name = Object.keys(tsy_data)[index];
document.getElementById("tsInput").innerHTML += `
<option value="`+key_name+`">`+key_name+`</option>
`;
}
});
//* Retrieve the options for temelInput
fetch("./data/yapi-sinifi-puani.json").then((response) => response.json()).then((ysp_data) => {
// ysp_data = yapı sınıfı puanı data
const ts_data = ysp_data["temel sistemi"]; // ts_data = temel sistemi data
for (let index = 0; index < Object.keys(ts_data).length; index++) {
const key_name = Object.keys(ts_data)[index];
document.getElementById("temelInput").innerHTML += `
<option value="`+key_name+`">`+key_name+`</option>
`;
}
});
function retrive_msInput_options(data) {
let msInput = document.getElementById("msInput");
for (let index = 0; index < Object.keys(data).length; index++) {
const city_name = Object.keys(data)[index];
msInput.innerHTML += '<option id="' + city_name + '" value="' + city_name + '">' + city_name + '</option>';
}
}
function msInput_selected(selected_value) {
let pubInput = document.getElementById("pubInput");
pubInput.innerHTML = "";
//* retrieve the options for pubInput
fetch("./data/bolge-katsayileri.json").then((response) => response.json()).then((tablo8_data) => {
for (let index = 0; index < Object.keys(tablo8_data[selected_value]).length; index++) {
const element = Object.keys(tablo8_data[selected_value])[index];
let html_string;
if (index != 0) html_string = '<option id="' + element + '" value="' + element + '">' + element + '</option>';
else {
html_string = '<option id="' + element + '" value="' + element + '" selected>' + element + '</option>';
}
pubInput.innerHTML += html_string;
}
});
}
function increase_us(is_tab2 = false) {
if(is_tab2){
uygulama_sayisi_tab2 += 1;
document.getElementById("usInput_tab2").value = uygulama_sayisi_tab2.toString() + ". Uygulama";
}
else{
uygulama_sayisi += 1;
document.getElementById("usInput").value = uygulama_sayisi.toString() + ". Uygulama";
}
}
function decrease_us(is_tab2 = false) {
if(is_tab2){
if (uygulama_sayisi_tab2 == 1) return;
uygulama_sayisi_tab2 -= 1;
document.getElementById("usInput_tab2").value = uygulama_sayisi_tab2.toString() + ". Uygulama";
}
else{
if (uygulama_sayisi == 1) return;
uygulama_sayisi -= 1;
document.getElementById("usInput").value = uygulama_sayisi.toString() + ". Uygulama";
}
}
async function tab1_calculate() {
let should_calculate = true;
let yaInput = document.getElementById("yaInput");
let msInput = document.getElementById("msInput");
let pubInput = document.getElementById("pubInput");
let ygInput = document.getElementById("ygInput");
let tsInput = document.getElementById("tsInput");
let temelInput = document.getElementById("temelInput");
let inputs = [yaInput, msInput, pubInput, ygInput, tsInput, temelInput];
let YSK, BM, PUO, PYK, HB, BK;
// YSK : Yapı Sınıfı Katsayısı
// BM : Birim Maliyet
// PUO : Proje Ücret Oranı
// IMHO : İnşaat Mühendisliği Hizmet Oranı
// PYK : Proje Yineleme Katsayısı
// HB : Hizmet Bölümleri
// BK : Bölge Katsayısı
let ysk_puani = 0;
let YA = parseInt(yaInput.value); // Yapı Alanı
let IMHO = 0.75;
//* Highlight the input as red if empty.
for (let index = 0; index < inputs.length; index++) {
const input_element = inputs[index];
if (input_element.value == "empty") {
input_element.classList.add("border-3", "border-danger");
should_calculate = false;
}
else input_element.classList.remove("border-3", "border-danger");
}
if (Number.isNaN(YA) || YA < 0) {
yaInput.classList.add("border-3", "border-danger");
should_calculate = false;
}
else yaInput.classList.remove("border-3", "border-danger");
if (should_calculate) {
//* Calculate BM
let ygInput_0 = ygInput.value[0];
let ygInput_1 = ygInput.value[1];
await fetch("./data/birim-maliyet.json").then((response) => response.json()).then((tablo1_data) => {
BM = tablo1_data[ygInput_0 + ". sinif yapilar"][ygInput_1];
});
//* Calculate YSK
await fetch("./data/yapi-sinifi-puani.json").then((response) => response.json()).then((tablo2_data) => {
ysk_puani += tablo2_data["tasiyici sistem yapisi"][tsInput.value];
});
await fetch("./data/yapi-sinifi-puani.json").then((response) => response.json()).then((tablo2_data) => {
ysk_puani += tablo2_data["temel sistemi"][temelInput.value];
});
await fetch("./data/yapi-sinifi-katsayisi.json").then((response) => response.json()).then((tablo3_data) => {
YSK = tablo3_data[ysk_puani];
});
//* Calculate PUO
await fetch("./data/ucret-oranlari.csv").then((response) => response.text()).then((tablo4_data) => {
tablo4_data = tablo4_data.split("\n");
tablo4_data.shift();
if (YA >= 80000) {
const pricing_row = tablo4_data[tablo4_data.length - 1].split(",");
PUO = parseFloat(pricing_row[parseInt(ygInput.value[0])]) / 100;
}
else {
for (let index = 0; index < tablo4_data.length; index++) {
const tablo4_row = tablo4_data[index].split(",");
if (parseInt(tablo4_row[0]) > YA) {
const pricing_row = tablo4_data[index - 1].split(",");
PUO = parseFloat(pricing_row[parseInt(ygInput.value[0])]) / 100;
break;
}
}
}
});
//* Calculate PYK
await fetch("./data/proje-yineleme-katsayisi.json").then((response) => response.json()).then((tablo5_data) => {
let result = 0;
for (let index = 1; index <= uygulama_sayisi; index++) {
let k;
if (index <= 4) k = index.toString();
else k = ">4";
result += tablo5_data[k];
}
PYK = parseFloat(result.toPrecision(3));
});
//* Calculate HB
await fetch("./data/hizmet-bolumleri.json").then((response) => response.json()).then((tablo6_data) => {
if (document.getElementById("yeni proje").checked) {
// get the checkboxes
let checkboxes = document.querySelectorAll("#yeni_proje_checkbox_div input[type='checkbox']:checked");
let result = 0;
for (let index = 0; index < checkboxes.length; index++) {
const checkbox = checkboxes[index];
result += tablo6_data[checkbox.value];
}
HB = result;
}
else {
let selected_value = document.querySelector('input[name="options"]:checked').value;
HB = tablo6_data[selected_value];
}
});
//* Calculate BK
await fetch("./data/bolge-katsayileri.json").then((response) => response.json()).then((tablo8_data) => {
BK = tablo8_data[msInput.value][pubInput.value];
});
PUO = parseFloat(PUO.toPrecision(4));
let result = YA * BM * YSK * PUO * IMHO * PYK * HB * BK;
// result = parseFloat(result.toPrecision(3));
//* Log the calculation to the console
const debug_log = {
"BM": BM,
"YSK": YSK,
"PUO": PUO,
"PYK": PYK,
"HB": HB,
"BK": BK
};
console.table(debug_log);
let _result = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(result);
console.info("Sonuç", _result);
if(document.getElementById("yeni proje").checked == false) display_result(result);
else display_result(result / HB);
}
}
async function tab2_calculate(){
let YA, BM, HBK, PYK;
// YA : Yapı Alanı
// BM : Birim Maliyet
// HBK : Hizmet Bedeli Katsayısı
// PYK : Proje Yineleme Katsayısı
let should_calculate = true;
let yaInput = document.getElementById("yaInput_tab2");
let ygInput = document.getElementById("ygInput_tab2");
YA = parseInt(yaInput.value);
if (Number.isNaN(YA) || YA < 0 || YA > 100000) {
yaInput.classList.add("border-3", "border-danger");
should_calculate = false;
}
else {
yaInput.classList.remove("border-3", "border-danger");
}
if (ygInput.value == "empty") {
ygInput.classList.add("border-3", "border-danger");
should_calculate = false;
}
else ygInput.classList.remove("border-3", "border-danger");
if(should_calculate){
//* Calculate BM
let ygInput_0 = ygInput.value[0];
let ygInput_1 = ygInput.value[1];
await fetch("./data/birim-maliyet.json").then((response) => response.json()).then((tablo1_data) => {
BM = tablo1_data[ygInput_0 + ". sinif yapilar"][ygInput_1];
});
//* Calculate HBK
await fetch("./data/hizmet-bedeli-cetveli.csv").then((response) => response.text()).then((d) => {
data = d.split("\n");
data.shift();
let existing_YA_values = [];
for (let index = 0; index < data.length; index++) {
const row = data[index].split(",");
existing_YA_values.push(parseInt(row[0]));
}
if (YA <= 600) {
const reference_row = data[0].split(",");
HBK = parseFloat(reference_row[1]) / 1000;
}
else {
if(existing_YA_values.includes(YA)){ // no need for interpolation, data already exists.
for (let index = 0; index < data.length; index++) {
const row = data[index].split(",");
if (parseInt(row[0]) == YA) {
const reference_row = data[index].split(",");
HBK = parseFloat(reference_row[1]) / 1000;
break;
}
}
}
else{ // use linear interpolation
for (let index = 0; index < data.length; index++) {
const row = data[index].split(",");
if (parseInt(row[0]) > YA) {
const reference_row1 = data[index - 1].split(",");
const reference_row2 = data[index].split(",");
HBK = (linear_interpolation(
[parseInt(reference_row1[0]),parseFloat(reference_row1[1])],
[parseInt(reference_row2[0]),parseFloat(reference_row2[1])],
YA
)) / 1000;
break;
}
}
}
}
});
//* Calculate PYK
await fetch("./data/proje-yineleme-katsayisi.json").then((response) => response.json()).then((tablo5_data) => {
let result = 0;
for (let index = 1; index <= uygulama_sayisi_tab2; index++) {
let k;
if (index <= 4) k = index.toString();
else k = ">4";
result += tablo5_data[k];
}
PYK = parseFloat(result.toPrecision(4));
});
let result = YA * BM * HBK * PYK;
let _result = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(result);
// result = parseFloat(result.toPrecision(3));
//* Log the calculation to the console
const debug_log = {
"HBK" : HBK,
"BM": BM,
"PYK": PYK,
};
console.table(debug_log);
console.info(_result);
//* Display the result on UI
document.getElementById("resultDiv_tab2").replaceChildren();
let p = document.createElement("p");
p.innerHTML = "Mesleki Denetim Hizmet Bedeli Ücreti: " + _result;
p.className = "text-center mt-4 fs-5";
document.getElementById("resultDiv_tab2").appendChild(p);
window.scrollTo({
top: document.body.scrollHeight,
behavior: "smooth"
});
}
}
async function display_result(result){
//* If the project type is "yeni proje", a table will be displayed.
//* Otherwise, just a text will be displayed.
//* This is why the if statement is needed.
document.getElementById("resultDiv").replaceChildren();
if(document.getElementById("yeni proje").checked){
document.getElementById("resultDiv").innerHTML+= `
<div class="table-responsive mt-4">
<table class="table">
<thead class="table-light">
<tr>
<th scope="col">Proje</th>
<th scope="col" class="text-end">Ücret</th>
</tr>
</thead>
<tbody>
</tbody>
<tfoot>
<tr>
<th>Toplam Yeni Proje Ücreti</th>
<th class="text-end"></th>
</tr>
</tfoot>
</table>
</div>
`;
let selected_checkboxes = document.querySelectorAll("#yeni_proje_checkbox_div input[type='checkbox']:checked");
let total_price = 0;
await fetch("./data/hizmet-bolumleri.json").then((response) => response.json()).then((tablo6_data) => {
for (let index = 0; index < selected_checkboxes.length; index++) {
const sc = selected_checkboxes[index]; // sc = selected checkbox
let tsv = result * tablo6_data[sc.value]; // tsv = this service's value
let _tsv = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(tsv);
document.querySelector("table tbody").innerHTML += `
<tr>
<td>`+document.querySelector("label[for='"+ sc.id +"']").innerHTML+`</td>
<td class="text-end">`+_tsv+`</td>
</tr>
`;
total_price += tsv;
}
});
let _total_price = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(total_price);
document.querySelector("table tfoot tr th.text-end").innerHTML += _total_price;
}
else{
let _result = new Intl.NumberFormat('tr-TR', { style: 'currency', currency: 'TRY' }).format(result);
const sr = document.querySelector('input[name="options"]:checked').id; // sr = selected radiobutton
const label = document.querySelector("label[for='"+sr+"']").innerHTML;
let p = document.createElement("p");
p.innerHTML = label + " Ücreti: " + _result;
p.className = "text-center mt-4 fs-5";
document.getElementById("resultDiv").appendChild(p);
}
window.scrollTo({
top: document.body.scrollHeight,
behavior: "smooth"
});
}
/** if a checkbox is selected,
* automatically deselect all the other radio buttons except "Yeni Proje". */
function select_yeni_proje_radiobutton() {
if (document.querySelector("input[type='radio']").checked) return;
for (let index = 0; index < document.querySelectorAll("input[type='radio']").length; index++) {
const radio_element = document.querySelectorAll("input[type='radio']")[index];
radio_element.checked = false;
}
document.querySelector("input[type='radio']").checked = true;
}
/** if a radio button other than "Yeni Proje" is selected,
* all the checkboxes under "Yeni Proje" should be deselected
*/
function deselect_checkboxes() {
for (let index = 0; index < document.querySelectorAll("input[type='checkbox']").length; index++) {
const radio_element = document.querySelectorAll("input[type='checkbox']")[index];
radio_element.checked = false;
}
}
/**
* point1 = [x1, y1]
*
* point2 = [x2, y2]
*
* x = the x value between x1 and x2
*/
function linear_interpolation(point1, point2, x){
let diff_x = point1[0] - point2[0];
let diff_y = point1[1] - point2[1];
return point1[1] + ((x - point1[0]) * diff_y / diff_x);
}