Access to system settings moved from global scope to SysPrefs.
[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, $item_like)
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   if($item_like)
52   {
53     $regexp = null;
54
55     if(sscanf($item_like, "/%s", $regexp)==1)
56       $sql .= " AND ".TB_PREF."stock_master.stock_id RLIKE ".db_escape($regexp);
57     else
58       $sql .= " AND ".TB_PREF."stock_master.stock_id LIKE ".db_escape($item_like);
59   }
60         $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
61                 ".TB_PREF."stock_category.description,
62                 ".TB_PREF."stock_master.stock_id,
63                 ".TB_PREF."stock_master.description
64                 ORDER BY ".TB_PREF."stock_master.category_id,
65                 ".TB_PREF."stock_master.stock_id";
66
67     return db_query($sql,"No transactions were returned");
68 }
69
70 //----------------------------------------------------------------------------------------------------
71
72 function print_stock_check()
73 {
74     global $path_to_root, $SysPrefs;
75
76         $category = $_POST['PARAM_0'];
77         $location = $_POST['PARAM_1'];
78         $pictures = $_POST['PARAM_2'];
79         $check    = $_POST['PARAM_3'];
80         $shortage = $_POST['PARAM_4'];
81         $no_zeros = $_POST['PARAM_5'];
82         $like     = $_POST['PARAM_6']; 
83         $comments = $_POST['PARAM_7'];
84         $orientation = $_POST['PARAM_8'];
85         $destination = $_POST['PARAM_9'];
86
87         if ($destination)
88                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
89         else
90                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
91
92         $orientation = ($orientation ? 'L' : 'P');
93         if ($category == ALL_NUMERIC)
94                 $category = 0;
95         if ($category == 0)
96                 $cat = _('All');
97         else
98                 $cat = get_category_name($category);
99
100         if ($location == ALL_TEXT)
101                 $location = 'all';
102         if ($location == 'all')
103                 $loc = _('All');
104         else
105                 $loc = get_location_name($location);
106         if ($shortage)
107         {
108                 $short = _('Yes');
109                 $available = _('Shortage');
110         }
111         else
112         {
113                 $short = _('No');
114                 $available = _('Available');
115         }
116         if ($no_zeros) $nozeros = _('Yes');
117         else $nozeros = _('No');
118         if ($check)
119         {
120                 $cols = array(0, 75, 225, 250, 295, 345, 390, 445,      515);
121                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Check'), _('Demand'), $available, _('On Order'));
122                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
123         }
124         else
125         {
126                 $cols = array(0, 75, 225, 250, 315, 380, 445,   515);
127                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Demand'), $available, _('On Order'));
128                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
129         }
130
131
132         $params =   array(      0 => $comments,
133                                 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
134                                 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
135                                 3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''),
136                                 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
137
138         if ($pictures)
139                 $user_comp = user_company();
140         else
141                 $user_comp = "";
142
143         $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize(), 9, $orientation);
144     if ($orientation == 'L')
145         recalculate_cols($cols);
146
147     $rep->Font();
148     $rep->Info($params, $cols, $headers, $aligns);
149     $rep->NewPage();
150
151         $res = getTransactions($category, $location, $like);
152         $catt = '';
153         while ($trans=db_fetch($res))
154         {
155                 if ($location == 'all')
156                         $loc_code = "";
157                 else
158                         $loc_code = $location;
159                 $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
160                 $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
161                 $onorder = get_on_porder_qty($trans['stock_id'], $loc_code);
162                 $flag = get_mb_flag($trans['stock_id']);
163                 if ($flag == 'M')
164                         $onorder += get_on_worder_qty($trans['stock_id'], $loc_code);
165                 if ($no_zeros && $trans['QtyOnHand'] == 0 && $demandqty == 0 && $onorder == 0)
166                         continue;
167                 if ($shortage && $trans['QtyOnHand'] - $demandqty >= 0)
168                         continue;
169                 if ($catt != $trans['cat_description'])
170                 {
171                         if ($catt != '')
172                         {
173                                 $rep->Line($rep->row - 2);
174                                 $rep->NewLine(2, 3);
175                         }
176                         $rep->TextCol(0, 1, $trans['category_id']);
177                         $rep->TextCol(1, 2, $trans['cat_description']);
178                         $catt = $trans['cat_description'];
179                         $rep->NewLine();
180                 }
181                 $rep->NewLine();
182                 $dec = get_qty_dec($trans['stock_id']);
183                 $rep->TextCol(0, 1, $trans['stock_id']);
184                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
185                 $rep->TextCol(2, 3, $trans['units']);
186                 $rep->AmountCol(3, 4, $trans['QtyOnHand'], $dec);
187                 if ($check)
188                 {
189                         $rep->TextCol(4, 5, "_________");
190                         $rep->AmountCol(5, 6, $demandqty, $dec);
191                         $rep->AmountCol(6, 7, $trans['QtyOnHand'] - $demandqty, $dec);
192                         $rep->AmountCol(7, 8, $onorder, $dec);
193                 }
194                 else
195                 {
196                         $rep->AmountCol(4, 5, $demandqty, $dec);
197                         $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
198                         $rep->AmountCol(6, 7, $onorder, $dec);
199                 }
200                 if ($pictures)
201                 {
202                         $image = company_path() . '/images/'
203                                 . item_img_name($trans['stock_id']) . '.jpg';
204                         if (file_exists($image))
205                         {
206                                 $rep->NewLine();
207                                 if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin)
208                                         $rep->NewPage();
209                                 $rep->AddImage($image, $rep->cols[1], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height);
210                                 $rep->row -= $SysPrefs->pic_height;
211                                 $rep->NewLine();
212                         }
213                 }
214         }
215         $rep->Line($rep->row - 4);
216         $rep->NewLine();
217     $rep->End();
218 }
219