Merging latest changes from stable branch up to 2.3.24
[fa-stable.git] / reporting / rep304.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:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Sales 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, $location, $fromcust, $from, $to, $show_service)
33 {
34         $from = date2sql($from);
35         $to = date2sql($to);
36 <<<<<<< HEAD
37         $sql = "SELECT item.category_id,
38                         category.description AS cat_description,
39                         item.stock_id,
40                         item.description, item.inactive,
41                         item.mb_flag,
42                         move.loc_code,
43                         trans.debtor_no,
44                         debtor.name AS debtor_name,
45                         move.tran_date,
46                         SUM(-move.qty) AS qty,
47                         SUM(-move.qty*move.price) AS amt,
48                         SUM(-IF(move.standard_cost <> 0, move.qty * move.standard_cost, move.qty *(item.material_cost + item.labour_cost + item.overhead_cost))) AS cost
49                 FROM ".TB_PREF."stock_master item,
50                         ".TB_PREF."stock_category category,
51                         ".TB_PREF."debtor_trans trans,
52                         ".TB_PREF."debtors_master debtor,
53                         ".TB_PREF."stock_moves move
54                 WHERE item.stock_id=move.stock_id
55                 AND item.category_id=category.category_id
56                 AND trans.debtor_no=debtor.debtor_no
57                 AND move.type=trans.type
58                 AND move.trans_no=trans.trans_no
59                 AND move.tran_date>='$from'
60                 AND move.tran_date<='$to'
61                 AND (trans.type=".ST_CUSTDELIVERY." OR move.type=".ST_CUSTCREDIT.")";
62
63         if (!$show_service)
64                 $sql .= " AND (item.mb_flag='B' OR item.mb_flag='M')";
65
66         if ($category != 0)
67                 $sql .= " AND item.category_id = ".db_escape($category);
68
69         if ($location != '')
70                 $sql .= " AND move.loc_code = ".db_escape($location);
71
72         if ($fromcust != '')
73                 $sql .= " AND debtor.debtor_no = ".db_escape($fromcust);
74
75         $sql .= " GROUP BY item.stock_id, debtor.name ORDER BY item.category_id,
76                 item.stock_id, debtor.name";
77
78     return db_query($sql,"No transactions were returned");
79
80 }
81
82 //----------------------------------------------------------------------------------------------------
83
84 function print_inventory_sales()
85 {
86     global $path_to_root;
87
88         $from = $_POST['PARAM_0'];
89         $to = $_POST['PARAM_1'];
90     $category = $_POST['PARAM_2'];
91     $location = $_POST['PARAM_3'];
92     $fromcust = $_POST['PARAM_4'];
93         $show_service = $_POST['PARAM_5'];
94         $comments = $_POST['PARAM_6'];
95         $orientation = $_POST['PARAM_7'];
96         $destination = $_POST['PARAM_8'];
97         if ($destination)
98                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
99         else
100                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
101
102         $orientation = ($orientation ? 'L' : 'P');
103     $dec = user_price_dec();
104
105         if ($category == ALL_NUMERIC)
106                 $category = 0;
107         if ($category == 0)
108                 $cat = _('All');
109         else
110                 $cat = get_category_name($category);
111
112         if ($location == '')
113                 $loc = _('All');
114         else
115                 $loc = get_location_name($location);
116
117         if ($fromcust == '')
118                 $fromc = _('All');
119         else
120                 $fromc = get_customer_name($fromcust);
121         if ($show_service) $show_service_items = _('Yes');
122         else $show_service_items = _('No');
123
124         $cols = array(0, 75, 175, 250, 300, 375, 450,   515);
125
126         $headers = array(_('Category'), _('Description'), _('Customer'), _('Qty'), _('Sales'), _('Cost'), _('Contribution'));
127         if ($fromcust != '')
128                 $headers[2] = '';
129
130         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
131
132     $params =   array(  0 => $comments,
133                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
134                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
135                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
136                                     4 => array('text' => _('Customer'), 'from' => $fromc, 'to' => ''),
137                                     5 => array('text' => _('Show Service Items'), 'from' => $show_service_items, 'to' => ''));
138
139     $rep = new FrontReport(_('Inventory Sales Report'), "InventorySalesReport", user_pagesize(), 9, $orientation);
140         if ($orientation == 'L')
141         recalculate_cols($cols);
142
143     $rep->Font();
144     $rep->Info($params, $cols, $headers, $aligns);
145     $rep->NewPage();
146
147         $res = getTransactions($category, $location, $fromcust, $from, $to, $show_service);
148         $total = $grandtotal = 0.0;
149         $total1 = $grandtotal1 = 0.0;
150         $total2 = $grandtotal2 = 0.0;
151         $catt = '';
152         while ($trans=db_fetch($res))
153         {
154                 if ($catt != $trans['cat_description'])
155                 {
156                         if ($catt != '')
157                         {
158                                 $rep->NewLine(2, 3);
159                                 $rep->TextCol(0, 4, _('Total'));
160                                 $rep->AmountCol(4, 5, $total, $dec);
161                                 $rep->AmountCol(5, 6, $total1, $dec);
162                                 $rep->AmountCol(6, 7, $total2, $dec);
163                                 $rep->Line($rep->row - 2);
164                                 $rep->NewLine();
165                                 $rep->NewLine();
166                                 $total = $total1 = $total2 = 0.0;
167                         }
168                         $rep->TextCol(0, 1, $trans['category_id']);
169                         $rep->TextCol(1, 6, $trans['cat_description']);
170                         $catt = $trans['cat_description'];
171                         $rep->NewLine();
172                 }
173
174                 $curr = get_customer_currency($trans['debtor_no']);
175                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
176                 $trans['amt'] *= $rate;
177                 $cb = $trans['amt'] - $trans['cost'];
178                 $rep->NewLine();
179                 $rep->fontSize -= 2;
180                 $rep->TextCol(0, 1, $trans['stock_id']);
181                 if ($fromcust == ALL_TEXT)
182                 {
183                         $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
184                         $rep->TextCol(2, 3, $trans['debtor_name']);
185                 }
186                 else
187                         $rep->TextCol(1, 3, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
188                 $rep->AmountCol(3, 4, $trans['qty'], get_qty_dec($trans['stock_id']));
189                 $rep->AmountCol(4, 5, $trans['amt'], $dec);
190                 if (is_service($trans['mb_flag']))
191                         $rep->TextCol(5, 6, "---");
192                 else    
193                         $rep->AmountCol(5, 6, $trans['cost'], $dec);
194                 $rep->AmountCol(6, 7, $cb, $dec);
195                 $rep->fontSize += 2;
196                 $total += $trans['amt'];
197                 $total1 += $trans['cost'];
198                 $total2 += $cb;
199                 $grandtotal += $trans['amt'];
200                 $grandtotal1 += $trans['cost'];
201                 $grandtotal2 += $cb;
202         }
203         $rep->NewLine(2, 3);
204         $rep->TextCol(0, 4, _('Total'));
205         $rep->AmountCol(4, 5, $total, $dec);
206         $rep->AmountCol(5, 6, $total1, $dec);
207         $rep->AmountCol(6, 7, $total2, $dec);
208         $rep->Line($rep->row - 2);
209         $rep->NewLine();
210         $rep->NewLine(2, 1);
211         $rep->TextCol(0, 4, _('Grand Total'));
212         $rep->AmountCol(4, 5, $grandtotal, $dec);
213         $rep->AmountCol(5, 6, $grandtotal1, $dec);
214         $rep->AmountCol(6, 7, $grandtotal2, $dec);
215
216         $rep->Line($rep->row  - 4);
217         $rep->NewLine();
218     $rep->End();
219 }