a7beebc1988b1ba5b0a39a83b44acc6a18be8b63
[fa-stable.git] / reporting / rep303.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_ITEMSVALREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Stock Check Sheet
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 . "/gl/includes/gl_db.inc");
25 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
26 include_once($path_to_root . "/includes/db/manufacturing_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_stock_check();
31
32 function getTransactions($category, $location)
33 {
34         $sql = "SELECT ".TB_PREF."stock_master.category_id,
35                         ".TB_PREF."stock_category.description AS cat_description,
36                         ".TB_PREF."stock_master.stock_id,
37                         ".TB_PREF."stock_master.units,
38                         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
39                         IF(".TB_PREF."stock_moves.stock_id IS NULL, '', ".TB_PREF."stock_moves.loc_code) AS loc_code,
40                         SUM(IF(".TB_PREF."stock_moves.stock_id IS NULL,0,".TB_PREF."stock_moves.qty)) AS QtyOnHand
41                 FROM (".TB_PREF."stock_master,
42                         ".TB_PREF."stock_category)
43                 LEFT JOIN ".TB_PREF."stock_moves ON
44                         (".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id)
45                 WHERE ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
46                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
47         if ($category != 0)
48                 $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
49         if ($location != 'all')
50                 $sql .= " AND IF(".TB_PREF."stock_moves.stock_id IS NULL, '1=1',".TB_PREF."stock_moves.loc_code = ".db_escape($location).")";
51         $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
52                 ".TB_PREF."stock_category.description,
53                 ".TB_PREF."stock_master.stock_id,
54                 ".TB_PREF."stock_master.description
55                 ORDER BY ".TB_PREF."stock_master.category_id,
56                 ".TB_PREF."stock_master.stock_id";
57
58     return db_query($sql,"No transactions were returned");
59 }
60
61 //----------------------------------------------------------------------------------------------------
62
63 function print_stock_check()
64 {
65     global $path_to_root, $pic_height;
66
67         $category = $_POST['PARAM_0'];
68         $location = $_POST['PARAM_1'];
69         $pictures = $_POST['PARAM_2'];
70         $check    = $_POST['PARAM_3'];
71         $shortage = $_POST['PARAM_4'];
72         $no_zeros = $_POST['PARAM_5'];
73         $comments = $_POST['PARAM_6'];
74                 $destination = $_POST['PARAM_7'];
75
76         if ($destination)
77                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
78         else
79                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
80
81         if ($category == ALL_NUMERIC)
82                 $category = 0;
83         if ($category == 0)
84                 $cat = _('All');
85         else
86                 $cat = get_category_name($category);
87
88         if ($location == ALL_TEXT)
89                 $location = 'all';
90         if ($location == 'all')
91                 $loc = _('All');
92         else
93                 $loc = get_location_name($location);
94         if ($shortage)
95         {
96                 $short = _('Yes');
97                 $available = _('Shortage');
98         }
99         else
100         {
101                 $short = _('No');
102                 $available = _('Available');
103         }
104         if ($no_zeros) $nozeros = _('Yes');
105         else $nozeros = _('No');
106         if ($check)
107         {
108                 $cols = array(0, 75, 225, 250, 295, 345, 390, 445,      515);
109                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Check'), _('Demand'), $available, _('On Order'));
110                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
111         }
112         else
113         {
114                 $cols = array(0, 75, 225, 250, 315, 380, 445,   515);
115                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Demand'), $available, _('On Order'));
116                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
117         }
118
119
120         $params =   array(      0 => $comments,
121                                 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
122                                 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
123                                 3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''),
124                                 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
125
126         if ($pictures)
127                 $user_comp = user_company();
128         else
129                 $user_comp = "";
130
131         $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize());
132
133     $rep->Font();
134     $rep->Info($params, $cols, $headers, $aligns);
135     $rep->NewPage();
136
137         $res = getTransactions($category, $location);
138         $catt = '';
139         while ($trans=db_fetch($res))
140         {
141                 if ($location == 'all')
142                         $loc_code = "";
143                 else
144                         $loc_code = $location;
145                 $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
146                 $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
147                 $onorder = get_on_porder_qty($trans['stock_id'], $loc_code);
148                 $flag = get_mb_flag($trans['stock_id']);
149                 if ($flag == 'M')
150                         $onorder += get_on_worder_qty($trans['stock_id'], $loc_code);
151                 if ($no_zeros && $trans['QtyOnHand'] == 0 && $demandqty == 0 && $onorder == 0)
152                         continue;
153                 if ($shortage && $trans['QtyOnHand'] - $demandqty >= 0)
154                         continue;
155                 if ($catt != $trans['cat_description'])
156                 {
157                         if ($catt != '')
158                         {
159                                 $rep->Line($rep->row - 2);
160                                 $rep->NewLine(2, 3);
161                         }
162                         $rep->TextCol(0, 1, $trans['category_id']);
163                         $rep->TextCol(1, 2, $trans['cat_description']);
164                         $catt = $trans['cat_description'];
165                         $rep->NewLine();
166                 }
167                 $rep->NewLine();
168                 $dec = get_qty_dec($trans['stock_id']);
169                 $rep->TextCol(0, 1, $trans['stock_id']);
170                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
171                 $rep->TextCol(2, 3, $trans['units']);
172                 $rep->AmountCol(3, 4, $trans['QtyOnHand'], $dec);
173                 if ($check)
174                 {
175                         $rep->TextCol(4, 5, "_________");
176                         $rep->AmountCol(5, 6, $demandqty, $dec);
177                         $rep->AmountCol(6, 7, $trans['QtyOnHand'] - $demandqty, $dec);
178                         $rep->AmountCol(7, 8, $onorder, $dec);
179                 }
180                 else
181                 {
182                         $rep->AmountCol(4, 5, $demandqty, $dec);
183                         $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
184                         $rep->AmountCol(6, 7, $onorder, $dec);
185                 }
186                 if ($pictures)
187                 {
188                         $image = company_path() . '/images/'
189                                 . item_img_name($trans['stock_id']) . '.jpg';
190                         if (file_exists($image))
191                         {
192                                 $rep->NewLine();
193                                 if ($rep->row - $pic_height < $rep->bottomMargin)
194                                         $rep->NewPage();
195                                 $rep->AddImage($image, $rep->cols[1], $rep->row - $pic_height, 0, $pic_height);
196                                 $rep->row -= $pic_height;
197                                 $rep->NewLine();
198                         }
199                 }
200         }
201         $rep->Line($rep->row - 4);
202         $rep->NewLine();
203     $rep->End();
204 }
205
206 ?>