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