1<?php
2
3
4include('includes/session.php');
5
6$Title = _('Stock Usage');
7
8if (isset($_GET['StockID'])){
9	$StockID = trim(mb_strtoupper($_GET['StockID']));
10} elseif (isset($_POST['StockID'])){
11	$StockID = trim(mb_strtoupper($_POST['StockID']));
12} else {
13	$StockID = '';
14}
15
16if (isset($_POST['ShowGraphUsage'])) {
17	echo '<meta http-equiv="Refresh" content="0; url=' . $RootPath . '/StockUsageGraph.php?StockLocation=' . $_POST['StockLocation']  . '&amp;StockID=' . $StockID . '">';
18	prnMsg(_('You should automatically be forwarded to the usage graph') .
19			'. ' . _('If this does not happen') .' (' . _('if the browser does not support META Refresh') . ') ' .
20			'<a href="' . $RootPath . '/StockUsageGraph.php?StockLocation=' . $_POST['StockLocation'] .'&amp;StockID=' . $StockID . '">' . _('click here') . '</a> ' . _('to continue'),'info');
21	exit;
22}
23
24include('includes/header.php');
25
26echo '<p class="page_title_text">
27		<img src="'.$RootPath.'/css/'.$Theme.'/images/magnifier.png" title="' . _('Dispatch') .
28		'" alt="" />' . ' ' . $Title . '
29	</p>';
30
31$result = DB_query("SELECT description,
32						units,
33						mbflag,
34						decimalplaces
35					FROM stockmaster
36					WHERE stockid='".$StockID."'");
37$myrow = DB_fetch_row($result);
38
39$DecimalPlaces = $myrow[3];
40
41echo '<form action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '" method="post">';
42echo '<div>';
43echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
44echo '<table class="selection">';
45
46$Its_A_KitSet_Assembly_Or_Dummy =False;
47if ($myrow[2]=='K'
48	OR $myrow[2]=='A'
49	OR $myrow[2]=='D') {
50
51	$Its_A_KitSet_Assembly_Or_Dummy =True;
52	echo '<h3>' . $StockID . ' - ' . $myrow[0] . '</h3>';
53
54	prnMsg( _('The selected item is a dummy or assembly or kit-set item and cannot have a stock holding') . '. ' . _('Please select a different item'),'warn');
55
56	$StockID = '';
57} else {
58	echo '<tr>
59			<th><h3>' . _('Item') . ' : ' . $StockID . ' - ' . $myrow[0] . '   (' . _('in units of') . ' : ' . $myrow[1] . ')</h3></th>
60		</tr>';
61}
62
63echo '<tr><td>' . _('Stock Code') . ':<input type="text" pattern="(?!^\s+$)[^%]{1,20}" title="'._('The input should not be blank or percentage mark').'" required="required" name="StockID" size="21" maxlength="20" value="' . $StockID . '" />';
64
65echo _('From Stock Location') . ':<select name="StockLocation">';
66
67$sql = "SELECT locations.loccode, locationname FROM locations
68			INNER JOIN locationusers ON locationusers.loccode=locations.loccode AND locationusers.userid='" .  $_SESSION['UserID'] . "' AND locationusers.canview=1";
69$resultStkLocs = DB_query($sql);
70while ($myrow=DB_fetch_array($resultStkLocs)){
71	if (isset($_POST['StockLocation'])){
72		if ($myrow['loccode'] == $_POST['StockLocation']){
73		     echo '<option selected="selected" value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
74		} else {
75		     echo '<option value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
76		}
77	} elseif ($myrow['loccode']==$_SESSION['UserStockLocation']){
78		 echo '<option selected="selected" value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
79		 $_POST['StockLocation']=$myrow['loccode'];
80	} else {
81		 echo '<option value="' . $myrow['loccode'] . '">' . $myrow['locationname'] . '</option>';
82	}
83}
84if (isset($_POST['StockLocation'])){
85	if ('All'== $_POST['StockLocation']){
86	     echo '<option selected="selected" value="All">' . _('All Locations') . '</option>';
87	} else {
88	     echo '<option value="All">' . _('All Locations') . '</option>';
89	}
90}
91echo '</select>';
92
93echo ' <input type="submit" name="ShowUsage" value="' . _('Show Stock Usage') . '" />';
94echo ' <input type="submit" name="ShowGraphUsage" value="' . _('Show Graph Of Stock Usage') . '" /></td>
95		</tr>
96		</table>
97		<br />';
98
99
100/*HideMovt ==1 if the movement was only created for the purpose of a transaction but is not a physical movement eg. A price credit will create a movement record for the purposes of display on a credit note
101but there is no physical stock movement - it makes sense honest ??? */
102
103$CurrentPeriod = GetPeriod(Date($_SESSION['DefaultDateFormat']));
104
105if (isset($_POST['ShowUsage'])){
106	if($_POST['StockLocation']=='All'){
107		$sql = "SELECT periods.periodno,
108				periods.lastdate_in_period,
109				canview,
110				SUM(CASE WHEN (stockmoves.type=10 OR stockmoves.type=11 OR stockmoves.type=17 OR stockmoves.type=28 OR stockmoves.type=38)
111							AND stockmoves.hidemovt=0
112							AND stockmoves.stockid = '" . $StockID . "'
113						THEN -stockmoves.qty ELSE 0 END) AS qtyused
114				FROM periods LEFT JOIN stockmoves
115					ON periods.periodno=stockmoves.prd
116				INNER JOIN locationusers ON locationusers.loccode=stockmoves.loccode AND locationusers.userid='" .  $_SESSION['UserID'] . "' AND locationusers.canview=1
117				WHERE periods.periodno <='" . $CurrentPeriod . "'
118				GROUP BY periods.periodno,
119					periods.lastdate_in_period
120				ORDER BY periodno DESC LIMIT " . $_SESSION['NumberOfPeriodsOfStockUsage'];
121	} else {
122		$sql = "SELECT periods.periodno,
123				periods.lastdate_in_period,
124				SUM(CASE WHEN (stockmoves.type=10 OR stockmoves.type=11 OR stockmoves.type=17 OR stockmoves.type=28 OR stockmoves.type=38)
125								AND stockmoves.hidemovt=0
126								AND stockmoves.stockid = '" . $StockID . "'
127								AND stockmoves.loccode='" . $_POST['StockLocation'] . "'
128							THEN -stockmoves.qty ELSE 0 END) AS qtyused
129				FROM periods LEFT JOIN stockmoves
130					ON periods.periodno=stockmoves.prd
131				WHERE periods.periodno <='" . $CurrentPeriod . "'
132				GROUP BY periods.periodno,
133					periods.lastdate_in_period
134				ORDER BY periodno DESC LIMIT " . $_SESSION['NumberOfPeriodsOfStockUsage'];
135
136	}
137	$MovtsResult = DB_query($sql);
138	if (DB_error_no() !=0) {
139		echo _('The stock usage for the selected criteria could not be retrieved because') . ' - ' . DB_error_msg();
140		if ($debug==1){
141		echo '<br />' . _('The SQL that failed was') . $sql;
142		}
143		exit;
144	}
145
146	echo '<table class="selection">
147		<thead>
148			<tr>
149						<th class="ascending">' . _('Month') . '</th>
150						<th class="ascending">' . _('Usage') . '</th>
151			</tr>
152		</thead>
153		<tbody>';
154
155	$TotalUsage = 0;
156	$PeriodsCounter =0;
157
158	while ($myrow=DB_fetch_array($MovtsResult)) {
159
160		$DisplayDate = MonthAndYearFromSQLDate($myrow['lastdate_in_period']);
161
162		$TotalUsage += $myrow['qtyused'];
163		$PeriodsCounter++;
164		printf('<tr class="striped_row">
165				<td>%s</td>
166				<td class="number">%s</td>
167				</tr>',
168				$DisplayDate,
169				locale_number_format($myrow['qtyused'],$DecimalPlaces));
170	} //end of while loop
171
172	echo '</tbody></table>';
173
174	if ($TotalUsage>0 AND $PeriodsCounter>0){
175		echo '<table class="selection"><tr>
176				<th colspan="2">' . _('Average Usage per month is') . ' ' . locale_number_format($TotalUsage/$PeriodsCounter) . '</th>
177			</tr></table>';
178	}
179
180} /* end if Show Usage is clicked */
181
182echo '<div class="centre">';
183echo '<br />
184    <a href="' . $RootPath . '/StockStatus.php?StockID=' . $StockID . '">' . _('Show Stock Status')  . '</a>';
185echo '<br />
186	<a href="' . $RootPath . '/StockMovements.php?StockID=' . $StockID . '&amp;StockLocation=' . $_POST['StockLocation'] . '">' . _('Show Stock Movements') . '</a>';
187echo '<br />
188	<a href="' . $RootPath . '/SelectSalesOrder.php?SelectedStockItem=' . $StockID . '&amp;StockLocation=' . $_POST['StockLocation'] . '">' . _('Search Outstanding Sales Orders') . '</a>';
189echo '<br />
190	<a href="' . $RootPath . '/SelectCompletedOrder.php?SelectedStockItem=' . $StockID . '">' . _('Search Completed Sales Orders') . '</a>';
191echo '<br />
192	<a href="' . $RootPath . '/PO_SelectOSPurchOrder.php?SelectedStockItem=' . $StockID . '">' . _('Search Outstanding Purchase Orders') . '</a>';
193
194echo '</div>
195      </div>
196      </form>';
197include('includes/footer.php');
198
199?>
200