
<?php
require_once ('../include/tc_calendar.php');
$start = strtotime($_SESSION['purchase_date']);
$d = date("d", $start );
$m = date("m", $start );
$y = date("Y", $start );


$table_name = $_SESSION['db_prefix']."_purchase_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  purchase_id > 0 "; 
execsql($delete2_query);


$table_name = $_SESSION['db_prefix']."_list_names_authorise";
    $position_query = "SELECT item_name, item_name_2 FROM $table_name ORDER BY item_name ASC";
  $arrId1 = array ();
  $arrName1 = array ();
  $position_result = execsql($position_query);
  if ($position_result!="") {
  $position_num = mysql_num_rows($position_result);
  $b = 0;
  while ($b < $position_num) {
    $arrId1[$b] = mysql_result($position_result, $b, "item_name_2");
    $arrName1[$b] = mysql_result($position_result, $b, "item_name");
    $b++;
  }
  }  

$table_name = $_SESSION['db_prefix']."_suppliers";
$suppliersQuery = "SELECT supplier_id, account_name FROM $table_name  ORDER BY account_name ASC";
  $suppliersResult = execsql($suppliersQuery);
  $suppliersNum = mysql_num_rows($suppliersResult);
  $w = 0;
  $arrId2 = array ();
  $arrName2 = array ();
  while ($w < $suppliersNum) {
    $arrId2[$w] = mysql_result($suppliersResult, $w, "supplier_id");
    $arrName2[$w] = mysql_result($suppliersResult, $w, "account_name");
    $w++;
  }
  
$table_name = $_SESSION['db_prefix']."_list_names_admin";
    $position_query = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
  $arrId3 = array ();
  $arrName3 = array ();
  $position_result = execsql($position_query);
  if ($position_result!="") {
  $position_num = mysql_num_rows($position_result);
  $b = 0;
  while ($b < $position_num) {
    $arrId3[$b] = mysql_result($position_result, $b, "item_name");
    $arrName3[$b] = mysql_result($position_result, $b, "item_name");
    $b++;
  }
  }
  
  
$table_name = $_SESSION['db_prefix']."_compiled_jobs";
$jobQuery = "SELECT job_card_no, project, cust_id  FROM $table_name WHERE captured='yes' AND completed IS NULL ORDER BY job_card_no DESC";
$jobResult = execsql($jobQuery);
$jobNum = mysql_num_rows($jobResult);
  $w = 0;
  $arrId4 = array ();
  $arrName4 = array ();
  while ($w < $jobNum) {
    $cust_id = mysql_result($jobResult, $w, "cust_id"); 
    $table_name2 = $_SESSION['db_prefix']."_customer";
    $customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
    $customer_result = mySQL_query($customer_query);
    $customer_num = mysql_num_rows($customer_result);  
    $a1 = mysql_result($customer_result, 0, "account_type");
      if ($a1=="corporate") { 
        $cust_name = mysql_result($customer_result, 0, "comp_name"); 
      } 
      else {
        $cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
       }
    $arrId4[$w] = mysql_result($jobResult, $w, 'job_card_no');
    $arrName4[$w] = mysql_result($jobResult, $w, 'job_card_no').' '.$cust_name.' ('.mysql_result($jobResult, $w, 'project').')';
    $w++;
  }  
    
?>
<head>
<!--jscript-->
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/upper_case.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script>  

<!--style sheets-->
<link href="../css/wait.css" rel="stylesheet" type="text/css">

</head>
<?php 
$page_name = "Stores-purchase";
include ("../navigation/access.php");

if ($permission_num==1) {
   
  ?>
<?php include('../include/wait.php') ?>

<body onLoad="MM_showHideLayers('wait','','hide'); <?php if ($_SESSION['ordered_for']=='Order') {?> document.getElementById('order_select').disabled = false; document.getElementById('order_select').selectedIndex=1;<?php } else {?> document.getElementById('order_select').disabled = true; <?php } ?>" onFocus="MM_showHideLayers('wait','','hide'); "  onBlur="MM_showHideLayers('wait','','hide');">
<div class="wizard_page_middle_frame">
<form id="form1" method="get" action="purchaseWizbase.php">
<div class="wizard_frame">
Purchase order for:
  <label class="star">*</label>
&nbsp;&nbsp;
<input type="radio" name="company_group" value="Manase" id="company_group_0" <?php if ($_SESSION['company']=='Manase') { ?> checked=checked; <?php } ?> >
      Manase
   <input type="radio" name="company_group" value="M-works" id="company_group_1" <?php if ($_SESSION['company']=='M-works') { ?> checked=checked; <?php } ?> >
      M-works
</div>
  <div class="wizard_frame">
      <label class="label_std" >Purchase from:&nbsp;&nbsp;</label>
      <label class="star">*</label>

      <select name="supplier_id_select" id="supplier_id_select" class="select200">
        <option value="<?php echo $_SESSION['supplier_id']?>" ><selected="selected"><?php echo $_SESSION['sup_name'];?></option>
            <?php 
      for ($w = 0; $w < count($arrId2); $w++) {
    echo "<option value=\"".$arrId2[$w] . "\"" . ">" . $arrName2[$w] . "</option>";  
      }
     ?>
          </select>
&nbsp;&nbsp;  </div>
        <br />
        <br />
        
    <div class="wizard_frame">
    <table width="600" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="291"> Ordered by:&nbsp;
          <label class="star">*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<select name="ordered_by_select" class="select120"  >
  <option value="<?php echo $_SESSION['ordered_by'] ?>" selected="selected"><?php echo $_SESSION['ordered_by']; ?></option>
  <?php
          for ($j = 0; $j < count($arrId3); $j++) {
        echo "<option value=" . "\"" . $arrId3[$j] . "\"" . ">" . $arrName3[$j] . "</option>";
  }
?>
</select>
          </label></td>
        <td colspan="2">&nbsp;</td>
        <td width="144">&nbsp;</td>
      </tr>
  <tr>
    <td>&nbsp;</td>
    <td width="30">&nbsp;</td>
    <td width="135">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

    </div> 
    
     <div class="wizard_frame">
     Order for:
       <label class="star">*</label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <label>
    <input type="radio" name="order_radio_group" value="Stores" id="order_radio_group_0" onClick="document.getElementById('order_select').disabled = true; document.getElementById('order_select').selectedIndex=0;" <?php if ($_SESSION['ordered_for']=='Stores') {?> checked <?php } ?> >
    Stores</label>
  <label>
    <input type="radio" name="order_radio_group" value="Order" id="order_radio_group_1" onClick="document.getElementById('order_select').disabled = false; document.getElementById('order_select').selectedIndex=1;" <?php if ($_SESSION['ordered_for']=='Order') {?> checked <?php } ?>>
    Order</label>
  :&nbsp;&nbsp; 
  <select name="order_select" class="selectbox300" id="order_select" >
    <option value="-">-</option>
    <option value="<?php echo $_SESSION['order_no']?>"><?php echo $_SESSION['order_text']; ?></option>
    <?php
      for ($j = 0; $j < count($arrId4); $j++) {
      echo "<option value=".$arrId4[$j].">".$arrName4[$j]."</option>";  
    }
     ?>
  </select>
    </div>   
     
    <div class="wizard_frame">
          <table width="500" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="97">Purchase date:</td>
              <td width="403"><?php

    $myCalendar = new tc_calendar("purchase_date", true);
    $myCalendar->setIcon("../images/iconCalendar.gif");
    $myCalendar->setDate($d, $m, $y);
    $myCalendar->setPath("./");
    $myCalendar->setYearInterval(2010, 2099);
    $myCalendar->writeScript();
?></td>
            </tr>
          </table>
        </div>
    <div class="wizard_spacer_frame" style="height:130px;"></div>
        <div class="wizard_frame">
        <table width="590" height="50" align="center" id="table_button_bar">
          <tr>
            <td width="117">&nbsp;</td>
            <td width="115">
              <input name="s" type="hidden" id="s" value="1" />
              <input name="p" type="hidden" id="p" value="1" />
              </td>
            <td width="99">&nbsp;</td>
            <td width="125"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="needToConfirm = false; cancel_confirmation(1, '../store/store_main.php');" value="Cancel"  tabindex="-1"/></td>
            <td width="110" align="left"><input type="submit" name="next_button" id="next_button" class="imagebutton" value="Next" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');" /></td>
          </tr>
        </table>
        </div>
</form>
</div>
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("company_group","selone","- company.");
  frmvalidator.addValidation("supplier_id_select","req","- a supplier.");
  frmvalidator.addValidation("ordered_by_select","req","- ordered by.");
  frmvalidator.addValidation("order_radio_group","selone","- ordered for.");
  frmvalidator.addValidation("order_select","req","- order number.");
  
</script>


<?php } else { 
    $_SESSION['denied_return'] = "store/store_main.php?date=";  ?>
  <body onLoad="needToConfirm = false; accessdenied(); return false">
</body>
<?php }
?>

<html>