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