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