[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[fa-stable.git] / reporting / rep501.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_DIMENSIONREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Dimension Summary
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
26 //----------------------------------------------------------------------------------------------------
27
28 print_dimension_summary();
29
30 function getTransactions($from, $to)
31 {
32         $sql = "SELECT *
33                 FROM
34                         ".TB_PREF."dimensions
35                 WHERE id >= ".db_escape($from)."
36                 AND id <= ".db_escape($to)."
37                 ORDER BY
38                         reference";
39
40     return db_query($sql,"No transactions were returned");
41 }
42
43 function getYTD($dim)
44 {
45         $date = Today();
46         $date = begin_fiscalyear($date);
47         date2sql($date);
48         
49         $sql = "SELECT SUM(amount) AS Balance
50                 FROM
51                         ".TB_PREF."gl_trans
52                 WHERE (dimension_id = '$dim' OR dimension2_id = '$dim')
53                 AND tran_date >= '$date'";
54
55     $TransResult = db_query($sql,"No transactions were returned");
56         if (db_num_rows($TransResult) == 1)
57         {
58                 $DemandRow = db_fetch_row($TransResult);
59                 $balance = $DemandRow[0];
60         }
61         else
62                 $balance = 0.0;
63
64     return $balance;
65 }
66
67 //----------------------------------------------------------------------------------------------------
68
69 function print_dimension_summary()
70 {
71     global $path_to_root;
72
73     $fromdim = $_POST['PARAM_0'];
74     $todim = $_POST['PARAM_1'];
75     $showbal = $_POST['PARAM_2'];
76     $comments = $_POST['PARAM_3'];
77         $orientation = $_POST['PARAM_4'];
78         $destination = $_POST['PARAM_5'];
79         if ($destination)
80                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
81         else
82                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
83
84         $orientation = ($orientation ? 'L' : 'P');
85         $cols = array(0, 50, 210, 250, 320, 395, 465,   515);
86
87         $headers = array(_('Reference'), _('Name'), _('Type'), _('Date'), _('Due Date'), _('Closed'), _('YTD'));
88
89         $aligns = array('left', 'left', 'left', 'left', 'left', 'left', 'right');
90
91     $params =   array(  0 => $comments,
92                                     1 => array('text' => _('Dimension'), 'from' => get_dimension_string($fromdim), 'to' => get_dimension_string($todim)));
93
94     $rep = new FrontReport(_('Dimension Summary'), "DimensionSummary", user_pagesize(), 9, $orientation);
95     if ($orientation == 'L')
96         recalculate_cols($cols);
97
98     $rep->Font();
99     $rep->Info($params, $cols, $headers, $aligns);
100     $rep->NewPage();
101
102         $res = getTransactions($fromdim, $todim);
103         while ($trans=db_fetch($res))
104         {
105                 $rep->TextCol(0, 1, $trans['reference']);
106                 $rep->TextCol(1, 2, $trans['name']);
107                 $rep->TextCol(2, 3, $trans['type_']);
108                 $rep->DateCol(3, 4, $trans['date_'], true);
109                 $rep->DateCol(4, 5, $trans['due_date'], true);
110                 if ($trans['closed'])
111                         $str = _('Yes');
112                 else
113                         $str = _('No');
114                 $rep->TextCol(5, 6, $str);
115                 if ($showbal)
116                 {
117                         $balance = getYTD($trans['id']);
118                         $rep->AmountCol(6, 7, $balance, 0);
119                 }       
120                 $rep->NewLine(1, 2);
121         }
122         $rep->Line($rep->row);
123     $rep->End();
124 }
125