<?php 
  $header_file=APPPATH.'views/include/header.php';
  require($header_file);
  $sidenav_file=APPPATH.'views/include/sidenav.php';
  require($sidenav_file); ?>
<!-- BEGIN: Content-->
    <div class="app-content content">
      <div class="content-overlay"></div>
      <div class="content-wrapper">
        <div class="content-header row">
          <div class="content-header-left col-md-6 col-12 mb-2 breadcrumb-new">
            <h3 class="content-header-title mb-0 d-inline-block">GRN Invoice </h3>
            <div class="row breadcrumbs-top d-inline-block">
              <div class="breadcrumb-wrapper col-12">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="#">Home</a>
                  </li>
                  <li class="breadcrumb-item"><a href="#">Purchase Order</a>
                  </li>
                  <li class="breadcrumb-item active">GRN Invoice 
                  </li>
                </ol>
              </div>
            </div>
          </div>
         
        </div>
        <div class="content-body"><section class="card">
  <div id="invoice-template" class="card-body p-4">
    <!-- Invoice Company Details 
    <div id="invoice-company-details" class="row">
      <div class="col-sm-6 col-12 text-center text-sm-left">
        <div class="media row">
          <div class="col-12 col-sm-3 col-xl-2">
            <img src="../../../app-assets/images/logo/logo-80x80.png" alt="company logo" class="mb-1 mb-sm-0" />
          </div>
          <div class="col-12 col-sm-9 col-xl-10">
            <div class="media-body">
              <ul class="ml-2 px-0 list-unstyled">
                <li class="text-bold-800">Modern Creative Studio</li>
                <li>4025 Oak Avenue,</li>
                <li>Melbourne,</li>
                <li>Florida 32940,</li>
                <li>USA</li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      <div class="col-sm-6 col-12 text-center text-sm-right">
        <h2>INVOICE</h2>
        <p class="pb-sm-3"># INV-001001</p>
        <ul class="px-0 list-unstyled">
          <li>Balance Due</li>
          <li class="lead text-bold-800">$12,000.00</li>
        </ul>
      </div>
    </div>-->
    <!-- Invoice Company Details -->

    <!-- Invoice Customer Details 
    <div id="invoice-customer-details" class="row pt-2">
      <div class="col-12 text-center text-sm-left">
        <p class="text-muted">Bill To</p>
      </div>
      <div class="col-sm-6 col-12 text-center text-sm-left">
        <ul class="px-0 list-unstyled">
          <li class="text-bold-800">Mr. Bret Lezama</li>
          <li>4879 Westfall Avenue,</li>
          <li>Albuquerque,</li>
          <li>New Mexico-87102.</li>
        </ul>
      </div>
      <div class="col-sm-6 col-12 text-center text-sm-right">
        <p><span class="text-muted">Invoice Date :</span> 06/05/2019</p>
        <p><span class="text-muted">Terms :</span> Due on Receipt</p>
        <p><span class="text-muted">Due Date :</span> 10/05/2019</p>
      </div>
    </div>-->
    <!-- Invoice Customer Details -->

    <!-- Invoice Items Details -->
    <div id="invoice-items-details" class="pt-2">
      <div class="row">
        <div class="table-responsive col-12">
        <?php
          foreach($get_ordered_gnr as $grn)
        ?>
          <table class="table table-bordered zero-configuration">
                    <tr class="main_heading">
                      <th  colspan="17">Goods Received Note (GRN) Invoice View<span class="pull-right"><?php echo $grn->user_name; ?></span></th>
                    </tr>
                    <tr class="data_details">
                      <th colspan="3"><span>PO No : </span><?php echo $grn->purchase_no; ?></th>
                      <th colspan="3"><span>PO Date : </span><?php echo $grn->purchase_date; ?></th>
                      <th colspan="3"><span>GRN No : </span><?php echo $grn->grn_no; ?></th>
                      <th colspan="3"><span>GRN Date : </span><?php echo $grn->grn_date; ?></th>
                      <th colspan="3"><span>Supplier Name : </span><?php echo $grn->venders_name; ?></th>
                      <th colspan="2" >Invoice : <?php echo $grn->invoice_no; ?></th>
                    </tr>
                    <tr class="data_details">
                      <th colspan="8"><span>Purchase Order </th>
                        <th colspan="9"><span>Goods Receive Notes</th>
                        </tr>
                      </table>
                      <table  class="table table-bordered zero-configuration dataTable">
                        <thead>
                          <tr class="data_heading">
                            <th>Product Name</th>
                            <th>UOM</th>
                            <th>Qty</th>
                            <th>Price</th>
                            <th>Qty</th>
                            <th>Price</th>
                            <th>Tax(%)</th>
                            <th>Tax Amt</th>
                            <th>Total Amount</th>
                            <th>Remarks</th>                          
                          </tr>
                        </thead>
                        <tbody>
                          <?php
                           $count = 1;
                            foreach($grn_item as $data){

                              ?>
                              <tr data-row-id1="<?php echo $data->purchase_grn_id;?>" class="data_list">
                                <td class="editable-col1" contenteditable="false" col-index='1' oldVal ="<?php echo $data->item_name;?>" ><?php echo $data->item_name; ?></td>
                                <td class="editable-col1" contenteditable="false" col-index='2' oldVal ="<?php echo $data->unit_measurement;?>" ><?php echo $data->unit_measurement; ?></td>
                                <td class="editable-col1" contenteditable="false" col-index='3' oldVal ="<?php echo $data->required_qty;?>" ><?php echo $data->required_qty; ?></td>
                                <td class="editable-col1" contenteditable="false" col-index='4' oldVal ="<?php echo $data->item_price;?>" ><?php echo $data->item_price; ?></td>
                                <td class="editable-col1" contenteditable="false" col-index='5' oldVal ="<?php echo $data->grn_item_qty;?>" ><?php echo $data->grn_item_qty; ?></td>
                                <td class="editable-col1" contenteditable="true" col-index='6' oldVal ="<?php echo $data->grn_item_price;?>" ><?php echo $data->grn_item_price; ?></td>
                                <td class="editable-col1" contenteditable="true" col-index='7' oldVal ="<?php echo $data->grn_tax;?>" ><?php echo $data->grn_tax; ?></td>
                                <td class="editable-col1" contenteditable="false" col-index='8' oldVal ="<?php echo $data->grn_tax_amount;?>" ><?php echo $data->grn_tax_amount; ?></td>
                                <td class="editable-col1" contenteditable="false" col-index='9' oldVal ="<?php echo $data->grn_grand;?>" ><?php echo $data->grn_grand; ?>
                                 
                                </td>
                                <td class="tiptext">
                                  <?php 
                                    //$mid = $mid;
                                    $pg_id =$data->purchase_grn_id;
                                    $query = $this->db->query("select * from remarks where gnr_id='$mid' and purchase_grn_id='$pg_id' and status='UnSeen'");
                                    foreach($query->result_array() as $row3){
                                    ?>
                                    <p class="user_color"><strong><?php echo $row3['remark']; ?></strong></p>
                                    <?php }
                                  ?>
                                  <?php 
                                    //$mid = $_GET['mid'];
                                    $pg_id =$data->purchase_grn_id;
                                    $query1 = $this->db->query("select * from remarks where gnr_id='$mid' and purchase_grn_id='$pg_id' and status='Seen'");
                                    foreach($query1->result_array() as $row4){
                                    ?>
                                    <?php echo $row4['remark']; ?>
                                    <?php }
                                  ?>
                                  <div class="description">
                                    <div class="uk-width-medium-1-1 details_discription">
                                      <?php 
                                        $item_id = $data->item_id;
                                       
                                        $query2 = $this->db->query("select * from remarks where gnr_id='$mid' and item_id='$item_id' and purchase_grn_id='$pg_id'");
                                         foreach($query2->result_array() as $row5){
                                       
                                        ?>
                                        <p class="admin_color"><strong><?php echo $row5['frm_user'];?></strong></p>
                                        <p>
                                          <?php echo $row5['remark'];?>
                                        </p>
                                      <?php } ?>
                                    </div>
                                    <div class="uk-width-medium-1-1">
                                      <div class="uk-form-row">
                                        <div class="md-input-wrapper"><label for="fullname">Enter Remarks</label><span class="md-input-bar"></span></div> </br>
                                      </div>
                                      <div class="uk-form-row">
                                        <table >
                                          <tr data-row-id="<?php echo $data->purchase_grn_id;?>" data-item-id="<?php echo $data->item_id;?>" >
                                            <td class="editable-col" contenteditable="true" col-index='0' width="100"></td>
                                            <td>
                                              <button class="md-btn md-btn-primary" onclick="myFunction()">Submit <!--<i class="material-icons" style="color: #fafafa;">autorenew</i>--></button> 
                                            </td>
                                          </tr>
                                        </table>
                                        <input type="hidden" name="user" id="user" value="<?php echo $_SESSION['username']; ?>" >
                                        <input type="hidden" name="gnrid" id="gnrid" value="<?php echo $data->gnr_id; ?>" >
                                        <input type="hidden" name="login_id" id="login_id" value="<?php echo $_SESSION['user_id']; ?>" >
                                      </div>
                                    </div>
                                  </div>
                                </td>
                              </tr>
                              
                          <?php $count++; } ?>
                          
                          
                          <?php
                            
                              foreach($grn_sumamount as $gsum){  
                            ?>
                            <tr>
                              <td colspan="7" >Total Amount</td>
                              <td ><?php echo $gsum->sum_tax_amount; ?></td>
                              <td ><?php echo $gsum->sum_amount; ?></td>
                              <td></td>
                            </tr>
                            <?php if($grn->courier != 0) { ?>
                              <tr>
                                <td colspan="6"></td>
                                <td colspan="2" >Courier Charge</td>
                                <!--<td style="font-weight:bold"><?php //echo $row['courier']; ?></td>-->
                                <td ><?php echo $grn->courier; ?></td>
                                <td></td>
                              </tr>
                            <?php } ?>
                            <?php if($grn->transport != 0) { ?>
                              <tr>
                                <td colspan="6"></td>
                                <td colspan="2" >Transport Charge</td>
                                <!--<td style="font-weight:bold"><?php //echo $row1['sum_tax_amount']; ?></td>-->
                                <td ><?php echo $grn->transport; ?></td>
                                <td></td>
                              </tr>
                            <?php } ?>
                            <?php if($grn->insurance != 0) { ?>
                              <tr>
                                <td colspan="6"></td>
                                <td colspan="2" >Insurance Charge</td>
                                <!--<td style="font-weight:bold"><?php //echo $row1['sum_tax_amount']; ?></td>-->
                                <td ><?php echo $grn->insurance; ?></td>
                                <td></td>
                              </tr>
                            <?php } ?>
                            <tr>
                          <td colspan="7" >Total Charge</td>
                          <td ><?php //echo $row1['sum_tax_amount']; ?></td>
                          <td ><strong><?php echo $grn->tot_amt; ?></strong></td>
                          <td></td>
                        </tr>
                        <tr>
                          <td colspan="7" >Round Off</td>
                          <td ><?php //echo $row1['sum_tax_amount']; ?></td>
                          <td ><strong><?php echo $grn->round_off; ?></strong></td>
                          <td></td>
                        </tr>
                        <tr>
                          <td colspan="7" >Final Charge</td>
                          <td ><?php //echo $row1['sum_tax_amount']; ?></td>
                          <td ><strong><?php echo $grn->total_amount; ?></strong></td>
                          <td></td>
                        </tr>
                            <?php
                            }
                          ?>  
                          
                        </tbody>
                      </table>                      
                  <table width="100%" class="table table-bordered zero-configuration " >
              <thead>
               
                <tr>
                <th colspan="11">Rejected Products</th>
                </tr>
                <tr class="data_heading">
                  <th>Product Name</th>
                  <th>UOM</th>
                  <th>PO Qty</th>
                  <th>GRN Qty</th>
                  <th>GRN Price</th>
                  <th>RJN Qty</th>
                  <th>Price</th>
                  <th>Tax</th>
                  <th>Amount</th>
                  <th>Remarks</th>
                </tr>
              </thead>
              <tbody>
                        <?php
               
         foreach($rejection_view as $rejv){ 
    ?>   
                <?php
                  $rmid = $rejv->rejection_id;
                  $count = 1;
                 
                   $query3 = $this->db->query("SELECT *,ROUND(J.rejection_total_amount,2) as rejection_total_amount FROM purchase_order P inner join purchase_order_item  I on  P.purchase_id=I.purchase_id inner join purchase_order_grn O on I.purchase_order_id=O.purchase_order_id inner join  purchase_order_rejection J on O.purchase_grn_id=J.purchase_grn_id where J.rejection_id='$rmid' and J.rejection_qty !=''");
                      foreach($query3->result_array() as $row6){
                  
                    ?>
                    <tr class="data_list">
                      <td><?php echo $row6['item_name']; ?></td>
                      <td><?php echo $row6['unit_measurement']; ?></td>
                      <td><?php echo $row6['required_qty']; ?></td>
                      <td><?php echo $row6['grn_item_qty']; ?></td>
                      <td><?php echo $row6['grn_item_price']; ?></td>
                      <td><?php echo $row6['rejection_qty']; ?></td>
                      <td><?php echo $row6['rejection_price']; ?></td>
                      <td><?php echo $row6['rejection_tax']; ?></td>                 
                      <td><input type="hidden" class="rej_amt" value="<?php echo $row6['rejection_total_amount']; ?>"><?php echo $row6['rejection_total_amount']; ?></td>
                      <td><?php echo $row6['rejection_remarks']; ?></td>
                    </tr>
                     
                <?php $count++; } ?>
                
                <?php } ?>
                
                  <tr>
                    <td colspan="8" >Total Amount</td>
                    <td ><input type="hidden" id="grn_amt" value="<?php echo $grn->total_amount; ?>">
                      <strong><span id="rej_tot"> </span>
                      </strong></td>
                    <td></td>
                  </tr>
                  <tr> 
                  <td colspan="8" >GRN Total After Rejection </td>
                    <td ><strong><span id="rej_aftr_tot"> </span></strong></td>
                    <td></td>
                
                    
              </tbody>
            </table>
      
                    </div>
                    <br/>
                    


                  </div>
               
                <br/>
          <div class="row justify-content-md-center">
                      <!--<div class="col-md-4">
                        <div class="md-card-toolbar-actions hidden-print" >
                          <a class="md-btn md-btn-flat md-btn-flat-primary" href="grn_list.php?page_id=purchase-grn-list">Back</a>
                        </div>
                      </div>-->
                      <?php
                        
                          foreach($ordered_gnr_approve as $ognrapp){  
                        ?>
                        <div class="col-md-8 print_receipt">
                          <div class="md-card-toolbar-actions hidden-print" >
                            <?php
                              $cur_date = date("d-m-Y h:i");
                            ?>
                            <input name="gnr_id" id="gnr_id" type="hidden" class="textbox" value="<?php echo $mid ?>">
                            <input name="apporval_date" id="apporval_date" type="hidden" class="textbox" value="<?php echo $cur_date ?>">
                            <?php
                              if($ognrapp->gnr_status == 'Approved') {
                              ?>
                              <span ><img src="<?php echo base_url(); ?>app-assets/images/right.png" alt="" class="img_main"/> Approved</span>
                              <?php
                                } else {
                              ?>  
                              <span class="badge badge-danger"><i class="uk-icon-spinner uk-icon-medium uk-icon-spin" ></i>Approve Pending...</span>
                              <?php
                              }
                            ?>
                          </div>
                        </div>
                        <?php 
                        }
                      ?>
                      <div class="col-md-4 text-right">
                        <button type="button" id="print_receipt" class="btn btn-info print_receipt  btn-sm my-1"><i class="fa ft-printer"></i></button>
                      </div>
                    </div>      
                
    <!-- Invoice Footer -->
    <div class="row hidden-print print_receipt">
      <?php if($grn->invoice_upload == ""){ ?>
     <form class="uk-form-stacked" id="upload_form" action="<?php echo site_url('Purchase/grn_in_upload');?>"  method="post" enctype="multipart/form-data">
                  <div class="md-card">
                    <div class="md-card-content">
                    <h4><b>Invoice Upload</b></h4>
                      <div class="row">
                        <div class="col-md-6">
                          <div class="form-group">
                            <!--<label>INVOICE UPLOAD</label>-->
                            <input name="gid" id="gid" type="hidden" class="textbox" value="<?php echo $mid ?>">
                            <input type="file" name="grn_in_upload" id="grn_in_upload" class="form-control image_file" accept="image/*" multiple />
                            <div id="errfn2" ></div>
                          </div>
                        </div>
                        <div class="col-md-3">
                          <div class="form-group">
                            <input type="submit" name="upload" id="upload"  class="btn btn-primary btn-sm" value="Upload">
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </form>
      </br>
      <?php } ?>
      </div>
      <div class="row hidden-print">
        <div class="col-md-12">
          <?php if($grn->invoice_upload != ""){
            
          ?>
            <!--<span class="click-file" id="click-file" target="1"> View File</span>-->
            <?php
            
             
              $i = 0;
              foreach($grn_upload as $gupload){
                $imageURL = $gupload->file_name;
            ?>
            <?php if($gupload->gstatus == 'Grn Status'){ ?>
                <a href="#demo1<?php echo $i; ?>" class='btn btn-primary category_list print_receipt' data-toggle='collapse'>View File</a>
                <!--<div class="show-document" id="div1">-->
                <div id="demo1<?php echo $i; ?>" class='collapse'>
                  <img src="<?php echo base_url(); ?><?php echo $imageURL; ?>" class="img-rounded" alt="Grn Document" style="width:90%!important;" >
                  
                </div>
            <?php } ?>
            <?php 
              $i++;
              } 
            ?>
            <?php } ?>
        </div>
      </div>
      <div id="image_preview"></div>
      <div class="doc_viewer">
        <!--<iframe id="viewer" frameborder="0" scrolling="yes" width="950" height="500"></iframe>-->
        <img id="viewer" />
      </div>
  </div>
</section>

<!-- // Basic form layout section end -->
        </div>
      </div>
    </div>
    <?php //require('include/theme_customizer.php');?>

   <?php 
  $footer_file=APPPATH.'views/include/footer.php';
  require($footer_file);
  ?>
  <script>
              jQuery(document).ready(function() {
                /*function close_accordion_section() {
                  jQuery('.uk-accordion .accordion-section-title').removeClass('active');
                  jQuery('.uk-accordion .accordion-section-content').slideUp(300).removeClass('open');
                }*/
                jQuery('.uk-accordion-title').click(function(e) {
                  // Grab current anchor value
                  $('#accordion').css({display:'block'});
                  var currentAttrValue = jQuery(this).attr('href');
                  if(jQuery(e.target).is('.active')) {
                    close_accordion_section();
                    }else {
                    close_accordion_section();
                    // Add active class to section title
                    jQuery(this).addClass('active');
                    // Open up the hidden content panel
                    jQuery('.uk-accordion ' + currentAttrValue).slideDown(300).addClass('open'); 
                  }
                  e.preventDefault();
                });
              });
            </script>  
            <script type='text/javascript'>//<![CDATA[
               $(document).ready(function(){
                $(".tiptext").mouseover(function() {
                  $(this).children(".description").show();
                  }).mouseout(function() {
                  $(this).children(".description").hide();
                });
              });//]]> 
            </script>
            <script type="text/javascript">
              $(document).ready(function(){
                $('td.editable-col').on('focusout', function() {
                  data = {};
                  var remarks = $(this).text();
                  //data['id'] = $(this).parent('tr').attr('data-row-id');
                  var pg_id = $(this).parent('tr').attr('data-row-id');
                  var item_id = $(this).parent('tr').attr('data-item-id');
                  var uname = $('#user').val();
                  var gnr_id = $('#gnrid').val();
                  var login_id = $('#login_id').val();
                 
                  $.ajax({   
                    type: "POST",  
                    url: "<?php echo base_url() ?>index.php/Purchase/remark_insertion",  
                    cache:false,  
                    data: "uname="+uname+"&gnr_id="+gnr_id+"&pg_id="+pg_id+"&login_id="+login_id+"&remarks="+remarks+"&item_id="+item_id,
                    //dataType: "json",       
                    success:function(data){
                      $("#info").html(data);
                    }
                  });
                });
              });
            </script>
            <script>
              function myFunction() {
                location.reload();
              }
            </script>
            
            <script>
  $(document).ready(function(){
    var tot = $('#grn_amt').val();
    var sum = 0;
    //alert(tot);
    $('.rej_amt').each(function() {
      sum += Number($(this).val());
    });
    var sum1 = (parseFloat(tot) - parseFloat(sum)).toFixed(2)
    //alert(sum1);
    $('#rej_aftr_tot').html(sum1);
    $('#rej_tot').html(sum.toFixed(2));
    
    
    var tot2 = $('#grn_amt1').val();
    var sum2 = 0;
    //alert(tot);
    $('.rej_amt1').each(function() {
      sum2 += Number($(this).val());
    });
    var sum4 = (parseFloat(tot) - parseFloat(sum2)).toFixed(2)
    //alert(sum1);
    $('#rej_aftr_tot1').html(sum4);
    $('#rej_tot1').html(sum2);
  });
</script>
<script type="text/javascript">
      $(document).ready(function () {
        $('.image_file').change(function () {
          var val = $(this).val().toLowerCase();
          var regex = new RegExp("(.*?)\.(jpg|png|jpeg|JPG)$");
          if(!(regex.test(val))) {
            $(this).val('');
            //alert('Please select correct file format');
            document.getElementById('errfn2').innerHTML="Please upload proper image format";
            $('#image_preview').empty();
          }
          else
        {
            preview_image();
            document.getElementById('errfn2').innerHTML="";
        }
            
        }); });
    </script> 
    <script type="text/javascript">
       $(document).ready(function () {
         $('.image_file').change(function () {
      // function PreviewImage() {
          pdffile=document.getElementById("grn_in_upload").files[0];
          pdffile_url=URL.createObjectURL(pdffile);
        //  $('#viewer').attr('src',pdffile_url);
         // }
         });
       });
    </script>
    <script type="text/javascript">
      $(document).ready(function(){
        $('td.editable-col1').on('focusout', function() {
          data = {};
          data['val'] = $(this).text();
          data['id'] = $(this).parent('tr').attr('data-row-id1');
          data['index'] = $(this).attr('col-index');
          if($(this).attr('oldVal') === data['val'])
          return false;
          $.ajax({   
            type: "POST",  
            url: "<?php echo base_url() ?>index.php/Purchase/grn_price_update",  
            cache:false,  
            data: data,
            dataType: "json",       
            success: function(response)  
            {   
              //$("#loading").hide();
              if(!response.error) {
                $("#msg").removeClass('alert-danger');
                $("#msg").addClass('alert-success').html(response.msg);
                } else {
                $("#msg").removeClass('alert-success');
                $("#msg").addClass('alert-danger').html(response.msg);
              }
            }   
          });
          //alert("xyz");
          //$('#dt').load('grn_view_list.php?page_id=purchase-grn-list');
          location.reload();
        });
      });
    </script>
    <script>
      function preview_image() 
      {
        var total_file=document.getElementById("grn_in_upload").files.length;
        for(var i=0;i<total_file;i++)
        {
          $('#image_preview').append("<img src='"+URL.createObjectURL(event.target.files[i])+"'><br>");
        }
      }
    </script>
    <script>
  $("#print_receipt").click(function() {
    window.print();
    setTimeout("closePrintView()", 50);
  });
  function closePrintView() {
    window.location = '<?php echo site_url('Purchase/cw3grn_list');?>';
  } 
</script>