*** empty log message ***
[fa-stable.git] / reporting / rep301.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Supplier Balances
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 . "gl/includes/gl_db.inc");
16 include_once($path_to_root . "inventory/includes/db/items_category_db.inc");
17
18 //----------------------------------------------------------------------------------------------------
19
20 // trial_inquiry_controls();
21 print_inventory_valuation_report();
22
23 function getTransactions($category, $location)
24 {
25         $sql = "SELECT ".TB_PREF."stock_master.category_id,
26                         ".TB_PREF."stock_category.description AS cat_description,
27                         ".TB_PREF."stock_master.stock_id,
28                         ".TB_PREF."stock_master.description,
29                         ".TB_PREF."stock_moves.loc_code,
30                         SUM(".TB_PREF."stock_moves.qty) AS QtyOnHand,
31                         ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS UnitCost,
32                         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 ItemTotal
33                 FROM ".TB_PREF."stock_master,
34                         ".TB_PREF."stock_category,
35                         ".TB_PREF."stock_moves
36                 WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id
37                 AND ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
38                 GROUP BY ".TB_PREF."stock_master.category_id,
39                         ".TB_PREF."stock_category.description,
40                         UnitCost,
41                         ".TB_PREF."stock_master.stock_id,
42                         ".TB_PREF."stock_master.description
43                 HAVING SUM(".TB_PREF."stock_moves.qty) != 0";
44                 if ($category != 0)
45                         $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
46                 if ($location != 'all')
47                         $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
48                 $sql .= " ORDER BY ".TB_PREF."stock_master.category_id,
49                         ".TB_PREF."stock_master.stock_id";
50
51     return db_query($sql,"No transactions were returned");
52 }
53
54 //----------------------------------------------------------------------------------------------------
55
56 function print_inventory_valuation_report()
57 {
58     global $path_to_root;
59
60     include_once($path_to_root . "reporting/includes/pdf_report.inc");
61
62     $category = $_POST['PARAM_0'];
63     $location = $_POST['PARAM_1'];
64     $detail = $_POST['PARAM_2'];
65     $comments = $_POST['PARAM_3'];
66     
67     $dec = user_price_dec();
68
69         if ($category == reserved_words::get_all_numeric())
70                 $category = 0;
71         if ($category == 0)
72                 $cat = _('All');
73         else
74                 $cat = get_category_name($category);
75
76         if ($location == reserved_words::get_all())
77                 $location = 'all';
78         if ($location == 'all')
79                 $loc = _('All');
80         else
81                 $loc = $location;
82
83         $cols = array(0, 100, 250, 350, 450,    515);
84
85         $headers = array(_('Category'), '', _('Quantity'), _('Unit Cost'), _('Value'));
86
87         $aligns = array('left', 'left', 'right', 'right', 'right');
88
89     $params =   array(  0 => $comments,
90                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
91                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
92
93     $rep = new FrontReport(_('Inventory Valuation Report'), "InventoryValReport.pdf", user_pagesize());
94
95     $rep->Font();
96     $rep->Info($params, $cols, $headers, $aligns);
97     $rep->Header();
98
99         $res = getTransactions($category, $location);
100         $total = $grandtotal = 0.0; 
101         $catt = '';
102         while ($trans=db_fetch($res))
103         {
104                 if ($catt != $trans['cat_description'])
105                 {
106                         if ($catt != '')
107                         {
108                                 if ($detail)
109                                 {
110                                         $rep->NewLine(2, 3);
111                                         $rep->TextCol(0, 4, _('Total'));
112                                 }       
113                                 $rep->Textcol(4, 5, number_format2($total, $dec));
114                                 if ($detail)
115                                 {
116                                         $rep->Line($rep->row - 2);
117                                         $rep->NewLine();
118                                 }       
119                                 $rep->NewLine();
120                                 $total = 0.0;
121                         }
122                         $rep->TextCol(0, 1, $trans['category_id']);
123                         $rep->TextCol(1, 2, $trans['cat_description']);
124                         $catt = $trans['cat_description'];
125                         if ($detail)
126                                 $rep->NewLine();
127                 }
128                 if ($detail)
129                 {
130                         $rep->NewLine();
131                         $rep->fontsize -= 2;
132                         $rep->TextCol(0, 1, $trans['stock_id']);
133                         $rep->TextCol(1, 2, $trans['description']);
134                         $rep->TextCol(2, 3, number_format2($trans['QtyOnHand'], user_qty_dec()));
135                         $rep->TextCol(3, 4, number_format2($trans['UnitCost'], $dec));
136                         $rep->TextCol(4, 5, number_format2($trans['ItemTotal'], $dec));
137                         $rep->fontsize += 2;
138                 }
139                 $total += $trans['ItemTotal'];
140                 $grandtotal += $trans['ItemTotal'];
141         }
142         if ($detail)
143         {
144                 $rep->NewLine(2, 3);
145                 $rep->TextCol(0, 4, _('Total'));
146         }       
147         $rep->Textcol(4, 5, number_format2($total, $dec));
148         if ($detail)
149         {
150                 $rep->Line($rep->row - 2);
151                 $rep->NewLine();
152         }
153         $rep->NewLine(2, 1);
154         $rep->TextCol(0, 4, _('Grand Total'));
155         $rep->TextCol(4, 5, number_format2($grandtotal, $dec));
156         $rep->Line($rep->row  - 4);
157     $rep->End();
158 }
159
160 ?>