Moving 2.0 development version to main trunk.
[fa-stable.git] / reporting / rep304.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Inventory Planning
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "includes/banking.inc");
16 include_once($path_to_root . "gl/includes/gl_db.inc");
17 include_once($path_to_root . "inventory/includes/db/items_category_db.inc");
18
19 //----------------------------------------------------------------------------------------------------
20
21 // trial_inquiry_controls();
22 print_inventory_sales();
23
24 function getTransactions($category, $location, $from, $to)
25 {
26         $from = date2sql($from);
27         $to = date2sql($to);
28 /*
29         $sql = "SELECT ".TB_PREF."stock_master.category_id,
30                         ".TB_PREF."stock_category.description AS cat_description,
31                         ".TB_PREF."stock_master.stock_id,
32                         ".TB_PREF."stock_master.description,
33                         ".TB_PREF."stock_moves.loc_code,
34                         SUM(-".TB_PREF."stock_moves.qty*".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent)) AS amt,
35                         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
36                 FROM ".TB_PREF."stock_master,
37                         ".TB_PREF."stock_category,
38                         ".TB_PREF."debtor_trans,
39                         ".TB_PREF."stock_moves
40                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
41                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
42
43                 AND ".TB_PREF."stock_moves.type=".TB_PREF."debtor_trans.type
44                 AND ".TB_PREF."stock_moves.trans_no=".TB_PREF."debtor_trans.trans_no
45                 AND ".TB_PREF."stock_moves.tran_date>='$from'
46                 AND ".TB_PREF."stock_moves.tran_date<='$to'
47                 AND ((".TB_PREF."debtor_trans.type=13 AND ".TB_PREF."debtor_trans.version=1) OR ".TB_PREF."stock_moves.type=11)
48                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
49                 if ($category != 0)
50                         $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
51                 if ($location != 'all')
52                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
53                 //$sql .= " AND SUM(".TB_PREF."stock_moves.qty) != 0
54                 $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
55                         ".TB_PREF."stock_category.description,
56                         ".TB_PREF."stock_master.stock_id,
57                         ".TB_PREF."stock_master.description
58                 ORDER BY ".TB_PREF."stock_master.category_id,
59                         ".TB_PREF."stock_master.stock_id";
60 */
61         $sql = "SELECT ".TB_PREF."stock_master.category_id,
62                         ".TB_PREF."stock_category.description AS cat_description,
63                         ".TB_PREF."stock_master.stock_id,
64                         ".TB_PREF."stock_master.description,
65                         ".TB_PREF."stock_moves.loc_code,
66                         ".TB_PREF."debtor_trans.debtor_no,
67                         ".TB_PREF."stock_moves.tran_date,
68                         -".TB_PREF."stock_moves.qty*".TB_PREF."stock_moves.price*(1-".TB_PREF."stock_moves.discount_percent) AS amt,
69                         -".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
70                 FROM ".TB_PREF."stock_master,
71                         ".TB_PREF."stock_category,
72                         ".TB_PREF."debtor_trans,
73                         ".TB_PREF."stock_moves
74                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
75                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
76
77                 AND ".TB_PREF."stock_moves.type=".TB_PREF."debtor_trans.type
78                 AND ".TB_PREF."stock_moves.trans_no=".TB_PREF."debtor_trans.trans_no
79                 AND ".TB_PREF."stock_moves.tran_date>='$from'
80                 AND ".TB_PREF."stock_moves.tran_date<='$to'
81                 AND ((".TB_PREF."debtor_trans.type=13 AND ".TB_PREF."debtor_trans.version=1) OR ".TB_PREF."stock_moves.type=11)
82                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
83                 if ($category != 0)
84                         $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
85                 if ($location != 'all')
86                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
87                 //$sql .= " AND SUM(".TB_PREF."stock_moves.qty) != 0
88                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
89                         ".TB_PREF."stock_master.stock_id";
90     return db_query($sql,"No transactions were returned");
91
92 }
93
94 //----------------------------------------------------------------------------------------------------
95
96 function print_inventory_sales()
97 {
98     global $path_to_root;
99
100     include_once($path_to_root . "reporting/includes/pdf_report.inc");
101
102         $from = $_POST['PARAM_0'];
103         $to = $_POST['PARAM_1'];
104     $category = $_POST['PARAM_2'];
105     $location = $_POST['PARAM_3'];
106     $detail = $_POST['PARAM_4'];
107         $comments = $_POST['PARAM_5'];
108
109     $dec = user_price_dec();
110
111         if ($category == reserved_words::get_all_numeric())
112                 $category = 0;
113         if ($category == 0)
114                 $cat = _('All');
115         else
116                 $cat = get_category_name($category);
117
118         if ($location == reserved_words::get_all())
119                 $location = 'all';
120         if ($location == 'all')
121                 $loc = _('All');
122         else
123                 $loc = get_location_name($location);
124
125         $cols = array(0, 100, 250, 350, 450,    515);
126
127         $headers = array(_('Category'), '', _('Sales'), _('Cost'), _('Contribution'));
128
129         $aligns = array('left', 'left', 'right', 'right', 'right');
130
131     $params =   array(  0 => $comments,
132                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
133                                     2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
134                                     3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
135
136     $rep = new FrontReport(_('Inventory Sales Report'), "InventorySalesReport.pdf", user_pagesize());
137
138     $rep->Font();
139     $rep->Info($params, $cols, $headers, $aligns);
140     $rep->Header();
141
142         $res = getTransactions($category, $location, $from, $to);
143         $total = $grandtotal = 0.0;
144         $total1 = $grandtotal1 = 0.0;
145         $total2 = $grandtotal2 = 0.0;
146         $amt = $cost = $cb = 0;
147         $catt = $stock_id = $stock_desc = '';
148         while ($trans=db_fetch($res))
149         {
150                 if ($catt != $trans['cat_description'])
151                 {
152                         if ($catt != '')
153                         {
154                                 if ($detail)
155                                 {
156                                         $rep->NewLine(2, 3);
157                                         $rep->TextCol(0, 2, _('Total'));
158                                 }
159                                 $rep->TextCol(2, 3, number_format2($total, $dec));
160                                 $rep->TextCol(3, 4, number_format2($total1, $dec));
161                                 $rep->Textcol(4, 5, number_format2($total2, $dec));
162                                 if ($detail)
163                                 {
164                                         $rep->Line($rep->row - 2);
165                                         $rep->NewLine();
166                                 }
167                                 $rep->NewLine();
168                                 $total = $total1 = $total2 = 0.0;
169                         }
170                         $rep->TextCol(0, 1, $trans['category_id']);
171                         $rep->TextCol(1, 2, $trans['cat_description']);
172                         $catt = $trans['cat_description'];
173                         if ($detail)
174                                 $rep->NewLine();
175                 }
176                 if ($stock_id != $trans['stock_id'])
177                 {
178                         if ($stock_id != '')
179                         {
180                                 if ($detail)
181                                 {
182                                         $rep->NewLine();
183                                         $rep->fontsize -= 2;
184                                         $rep->TextCol(0, 1, $stock_id);
185                                         $rep->TextCol(1, 2, $stock_desc);
186                                         $rep->TextCol(2, 3, number_format2($amt, $dec));
187                                         $rep->TextCol(3, 4, number_format2($cost, $dec));
188                                         $rep->TextCol(4, 5, number_format2($cb, $dec));
189                                         $rep->fontsize += 2;
190                                 }
191                                 $amt = $cost = $cb = 0;
192                         }
193                         $stock_id = $trans['stock_id'];
194                         $stock_desc = $trans['description'];
195                 }
196                 $curr = get_customer_currency($trans['debtor_no']);
197                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
198                 $trans['amt'] *= $rate;
199                 $amt += $trans['amt'];
200                 $cost += $trans['cost'];
201                 $cb1 = $trans['amt'] - $trans['cost'];
202                 $cb += $cb1;
203                 $total += $trans['amt'];
204                 $total1 += $trans['cost'];
205                 $total2 += $cb1;
206                 $grandtotal += $trans['amt'];
207                 $grandtotal1 += $trans['cost'];
208                 $grandtotal2 += $cb1;
209         }
210         if ($detail)
211         {
212                 $rep->NewLine();
213                 $rep->fontsize -= 2;
214                 $rep->TextCol(0, 1, $stock_id);
215                 $rep->TextCol(1, 2, $stock_desc);
216                 $rep->TextCol(2, 3, number_format2($amt, $dec));
217                 $rep->TextCol(3, 4, number_format2($cost, $dec));
218                 $rep->TextCol(4, 5, number_format2($cb, $dec));
219                 $rep->fontsize += 2;
220
221                 $rep->NewLine(2, 3);
222                 $rep->TextCol(0, 2, _('Total'));
223         }
224         $rep->TextCol(2, 3, number_format2($total, $dec));
225         $rep->TextCol(3, 4, number_format2($total1, $dec));
226         $rep->Textcol(4, 5, number_format2($total2, $dec));
227         if ($detail)
228         {
229                 $rep->Line($rep->row - 2);
230                 $rep->NewLine();
231         }
232         $rep->NewLine(2, 1);
233         $rep->TextCol(0, 2, _('Grand Total'));
234         $rep->TextCol(2, 3, number_format2($grandtotal, $dec));
235         $rep->TextCol(3, 4, number_format2($grandtotal1, $dec));
236         $rep->Textcol(4, 5, number_format2($grandtotal2, $dec));
237
238         $rep->Line($rep->row  - 4);
239     $rep->End();
240 }
241
242 ?>