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