Changed the Quantity routines to use the Item Units decimals if any. A lot of files.
[fa-stable.git] / reporting / rep302.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Inventory Planning
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_inventory_planning();
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 qty_on_hand
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_master.description,
43                 ".TB_PREF."stock_category.description,
44                 ".TB_PREF."stock_moves.stock_id,
45                 ".TB_PREF."stock_master.stock_id
46                 ORDER BY ".TB_PREF."stock_master.category_id,
47                 ".TB_PREF."stock_master.stock_id";
48
49     return db_query($sql,"No transactions were returned");
50
51 }
52
53 function getCustQty($stockid, $location)
54 {
55         $sql = "SELECT SUM(".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_sent) AS qty_demand
56                                 FROM ".TB_PREF."sales_order_details,
57                                         ".TB_PREF."sales_orders
58                                 WHERE ".TB_PREF."sales_order_details.order_no=".TB_PREF."sales_orders.order_no AND
59                                         ".TB_PREF."sales_orders.from_stk_loc ='$location' AND
60                                         ".TB_PREF."sales_order_details.stk_code = '$stockid'";
61
62     $TransResult = db_query($sql,"No transactions were returned");
63         $DemandRow = db_fetch($TransResult);
64         return $DemandRow['qty_demand'];
65 }
66
67 function getCustAsmQty($stockid, $location)
68 {
69         $sql = "SELECT SUM((".TB_PREF."sales_order_details.quantity-".TB_PREF."sales_order_details.qty_sent)*".TB_PREF."bom.quantity)
70                                    AS Dem
71                                    FROM ".TB_PREF."sales_order_details,
72                                                 ".TB_PREF."sales_orders,
73                                                 ".TB_PREF."bom,
74                                                 ".TB_PREF."stock_master
75                                    WHERE ".TB_PREF."sales_order_details.stk_code=".TB_PREF."bom.parent AND
76                                    ".TB_PREF."sales_orders.order_no = ".TB_PREF."sales_order_details.order_no AND
77                                    ".TB_PREF."sales_orders.from_stk_loc='$location' AND
78                                    ".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 function getSuppQty($stockid, $location)
96 {
97         $sql = "SELECT SUM(".TB_PREF."purch_order_details.quantity_ordered - ".TB_PREF."purch_order_details.quantity_received) AS QtyOnOrder
98                                 FROM ".TB_PREF."purch_order_details,
99                                         ".TB_PREF."purch_orders
100                                 WHERE ".TB_PREF."purch_order_details.order_no = ".TB_PREF."purch_orders.order_no
101                                 AND ".TB_PREF."purch_order_details.item_code = '$stockid'
102                                 AND ".TB_PREF."purch_orders.into_stock_location= '$location'";
103
104     $TransResult = db_query($sql,"No transactions were returned");
105         $DemandRow = db_fetch($TransResult);
106         return $DemandRow['QtyOnOrder'];
107 }
108
109 function getPeriods($stockid, $location)
110 {
111         $date5 = date('Y-m-d');
112         $date4 = date('Y-m-d',mktime(0,0,0,date('m'),1,date('Y')));
113         $date3 = date('Y-m-d',mktime(0,0,0,date('m')-1,1,date('Y')));
114         $date2 = date('Y-m-d',mktime(0,0,0,date('m')-2,1,date('Y')));
115         $date1 = date('Y-m-d',mktime(0,0,0,date('m')-3,1,date('Y')));
116         $date0 = date('Y-m-d',mktime(0,0,0,date('m')-4,1,date('Y')));
117
118         $sql = "SELECT SUM(CASE WHEN tran_date >= '$date0' AND tran_date < '$date1' THEN -qty ELSE 0 END) AS prd0,
119                                 SUM(CASE WHEN tran_date >= '$date1' AND tran_date < '$date2' THEN -qty ELSE 0 END) AS prd1,
120                                 SUM(CASE WHEN tran_date >= '$date2' AND tran_date < '$date3' THEN -qty ELSE 0 END) AS prd2,
121                                 SUM(CASE WHEN tran_date >= '$date3' AND tran_date < '$date4' THEN -qty ELSE 0 END) AS prd3,
122                                 SUM(CASE WHEN tran_date >= '$date4' AND tran_date <= '$date5' THEN -qty ELSE 0 END) AS prd4
123                         FROM ".TB_PREF."stock_moves
124                         WHERE stock_id='$stockid'
125                         AND loc_code ='$location'
126                         AND (type=13 OR type=11)
127                         AND visible=1";
128
129     $TransResult = db_query($sql,"No transactions were returned");
130         return db_fetch($TransResult);
131 }
132
133 //----------------------------------------------------------------------------------------------------
134
135 function print_inventory_planning()
136 {
137     global $path_to_root;
138
139     include_once($path_to_root . "reporting/includes/pdf_report.inc");
140
141     $category = $_POST['PARAM_0'];
142     $location = $_POST['PARAM_1'];
143     $comments = $_POST['PARAM_2'];
144
145         if ($category == reserved_words::get_all_numeric())
146                 $category = 0;
147         if ($category == 0)
148                 $cat = _('All');
149         else
150                 $cat = get_category_name($category);
151
152         if ($location == reserved_words::get_all())
153                 $location = 'all';
154         if ($location == 'all')
155                 $loc = _('All');
156         else
157                 $loc = $location;
158
159         $cols = array(0, 50, 150, 180, 210, 240, 270, 300, 330, 390, 435, 480, 525);
160
161         $per0 = strftime('%b',mktime(0,0,0,date('m'),1,date('Y')));
162         $per1 = strftime('%b',mktime(0,0,0,date('m')-1,1,date('Y')));
163         $per2 = strftime('%b',mktime(0,0,0,date('m')-2,1,date('Y')));
164         $per3 = strftime('%b',mktime(0,0,0,date('m')-3,1,date('Y')));
165         $per4 = strftime('%b',mktime(0,0,0,date('m')-4,1,date('Y')));
166
167         $headers = array(_('Category'), '', $per4, $per3, $per2, $per1, $per0, '3*M',
168                 _('QOH'), _('Cust Ord'), _('Supp Ord'), _('Sugg Ord'));
169
170         $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right', 'right',
171                 'right', 'right', 'right', 'right');
172
173     $params =   array(  0 => $comments,
174                                     1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
175                                     2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''));
176
177     $rep = new FrontReport(_('Inventory Planning Report'), "InventoryPlanning.pdf", user_pagesize());
178
179     $rep->Font();
180     $rep->Info($params, $cols, $headers, $aligns);
181     $rep->Header();
182
183         $res = getTransactions($category, $location);
184         $catt = '';
185         while ($trans=db_fetch($res))
186         {
187                 if ($catt != $trans['cat_description'])
188                 {
189                         if ($catt != '')
190                         {
191                                 $rep->Line($rep->row - 2);
192                                 $rep->NewLine(2, 3);
193                         }
194                         $rep->TextCol(0, 1, $trans['category_id']);
195                         $rep->TextCol(1, 2, $trans['cat_description']);
196                         $catt = $trans['cat_description'];
197                         $rep->NewLine();
198                 }
199
200                 $custqty = getCustQty($trans['stock_id'], $trans['loc_code']);
201                 $custqty += getCustAsmQty($trans['stock_id'], $trans['loc_code']);
202                 $suppqty = getSuppQty($trans['stock_id'], $trans['loc_code']);
203                 $period = getPeriods($trans['stock_id'], $trans['loc_code']);
204                 $rep->NewLine();
205                 $dec = get_qty_dec($trans['stock_id']);
206                 $rep->TextCol(0, 1, $trans['stock_id']);
207                 $rep->TextCol(1, 2, $trans['description']);
208                 $rep->TextCol(2, 3, number_format2($period['prd0'], $dec));
209                 $rep->TextCol(3, 4, number_format2($period['prd1'], $dec));
210                 $rep->TextCol(4, 5, number_format2($period['prd2'], $dec));
211                 $rep->TextCol(5, 6, number_format2($period['prd3'], $dec));
212                 $rep->TextCol(6, 7, number_format2($period['prd4'], $dec));
213
214                 $MaxMthSales = Max($period['prd0'], $period['prd1'], $period['prd2'], $period['prd3']);
215                 $IdealStockHolding = $MaxMthSales * 3;
216                 $rep->TextCol(7, 8, number_format2($IdealStockHolding, $dec));
217
218                 $rep->TextCol(8, 9, number_format2($trans['qty_on_hand'], $dec));
219                 $rep->TextCol(9, 10, number_format2($custqty, $dec));
220                 $rep->TextCol(10, 11, number_format2($suppqty, $dec));
221
222                 $SuggestedTopUpOrder = $IdealStockHolding - $trans['qty_on_hand'] + $custqty - $suppqty;
223                 if ($SuggestedTopUpOrder < 0.0)
224                         $SuggestedTopUpOrder = 0.0;
225                 $rep->TextCol(11, 12, number_format2($SuggestedTopUpOrder, $dec));
226         }
227         $rep->Line($rep->row - 4);
228     $rep->End();
229 }
230
231 ?>