-
Notifications
You must be signed in to change notification settings - Fork 1
/
FixedAssetDepreciation.php
executable file
·283 lines (257 loc) · 12.2 KB
/
FixedAssetDepreciation.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
<?php
/* $Revision: 1.1 $ */
include('includes/DefineJournalClass.php');
include('includes/session.inc');
$title = _('Depreciation Journal Entry');
include('includes/header.inc');
include('includes/SQL_CommonFunctions.inc');
/*Get the last period depreciation (depn is transtype =44) was posted for */
$result = DB_query("SELECT periods.lastdate_in_period,
max(fixedassettrans.periodno)
FROM fixedassettrans INNER JOIN periods
ON fixedassettrans.periodno=periods.periodno
WHERE transtype=44
GROUP BY periods.lastdate_in_period
ORDER BY periods.lastdate_in_period DESC",$db);
$LastDepnRun = DB_fetch_row($result);
//echo '<br />LastRun period = ' . $LastDepnRun[1] . ' Last date in period = ' . $LastDepnRun[0];
$AllowUserEnteredProcessDate = true;
if (DB_num_rows($result)==0) { //then depn has never been run yet?
/* in this case default depreciation calc to the last day of last month - and allow user to select a period */
if (!isset($_POST['ProcessDate'])) {
$_POST['ProcessDate'] = Date($_SESSION['DefaultDateFormat'],mktime(0,0,0,date('m'),0,date('Y')));
} else { //ProcessDate is set - make sure it is on the last day of the month selected
if (!Is_Date($_POST['ProcessDate'])){
prnMsg(_('The date is expected to be in the format') . ' ' . $_SESSION['DefaultDateFormat'], 'error');
$InputError =true;
}else {
$_POST['ProcessDate'] = LastDayOfMonth($_POST['ProcessDate']);
}
}
} else { //depn calc has been run previously
$AllowUserEnteredProcessDate = false;
//prnMsg('LastDepnRun[0] = ' . $LastDepnRun[0] . '<br />ConvertSQLDate($LastDepnRun[0]) = ' . ConvertSQLDate($LastDepnRun[0]) . '<br />DateAdd(ConvertSQLDate($LastDepnRun[0]),d,28) = ' . DateAdd(ConvertSQLDate($LastDepnRun[0]),'d',28), 'info');
prnMsg('LastDepnRun = ' . ConvertSQLDate($LastDepnRun[0]), 'info');
$_POST['ProcessDate'] = LastDayOfMonth(DateAdd(ConvertSQLDate($LastDepnRun[0]),'d',28));
}
/* Get list of assets for journal */
$sql="SELECT fixedassets.assetid,
fixedassets.description,
fixedassets.depntype,
fixedassets.depnrate,
fixedassets.datepurchased,
fixedassetcategories.accumdepnact,
fixedassetcategories.depnact,
fixedassetcategories.categorydescription,
SUM(CASE WHEN fixedassettrans.fixedassettranstype='cost' THEN fixedassettrans.amount ELSE 0 END) AS costtotal,
SUM(CASE WHEN fixedassettrans.fixedassettranstype='depn' THEN fixedassettrans.amount ELSE 0 END) AS depnbfwd
FROM fixedassets
INNER JOIN fixedassetcategories
ON fixedassets.assetcategoryid=fixedassetcategories.categoryid
INNER JOIN fixedassettrans
ON fixedassets.assetid=fixedassettrans.assetid
WHERE fixedassettrans.transdate<='" . FormatDateForSQL($_POST['ProcessDate']) . "'
GROUP BY fixedassets.assetid,
fixedassets.description,
fixedassets.depntype,
fixedassets.depnrate,
fixedassets.datepurchased,
fixedassetcategories.accumdepnact,
fixedassetcategories.depnact,
fixedassetcategories.categorydescription
ORDER BY assetcategoryid, assetid";
$AssetsResult=DB_query($sql, $db);
$InputError = false; //always hope for the best
if (Date1GreaterThanDate2($_POST['ProcessDate'],Date($_SESSION['DefaultDateFormat']))){
prnMsg(_('No depreciation will be committed as the processing date is beyond the current date. The depreciation run can only be run for periods prior to today'),'warn');
$InputError =true;
}
if (isset($_POST['CommitDepreciation']) AND $InputError==false){
$result = DB_Txn_Begin($db);
$TransNo = GetNextTransNo(44, $db);
$PeriodNo = GetPeriod($_POST['ProcessDate'],$db);
}
echo '<p class="page_title_text"><img src="'.$rootpath.'/css/'.$theme.'/images/money_add.png" title="' . _('Fixed Asset Depreciation') . '" alt="" />' . ' ' . $title . '</p>';
echo '<table class="selection">';
$Heading = '<tr><th>' . _('Asset ID') . '</th>
<th>' . _('Description') . '</th>
<th>' . _('Date Purchased') . '</th>
<th>' . _('Cost') . '</th>
<th>' . _('Accum Depn') . '</th>
<th>' . _('B/fwd Book Value') . '</th>
<th>' . _('Depn Type') . '</th>
<th>' . _('Depn Rate') . '</th>
<th>' . _('New Depn') . '</th>
</tr>';
echo $Heading;
$AssetCategoryDescription ='0';
$TotalCost = 0;
$TotalAccumDepn = 0;
$TotalDepn = 0;
$RowCounter = 0;
$TotalCategoryCost = 0;
$TotalCategoryAccumDepn = 0;
$TotalCategoryDepn = 0;
$k=0;
while ($AssetRow=DB_fetch_array($AssetsResult)) {
if ($AssetCategoryDescription != $AssetRow['categorydescription'] OR $AssetCategoryDescription =='0'){
if ($AssetCategoryDescription !='0'){ //then print totals
echo '<tr><th colspan="3" align="right">' . _('Total for') . ' ' . $AssetCategoryDescription . ' </th>
<th class="number">' . locale_money_format($TotalCategoryCost,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th class="number">' . locale_money_format($TotalCategoryAccumDepn,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th class="number">' . locale_money_format(($TotalCategoryCost-$TotalCategoryAccumDepn),$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th colspan="2"></th>
<th class="number">' . locale_money_format($TotalCategoryDepn,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
</tr>';
}
echo '<tr><th colspan="9" align="left">' . $AssetRow['categorydescription'] . '</th></tr>';
$AssetCategoryDescription = $AssetRow['categorydescription'];
$TotalCategoryCost = 0;
$TotalCategoryAccumDepn =0;
$TotalCategoryDepn = 0;
}
$BookValueBfwd = $AssetRow['costtotal'] - $AssetRow['depnbfwd'];
if ($AssetRow['depntype']==0){ //striaght line depreciation
$DepreciationType = _('SL');
$NewDepreciation = $AssetRow['costtotal'] * $AssetRow['depnrate']/100/12;
if ($NewDepreciation > $BookValueBfwd){
$NewDepreciation = $BookValueBfwd;
}
} else { //Diminishing value depreciation
$DepreciationType = _('DV');
$NewDepreciation = $BookValueBfwd * $AssetRow['depnrate']/100/12;
}
if (Date1GreaterThanDate2($AssetRow['datepurchased'],$_POST['ProcessDate'])){
/*Over-ride calculations as the asset was not purchased at the date of the calculation!! */
$NewDepreciation =0;
}
$RowCounter++;
if ($RowCounter ==15){
echo $Heading;
$RowCounter =0;
}
if ($k==1){
echo '<tr class="EvenTableRows">';
$k=0;
} else {
echo '<tr class="OddTableRows">';
$k++;
}
echo '<td>' . $AssetRow['assetid'] . '</td>
<td>' . $AssetRow['description'] . '</td>
<td>' . ConvertSQLDate($AssetRow['datepurchased']) . '</td>
<td class="number">' . locale_money_format($AssetRow['costtotal'],$_SESSION['CompanyRecord']['currencydefault']) . '</td>
<td class="number">' . locale_money_format($AssetRow['depnbfwd'],$_SESSION['CompanyRecord']['currencydefault']) . '</td>
<td class="number">' . locale_money_format($AssetRow['costtotal']-$AssetRow['depnbfwd'],$_SESSION['CompanyRecord']['currencydefault']) . '</td>
<td align="center">' . $DepreciationType . '</td>
<td class="number">' . $AssetRow['depnrate'] . '</td>
<td class="number">' . locale_money_format($NewDepreciation ,$_SESSION['CompanyRecord']['currencydefault']) . '</td>
</tr>';
$TotalCategoryCost +=$AssetRow['costtotal'];
$TotalCategoryAccumDepn +=$AssetRow['depnbfwd'];
$TotalCategoryDepn +=$NewDepreciation;
$TotalCost +=$AssetRow['costtotal'];
$TotalAccumDepn +=$AssetRow['depnbfwd'];
$TotalDepn +=$NewDepreciation;
if (isset($_POST['CommitDepreciation']) AND $NewDepreciation !=0 AND $InputError==false){
//debit depreciation expense
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (44,
'" . $TransNo . "',
'" . FormatDateForSQL($_POST['ProcessDate']) . "',
'" . $PeriodNo . "',
'" . $AssetRow['depnact'] . "',
'" . $AssetRow['assetid'] . "',
'" . $NewDepreciation ."')";
$ErrMsg = _('Cannot insert a depreciation GL entry for the depreciation because');
$DbgMsg = _('The SQL that failed to insert the GL Trans record was');
$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
$SQL = "INSERT INTO gltrans (type,
typeno,
trandate,
periodno,
account,
narrative,
amount)
VALUES (44,
'" . $TransNo . "',
'" . FormatDateForSQL($_POST['ProcessDate']) . "',
'" . $PeriodNo . "',
'" . $AssetRow['accumdepnact'] . "',
'" . $AssetRow['assetid'] . "',
'" . -$NewDepreciation ."')";
$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
//insert the fixedassettrans record
$SQL = "INSERT INTO fixedassettrans (assetid,
transtype,
transno,
transdate,
periodno,
inputdate,
fixedassettranstype,
amount)
VALUES ('" . $AssetRow['assetid'] . "',
'44',
'" . $TransNo . "',
'" . FormatDateForSQL($_POST['ProcessDate']) . "',
'" . $PeriodNo . "',
'" . Date('Y-m-d') . "',
'depn',
'" . $NewDepreciation . "')";
$ErrMsg = _('Cannot insert a fixed asset transaction entry for the depreciation because');
$DbgMsg = _('The SQL that failed to insert the fixed asset transaction record was');
$result = DB_query($SQL,$db,$ErrMsg,$DbgMsg,true);
/*now update the accum depn in fixedassets */
$SQL = "UPDATE fixedassets SET accumdepn = accumdepn + " . $NewDepreciation . "
WHERE assetid = '" . $AssetRow['assetid'] . "'";
$ErrMsg = _('CRITICAL ERROR! NOTE DOWN THIS ERROR AND SEEK ASSISTANCE. The fixed asset accumulated depreciation could not be updated:');
$DbgMsg = _('The following SQL was used to attempt the update the accumulated depreciation of the asset was:');
$Result = DB_query($SQL,$db,$ErrMsg, $DbgMsg, true);
} //end if Committing the depreciation to DB
} //end loop around the assets to calculate depreciation for
echo '<tr><th colspan="3" align="right">' . _('Total for') . ' ' . $AssetCategoryDescription . ' </th>
<th class="number">' . locale_money_format($TotalCategoryCost,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th class="number">' . locale_money_format($TotalCategoryAccumDepn,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th class="number">' . locale_money_format(($TotalCategoryCost-$TotalCategoryAccumDepn),$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th colspan="2"></th>
<th class="number">' . locale_money_format($TotalCategoryDepn,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
</tr>';
echo '<tr><th colspan="3" align="right">' . _('GRAND Total') . ' </th>
<th class="number">' . locale_money_format($TotalCost,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th class="number">' . locale_money_format($TotalAccumDepn,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th class="number">' . locale_money_format(($TotalCost-$TotalAccumDepn),$_SESSION['CompanyRecord']['currencydefault']) . '</th>
<th colspan="2"></th>
<th class="number">' . locale_money_format($TotalDepn,$_SESSION['CompanyRecord']['currencydefault']) . '</th>
</tr>';
echo '</table>';
if (isset($_POST['CommitDepreciation']) AND $InputError==false){
$result = DB_Txn_Commit($db);
prnMsg(_('Depreciation') . ' ' . $TransNo . ' ' . _('has been successfully entered'),'success');
unset($_POST['ProcessDate']);
echo '<br /><a href="index.php' . '">' ._('Return to main menu').'</a>';
/*And post the journal too */
include ('includes/GLPostings.inc');
} else {
echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES, 'UTF-8') . '" method="post" name="form">';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<p></p>';
echo '<table class="selection" width=30%><tr></tr><tr>';
if ($AllowUserEnteredProcessDate){
echo '<td>'._('Date to Process Depreciation'). ':</td>
<td><input type="text" class="date" alt="' .$_SESSION['DefaultDateFormat']. '" name="ProcessDate" maxlength="10" size="11" value="' . $_POST['ProcessDate'] . '" /></td>';
} else {
echo '<td>'._('Date to Process Depreciation'). ':</td>
<td>' . $_POST['ProcessDate'] .'</td>';
}
echo '<td><div class="centre"><button type="submit" name="CommitDepreciation">'._('Commit Depreciation').'</button></div>';
echo '</tr></table><br />';
echo '</form>';
}
include('includes/footer.inc');
?>