Preparation for Excel Writer continued
[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 // trial_inquiry_controls();
31 print_inventory_sales();
32
33 function getTransactions($category, $location, $from, $to)
34 {
35         $from = date2sql($from);
36         $to = date2sql($to);
37 /*
38         $sql = "SELECT ".TB_PREF."stock_master.category_id,
39                         ".TB_PREF."stock_category.description AS cat_description,
40                         ".TB_PREF."stock_master.stock_id,
41                         ".TB_PREF."stock_master.description,
42                         ".TB_PREF."stock_moves.loc_code,
43                         SUM(-".TB_PREF."stock_moves.qty*".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent)) AS amt,
44                         SUM(-".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 .= " GROUP BY ".TB_PREF."stock_master.category_id,
64                         ".TB_PREF."stock_category.description,
65                         ".TB_PREF."stock_master.stock_id,
66                         ".TB_PREF."stock_master.description
67                 ORDER BY ".TB_PREF."stock_master.category_id,
68                         ".TB_PREF."stock_master.stock_id";
69 */
70         $sql = "SELECT ".TB_PREF."stock_master.category_id,
71                         ".TB_PREF."stock_category.description AS cat_description,
72                         ".TB_PREF."stock_master.stock_id,
73                         ".TB_PREF."stock_master.description,
74                         ".TB_PREF."stock_moves.loc_code,
75                         ".TB_PREF."debtor_trans.debtor_no,
76                         ".TB_PREF."stock_moves.tran_date,
77                         -".TB_PREF."stock_moves.qty*".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent) AS amt,
78                         -".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
79                 FROM ".TB_PREF."stock_master,
80                         ".TB_PREF."stock_category,
81                         ".TB_PREF."debtor_trans,
82                         ".TB_PREF."stock_moves
83                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
84                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
85
86                 AND ".TB_PREF."stock_moves.type=".TB_PREF."debtor_trans.type
87                 AND ".TB_PREF."stock_moves.trans_no=".TB_PREF."debtor_trans.trans_no
88                 AND ".TB_PREF."stock_moves.tran_date>='$from'
89                 AND ".TB_PREF."stock_moves.tran_date<='$to'
90                 AND ((".TB_PREF."debtor_trans.type=13 AND ".TB_PREF."debtor_trans.version=1) OR ".TB_PREF."stock_moves.type=11)
91                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
92                 if ($category != 0)
93                         $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
94                 if ($location != 'all')
95                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
96                 //$sql .= " AND SUM(".TB_PREF."stock_moves.qty) != 0
97                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
98                         ".TB_PREF."stock_master.stock_id";
99     return db_query($sql,"No transactions were returned");
100
101 }
102
103 //----------------------------------------------------------------------------------------------------
104
105 function print_inventory_sales()
106 {
107     global $path_to_root;
108
109         $from = $_POST['PARAM_0'];
110         $to = $_POST['PARAM_1'];
111     $category = $_POST['PARAM_2'];
112     $location = $_POST['PARAM_3'];
113     $detail = $_POST['PARAM_4'];
114         $comments = $_POST['PARAM_5'];
115         $destination = $_POST['PARAM_6'];
116         if ($destination)
117         {
118                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
119                 $filename = "InventorySalesReport.xml";
120         }       
121         else
122         {
123                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
124                 $filename = "InventorySalesReport.pdf";
125         }
126
127     $dec = user_price_dec();
128
129         if ($category == reserved_words::get_all_numeric())
130                 $category = 0;
131         if ($category == 0)
132                 $cat = _('All');
133         else
134                 $cat = get_category_name($category);
135
136         if ($location == reserved_words::get_all())
137                 $location = 'all';
138         if ($location == 'all')
139                 $loc = _('All');
140         else
141                 $loc = get_location_name($location);
142
143         $cols = array(0, 100, 250, 350, 450,    515);
144
145         $headers = array(_('Category'), '', _('Sales'), _('Cost'), _('Contribution'));
146
147         $aligns = array('left', 'left', 'right', 'right', 'right');
148
149     $params =   array(  0 => $comments,
150                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
151                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
152                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
153
154     $rep = new FrontReport(_('Inventory Sales Report'), "InventorySalesReport.pdf", user_pagesize());
155
156     $rep->Font();
157     $rep->Info($params, $cols, $headers, $aligns);
158     $rep->Header();
159
160         $res = getTransactions($category, $location, $from, $to);
161         $total = $grandtotal = 0.0;
162         $total1 = $grandtotal1 = 0.0;
163         $total2 = $grandtotal2 = 0.0;
164         $amt = $cost = $cb = 0;
165         $catt = $stock_id = $stock_desc = '';
166         while ($trans=db_fetch($res))
167         {
168                 if ($catt != $trans['cat_description'])
169                 {
170                         if ($catt != '')
171                         {
172                                 if ($detail)
173                                 {
174                                         $rep->NewLine(2, 3);
175                                         $rep->TextCol(0, 2, _('Total'));
176                                 }
177                                 $rep->AmountCol(2, 3, $total, $dec);
178                                 $rep->AmountCol(3, 4, $total1, $dec);
179                                 $rep->AmountCol(4, 5, $total2, $dec);
180                                 if ($detail)
181                                 {
182                                         $rep->Line($rep->row - 2);
183                                         $rep->NewLine();
184                                 }
185                                 $rep->NewLine();
186                                 $total = $total1 = $total2 = 0.0;
187                         }
188                         $rep->TextCol(0, 1, $trans['category_id']);
189                         $rep->TextCol(1, 2, $trans['cat_description']);
190                         $catt = $trans['cat_description'];
191                         if ($detail)
192                                 $rep->NewLine();
193                 }
194                 if ($stock_id != $trans['stock_id'])
195                 {
196                         if ($stock_id != '')
197                         {
198                                 if ($detail)
199                                 {
200                                         $rep->NewLine();
201                                         $rep->fontsize -= 2;
202                                         $rep->TextCol(0, 1, $stock_id);
203                                         $rep->TextCol(1, 2, $stock_desc);
204                                         $rep->AmountCol(2, 3, $amt, $dec);
205                                         $rep->AmountCol(3, 4, $cost, $dec);
206                                         $rep->AmountCol(4, 5, $cb, $dec);
207                                         $rep->fontsize += 2;
208                                 }
209                                 $amt = $cost = $cb = 0;
210                         }
211                         $stock_id = $trans['stock_id'];
212                         $stock_desc = $trans['description'];
213                 }
214                 $curr = get_customer_currency($trans['debtor_no']);
215                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
216                 $trans['amt'] *= $rate;
217                 $amt += $trans['amt'];
218                 $cost += $trans['cost'];
219                 $cb1 = $trans['amt'] - $trans['cost'];
220                 $cb += $cb1;
221                 $total += $trans['amt'];
222                 $total1 += $trans['cost'];
223                 $total2 += $cb1;
224                 $grandtotal += $trans['amt'];
225                 $grandtotal1 += $trans['cost'];
226                 $grandtotal2 += $cb1;
227         }
228         if ($detail)
229         {
230                 $rep->NewLine();
231                 $rep->fontsize -= 2;
232                 $rep->TextCol(0, 1, $stock_id);
233                 $rep->TextCol(1, 2, $stock_desc);
234                 $rep->AmountCol(2, 3, $amt, $dec);
235                 $rep->AmountCol(3, 4, $cost, $dec);
236                 $rep->AmountCol(4, 5, $cb, $dec);
237                 $rep->fontsize += 2;
238
239                 $rep->NewLine(2, 3);
240                 $rep->TextCol(0, 2, _('Total'));
241         }
242         $rep->AmountCol(2, 3, $total, $dec);
243         $rep->AmountCol(3, 4, $total1, $dec);
244         $rep->AmountCol(4, 5, $total2, $dec);
245         if ($detail)
246         {
247                 $rep->Line($rep->row - 2);
248                 $rep->NewLine();
249         }
250         $rep->NewLine(2, 1);
251         $rep->TextCol(0, 2, _('Grand Total'));
252         $rep->AmountCol(2, 3, $grandtotal, $dec);
253         $rep->AmountCol(3, 4, $grandtotal1, $dec);
254         $rep->AmountCol(4, 5, $grandtotal2, $dec);
255
256         $rep->Line($rep->row  - 4);
257         $rep->NewLine();
258     $rep->End();
259 }
260
261 ?>