3 /* $Revision: 1.11 $ */
7 include('includes/session.inc');
9 $title = _('Search All Sales Orders');
11 include('includes/header.inc');
13 echo "<FORM ACTION='" . $_SERVER['PHP_SELF'] . '?' . SID
."' METHOD=POST>";
15 if (isset($_GET['SelectedStockItem'])){
16 $SelectedStockItem = $_GET['SelectedStockItem'];
17 } elseif (isset($_POST['SelectedStockItem'])){
18 $SelectedStockItem = $_POST['SelectedStockItem'];
20 if (isset($_GET['OrderNumber'])){
21 $OrderNumber = $_GET['OrderNumber'];
22 } elseif (isset($_POST['OrderNumber'])){
23 $OrderNumber = $_POST['OrderNumber'];
25 if (isset($_GET['CustomerRef'])){
26 $CustomerRef = $_GET['CustomerRef'];
27 } elseif (isset($_POST['CustomerRef'])){
28 $CustomerRef = $_POST['CustomerRef'];
30 if (isset($_GET['SelectedCustomer'])){
31 $SelectedCustomer = $_GET['SelectedCustomer'];
32 } elseif (isset($_POST['SelectedCustomer'])){
33 $SelectedCustomer = $_POST['SelectedCustomer'];
36 if ($SelectedStockItem==''){
37 unset($SelectedStockItem);
39 if ($OrderNumber==''){
42 if ($CustomerRef==''){
45 if ($SelectedCustomer==''){
46 unset($SelectedCustomer);
48 If ($_POST['ResetPart']){
49 unset($SelectedStockItem);
52 If (isset($OrderNumber)) {
53 echo _('Order Number') . ' - ' . $OrderNumber;
54 } elseif (isset($CustomerRef)) {
55 echo _('Customer Ref') . ' - ' . $CustomerRef;
57 If (isset($SelectedCustomer)) {
58 echo _('For customer') . ': ' . $SelectedCustomer .' ' . _('and') . ' ';
59 echo "<input type=hidden name='SelectedCustomer' value='$SelectedCustomer'>";
62 If (isset($SelectedStockItem)) {
64 echo _('for the part') . ': ' . $SelectedStockItem . ' ' . _('and') . ' ' ."<input type=hidden name='SelectedStockItem' value='$SelectedStockItem'>";
70 if ($_POST['SearchParts']!=''){
72 If ($_POST['Keywords']!='' AND $_POST['StockCode']!='') {
73 echo _('Stock description keywords have been used in preference to the Stock code extract entered');
75 If ($_POST['Keywords']!='') {
76 //insert wildcard characters in spaces
80 while (strpos($_POST['Keywords'], ' ', $i)) {
81 $wrdlen=strpos($_POST['Keywords'],' ',$i) - $i;
82 $SearchString=$SearchString . substr($_POST['Keywords'],$i,$wrdlen) . '%';
83 $i=strpos($_POST['Keywords'],' ',$i) +
1;
85 $SearchString = $SearchString. substr($_POST['Keywords'],$i).'%';
87 $SQL = "SELECT stockmaster.stockid,
88 stockmaster.description,
89 SUM(locstock.quantity) AS qoh,
91 SUM(salesorderdetails.quantity - salesorderdetails.qtyinvoiced) AS qdem
95 WHERE stockmaster.stockid=locstock.stockid
96 AND stockmaster.stockid = salesorderdetails.stkcode
97 AND salesorderdetails.completed =0
98 AND stockmaster.description " . LIKE
. "'$SearchString'
99 AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
100 GROUP BY stockmaster.stockid,
101 stockmaster.description,
103 ORDER BY stockmaster.stockid";
105 } elseif ($_POST['StockCode']!=''){
107 $SQL = "SELECT stockmaster.stockid,
108 stockmaster.description,
109 SUM(locstock.quantity) AS qoh,
110 SUM(salesorderdetails.quantity - salesorderdetails.qtyinvoiced) AS qdem,
115 WHERE stockmaster.stockid=locstock.stockid
116 AND stockmaster.stockid = salesorderdetails.stkcode
117 AND salesorderdetails.completed =0
118 AND stockmaster.stockid " . LIKE
. " '%" . $_POST['StockCode'] . "%'
119 AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
120 GROUP BY stockmaster.stockid,
121 stockmaster.description,
123 ORDER BY stockmaster.stockid";
125 } elseif ($_POST['StockCode']=='' AND $_POST['Keywords']=='' AND $_POST['StockCat']!='') {
127 $SQL = "SELECT stockmaster.stockid,
128 stockmaster.description,
129 SUM(locstock.quantity) AS qoh,
130 SUM(salesorderdetails.quantity - salesorderdetails.qtyinvoiced) AS qdem,
135 WHERE stockmaster.stockid=locstock.stockid
136 AND stockmaster.stockid = salesorderdetails.stkcode
137 AND salesorderdetails.completed =0
138 AND stockmaster.categoryid='" . $_POST['StockCat'] . "'
139 GROUP BY stockmaster.stockid,
140 stockmaster.description,
142 ORDER BY stockmaster.stockid";
147 prnMsg(_('No selections have been made to search for parts') . ' - ' . _('choose a stock category or enter some characters of the code or description then try again'),'warn');
150 $ErrMsg = _('No stock items were returned by the SQL because');
151 $DbgMsg = _('The SQL used to retrieve the searched parts was');
152 $StockItemsResult = DB_query($SQL,$db,$ErrMsg,$DbgMsg);
154 if (DB_num_rows($StockItemsResult)==1){
155 $myrow = DB_fetch_row($StockItemsResult);
156 $SelectedStockItem = $myrow[0];
157 $_POST['SearchOrders']='True';
158 unset($StockItemsResult);
159 echo '<BR>' . _('For the part') . ': ' . $SelectedStockItem . ' ' . _('and') . " <input type=hidden name='SelectedStockItem' value='$SelectedStockItem'>";
162 } else if (isset($_POST['SearchOrders']) AND Is_Date($_POST['OrdersAfterDate'])==1) {
164 //figure out the SQL required from the inputs available
165 if (isset($OrderNumber)) {
166 $SQL = "SELECT salesorders.orderno,
169 salesorders.customerref,
171 salesorders.deliverydate,
172 salesorders.deliverto, SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)) AS ordervalue
177 WHERE salesorders.orderno = salesorderdetails.orderno
178 AND salesorders.branchcode = custbranch.branchcode
179 AND salesorders.debtorno = debtorsmaster.debtorno
180 AND debtorsmaster.debtorno = custbranch.debtorno
181 AND salesorders.orderno=". $OrderNumber ."
182 AND salesorders.quotation=0
183 GROUP BY salesorders.orderno,
186 salesorders.customerref,
188 salesorders.deliverydate,
189 salesorders.deliverto
190 ORDER BY salesorders.orderno";
191 } elseif (isset($CustomerRef)) {
192 $SQL = "SELECT salesorders.orderno,
195 salesorders.customerref,
197 salesorders.deliverydate,
198 salesorders.deliverto, SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)) AS ordervalue
203 WHERE salesorders.orderno = salesorderdetails.orderno
204 AND salesorders.branchcode = custbranch.branchcode
205 AND salesorders.debtorno = debtorsmaster.debtorno
206 AND debtorsmaster.debtorno = custbranch.debtorno
207 AND salesorders.customerref like '%". $CustomerRef."%'
208 AND salesorders.quotation=0
209 GROUP BY salesorders.orderno,
212 salesorders.customerref,
214 salesorders.deliverydate,
215 salesorders.deliverto
216 ORDER BY salesorders.orderno";
219 $DateAfterCriteria = FormatDateforSQL($_POST['OrdersAfterDate']);
221 if (isset($SelectedCustomer) AND !isset($OrderNumber) AND !isset($CustomerRef)) {
223 if (isset($SelectedStockItem)) {
224 $SQL = "SELECT salesorders.orderno,
227 salesorders.customerref,
229 salesorders.deliverydate,
230 salesorders.deliverto, SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)) AS ordervalue
235 WHERE salesorders.orderno = salesorderdetails.orderno
236 AND salesorders.branchcode = custbranch.branchcode
237 AND salesorders.debtorno = debtorsmaster.debtorno
238 AND debtorsmaster.debtorno = custbranch.debtorno
239 AND salesorderdetails.stkcode='". $SelectedStockItem ."'
240 AND salesorders.debtorno='" . $SelectedCustomer ."'
241 AND salesorders.orddate >= '" . $DateAfterCriteria ."'
242 AND salesorders.quotation=0
243 GROUP BY salesorders.orderno,
246 salesorders.customerref,
248 salesorders.deliverydate,
249 salesorders.deliverto
250 ORDER BY salesorders.orderno";
252 $SQL = "SELECT salesorders.orderno,
255 salesorders.customerref,
257 salesorders.deliverto,
258 salesorders.deliverydate, SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)) AS ordervalue
263 WHERE salesorders.orderno = salesorderdetails.orderno
264 AND salesorders.debtorno = debtorsmaster.debtorno
265 AND salesorders.branchcode = custbranch.branchcode
266 AND debtorsmaster.debtorno = custbranch.debtorno
267 AND salesorders.debtorno='" . $SelectedCustomer . "'
268 AND salesorders.orddate >= '" . $DateAfterCriteria . "'
269 AND salesorders.quotation=0
270 GROUP BY salesorders.orderno,
273 salesorders.customerref,
275 salesorders.deliverydate,
276 salesorders.deliverto
277 ORDER BY salesorders.orderno";
279 } else { //no customer selected
280 if (isset($SelectedStockItem)) {
281 $SQL = "SELECT salesorders.orderno,
284 salesorders.customerref,
286 salesorders.deliverto,
287 salesorders.deliverydate, SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)) AS ordervalue
292 WHERE salesorders.orderno = salesorderdetails.orderno
293 AND salesorders.debtorno = debtorsmaster.debtorno
294 AND salesorders.branchcode = custbranch.branchcode
295 AND debtorsmaster.debtorno = custbranch.debtorno
296 AND salesorderdetails.stkcode='". $SelectedStockItem ."'
297 AND salesorders.orddate >= '" . $DateAfterCriteria . "'
298 AND salesorders.quotation=0
299 GROUP BY salesorders.orderno,
302 salesorders.customerref,
304 salesorders.deliverydate,
305 salesorders.deliverto
306 ORDER BY salesorders.orderno";
308 $SQL = "SELECT salesorders.orderno,
311 salesorders.customerref,
313 salesorders.deliverto,
314 salesorders.deliverydate, SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)) AS ordervalue
319 WHERE salesorders.orderno = salesorderdetails.orderno
320 AND salesorders.debtorno = debtorsmaster.debtorno
321 AND salesorders.branchcode = custbranch.branchcode
322 AND debtorsmaster.debtorno = custbranch.debtorno
323 AND salesorders.orddate >= '$DateAfterCriteria'
324 AND salesorders.quotation=0
325 GROUP BY salesorders.orderno,
328 salesorders.customerref,
330 salesorders.deliverydate,
331 salesorders.deliverto
332 ORDER BY salesorders.orderno";
335 } //end selected customer
336 } //end not order number selected
338 $SalesOrdersResult = DB_query($SQL,$db);
340 if (DB_error_no($db) !=0) {
341 echo '<BR>' . _('No orders were returned by the SQL because') . ' ' . DB_error_msg($db);
345 }//end of which button clicked options
347 if (!isset($_POST['OrdersAfterDate']) OR $_POST['OrdersAfterDate'] == '' OR ! Is_Date($_POST['OrdersAfterDate'])){
348 $_POST['OrdersAfterDate'] = Date($_SESSION['DefaultDateFormat'],Mktime(0,0,0,Date('m')-2,Date('d'),Date('Y')));
351 if ($OrderNumber=='' OR !isset($OrderNumber)){
352 echo '<TR><TD>' . _('Order Number') . ':</TD><TD>' . "<INPUT type=text name='OrderNumber' MAXLENGTH =8 SIZE=9></TD><TD rowspan=2>" . _('for all orders placed after') .
353 ": </TD><TD rowspan=2><INPUT type=text name='OrdersAfterDate' MAXLENGTH =10 SIZE=11 value=" . $_POST['OrdersAfterDate'] . "></td><td rowspan=2>" .
354 "<INPUT TYPE=SUBMIT NAME='SearchOrders' VALUE='" . _('Search Orders') . "'></TD></TR>";
355 echo '<TR><TD>' . _('Customer Ref') . ':</TD><TD>' . "<INPUT type=text name='CustomerRef' MAXLENGTH =8 SIZE=9></TD></TR>";
359 if (!isset($SelectedStockItem)) {
360 $SQL='SELECT categoryid, categorydescription FROM stockcategory ORDER BY categorydescription';
361 $result1 = DB_query($SQL,$db);
364 echo '<FONT SIZE=1>' . _('To search for sales orders for a specific part use the part selection facilities below') . '</FONT>';
365 echo '<INPUT TYPE=SUBMIT NAME="SearchParts" VALUE="' . _('Search Parts Now') . '">';
367 if (count($_SESSION['AllowedPageSecurityTokens'])>1){
368 echo '<INPUT TYPE=SUBMIT NAME="ResetPart" VALUE="' . _('Show All') . '">';
371 echo '<TR><TD><FONT SIZE=1>' . _('Select a stock category') . ':</FONT>';
372 echo '<SELECT NAME="StockCat">';
374 while ($myrow1 = DB_fetch_array($result1)) {
375 if ($myrow1['categoryid'] == $_POST['StockCat']){
376 echo "<OPTION SELECTED VALUE='". $myrow1['categoryid'] . "'>" . $myrow1['categorydescription'];
378 echo "<OPTION VALUE='". $myrow1['categoryid'] . "'>" . $myrow1['categorydescription'];
383 echo '<TD><FONT SIZE=1>' . _('Enter text extracts in the description') . ':</FONT></TD>';
384 echo '<TD><INPUT TYPE="Text" NAME="Keywords" SIZE=20 MAXLENGTH=25></TD></TR>';
385 echo '<TR><TD></TD>';
386 echo '<TD><FONT SIZE 3><B> ' ._('OR') . ' </B></FONT><FONT SIZE=1>' . _('Enter extract of the Stock Code') . ':</FONT></TD>';
387 echo '<TD><INPUT TYPE="Text" NAME="StockCode" SIZE=15 MAXLENGTH=18></TD>';
395 If (isset($StockItemsResult)) {
397 echo '<TABLE CELLPADDING=2 COLSPAN=7 BORDER=2>';
399 $TableHeadings = "<TR><TD class='tableheader'>" . _('Code') . "</TD>" .
400 "<TD class='tableheader'>" . _('Description') . "</TD>" .
401 "<TD class='tableheader'>" . _('On Hand') . '</TD>' .
402 "<TD class='tableheader'>" . _('Purchase Orders') . '</TD>' .
403 "<TD class='tableheader'>" . _('Sales Orders') . "</TD>" .
404 "<TD class='tableheader'>" . _('Units') . '</TD></TR>';
409 $k=0; //row colour counter
411 while ($myrow=DB_fetch_array($StockItemsResult)) {
414 echo "<tr bgcolor='#CCCCCC'>";
417 echo "<tr bgcolor='#EEEEEE'>";
421 printf("<td><FONT SIZE=1><INPUT TYPE=SUBMIT NAME='SelectedStockItem' VALUE='%s'</FONT></td>
422 <td><FONT SIZE=1>%s</FONT></td>
423 <td ALIGN=RIGHT><FONT SIZE=1>%s</FONT></td>
424 <td ALIGN=RIGHT><FONT SIZE=1>%s</FONT></td>
425 <td ALIGN=RIGHT><FONT SIZE=1>%s</FONT></td>
426 <td><FONT SIZE=1>%s</FONT></td></tr>",
428 $myrow['description'],
439 //end of page full new headings if
446 //end if stock search results to show
448 If ($SalesOrdersResult) {
450 /*show a table of the orders returned by the SQL */
452 echo '<TABLE CELLPADDING=2 COLSPAN=6 WIDTH=100%>';
454 $tableheader = "<TR><TD class='tableheader'>" . _('Order') . " #</TD>
455 <TD class='tableheader'>" . _('Customer') . "</TD>
456 <TD class='tableheader'>" . _('Branch') . "</TD>
457 <TD class='tableheader'>" . _('Cust Order') . " #</TD>
458 <TD class='tableheader'>" . _('Order Date') . "</TD>
459 <TD class='tableheader'>" . _('Req Del Date') . "</TD>
460 <TD class='tableheader'>" . _('Delivery To') . "</TD>
461 <TD class='tableheader'>" . _('Order Total') . "</TD></TR>";
466 $k=0; //row colour counter
467 while ($myrow=DB_fetch_array($SalesOrdersResult)) {
471 echo "<tr bgcolor='#CCCCCC'>";
474 echo "<tr bgcolor='#EEEEEE'>";
478 $ViewPage = $rootpath . '/OrderDetails.php?' .SID
. '&OrderNumber=' . $myrow['orderno'];
479 $FormatedDelDate = ConvertSQLDate($myrow['deliverydate']);
480 $FormatedOrderDate = ConvertSQLDate($myrow['orddate']);
481 $FormatedOrderValue = number_format($myrow['ordervalue'],2);
483 printf("<td><A target='_blank' HREF='%s'>%s</A></td>
490 <td ALIGN=RIGHT>%s</td>
496 $myrow['customerref'],
500 $FormatedOrderValue);
507 //end of page full new headings if
516 include('includes/footer.inc');