<?php    include_once('config/db.php');  include_once('config/check.php'); include_once('functions/expenses.php');


if (filter_has_var(INPUT_POST, "edit")) 
  try{
        $id = $_POST['edit']; 
   
        if($id == '')
              {   header("location:acc_recview.php");   }

        else  {              
      
                        $stmt = $conn->prepare(" SELECT * from acctransaction a WHERE a.id=? ");
                        $stmt->execute([$id]);  $details = $stmt->fetch(); 
            
                        $items = $conn->query(" SELECT  * from acctransaction_item a  where a.transactionid =  '".$details["id"]."' and a.total>0     ")->fetchAll();
                }
      } catch(Exception $e) { echo 'Message: ' .$e->getMessage();} 


 if (filter_has_var(INPUT_POST, "submit")) 
  try{  
        $cid =              htmlspecialchars (strtoupper($_POST['cid']));
         $startdate =         htmlspecialchars (strtoupper($_POST['startdate']));
         $startdate = date("Y-m-d", strtotime($startdate));
        
       // $partner =      0; //   
        $memo   =            htmlspecialchars ($_POST['memo']);              
        // $description =       htmlspecialchars ($_POST['description']);     
         
        // $amount   =       htmlspecialchars (strtoupper($_POST['g_total']));
        // $glamount =  $amount  ;

        // $rid =                $_POST['rid'];
        // $coa =                $_POST['coa'];
        
        // $name =              $_POST['name'];
        // $unit =              $_POST['unit'];
        // $uom =              $_POST['uom'];
        // $price =             $_POST['price'];
        // $vat =              0; 
        // $vatamount =       0 ;
        // $gross =             $_POST['total'];
        // $payable =           $_POST['payable'];
        
        // $number = count($_POST["coa"]);  

       

        $sql = "UPDATE acctransaction SET date=:startdate,enddate=:enddate,memo=:memo WHERE id=:id "; 
        $conn->prepare($sql)->execute(['startdate'=>$startdate,'enddate'=>$startdate,'memo' => $memo,'id'=>$cid  ]); 

     

        

            
        header("location:acc_recview.php"); 
} catch(Exception $e) { echo 'Message: ' .$e->getMessage();}
       
       



?>

<!DOCTYPE html>
<html>
<head>
<?php include_once('inc/link.php');   ?>  
</head>
<body <?php include_once('inc/nav.php');  ?> >
<div class="wrapper">

  <!-- Main Header -->
    <header class="main-header">

    <!-- Logo -->
       <?php include_once('inc/brand.php');   ?>

    <!-- Header Navbar -->
    <?php include_once('inc/navbar.php');   ?>

  </header>
 
  <!-- Left side column. contains the logo and sidebar -->
    <aside class="main-sidebar">

    <!-- sidebar: style can be found in sidebar.less -->
     <?php include_once('inc/menu.php');   ?>
    <!-- /.sidebar -->
  </aside>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">

  <section class="content-header" >
    <div class="row">
      <div class="">
         <div class="col-md-4">
              <a href="acc_recview.php" class="btn btn-success"> <i class="fa fa-angle-left"></i>Go back </a>
             
         </div>
         <div class="col-md-4">
             <p style="color:green; font-size:20px ;font-weight:bold; " > <i class="fa fa-file-o" ></i> Update bulk payment transaction  </p>
     
         </div>
         <div class="col-md-4">
             
         </div>
      </div>
   
    </div>
   
  </section>

 


  <!-- Main content -->
  <section class="content container-fluid" style="padding-top:0px;" >
    <div class="">
      <div class="box box-warning box-xs">
              <div class="box-body">
                  <div class="row">
             
                    <form class="form-horizontal "  action="" method="post"  >
                        <div class="row">
                                                                           
                                  <div class="col-md-12 "  > 
                                           
                                                        
                                        <!-- CURRENT DATE  --->
                                            

                                                <div class="col-md-2">
                                                    <label > Start Date  </label>
                                                    <input type="hidden" class="" autocomplete="off" value="<?php  echo $details["id"]?>"  name="cid">    
                                                    <input type="text" class="form-control datepickere " autocomplete="off" value="<?php  echo $details["date"]?>"  name="startdate">    
                                                </div>                                                                  
                                                                      
                                                <div class="col-md-2">
                                                   <label>  Memo/ Reference No  </label>
                                                   <input type="text" class="form-control" name="memo" required=''  value="<?php  echo $details["memo"]?>" autocomplete="off">
                                                </div> 
                                                <div class="col-md-3  ">
                                                      <label> Customer* </label>
                                                        <select class="form-control  js-example-basic-single  "  required="" name="partner" 
                                                        style="width:100%" >
                                                            <?php  $stmt = $conn->prepare("SELECT id,name  FROM bppartner "); $stmt->execute();
                                                            foreach($stmt as $row)
                                                                {
                                                                    if( $row['id'] == $details["bpartner"] )
                                                                            { ?>
                                                                                <option selected value="<?php echo $details["bpartner"]  ?>" > 
                                                                                        <?php echo $row['name'] ?>  <option>
                                                                                <?php  }    
                                                                    else  { ?>
                                                                                    
                                                                                    <option  value="<?php echo $row['id']; ?>" > 
                                                                                            <?php echo $row['name'] ?>  <option>
                                                                                    <?php } 
                                                                    } 
                                                            ?>
                                                        </select> 

                                                </div> 
                                                <div class="col-md-3">
                                                    <label>  Description   </label>
                                                    <input type="text" name="description"  class="form-control"  value="<?php  echo $details["description"]?>" autocomplete="off">
                                                </div>
                                                <div class="col-md-2">
                                                        <label> Total </label>
                                                        <input type="text"    name="g_total" readonly="true"  id="quotepay"class="form-control totalsum"  value="<?php  echo $details["amount"] ?>"  >
                                                </div>
                                               
                                              <!---  QUOTE PRODUCTS AND DETAILS   --->
                                                  <!---  QUOTE HEADER  --->                               
                                                    <div class="col-xs-12">
                                                        <div class="" style="margin-top:5px;">
                                                        <table class="table  table-condesed table-sm table-condensed    item_table " id="item_table" >
                                                        <tr  >
                                                            <th style="width:20%;">   invoice Number  </th>
                                                            <th style="width:20%;">   Details   </th>
                                                        
                                                            <!-- <th style="width:13%;">   Uom  </th> -->
                                                            <th style="width:13%;">   Amount  </th>
                                                        
                                                        </tr>
                                                        <?php   $i = 0; foreach($items as $item )
                                                                 { extract($item); 
                                                              
                                                                 
                                                                  $i++;                                
                                                            ?>	
                                                                                       
                                                                <tr id="row'+i+'">
                                                                  
                                                                <td style="border:none;" >  <input type="" name="rid[]"  readonly="true"  value="<?php echo $b_invoiceid; ?>" id="rid_'+i+'" class="form-control table-condesed rid" />  </td>
                                                                <td style="border:none;" >  <input type="" name="rid[]"  readonly="true"  value="<?php echo $name; ?>" id="rid_'+i+'" class="form-control table-condesed rid" />                           
                                                                                            
                                                                </td>
                                                                <!-- <td style="border:none;" > <input type="" name="name[]"    value="<?php echo $currency; ?>" id="name_'+i+'" class="form-control table-condesed item_name" /></td>
                                                                -->
                                                                <td style="border:none;" > <input type="" name="unit[]"  readonly="true"  value="<?php echo $total; ?>" id="unit_'+i+'" class="form-control table-condesed item_unit" /></td>  
                                                                
                                                                </tr>
                                                            
                                                            <?php } ?>
    
                                                  </table>
                                                           
                                                        </div>
                                                    </div>
                                                    <!---  QUOTE HEADER  ---> 
                                                
                                                                         
                                        
                                          <!-- submit button -->
                                        <div class="form-group " >
                                          <div class="col-sm-offset-4 col-xs-10" style="padding-top:10px;">                             
                                          <button type="submit"name="submit" class="btn btn-warning col-xs-4">
                                                <i class="fa fa-plus "></i>  Update transaction details   </button>
                                                </div>
                                    </div>
                            
                        </div>
                    </form>
                        
                    </div>
                </div>
            </div>
      </div>
   </section>
</div>
  <!-- /.content-wrapper -->
  
  <!-- Main Footer -->
  <footer class="main-footer">
    <?php  include_once('inc/footer.php'); ?>
  </footer>
  
</div>
                         
<!-- ./wrapper -->

<!-- REQUIRED JS SCRIPTS -->
<?php include_once('inc/linkjs.php') ?>

</body>
</html>

<!-- dynamic rows start here -->
<script>
    
$(document).ready(function(){
// loading the footer details


$("#item_table").on('change', ".item_unit", function()
         {                     
                      var unit =      $(this).closest('tr').find('.item_unit').val();
                      
                      $(this).parents("tr").find(".item_total").val(unit) ;             
                       calculatePayable ();                   
          }); 

                                                    
          
                                   function calculatePayable () { $(".item_table").each(function () {  
                                                     var sum = 0; 
                                                      $("input[name='unit[]']").each(function () {
                                                      if (!isNaN(this.value) && this.value.length != 0) 
                                                             { sum += parseFloat(this.value);  }
                                                                       $("input[name='g_total']").val(sum);
                                                                      
                                                                                                    });  
                                                                                      });
                                                                 }
                        
                                    
                                                       
                                                  

                   });
</script>
<!-- DYANAMIC ROWS LISTING HERE --->

<!-- DYNAMIC ROWS ENDS HERE --->