<?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 = _('ItemWise Purchase Status');
$PageName ='ItemWise Purchase Status'; 
setlocale(LC_MONETARY, 'en_IN');
?>
<script language="javascript" type="text/javascript">
function validate_branch()
{
  $branch = $('#site').val();
  if($branch == "")
  {
    alert("Select Branch");
    return false;
  }
  return true;
}

</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 Status</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"> 
                <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="client" id="client">
                <option value="0">All</option>
                  <?php $SQLEmp=DB_query("SELECT * FROM  suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' order by vendor_name",$db);

                   while($EmpId=DB_fetch_array($SQLEmp))
                  { ?>
                    <option value="<?php echo $EmpId['id']; ?>"<?php if($EmpId['id'] == $_REQUEST['client']) { 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 align="center" 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['from_date']!='' && $_REQUEST['to_date']!='')
       {
          $_SESSION['period']="From"."&nbsp".$_REQUEST['from_date']."&nbsp"."To"."&nbsp".$_REQUEST['to_date'];
          //echo $_SESSION['period'];
       }
   
   
           if($_REQUEST['client']=='0')
           {

               $query1="SELECT DISTINCT(vendorId) FROM purchorders";
               $result1=DB_query($query1,$db);

               while($row=DB_fetch_array($result1))
               {
                 $clients[]=$row['vendorId'];
               }  
              //  print_r($clients);
           }
           else
           {
            $clients[]=$_REQUEST['client'];
           }
       
  }
 //$_SESSION['period']="From"."&nbsp".$_REQUEST['from_date']."&nbsp".$_REQUEST['to_date'];
?>
<?php if($_REQUEST['view']!='')
{?>
      <p align="center"><strong><?php echo $message; ?></strong> <a href="partywisereportprint.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>Product Name</th> 
            <th width="10%" class="text-right">Qty.</th>
            <th width="10%" class="text-right">Rate</th>
            <th width="10%" class="text-right">Amount</th>
        </tr>
  <?php 
      if($_REQUEST['site']!='')
      $filter[]="purchorders.site='".$_REQUEST['site']."'";

      if($_REQUEST['from_date']!=''&& $_REQUEST['to_date']!='')
        $filter[]="purchorders.po_date between '".$From."' and '".$To."'";

      // if($_REQUEST['client_group']!='0')
      //$filter[]="clientmaster.comp_group='".$_REQUEST['client_group']."'";

      if(!empty($filter))
      $filter2=implode(" and ",$filter);
     
      $products =array();
      
      for($i=0;$i<count($clients);$i++)
        {

          $filter1='';
           if($clients[$i]!='')
             $filter1[]="purchorders.vendorId ='".$clients[$i]."'"; 

           if(!empty($filter1))
              $querys= implode(" and ",$filter1); 

          //$totalPrice='';
          $sno=1;
          $query1="SELECT purchorders.* , purchorderdetails.mst_itemid, purchorderdetails.Quantity, purchorderdetails.Rate , purchorderdetails.totalPrice FROM purchorders,purchorderdetails  WHERE purchorders.id = purchorderdetails.parantId and ".$filter2." AND ".$querys."  ";

         /* $query1="SELECT salesorder.* , salesorder_details.ItemCode, salesorder_details.stock_qty, salesorder_details.Rate , salesorder_details.Amount,clientmaster.comp_group FROM salesorder inner join salesorder_details on salesorder.id=salesorder_details.masterId left join clientmaster on  salesorder.client=clientmaster.id   WHERE ".$filter2." AND ".$querys."  ";*/
        //  echo $query1;
          
          $_SESSION['sql']=$query1;
          $result=DB_query($query1,$db);
          $count=DB_num_rows($result);
         
          if(DB_num_rows($result)>0)
          {
            $subtotal=0;
         
            ?>
            <tr>
                <td colspan="5" align="left" style="background:#e4e4e4; color:#00463f; font-weight:bold;">Vendor : <?php echo $client_name = $s->getField('suppliers','id',$clients[$i],'vendor_name'); ?></td>
            </tr>
            <?php 
              while($myrow=DB_fetch_array($result))
              { ?> 
                <tr>
                    <td><?php echo $sno;?></td>
                    <td><?php echo $itemname=$s->getField('item_master','id',$myrow['mst_itemid'],'item_name'); ?></td>
                    <td style="text-align:right;"><?php echo number_format((float)$myrow['Quantity'],2); ?></td>
                    <td style="text-align:right;"><?php echo $myrow['Rate'];?></td>
                    <td style="text-align:right;"><?php echo money_format('%!i',$myrow['totalPrice']);
                                                         $tot=$myrow['totalPrice'];
                                                      $subtotal=$subtotal+$tot;
                                                      $grand_total=$grand_total+$tot;
                            ?></td>
                 </tr>
            <?php   
                  $itemName = $myrow['client'];
                  $products[] =array($client_name,$sno,$itemname,$myrow['Quantity'], $myrow['Rate'],$myrow['totalPrice'],$count);
                  $sno++;
                 }
                 $_SESSION['products'] = $products; 
                 ?>
                 <tr>
                    <td colspan="4" style="text-align:right"><strong>SubTotal</strong></td>
                    <td style="text-align:right"><strong><?php echo money_format('%!i',$subtotal); ?></strong></td>
                 </tr>
                 <?php 
             /*  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;'>$totalQty</td><td colspan='1' style='text-align:right;'></td> <td  align='right' style='text-align:right;'>$totalPrice</td>"; */           
            } ?>
           <?php 
         } ?>
              <tr>
                  <td colspan="4" style="text-align:right"><strong>Grand Total</strong></td>
                  <td style="text-align:right"><strong><?php echo money_format('%!i',$grand_total); ?></strong></td>
              </tr>
      </table>
<?php } ?>

</div>
</div>