/
asset-autodep.php
executable file
·141 lines (112 loc) · 3.39 KB
/
asset-autodep.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
<?
#This program is copyright by Andre Coetzee email: ac@main.me
#and is licensed under the GPL v3
#
#
#
#
#Please add yourself to: http://www.accounting-123.com
#Developers, Software Vendors, Support, Accountants, Users
#
#
#The full software license can be found here:
#http://www.accounting-123.com/a.php?a=153/GPLv3
#
#
#
#
#
#
#
#
#
#
#
require ("settings.php");
require ("core-settings.php");
cFramework::run("select");
cFramework::parse();
function select(&$frm) {
$frm->setkey("confirm");
$frm->settitle("Automatic Depreciation");
$frm->add_heading("Options");
$frm->add_date("Date to Depreciate To", "date", DATE_YEAR, DATE_MONTH, DATE_DAY);
return $frm->getfrm_input();
}
function confirm(&$frm) {
if ($frm->validate("confirm")) {
return select($frm);
}
$frm->setkey("write");
return $frm->getfrm_input();
}
function write($frm) {
extract($_REQUEST);
if ($frm->validate("write")) {
return confirm($frm);
}
pglib_transaction("BEGIN");
db_conn('cubit');
$user = USER_NAME;
$Sql = "SELECT * FROM assets WHERE (dep_month='yes' AND remaction IS NULL)";
$Rslt = db_exec($Sql) or errDie ("Unable to access database.");
$cc = "";
$todate = mkdate($date_year, $date_month, $date_day);
$ttime = mktimefd($todate);
$refnum = getrefnum($todate);
while ($led = pg_fetch_array($Rslt)) {
if (empty($led["autodepr_date"])) {
$led["autodepr_date"] = $led["date"];
}
explodeDate($led["autodepr_date"], $date_year, $date_month, $date_day);
$ftime = mktime(0, 0, 0, $date_month, $date_day, $date_year);
$depmonths = 0;
while ($ftime < $ttime) {
++$depmonths;
$ftime = mktime(0, 0, 0, $date_month + $depmonths, $date_day, $date_year);
}
if ($depmonths == 0) {
continue;
}
$depperc = $led["dep_perc"];
$ml_perc = $depperc * (($depmonths % 12) / 12);
$years = ($depmonths - ($depmonths % 12)) / 12;
$baseamt = $led["amount"] - $led["accdep"];
$depamt = 0;
/* yearly depreciations */
for ($i = 1; $i <= $years; ++$i) {
$depamt += ($baseamt - $depamt) * ($depperc / 100);
}
/* monthly depreciation */
$depamt += ($baseamt - $depamt) * ($ml_perc / 100);
$sql = "SELECT * FROM assetgrp WHERE grpid = '$led[grpid]' AND div = '".USER_DIV."'";
$grpRslt = db_exec($sql);
$grp = pg_fetch_array($grpRslt);
writetrans($grp['depacc'], $grp['accdacc'], $todate, $refnum, $depamt, "$led[des] Depreciation");
db_connect();
$sql = "UPDATE assets SET accdep = (accdep + '$depamt'), autodepr_date='$todate'
WHERE (id='$led[id]' AND div = '".USER_DIV."')";
db_exec($sql) or errdie("Could not update assets table.");
$snetval = ($baseamt - $depamt);
$sdate = date("Y-m-d");
$sql = "INSERT INTO assetledger(assetid, asset, date, depamt, netval, div)
VALUES ('$led[id]', '$led[des]', '$todate', '$depamt', '$snetval', '".USER_DIV."')";
db_exec($sql) or errdie("Could not write to asset ledger.");
$cc .= "CostCenter('ct', 'Asset Depreciation', '$todate', '$led[des] Depreciation', '$depamt', '');";
}
pglib_transaction("COMMIT");
$write = "
<script>
$cc
</script>
<table ".TMPL_tblDflts." width='50%'>
<tr>
<th>Auto Asset Depreciation</th>
</tr>
<tr class='datacell'>
<td>Asset Depreciation has calculated and recorded.</td>
</tr>
</table>";
return $write;
}
?>