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