rerun of bug 5232 fix.
[fa-stable.git] / reporting / rep451.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL,
5         as published by the Free Software Foundation, either version 3
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_ASSETSANALYTIC';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2015-12-01
17 // Title:       Fixed Assets Valuation
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
26 include_once($path_to_root . "/fixed_assets/includes/fixed_assets_db.inc");
27 include_once($path_to_root . "/fixed_assets/includes/fa_classes_db.inc");
28
29 function find_last_location($stock_id, $end_date)
30 {
31         $end_date = date2sql($end_date);
32         $sql = "SELECT loc_code FROM ".TB_PREF."stock_moves WHERE stock_id = ".db_escape($stock_id)." AND
33                 tran_date <= '$end_date' ORDER BY tran_date DESC LIMIT 1";
34         $res = db_query($sql,"No stock moves were returned");
35         $row = db_fetch_row($res);
36         return $row[0];
37 }
38
39 //----------------------------------------------------------------------------------------------------
40
41 print_fixed_assets_valuation_report();
42
43 //----------------------------------------------------------------------------------------------------
44
45 function print_fixed_assets_valuation_report()
46 {
47     global $path_to_root, $SysPrefs;
48
49         $date = $_POST['PARAM_0'];
50     $class = $_POST['PARAM_1'];
51     $location = $_POST['PARAM_2'];
52     $detail = $_POST['PARAM_3'];
53     $comments = $_POST['PARAM_4'];
54         $orientation = $_POST['PARAM_5'];
55         $destination = $_POST['PARAM_6'];
56         if ($destination)
57                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
58         else
59                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
60         $detail = !$detail;
61     $dec = user_price_dec();
62
63         $orientation = ($orientation ? 'L' : 'P');
64         if ($class == ALL_NUMERIC)
65                 $class = 0;
66         if ($class== 0)
67                 $cln = _('All');
68         else
69                 $cln = get_fixed_asset_classname($class);
70
71         if ($location == ALL_TEXT)
72                 $location = 'all';
73         if ($location == 'all')
74                 $loc = _('All');
75         else
76                 $loc = get_location_name($location);
77
78         $cols = array(0, 75, 225, 250, 350, 450,        515);
79
80         $headers = array(_('Class'), '', _('UOM'),  _('Initial'), _('Depreciations'), _('Current'));
81
82         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
83
84     $params =   array(  0 => $comments,
85                                         1 => array('text' => _('End Date'), 'from' => $date,            'to' => ''),
86                                     2 => array('text' => _('Class'), 'from' => $cln, 'to' => ''),
87                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
88
89     $rep = new FrontReport(_('Fixed Assets Valuation Report'), "FixedAssetsValReport", user_pagesize(), 9, $orientation);
90     if ($orientation == 'L')
91         recalculate_cols($cols);
92     $rep->Font();
93     $rep->Info($params, $cols, $headers, $aligns);
94     $rep->NewPage();
95
96         //$res = getTransactions($category, $location, $date);
97         $sql = get_sql_for_fixed_assets(false);
98         $res = db_query($sql,"No transactions were returned");
99         
100         $total = $grandtotal = 0.0;
101         $catt = '';
102         while ($trans=db_fetch($res))
103         {
104                 $loc = find_last_location($trans['stock_id'], $date);
105                 if ($location != 'all' && $location != $loc)
106                         continue;
107                 $purchase = get_fixed_asset_purchase($trans['stock_id']);
108                 $d = sql2date($purchase['tran_date']);
109                 if (date1_greater_date2($d, $date))
110                         continue;
111                 if ($class != 0 && $cln != $trans['description'])
112                         continue;
113                 if ($catt != $trans['description'])
114                 {
115                         if ($catt != '')
116                         {
117                                 if ($detail)
118                                 {
119                                         $rep->NewLine(2, 3);
120                                         $rep->TextCol(0, 4, _('Total'));
121                                 }
122                                 $rep->AmountCol(5, 6, $total, $dec);
123                                 if ($detail)
124                                 {
125                                         $rep->Line($rep->row - 2);
126                                         $rep->NewLine();
127                                 }
128                                 $rep->NewLine();
129                                 $total = 0.0;
130                         }
131                         $rep->TextCol(0, 2, $trans['description']);
132                         $catt = $trans['description'];
133                         if ($detail)
134                                 $rep->NewLine();
135                 }
136                 $UnitCost = $trans['purchase_cost'];
137                 $Depreciation = $trans['purchase_cost'] - $trans['material_cost'];
138                 $Balance = $trans['material_cost'];
139                 if ($detail)
140                 {
141                         $rep->NewLine();
142                         $rep->TextCol(0, 1, $trans['stock_id']);
143                         $rep->TextCol(1, 2, $trans['name']);
144                         $rep->TextCol(2, 3, $trans['units']);
145                         $rep->AmountCol(3, 4, $UnitCost, $dec);
146                         $rep->AmountCol(4, 5, $Depreciation, $dec);
147                         $rep->AmountCol(5, 6, $Balance, $dec);
148                 }
149                 $total += $Balance;
150                 $grandtotal += $Balance;
151         }
152         if ($detail)
153         {
154                 $rep->NewLine(2, 3);
155                 $rep->TextCol(0, 4, _('Total'));
156         }
157         $rep->Amountcol(5, 6, $total, $dec);
158         if ($detail)
159         {
160                 $rep->Line($rep->row - 2);
161                 $rep->NewLine();
162         }
163         $rep->NewLine(2, 1);
164         $rep->TextCol(0, 4, _('Grand Total'));
165         $rep->AmountCol(5, 6, $grandtotal, $dec);
166         $rep->Line($rep->row  - 4);
167         $rep->NewLine();
168     $rep->End();
169 }
170