Merging version 2.1 RC to main trunk.
[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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Inventory Planning
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, $from, $to)
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,
40                         ".TB_PREF."stock_moves.loc_code,
41                         ".TB_PREF."debtor_trans.debtor_no,
42                         ".TB_PREF."stock_moves.tran_date,
43                         -".TB_PREF."stock_moves.qty*".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent) AS amt,
44                         -".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
45                 FROM ".TB_PREF."stock_master,
46                         ".TB_PREF."stock_category,
47                         ".TB_PREF."debtor_trans,
48                         ".TB_PREF."stock_moves
49                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
50                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
51
52                 AND ".TB_PREF."stock_moves.type=".TB_PREF."debtor_trans.type
53                 AND ".TB_PREF."stock_moves.trans_no=".TB_PREF."debtor_trans.trans_no
54                 AND ".TB_PREF."stock_moves.tran_date>='$from'
55                 AND ".TB_PREF."stock_moves.tran_date<='$to'
56                 AND ((".TB_PREF."debtor_trans.type=13 AND ".TB_PREF."debtor_trans.version=1) OR ".TB_PREF."stock_moves.type=11)
57                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
58                 if ($category != 0)
59                         $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
60                 if ($location != 'all')
61                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
62                 //$sql .= " AND SUM(".TB_PREF."stock_moves.qty) != 0
63                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
64                         ".TB_PREF."stock_master.stock_id";
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     $location = $_POST['PARAM_3'];
79     $detail = $_POST['PARAM_4'];
80         $comments = $_POST['PARAM_5'];
81         $destination = $_POST['PARAM_6'];
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     $dec = user_price_dec();
88
89         if ($category == reserved_words::get_all_numeric())
90                 $category = 0;
91         if ($category == 0)
92                 $cat = _('All');
93         else
94                 $cat = get_category_name($category);
95
96         if ($location == reserved_words::get_all())
97                 $location = 'all';
98         if ($location == 'all')
99                 $loc = _('All');
100         else
101                 $loc = get_location_name($location);
102
103         $cols = array(0, 100, 250, 350, 450,    515);
104
105         $headers = array(_('Category'), '', _('Sales'), _('Cost'), _('Contribution'));
106
107         $aligns = array('left', 'left', 'right', 'right', 'right');
108
109     $params =   array(  0 => $comments,
110                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
111                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
112                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
113
114     $rep = new FrontReport(_('Inventory Sales Report'), "InventorySalesReport", user_pagesize());
115
116     $rep->Font();
117     $rep->Info($params, $cols, $headers, $aligns);
118     $rep->Header();
119
120         $res = getTransactions($category, $location, $from, $to);
121         $total = $grandtotal = 0.0;
122         $total1 = $grandtotal1 = 0.0;
123         $total2 = $grandtotal2 = 0.0;
124         $amt = $cost = $cb = 0;
125         $catt = $stock_id = $stock_desc = '';
126         while ($trans=db_fetch($res))
127         {
128                 if ($catt != $trans['cat_description'])
129                 {
130                         if ($catt != '')
131                         {
132                                 if ($detail)
133                                 {
134                                         $rep->NewLine(2, 3);
135                                         $rep->TextCol(0, 2, _('Total'));
136                                 }
137                                 $rep->AmountCol(2, 3, $total, $dec);
138                                 $rep->AmountCol(3, 4, $total1, $dec);
139                                 $rep->AmountCol(4, 5, $total2, $dec);
140                                 if ($detail)
141                                 {
142                                         $rep->Line($rep->row - 2);
143                                         $rep->NewLine();
144                                 }
145                                 $rep->NewLine();
146                                 $total = $total1 = $total2 = 0.0;
147                         }
148                         $rep->TextCol(0, 1, $trans['category_id']);
149                         $rep->TextCol(1, 2, $trans['cat_description']);
150                         $catt = $trans['cat_description'];
151                         if ($detail)
152                                 $rep->NewLine();
153                 }
154                 if ($stock_id != $trans['stock_id'])
155                 {
156                         if ($stock_id != '')
157                         {
158                                 if ($detail)
159                                 {
160                                         $rep->NewLine();
161                                         $rep->fontsize -= 2;
162                                         $rep->TextCol(0, 1, $stock_id);
163                                         $rep->TextCol(1, 2, $stock_desc);
164                                         $rep->AmountCol(2, 3, $amt, $dec);
165                                         $rep->AmountCol(3, 4, $cost, $dec);
166                                         $rep->AmountCol(4, 5, $cb, $dec);
167                                         $rep->fontsize += 2;
168                                 }
169                                 $amt = $cost = $cb = 0;
170                         }
171                         $stock_id = $trans['stock_id'];
172                         $stock_desc = $trans['description'];
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                 $amt += $trans['amt'];
178                 $cost += $trans['cost'];
179                 $cb1 = $trans['amt'] - $trans['cost'];
180                 $cb += $cb1;
181                 $total += $trans['amt'];
182                 $total1 += $trans['cost'];
183                 $total2 += $cb1;
184                 $grandtotal += $trans['amt'];
185                 $grandtotal1 += $trans['cost'];
186                 $grandtotal2 += $cb1;
187         }
188         if ($detail)
189         {
190                 $rep->NewLine();
191                 $rep->fontsize -= 2;
192                 $rep->TextCol(0, 1, $stock_id);
193                 $rep->TextCol(1, 2, $stock_desc);
194                 $rep->AmountCol(2, 3, $amt, $dec);
195                 $rep->AmountCol(3, 4, $cost, $dec);
196                 $rep->AmountCol(4, 5, $cb, $dec);
197                 $rep->fontsize += 2;
198
199                 $rep->NewLine(2, 3);
200                 $rep->TextCol(0, 2, _('Total'));
201         }
202         $rep->AmountCol(2, 3, $total, $dec);
203         $rep->AmountCol(3, 4, $total1, $dec);
204         $rep->AmountCol(4, 5, $total2, $dec);
205         if ($detail)
206         {
207                 $rep->Line($rep->row - 2);
208                 $rep->NewLine();
209         }
210         $rep->NewLine(2, 1);
211         $rep->TextCol(0, 2, _('Grand Total'));
212         $rep->AmountCol(2, 3, $grandtotal, $dec);
213         $rep->AmountCol(3, 4, $grandtotal1, $dec);
214         $rep->AmountCol(4, 5, $grandtotal2, $dec);
215
216         $rep->Line($rep->row  - 4);
217         $rep->NewLine();
218     $rep->End();
219 }
220
221 ?>