56ec99cb068a5f63328d04fecd0cfcfc44cb127a
[fa-stable.git] / reporting / rep305.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_grn_valuation();
31
32 function getTransactions($from, $to)
33 {
34         $from = date2sql($from);
35         $to = date2sql($to);
36         $sql = "SELECT ".TB_PREF."grn_batch.delivery_date, ".TB_PREF."grn_batch.supplier_id, 
37                         ".TB_PREF."purch_order_details.*,
38                         ".TB_PREF."stock_master.description
39                 FROM ".TB_PREF."stock_master,
40                         ".TB_PREF."purch_order_details,
41                         ".TB_PREF."grn_batch
42                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."purch_order_details.item_code
43                 AND ".TB_PREF."grn_batch.purch_order_no=".TB_PREF."purch_order_details.order_no
44                 AND ".TB_PREF."purch_order_details.quantity_received>0
45                 AND ".TB_PREF."grn_batch.delivery_date>='$from'
46                 AND ".TB_PREF."grn_batch.delivery_date<='$to'
47                 ORDER BY ".TB_PREF."stock_master.stock_id, ".TB_PREF."grn_batch.delivery_date";
48     return db_query($sql,"No transactions were returned");
49
50 }
51
52 //----------------------------------------------------------------------------------------------------
53
54 function print_grn_valuation()
55 {
56     global $path_to_root;
57
58         $from = $_POST['PARAM_0'];
59         $to = $_POST['PARAM_1'];
60         $comments = $_POST['PARAM_2'];
61         $destination = $_POST['PARAM_3'];
62         if ($destination)
63                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
64         else
65                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
66
67     $dec = user_price_dec();
68
69         $cols = array(0, 75, 225, 275, 345, 390, 445,   515);
70         $headers = array(_('Stock ID'), _('Description'), _('PO No'), _('Qty Received'), _('Unit Price'), _('Actual Price'), _('Total'));
71
72         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
73
74     $params =   array(  0 => $comments,
75                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
76
77     $rep = new FrontReport(_('GRN Valuation Report'), "GRNValuationReport", user_pagesize());
78
79     $rep->Font();
80     $rep->Info($params, $cols, $headers, $aligns);
81     $rep->Header();
82
83         $res = getTransactions($from, $to);
84         $total = $qtotal = $grandtotal = 0.0;
85         $stock_id = '';
86         while ($trans=db_fetch($res))
87         {
88                 if ($stock_id != $trans['item_code'])
89                 {
90                         if ($stock_id != '')
91                         {
92                                 $rep->Line($rep->row  - 4);
93                                 $rep->NewLine(2);
94                                 $rep->TextCol(0, 3, _('Total'));
95                                 $rep->AmountCol(3, 4, $qtotal, $qdec);
96                                 $rep->AmountCol(6, 7, $total, $dec);
97                                 $rep->NewLine();
98                                 $total = $qtotal = 0;
99                         }
100                         $stock_id = $trans['item_code'];
101                 }
102                 $curr = get_supplier_currency($trans['supplier_id']);
103                 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['delivery_date']));
104                 $trans['unit_price'] *= $rate;
105                 $trans['act_price'] *= $rate;
106
107                 $rep->NewLine();
108                 $rep->TextCol(0, 1, $trans['item_code']);
109                 $rep->TextCol(1, 2, $trans['description']);
110                 $rep->TextCol(2, 3, $trans['order_no']);
111                 $qdec = get_qty_dec($trans['item_code']);
112                 $rep->AmountCol(3, 4, $trans['quantity_received'], $qdec);
113                 $rep->AmountCol(4, 5, $trans['unit_price'], $dec);
114                 $rep->AmountCol(5, 6, $trans['act_price'], $dec);
115                 $amt = round2($trans['quantity_received'] * $trans['act_price'], $dec);
116                 $rep->AmountCol(6, 7, $amt, $dec);
117                 $total += $amt;
118                 $qtotal += $trans['quantity_received'];
119                 $grandtotal += $amt;
120         }
121         if ($stock_id != '')
122         {
123                 $rep->Line($rep->row  - 4);
124                 $rep->NewLine(2);
125                 $rep->TextCol(0, 3, _('Total'));
126                 $rep->AmountCol(3, 4, $qtotal, $qdec);
127                 $rep->AmountCol(6, 7, $total, $dec);
128                 $rep->Line($rep->row  - 4);
129                 $rep->NewLine(2);
130                 $rep->TextCol(0, 6, _('Grand Total'));
131                 $rep->AmountCol(6, 7, $grandtotal, $dec);
132         }
133
134         $rep->Line($rep->row  - 4);
135         $rep->NewLine();
136     $rep->End();
137 }
138
139 ?>