-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
241 lines (215 loc) · 8.58 KB
/
index.php
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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link href="assets/fontawesome/css/all.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<title>Company Storefront</title>
</head>
<body>
<!-- Navbar -->
<nav class="navbar navbar-dark bg-dark">
<a class="navbar-brand py-4" href="#">
Storefront Billing Form
</a>
</nav>
<div class="container-fluid py-4">
<div class="form-group row">
<div class="col-md-4">
<label class="mb-0" for="description">Description</label>
</div>
<div class="col-md-2">
<label class="mb-0" for="cost">Cost</label>
</div>
<div class="col-md-2">
<label class="mb-0" for="cost">Quantity</label>
</div>
<div class="col-md-1">
<label class="mb-0" for="cost">VAT(%)</label>
</div>
<div class="col-md-2">
<label class="mb-0" for="cost">Total</label>
</div>
<div class="col-md-1 text-center">
<label class="mb-0" for="cost">Actions</label>
</div>
</div>
<div class="form-group row mb-2 item-fields" id="template">
<div class="col-md-4">
<input type="email" class="form-control" name="description">
</div>
<div class="col-md-2">
<input type="number" class="form-control cost" name="cost[]" value="0">
</div>
<div class="col-md-2">
<input type="number" class="form-control quantity" name="quantity[]" value="0">
</div>
<div class="col-md-1">
<input type="number" class="form-control vat" name="vat[]" value="0">
</div>
<div class="col-md-2 d-flex align-items-center text-bold">
<div>$<span class="total">0</span></div>
</div>
<div class="col-md-1 d-flex align-items-center justify-content-center text-center text-danger">
<a class="deleteItemButton"><i class="fa fa-trash"></i></a>
</div>
</div>
<div class="form-group row mb-4 pb-4">
<div class="col-md-12">
<button class="btn btn-secondary btn-sm" id="addItemButton">Add item</button>
</div>
</div>
<hr>
<div class="row d-flex justify-content-end">
<div class="col-md-5 pr-4 mr-4" id="summaries">
<div class="d-flex justify-content-between" id="subtotals-elements">
<div>
Subtotal
</div>
<div>
$<span id="subtotal">0</span>
</div>
</div>
<div class="d-flex justify-content-between">
<div>
VAT Total
</div>
<div>
$<span id="vat-total">0</span>
</div>
</div>
<hr>
<div class="d-flex justify-content-between align-items-center">
<div>
<h2>Total</h2>
</div>
<div >
<h2>$<span id="total">0</span></h2>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(document).on("click", "#addItemButton", function(e){
console.log("Add item fields");
var control = $("#template");
var clone = $(control).clone();
$(clone).find("[name='cost[]']").val("0");
$(clone).find("[name='quantity[]']").val("0");
$(clone).find("[name='vat[]']").val("0");
$(clone).find(".total").html("0");
$(clone).removeAttr('id').insertBefore(this);
});
$(document).on("click", ".deleteItemButton", function(e){
e.preventDefault();
if($(this).closest('.form-group').attr('id') != "template"){
console.log("Delete item fields");
$(this).closest('.form-group').remove();
}
updateTotal();
});
$(document).on("keyup", "[name='cost[]']", function(e){
var row = $(this).closest('.form-group');
updateRowTotal(row);
updateTotal();
});
$(document).on("change", "[name='cost[]']", function(e){
var row = $(this).closest('.form-group');
updateRowTotal(row);
updateTotal();
});
$(document).on("keyup", "[name='quantity[]']", function(e){
var row = $(this).closest('.form-group');
updateRowTotal(row);
updateTotal();
});
$(document).on("change", "[name='quantity[]']", function(e){
var row = $(this).closest('.form-group');
updateRowTotal(row);
updateTotal();
});
$(document).on("keyup", "[name='vat[]']", function(e){
var row = $(this).closest('.form-group');
updateRowTotal(row);
updateTotal();
});
$(document).on("change", "[name='vat[]']", function(e){
var row = $(this).closest('.form-group');
updateRowTotal(row);
updateTotal();
});
});
function updateRowTotal(row){
console.log("Updating row total");
var cost = $(row).find("[name='cost[]']").val();
var quantity = $(row).find("[name='quantity[]']").val();
var total = cost * quantity;
$(row).find(".total").html(total);
}
function updateTotal(){
$(".vat-breakdowns").remove();
var total = 0;
var subtotal = 0;
var vat_total = 0;
var row_total = 0;
var row_vat = 0;
var vat_html = "";
$(".item-fields").each(function(index, item) {
row_total = parseInt($(this).find('.total').text());
row_vat = parseInt($(this).find("[name='vat[]']").val());
row_vat_amount = row_vat/100 * row_total;
vat_total += row_vat_amount;
subtotal += row_total;
if(row_vat != 0 && !isNaN(row_vat)){
// if($("#summaries").find("[data-vat='"+row_vat+"']").length != 0){
// console.log("found");
// console.log($("#summaries").find("[data-vat='"+row_vat+"']").text());
// }
// else{
// console.log("nomaaa");
// console.log($("#summaries").find("[data-vat='"+row_vat+"']").text());
// }
if($("[value='"+row_vat+"']").length != 0){
console.log("found");
// console.log($("[data-vat='"+row_vat+"']").length);
}
else{
console.log("nomaaa");
// console.log($("[data-vat='"+row_vat+"']").length);
}
vat_html +=
`
<div class="d-flex justify-content-between vat-breakdowns" data-vat="${row_vat}">
<div>
VAT (${row_vat}%)
</div>
<div>
$<span id="vat-total">${row_vat_amount}</span>
</div>
</div>
`;
}
else{
row_vat = 0;
}
});
total = subtotal + vat_total;
$("#vat-total").html(vat_total);
$("#subtotal").html(subtotal);
$("#total").html(total);
$(vat_html).insertAfter("#subtotals-elements");
}
</script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>
</html>