Added non closed work order requirements on On Order in Inventory Items Status
[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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Stock Check
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
27 //----------------------------------------------------------------------------------------------------
28
29 print_stock_check();
30
31 function getTransactions($category, $location)
32 {
33         $sql = "SELECT ".TB_PREF."stock_master.category_id,
34                         ".TB_PREF."stock_category.description AS cat_description,
35                         ".TB_PREF."stock_master.stock_id,
36                         ".TB_PREF."stock_master.description,
37                         IF(".TB_PREF."stock_moves.stock_id IS NULL, '', ".TB_PREF."stock_moves.loc_code) AS loc_code,
38                         SUM(IF(".TB_PREF."stock_moves.stock_id IS NULL,0,".TB_PREF."stock_moves.qty)) AS QtyOnHand
39                 FROM (".TB_PREF."stock_master,
40                         ".TB_PREF."stock_category)
41                 LEFT JOIN ".TB_PREF."stock_moves ON
42                         (".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id OR ".TB_PREF."stock_master.stock_id IS NULL)
43                 WHERE ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
44                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
45         if ($category != 0)
46                 $sql .= " AND ".TB_PREF."stock_master.category_id = '$category'";
47         if ($location != 'all')
48                 $sql .= " AND ".TB_PREF."stock_moves.loc_code = '$location'";
49         $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
50                 ".TB_PREF."stock_category.description,
51                 ".TB_PREF."stock_master.stock_id,
52                 ".TB_PREF."stock_master.description
53                 ORDER BY ".TB_PREF."stock_master.category_id,
54                 ".TB_PREF."stock_master.stock_id";
55
56     return db_query($sql,"No transactions were returned");
57 }
58
59 function getDemandQty($stockid, $location)
60 {
61         $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS QtyDemand
62                                 FROM ".TB_PREF."sales_order_details,
63                                         ".TB_PREF."sales_orders
64                                 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND ";
65         if ($location != "")
66                 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
67         $sql .= TB_PREF."sales_order_details.stk_code = '$stockid'";
68
69     $TransResult = db_query($sql,"No transactions were returned");
70         $DemandRow = db_fetch($TransResult);
71         return $DemandRow['QtyDemand'];
72 }
73
74 function getDemandAsmQty($stockid, $location)
75 {
76         $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
77                                    AS Dem
78                                    FROM ".TB_PREF."sales_order_details,
79                                                 ".TB_PREF."sales_orders,
80                                                 ".TB_PREF."bom,
81                                                 ".TB_PREF."stock_master
82                                    WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
83                                    ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND ";
84         if ($location != "")
85                 $sql .= TB_PREF."sales_orders.from_stk_loc ='$location' AND ";
86         $sql .= TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent > 0 AND
87                                    ".TB_PREF."bom.component='$stockid' AND
88                                    ".TB_PREF."stock_master.stock_id=".TB_PREF."bom.parent AND
89                                    (".TB_PREF."stock_master.mb_flag='M' OR ".TB_PREF."stock_master.mb_flag='A')";
90
91     $TransResult = db_query($sql,"No transactions were returned");
92         if (db_num_rows($TransResult)==1)
93         {
94                 $DemandRow = db_fetch_row($TransResult);
95                 $DemandQty = $DemandRow[0];
96         }
97         else
98                 $DemandQty = 0.0;
99
100     return $DemandQty;
101 }
102
103 //----------------------------------------------------------------------------------------------------
104
105 function print_stock_check()
106 {
107     global $comp_path, $path_to_root, $pic_height, $pic_width;
108
109     $category = $_POST['PARAM_0'];
110     $location = $_POST['PARAM_1'];
111     $pictures = $_POST['PARAM_2'];
112     $check    = $_POST['PARAM_3'];
113     $comments = $_POST['PARAM_4'];
114         $destination = $_POST['PARAM_5'];
115         if ($destination)
116                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
117         else
118                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
119
120         if ($category == reserved_words::get_all_numeric())
121                 $category = 0;
122         if ($category == 0)
123                 $cat = _('All');
124         else
125                 $cat = get_category_name($category);
126
127         if ($location == reserved_words::get_all())
128                 $location = 'all';
129         if ($location == 'all')
130                 $loc = _('All');
131         else
132                 $loc = $location;
133
134         if ($check)
135         {
136                 $cols = array(0, 100, 250, 305, 375, 445,       515);
137                 $headers = array(_('Category'), _('Description'), _('Quantity'), _('Check'), _('Demand'), _('Difference'));
138                 $aligns = array('left', 'left', 'right', 'right', 'right', 'right');
139         }
140         else
141         {
142                 $cols = array(0, 100, 305, 375, 445,    515);
143                 $headers = array(_('Category'), _('Description'), _('Quantity'), _('Demand'), _('Difference'));
144                 $aligns = array('left', 'left', 'right', 'right', 'right');
145         }
146
147
148     $params =   array(  0 => $comments,
149                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
150                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
151
152         if ($pictures)
153                 $user_comp = user_company();
154         else
155                 $user_comp = "";
156
157     $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize());
158
159     $rep->Font();
160     $rep->Info($params, $cols, $headers, $aligns);
161     $rep->Header();
162
163         $res = getTransactions($category, $location);
164         $catt = '';
165         while ($trans=db_fetch($res))
166         {
167                 if ($catt != $trans['cat_description'])
168                 {
169                         if ($catt != '')
170                         {
171                                 $rep->Line($rep->row - 2);
172                                 $rep->NewLine(2, 3);
173                         }
174                         $rep->TextCol(0, 1, $trans['category_id']);
175                         $rep->TextCol(1, 2, $trans['cat_description']);
176                         $catt = $trans['cat_description'];
177                         $rep->NewLine();
178                 }
179                 if ($location == 'all')
180                         $loc_code = "";
181                 else
182                         $loc_code = $trans['loc_code'];
183                 $demandqty = getDemandQty($trans['stock_id'], $loc_code);
184                 $demandqty += getDemandAsmQty($trans['stock_id'], $loc_code);
185                 $rep->NewLine();
186                 $dec = get_qty_dec($trans['stock_id']);
187                 $rep->TextCol(0, 1, $trans['stock_id']);
188                 $rep->TextCol(1, 2, $trans['description']);
189                 $rep->AmountCol(2, 3, $trans['QtyOnHand'], $dec);
190                 if ($check)
191                 {
192                         $rep->TextCol(3, 4, "_________");
193                         $rep->AmountCol(4, 5, $demandqty, $dec);
194                         $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
195                 }
196                 else
197                 {
198                         $rep->AmountCol(3, 4, $demandqty, $dec);
199                         $rep->AmountCol(4, 5, $trans['QtyOnHand'] - $demandqty, $dec);
200                 }
201                 if ($pictures)
202                 {
203                         $image = $comp_path .'/'. $user_comp . '/images/' 
204                                 . item_img_name($trans['stock_id']) . '.jpg';
205                         if (file_exists($image))
206                         {
207                                 $rep->NewLine();
208                                 if ($rep->row - $pic_height < $rep->bottomMargin)
209                                         $rep->Header();
210                                 $rep->AddImage($image, $rep->cols[1], $rep->row - $pic_height, 0, $pic_height);
211                                 $rep->row -= $pic_height;
212                                 $rep->NewLine();
213                         }
214                 }
215         }
216         $rep->Line($rep->row - 4);
217         $rep->NewLine();
218     $rep->End();
219 }
220
221 ?>