<?php session_name('CAKEPHP'); session_start();
error_reporting(true);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
$title = _('Purchase Order Register');
$PageName ='Purchase Order Register'; 
?>
<script language="javascript" type="text/javascript">
function validate_branch()
{
  $branch = $('#site').val();
  if($branch == "")
  {
    alert("Select Branch");
    return false;
  }
  return true;
}
function get_itemsubgroup(str)
{
  if(str=="")
  {
  document.getElementById("sub_id").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("sub_id").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","item_subcategory.php?main="+str,true);
xmlhttp.send();
}
function get_item(str)
{
if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?sub="+str,true);
xmlhttp.send();
}
function showItems(str)
{
    if(str=="")
  {
  document.getElementById("itemid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("itemid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Get_itemcode.php?partName="+str,true);
xmlhttp.send();
}



function showsitename160(str)
{

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
  var data=xmlhttp.responseText.split("__");
        alert(xmlhttp.responseText);
  document.getElementById("storedata7").innerHTML=data[0];
    }
  }
  xmlhttp.open("GET","get_sitenamedata2.php?site="+str,true);
  xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);  
}
function showVendor(str)
{

var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
  //var data=xmlhttp.responseText.split("__");  
  //alert(data);
  //document.getElementById("vender").innerHTML= xmlhttp.responseText;
        document.getElementById("item").innerHTML=xmlhttp.responseText;
    
  
    }
  }

xmlhttp.open("GET","check_data_fin.php?section=VendornameQuotReg&site="+str,true);
xmlhttp.send();
}
</script>
  <link rel="stylesheet" href="css/new_mxerp.css">
  <div class="body_contain">
<div class="tab_div">
  <form method="post" action="">
      <h1>Vendor Wise Purchase Order</h1>
      <table width="100%" align="center" border="0" cellspacing="0" cellpadding="3">
          <tr class="row1">
            <td width="13%" align="right">Branch</td>
            <td width="35%">
              <select name="site" id="site" onChange="showVendor(this.value)"> 
                <option value="">Select</option>
                    <?php  $ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";
                           $site_sql=DB_query($ss1,$db);
                           while($site_row=DB_fetch_array($site_sql))
                           {
                              $p='';
                              if($site_row['loccode'] == $_POST['site'])  
                                  $p="selected";?>
                              <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
                     <?php } ?>
               </select>
            </td>
            <td width="13%" align="right">Vendor</td>
            <td>
              <select name="item" id="item">
                <option value="0">All</option>
                  <?php $SQLEmp=DB_query("SELECT * FROM  suppliers where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
                   while($EmpId=DB_fetch_array($SQLEmp))
                  { ?>
                    <option value="<?php echo $EmpId['id']; ?>"<?php if($EmpId['id'] == $_POST['item']) { echo 'selected="selected"'; } ?>><?php echo $EmpId['vendor_name']; ?></option>
            <?php } ?>  
            </select>
            </td>
        </tr>
        <tr class="row1">
           <td align="right">From Date</td>
           <td><input type="text" name="from_date" value="<?php echo $_REQUEST['from_date'];?>" class="datepicker"></td>
           <td align="right">To Date</td>
           <td><input type="text" name="to_date" value="<?php echo $_REQUEST['to_date'];?>" class="datepicker"></td>
        </tr>
        <tr class="row1">
          <td style="text-align:center;" colspan="4"><input type="submit" name="view" value="Search" class="search" onclick="return validate_branch();"></td>
        </tr>
  </table>
</form>
<?php 
if($_REQUEST['view']!='')
{
        $filter=array();
    $fromDate=$_REQUEST['from_date'];
    $fromDateBrk=explode("/",$fromDate);
    $fromDate=$fromDateBrk[0];
    $fromMonth=$fromDateBrk[1];
    $fromYear=$fromDateBrk[2];
    $From=$fromYear."-".$fromMonth."-".$fromDate;
    $toDate=$_REQUEST['to_date'];
    $toDateBrk=explode("/",$toDate);
    $toDate=$toDateBrk[0];
    $toMonth=$toDateBrk[1];
    $toYear=$toDateBrk[2];
    $To=$toYear."-".$toMonth."-".$toDate; 
   
   
   if($_REQUEST['item']=='0')
   {
     $query1="SELECT DISTINCT(vendorId) FROM purchorders";
    $result1=DB_query($query1,$db);
    while($row=DB_fetch_array($result1))
        $vendors[]=$row['vendorId'];
   }
   else
   {
    $vendors[]=$_REQUEST['item'];
   }     
  }
?>
<?php if($_REQUEST['view']!='')
{?>
<p align="center"><strong><?php echo $message; ?></strong> <a href="vendorwisereportprint.php" target="_blank" class="search" style="float:right;">Print</a></p>
      
      <table width="100%" cellpadding="3" cellspacing="0" border="0" class="thborder">
      <tr>
          <th width="4%">S. No.</th>
          <th width="6%">Date</th>
          <th width="12%">Project Name</th>
          <th width="12%">Order No.</th>
          <th>Product Name</th>
          <th width="6%" class="text-right">Qty.</th>
          <th width="6%" class="text-right">Rate</th>
          <th width="6%" class="text-right">Tax %</th>
          <th width="6%" class="text-right">Tax Amt.</th>
          <th width="6%" class="text-right">Amount</th>
      </tr>
  <?php 
      if($_REQUEST['site']!='')
      $filter[]="purchorders.site='".$_REQUEST['site']."'";
      if($_REQUEST['from_date']!='0'&& $_REQUEST['to_date']!='')
        $filter[]="purchorders.po_date between '".$From."' and '".$To."'";
      if(!empty($filter))
      $filter2=implode(" and ",$filter);
     
      $sitename=$s->getField('locations','loccode',$_REQUEST['site'],'locationname');
     
      $sitename1=$s->getField('suppliers','id',$_REQUEST['vendor'],'vendor_name');
     
      if($_REQUEST['site']!='')
        $message="Showing Records of Branch ".$sitename;
     
      if($_REQUEST['from_date']!='' && $_REQUEST['to_date']!='')
        $message .=" From  ".$From." to ".$To;
     
      $products =array();
      
      for($i=0;$i<count($vendors);$i++)
        {

          $filter1='';
          if($vendors[$i]!='')
            $filter1[]="purchorders.vendorId ='".$vendors[$i]."'"; 
          if(!empty($filter1))
              $querys= implode(" and ",$filter1);
          $totalPrice='';
          $sno=1;
          $query1="SELECT purchorders.* , purchorderdetails.mst_itemid, purchorderdetails.Quantity , purchorderdetails.Rate , purchorderdetails.totalPrice,purchorderdetails.gst_percent,purchorderdetails.vat_value FROM purchorders ,purchorderdetails  WHERE purchorders.id = purchorderdetails.parantId and ".$filter2." AND ".$querys."";
         

          $result=DB_query($query1,$db);

          $count=DB_num_rows($result);
          if(DB_num_rows($result)>0)
          {
            $totalQty = 0;
            $totalTax=0;
            $itemName='';?>
            <tr>
                <td colspan="10" align="left" style="background:#e4e4e4; color:#00463f; font-weight:bold;"><?php echo $vendor_name = $s->getField('suppliers','id',$vendors[$i],'vendor_name'); ?></td>
            </tr>
            <?php 
              while($myrow=DB_fetch_array($result))
              { ?> 
                <tr>
                    <td ><?php echo $sno;?></td>
                    <td><?php echo $podate = ConvertSQLDate($myrow['po_date']);?></td>
                    <td><?php echo $myrow['site'];?></td>
                    <td><?php echo $myrow['purchase_ordNo'];?></td>                     
                    <td><?php echo $itemname=$s->getField('item_master','id',$myrow['mst_itemid'],'item_name'); ?></td>
                    <td style="text-align:right;"><?php $totalQty += $myrow['Quantity']; echo $myrow['Quantity']; ?></td>
                    <td style="text-align:right;"><?php echo  $myrow['Rate'];?></td>
                    <td style="text-align:right;"><?php echo  $myrow['gst_percent'];?></td>
                    <td style="text-align:right;"><?php $totalTax += $myrow['vat_value']; echo  $myrow['vat_value'];?></td>
                    <td style="text-align:right;"><?php $totalPrice += $myrow['totalPrice']; echo $myrow['totalPrice']; ?></td>
                 </tr>
            <?php   
                  $itemName = $myrow['vendorId'];
                  $products[] =array($vendor_name,$sno,$podate,$myrow['site'],$myrow['purchase_ordNo'],$itemname,$myrow['Quantity'], $myrow['Rate'],$myrow['totalPrice'],$count,$myrow['gst_percent']);
                  $sno++;
                 }
               if($itemName!=$myrow['vendorId'])
                  echo "<tr bgcolor='#fff'><td colspan='5' align='right' style='text-align:right;'><strong>Total</strong></td><td style='text-align:right;'><strong>$totalQty</strong></td><td colspan='1' style='text-align:right;'></td><td colspan='1' style='text-align:right;'></td><td style='text-align:right;'><strong>$totalTax</strong></td> <td  align='right' style='text-align:right;'><strong>$totalPrice</strong></td>";
           
            } ?>
           <?php $_SESSION['products'] = $products; 
         } ?>
 
</table>
<?php } ?>

</div>
</div>