/
invoices.php
723 lines (601 loc) · 19.4 KB
/
invoices.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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
<?php
require_once("crud.php");
class InvoiceCrud extends Crud {
/* Post header event. */
public function postHeaderEvent() {
createConfirmDialog("confirmRemoveDialog", "Confirm removal ?", "confirmRemoval");
createDocumentLink();
}
public function afterInsertRow() {
?>
var status = rowData['status'];
if (status == "1") {
$(this).jqGrid('setRowData', rowid, false, { color: '#0000FF' });
}
<?php
}
public function postUpdateEvent($invoiceid) {
$items = json_decode($_POST['item_serial'], true);
$memberid = getLoggedOnMemberID();
$qry = "DELETE FROM {$_SESSION['DB_PREFIX']}invoiceitem
WHERE invoiceid = $invoiceid";
$result = mysql_query($qry);
if (! $result) {
logError($qry . " - " . mysql_error());
}
foreach ($items as $k=>$item) {
$qty = $item['quantity'];
$vatrate = $item['vatrate'];
$linetotal = $item['linetotal'];
$vat = $item['vat'];
$unitprice = $item['priceeach'];
$productid = $item['productid'];
$sequence = $item['sequence'];
$qry = "INSERT INTO {$_SESSION['DB_PREFIX']}invoiceitem
(invoiceid, sequence, quantity, priceeach, vatrate, vat, linetotal,
productid, metacreateddate, metacreateduserid, metamodifieddate, metamodifieduserid)
VALUES
($invoiceid, $sequence, '$qty', '$unitprice', $vatrate, '$vat', $linetotal,
'$productid', NOW(), $memberid , NOW(), $memberid)";
$result = mysql_query($qry);
if (! $result) {
logError($qry . " - " . mysql_error());
}
}
$sql = "UPDATE {$_SESSION['DB_PREFIX']}invoice SET
status = 0,
converteddatetime = null,
metacreateddate = NOW()
WHERE id = $invoiceid";
$result = mysql_query($sql);
if (! $result) {
logError($sql . " - " . mysql_error());
}
}
public function postInsertEvent() {
$invoiceid = mysql_insert_id();
$items = json_decode($_POST['item_serial'], true);
$memberid = getLoggedOnMemberID();
foreach ($items as $k=>$item) {
$qty = $item['quantity'];
$vatrate = $item['vatrate'];
$linetotal = $item['linetotal'];
$vat = $item['vat'];
$unitprice = $item['priceeach'];
$productid = $item['productid'];
$sequence = $item['sequence'];
$qry = "INSERT INTO {$_SESSION['DB_PREFIX']}invoiceitem
(invoiceid, sequence, quantity, priceeach, vatrate, vat, linetotal,
productid, metacreateddate, metacreateduserid, metamodifieddate, metamodifieduserid)
VALUES
($invoiceid, $sequence, '$qty', '$unitprice', $vatrate, '$vat', $linetotal,
'$productid', NOW(), $memberid , NOW(), $memberid)";
logError("SQL:$qry", false);
$result = mysql_query($qry);
if (! $result) {
logError($qry . " - " . mysql_error());
}
}
}
public function postAddScriptEvent() {
?>
$("#customerid").val("").trigger("change");
$("#clientid").val("").trigger("change");
$("#crudaddbutton").show();
$("#revision").val("1");
$("#deliverycharge").val("0.00");
$("#discount").val("0.00");
$("#total").val("0.00");
$("#invoicedate").val("<?php echo date("d/m/Y"); ?>");
$("#takenbyid").val("<?php echo getLoggedOnMemberID(); ?>");
$("#invoiceitemdialog input, #invoiceitemdialog select").removeAttr("disabled");
itemArray = [];
populateTable();
<?php
}
public function postEditScriptEvent() {
$this->showInvoice(false);
?>
$("#invoiceitemdialog input, #invoiceitemdialog select").removeAttr("disabled");
$("#crudaddbutton").show();
<?php
}
public function postViewScriptEvent() {
$this->showInvoice(true);
?>
$("#invoiceitemdialog input, #invoiceitemdialog select").attr("disabled", true);
$("#crudaddbutton").hide();
<?php
}
public function showInvoice($readonly) {
?>
$("#revision").val(parseInt($("#revision").val()) + 1);
showHeader();
callAjax(
"finddata.php",
{
sql: "SELECT A.*, B.description FROM <?php echo $_SESSION['DB_PREFIX'];?>invoiceitem A LEFT OUTER JOIN <?php echo $_SESSION['DB_PREFIX'];?>product B ON B.id = A.productid WHERE A.invoiceid = " + currentCrudID + " ORDER BY A.sequence"
},
function(data) {
itemArray = data;
populateTable(data);
},
false
);
<?php
}
public function editScreenSetup() {
include("invoiceform.php");
}
public function postScriptEvent() {
?>
var currentID = 0;
var currentItem = -1;
var itemArray = [];
function showHeader() {
callAjax(
"finddata.php",
{
sql: "SELECT A.*, B.id AS clientid, B.customerid, B.invoiceaddress1, B.invoiceaddress2, " +
"B.invoiceaddress3, B.invoicecity, B.invoicepostcode " +
"FROM <?php echo $_SESSION['DB_PREFIX'];?>customerclientsite A " +
"INNER JOIN <?php echo $_SESSION['DB_PREFIX'];?>customerclient B " +
"ON B.id = A.clientid " +
"WHERE A.id = " + $("#siteid").val()
},
function(data) {
if (data.length > 0) {
var node = data[0];
var invoiceaddress = "";
var deliveryaddress = "";
if (node.deliveryaddress1 != "") deliveryaddress += node.deliveryaddress1+ "\n";
if (node.deliveryaddress2!= "") deliveryaddress += node.deliveryaddress2+ "\n";
if (node.deliveryaddress3!= "") deliveryaddress += node.deliveryaddress3+ "\n";
if (node.deliverycity!= "") deliveryaddress += node.deliverycity+ "\n";
if (node.deliverypostcode!= "") deliveryaddress += node.deliverypostcode+ "\n";
if (node.invoiceaddress1!= "") invoiceaddress += node.invoiceaddress1+ "\n";
if (node.invoiceaddress2!= "") invoiceaddress += node.invoiceaddress2+ "\n";
if (node.invoiceaddress3!= "") invoiceaddress += node.invoiceaddress3+ "\n";
if (node.invoicecity!= "") invoiceaddress += node.invoicecity+ "\n";
if (node.invoicepostcode!= "") invoiceaddress += node.invoicepostcode+ "\n";
if (deliveryaddress == "") {
deliveryaddress = invoiceaddress;
}
$("#invoiceaddress").val(invoiceaddress);
$("#deliveryaddress").val(deliveryaddress);
$("#customerid").val(node.customerid);
$("#clientid").val(node.clientid);
}
},
false
);
}
function customerid_onchange() {
$.ajax({
url: "createclientcombo.php",
dataType: 'html',
async: false,
data: {
customerid: $("#customerid").val()
},
type: "POST",
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
success: function(data) {
$("#clientid").html(data).trigger("change");
}
});
}
function clientid_onchange() {
$.ajax({
url: "createclientsitecombo.php",
dataType: 'html',
async: false,
data: {
clientid: $("#clientid").val()
},
type: "POST",
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
success: function(data) {
$("#siteid").html(data).trigger("change");
}
});
}
function siteid_onchange() {
showHeader();
}
function total_onchange() {
calculate_total();
}
function calculate_total() {
var total;
var deliverycharge;
var discount;
deliverycharge = parseFloat($("#deliverycharge").val());
discount = parseFloat($("#discount").val());
total = parseFloat($("#total").val());
total -= deliverycharge;
if (total < 0) {
total = 0;
}
total -= (total * (discount) / 100);
$("#discount").val(new Number(discount).toFixed(2));
$("#deliverycharge").val(new Number(deliverycharge).toFixed(2));
$("#total").val(new Number(total).toFixed(2));
}
function productid_onchange() {
callAjax(
"finddata.php",
{
sql: "SELECT A.rspnet, A.productcode, B.priceeach, B.qtyfrom, B.qtyto FROM <?php echo $_SESSION['DB_PREFIX'];?>product A LEFT OUTER JOIN <?php echo $_SESSION['DB_PREFIX'];?>pricebreak B ON B.productid = A.id WHERE A.id = " + $("#item_productid").val()
},
function(data) {
var i;
for (i = 0; i < data.length; i++) {
var node = data[i];
if (i == 0) {
/* Default to unit price. */
$("#item_unitprice").val(new Number(node.rspnet).toFixed(2)).trigger("change");
}
$("#item_productcode").val(node.productcode);
if (node.qtyfrom != null) {
var qty = parseInt($("#item_quantity").val());
if (node.qtyfrom <= qty && node.qtyto >= qty) {
/* Use price break. */
$("#item_unitprice").val(new Number(node.priceeach).toFixed(2)).trigger("change");
}
}
}
}
);
}
function qty_onchange(node) {
var qty = parseInt($("#item_quantity").val());
var unitprice = parseFloat($("#item_unitprice").val());
var vatrate = parseFloat($("#item_vatrate").val());
if (isNaN(unitprice)) {
unitprice = 0;
}
if (isNaN(vatrate)) {
vatrate = 0;
}
if (isNaN(qty)) {
qty = 0;
}
var total = parseFloat(qty * unitprice);
var vat = total * (vatrate / 100);
total += vat;
$("#item_vatrate").val(new Number(vatrate).toFixed(2));
$("#item_vat").val(new Number(vat).toFixed(2));
$("#item_unitprice").val(new Number(unitprice).toFixed(2));
$("#item_quantity").val(new Number(qty).toFixed(0));
$("#item_linetotal").val(new Number(total).toFixed(2));
}
function printInvoice(id) {
window.open("invoicereport.php?id=" + id);
}
function populateTable(data) {
var total = 0;
var html = "<TABLE width='100%' class='grid list'><THEAD><?php createHeader(); ?></THEAD>";
if (data != null) {
data.sort(
function(a, b) {
if(a.sequence < b.sequence) return -1;
if(a.sequence > b.sequence) return 1;
return 0;
}
);
}
$("#item_serial").val(JSON.stringify(data));
if (data != null) {
for (var i = 0; i < data.length; i++) {
var node = data[i];
if (node.description != null) {
html += "<TR>";
html += "<TD>" +
"<img src='images/edit.png' title='Edit item' onclick='editItem(" + i + ")' /> " +
"<img src='images/delete.png' title='Remove item' onclick='removeItem(" + i + ")' /> ";
if (i > 0) {
html += "<img src='images/up.png' title='Move up' onclick='moveUpItem(" + i + ")' /> ";
} else {
html += "<img src='images/up.png' style='visibility:hidden' /> ";
}
if (i < (data.length - 1)) {
html += "<img src='images/down.png' title='Move down' onclick='moveDownItem(" + i + ")' />";
} else {
html += "<img src='images/down.png' style='visibility:hidden' /> ";
}
html +=
"</TD>";
html += "<TD>" + node.description + "</TD>";
html += "<TD align=right>" + new Number(node.quantity).toFixed(0) + "</TD>";
html += "<TD align=right>" + new Number(node.priceeach).toFixed(2) + "</TD>";
html += "<TD align=right>" + new Number(node.vatrate).toFixed(2) + "</TD>";
html += "<TD align=right>" + new Number(node.vat).toFixed(2) + "</TD>";
html += "<TD align=right>" + new Number(node.linetotal).toFixed(2) + "</TD>";
html += "</TR>\n";
total += parseFloat(node.linetotal);
}
}
}
if ($("#deliverycharge").val() == "6.50" || $("#deliverycharge").val() == "0.00") {
if (total < 75) {
$("#deliverycharge").val("6.50");
} else {
$("#deliverycharge").val("0.00");
}
}
$("#total").val(new Number(total).toFixed(2));
calculate_total();
html = html + "</TABLE>";
$("#divtable").html(html);
}
function saveQuoteItem() {
if (! verifyStandardForm("#invoiceitemform")) {
pwAlert("Invalid form");
return false;
}
if (currentItem == -1) {
var lastsequence = 1;
if (itemArray.length > 0) {
lastsequence = itemArray[itemArray.length - 1].sequence + 1;
}
} else {
lastsequence = itemArray[currentItem].sequence;
}
var item = {
id: $("#item_id").val(),
sequence: lastsequence,
quantity: $("#item_quantity").val(),
priceeach: $("#item_unitprice").val(),
vatrate: $("#item_vatrate").val(),
vat: $("#item_vat").val(),
linetotal: $("#item_linetotal").val(),
productid: $("#item_productid").val(),
description: $("#item_productid_lazy").val()
};
if (currentItem == -1) {
itemArray.push(item);
} else {
itemArray[currentItem] = item;
}
populateTable(itemArray);
return true;
}
function moveUpItem(id) {
var sequence = itemArray[id].sequence;
var prevsequence = itemArray[id - 1].sequence;
itemArray[id].sequence = prevsequence;
itemArray[id - 1].sequence = sequence;
populateTable(itemArray)
}
function moveDownItem(id) {
var sequence = itemArray[id].sequence;
var nextsequence = itemArray[id + 1].sequence;
itemArray[id].sequence = nextsequence;
itemArray[id + 1].sequence = sequence;
populateTable(itemArray)
}
function removeItem(id) {
currentItem = id;
$("#confirmRemoveDialog .confirmdialogbody").html("You are about to approve this item.<br>Are you sure ?");
$("#confirmRemoveDialog").dialog("open");
}
function confirmRemoval() {
var newItemArray = [];
var i;
$("#confirmRemoveDialog").dialog("close");
for (i = 0; i < itemArray.length; i++) {
if (currentItem != i) {
newItemArray.push(itemArray[i]);
}
}
itemArray = newItemArray;
populateTable(itemArray);
}
function editItem(id) {
currentItem = id;
var node = itemArray[id];
$("#item_itemid").val(node.id);
$("#item_productid").val(node.productid).trigger("change");
$("#item_productid_lazy").val(node.description);
$("#item_quantity").val(node.quantity);
$("#item_vat").val(node.vat);
$("#item_vatrate").val(node.vatrate);
$("#item_unitprice").val(node.priceeach);
$("#item_linetotal").val(node.linetotal);
$('#invoiceitemdialog').dialog('open');
}
function addQuoteItem() {
currentItem = -1;
$("#item_itemid").val("0");
$("#item_productid").val("0");
$("#item_productid_lazy").val("");
$("#item_quantity").val("1");
$("#item_vatrate").val("<?php echo getSiteConfigData()->vatrate; ?>");
$("#item_vat").val("0.00");
$("#item_unitprice").val("0.00");
$("#item_linetotal").val("0.00");
$('#invoiceitemdialog').dialog('open');
}
function validateForm() {
return true;
}
$(document).ready(
function() {
$("#item_productid").change(productid_onchange);
$("#customerid").change(customerid_onchange);
$("#clientid").change(clientid_onchange);
$("#siteid").change(siteid_onchange);
$("#invoiceitemdialog").dialog({
modal: true,
autoOpen: false,
show:"fade",
closeOnEscape: true,
width: 690,
hide:"fade",
title:"Quote Item",
open: function(event, ui){
},
buttons: {
"Save": function() {
if (saveQuoteItem()) {
$(this).dialog("close");
}
},
Cancel: function() {
$(this).dialog("close");
}
}
});
}
);
function orderReference(node) {
return "<?php echo getSiteConfigData()->bookingprefix; ?>" + padZero(node.orderid, 6);
}
function bookingReference(node) {
return "<?php echo getSiteConfigData()->invoiceprefix; ?>" + padZero(node.id, 6);
}
function editDocuments(node) {
viewDocument(node, "addinvoicedocument.php", node, "invoicedocs", "invoiceid");
}
<?php
}
}
$crud = new InvoiceCrud();
$crud->dialogwidth = 840;
$crud->title = "Invoices";
$crud->table = "{$_SESSION['DB_PREFIX']}invoice";
$crud->sql = "SELECT A.*, B.name AS sitename, C.fullname AS takenbyname, D.name AS clientname, E.name AS customername
FROM {$_SESSION['DB_PREFIX']}invoice A
INNER JOIN {$_SESSION['DB_PREFIX']}customerclientsite B
ON B.id = A.siteid
LEFT OUTER JOIN {$_SESSION['DB_PREFIX']}members C
ON C.member_id = A.takenbyid
INNER JOIN {$_SESSION['DB_PREFIX']}customerclient D
ON D.id = B.clientid
INNER JOIN {$_SESSION['DB_PREFIX']}customer E
ON E.id = D.customerid
ORDER BY A.id DESC";
$crud->columns = array(
array(
'name' => 'id',
'viewname' => 'uniqueid',
'length' => 6,
'showInView' => false,
'filter' => false,
'bind' => false,
'editable' => false,
'pk' => true,
'label' => 'ID'
),
array(
'name' => 'bookingref',
'function' => 'bookingReference',
'sortcolumn' => 'A.id',
'type' => 'DERIVED',
'length' => 17,
'editable' => false,
'bind' => false,
'filter' => false,
'label' => 'Invoice Number'
),
array(
'name' => 'customername',
'length' => 20,
'editable' => false,
'bind' => false,
'label' => 'Customer'
),
array(
'name' => 'clientname',
'length' => 20,
'editable' => false,
'bind' => false,
'label' => 'Customer Client'
),
array(
'name' => 'siteid',
'type' => 'LAZYDATACOMBO',
'length' => 30,
'label' => 'Site',
'table' => 'customerclientsite',
'required' => true,
'table_id' => 'id',
'alias' => 'sitename',
'table_name' => 'name'
),
array(
'name' => 'revision',
'length' => 10,
'readonly' => true,
'label' => 'Revision'
),
array(
'name' => 'invoicedate',
'length' => 12,
'datatype' => 'date',
'label' => 'Invoice Date'
),
array(
'name' => 'orderref',
'function' => 'orderReference',
'sortcolumn' => 'A.orderid',
'type' => 'DERIVED',
'length' => 17,
'editable' => false,
'bind' => false,
'filter' => false,
'label' => 'Order Number'
),
array(
'name' => 'takenbyid',
'type' => 'DATACOMBO',
'length' => 18,
'label' => 'Taken By',
'table' => 'members',
'required' => true,
'table_id' => 'member_id',
'alias' => 'takenbyname',
'table_name' => 'fullname'
),
array(
'name' => 'deliverycharge',
'length' => 13,
'datatype' => 'double',
'align' => 'right',
'label' => 'Delivery Charge'
),
array(
'name' => 'discount',
'length' => 12,
'align' => 'right',
'label' => 'Discount %'
),
array(
'name' => 'total',
'length' => 12,
'align' => 'right',
'label' => 'Total'
)
);
$crud->subapplications = array(
array(
'title' => 'Documents',
'imageurl' => 'images/document.gif',
'script' => 'editDocuments'
),
array(
'title' => 'Print',
'imageurl' => 'images/print.png',
'script' => 'printInvoice'
)
);
$crud->messages = array(
array('id' => 'invoiceid')
);
$crud->run();
?>