-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders.php
350 lines (294 loc) · 14.9 KB
/
orders.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
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
<?php $title = 'Manage Orders'; ?>
<?php
include_once 'includes/includes.php';
$session = new Session();
$functions = new Functions();
if($session->isUserLoggedIn() == false){
$functions->redirect('index.php');
}
?>
<style>
.card{
width:100% !important;
}
</style>
</head>
<body>
<?php include 'includes/layout/topBar.php' ?>
<div class="row" style="margin-top:30px">
<div class="col-md-12 col-lg-2">
<?php include 'includes/layout/sidebar.php' ?>
</div>
<div class="col-md-10 main-content">
<div class="ui breadcrumb">
<a class="section">Home</a>
<div class="divider"> / </div>
<a class="section">Orders</a>
<div class="divider"> / </div>
<div class="active section">Manage Orders</div>
</div>
<div></div>
<?php
$path = basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);
$permissions = $functions->get_user_permission($path);
if(in_array('create',$permissions)):
?>
<a href="add_order.php">
<div class="ui small primary labeled icon button">
<i class="add icon"></i> Add new order
</div>
</a>
<?php endif ?>
<?php
$from = date('Y-m-d');
$to = date('Y-m-d');
$user_name = ($_SESSION['user_data']['user_type'] == 'admin') ? "": $_SESSION['user_data']['user_name'];
$order_id = '';
if(isset($_POST['from']) && isset($_POST['to']) && isset($_POST['user_name']) && isset($_POST['order_id'])){
$from = $_POST['from'];
$to = $_POST['to'];
$user_name = $_POST['user_name'];
$order_id = $_POST['order_id'];
}
?>
<form action="?" method="post" style="margin-top:20px" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-6 col-md-2">
<div class="ui input focus">
<input type="date" value="<?php echo $from ?>" name="from" placeholder="date">
</div>
</div>
<div class="col-sm-6 col-md-2">
<div class="ui input focus">
<input type="date" value="<?php echo $to ?>" name="to" placeholder="date">
</div>
</div>
<?php if($_SESSION['user_data']['user_type'] == 'admin' || !in_array('create',$permissions)):?>
<div class="col-sm-12 col-md-2">
<div class="form-group">
<div class="ui input">
<select class="js-example-basic-single" name="user_name" style="width:100%" onchange="get_value(event)">
<?php
$sql = "select * from users";
$results = $functions->fetch_rows($functions->query($sql));
array_push($results,array("user_name"=>"all"));
foreach ($results as $result) {
if($user_name == ''){
$user_name = 'all';
}
$selected = '';
if($result['user_name'] == $user_name){
$selected = 'selected';
}else{
$selected = '';
}
echo '<option value="'.$result['user_name'].'" '.$selected.'>'.$result['user_name'].'</option>';
}
?>
</select>
</div>
</div>
</div>
<?php else:?>
<input type="hidden" name="user_name" value="<?php echo $_SESSION['user_data']['user_name'] ?>"/>
<?php endif?>
<div class="col-sm-12 col-md-2">
<div class="ui input focus">
<input type="text" value="<?php echo $order_id; ?>" style="height:45px" name="order_id" placeholder="Order ID"/>
</div>
</div>
<div class="col-sm-12 col-md-2">
<div class="ui input focus">
<button type="submit" style="height:43px" class="ui small primary labeled icon button">
<i class="search icon"></i> Search
</button>
</div>
</div>
<div class="col-sm-12 col-md-2">
<a href="orders_statement.php?from=<?php echo $from ?>&to=<?php echo $to?>&user_name=<?php echo $user_name ?>&order_id=<?php echo $order_id?>">
<button type="button" style="height:43px" class="ui small primary labeled icon button">
<i class="file icon"></i> Statement
</button>
</a>
</div>
</div>
</form>
<?php
if(isset($_SESSION['success']) && !empty($_SESSION['success'])){
echo $functions->message($_SESSION['success'],'green');
unset($_SESSION['success']);
}
if(isset($_SESSION['error']) && !empty($_SESSION['error'])){
echo $functions->message($_SESSION['error'],'red');
unset($_SESSION['error']);
}
?>
<div class="ui card">
<div class="content">
<div class="header">Products</div>
</div>
<div class="content">
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Order ID</th>
<th>Customer Name</th>
<th>Customer Number</th>
<th>Total Items</th>
<th>Total Amount</th>
<th>Payment Status</th>
<th>Sold by</th>
<th>Date Time</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$from .=' 00:00:00';
$to .=' 23:59:59';
if($user_name == 'all'){
$user_name = '';
}
if(strpos($order_id,'OD-') > -1){
$order_id = trim($order_id);
$order_id = sprintf("%01s", str_replace('OD-','',$order_id) + 1);
$order_id = $order_id - 1;
}
// if(!in_array('create',$permissions) && ){
// $user_name = '';
// }
if($order_id != ''){
$sql = "select *, sum(cart.cart_p_QTY) as cart_p_QTY, sum(cart.cart_p_AMOUNT) as cart_p_AMOUNT from orders left join cart on orders.order_id = cart.order_id where orders.order_id = '$order_id'";
}else if($user_name != ''){
$sql = "select *, sum(cart.cart_p_QTY) as cart_p_QTY, sum(cart.cart_p_AMOUNT) as cart_p_AMOUNT from orders left join cart on orders.order_id = cart.order_id where orders.date_time between '$from' and '$to' and orders.sold_by = '$user_name' group by orders.order_id order by orders.order_id desc";
}else{
$sql = "select *, sum(cart.cart_p_QTY) as cart_p_QTY, sum(cart.cart_p_AMOUNT) as cart_p_AMOUNT from orders left join cart on orders.order_id = cart.order_id where orders.date_time between '$from' and '$to' group by orders.order_id order by orders.order_id desc";
}
$results = $functions->fetch_rows($functions->query($sql));
foreach ($results as $result) {
$or_order_id = $invID = 'OD-'.str_pad($result['order_id'], 5, '0', STR_PAD_LEFT);
$payment_status = '<a class="ui green label">
Paid
</a>';
$returned_times = '';
if(strtoupper($result['or_payment_status']) == 'UNPAID'){
$payment_status = '<a class="ui red label">
Un paid
</a>';
}
$sql = "select * from cart where order_id = '".$result['order_id']."' and status = 'returned'";
$fetch = $functions->fetch_rows($functions->query($sql));
if(sizeof($fetch) > 0){
$returned_times = ' <button data-tooltip="Somre products where returned" data-position="top right" class="ui icon button"><i style="color:red" class="redo icon"></i></button>';
$returned_items_qty = 0;
$price_of_returned_items = 0;
foreach ($fetch as $item) {
$returned_items_qty = $returned_items_qty + $item['cart_p_QTY'];
$price_of_returned_items = $price_of_returned_items + $item['cart_p_AMOUNT'];
}
$result['or_net_amount'] = $result['cart_p_AMOUNT'] + $result['or_vat'] - $result['or_discount'] - $price_of_returned_items;
$result['cart_p_QTY'] = $result['cart_p_QTY'] - $returned_items_qty;
}
$actions = '';
$msg = 'Print receipt';
if($result['or_amount_received'] < 1){
$msg = 'Print Invoice';
}
if($result['cart_p_QTY'] > 0){
$actions .='<a target="_blank" href="includes/testprint.php?order_id='.$result['order_id'].'"><button data-tooltip="'.$msg.'" data-position="top right" class="ui icon button"><i class="print icon"></i></button></a>';
}
if(in_array('update',$permissions)){
$actions .= '
<a href="edit_order.php?order='.$result['order_id'].'"><button data-tooltip="Edit product" data-position="top right" class="ui icon button"><i class="edit icon"></i></button></a>
'.$returned_times.'';
}
echo '<tr>
<td>'.$or_order_id.'</td>
<td>'.$result['or_customer_name'].'</td>
<td>'.$result['or_customer_phone'].'</td>
<td>'.$result['cart_p_QTY'].'</td>
<td>¢ '.sprintf('%0.2f',$result['or_net_amount']).'</td>
<td>'.$payment_status.'</td>
<td>'.$result['sold_by'].'</td>
<td>'.gmdate('d M Y h:i A',strtotime($result['date_time'])).'</td>
<td>
'.$actions.'
</td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-7"></div>
<div class="col-sm-12 col-md-5">
<table class="ui celled structured table">
<thead>
<tr><th>Payment Status</th>
<th>Qty</th>
<th>Total Amount</th>
</tr></thead>
<tbody>
<?php
$total_qty = 0;
$total_price = 0;
$paid_qty = 0;
$unpaid_qty = 0;
$paid_net_amount = 0;
$unpaid_net_amount = 0;
if($order_id != ''){
$sql = "select *, sum(or_net_amount) as or_net_amount from orders where orders.order_id = '$order_id' group by or_payment_status";
}else if($user_name != ''){
$sql = "select *, sum(or_net_amount) as or_net_amount from orders where orders.date_time between '$from' and '$to' and orders.sold_by = '$user_name' group by or_payment_status";
}else{
$sql = "select *, sum(or_net_amount) as or_net_amount from orders where orders.date_time between '$from' and '$to' group by or_payment_status";
}
$results = $functions->fetch_rows($functions->query($sql));
foreach ($results as $result) {
if($order_id != ''){
$sql = "select * from orders where or_payment_status like '".$result['or_payment_status']."' and order_id = '$order_id'";
}else if($user_name != ''){
$sql = "select * from orders where or_payment_status like '".$result['or_payment_status']."' and orders.date_time between '$from' and '$to' and sold_by = '$user_name'";
}else{
$sql = "select * from orders where or_payment_status like '".$result['or_payment_status']."' and orders.date_time between '$from' and '$to'";
}
$orders = $functions->fetch_rows($functions->query($sql));
foreach ($orders as $order) {
$sql = "select *,sum(cart.cart_p_QTY) as cart_p_QTY from cart where order_id = '".$order['order_id']."' and status = 'active' ";
$fetch = $functions->fetch_rows($functions->query($sql));
$qty = $fetch[0]['cart_p_QTY'];
if($result['or_payment_status'] == 'Paid'){
$paid_qty = $qty + $paid_qty;
$paid_net_amount = $result['or_net_amount'];
}else{
$unpaid_qty = $qty + $unpaid_qty;
$unpaid_net_amount = $result['or_net_amount'];
}
$total_qty = $total_qty + $qty;
}
$total_price = $result['or_net_amount'] + $total_price;
}
?>
<tr>
<td data-label="Name">Paid</td>
<td data-label="Age"><?php echo $paid_qty ?></td>
<td data-label="Job"><?php echo '¢ '.sprintf('%0.2f',$paid_net_amount) ?></td>
</tr>
<tr>
<td data-label="Name">UnPaid</td>
<td data-label="Age"><?php echo $unpaid_qty ?></td>
<td data-label="Job"><?php echo '¢ '.sprintf('%0.2f',$unpaid_net_amount) ?></td>
</tr>
</tbody>
<thead>
<tr><th>Total</th>
<th><?php echo $total_qty ?></th>
<th><?php echo '¢'.sprintf('%0.2f',$total_price);?></th>
</tr></thead>
</table>
</div>
</div>
</div>
<?php include_once 'includes/layout/footer.php'?>