-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreports.php
2081 lines (1872 loc) · 69.8 KB
/
reports.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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
include_once 'defs/db_connect.php';
include_once 'defs/functions.php';
require 'defs/inc_accountcheck.php';
if ($displaycontent == true) {
$pagetitle = 'Reports';
?>
<!DOCTYPE html>
<html>
<head>
<?php require 'defs/inc_jscss.php' ?>
<script type="text/javascript" src="js/reports.js"></script>
</head>
<body>
<?php require 'defs/inc_header.php' ?>
<div id="subheader">
<div id="innersubheader">
<?php if (isset($_GET['s'])) {
if ($_GET['s'] == 'property') {
$s = 'property';
} else if ($_GET['s'] == 'landlord') {
$s = 'landlord';
} else if ($_GET['s'] == 'tenant') {
$s = 'tenant';
} else if ($_GET['s'] == 'arrears') {
$s = 'arrears';
} else if ($_GET['s'] == 'voids') {
$s = 'voids';
} else {
$s = 'roomtable';
}
} else {
$s = 'roomtable';
}?>
<span class="<?php echo ($s == 'roomtable' ? 'subtab_active' : 'subtab') ?>" id="roomtable">
Room Table
</span>
<?php /*<span class="<?php echo ($s == 'property' ? 'subtab_active' : 'subtab') ?>" id="property">
Property
</span>*/?>
<span class="<?php echo ($s == 'landlord' ? 'subtab_active' : 'subtab') ?>" id="landlord">
Landlord
</span>
<span class="<?php echo ($s == 'tenant' ? 'subtab_active' : 'subtab') ?>" id="tenant">
Tenant
</span>
<span class="<?php echo ($s == 'arrears' ? 'subtab_active' : 'subtab') ?>" id="arrears">
Arrears
</span>
<?php /* <span class="<?php echo ($s == 'voids' ? 'subtab_active' : 'subtab') ?>" id="voids">
Voids
</span> */?>
</div>
</div>
<div id="main">
<?php if ($s == 'roomtable') { ?>
<?php
if (isset($_GET['date'])) {
if (strtotime($_GET['date']) != 0) {
$date = date('j M Y', strtotime($_GET['date']));
} else {
$date = date('j M Y');
}
} else {
$date = date('j M Y');
}
$dbdate = date('Y-m-d', strtotime($date));
if (isset($_GET['mode'])) {
if ($_GET['mode'] == 'o' || $_GET['mode'] == 's') {
$mode = $_GET['mode'];
} else {
$mode = 'd';
}
} else {
$mode = 'd';
}
if (isset($_GET['type'])) {
if ($_GET['type'] == 'l' || $_GET['type'] == 'm') {
$type = $_GET['type'];
} else {
$type = 'a';
}
} else {
$type = 'a';
}
function showline ($start, $end, $date, $mode) {
$s = strtotime($start);
$e = strtotime($end);
$d = strtotime($date);
if ($s == 0) {
return true;
} else {
if ($mode == 's') {
if ($s <= $d && ($e == 0 || $e >= $d) ) {
return true;
} else {
return false;
}
} else if ($mode == 'o') {
if ($e == 0 || $e >= $d) {
return true;
} else {
return false;
}
} else {
if ($s <= strtotime($date.' +1month') && ($e == 0 || $e >= $d)) {
return true;
} else {
return false;
}
}
}
}
function choosecolor ($start, $end, $date) {
$s = strtotime($start);
$e = strtotime($end);
$d = strtotime($date);
if ($s == 0) {
return 'rtr';
} else if ($e != 0 && $e <= strtotime($date.' +1month')) {
return 'rto';
} else if ($s > $d) {
return 'rty';
} else {
return 'rtg';
}
}
?>
<div class="lfieldset" id="reportoptions">
<h2>Report options</h2>
<div class="optionline">
<label for="roomtablepropertytype">Property type:</label>
<select id="roomtablepropertytype">
<option value="a"<?php echo ($type == 'a' ? " selected=selected" : "") ?>>All</option>
<option value="l"<?php echo ($type == 'l' ? " selected=selected" : "") ?>>Leased</option>
<option value="m"<?php echo ($type == 'm' ? " selected=selected" : "") ?>>Managed</option>
</select>
<label for="roomtablemode">Mode:</label>
<select id="roomtablemode">
<option value="d"<?php echo ($mode == 'd' ? " selected=selected" : "") ?>>Default (+1 month)</option>
<option value="s"<?php echo ($mode == 's' ? " selected=selected" : "") ?>>Snapshot</option>
<option value="o"<?php echo ($mode == 'o' ? " selected=selected" : "") ?>>Open-ended</option>
</select>
<span class="datecont">
<label for="roomtabledate">Date:</label>
<input id="roomtabledate" class="genericdate" value="<?php echo $date ?>" />
</span>
<span class="reportbuttons">
<input type="button" class="button" id="roomtableupdate" value="Update" />
<input type="button" class="button" id="roomtableprint" value="Print" />
</span>
</div>
</div>
<div id="roomtable">
<?php
$q = $db->prepare("
SELECT
mp.property AS `property`,
mp.propertyid AS `propertyid`,
mp.mcid AS `mcid`,
rooms.no AS `roomno`,
tt.tenancyid AS `tenancyid`,
tt.rent AS `rent`,
tt.period AS `period`,
tt.tenantname AS `name`,
tt.tenantid AS `tenantid`,
tt.startdate AS `startdate`,
tt.enddate AS `enddate`,
tt.phone1 AS `phone1`,
tt.phone2 AS `phone2`,
tt.email1 AS `email1`,
tt.email2 AS `email2`,
tt.dob AS `dob`
FROM (
SELECT
tenancies.id AS `tenancyid`,
tenancies.tenantid AS `tenantid`,
tenancies.roomid AS `roomid`,
tenancies.rent AS `rent`,
tenancies.period AS `period`,
tenants.name AS `tenantname`,
tenancies.startdate AS `startdate`,
tenancies.enddate AS `enddate`,
tenants.phone1 AS `phone1`,
tenants.phone2 AS `phone2`,
tenants.email1 AS `email1`,
tenants.email2 AS `email2`,
tenants.dob AS `dob`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id
WHERE (tenancies.enddate >= '$dbdate' OR tenancies.enddate IS NULL)
) tt RIGHT JOIN (
`rooms` LEFT JOIN (
SELECT
properties.id AS `propertyid`,
CONCAT (properties.no,' ',properties.street) AS `property`,
properties.no AS `propertyno`,
properties.clientid AS `clientid`,
mgmtcontracts.id AS `mcid`
FROM `mgmtcontracts` LEFT JOIN `properties` ON mgmtcontracts.propertyid = properties.id
WHERE mgmtcontracts.startdate <= '$dbdate'
AND (mgmtcontracts.enddate >= '$dbdate' OR mgmtcontracts.enddate IS NULL)
) mp ON rooms.propertyid = mp.propertyid
) ON tt.roomid = rooms.id
WHERE mp.clientid = $clientid
AND rooms.del = 'n'
ORDER BY mp.propertyno, mp.property, rooms.no
");
$q->execute();
$r = $q->fetchAll(PDO::FETCH_ASSOC);
$rc = $q->rowCount();
// p($r);
$property = 0;
foreach ($r as $row) {
if (showline($row['startdate'], $row['enddate'], $date, $mode)) {
if ($row['propertyid'] != $property) {
if ($property != 0) { ?>
</tbody>
</table>
<br />
<?php } ?>
<table>
<thead>
<tr>
<?php if (isset($_GET['jupix']) && $_GET['jupix'] == 'yes') { ?>
<th colspan="9"><?php echo $row['property'];
$lq = $db->prepare("SELECT landlords.name AS `name` FROM `mgmtcontracts` LEFT JOIN `landlords` on mgmtcontracts.landlordid = landlords.id WHERE mgmtcontracts.id = ?");
$lq->execute(array($row['mcid']));
$lr = $lq->fetch();
echo " (".$lr['name'].")";
?></th>
<?php } else { ?>
<th colspan="6"><?php echo $row['property'] ?></th>
<?php } ?>
</tr>
<tr>
<th>Room</th>
<th>Rent</th>
<th>Period</th>
<th>Tenant</th>
<?php if (isset($_GET['jupix']) && $_GET['jupix'] == 'yes') { ?>
<th>Phone #</th>
<th>Email</th>
<th>D.o.B.</th>
<?php } ?>
<th>Start date</th>
<th>End date</th>
</tr>
</thead>
<tbody> <?php } ?>
<tr class="rt <?php echo choosecolor($row['startdate'], $row['enddate'], $date) ?>">
<td><?php echo $row['roomno'] ?></td>
<td><?php echo (strlen($row['rent']) > 0 ? number_format($row['rent'] / 100, 2, '.', ',') : " ") ?></td>
<td><?php echo $row['period'] ?></td>
<td><a href="tenants.php?s=detail&d=<?php echo $row['tenantid'] ?>"><?php echo $row['name'] ?></a></td>
<?php if (isset($_GET['jupix']) && $_GET['jupix'] == 'yes') { ?>
<td><?php echo $row['phone1'];
if (strlen($row['phone2']) > 0) {
echo " / ".$row['phone2'];
}?></td>
<td><?php echo $row['email1'];
if (strlen($row['email2']) > 0) {
echo "; ".$row['email2'];
}?></td>
<td><?php if (strtotime($row['dob']) != 0) {
echo date('j M Y', strtotime($row['dob']));
} ?></td>
<?php } ?>
<td><?php echo (strtotime($row['startdate']) != 0 ? date('j M Y', strtotime($row['startdate'])) : "") ?></td>
<td><?php echo (strtotime($row['enddate']) != 0 ? date('j M Y', strtotime($row['enddate'])) : "") ?></td>
</tr><?php
$property = $row['propertyid'];
}
}
if ($rc > 0) { ?>
</tbody>
</table>
<br />
<?php } ?>
</div>
<?php } else if ($s == 'property') { ?>
<p>Property</p>
<?php } else if ($s == 'landlord') { ?>
<?php
if (isset($_GET['d'])) {
$q = $db->prepare("SELECT * FROM `landlords` WHERE `clientid` = $clientid AND `id` = ?");
$q->execute(array($_GET['d']));
$rc = $q->rowCount();
if ($rc == 1) {
$r = $q->fetch();
// $d = $r['id'];
} else {
$q = $db->query("SELECT * FROM `landlords` WHERE `clientid` = $clientid ORDER BY `id` DESC LIMIT 1");
$r = $q->fetch();
// $d = $r['id'];
}
} else {
$q = $db->query("SELECT * FROM `landlords` WHERE `clientid` = $clientid ORDER BY `id` DESC LIMIT 1");
$r = $q->fetch();
// $d = $r['id'];
}
$lid = $r['id']; //temp (?)
if (isset($_GET['start'])) {
if (strtotime($_GET['start']) != 0) {
$start = date('j M Y', strtotime($_GET['start']));
} else {
$start = date('j M Y', strtotime("first day of previous month"));
}
} else {
$start = date('j M Y', strtotime("first day of previous month"));
}
$dbstart = date('Y-m-d', strtotime($start));
if (isset($_GET['end'])) {
if (strtotime($_GET['end']) != 0) {
$end = date('j M Y', strtotime($_GET['end']));
} else {
$end = date('j M Y', strtotime("last day of previous month"));
}
} else {
$end = date('j M Y', strtotime("last day of previous month"));
}
$dbend = date('Y-m-d', strtotime($end));
?>
<div class="lfieldset" id="reportoptions">
<h2>Landlord Report for <?php echo $r['name'] ?></h2>
<div class="optionline">
<label for="reportdselect">Choose landlord:</label>
<select id="reportdselect"><?php echo "\n";
foreach ($db->query("SELECT * FROM `landlords` WHERE `clientid` = $clientid ORDER BY `name`") as $row) {
echo " <option value=\"".$row['id']."\"";
echo ($row['id'] == $r['id'] ? " selected=\"selected\"" : "");
echo ">".$row['name']."</option>";
} ?>
</select>
<span class="datecont">
<label for="reportstartdate">Start:</label>
<input id="reportstartdate" class="genericdate" value="<?php echo $start ?>" />
</span>
<span class="datecont">
<label for="reportenddate">End:</label>
<input id="reportenddate" class="genericdate" value="<?php echo $end ?>" />
</span>
<span class="reportbuttons">
<input type="button" class="button" id="landlordreportupdate" value="Update" />
<input type="button" class="button" id="landlordreportxero" value="Xero" />
<input type="button" class="button" id="landlordreportprint" value="Print" />
</span>
</div>
</div>
<?php // =========================================================================================== start copy ?>
<div id="reportcont">
<p id="reportaddress" ><?php echo "\n";
echo " ".$r['name']."<br />\n";
echo " ".$r['address1']."<br />\n";
if (strlen(trim($r['address2'])) != 0 ) {
echo " ".$r['address2']."<br />\n";
}
echo " ".$r['town']."<br />\n";
echo " ".$r['postcode']."<br />\n";
?>
</p>
<p id="reportdate">
<?php echo date('j M Y', strtotime($end)) ?>
</p>
<h1>Landlord Report for <?php echo $r['name']?></h1>
<input type="hidden" id="llname" value="<?php echo $r['name']?>" /><?php //omg gay
if (!empty($r['sname'])) {
$lastname = $r['sname'];
} else {
$namearr = explode(' ', strtolower($r['name']));
$lastname = array_pop($namearr);
}
?>
<input type="hidden" id="invref" value="<?php echo 'lr-'.date('Ymd', strtotime($end)).'-'.$lastname; ?>" />
<h2>Date range: <?php echo date('j M Y', strtotime($start)) ?> - <?php echo date('j M Y', strtotime($end)) ?></h2><?php echo "\n";
$pbalances = array();
$mcquery = $db->prepare("
SELECT
mgmtcontracts.id AS `id`,
mgmtcontracts.propertyid AS `pid`,
mgmtcontracts.startdate AS `sdate`,
mgmtcontracts.enddate AS `edate`,
mgmtcontracts.mgmt AS `mgmt`,
mgmtcontracts.lease AS `lease`
FROM `mgmtcontracts` LEFT JOIN `landlords` ON mgmtcontracts.landlordid = landlords.id
WHERE landlords.id = ?
");
$mcquery->execute(array($lid));
$mcr = $mcquery->fetchAll(PDO::FETCH_ASSOC);
$mcps = array(); //properties array
// p($mcr);
$debugcount = 0;
foreach ($mcr as $mc) {
if (!array_key_exists($mc['pid'], $mcps)) {
// echo $debugcount."<br>";
$mcps[$mc['pid']] = array(); //each entry contains a subarray for each property
}
// echo "if (".strtotime($mc['sdate'])." <= ".strtotime($end)." && (".strtotime($mc['edate'])." >= ".strtotime($start)." || ".strtotime($mc['edate'])." == 0 ) )<br>";
if (strtotime($mc['sdate']) <= strtotime($end) && (strtotime($mc['edate']) >= strtotime($start) || strtotime($mc['edate']) == 0 ) ) { // if (started yet AND (not finished OR no end date))
// p($mcps);
// p($mc);
// echo $debugcount."<br>";
$mcps[$mc['pid']][$mc['id']] = array(); //which in turn contains a subarray for each mgmtcontract IF it's in date
$mcps[$mc['pid']][$mc['id']]['sdate'] = $mc['sdate'];
$mcps[$mc['pid']][$mc['id']]['edate'] = $mc['edate'];
$mcps[$mc['pid']][$mc['id']]['mgmt'] = $mc['mgmt'];
$mcps[$mc['pid']][$mc['id']]['lease'] = $mc['lease'];
// p($mcps);
}
// p($mcps);
$debugcount++;
}
// p($mcps);
$ptlarray = array();
$pcount = 0;
// p($mcps);
foreach($mcps as $pid => $p) { //property loop FOR EACH [PORTFOLIO] AS [PROPERTY ID] [IN DATE MC ARRAY]
if (count($p) > 0) {
// p($p);
// echo count($p);
$pcount++;
foreach ($db->query("SELECT * FROM `properties` WHERE `id` = $pid") as $pinfo) {
echo " <hr /> \n";
echo " <div class=\"nobreak\">\n";
echo " <h3>".$pinfo['no']." ".$pinfo['street']."</h3>";
}
?>
<table>
<thead>
<tr>
<th colspan="3"><h4>Tenant payments</h4></th>
<input type="hidden" class="pid" value="<?php echo $pid ?>" />
</tr>
</thead>
<tbody>
<?php $tptotal = 0;
$tpstartbal = 0;
// AND payments.date BETWEEN '$dbstart' AND '$dbend'
// AND payments.date BETWEEN '$dbstart' AND '$dbend'
// ==== NEW 5 JAN ====
unset($mcstart_tp);
unset($mcend_tp);
foreach ($p as $mcid => $mc) {
// p($mc);
if (!isset($mcstart_tp)) {
$mcstart_tp = $mc['sdate'];
} else {
if ($mc['sdate'] < $mcstart_tp) {
$mcstart_tp = $mc['sdate'];
}
}
if (!isset($mcend_tp)) {
$mcend_tp = (strtotime($mc['edate']) > 0 ? $mc['edate'] : '9999-12-31');
} else {
if ($mc['edate'] > $mcend_tp) {
$mcend_tp = (strtotime($mc['edate']) > 0 ? $mc['edate'] : '9999-12-31');
}
}
}
if (!isset($mcstart_tp)) {
$mcstart_tp = 0;
}
if (!isset($mcend_tp)) {
$mcend_tp = '9999-12-31';
}
// echo $mcstart_tp;
// echo "<br>";
// echo $mcend_tp;
// ==== NEW 5 JAN ====
foreach ($db->query("
(
SELECT
CONCAT ('rent payment') AS `desc`,
tt.tenantname AS `name`,
tt.tenantid AS `tenantid`,
rooms.no AS `room`,
payments.id AS `paymentid`,
payments.date AS `date`,
payments.amount AS `amount`
FROM `payments`
LEFT JOIN ((
SELECT
tenancies.id AS `tenancyid`,
tenancies.roomid AS `roomid`,
tenants.name AS `tenantname`,
tenants.id AS `tenantid`,
tenancies.startdate AS `startdate`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id) tt
LEFT JOIN (`rooms`
LEFT JOIN `properties`
ON rooms.propertyid = properties.id)
ON tt.roomid = rooms.id)
ON payments.contactid = tt.tenancyid
WHERE properties.id = $pid
AND payments.contacttype = 't'
) UNION (
SELECT
CONCAT ('fee: ', tenantfees.desc) AS `desc`,
tt.tenantname AS `name`,
tt.tenantid AS `tenantid`,
rooms.no AS `room`,
payments.id AS `paymentid`,
payments.date AS `date`,
payments.amount AS `amount`
FROM `payments` LEFT JOIN (
`tenantfees` LEFT JOIN (
(
SELECT
tenancies.id AS `tenancyid`,
tenancies.roomid AS `roomid`,
tenants.name AS `tenantname`,
tenants.id AS `tenantid`,
tenancies.startdate AS `startdate`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id
) tt LEFT JOIN (
`rooms` LEFT JOIN `properties` ON rooms.propertyid = properties.id
) ON tt.roomid = rooms.id
) ON tenantfees.tenancyid = tt.tenancyid
) ON payments.contactid = tenantfees.id
WHERE properties.id = $pid
AND payments.contacttype = 'f'
AND tenantfees.payableto = 'l'
)
ORDER BY `date`
") as $payment) {
if ($payment['date'] >= $mcstart_tp && $payment['date'] <= $mcend_tp ) { //mc range
if ($payment['date'] >= $dbstart && $payment['date'] <= $dbend ) { //report range
$tptotal += $payment['amount'];
?>
<tr data-type="rent">
<td class="date"><?php echo date('j M Y', strtotime($payment['date']))?></td>
<td><?php echo "Room ".$payment['room']." - <a href=\"reports.php?s=tenant&d=".$payment['tenantid']."\">".$payment['name']."</a> (".$payment['desc'].")" ?></td>
<td class="amount"><a href="bank.php?s=detail&d=<?php echo $payment['paymentid'] ?>"><?php echo number_format($payment['amount'] / 100, 2, '.', ',') ?></a></td>
</tr>
<?php
} else if ($payment['date'] < $dbstart) {
$tpstartbal += $payment['amount'];
}
} else if ($payment['date'] < $mcstart_tp) { //fixing bernhard error (this counts property transactions from before the MC start date but doesn't take into account the landlord might be different)
$tpstartbal += $payment['amount'];
// echo "DERP";
}
} ?>
</tbody>
<tfoot>
<tr>
<td class="total date"><?php echo date('j M Y', strtotime($end))?></td>
<td class="total">Total tenant payments:</td>
<td class="total amount"><?php echo number_format($tptotal / 100, 2, '.', ',') ?></td>
</tr>
</tfoot>
</table>
</div>
<br />
<p class="nobreak">
<table>
<thead>
<tr>
<th colspan="3"><h4>Management fees</h4></th>
<input type="hidden" class="pid" value="<?php echo $pid ?>" />
</tr>
</thead>
<tbody>
<?php
// p($p);
$mgmttotal = 0;
$mgmtstartbal = 0;
foreach ($p as $mcid => $mc) {
if ($mc['mgmt'] != 100) { //@@@@@@@@@to come back to leases
$mcstart = $mc['sdate'];
$mcend = (strtotime($mc['edate']) > 0 ? $mc['edate'] : '9999-12-31');
$mcpaymenttotal = 0;
$mcpaymentstartbal = 0;
// echo $mcstart;
// echo "<br>";
// echo $mcend;
foreach ($db->query("
SELECT
payments.date AS `date`,
payments.amount AS `amount`
FROM `payments`
LEFT JOIN ((
SELECT
tenancies.id AS `tenancyid`,
tenancies.roomid AS `roomid`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id) tt
LEFT JOIN (`rooms`
LEFT JOIN `properties`
ON rooms.propertyid = properties.id)
ON tt.roomid = rooms.id)
ON payments.contactid = tt.tenancyid
WHERE properties.id = $pid
AND payments.contacttype = 't'
AND (payments.date BETWEEN '$mcstart' AND '$mcend')
") as $payment) {
if ($payment['date'] >= $dbstart && $payment['date'] <= $dbend ) { //report range
$mcpaymenttotal += $payment['amount'] * $mc['mgmt'] / 100;
} else if ($payment['date'] < $dbstart) {
// $mcpaymentstartbal += $payment['amount'] * $mc['mgmt'] / 100; //does not look for MCs before report date, new function after p loop
}
}
?>
<tr data-type="man">
<td class="date"><?php echo date('j M Y', strtotime($end))?></td>
<td>Management fees at <?php echo $mc['mgmt'] ?>% on rent taken</td>
<td class="amount"><?php echo number_format($mcpaymenttotal / 100 * -1, 2, '.', ',') ?></td>
</tr>
<?php
// ADJUSTMENTS
foreach ($db->query("
SELECT
adjustments.date AS `date`,
adjustments.amount AS `amount`,
adjustments.desc AS `desc`,
tt.tenantname AS `tenantname`
FROM `adjustments`
LEFT JOIN ((
SELECT
tenancies.id AS `tenancyid`,
tenants.name AS `tenantname`,
tenancies.roomid AS `roomid`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id) tt
LEFT JOIN (`rooms`
LEFT JOIN `properties`
ON rooms.propertyid = properties.id)
ON tt.roomid = rooms.id)
ON adjustments.tenancyid = tt.tenancyid
WHERE properties.id = $pid
AND (adjustments.date BETWEEN '$mcstart' AND '$mcend')
AND adjustments.applymgmt = 'y'
ORDER BY adjustments.date
") as $adjustment) {
if ($adjustment['date'] >= $dbstart && $adjustment['date'] <= $dbend ) { //report range
?>
<tr data-type="man">
<td class="date"><?php echo date('j M Y', strtotime($adjustment['date']))?></td>
<td>Management fee at <?php echo $mc['mgmt'] ?>% on adjustment: <?php echo $adjustment['tenantname'] ?> - <?php echo $adjustment['desc'] ?> </td>
<td class="amount"><?php echo number_format($adjustment['amount'] * $mc['mgmt'] / 100 / 100 * -1, 2, '.', ',') ?></td>
</tr>
<?php
$mcpaymenttotal += $adjustment['amount'] * $mc['mgmt'] / 100;
} else if ($adjustment['date'] < $dbstart) {
// $mcpaymentstartbal += $adjustment['amount'] * $mc['mgmt'] / 100; //does not look for MCs before report date, new function after p loop
}
}
$mgmttotal += $mcpaymenttotal;
// $mgmtstartbal += $mcpaymentstartbal; //does not look for MCs before report date, new function after p loop
}
}
//new mc startbal function
$allrentpayments = array();
foreach ($db->query("
SELECT
payments.date AS `date`,
payments.amount AS `amount`
FROM `payments`
LEFT JOIN ((
SELECT
tenancies.id AS `tenancyid`,
tenancies.roomid AS `roomid`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id) tt
LEFT JOIN (`rooms`
LEFT JOIN `properties`
ON rooms.propertyid = properties.id)
ON tt.roomid = rooms.id)
ON payments.contactid = tt.tenancyid
WHERE properties.id = $pid
AND payments.contacttype = 't'
") as $propertyrentpayment) { //this gives me a list of all payments against the property, need to save it as an array then loop through all mgmtcontracts checking the array
$allrentpayments[] = $propertyrentpayment;
}
// p($allrentpayments);
$alladjustments = array();
foreach ($db->query("
SELECT
adjustments.date AS `date`,
adjustments.amount AS `amount`,
adjustments.desc AS `desc`,
tt.tenantname AS `tenantname`
FROM `adjustments`
LEFT JOIN ((
SELECT
tenancies.id AS `tenancyid`,
tenants.name AS `tenantname`,
tenancies.roomid AS `roomid`
FROM `tenancies` LEFT JOIN `tenants` ON tenancies.tenantid = tenants.id) tt
LEFT JOIN (`rooms`
LEFT JOIN `properties`
ON rooms.propertyid = properties.id)
ON tt.roomid = rooms.id)
ON adjustments.tenancyid = tt.tenancyid
WHERE properties.id = $pid
AND adjustments.applymgmt = 'y'
") as $propertyadjustment) {
$alladjustments[] = $propertyadjustment;
}
// this next bit originally had:
// WHERE landlords.id = ".$r.['id']."
// ...at the end. taken it out as it might mess some other balances out, to access and possibly correct when we get a property transfer
foreach ($db->query("
SELECT
mp.startdate AS `startdate`,
mp.enddate AS `enddate`,
mp.mgmt AS `mgmt`
FROM (
SELECT
mgmtcontracts.landlordid AS `landlordid`,
mgmtcontracts.startdate AS `startdate`,
mgmtcontracts.enddate AS `enddate`,
mgmtcontracts.mgmt AS `mgmt`
FROM `mgmtcontracts` LEFT JOIN `properties` ON mgmtcontracts.propertyid = properties.id WHERE properties.id = $pid
) mp LEFT JOIN `landlords` ON mp.landlordid = landlords.id
") as $mcrentpaymentcheck) {
// echo "meep";
if (empty($mcrentpaymentcheck['enddate'])) {
$mcrentpaymentcheck['enddate'] = '9999-12-31';
}
foreach ($allrentpayments as $propertyrentpayment) {
if ($propertyrentpayment['date'] >= $mcrentpaymentcheck['startdate'] && $propertyrentpayment['date'] <= $mcrentpaymentcheck['enddate'] && $propertyrentpayment['date'] < $dbstart) {
if ($mcrentpaymentcheck['mgmt'] != 100) {
$mcpaymentstartbal += $propertyrentpayment['amount'] * $mcrentpaymentcheck['mgmt'] / 100;
}
}
}
foreach ($alladjustments as $propertyadjustment) {
if ($propertyadjustment['date'] >= $mcrentpaymentcheck['startdate'] && $propertyadjustment['date'] <= $mcrentpaymentcheck['enddate'] && $propertyadjustment['date'] < $dbstart) {
if ($mcrentpaymentcheck['mgmt'] != 100) {
$mcpaymentstartbal += $propertyadjustment['amount'] * $mcrentpaymentcheck['mgmt'] / 100;
}
}
}
}
// p($mcrentpaymentcheck);
$mgmtstartbal += $mcpaymentstartbal;
?>
</tbody>
<tfoot>
<tr>
<td class="total date"><?php echo date('j M Y', strtotime($end))?></td>
<td class="total">Total management fee deduction:</td>
<td class="total amount"><?php echo number_format($mgmttotal / 100 * -1, 2, '.', ','); ?></td>
</tr>
</tfoot>
</table>
</p>
<p class="nobreak">
<table>
<thead>
<tr>
<th colspan="3"><h4>Letting fees</h4></th>
<input type="hidden" class="pid" value="<?php echo $pid ?>" />
</tr>
</thead>
<tbody>
<?php $lftotal = 0; $lfstartbal = 0;
/* foreach ($db->query("SELECT * FROM `propertyfees` WHERE `propertyid` = $pid AND type = 'l' AND `date` between '$dbstart' AND '$dbend' ORDER BY `date`") as $lfee) { */
foreach ($db->query("
SELECT
propertyfees.id AS `id`,
propertyfees.date AS `date`,
CONCAT(properties.no, ' ', properties.street) AS `property`,
propertyfees.amount AS `amount`,
propertyfees.desc AS `desc`
FROM `propertyfees` LEFT JOIN (`mgmtcontracts` LEFT JOIN `properties` ON mgmtcontracts.propertyid = properties.id) ON propertyfees.mcid = mgmtcontracts.id
WHERE properties.id = $pid
AND propertyfees.type = 'l'
ORDER BY propertyfees.date
") as $lfee) { // @@@@@@@@@@@@@@@@@@@@ NEED TO ADD THE $mcstart_tp / $mcend_tp check in here! @@@@@@@@@@@@@@@@@@@@
if ($lfee['date'] >= $dbstart && $lfee['date'] <= $dbend) {
$lftotal += $lfee['amount'];
?>
<tr data-type="let">
<td class="date"><?php echo date('j M Y', strtotime($lfee['date']))?></td>
<td><?php echo $lfee['desc'] ?></td>
<td class="amount"><?php echo number_format($lfee['amount'] / 100 * -1, 2, '.', ',') ?></td></td>
</tr>
<?php
} else if ($lfee['date'] < $dbstart) {
$lfstartbal += $lfee['amount'];
}
} ?>
</tbody>
<tfoot>
<tr>
<td class="total date"><?php echo date('j M Y', strtotime($end))?></td>
<td class="total">Total letting fees:</td>
<td class="total amount"><?php echo number_format($lftotal / 100 * -1, 2, '.', ',') ?></td>
</tr>
</tfoot>
</table>
</p>
<p class="nobreak">
<table>
<thead>
<tr>
<th colspan="3"><h4>Other fees & expenses</h4></th>
<input type="hidden" class="pid" value="<?php echo $pid ?>" />
</tr>
</thead>
<tbody>
<?php $ftotal = 0; $fstartbal = 0;
/* foreach ($db->query("SELECT * FROM `propertyfees` WHERE `propertyid` = $pid AND type = 'o' AND `date` between '$dbstart' AND '$dbend' ORDER BY `date`") as $fee) { */
foreach ($db->query("
SELECT
propertyfees.id AS `id`,
propertyfees.date AS `date`,
propertyfees.amount AS `amount`,
propertyfees.desc AS `desc`,
propertyfees.type AS `type`
FROM `propertyfees` LEFT JOIN (`mgmtcontracts` LEFT JOIN `properties` ON mgmtcontracts.propertyid = properties.id) ON propertyfees.mcid = mgmtcontracts.id
WHERE properties.id = $pid
AND (propertyfees.type = 'o' OR propertyfees.type = 'm')
ORDER BY propertyfees.date
") as $fee) { // @@@@@@@@@@@@@@@@@@@@ NEED TO ADD THE $mcstart_tp / $mcend_tp check in here! @@@@@@@@@@@@@@@@@@@@
if ($fee['date'] >= $dbstart && $fee['date'] <= $dbend) {
$ftotal += $fee['amount'];
?>
<tr data-type="<?php echo ($fee['type'] == 'm' ? 'main' : 'other') ?>">
<td class="date"><?php echo date('j M Y', strtotime($fee['date']))?></td>
<td><?php echo $fee['desc'] ?></td>
<td class="amount"><?php echo number_format($fee['amount'] / 100 * -1, 2, '.', ',') ?></td></td>
</tr>
<?php
} else if ($fee['date'] < $dbstart) {
$fstartbal += $fee['amount'];
}
} ?>
</tbody>
<tfoot>
<tr>
<td class="total date"><?php echo date('j M Y', strtotime($end))?></td>
<td class="total">Total other fees & expenses:</td>
<td class="total amount"><?php echo number_format($ftotal / 100 * -1, 2, '.', ',') ?></td>
</tr>
</tfoot>
</table>
</p>
<br />
<?php //landlord payments here so we can use them for start balances
// AND landlords.id = $lid
$llpayments = 0; $llpaystartbal = 0;
foreach ($db->query("
SELECT
payments.amount AS `amount`,
payments.date AS `date`
FROM `payments` LEFT JOIN (
(
SELECT
mgmtcontracts.id AS `mcid`,
mgmtcontracts.landlordid AS `lid`,
properties.id AS `pid`
FROM `mgmtcontracts` LEFT JOIN `properties` ON mgmtcontracts.propertyid = properties.id
) mp LEFT JOIN `landlords` ON mp.lid = landlords.id
) ON payments.contactid = mp.mcid
WHERE payments.contacttype = 'l'
AND mp.pid = $pid
") as $llpayment) {
if ($llpayment['date'] >= $dbstart && $llpayment['date'] <= $dbend) {
$llpayments += $llpayment['amount'];
} else if ($llpayment['date'] < $dbstart) {
$llpaystartbal += $llpayment['amount'];
}
}
// p($llpaystartbal);
?>
<table>
<tr>
<td class="borderless total alignright" colspan="2">Property total income:</td>
<td class="borderless total amount"><?php echo number_format($tptotal / 100, 2, '.', ',') ?></td>
</tr>
<tr>
<td class="borderless total alignright" colspan="2">Property total deductions:</td>
<td class="borderless total amount"><?php echo number_format((($mgmttotal / 100) + ($lftotal / 100) + ($ftotal / 100)) * -1 , 2, '.', ',') ?></td>
</tr>
<tr>
<td class="borderless total alignright" colspan="2">Property report total:</td>
<td class="borderless total amount"><?php
$prb = round(($tptotal / 100) - ($mgmttotal / 100) - ($lftotal / 100) - ($ftotal / 100), 2);
echo number_format($prb, 2, '.', ',') ?> <br /><br /></td>
</tr>
<tr>
<td class="borderless total alignright" colspan="2">Property opening balance:</td>
<td class="borderless total amount"><?php
// $pob = 610.24; //dave
// $pob = 1388.99; //andrew
// $pob = 1489.00; //niraj
// $pob = 166372; //tsen
$pob = round($tpstartbal - $mgmtstartbal - $lfstartbal - $fstartbal + $llpaystartbal);
// echo "$tpstartbal - $mgmtstartbal - $lfstartbal - $fstartbal + $llpaystartbal = \n";
echo number_format($pob / 100, 2, '.', ',');
?></td>
</tr>
<tr>
<td class="borderless total alignright" colspan="2">Less payments made to Landlord:</td>
<td class="borderless total amount"><?php
echo number_format($llpayments / 100, 2, '.', ',');
?></td>
</tr>
<tr>
<td class="borderless total alignright" colspan="2">Payable to Landlord:</td>
<td class="borderless total amount"><?php
$ptl = ($pob / 100) + $prb + ($llpayments / 100);
echo number_format($ptl, 2, '.', ',');
$ptlarray[$pinfo['no']." ".$pinfo['street']] = number_format($ptl, 2, '.', '');
?></td>
</tr>
</table>
<?php
// echo "prb: ".$prb." <br>";
// echo "pob: ".$pob." <br>";
// echo "llp: ".$llpayments." <br>";
// d(($tptotal / 100) ." ". ($mgmttotal / 100) ." ". ($lftotal / 100) ." ". ($ftotal / 100));
$pbalance = array($pinfo['no']." ".$pinfo['street'], ($tptotal / 100) - ($mgmttotal / 100) - ($lftotal / 100) - ($ftotal / 100));
$pbalances[] = $pbalance;
}
} // property loop
if ($pcount > 1) { ?>
<table style="display:none"> <!-- depreciated, being lazy here -->
<tr>
<th colspan="3"><h4>Total balances</h4></th>
</tr>
<?php $cbalance = 0;
foreach ($pbalances as $pbalance) { ?>
<tr>
<td class="date"><?php echo date('j M Y', strtotime($end))?></td>
<td><?php echo $pbalance[0] ?></td>
<td class="amount"><?php echo number_format($pbalance[1], 2, '.', ',') ?></td>
</tr>
<?php $cbalance += $pbalance[1]; } ?>
<tr>
<td class="total date"><?php echo date('j M Y', strtotime($end))?></td>