Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[fa-stable.git] / reporting / rep309.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_SALESANALYTIC';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Chaitanya
16 // date_:       2005-05-19
17 // Title:       Sales Summary Report
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 . "/includes/banking.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_inventory_sales();
31
32 function getTransactions($category, $from, $to)
33 {
34         $from = date2sql($from);
35         $to = date2sql($to);
36         $sql = "SELECT item.category_id,
37                         category.description AS cat_description,
38                         item.stock_id,
39                         item.description,
40                         line.unit_price * trans.rate AS unit_price,
41                         SUM(IF(line.debtor_trans_type = ".ST_CUSTCREDIT.", -line.quantity, line.quantity)) AS quantity
42                 FROM ".TB_PREF."stock_master item,
43                         ".TB_PREF."stock_category category,
44                         ".TB_PREF."debtor_trans trans,
45                         ".TB_PREF."debtor_trans_details line
46                 WHERE line.stock_id = item.stock_id
47                 AND item.category_id=category.category_id
48                 AND line.debtor_trans_type=trans.type
49                 AND line.debtor_trans_no=trans.trans_no
50                 AND trans.tran_date>='$from'
51                 AND trans.tran_date<='$to'
52                 AND line.quantity<>0
53                 AND item.mb_flag <>'F'
54                 AND (line.debtor_trans_type = ".ST_SALESINVOICE." OR line.debtor_trans_type = ".ST_CUSTCREDIT.")";
55                 if ($category != 0)
56                         $sql .= " AND item.category_id = ".db_escape($category);
57                 $sql .= " GROUP BY item.category_id,
58                         category.description,
59                         item.stock_id,
60                         item.description,
61                         line.unit_price
62                 ORDER BY item.category_id, item.stock_id, line.unit_price";
63                         
64         //display_notification($sql);
65         
66     return db_query($sql,"No transactions were returned");
67
68 }
69
70 //----------------------------------------------------------------------------------------------------
71
72 function print_inventory_sales()
73 {
74     global $path_to_root;
75
76         $from = $_POST['PARAM_0'];
77         $to = $_POST['PARAM_1'];
78     $category = $_POST['PARAM_2'];
79         $comments = $_POST['PARAM_3'];
80         $orientation = $_POST['PARAM_4'];
81         $destination = $_POST['PARAM_5'];
82         if ($destination)
83                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
84         else
85                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
86
87         $orientation = ($orientation ? 'L' : 'P');
88     $dec = user_price_dec();
89
90         if ($category == ALL_NUMERIC)
91                 $category = 0;
92         if ($category == 0)
93                 $cat = _('All');
94         else
95                 $cat = get_category_name($category);
96
97         $cols = array(0, 100, 260, 300, 350, 425, 430, 515);
98
99         $headers = array(_('Item/Category'), _('Description'), _('Qty'), _('Unit Price'), _('Sales'), '', _('Remark')); 
100
101         $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'left');
102
103     $params =   array(  0 => $comments,
104                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
105                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''));
106
107     $rep = new FrontReport(_('Item Sales Summary Report'), "ItemSalesSummaryReport", user_pagesize(), 9, $orientation);
108     if ($orientation == 'L')
109         recalculate_cols($cols);
110
111     $rep->Font();
112     $rep->Info($params, $cols, $headers, $aligns);
113     $rep->NewPage();
114
115         $res = getTransactions($category, $from, $to);
116         $total = $grandtotal = 0.0;
117         $catt = '';
118         while ($trans=db_fetch($res))
119         {
120                 if ($catt != $trans['cat_description'])
121                 {
122                         if ($catt != '')
123                         {
124                                 $rep->NewLine(2, 3);
125                                 $rep->TextCol(0, 4, _('Total'));
126                                 $rep->AmountCol(4, 5, $total, $dec);
127                                 $rep->Line($rep->row - 2);
128                                 $rep->NewLine();
129                                 $rep->NewLine();
130                                 $total = 0.0;
131                         }
132                         $rep->TextCol(0, 1, $trans['category_id']);
133                         $rep->TextCol(1, 7, $trans['cat_description']);
134                         $catt = $trans['cat_description'];
135                         $rep->NewLine();
136                 }
137
138                 $rep->NewLine();
139                 $rep->fontSize -= 2;
140                 $rep->TextCol(0, 1, $trans['stock_id']);
141                 $rep->TextCol(1, 2, $trans['description']);
142                 $rep->AmountCol(2, 3, $trans['quantity'], get_qty_dec($trans['stock_id']));
143                 $rep->AmountCol(3, 4, $trans['unit_price'], $dec);
144                 $rep->AmountCol(4, 5, $trans['quantity']*$trans['unit_price'], $dec);
145                 if ($trans['unit_price'] == 0)
146                         $rep->TextCol(6, 7, _('Gift'));
147                 $rep->fontSize += 2;
148                 $total += $trans['quantity']*$trans['unit_price'];
149                 $grandtotal += $trans['quantity']*$trans['unit_price'];
150         }
151         $rep->NewLine(2, 3);
152         $rep->TextCol(0, 4, _('Total'));
153         $rep->AmountCol(4, 5, $total, $dec);
154         $rep->Line($rep->row - 2);
155         $rep->NewLine();
156         $rep->NewLine(2, 1);
157         $rep->TextCol(0, 4, _('Grand Total'));
158         $rep->AmountCol(4, 5, $grandtotal, $dec);
159
160         $rep->Line($rep->row  - 4);
161         $rep->NewLine();
162     $rep->End();
163 }
164