<?php
define('DOCUMENT_ROOT_CLASS','../../');
define('DOCUMENT_ROOT_INC_FOLDER','inc/');
$filepath = realpath(dirname(__FILE__));
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call"){
    require "../../inc/ajax_header.php";
    $bill_to = $_POST["bill_to"];
    $getData = $commonFunctions->getData('crm_cust_details'," WHERE id = '".$bill_to."'");
    $html = $email = $contact = "";
    foreach ($getData as $rw) {
        $html = "<option value='".$rw['id']."'>".$rw['address']."</option>";
        $email .= $rw['email']."<br>";
        $contact .= $rw['phone']."<br>";
    }
    echo json_encode(array("option"=>$html,"email"=>$email, "contact"=>$contact));
    exit();
}
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call_1"){
    require "../../inc/ajax_header.php";
    $warehouse_id = $_POST["warehouse_id"];
    $product_id = $_POST["product_id"];
    $already_selected = array();
    if(!empty($_POST["already_selected"])){
        $already_selected = @$_POST["already_selected"];
    }
    $cond="";
    if(!empty($product_id)){
        $cond=" AND product_id = '".$product_id."' ";
    }
    $getData = $commonFunctions->getData('invty_pdt_stock_mgt'," WHERE warehouse_id = '".$warehouse_id."' ".$cond);
    $html = "";
    $stock_count = $price = 0;
    foreach ($getData as $rw) {
        $product_name = $commonFunctions->getname($rw['product_id'],"invty_pdt_details","label","id");
        $disabled = "";
        if(in_array($rw['product_id'], $already_selected)){
            $disabled = " style='display:none;'";
        }
        $html = "<option value='".$rw['product_id']."' ".$disabled.">".$product_name."</option>";
        if(!empty($product_id)){
            $stock_count = $rw['stock_count'];
            $price = $commonFunctions->getname($rw['product_id'],"invty_pdt_details","price","id");
        }
    }
    echo json_encode(array("option"=>$html,"stock_count"=>$stock_count,"price"=>$price));
    exit();
}
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call_2"){
    require "../../inc/ajax_header.php";
    $total = @$_POST["sub_total_amt"] - $_POST["discount"];
    $added_by = $_SESSION['id'];
    $delivery_note_status = 1;
    if($_POST['delivery_note_flag'] == 3){
        $delivery_note_status = 3;
    }

    $last_id = $commonFunctions->addData('sys_erp_sales_invoice'," (invoice_no,invoice_date,invoice_due_date,branch,bill_to,ship_to,payment_mode,payment_terms,po_number,project_number,discount,tax_rate,sub_total,total,invoice_creator,delivery_note_flag,delivery_note_status) VALUES ('".$_POST['invoice_number']."','".$_POST['invoice_date']."','".$_POST['invoice_due_date']."','".$_POST['warehouse_id']."','".$_POST['bill_to']."','".$_POST['ship_to']."','".$_POST['payment_modes']."','".$_POST['payment_terms']."','".$_POST['po_number']."','".$_POST['project']."','".$_POST['discount']."','0','".$_POST['sub_total_amt']."','".$total."','".$added_by."','".$_POST['delivery_note_flag']."','".$delivery_note_status."') ",'');
    if(!empty($last_id)){
        $data_array_details = array();
        for ($i=0; $i < count($_POST["product_id"]); $i++) { 
            $data_array_details["invoice_id"] = $last_id;
            $data_array_details["invoice_no"] = $_POST["invoice_number"];
            $data_array_details["product_id"] = $_POST["product_id"][$i];
            $data_array_details["unit_price"] = $_POST["unit_price"][$i];
            $data_array_details["quantity"] = $_POST["quantity"][$i];
            $data_array_details["total_amt"] = $_POST["total_pro_amt"][$i];
            $commonFunctions->addData('sys_erp_sales_invoice_prdct_details'," (invoice_no,invoice_id,product_id,unit_price,quantity,total_amt) VALUES ('".$_POST['invoice_number']."','".$data_array_details['invoice_id']."','".$_POST['product_id'][$i]."','".$_POST['unit_price'][$i]."','".$_POST['quantity'][$i]."','".$_POST['total_pro_amt'][$i]."') ",'');
        }
        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 added Successfully !</div>');
        echo "invoice.php?state=list";
        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);
?>
    
<!-- Page-Title -->
<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <div class="row">
                <div class="col">
                    <?php 
                    $rand = "INV-";
                    $rand .= rand(0,99999);
                    $rand .= @$commonFunctions->getRowDataCount('sys_erp_sales_invoice','');
                    ?>
                    <h4 class="page-title"><?php echo ($_GET['state'] != "list")?"Invoice # $rand":" Sales / Invoices List "; ?></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);"><?php echo ($_GET['state'] != "list")?$rand:"Sales / Invoices"; ?></a></li>
                    </ol>
                </div><!--end col-->
                <div class="col-auto align-self-center">
                    
                    <a href="invoice.php?state=add" role="button" ><button type="button" class="btn btn-sm btn-primary"><i class="fas fa-plus mr-2"></i> Create Invoice</button> </a>
                    <?php if(@$_GET['state'] == "list"){ ?>
                    
                    <?php }else{ ?>
                        <a href="invoice.php?state=list"><button type="button" class="btn btn-sm btn-secondary">Back to Invoice List </button></a>
                    <?php } ?>
                </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-12">
        <div class="card">
            <div class="card-header">
                <h4 class="card-title"> 
                    <div class="form-row">
                        <!-- <div class="form-group searchForm col-md-6">    
                            <a class="btn btn-soft-primary" href="invoice.php?state=add" role="button" ><i class="fas fa-plus mr-2"></i> Create Invoice </a>
                        </div>  --> 
                        <div class="form-group searchForm col-md-6">
                            <?php if(empty($_GET['state']) OR (!empty($_GET['state']) and $_GET['state'] == "list")): ?>
                            <form action="" id="searchForm" method="GET">
                                <div class="form-row">
                                    <div class="form-group searchForm col-md-8">
                                        <input type="text" name="search" id="search" placeholder="Search" class="form-control" value="<?php echo @$_GET['search']; ?>">
                                        <input type="hidden" name="state" value="<?php echo @$_GET['state']; ?>">
                                        <input type="hidden" name="page" value="<?php echo @$_GET['page']; ?>">
                                    </div>
                                    <div class="form-group searchForm col-md-4">
                                        <button type="submit" id="searchbtn" class="btn btn-primary"> <i class="fa fa-search"></i> Search</button>
                                        <button type="button" id="clearbtn" class="btn btn-danger"> <i class="fa fa-trash"></i> Clear</button>
                                    </div>
                                </div>
                            </form>
                            <?php endif; ?>
                        </div>
                    </div>
                </h4>
                <?php echo !empty($msg)?$msg:''; ?>        
            </div>
   
            <div class="card-body">
                <div class="row">
                    <?php if(@$_GET['state'] != "list"): ?>
                        <?php 
                        $error_ = 0;
                            if(@$_GET['inventory_id'] != ''){
                                $editData = $commonFunctions->getRowData('invty_pdt_stock_mgt'," WHERE stock_id = '".$_GET['inventory_id']."' ");
                            }
                        ?>
                        <div class="col-md-12">
                            <!--div class="main-card mb-3 card">
                                <div class="card-body"-->
                                    <form id="invoice_form" action="" method="post" enctype="multipart/form-data">
                                        <input type="hidden" name="inventory_id" id="inventory_id" value="<?php echo ($_GET['state'] == 'edit')?@$_GET['inventory_id']:0;?>">
                                        <input type="hidden" name="invoice_number" id="invoice_number" value="<?php echo (!empty($rand))?@$rand:0;?>">

                                        <div class="step-1">
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">Select Branch <span class="error">*</span></label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <?php $warehouseData = $commonFunctions->getData('invty_wrhus',' WHERE status = 0 '); ?>
                                                    <select class="form-control select2" name="warehouse_id" id="warehouse_id">
                                                        <option value="" >Select</option>
                                                        <?php foreach ($warehouseData as $rw) { ?>
                                                            <option value="<?php echo $rw['id'];?>" <?php echo (@$editData['warehouse_id'] == $rw['id'])?"selected":"";?> ><?php echo $rw['short_name_location']; ?></option>     
                                                        <?php } ?>
                                                    </select>

                                                    <label id="warehouse_id-error" class="error" for="warehouse_id"></label>
                                                </div>
                                                <div class="col-md-2 position-relative form-group">
                                                </div>
                                            </div>
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">Bill To <span class="error">*</span></label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <?php $custData = $commonFunctions->getData('crm_cust_details',''); ?>
                                                    <select class="form-control select2" name="bill_to" id="bill_to">
                                                        <option value="" >Select</option>
                                                        <?php foreach ($custData as $rw) { ?>
                                                            <option value="<?php echo $rw['id'];?>"><?php echo $rw['name'];?>(<?php echo $rw['customer_code'];?>)</option>
                                                        <?php } ?>
                                                    </select>

                                                    <label id="bill_to-error" class="error" for="bill_to"></label>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                            </div>
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">Ship To <span class="error">*</span></label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <select class="form-control select2" name="ship_to" id="ship_to">
                                                        <option value="" >Select</option>
                                                    </select>

                                                    <label id="ship_to-error" class="error" for="ship_to"></label>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                            </div>
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">P.O Number</label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <input type="text" maxlength="64" name="po_number" id="cust_po" value="" class="form-control" placeholder="P.O Number">
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                            </div>
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">Payment Mode <span class="error">*</span></label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <?php $payModesData = $commonFunctions->getData('fin_accnt_payment_mode',' WHERE status = 0'); ?>
                                                    <select class="form-control select2" name="payment_modes" id="payment_modes" required="">
                                                        <option value="">Select</option>
                                                        <?php foreach ($payModesData as $rw) { ?>
                                                            <option value="<?php echo $rw['id'];?>"><?php echo $rw['label'];?></option>
                                                        <?php } ?>
                                                    </select>
                                                    <label id="payment_modes-error" class="error" for="payment_modes"></label>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                            </div>
                                            <div class="form-row cheque_div" style="margin-bottom: 5px;display: none;">
                                                <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-2 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="" class="form-control" placeholder='Cheque Name'>
                                                </div>
                                                <div class="col-md-2 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="0" class="form-control" placeholder='Cheque Number'>
                                                </div>
                                                <div class="col-md-2 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="" class="form-control" placeholder='Cheque Date'>
                                                </div>
                                                <div class="col-md-2 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="" class="form-control" placeholder='Cheque Bank'>
                                                </div>

                                            </div>
                                            <div class="form-row bank_transfer_div" style="margin-bottom: 5px;display: none;">
                                                <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-2 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label>Holder Name <span class="error">*</span></label>
                                                    <input type="text" name="holder_name" id="holder_name" value="" class="form-control" placeholder='Holder Name'>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label>Date <span class="error">*</span></label>
                                                    <input type="text" name="trans_date" id="trans_date" value="" class="form-control" placeholder='Date'>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label>Bank <span class="error">*</span></label>
                                                    <input type="text" name="bank" id="bank" value="" class="form-control" placeholder='Bank'>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label>Transfer Bank <span class="error">*</span></label>
                                                    <input type="text" name="transfer_bank" id="transfer_bank" value="" class="form-control" placeholder='Transfer Bank'>
                                                </div>
                                            </div>
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">Payment Terms <span class="error">*</span></label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <?php $payTermsData = $commonFunctions->getData('fin_accnt_payment_trems',''); ?>
                                                    <select class="form-control select2" name="payment_terms" id="payment_terms">
                                                        <option value="">Select</option>
                                                        <?php foreach ($payTermsData as $rw) { ?>
                                                            <option value="<?php echo $rw['id'];?>"><?php echo $rw['label'];?></option>
                                                        <?php } ?>
                                                    </select>
                                                    <label id="payment_terms-error" class="error" for="payment_terms"></label>
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                            </div>
                                            <div class="form-row" style="margin-bottom: 5px;">
                                                <div class="col-md-1 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                                <div class="col-md-3 position-relative form-group" style="margin-bottom: 5px;">
                                                    <label for="label" class="">Project</label>
                                                </div>
                                                <div class="col-md-6 position-relative form-group" style="margin-bottom: 5px;">
                                                    <input type="text" maxlength="64" name="project" value="" id="project" class="form-control" placeholder="Project">
                                                </div>
                                                <div class="col-md-2 position-relative form-group" style="margin-bottom: 5px;">
                                                </div>
                                            </div>
                                            <div class="form-row">
                                                <div class="col-md-1 position-relative form-group"></div>
                                                <div class="col-md-6 position-relative form-group">
                                                    <button type="button" class="mt-1 btn btn-outline-primary btn-step-1">Next >></button>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="step-2" style="display: none;">
                                            <div class="">
                                                
                                                <div class="row">
                                                   <div class="col-lg-12 mx-auto">
                                                      <div class="card">
                                                         <div class="card-body invoice-head">
                                                            <div class="row">
                                                               <div class="col-md-4 align-self-center">
                                                                  <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-8">
                                                                  <ul class="list-inline mb-0 contact-detail float-right">
                                                                     <li class="list-inline-item">
                                                                        <div class="pl-3">
                                                                           <i class="mdi mdi-web"></i>
                                                                           <p class="text-muted mb-0 email"></p>
                                                                           
                                                                        </div>
                                                                     </li>
                                                                     <li class="list-inline-item">
                                                                        <div class="pl-3">
                                                                           <p class="text-muted mb-0 contact"></p>
                                                                        </div>
                                                                     </li>
                                                                     <li class="list-inline-item">
                                                                        <div class="pl-3">
                                                                           <i class="mdi mdi-map-marker"></i>
                                                                           <p class="text-muted mb-0 custAddress"></p>
                                                                        </div>
                                                                     </li>
                                                                  </ul>
                                                               </div>
                                                               <!--end col-->    
                                                            </div>
                                                            <!--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>
                                                                        <span class="custAddress"></span>
                                                                     </address>
                                                                  </div>
                                                               </div>
                                                               <!--end col--> 
                                                               <div class="col-md-3">
                                                                  <div class="">
                                                                     <address class="font-13">
                                                                        <strong class="font-14">Shipped To:</strong><br>
                                                                        <span class="custAddress"></span>
                                                                     </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 @$rand;?></th>
                                                                        </tr>
                                                                        <!--end tr-->
                                                                        <tr>
                                                                           <th>Invoice Date :</th>
                                                                           <th> <?php echo date("d M Y");?>
                                                                               <input type="hidden" name="invoice_date" value="<?php echo date("Y-m-d");?>">
                                                                           </th>
                                                                        </tr>
                                                                        <!--end tr-->
                                                                        <tr>
                                                                           <th>Due Date :</th>
                                                                           <th> <?php echo date("d M Y");?>
                                                                               <input type="hidden" name="invoice_due_date" value="<?php echo date("Y-m-d");?>">
                                                                           </th>
                                                                        </tr>
                                                                        <!--end tr-->
                                                                        <tr>
                                                                           <th>Delivery Note :</th>
                                                                           <th><span class="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"></span></td>
                                                                              <td><span class="paymode"></span></td>
                                                                              <td><span class="paytype"></span></td>
                                                                              <td>CUS PID#
                                                                                <span class="project_id_label"></span></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</th>
                                                                              <th>Unit Price</th>
                                                                              <th>Quantity</th>
                                                                              <th>Total</th>
                                                                              <th>Add/Remove</th>
                                                                           </tr>
                                                                           <!--end tr-->
                                                                        </thead>
                                                                        <tbody>
                                                                           <tr class="after-add-more">
                                                                              <td>
                                                                                <select name="product_id[]" class="product_id form-control product_id1" required="">
                                                                                    <option value="0">Select</option>
                                                                                </select>
                                                                              </td>
                                                                              <td>
                                                                                <input type="text" name="unit_price[]" class="form-control unit_price1" readonly="">
                                                                              </td>
                                                                              <td>
                                                                                <input type="text" name="quantity[]" class="form-control quantity1" required="">
                                                                              </td>
                                                                              <td>
                                                                                <input type="text" name="total_pro_amt[]" class="total_pro_amt form-control total_pro_amt1" readonly="">
                                                                              </td>
                                                                              <td class="change">
                                                                                <button type="button" class="btn btn-soft-success btn-icon-circle btn-icon-circle-sm align-self-center add-more">
                                                                                 <i class="fas fa-plus font-16"></i>
                                                                                 </button>
                                                                              </td>
                                                                           </tr>
                                                                           
                                                                           <!--end tr-->
                                                                           <tr>
                                                                              <td colspan="2" 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">0</span></b></td>
                                                                           </tr>
                                                                           <!--end tr-->
                                                                           <tr>
                                                                              <th colspan="2" 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="2" class="border-0"></th>
                                                                              <td class="border-0 font-14 text-dark"><b>Discount</b></td>
                                                                              <td colspan="2" class="border-0 font-14 text-dark"><b><input type="text" name="discount" id="discount" style="width:50px!important"></b></td>
                                                                           </tr>
                                                                           <!--end tr-->
                                                                           <tr class="bg-black text-white">
                                                                              <th colspan="2" class="border-0"></th>
                                                                              <td class="border-0 font-14"><b>Total</b></td>
                                                                              <td colspan="2" class="border-0 font-14"><b><span class="total_amt">0</span></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-4 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-4">
                                                                  <div class="float-right d-print-none">
                                                                     <!-- <a href="#" class="btn btn-soft-primary btn-sm">
                                                                     <i class="fas fa-save align-self-center icon-xs"></i> Save
                                                                     </a> -->
                                                                    <button type="button" class="btn btn-soft-secondary back_to_first btn-sm">Back <<</button>
                                                                    <button type="button" class="btn btn-soft-success btn-sm save_form" id="save_form">
                                                                     <i class="fa fa-save align-self-center icon-xs"></i> Save Invoice
                                                                    </button>
                                                                  </div>
                                                               </div>
                                                               <!--end col-->
                                                            </div>
                                                            <!--end row-->
                                                         </div>
                                                         <!--end card-body-->
                                                      </div>
                                                      <!--end card-->
                                                   </div>
                                                   <!--end col-->
                                                </div>

                                                
                                            </div>
                                        </div>

                                    </form>
                                <!--/div>
                            </div-->
                        </div>
                    <?php else: ?>
                        <div class="col-md-12">
                            <!--div class="main-card mb-3 card">
                                <div class="card-body"-->
                                    <style type="text/css">
                                        tr {cursor: pointer;}
                                    </style>
                                    <?php 
                                    $targetpage = "invoice.php?state=list";
                                    $limit1 = 50;
                                    $limit = 50;
                                    $total_pages = $commonFunctions->getData('sys_erp_sales_invoice','');
                                    $total_pages = count($total_pages);
                                    $stages = 1;
                                    $page = @$_GET['page'];
                                    if($page){
                                        $start = ($page - 1) * $limit; 
                                    }else{
                                        $start = 0; 
                                    } 
                                    $search_con = "";
                                    if(!empty($_GET['search'])){
                                        $search_con = " AND invoice_no LIKE '%".$_GET['search']."%' OR invoice_date LIKE '%".$_GET['search']."%' OR invoice_due_date LIKE '%".$_GET['search']."%' OR project_number LIKE '%".$_GET['search']."%' OR total LIKE '%".$_GET['search']."%' ";

                                        $warehArr = $commonFunctions->getIDs('invty_wrhus','id'," WHERE short_name_location LIKE '%".$_GET['search']."%' ");
                                        $whr_ids = $warehArr->ids;
                                        if(!empty($whr_ids)){ $search_con .= "  OR branch IN(".$whr_ids.") "; }

                                        $warehArr = $commonFunctions->getIDs('crm_cust_details','id'," WHERE address LIKE '%".$_GET['search']."%' ");
                                        $whr_ids = $warehArr->ids;
                                        if(!empty($whr_ids)){ $search_con .= "  OR bill_to IN(".$whr_ids.") "; }
                                        if(!empty($whr_ids)){ $search_con .= "  OR ship_to IN(".$whr_ids.") "; }
                                    }
                                    $where = " WHERE id ".$search_con." ORDER BY id DESC LIMIT $start, $limit ";
                                    // Initial page num setup
                                    if ($page == 0){ $page = 1; }
                                    $prev = $page - 1;  
                                    $next = $page + 1;  
                                    $lastpage = ceil($total_pages/$limit);  
                                    $LastPagem1 = $lastpage - 1;    
                                    $paginate = '';
                                    if($lastpage > 1)
                                    {       
                                        $paginate .= "<div class='paginate'>";
                                        // Previous

                                        if ($page > 1){
                                            $paginate.= "<a href='$targetpage&page=$prev'>Previous</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Previous</span>";   }
                                        // Pages    
                                        if ($lastpage < 2 + ($stages * 2))  // Not enough pages to breaking it up
                                        {   
                                            for ($counter = 1; $counter <= $lastpage; $counter++)
                                            {
                                                if ($counter == $page){
                                                    $paginate.= "<span class='current'>$counter</span>";
                                                }else{
                                                    $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                            }
                                        }
                                        elseif($lastpage > 2 + ($stages * 2))   // Enough pages to hide a few?
                                        {
                                            // Beginning only hide later pages
                                            if($page < 1 + ($stages * 2))   
                                            {
                                                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // Middle hide some front and some back
                                            elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}    
                                                }
                                                $paginate.= "...";
                                                $paginate.= "<a href='$targetpage&page=$LastPagem1'>$LastPagem1</a>";
                                                $paginate.= "<a href='$targetpage&page=$lastpage'>$lastpage</a>";
                                            }
                                            // End only hide early pages
                                            else
                                            {
                                                $paginate.= "<a href='$targetpage&page=1'>1</a>";
                                                $paginate.= "<a href='$targetpage&page=2'>2</a>";
                                                $paginate.= "...";
                                                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
                                                {
                                                    if ($counter == $page){
                                                        $paginate.= "<span class='current'>$counter</span>";
                                                    }else{
                                                        $paginate.= "<a href='$targetpage&page=$counter'>$counter</a>";}
                                                }
                                            }       
                                        }       
                                                // Next
                                        if ($page < $counter - 1){ 
                                            $paginate.= "<a href='$targetpage&page=$next'>Next</a>";
                                        }else{
                                            $paginate.= "<span class='disabled'>Next</span>";
                                            }
                                        $paginate.= "</div>";   
                                    }
                                    $invtData = $commonFunctions->getData('sys_erp_sales_invoice',$where);

                                    $startP = $limit + $start;
                                    if($startP > @count($invtData) ){
                                        $startP = @count($invtData) + $start;
                                    }
                                    echo "<div style='float: right;'>".$paginate."</div>";?>
                                    <table style="margin-top: 10px;width: 100% !important;" class="table datatable_t table-hover table-striped table-bordered">
                                        <thead>
                                            <tr>
                                                <th class="text-center">Invoice No#</th>
                                                <th class="text-center">Customer</th>
                                                <th class="text-center">Invoice Date</th>
                                                <th class="text-center">Amount</th>
                                                <th class="text-center">Status</th>
                                                <th class="text-center">Created By</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php 
                                            foreach ($invtData as $row) { ?>
                                                <tr onclick="redirect_('invoice-card.php?invoice_id=<?php echo $row['id'];?>')">
                                                    <th class="text-center">
                                                        <?php echo $row['invoice_no']; ?>
                                                    </th>
                                                    <th class="text-center">
                                                        <?php echo $commonFunctions->getname($row['bill_to'],"crm_cust_details","name","id");
                                                        ?>
                                                    </th>
                                                    <th class="text-center">
                                                        <?php echo $row['invoice_date']; ?>
                                                    </th>
                                                    <th class="text-center">
                                                        <?php echo $row['total']; ?>
                                                    </th>
                                                    <th class="text-center">
                                                        <?php if($row['invoice_status'] == 0){ echo "<span class='btn btn-soft-danger btn-sm'>Unpaid</span>"; }
                                                        if($row['invoice_status'] == 1){ echo "<span class='btn btn-soft-success btn-sm'>Paid</span>"; }
                                                        if($row['invoice_status'] == 2){ echo "<span class='btn btn-soft-primary btn-sm'>Overdue</span>"; } ?>
                                                    </th>
                                                    <th class="text-center">
                                                        <?php echo $commonFunctions->getname($row['invoice_creator'],"sys_erp_adm_user","erp_sys_user_name_first","id")." ".$commonFunctions->getname($row['invoice_creator'],"sys_erp_adm_user","erp_sys_user_name_last","id");
                                                        ?>
                                                    </th>
                                                </tr>
                                            <?php } ?>
                                        </tbody>
                                    </table>
                                <!--/div>
                            </div-->
                        </div>

                    <?php endif; ?>
                </div>
            </div>

        </div>
    </div> <!-- end col -->
</div> <!-- end row -->


<div class="modal fade" id="exampleModalDefault" tabindex="-1" role="dialog" aria-labelledby="exampleModalDefaultLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form class="form-group save_popup_form" id="save_popup_form">
                <div class="modal-header">
                    <h6 class="modal-title m-0" id="exampleModalDefaultLabel">Do you want to create delivery note ?</h6>
                    <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="modal-body">
                    <div class="row">
                        <div class="col-lg-3"></div>
                        <div class="col-lg-9">
                            <div class="form-check form-check-inline" style="margin-top: 15px;">
                              <input name="delivery_note_flag" class="delivery_note_flag form-check-input" type="radio" id="inlineCheckbox1" value="1" required="">
                              <label class="form-check-label" for="inlineCheckbox1"><b>I want to make delivery note now.</b></label>
                            </div>             
                        </div><!--end col-->
                        <div class="col-lg-3"></div>
                        <div class="col-lg-9">
                            <div class="form-check form-check-inline" style="margin-top: 15px;">
                              <input name="delivery_note_flag" class="delivery_note_flag form-check-input" type="radio" id="inlineCheckbox2" value="2" required="">
                              <label class="form-check-label" for="inlineCheckbox2"><b>I want to make delivery note later.</b></label>
                            </div>             
                        </div><!--end col-->
                        <div class="col-lg-3"></div>
                        <div class="col-lg-9">
                            <div class="form-check form-check-inline" style="margin-top: 15px;">
                              <input name="delivery_note_flag" class="delivery_note_flag form-check-input" type="radio" id="inlineCheckbox3" value="3" required="">
                              <label class="form-check-label" for="inlineCheckbox3"><b>I don`t want to make delivery note.</b></label>
                            </div>             
                        </div><!--end col-->
                        <div class="col-lg-3"></div>
                        <div class="col-lg-9">
                            <br>
                            <label id="delivery_note_flag-error" class="error" for="delivery_note_flag" style=""></label>
                        </div> 
                    </div><!--end row-->                                                      
                </div><!--end modal-body-->
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Close</button>
                    <button type="button" id="save_popup_form_btn" class="btn btn-primary btn-sm">Save changes</button>
                </div><!--end modal-footer-->
            </form>
        </div><!--end modal-content-->
    </div><!--end modal-dialog-->
</div><!--end modal-->
<?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">
    $("#invoice_form").validate();
    $("#save_form").click(function(){
        if($("#invoice_form").valid()){
            $("#exampleModalDefault").modal("show");
        }
    });
    $("#save_popup_form_btn").click(function(){
        if($("#save_popup_form").valid()){
            var delivery_note_flag = $(".delivery_note_flag:checked").val();
            $.ajax({
                type: 'POST',
                url: "",
                data: $('#invoice_form').serialize()+"&action=ajax_call_2&delivery_note_flag="+delivery_note_flag,
                success: function(response) { 
                    window.location.href = response;
                }
            });
        }
    });
    $("#payment_modes").change(function(){
        $("#payment_modes-error").html("");
        var payment_modes = $("#payment_modes option:selected").text();
        $(".paymode").html(payment_modes);
        if(payment_modes == "Cheque"){
            $(".cheque_div").css("display","");
            $("#cheque_name").attr("required","");
            $("#cheque_no").attr("required","");
            $("#cheque_date").attr("required","");
            $("#cheque_bank").attr("required","");
        }else{
            $(".cheque_div").css("display","none");
            $("#cheque_name").removeAttr("required","");
            $("#cheque_no").removeAttr("required","");
            $("#cheque_date").removeAttr("required","");
            $("#cheque_bank").removeAttr("required","");
        }
        if(payment_modes == "Bank Transfer"){
            $(".bank_transfer_div").css("display","");
            $("#holder_name").attr("required","");
            $("#trans_date").attr("required","");
            $("#bank").attr("required","");
            $("#transfer_bank").attr("required","");
        }else{
            $(".bank_transfer_div").css("display","none");
            $("#holder_name").removeAttr("required","");
            $("#trans_date").removeAttr("required","");
            $("#bank").removeAttr("required","");
            $("#transfer_bank").removeAttr("required","");
        }
    });    
    $("#payment_terms").change(function(){
        var payment_terms = $("#payment_terms option:selected").text();
        $(".paytype").html(payment_terms);
        $("#payment_terms-error").html("");
    });
    $("#ship_to").change(function(){
        var ship_to = $("#ship_to option:selected").text();
        $(".custAddress").html(ship_to);
        $("#ship_to-error").html("");
    });    
    $("#cust_po").keyup(function(){
        var cust_po = $("#cust_po").val();
        $(".cust_po").val(cust_po);
        $(".cust_po").html(cust_po);
    });
    $("#project").keyup(function(){
        var project = $("#project").val();
        $(".project_id_label").html(project);
    });
    $("#bill_to").change(function(){
        $("#bill_to-error").html("");
    });
    $(".product_id1").change(function(){
        var warehouse_id = $("#warehouse_id option:selected").val();
        var product_id = $(".product_id1 option:selected").val();
        $.ajax({
            type: 'POST',
            url: "",
            data: {"warehouse_id":warehouse_id,"product_id":product_id,"action":"ajax_call_1"},
            success: function(response) { 
                obj = JSON.parse(response);
                $(".quantity1").attr("max",obj.stock_count);
                $(".unit_price1").val(obj.price);
            }
        });
    });
    $("#warehouse_id").change(function(){
        var warehouse_id = $("#warehouse_id option:selected").val();
        var product_id = $(".product_id1 option:selected").val();
        $.ajax({
            type: 'POST',
            url: "",
            data: {"warehouse_id":warehouse_id,"product_id":product_id,"action":"ajax_call_1"},
            success: function(response) { 
                obj = JSON.parse(response);
                $(".product_id1").html("<option value=''>Select</option>"+obj.option);
                $(".quantity1").attr("max",obj.stock_count);
                $(".unit_price1").val(obj.price);
            }
        });
    });
    $(".quantity1").keyup(function(){
        var quantity = parseInt($(".quantity1").val());
        if(quantity != ''){    
            var unit_price = parseInt($(".unit_price1").val());
            $(".total_pro_amt1").val(unit_price * quantity);

            var total_pro_amt = parseInt($(".total_pro_amt1").val());
            var sub_total_amt = parseInt($(".sub_total_amt").val());
            total_pro_amt = 0;
            $(".total_pro_amt").each(function() {
                total_pro_amt = total_pro_amt + parseInt(this.value);
                $(".sub_total_amt").val(total_pro_amt);
                $(".sub_total_amt").html(total_pro_amt);
                $("#discount").attr("max",total_pro_amt);
                var discount = parseInt($("#discount").val());
                if(discount == ""){ discount = 0; }
                $(".total_amt").html(total_pro_amt - discount);
            });
        }
    });
    $("#bill_to").change(function(){
        var bill_to = $("#bill_to option:selected").val();
        $.ajax({
            type: 'POST',
            url: "",
            data: {"bill_to":bill_to,"action":"ajax_call"},
            success: function(response) { 
                obj = JSON.parse(response);

                $("#ship_to").html("<option value=''>Select</option>"+obj.option);
                $(".email").html(obj.email);
                $(".contact").html(obj.contact);

            }
        });
    });
    $(".btn-step-1").click(function(){
        var warehouse_id = $("#warehouse_id option:selected").val();
        var bill_to = $("#bill_to option:selected").val();
        var ship_to = $("#ship_to option:selected").val();
        var payment_modes = $("#payment_modes option:selected").val();
        var payment_terms = $("#payment_terms option:selected").val();
        if(warehouse_id != '' && bill_to != '' && ship_to != '' && payment_modes != '' && payment_terms != '' ){
            $("#warehouse_id-error").html("");
            $("#bill_to-error").html("");
            $("#ship_to-error").html("");
            $("#payment_modes-error").html("");
            $("#payment_terms-error").html("");

            $(".step-1").css("display","none");
            $(".step-2").css("display","block");
        }else{
            if(warehouse_id == ''){ $("#warehouse_id-error").html("Please enter warehouse name."); }
            if(bill_to == ''){ $("#bill_to-error").html("Please enter bill to."); }
            if(ship_to == ''){ $("#ship_to-error").html("Please enter ship to."); }
            if(payment_modes == ''){ $("#payment_modes-error").html("Please enter payment modes."); }
            if(payment_terms == ''){ $("#payment_terms-error").html("Please enter payment terms."); }
        }
    });
    $(".back_to_first").click(function(){
        $(".step-1").css("display","block");
        $(".step-2").css("display","none");
    });
    $("#discount").keyup(function(){
        var discount = parseInt($("#discount").val());
        if(discount == ""){ discount = 0; }
        var sub_total_amt = parseInt($(".sub_total_amt").val());
        $(".total_amt").html(sub_total_amt - discount);
    });
$(document).ready(function() {
    var i = 2;
    $("body").on("click",".add-more",function(){ 
        //var html = $(".after-add-more").first().clone();
        var html = '<tr class="after-add-more">\
          <td>\
            <select name="product_id[]" class="product_id form-control product_id'+i+'">\
                <option value="0">Select</option>\
            </select>\
          </td>\
          <td>\
            <input type="text" name="unit_price[]" class="form-control unit_price'+i+'" readonly="">\
          </td>\
          <td>\
            <input type="text" name="quantity[]" class="form-control quantity'+i+'">\
          </td>\
          <td>\
            <input type="text" name="total_pro_amt[]" class="total_pro_amt form-control total_pro_amt'+i+'" readonly="">\
          </td>\
          <td class="change"><button type="button" class="btn btn-soft-danger btn-icon-circle btn-icon-circle-sm align-self-center remove"><i class="fas fa-times font-16"></i></button></td>\
       </tr>';
        $(html).find(".change").html("<button type='button' class='btn btn-soft-danger btn-icon-circle btn-icon-circle-sm align-self-center remove'><i class='fas fa-times font-16'></i></button>");      
        $(".after-add-more").last().after(html);
        var j = i;
        var warehouse_id = $("#warehouse_id option:selected").val();
        var product_id = $(".product_id"+j+" option:selected").val();
        var value = [];
        $(".product_id option:selected").each(function() {
            value.push(this.value);
            //$(".product_id"+j+" option:[value='"+value+"']").prop('disabled', true);
        });
        $.ajax({
            type: 'POST',
            url: "",
            data: {"warehouse_id":warehouse_id,"product_id":product_id,"action":"ajax_call_1","already_selected":value},
            success: function(response) { 
                obj = JSON.parse(response);
                $(".product_id"+j).html("<option value=''>Select</option>"+obj.option);
                $(".quantity"+j).attr("max",obj.stock_count);
                $(".unit_price"+j).val(obj.price);
            }
        });

        $(".product_id"+j).change(function(){
            var warehouse_id = $("#warehouse_id option:selected").val();
            var product_id = $(".product_id"+j+" option:selected").val();
            $.ajax({
                type: 'POST',
                url: "",
                data: {"warehouse_id":warehouse_id,"product_id":product_id,"action":"ajax_call_1"},
                success: function(response) { 
                    obj = JSON.parse(response);
                    $(".quantity"+j).attr("max",obj.stock_count);
                    $(".unit_price"+j).val(obj.price);
                }
            });
        });
        $(".quantity"+j).keyup(function(){
            var quantity = parseInt($(".quantity"+j).val());
            if(quantity != ''){    
                var unit_price = parseInt($(".unit_price"+j).val());
                $(".total_pro_amt"+j).val(unit_price * quantity);

                var total_pro_amt = parseInt($(".total_pro_amt"+j).val());
                var sub_total_amt = parseInt($(".sub_total_amt").val());
                total_pro_amt = 0;
                $(".total_pro_amt").each(function() {
                    total_pro_amt = total_pro_amt + parseInt(this.value);
                    $(".sub_total_amt").val(total_pro_amt);
                    $(".sub_total_amt").html(total_pro_amt);
                    $("#discount").attr("max",total_pro_amt);
                    var discount = parseInt($("#discount").val());
                    if(discount == ""){ discount = 0; }
                    $(".total_amt").html(total_pro_amt - discount);
                });
                
            }
        });
        i++;
    });

    $("body").on("click",".remove",function(){ 
        $(this).parents(".after-add-more").remove();
    });
});
</script>
<script>
    $( function() {
        $( "#cheque_date,#trans_date" ).datepicker();
    } );
    function redirect_(uri){
        window.location.href = uri;
    }
  </script>