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