<?php 
define('DOCUMENT_ROOT_CLASS','../../');
define('DOCUMENT_ROOT_INC_FOLDER','inc/');
$filepath = realpath(dirname(__FILE__));

if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call_2"){
  require "../../inc/ajax_header.php";
  $holder_name = $_POST['cheque_name'];
  $cheque_no = $_POST['cheque_no'];
  $cheque_date = $_POST['cheque_date'];
  $bank = $_POST['cheque_bank'];
  $commonFunctions->updateData('sys_erp_sales_invoice'," WHERE id = '".$_POST['invoice_id']."'"," holder_name='".$holder_name."', cheque_no='".$cheque_no."', cheque_date='".$cheque_date."' ,bank = '".$bank."' ",'');

  Session::set('msg', '<div class="alert alert-success alert-dismissible mt-3" id="flash-msg">
        <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
        <strong>Success !</strong> Wow, Your Information updated Successfully !</div>');
  echo "invoice-view.php?invoice_id=".$_POST['invoice_id'];
  exit();
}
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call_1"){
    require "../../inc/ajax_header.php";
    $added_by = $_SESSION['id'];
    $where = "";
    if(!empty($_POST['po_number'])){
      $where = " ,po_number='".$_POST['po_number']."' ";
    }
    $invoice_status = !empty($_POST['invoice_status'])?$_POST['invoice_status']:0;
    $last_id = $commonFunctions->updateData('sys_erp_sales_invoice'," WHERE id = '".$_POST['invoice_id']."' ","invoice_status = '".$invoice_status."' ".$where,"");

    Session::set('msg', '<div class="alert alert-success alert-dismissible mt-3" id="flash-msg">
          <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
          <strong>Success !</strong> Wow, Your Information updated Successfully !</div>');
    echo "invoice-view.php?invoice_id=".$_POST['invoice_id'];
    exit();
    /*}else{
        Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg">
<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
<strong>Error !</strong> Data not inserted !</div>');
        echo "";
        exit();
    }*/
}
require "../../inc/header.php";
$msg = Session::get('msg');
Session::set("msg", NULL);
$invoiceData = $commonFunctions->getRowData('sys_erp_sales_invoice'," WHERE id = '".@$_GET['invoice_id']."' ");
?>
	
  <!-- Page-Title -->
  <div class="row">
      <div class="col-sm-12">
          <div class="page-title-box">
              <div class="row">
                  <div class="col form-row">
                  	<div class="col-sm-6">
                        <h4 class="page-title">Invoice #<?php echo @$invoiceData['invoice_no'];?></h4>
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Sales & Purchases</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Sales & Invoices</a></li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Invoice #<?php echo @$invoiceData['invoice_no'];?></a></li>
                        </ol>
                    </div>
                    <!-- <div class="col-sm-2" style="text-align: end;">
                      <a href="invoice.php?state=list"><button type="button" class="btn btn-sm btn-secondary clear_form">Back to Invoice List</button></a>
                    </div> -->
                    <div class="col-sm-6" style="text-align: end;">
                      <div class="col-auto align-self-center">
                          <a href="javascript:void(0);" class="btn btn-secondary btn-sm" onclick='printDiv();'>
                              <i class="fas fa-print align-self-center icon-xs"></i> Print
                          </a>
                          <a href="javascript:void(0);" class="btn btn-info btn-sm">
                              <i class="fa fa-envelope-open align-self-center icon-xs"></i> Email
                          </a>
                          <button type='button' class="btn btn-success btn-sm mark_paid" <?php echo ($invoiceData['invoice_status'] == 1)?"disabled='' placeholder='Sorry you can`t change status because it is paid' title='Sorry you can`t change status because it is already paid!' ":"";?> >
                            <i class="fas fa-money-bill-alt align-self-center icon-xs"></i> Mark Paid 
                          </button>
                          <?php if($invoiceData['invoice_status'] != 1 and @$invoiceData['payment_mode'] == 2 ){ ?>
                            <a href="javascript:void(0);" class="btn btn-warning btn-sm" id="cheque_model"  data-toggle="modal" data-target="#paymode_modal">
                              <i class="fa fa-money-bill-alt align-self-center icon-xs"></i> Cheque Details
                            </a>
                          <?php } ?>
                          <!-- <a href="#" class="btn btn-danger btn-sm">
                              <i class="fas fa-times-circle align-self-center icon-xs"></i> Cancel
                          </a> -->

                          <a href="invoice.php?state=list"><button type="button" class="btn btn-dark btn-sm clear_form">Back to Invoice List</button></a>
                    
                      </div>
                    </div>
                  </div><!--end col-->
              </div><!--end row-->                                                              
          </div><!--end page-title-box-->
      </div><!--end col-->
  </div><!--end row-->
  <!-- end page title end breadcrumb -->

  <div class="row">
      <div class="col-md-12">
        	<div class="row card ">
        		<div class="card-header" style="display: none;">
                  <h4 class="card-title" style="line-height: 5px;"> 
                      <form action="" method="post" id="invoice_form" class="form-row">
                      	<div class="col-sm-3">
                          <input type="hidden" name="invoice_id" value="<?php echo @$_GET['invoice_id'];?>">
                      		
                          <div class="form-check form-check-inline" style="margin-top: 25px;">
                            <input name="invoice_status" class="form-check-input" type="checkbox" id="inlineCheckbox3" value="1" checked >
                            <label class="form-check-label" for="inlineCheckbox3"  >Mark as Paid</label>
                            <label id="invoice_status-error" class="error" for="invoice_status" style=""></label>
                          </div>

                        </div>
                        <?php if($invoiceData['invoice_status'] != 1 and $invoiceData['po_number'] == "" ){ ?>
                          <div class="col-sm-3">
                            <label>PO Number</label>
                            <input type="text" name="po_number" class="form-control" value="" placeholder="PO Number">
                          </div>
                        <?php } ?>
                        <div class="col-sm-6">
                        	<br><br>
                        	<button type="button" class="btn btn-soft-primary" id="save_form" <?php echo ($invoiceData['invoice_status'] == 1)?"disabled='' placeholder='Sorry you can`t change status because it is paid' title='Sorry you can`t change status because it is paid' ":"";?> >Save Invoice Changes</button>
                        </div>
                      </form>
                  </h4>
            </div>
            <?php echo !empty($msg)?$msg:''; ?>
            <div class="card-body1 col-lg-12 mx-auto" id="DivIdToPrint">
              <div class="card1">
                 <div class="card-body invoice-head">
                    <div class="row">
                      <div class="col-md-5 align-self-center">    
                        <?php $orgData = $commonFunctions->getRowData("sys_erp_organaization",""); ?>
                        <?php $warehouseData = $commonFunctions->getRowData("invty_wrhus"," WHERE id='".$invoiceData["branch"]."' "); ?>
                          <img src="<?php echo DOCUMENT_ROOT_CLASS;?>uploads/admin-setting/company/<?php echo @$orgData['erp_sys_org_logo'];?>" alt="logo-large" class="logo-lg logo-dark" height="80">  
                          <img src="<?php echo DOCUMENT_ROOT_CLASS;?>uploads/admin-setting/company/<?php echo @$orgData['erp_sys_org_logo'];?>" alt="logo-large" class="logo-lg logo-light" height="80"> 
                          <!--p class="mt-2 mb-0 text-muted">If account is not paid within 7 days the credits details supplied as confirmation.</p-->                                             
                      </div><!--end col-->    
                      <div class="col-md-7">                                                
                          <ul class="list-inline mb-0float-right">                                                   
                              <li class="list-inline-item">
                                  <div class="pl-3">
                                      <p class="text-muted mb-0"><?php echo @$warehouseData['address'];?></p>
                                      <p class="text-muted mb-0"><?php echo @$warehouseData['city'];?>, P.O.Box <?php echo @$warehouseData['zip_code'];?></p>
                                      <p class="text-muted mb-0">State of <?php echo $commonFunctions->getname(@$warehouseData['province'],"sys_erp_adm_countries_states","label","rowid");?></p>
                                  </div>                                                 
                              </li>
                              <li class="list-inline-item">
                                  <div class="pl-3">
                                      <p class="text-muted mb-0">C.R # <?php echo @$orgData['erp_sys_org_cr_num'];?></p>
                                      <p class="text-muted mb-0"><?php echo !empty(@$orgData['erp_sys_email_work'])?@$orgData['erp_sys_email_work']:"&nbsp;";?></p>
                                      <p class="text-muted mb-0"><?php echo @$orgData['erp_sys_org_phone_work'];?></p>
                                  </div>
                              </li>
                              <li class="list-inline-item">
                                  <div class="pl-1">
                                      <img alt='barcode' src='<?php echo DOCUMENT_ROOT_CLASS;?>lib/barcode.php?codetype=Code128&size=40&text=<?php echo @$invoiceData['invoice_no'];?>&print=true'/>
                                  </div>
                              </li>
                          </ul>
                      </div><!--end col-->     
                  </div><!--end row--> 
                    <!--end row-->     
                 </div>
                 <!--end card-body-->
                 <div class="card-body">
                    <div class="row">
                       <div class="col-md-3">
                          <div class="float-left">
                             <address class="font-13">
                                <strong class="font-14">Billed To :</strong><br>
                                <?php $bcust = $commonFunctions->getRowData('crm_cust_details'," WHERE id = '".$invoiceData['bill_to']."' ");
                                  echo $bcust['name'].'<br>'.$bcust['address'].','.$bcust['city'].' '.$bcust['postcode'].'<br>P: '.$bcust['phone']; ?>
                             </address>
                          </div>
                       </div>
                       <!--end col--> 
                       <div class="col-md-3">
                          <div class="">
                             <address class="font-13">
                                <strong class="font-14">Shipped To :</strong><br>
                                <?php $scust = $commonFunctions->getRowData('crm_cust_details'," WHERE id = '".$invoiceData['ship_to']."' ");
                                  echo $scust['name'].'<br>'.$scust['address'].','.$scust['city'].' '.$scust['postcode'].'<br>P: '.$scust['phone']; ?>
                             </address>
                          </div>
                       </div>
                       <!--end col-->
                       <div class="col-md-2">
                       </div>
                       <!--end col-->  
                       <div class="col-md-4">
                          <table id="" class="table table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" style="border-collapse: collapse; border-spacing: 0px; width: 100%;" role="grid" aria-describedby="datatable_info">
                             <thead class="thead-light">
                                <tr>
                                   <th>Invoice Number :</th>
                                   <th><?php echo @$invoiceData['invoice_no'];?></th>
                                </tr>
                                <!--end tr-->
                                <tr>
                                   <th>Invoice Date :</th>
                                   <th> <?php echo date("d M Y",strtotime(@$invoiceData['invoice_date']));?>
                                   </th>
                                </tr>
                                <!--end tr-->
                                <tr>
                                   <th>Due Date :</th>
                                   <th> <?php echo date("d M Y",strtotime(@$invoiceData['invoice_due_date']));?>
                                   </th>
                                </tr>
                                <!--end tr-->
                                <tr>
                                   <th>Delivery Note :</th>
                                   <th><span class="delivery_note"><?php echo @$invoiceData['delivery_note'];?></span></th>
                                </tr>
                                <!--end tr-->
                             </thead>
                          </table>
                       </div>
                       <!--end col-->                       
                    </div>
                    <!--end row-->
                    <div class="row">
                       <div class="col-lg-12">
                          <div class="">
                             <table class="table table-bordered mb-0">
                                <thead class="thead-light">
                                   <tr>
                                      <th>PO Number</th>
                                      <th>Payment Mode</th>
                                      <th>Payment Type</th>
                                      <th>Project ID</th>
                                   </tr>
                                   <!--end tr-->
                                </thead>
                                <tbody>
                                   <tr>
                                      <td>CUS PO#<span class="cust_po"><?php echo @$invoiceData['po_number'];?></span></td>
                                      <td><?php echo $commonFunctions->getname($invoiceData['payment_mode'],'fin_accnt_payment_mode','label','id'); ?></td>
                                      <td><?php echo $commonFunctions->getname($invoiceData['payment_terms'],'fin_accnt_payment_trems','label','id'); ?></td>
                                      <td>CUS PID#
                                        <?php echo @$invoiceData['project_number'];?></td>
                                   </tr>
                                   <!--end tr-->
                                </tbody>
                             </table>
                             <!--end table-->
                          </div>
                          <!--end /div-->                                          
                       </div>
                       <!--end col-->                                      
                    </div>
                    <!--end row-->
                    <p></p>
                    <div class="row">
                       <div class="col-lg-12">
                          <div class="">
                             <table class="table table-bordered mb-0">
                                <thead class="thead-light">
                                   <tr>
                                      <th>Product Ref / ID</th>
                                      <th>Product Description</th>
                                      <th>Unit Price</th>
                                      <th>Quantity</th>
                                      <th>Total</th>
                                   </tr>
                                   <!--end tr-->
                                </thead>
                                <tbody>
                                	<?php $invoiceProductData = $commonFunctions->getData('sys_erp_sales_invoice_prdct_details'," WHERE invoice_id = '".@$_GET['invoice_id']."' "); 
                                  foreach ($invoiceProductData as $rw) {
                                  ?>
                                     <tr class="after-add-more">
                                        <td>
                                          <label class="form-control"> <?php echo $commonFunctions->getname($rw['product_id'],'invty_pdt_details','label','id'); ?></label>
                                        </td>
                                        <td>
                                          <label class="form-control"> <?php echo $rw['product_desc']; ?></label>
                                        </td>
                                        <td>
                                        	<label class="form-control"> <?php echo $rw['unit_price']; ?></label>
                                        </td>
                                        <td>
                                          <label class="form-control"> <?php echo $rw['quantity']; ?></label>
                                        </td>
                                        <td>
                                        	<label class="form-control"> <?php echo $rw['total_amt']; ?></label>
                                          <!-- <input type="text" name="total_pro_amt[]" class="total_pro_amt form-control total_pro_amt1" readonly=""> -->
                                        </td>
                                        
                                     </tr>
                                   <?php } ?>
                                   <!--end tr-->
                                   <tr>
                                      <td colspan="3" class="border-0"></td>
                                      <td class="border-0 font-14 text-dark"><b>Sub Total</b></td>
                                      <td class="border-0 font-14 text-dark"><b>
                                        <input type="hidden" name="sub_total_amt" class="sub_total_amt" value="0">
                                        <span class="sub_total_amt"><?php echo @$invoiceData['sub_total'];?></span></b></td>
                                   </tr>
                                   <!--end tr-->
                                   <tr>
                                      <th colspan="3" class="border-0"></th>
                                      <td class="border-0 font-14 text-dark"><b>Tax Rate</b></td>
                                      <td class="border-0 font-14 text-dark"><b>$0.00%</b></td>
                                   </tr>
                                   <!--end tr-->
                                   <tr>
                                      <th colspan="3" class="border-0"></th>
                                      <td class="border-0 font-14 text-dark"><b>Discount</b></td>
                                      <td class="border-0 font-14 text-dark"><b><label class="form-control"><?php echo @$invoiceData['discount'];?></label></b></td>
                                   </tr>
                                   <!--end tr-->
                                   <tr class="bg-black text-white">
                                      <th colspan="3" class="border-0"></th>
                                      <td class="border-0 font-14"><b>Total</b></td>
                                      <td class="border-0 font-14"><b><label class="form-control"><?php echo @$invoiceData['total'];?></label></b></td>
                                   </tr>
                                   <!--end tr-->
                                </tbody>
                             </table>
                             <!--end table-->
                          </div>
                          <!--end /div-->                                          
                       </div>
                       <!--end col-->                                      
                    </div>
                    <!--end row-->
                    <div class="row justify-content-center">
                       <div class="col-lg-6">
                          <h5 class="mt-4">Terms And Condition :</h5>
                          <ul class="pl-3">
                             <li><small class="font-12">All accounts are to be paid within 7 days from receipt of invoice. </small></li>
                             <li><small class="font-12">To be paid by cheque or credit card or direct payment online.</small></li>
                             <li><small class="font-12"> If account is not paid within 7 days the credits details supplied as confirmation of work undertaken will be charged the agreed quoted fee noted above.</small></li>
                          </ul>
                       </div>
                       <!--end col-->                                       
                       <div class="col-lg-6 align-self-end">
                          <div class="float-right" style="width: 30%;">
                             <small>Account Manager</small>
                             <img src="assets/images/signature.png" alt="" class="mt-2 mb-1" height="26">
                             <p class="border-top">Signature</p>
                          </div>
                       </div>
                       <!--end col-->
                    </div>
                    <!--end row-->
                    <hr>
                    <div class="row d-flex justify-content-center">
                       <div class="col-lg-12 col-xl-7 ml-auto align-self-center">
                          <div class="text-center"><small class="font-12">Thank you very much for doing business with us.</small></div>
                       </div>
                       <!--end col-->
                       <div class="col-lg-12 col-xl-5 action_button" style="text-align: end;">
                          <div class="col-auto align-self-center">
                            <a href="javascript:void(0);" class="btn btn-secondary btn-sm" onclick='printDiv();'>
                                <i class="fas fa-print align-self-center icon-xs"></i> Print
                            </a>
                            <a href="javascript:void(0);" class="btn btn-info btn-sm">
                                <i class="fa fa-envelope-open align-self-center icon-xs"></i> Email
                            </a>
                            <button type='button' class="btn btn-success btn-sm mark_paid" <?php echo ($invoiceData['invoice_status'] == 1)?"disabled='' placeholder='Sorry you can`t change status because it is paid' title='Sorry you can`t change status because it is already paid!' ":"";?> >
                              <i class="fas fa-money-bill-alt align-self-center icon-xs"></i> Mark Paid 
                            </button>
                            <!-- <a href="#" class="btn btn-danger btn-sm">
                                <i class="fas fa-times-circle align-self-center icon-xs"></i> Cancel
                            </a> -->

                            <a href="invoice.php?state=list"><button type="button" class="btn btn-dark btn-sm clear_form">Back to Invoice List</button></a>
                      
                        </div>
                       </div>
                       <!--end col-->
                    </div>
                    <!--end row-->
                 </div>
                 <!--end card-body-->
              </div>
              <!--end card-->
            </div>
             <!--end col-->
          </div>  
      </div><!--end col-->
  </div><!--end row-->
  <div class="modal fade bd-example-modal-x3" id="paymode_modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-x3" role="document">
        <div class="modal-content">
          <form action="" method="post" id="paymentForm">  
            <div class="modal-header">
              <h4 class="modal-title m-0" id="myLargeModalLabel">Add Payment Mode</h4>
              <button type="button" class="close " data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true"><i class="la la-times"></i></span>
              </button>
            </div><!--end modal-header-->
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                      <div class="form-row">
                        <div class="col-lg-12 position-relative form-group">
                          <input type="hidden" name="invoice_id" value="<?php echo $invoiceData['id'];?>">
                          <label>Payment Mode <span class="error">*</span></label>
                            <?php $payModesData = $commonFunctions->getData('fin_accnt_payment_mode',' WHERE status = 0 and id = 2'); ?>
                                <select class="form-control" name="payment_modes" id="payment_modes" required="">
                                  <?php foreach ($payModesData as $rw) { ?>
                                      <option value="<?php echo $rw['id'];?>"><?php echo $rw['label'];?></option>
                                  <?php } ?>
                                </select>
                          </div>
                          <div class="form-row cheque_div" style="margin-bottom: 5px;">
                              <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                              </div>
                              <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                  
                              </div>
                              <div class="col-md-12 position-relative form-group" style="margin-bottom: 5px;">
                                  <label>Cheque Name <span class="error">*</span></label>
                                  <input type="text" name="cheque_name" id="cheque_name" value="<?php echo $invoiceData['holder_name'];?>" class="form-control" placeholder='Cheque Name' required="">
                              </div>
                              <div class="col-md-12 position-relative form-group" style="margin-bottom: 5px;">
                                  <label>Cheque Number <span class="error">*</span></label>
                                  <input type="text" name="cheque_no" id="cheque_no" value="<?php echo $invoiceData['cheque_no'];?>" class="form-control" placeholder='Cheque Number' required="">
                              </div>
                              <div class="col-md-12 position-relative form-group" style="margin-bottom: 5px;">
                                  <label>Cheque Date <span class="error">*</span></label>
                                  <input type="text" name="cheque_date" id="cheque_date" value="<?php echo $invoiceData['cheque_date'];?>" class="form-control" placeholder='Cheque Date' required="">
                              </div>
                              <div class="col-md-12 position-relative form-group" style="margin-bottom: 5px;">
                                  <label>Cheque Bank <span class="error">*</span></label>
                                  <input type="text" name="cheque_bank" id="cheque_bank" value="<?php echo $invoiceData['bank'];?>" class="form-control" placeholder='Cheque Bank' required="">
                              </div>

                          </div>
                        
                          <div class="col-lg-12"><br>
                            <button type="button" class="btn btn-secondary paymentButt" >Add</button>
                          </div>
                      </div>
                    </div><!--end card-body--> 
                </div><!--end card--> 
            </div> <!--end col-->
          </form>  
        </div><!--end modal-content-->
    </div><!--end modal-dialog-->
</div><!--end modal-->
</div> <!--end col-->
<?php require "../../inc/footer.php"; ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script type="text/javascript">
$("#save_form").click(function(){
  $("#invoice_form").validate();  
    if($("#invoice_form").valid()){
      $.ajax({
        type: 'POST',
        url: "",
        data: $('#invoice_form').serialize()+"&action=ajax_call_1",
        success: function(response) { 
          window.location.href = response;
        }
    });
  }
});

$(".paymentButt").click(function(){

  if($("#paymentForm").valid()){
    $.ajax({
      type: 'POST',
      url: "",
      data: $("#paymentForm").serialize()+"&action=ajax_call_2",
      success: function(response) { 
        window.location.href = response;
      }
    });
  }

});
function printDiv(){
  var printContents = document.getElementById('DivIdToPrint').innerHTML;
  var originalContents = document.body.innerHTML;
  document.body.innerHTML = printContents;
  window.print();
  document.body.innerHTML = originalContents;
}
function printDiv1() 
{
  $(".action_button").css("display","none");
  var divToPrint=document.getElementById('DivIdToPrint');
  var newWin=window.open('','Print-Window');
  newWin.document.open();
  newWin.document.write('<html><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/css/common.css" rel="stylesheet" type="text/css" /><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/plugins/leaflet/leaflet.css" rel="stylesheet"><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/plugins/lightpick/lightpick.css" rel="stylesheet" /><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" /><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/css/icons.min.css" rel="stylesheet" type="text/css" /><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/css/app.min.css" rel="stylesheet" type="text/css" /><link href="<?php echo DOCUMENT_ROOT_CLASS;?>assets/plugins/treeview/themes/default/style.css" rel="stylesheet"><link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
  newWin.document.close();
  setTimeout(function(){newWin.close();$(".action_button").css("display","block");},10);
}
$(".mark_paid").click(function(){
  $("#save_form").click();
});
</script>