1bb9ba06a7df49c8abd83794b3da93cd48c800a0
[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(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 line.debtor_trans_type = ".ST_SALESINVOICE;
54                 if ($category != 0)
55                         $sql .= " AND item.category_id = ".db_escape($category);
56                 $sql .= " GROUP BY item.category_id,
57                         category.description,
58                         item.stock_id,
59                         item.description,
60                         line.unit_price
61                 ORDER BY item.category_id, item.stock_id, line.unit_price";
62                         
63         //display_notification($sql);
64         
65     return db_query($sql,"No transactions were returned");
66
67 }
68
69 //----------------------------------------------------------------------------------------------------
70
71 function print_inventory_sales()
72 {
73     global $path_to_root;
74
75         $from = $_POST['PARAM_0'];
76         $to = $_POST['PARAM_1'];
77     $category = $_POST['PARAM_2'];
78         $comments = $_POST['PARAM_3'];
79         $orientation = $_POST['PARAM_4'];
80         $destination = $_POST['PARAM_5'];
81         if ($destination)
82                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
83         else
84                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
85
86         $orientation = ($orientation ? 'L' : 'P');
87     $dec = user_price_dec();
88
89         if ($category == ALL_NUMERIC)
90                 $category = 0;
91         if ($category == 0)
92                 $cat = _('All');
93         else
94                 $cat = get_category_name($category);
95
96         $cols = array(0, 100, 260, 300, 350, 425, 430, 515);
97
98         $headers = array(_('Item/Category'), _('Description'), _('Qty'), _('Unit Price'), _('Sales'), '', _('Remark')); 
99
100         $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'left');
101
102     $params =   array(  0 => $comments,
103                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
104                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''));
105
106     $rep = new FrontReport(_('Item Sales Summary Report'), "ItemSalesSummaryReport", user_pagesize(), 9, $orientation);
107     if ($orientation == 'L')
108         recalculate_cols($cols);
109
110     $rep->Font();
111     $rep->Info($params, $cols, $headers, $aligns);
112     $rep->NewPage();
113
114         $res = getTransactions($category, $from, $to);
115         $total = $grandtotal = 0.0;
116         $total1 = $grandtotal1 = 0.0;
117         $total2 = $grandtotal2 = 0.0;
118         $catt = '';
119         while ($trans=db_fetch($res))
120         {
121                 if ($catt != $trans['cat_description'])
122                 {
123                         if ($catt != '')
124                         {
125                                 $rep->NewLine(2, 3);
126                                 $rep->TextCol(0, 4, _('Total'));
127                                 $rep->AmountCol(4, 5, $total, $dec);
128                                 $rep->Line($rep->row - 2);
129                                 $rep->NewLine();
130                                 $rep->NewLine();
131                                 $total = $total1 = $total2 = 0.0;
132                         }
133                         $rep->TextCol(0, 1, $trans['category_id']);
134                         $rep->TextCol(1, 7, $trans['cat_description']);
135                         $catt = $trans['cat_description'];
136                         $rep->NewLine();
137                 }
138
139                 $rep->NewLine();
140                 $rep->fontSize -= 2;
141                 $rep->TextCol(0, 1, $trans['stock_id']);
142                 $rep->TextCol(1, 2, $trans['description']);
143                 $rep->AmountCol(2, 3, $trans['quantity'], get_qty_dec($trans['stock_id']));
144                 $rep->AmountCol(3, 4, $trans['unit_price'], $dec);
145                 $rep->AmountCol(4, 5, $trans['quantity']*$trans['unit_price'], $dec);
146                 if ($trans['unit_price'] == 0)
147                         $rep->TextCol(6, 7, _('Gift'));
148                 $rep->fontSize += 2;
149                 $total += $trans['quantity']*$trans['unit_price'];
150                 $grandtotal += $trans['quantity']*$trans['unit_price'];
151         }
152         $rep->NewLine(2, 3);
153         $rep->TextCol(0, 4, _('Total'));
154         $rep->AmountCol(4, 5, $total, $dec);
155         $rep->Line($rep->row - 2);
156         $rep->NewLine();
157         $rep->NewLine(2, 1);
158         $rep->TextCol(0, 4, _('Grand Total'));
159         $rep->AmountCol(4, 5, $grandtotal, $dec);
160
161         $rep->Line($rep->row  - 4);
162         $rep->NewLine();
163     $rep->End();
164 }
165
166 ?>