Preparation for Excel Writer continued
[fa-stable.git] / reporting / rep708.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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Trial Balance
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 // trial_inquiry_controls();
29 print_trial_balance();
30
31 //----------------------------------------------------------------------------------------------------
32 function get_balance($account, $dimension, $dimension2, $from, $to, $from_incl=true, $to_incl=true) 
33 {
34         $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) as debit, SUM(IF(amount < 0, -amount, 0)) as credit, SUM(amount) as balance 
35                 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class 
36                 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id 
37                 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
38                 
39         if ($account != null)
40                 $sql .= " account='$account' AND";
41         if ($dimension > 0)
42                 $sql .= " dimension_id=$dimension AND";
43         if ($dimension2 > 0)
44                 $sql .= " dimension2_id=$dimension2 AND";
45         $from_date = date2sql($from);
46         if ($from_incl)
47                 $sql .= " tran_date >= '$from_date'  AND";
48         else
49                 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
50         $to_date = date2sql($to);
51         if ($to_incl)
52                 $sql .= " tran_date <= '$to_date' ";
53         else
54                 $sql .= " tran_date < '$to_date' ";
55
56         $result = db_query($sql,"No general ledger accounts were returned");
57
58         return db_fetch($result);
59 }
60
61 //----------------------------------------------------------------------------------------------------
62
63 function print_trial_balance()
64 {
65         global $path_to_root;
66
67         $dim = get_company_pref('use_dimension');
68         $dimension = $dimension2 = 0;
69
70         $from = $_POST['PARAM_0'];
71         $to = $_POST['PARAM_1'];
72         $zero = $_POST['PARAM_2'];
73         $balances = $_POST['PARAM_3'];
74         if ($dim == 2)
75         {
76                 $dimension = $_POST['PARAM_4'];
77                 $dimension2 = $_POST['PARAM_5'];
78                 $comments = $_POST['PARAM_6'];
79                 $destination = $_POST['PARAM_7'];
80         }
81         else if ($dim == 1)
82         {
83                 $dimension = $_POST['PARAM_4'];
84                 $comments = $_POST['PARAM_5'];
85                 $destination = $_POST['PARAM_6'];
86         }
87         else
88         {
89                 $comments = $_POST['PARAM_4'];
90                 $destination = $_POST['PARAM_5'];
91         }
92         if ($destination)
93         {
94                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
95                 $filename = "TrialBalance.xml";
96         }       
97         else
98         {
99                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
100                 $filename = "TrialBalance.pdf";
101         }
102         $dec = user_price_dec();
103
104         //$cols2 = array(0, 50, 230, 330, 430, 530);
105         $cols2 = array(0, 50, 190, 310, 430, 530);
106         //-------------0--1---2----3----4----5--
107
108         $headers2 = array('', '', _('Brought Forward'), _('This Period'), _('Balance'));
109
110         $aligns2 = array('left', 'left', 'left', 'left', 'left');
111
112         //$cols = array(0, 50, 200, 250, 300,   350, 400, 450, 500,     550);
113         $cols = array(0, 50, 150, 210, 270,     330, 390, 450, 510,     570);
114         //------------0--1---2----3----4----5----6----7----8----9--
115
116         $headers = array(_('Account'), _('Account Name'), _('Debit'), _('Credit'), _('Debit'),
117                 _('Credit'), _('Debit'), _('Credit'));
118
119         $aligns = array('left', 'left', 'right', 'right', 'right', 'right',     'right', 'right');
120
121     if ($dim == 2)
122     {
123         $params =   array(      0 => $comments,
124                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
125                         2 => array('text' => _('Dimension')." 1",
126                             'from' => get_dimension_string($dimension), 'to' => ''),
127                         3 => array('text' => _('Dimension')." 2",
128                             'from' => get_dimension_string($dimension2), 'to' => ''));
129     }
130     else if ($dim == 1)
131     {
132         $params =   array(      0 => $comments,
133                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
134                         2 => array('text' => _('Dimension'),
135                             'from' => get_dimension_string($dimension), 'to' => ''));
136     }
137     else
138     {
139         $params =   array(      0 => $comments,
140                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
141     }
142
143         $rep = new FrontReport(_('Trial Balance'), $filename, user_pagesize());
144
145         $rep->Font();
146         $rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);
147         $rep->Header();
148
149         $accounts = get_gl_accounts();
150
151         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
152         $begin = begin_fiscalyear();
153         if (date1_greater_date2($begin, $from))
154                 $begin = $from;
155         $begin = add_days($begin, -1);
156         while ($account=db_fetch($accounts))
157         {
158                 $prev = get_balance($account["account_code"], $dimension, $dimension2, $begin, $from, false, false);
159                 $curr = get_balance($account["account_code"], $dimension, $dimension2, $from, $to, true, true);
160                 $tot = get_balance($account["account_code"], $dimension, $dimension2, $begin, $to, false, true);
161
162                 if ($zero == 0 && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
163                         continue;
164                 $rep->TextCol(0, 1, $account['account_code']);
165                 $rep->TextCol(1, 2,     $account['account_name']);
166                 if ($balances != 0)
167                 {
168                         if ($prev['balance'] >= 0.0)
169                                 $rep->AmountCol(2, 3, $prev['balance'], $dec);
170                         else
171                                 $rep->AmountCol(3, 4, abs($prev['balance']), $dec);
172                         if ($curr['balance'] >= 0.0)
173                                 $rep->AmountCol(4, 5, $curr['balance'], $dec);
174                         else
175                                 $rep->AmountCol(5, 6, abs($curr['balance']), $dec);
176                         if ($tot['balance'] >= 0.0)
177                                 $rep->AmountCol(6, 7, $tot['balance'], $dec);
178                         else
179                                 $rep->AmountCol(7, 8, abs($tot['balance']), $dec);
180                 }
181                 else
182                 {
183                         $rep->AmountCol(2, 3, $prev['debit'], $dec);
184                         $rep->AmountCol(3, 4, $prev['credit'], $dec);
185                         $rep->AmountCol(4, 5, $curr['debit'], $dec);
186                         $rep->AmountCol(5, 6, $curr['credit'], $dec);
187                         $rep->AmountCol(6, 7, $tot['debit'], $dec);
188                         $rep->AmountCol(7, 8, $tot['credit'], $dec);
189                         $pdeb += $prev['debit'];
190                         $pcre += $prev['credit'];
191                         $cdeb += $curr['debit'];
192                         $ccre += $curr['credit'];
193                         $tdeb += $tot['debit'];
194                         $tcre += $tot['credit'];
195                         
196                 }       
197                 $pbal += $prev['balance'];
198                 $cbal += $curr['balance'];
199                 $tbal += $tot['balance'];
200                 $rep->NewLine();
201
202                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
203                 {
204                         $rep->Line($rep->row - 2);
205                         $rep->Header();
206                 }
207         }
208         $rep->Line($rep->row);
209         $rep->NewLine();
210         $rep->Font('bold');
211
212         //$prev = get_balance(null, $dimension, $dimension2, $begin, $from, false, false);
213         //$curr = get_balance(null, $dimension, $dimension2, $from, $to, true, true);
214         //$tot = get_balance(null, $dimension, $dimension2, $begin, $to, false, true);
215
216         if ($balances == 0)
217         {
218                 $rep->TextCol(0, 2, _("Total"));
219                 $rep->AmountCol(2, 3, $pdeb, $dec);
220                 $rep->AmountCol(3, 4, $pcre, $dec);
221                 $rep->AmountCol(4, 5, $cdeb, $dec);
222                 $rep->AmountCol(5, 6, $ccre, $dec);
223                 $rep->AmountCol(6, 7, $tdeb, $dec);
224                 $rep->AmountCol(7, 8, $tcre, $dec);
225                 $rep->NewLine();
226         }       
227         $rep->TextCol(0, 2, _("Ending Balance"));
228
229         if ($pbal >= 0.0)
230                 $rep->AmountCol(2, 3, $pbal, $dec);
231         else
232                 $rep->AmountCol(3, 4, abs($pbal), $dec);
233         if ($cbal >= 0.0)
234                 $rep->AmountCol(4, 5, $cbal, $dec);
235         else
236                 $rep->AmountCol(5, 6, abs($cbal), $dec);
237         if ($tbal >= 0.0)
238                 $rep->AmountCol(6, 7, $tbal, $dec);
239         else
240                 $rep->AmountCol(7, 8, abs($tbal), $dec);
241         $rep->NewLine();
242         
243         $rep->Line($rep->row);
244         
245         $rep->End();
246 }
247
248 ?>