Moving 2.0 development version to main trunk.
[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     $comments = $_POST['PARAM_3'];
105
106         if ($category == reserved_words::get_all_numeric())
107                 $category = 0;
108         if ($category == 0)
109                 $cat = _('All');
110         else
111                 $cat = get_category_name($category);
112
113         if ($location == reserved_words::get_all())
114                 $location = 'all';
115         if ($location == 'all')
116                 $loc = _('All');
117         else
118                 $loc = $location;
119
120         $cols = array(0, 100, 305, 375, 445,    515);
121
122         $headers = array(_('Category'), _('Description'), _('Quantity'), _('Demand'), _('Difference'));
123
124         $aligns = array('left', 'left', 'right', 'right', 'right');
125
126     $params =   array(  0 => $comments,
127                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
128                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
129
130         if ($pictures)
131                 $user_comp = user_company();
132         else
133                 $user_comp = "";
134
135     $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet.pdf", user_pagesize());
136
137     $rep->Font();
138     $rep->Info($params, $cols, $headers, $aligns);
139     $rep->Header();
140
141         $res = getTransactions($category, $location);
142         $catt = '';
143         while ($trans=db_fetch($res))
144         {
145                 if ($catt != $trans['cat_description'])
146                 {
147                         if ($catt != '')
148                         {
149                                 $rep->Line($rep->row - 2);
150                                 $rep->NewLine(2, 3);
151                         }
152                         $rep->TextCol(0, 1, $trans['category_id']);
153                         $rep->TextCol(1, 2, $trans['cat_description']);
154                         $catt = $trans['cat_description'];
155                         $rep->NewLine();
156                 }
157                 $demandqty = getDemandQty($trans['stock_id'], $trans['loc_code']);
158                 $demandqty += getDemandAsmQty($trans['stock_id'], $trans['loc_code']);
159                 $rep->NewLine();
160                 $dec = get_qty_dec($trans['stock_id']);
161                 $rep->TextCol(0, 1, $trans['stock_id']);
162                 $rep->TextCol(1, 2, $trans['description']);
163                 $rep->TextCol(2, 3, number_format2($trans['QtyOnHand'], $dec));
164                 $rep->TextCol(3, 4, number_format2($demandqty, $dec));
165                 $rep->TextCol(4, 5, number_format2($trans['QtyOnHand'] - $demandqty, $dec));
166                 if ($pictures)
167                 {
168                         $image = $comp_path .'/'. $user_comp . '/images/' . $trans['stock_id'] . '.jpg';
169                         if (file_exists($image))
170                         {
171                                 $rep->NewLine();
172                                 if ($rep->row - $height < $rep->bottomMargin)
173                                         $rep->Header();
174                                 $rep->AddImage($image, $rep->cols[1], $rep->row - $pic_height, $pic_width, $pic_height);
175                                 $rep->row -= $pic_height;
176                                 $rep->NewLine();
177                         }
178                 }
179         }
180         $rep->Line($rep->row - 4);
181     $rep->End();
182 }
183
184 ?>