forked from itflow-org/itflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclients.php
More file actions
342 lines (310 loc) · 14.5 KB
/
Copy pathclients.php
File metadata and controls
342 lines (310 loc) · 14.5 KB
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
<?php include("header.php");
// Role / Client Access Permission Check
if($session_user_role == 2){
$permission_sql = "AND client_id IN ($session_user_client_access)";
}else{
$permission_sql = "";
}
//Paging
if(isset($_GET['p'])){
$p = intval($_GET['p']);
$record_from = (($p)-1)*$_SESSION['records_per_page'];
$record_to = $_SESSION['records_per_page'];
}else{
$record_from = 0;
$record_to = $_SESSION['records_per_page'];
$p = 1;
}
//Custom Query Filter
if(isset($_GET['query'])){
$query = mysqli_real_escape_string($mysqli,$_GET['query']);
}else{
$query = "";
}
//Column Filter
if(!empty($_GET['sortby'])){
$sortby = mysqli_real_escape_string($mysqli,$_GET['sortby']);
}else{
$sortby = "client_accessed_at";
}
//Column Order Filter
if(isset($_GET['order'])){
if($_GET['order'] == 'ASC'){
$order = "ASC";
$order_display = "DESC";
}else{
$order = "DESC";
$order_display = "ASC";
}
}else{
$order = "DESC";
$order_display = "ASC";
}
if (empty($_GET['canned_date'])) {
//Prevents lots of undefined variable errors.
// $dtf and $dtt will be set by the below else to 0000-00-00 / 9999-00-00
$_GET['canned_date'] = 'custom';
}
//Date Filter
if($_GET['canned_date'] == "custom" AND !empty($_GET['date_from'])){
$date_from = mysqli_real_escape_string($mysqli,$_GET['date_from']);
$date_to = mysqli_real_escape_string($mysqli,$_GET['date_to']);
}elseif($_GET['canned_date'] == "today"){
$date_from = date('Y-m-d');
$date_to = date('Y-m-d');
}elseif($_GET['canned_date'] == "yesterday"){
$date_from = date('Y-m-d',strtotime("yesterday"));
$date_to = date('Y-m-d',strtotime("yesterday"));
}elseif($_GET['canned_date'] == "thisweek"){
$date_from = date('Y-m-d',strtotime("monday this week"));
$date_to = date('Y-m-d');
}elseif($_GET['canned_date'] == "lastweek"){
$date_from = date('Y-m-d',strtotime("monday last week"));
$date_to = date('Y-m-d',strtotime("sunday last week"));
}elseif($_GET['canned_date'] == "thismonth"){
$date_from = date('Y-m-01');
$date_to = date('Y-m-d');
}elseif($_GET['canned_date'] == "lastmonth"){
$date_from = date('Y-m-d',strtotime("first day of last month"));
$date_to = date('Y-m-d',strtotime("last day of last month"));
}elseif($_GET['canned_date'] == "thisyear"){
$date_from = date('Y-01-01');
$date_to = date('Y-m-d');
}elseif($_GET['canned_date'] == "lastyear"){
$date_from = date('Y-m-d',strtotime("first day of january last year"));
$date_to = date('Y-m-d',strtotime("last day of december last year"));
}else{
$date_from = "0000-00-00";
$date_to = "9999-00-00";
}
//Rebuild URL
$url_query_strings_sortby = http_build_query(array_merge($_GET,array('sortby' => $sortby, 'order' => $order)));
$sql = mysqli_query($mysqli,"SELECT SQL_CALC_FOUND_ROWS * FROM clients
LEFT JOIN contacts ON clients.primary_contact = contacts.contact_id AND contact_archived_at IS NULL
LEFT JOIN locations ON clients.primary_location = locations.location_id AND location_archived_at IS NULL
WHERE (client_name LIKE '%$query%' OR client_type LIKE '%$query%' OR contact_email LIKE '%$query%' OR contact_name LIKE '%$query%' OR contact_phone LIKE '%$query%'
OR contact_mobile LIKE '%$query%' OR location_address LIKE '%$query%' OR location_city LIKE '%$query%' OR location_state LIKE '%$query%' OR location_zip LIKE '%$query%')
AND DATE(client_created_at) BETWEEN '$date_from' AND '$date_to'
AND clients.company_id = $session_company_id $permission_sql
ORDER BY $sortby $order LIMIT $record_from, $record_to"
);
$num_rows = mysqli_fetch_row(mysqli_query($mysqli,"SELECT FOUND_ROWS()"));
?>
<div class="card card-dark">
<div class="card-header py-2">
<h3 class="card-title mt-2"><i class="fa fa-fw fa-users"></i> Clients</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addClientModal"><i class="fas fa-fw fa-plus"></i> New Client</button>
</div>
</div>
<div class="card-body">
<form class="mb-4" autocomplete="off">
<div class="row">
<div class="col-sm-4">
<div class="input-group">
<input type="search" class="form-control" name="query" value="<?php if(isset($query)){echo stripslashes($query);} ?>" placeholder="Search Clients" autofocus>
<div class="input-group-append">
<button class="btn btn-secondary" type="button" data-toggle="collapse" data-target="#advancedFilter"><i class="fas fa-filter"></i></button>
<button class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</div>
<div class="collapse mt-3 <?php if(!empty($_GET['date_from'])){ echo "show"; } ?>" id="advancedFilter">
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label>Canned Date</label>
<select class="form-control select2" name="canned_date">
<option <?php if($_GET['canned_date'] == "custom"){ echo "selected"; } ?> value="custom">Custom</option>
<option <?php if($_GET['canned_date'] == "today"){ echo "selected"; } ?> value="today">Today</option>
<option <?php if($_GET['canned_date'] == "yesterday"){ echo "selected"; } ?> value="yesterday">Yesterday</option>
<option <?php if($_GET['canned_date'] == "thisweek"){ echo "selected"; } ?> value="thisweek">This Week</option>
<option <?php if($_GET['canned_date'] == "lastweek"){ echo "selected"; } ?> value="lastweek">Last Week</option>
<option <?php if($_GET['canned_date'] == "thismonth"){ echo "selected"; } ?> value="thismonth">This Month</option>
<option <?php if($_GET['canned_date'] == "lastmonth"){ echo "selected"; } ?> value="lastmonth">Last Month</option>
<option <?php if($_GET['canned_date'] == "thisyear"){ echo "selected"; } ?> value="thisyear">This Year</option>
<option <?php if($_GET['canned_date'] == "lastyear"){ echo "selected"; } ?> value="lastyear">Last Year</option>
</select>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Date From</label>
<input type="date" class="form-control" name="date_from" value="<?php echo $date_from; ?>">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Date To</label>
<input type="date" class="form-control" name="date_to" value="<?php echo $date_to; ?>">
</div>
</div>
</div>
</div>
</form>
<hr>
<div class="table-responsive">
<table class="table table-striped table-hover table-borderless">
<thead class="<?php if($num_rows[0] == 0){ echo "d-none"; } ?>">
<tr>
<th><a class="text-dark" href="?<?php echo $url_query_strings_sortby; ?>&sortby=client_name&order=<?php echo $order_display; ?>">Name</a></th>
<th><a class="text-dark" href="?<?php echo $url_query_strings_sortby; ?>&sortby=location_city&order=<?php echo $order_display; ?>">Address </a></th>
<th><a class="text-dark" href="?<?php echo $url_query_strings_sortby; ?>&sortby=contact_name&order=<?php echo $order_display; ?>">Contact</a></th>
<th class="text-right">Billing</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($sql)){
$client_id = $row['client_id'];
$client_name = $row['client_name'];
$client_type = $row['client_type'];
$location_id = $row['location_id'];
$location_country = $row['location_country'];
$location_address = $row['location_address'];
$location_city = $row['location_city'];
$location_state = $row['location_state'];
$location_zip = $row['location_zip'];
if(empty($location_address) AND empty($location_city) AND empty($location_state) AND empty($location_zip)){
$location_address_display = "-";
}else{
$location_address_display = "$location_address<br>$location_city $location_state $location_zip";
}
$contact_id = $row['contact_id'];
$contact_name = $row['contact_name'];
$contact_title = $row['contact_title'];
$contact_phone = formatPhoneNumber($row['contact_phone']);
$contact_extension = $row['contact_extension'];
$contact_mobile = formatPhoneNumber($row['contact_mobile']);
$contact_email = $row['contact_email'];
$client_website = $row['client_website'];
$client_currency_code = $row['client_currency_code'];
$client_net_terms = $row['client_net_terms'];
$client_referral = $row['client_referral'];
$client_notes = $row['client_notes'];
$client_created_at = $row['client_created_at'];
$client_updated_at = $row['client_updated_at'];
//Client Tags
$client_tag_name_display_array = array();
$client_tag_id_array = array();
$sql_client_tags = mysqli_query($mysqli,"SELECT * FROM client_tags LEFT JOIN tags ON client_tags.tag_id = tags.tag_id WHERE client_tags.client_id = $client_id");
while($row = mysqli_fetch_array($sql_client_tags)){
$client_tag_id = $row['tag_id'];
$client_tag_name = $row['tag_name'];
$client_tag_color = $row['tag_color'];
$client_tag_icon = $row['tag_icon'];
if(empty($client_tag_icon)){
$client_tag_icon = "tag";
}
$client_tag_id_array[] = $client_tag_id;
$client_tag_name_display_array[] = "<span class='badge bg-$client_tag_color'><i class='fa fa-fw fa-$client_tag_icon'></i> $client_tag_name</span>";
}
$client_tags_display = implode('', $client_tag_name_display_array);
//Add up all the payments for the invoice and get the total amount paid to the invoice
$sql_invoice_amounts = mysqli_query($mysqli,"SELECT SUM(invoice_amount) AS invoice_amounts FROM invoices WHERE invoice_client_id = $client_id AND invoice_status NOT LIKE 'Draft' AND invoice_status NOT LIKE 'Cancelled' ");
$row = mysqli_fetch_array($sql_invoice_amounts);
$invoice_amounts = $row['invoice_amounts'];
$sql_amount_paid = mysqli_query($mysqli,"SELECT SUM(payment_amount) AS amount_paid FROM payments, invoices WHERE payment_invoice_id = invoice_id AND invoice_client_id = $client_id");
$row = mysqli_fetch_array($sql_amount_paid);
$amount_paid = $row['amount_paid'];
$balance = $invoice_amounts - $amount_paid;
//set Text color on balance
if($balance > 0){
$balance_text_color = "text-danger font-weight-bold";
}else{
$balance_text_color = "";
}
?>
<tr>
<td>
<strong><a href="client.php?client_id=<?php echo $client_id; ?>&tab=contacts"><?php echo $client_name; ?></a></strong>
<?php
if(!empty($client_type)){
?>
<br>
<small class="text-secondary"><?php echo $client_type; ?></small>
<?php } ?>
<?php
if(!empty($client_tags_display)){
?>
<br>
<?php echo $client_tags_display; ?>
<?php } ?>
<br>
<small class="text-secondary"><b>Added:</b> <?php echo $client_created_at; ?></small>
</td>
<td><?php echo $location_address_display; ?></td>
<td>
<?php
if(empty($contact_name) AND empty($contact_phone) AND empty($contact_mobile) AND empty($client_email)){
echo "-";
}
?>
<?php
if(!empty($contact_name)){
?>
<i class="fa fa-fw fa-user text-secondary mr-2 mb-2"></i><?php echo $contact_name; ?>
<br>
<?php
}else{
echo $contact_name;
}
?>
<?php
if(!empty($contact_phone)){
?>
<i class="fa fa-fw fa-phone text-secondary mr-2 mb-2"></i><?php echo $contact_phone; ?> <?php if(!empty($contact_extension)){ echo "x$contact_extension"; } ?>
<br>
<?php
}
?>
<?php
if(!empty($contact_mobile)){
?>
<i class="fa fa-fw fa-mobile-alt text-secondary mr-2 mb-2"></i><?php echo $contact_mobile; ?>
<br>
<?php
}
?>
<?php
if(!empty($contact_email)){
?>
<i class="fa fa-fw fa-envelope text-secondary mr-2 mb-2"></i><a href="mailto:<?php echo $contact_email; ?>"><?php echo $contact_email; ?></a>
<?php
}
?>
</td>
<td class="text-right">
Balance: <span class="<?php echo $balance_text_color; ?>"><?php echo get_currency_symbol($session_company_currency); ?> <?php echo number_format($balance,2); ?></span>
<br>
Paid: <?php echo get_currency_symbol($session_company_currency); ?> <?php echo number_format($amount_paid,2); ?>
</td>
<td>
<div class="dropdown dropleft text-center">
<button class="btn btn-secondary btn-sm" type="button" data-toggle="dropdown">
<i class="fas fa-ellipsis-h"></i>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#" data-toggle="modal" data-target="#editClientModal<?php echo $client_id; ?>">Edit</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-danger" href="#" data-toggle="modal" data-target="#deleteClientModal<?php echo $client_id; ?>">Delete</a>
</div>
</div>
</td>
</tr>
<?php
include("edit_client_modal.php");
include("delete_client_modal.php");
}
?>
</tbody>
</table>
</div>
<?php include("pagination.php"); ?>
</div>
</div>
<?php include("add_client_modal.php"); ?>
<?php include("add_quick_modal.php"); ?>
<?php include("footer.php");