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