
<style type="text/css">
    .taxdiv{
        font-size: 11px;
    }
    .buttontopnav{
        text-align: left;
        margin-top: 6px;
        padding: 6px;
        z-index: 100;
    }
    .modal-body {
        padding: 0px;
    }
    #myModal .DTTTFooter{
        margin-right: 0px;
        margin-left: 0px;
    }
    #myModal .widget-body{
        box-shadow: none;
    }
    #createproductform{
        padding: 10px;
    }
    .retqtytxt{
        text-align: right;
        height: 38px;
        width: 100%;
        border: none !important;
        background: transparent;
    }
     #loading{
        position: absolute;
        background-color: #63c4e90a;
        width: 97.5%;
        text-align: center;
        height: 100%;
        z-index: 100;
        padding-top: 120px;
    }
    #deliverydate{
        cursor: auto;
    }
    .copyqtytxt {
        text-align: right;
        height: 38px;
        width: 100%;
        border: none !important;
        background: transparent;
    }
    .inputs {
        padding-left: 8px;
    }
</style>

<link rel="stylesheet" type="text/css" href="<?= base_url() ?>assets/css/style.css">
<!-- Page Content -->
<div class="page-content" id="purchaseinvoicediv">
    <!-- Page Header -->
    <div class="page-header position-relative">
        <div class="header-title">
            
            <h1>
                <?php
            if ($editRec->STATUS == -1 || $editRec->STATUS == -2) {
                echo $this->lang->line("PURCHASERETURNINVOICE");
             }else {
                echo $this->lang->line("PURCHASEINVOICE");
            } ?>
            </h1>
        </div>
        <!--Header Buttons-->
        <div class="header-buttons">
            <a class="sidebar-toggler" href="#">
                <i class="fa fa-arrows-h"></i>
            </a>
            <a class="refresh" id="refresh-toggler" href="">
                <i class="glyphicon glyphicon-refresh"></i>
            </a>
            <a class="fullscreen" id="fullscreen-toggler" href="#">
                <i class="glyphicon glyphicon-fullscreen"></i>
            </a>
        </div>
        <!--Header Buttons End-->
    </div>
    <!-- /Page Header -->
    <!-- Page Body -->
    <div class="page-body">
        <?php
        if ($this->session->flashdata('msg')) {
        ?>
            <div class="alert alert-danger fade in">
                <?php echo $this->session->flashdata('msg'); $this->session->unset_userdata('msg');?>
            </div>
        <?php
        }
        ?>
        <div class="btn-group">
            <button type="button" class="btn btn-blue shiny" id="backtolist"
                    onclick="location.href = '<?php echo base_url('inventory/purchaseinvoice/'); ?>'">
                <i class="fa fa-arrow-left"></i> <?php echo $this->lang->line("BACKTOLIST"); ?>
            </button>
            <?php
            if ($editRec->STATUS != 2 && $editRec->STATUS != -2) {
                ?>
                <button type="button" class="btn btn-success shiny" id="submit" data-toggle="modal" data-target="#submitmodal"><i class="fa fa fa-send-o"></i> <?php echo $this->lang->line("SUBMIT"); ?></button>
                <?php
            }
            ?>
            <button type="button" class="btn btn-maroon shiny" id="printpi"><i class="fa fa-print"></i> <?php echo $this->lang->line("PRINT"); ?> </button>
    <!--        <button type="button" class="btn btn-default" id="printpo"><i class="fa fa-print"></i> Print PO</button>
            <button type="button" class="btn btn-default" id="printpi"><i class="fa fa-print"></i> Print PI</button>-->
            <?php
            if ($editRec->STATUS != 2 && $editRec->STATUS != -2) {
                ?>
                <button type="button" class="btn btn-danger shiny" data-toggle="modal" data-target="#confirmmodal"><i class="fa fa-trash-o"></i> <?php echo $this->lang->line("DELETE"); ?> </button>
            <?php
            }
            ?>
            <?php
            if ($editRec->STATUS == 2) {
            ?>
                <button type="button" class="btn btn-warning shiny" id="purchasereturnbtn">
                    <i class="fa fa-undo"></i> <?php echo $this->lang->line("PURCHASERETURN"); ?> </button>
            <?php
            }
            ?>
                <button type="button" class="btn btn-purple shiny" id="copypurchasebtn">
                    <i class="fa fa-copy"></i> <?php echo $this->lang->line("COPYINVOICE"); ?> </button>
        </div>

        <div class="row">
            <input type="hidden" name="purchasereturnId" id="purchasereturnId" value="" />
            <div class="col-lg-12 col-sm-12 col-xs-12">
                <div id="loading"><img src="<?php echo base_url(); ?>assets/img/ajax-loader.gif" /></div>
                <div class="well invoice-container">
                    <div class="row">
                        <div class="col-xs-6">
                            <h3 class="">
                                <i class="fa fa-check"></i>
<?php
if ($editRec->STATUS == -1 || $editRec->STATUS == -2) {
    echo $this->lang->line("PURCHASERETURNINVOICE");
 }else {
    echo $this->lang->line("PURCHASEINVOICE");
} ?>
                            </h3>
                            <span><?php echo $this->lang->line("CREATEDON"); ?> 
                                <?php echo $this->formatslist->datetimeFormat($editRec->CREATEDDATE); ?>
                            </span>
                        </div>
                        <div class="col-xs-6 
<?php if($this->session->lang == "ar" && $this->session->langfolder == "arabic") echo "text-left"; else echo "text-right"; ?>">
                            <h1>
                                <a href="">
                                    <img src="<?php echo $company->DISPLAYLOGO; ?>" alt="" style="height:60px; margin-right:-17px">
                                </a>
                            </h1>
                            <div>
                                <span><?php echo $company->ADDRESS1; ?>, </span>
                                <span><?php echo $company->ADDRESS2; ?>, </span>
                                <span><?php echo $company->EMAIL; ?>, </span>
                                <span><?php echo $company->WEBSITE; ?>, </span>
                                <span><?php echo $company->PHONE1; ?>.</span>
                            </div>
                            <div class="horizontal-space"></div>
                        </div>
                    </div>
                    <link href="<?php echo base_url(); ?>assets/css/jquery-ui.min.css" rel="stylesheet" />
                    <script src="<?php echo base_url(); ?>assets/js/jquery-ui.js"></script>
                    <script type="text/javascript">
                        $(function () {
                            $("#vendorautocomp").autocomplete({
                                source: "<?php echo base_url('inventory/purchaseinvoice/getsuppliersbyname/'); ?>",
                                minLength: 1,
                                select: function (event, ui) {
                                    $("#supplieraddress").html("");
                                    if (ui.item.address) {
                                        $("#supplieraddress").append("<li>" + ui.item.address + "</li>");
                                    }
                                    if (ui.item.city) {
                                        $("#supplieraddress").append("<li>" + ui.item.city + "</li>");
                                    }
                                    if (ui.item.region) {
                                        $("#supplieraddress").append("<li>" + ui.item.region + "</li>");
                                    }
                                    if (ui.item.country) {
                                        $("#supplieraddress").append("<li>" + ui.item.country + "</li>");
                                    }
                                    if (ui.item.postal) {
                                        $("#supplieraddress").append("<li>" + ui.item.postal + "</li>");
                                    }
                                    if (ui.item.phone) {
                                        $("#supplieraddress").append("<li>" + ui.item.phone + "</li>");
                                    }
                                    if (ui.item.email) {
                                        $("#supplieraddress").append("<li>" + ui.item.email + "</li>");
                                    }
                                    $("#SUPPLIERID").val(ui.item.id);
                                    SaveInvoice(ui.item.id);
                                }
                            });
                        });
                    </script>
                    
                    <div class="row">
                        <div class="col-xs-12 col-md-7">
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <div class="form-group clearfix">
                                        <div class="col-sm-12 nopadding">
                                            <div class="ui-widget">
                                                <div class="input-group" style="width: 100%;">
                                                <?php
                                                if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                                    ?>
                                                    <span class="form-control disabledspan"><?php echo $editRec->name; ?></span>
                                                    <?php
                                                } else {
                                                    ?>
                        <input type="text" name="vendorautocomp" id="vendorautocomp" class="inp-form form-control"
                               value="<?php echo $editRec->name; ?>"
 <?php if ($editRec->STATUS == 2 || $editRec->STATUS == -2) { echo "readonly"; } ?> placeholder="<?php echo $this->lang->line("ENTERSUPPLIERNAME"); ?>" />
<span class="input-group-btn">
    <a href="#" class="btn btn-sky shiny" data-toggle="modal" data-target="#MYMODALLEADCUSTOMER" id="BTNLEADCUSTOMERLIST"><i class="fa fa-search"></i> Browse</a>
</span>
                        <input type="hidden" name="SUPPLIERID" id="SUPPLIERID" value="<?php echo $editRec->SUPPLIERID; ?>" />
                                                    <?php
                                                }
                                                ?>
                        </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <div class="panel-body">
                                    <ul id="supplieraddress">
                                        <?php
                                        if ($supplierdata) {
                                            ?>
                                            <li>
                                                <?php
                                                echo $supplierdata["ADDRESS"];
                                                ?>
                                            </li>
                                            <li>
                                                <?php
                                                echo $supplierdata["CITY"];
                                                ?>
                                            </li>
                                            <li>
                                                <?php
                                                echo $supplierdata["REGION"];
                                                ?>
                                            </li>
                                            <li>
                                                <?php
                                                echo $supplierdata["COUNTRY"];
                                                ?>
                                            </li>
                                            <li>
                                                <?php
                                                echo $supplierdata["POSTAL"];
                                                ?>
                                            </li>
                                            <li>
                                                <?php
                                                echo $supplierdata["PHONE"];
                                                ?>
                                            </li>
                                            <li>
                                                <?php
                                                echo $supplierdata["EMAIL"];
                                                ?>
                                            </li>
                                            <?php
                                        }
                                        ?>
                                    </ul>                                      
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-12 col-md-5 text-right">
                            <div class="panel panel-default">
                                <div class="panel-body">
                                    
                                    <div class="form-group datefrmgrp clearfix">
                                        <label for="inputEmail3" class="col-sm-4 control-label no-padding-right">
                                            <?php echo $this->lang->line("INVOICE"); ?> #:</label>
                                        <div class="col-sm-8">
                                            <?php
                                            if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                                ?>
                                                <span class="form-control disabledspan"><?php echo $editRec->REFERENCE; ?></span>
                                                <?php
                                            } else {
                                                ?>
                                                <input type="text" class="form-control inptext" id="reference" 
                                                       placeholder="<?php echo $this->lang->line("INVOICENUMBER"); ?>"
                                                       value="<?php echo $editRec->REFERENCE; ?>">
                                                <?php
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    
                                    <div class="form-group datefrmgrp clearfix">
                                        <label for="inputEmail3" class="col-sm-4 control-label no-padding-right">
                                            Reference :</label>
                                        <div class="col-sm-8">
                                            <?php
                                            if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                                ?>
                                                <span class="form-control disabledspan"><?php echo $editRec->COMMENTS; ?></span>
                                                <?php
                                            } else {
                                                ?>
                                                <input type="text" class="form-control inptext" id="COMMENTS" 
                                                    placeholder="Reference"
                                                    value="<?php echo $editRec->COMMENTS; ?>">
                                                <?php
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    
                                    <div class="form-group datefrmgrp clearfix">
                                        <label for="inputEmail3" class="col-sm-4 control-label no-padding-right">
                                            <?php echo $this->lang->line("INVOICEDATE"); ?>
                                            :</label>
                                        <div class="col-sm-8">
                                            <?php
                                            if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                            ?>
                                                <span class="form-control disabledspan"><?php echo $editRec->DELIVERYDATE; ?></span>
                                            <?php
                                            } else {
                                            ?>
                                                <input type="text" class="form-control inptext" id="deliverydate"
                                                    name="deliverydate" value="<?php echo $editRec->DELIVERYDATE; ?>"
                                                    placeholder="<?php echo $this->lang->line("INVOICEDATE"); ?>" readonly />
                                            <?php
                                            }
                                            ?>
                                        </div>
                                    </div>
                                    
                                </div>
                            </div>
                        </div>
                    </div> <!-- / end client details section -->
                    <input type="hidden" id="pinvid" value="<?php echo $invoiceId; ?>" />
                    <div class="table-scrollable">
                        <table id="mainTable" class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th style="width: 70px;"><?php echo $this->lang->line("ITEMCODE"); ?></th>
                                    <th style="width: 300px;"><?php echo $this->lang->line("ITEMNAME"); ?></th>
                                    <th class="labelcenteralign" id="orgqtyid" style="width: 70px;"><?php echo $this->lang->line("QTY"); ?></th>
                                    <th class="labelrightalign returnqtylabeltd" style="display:none;width: 90px;">
                                        <?php echo $this->lang->line("RETURNQTY"); ?>
                                    </th>
                                    <th class="labelrightalign" style="width: 100px;"><?php echo $this->lang->line("PRICE"); ?></th>
                                    <th class="labelrightalign" style="display:none;"><?php echo $this->lang->line("DISCOUNT"); ?></th>
                                    <th class="labelrightalign" style="width: 100px;"><?php echo $this->lang->line("TAX"); ?></th>
                                    <th class="labelrightalign" style="width: 110px;"><?php echo $this->lang->line("LINETOTAL"); ?></th>
                                    <?php if ($editRec->STATUS != 2 && $editRec->STATUS != -2) { ?>
                                        <th></th>
                            <?php } ?>
                                </tr>
                            </thead>
                            <?php
                            $i = 1;
                            if ($editinvlinesRec) {
                                //print_r($editinvlinesRec);exit;
                                if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                    foreach ($editinvlinesRec as $invlinesRec) {
                                        ?>
                                        <tr id="tablerow_<?php echo $i; ?>" data-invitemid="<?php echo $invlinesRec->ID; ?>">
                                            <td class="disabledtd" style="width: 70px;">
                                                <?php echo $invlinesRec->CODE; ?>
                                            </td>
                                            <td class="disabledtd" style="width: 300px;">
                                                <?php echo $invlinesRec->NAME; ?>
                                            </td>
                                            <td class="disabledtd labelcenteralign" style="width: 70px;">
                                                <?php echo $invlinesRec->QUANTITY; ?>
                                            </td>
                                            <td class="nopaddingtd returnqtytd" style="display:none;width: 90px;">
                                                <input type="number" class="retqtytxt" name="retqtytxt" id="retqtytxt_<?php echo $i; ?>"
                                                       onchange="updatepreturnitem(<?php echo $i; ?>,'<?php echo $invlinesRec->PRODUCT; ?>'
                                                           ,this.value,<?php echo $invlinesRec->UNITPRICE; ?>,'<?php echo $invlinesRec->TAXID; ?>')"
                                                           data-invid="" />
                                            </td>
<!--                                            <td class="nopaddingtd copyqtytd" style="display:none;">
                                                <input type="number" class="copyqtytxt" name="copyqtytxt" id="copyqtytxt_<?php echo $i; ?>"
                                                       onchange="updatecopypiitem(<?php echo $i; ?>,'<?php echo $invlinesRec->PRODUCT; ?>'
,this.value,<?php echo $invlinesRec->UNITPRICE; ?>,'<?php echo $invlinesRec->TAXID; ?>')" data-invid="" />
                                            </td>-->
                                            <td class="disabledtd labelrightalign">
                                                <?php echo $this->formatslist->formatCurrencywithHash($invlinesRec->UNITPRICE); ?>
                                            <td class="disabledtd labelrightalign" style="display:none;">
                                            <?php echo $this->formatslist->formatCurrencywithHash($invlinesRec->DISCOUNT); ?>
                                            </td>
                                            <?php
                                            $linetotal = (($invlinesRec->QUANTITY * $invlinesRec->UNITPRICE) + $invlinesRec->DISCOUNT)+$invlinesRec->TAXAMOUNT;
                                            ?>
                                            <td class="disabledtd labelrightalign" style="width: 100px;">
                                                <?php echo $this->formatslist->formatCurrencywithHash($invlinesRec->TAXAMOUNT); ?>
                                            </td>
                                            <td class="labelrightalign disabledtd" style="width: 100px;">
                                                <?php
                                                //echo $this->formats->numberformat($linetotal);
                                                echo $this->formatslist->formatCurrencywithHash($linetotal);
                                                ?>
                                            </td>
                                        </tr>
                                        <?php
                                        $subtotal = $subtotal + $linetotal;
                                        $i++;
                                    }
                                } else {
                                    $taxtotal=0;
                                    foreach ($editinvlinesRec as $invlinesRec) {
                                        ?>
                                        <tr id="tablerow_<?php echo $i; ?>" data-invitemid="<?php echo $invlinesRec->ID; ?>" class="tooltiptr">
                                            <td class="nopaddingtd">
                                                <input type="text" class="inputs itemcode" name="itemcode[]" id="itemcode_<?php echo $i; ?>" data-ind="<?php echo $i; ?>" value="<?php echo $invlinesRec->CODE; ?>" />
                                                <input type="hidden" name="itemid[]" id="itemid_<?php echo $i; ?>" value="<?php echo $invlinesRec->PRODUCT; ?>" />
                                            </td>
                                            <td class="nopaddingtd">
                                                <input type="text" class="inputs itemname" name="item[]" id="item_<?php echo $i; ?>" data-ind="<?php echo $i; ?>" value="<?php echo $invlinesRec->NAME; ?>"  />
                                            </td>
                                            <td class="nopaddingtd">
                                                <input type="number" class="inputs itemqty labelcenteralign" name="qty[]" id="qty_<?php echo $i; ?>" data-ind="<?php echo $i; ?>" value="<?php echo $invlinesRec->QUANTITY; ?>" />
                                            </td>
                                            <td class="nopaddingtd">
                                                <input type="number" name="price[]" id="price_<?php echo $i; ?>" class="inputs itemprice labelrightalign" data-ind="<?php echo $i; ?>" value="<?php echo $this->formatslist->formatCurrency($invlinesRec->UNITPRICE); ?>" />
                                            <td class="nopaddingtd" style="display:none;">
                                                <input type="text" name="discount[]" class="inputs itemdiscount labelrightalign" id="discount_<?php echo $i; ?>" data-ind="<?php echo $i; ?>" value="<?php echo $this->formatslist->formatCurrency($invlinesRec->DISCOUNT); ?>" />
                                            </td>
                                            <?php
                                            $linetotal = ($invlinesRec->QUANTITY * $invlinesRec->UNITPRICE) + $invlinesRec->DISCOUNT+$invlinesRec->TAXAMOUNT;
                                            ?>
                                            <td class="text-right">
                                                <span id="taxamountItem_<?php echo $i; ?>" class="taxamountspan"><?php echo $this->formatslist->formatCurrency($invlinesRec->TAXAMOUNT); ?></span>
                                                <input type="hidden" name="taxid_<?php echo $i; ?>" id="taxid_<?php echo $i; ?>" value="<?php echo $invlinesRec->TAXID; ?>" />
                                            </td>
                                            <td class="nopaddingtd"><input type="number" name="linetotal[]" class="inputs itemlinetotal labelrightalign lst" id="linetotal_<?php echo $i; ?>" data-ind="<?php echo $i; ?>" value="<?php /* echo $this->formats->numberformat($linetotal); */ echo $this->formatslist->formatCurrency($linetotal); ?>" /></td>
                                            <?php if ($editRec->STATUS == 1 || $editRec->STATUS == -1) { ?>
                                            <td class="text-center" width="50"><a class="btn btn-danger btn-xs delete" onclick="tablerowremove(<?php echo $i; ?>,'<?php echo $invlinesRec->ID; ?>')"><i class="fa fa-trash-o"></i></a></td>
                                            <?php } ?>
                                        </tr>
                                        <?php
                                        $taxtotal   =   $taxtotal+$invlinesRec->TAXAMOUNT;
                                        $subtotal = $subtotal + $linetotal;
                                        $i++;
                                    }
                                }
                            } else {
                                $cnt = 1;
                                if ($editRec->STATUS != 2 && $editRec->STATUS != -2) {
                                    ?>
                                    <tr id="tablerow_1" data-invitemid="">
                                        <td class="nopaddingtd">
                                            <input type="text" class="inputs itemcode" name="itemcode[]" id="itemcode_1" data-ind="1" value="" />
                                            <input type="hidden" name="itemid[]" id="itemid_1" value="" />
                                        </td>
                                        <td class="nopaddingtd">
                                            <input type="text" class="inputs itemname" name="item[]" id="item_1" data-ind="1" value="" />
                                        </td>
                                        <td class="nopaddingtd">
                                            <input type="number" class="inputs itemqty text-center" name="qty[]" id="qty_1" data-ind="1" value="" />
                                        </td>
                                        <td class="nopaddingtd">
                                            <input type="number" name="price[]" id="price_1" class="inputs itemprice text-right" data-ind="1" value="" />
                                        <td class="nopaddingtd" style="display:none;">
                                            <input type="text" name="discount[]" class="inputs itemdiscount text-right" id="discount_1" data-ind="1" value="" />
                                        </td>
                                        <td class="text-right"><span id="taxamountItem_1" class="taxamountspan"></span><input type="hidden" name="taxid_1" id="taxid_1" value="" /></td>
                                        <td class="nopaddingtd"><input type="number" name="linetotal[]" class="inputs itemlinetotal text-right lst" id="linetotal_1" data-ind="1" value="0" /></td>
                                        <td class="text-center" width="50"><a class="btn btn-danger btn-xs delete" onclick="tablerowremove('1', '')"><i class="fa fa-trash-o"></i></a></td>
                                    </tr>
                            <?php
                                }
                            }
                            ?>
                            <tfoot>
                            <?php //$subtotal    =   $nettotal+$discounttotal;  ?>
                                <tr>
                                    <td colspan="4" class="invisible bg-snow footerpurchaseinvtable"></td>
                                    <td class="text-right"><div class="labelrightalign"><?php echo $this->lang->line("SUBTOTAL"); ?></div></td>
                                    <td class="text-right">
                                        <span id="subtotalspan">
                                            <?php
                                            //echo $this->formats->numberformat($subtotal);
                                            echo $this->formatslist->formatCurrencywithHash($subtotal);
                                            ?>
                                        </span>
                                    </td>
                                </tr>
<tr>
    <td colspan="4" class="invisible bg-snow footerpurchaseinvtable"></td>
    <td class="text-right">
        <?php echo $this->lang->line("TAXDETAILS"); ?>
    </td>
    <td class="text-right">
        <span id="taxtotalspan">
            <?php
                if($taxRecData){
                    foreach($taxRecData as $taxRec){
            ?>
            <div class="taxdiv"><?php echo $taxRec->NAME." : ".$this->formatslist->formatCurrencywithHash($taxRec->TAXAMOUNT);?></div>
            <?php
                    }
                }
                //echo $this->formatslist->formatCurrencywithHash($taxtotal);
            ?>
        </span>
    </td>
</tr>
                                <tr>
                                    <td colspan="4" class="invisible bg-snow footerpurchaseinvtable"></td>
                                    <td class="text-right"><?php echo $this->lang->line("LESSDISCOUNT"); ?></td>
                                    <?php
                                    if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                        ?>
                                        <td class="disabledtd text-right">
                                            <?php echo $this->formatslist->formatCurrencywithHash($editRec->DISCOUNT); ?>
                                        </td>
                                        <?php
                                    } else {
                                        ?>
                                        <td class="text-right nopaddingtd">
                                            <input type="text" name="discounttxt" id="discounttxt" class="text-right padding-right-10 txttotals" value="<?php echo $editRec->DISCOUNT; ?>" onchange="NetTotalsCalculation()" />
                                        </td>
                                    <?php } ?>
                                </tr>
                                <tr>
                                    <td colspan="4" class="invisible bg-snow footerpurchaseinvtable"></td>
                                    <td class="text-right"><?php echo $this->lang->line("FREIGHT"); ?></td>
                                    <?php
                                    if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                        ?>
                                        <td class="disabledtd text-right">
                                        <?php echo $this->formatslist->formatCurrencywithHash($editRec->FREIGHTAMOUNT); ?>
                                        </td>
                                        <?php
                                    } else {
                                        ?>
                                        <td class="text-right nopaddingtd">
                                            <input type="number" name="freighttxt" id="freighttxt"
                                                   class="text-right txttotals" value="<?php echo $editRec->FREIGHTAMOUNT; ?>"
                                                   onchange="NetTotalsCalculation()" />
                                        </td>
<?php } ?>
                                </tr>
                                <tr>
                                    <td colspan="4" class="invisible bg-snow footerpurchaseinvtable"></td>
                                    <td class="text-right"><?php echo $this->lang->line("ADJUSTMENT"); ?></td>
                                    <?php
                                    if ($editRec->STATUS == 2 || $editRec->STATUS == -2) {
                                        ?>
                                        <td class="disabledtd text-right">
                                            <?php echo $this->formatslist->formatCurrencywithHash($editRec->ADJUSTMENT); ?>
                                        </td>
                                        <?php
                                    } else {
                                        ?>
                                        <td class="text-right nopaddingtd">
                                            <input type="number" name="adjustmenttxt" id="adjustmenttxt"
                                                class="text-right txttotals" value="<?php echo $editRec->ADJUSTMENT; ?>"
                                                onchange="NetTotalsCalculation()" />
                                        </td>
                                    <?php } ?>
                                </tr>
                                <tr>
                                    <td colspan="4" class="invisible bg-snow footerpurchaseinvtable"></td>
                                    <td class="text-right"><strong><?php echo $this->lang->line("TOTAL"); ?></strong></td>
                                    <td class="text-right ">
                                        <strong>
                                            <span id="nettotalspan">
<?php
$nettotal   =   $subtotal+$editRec->DISCOUNT+$editRec->FREIGHTAMOUNT+$editRec->ADJUSTMENT;
echo $this->formatslist->formatCurrencywithHash($nettotal); ?>
                                            </span>
                                        </strong>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <hr class="wide">
                    <hr class="wide">
                    <div class="invoice-notes">
                        <h5><?php echo $this->lang->line("TERMSANDCONDITIONS"); ?>:</h5>
                        <p>
                            <?php echo $locale->POTERMS; ?>
                        </p>
                    </div>
                    
                    <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal"
                            id="productlist" style="display:none;"><?php echo $this->lang->line("PRODUCTLIST"); ?></button>
                    <!-- Modal -->
                    <div id="myModal" class="modal fade" role="dialog">
                        <div class="modal-dialog">
                            <!-- Modal content-->
                            <div class="modal-content">
                                <div class="modal-body">
                                    <div class="col-xs-6 buttontopnav">
                                        <a class="btn btn-success btn-sm icon-only white" id="buttoncreateproduct" onclick="createproduct()" href="javascript:void(0);"><i class="fa fa-plus"></i></a>
                                        <input type="text" class="dt-column_div" placeholder="<?php echo $this->lang->line("SEARCHBYBARCODE"); ?> ..." id="searchbarcodepi" name="searchbarcodepi" />
                                    </div>
                                    <button type="button" class="close" data-dismiss="modal" style="margin: 3px;">×</button>
                                    <div class="widget-body no-padding" id="productlistselect">
                                        <table class="table table-bordered table-hover table-striped" id="productsdatatablelist">
                                            <thead class="bordered-darkorange">
                                                <tr role="row">
                                                    <th><?php echo $this->lang->line("CATEGORY"); ?></th>
                                                    <th><?php echo $this->lang->line("REFERENCE"); ?></th>
                                                    <th><?php echo $this->lang->line("CODE"); ?></th>
                                                    <th><?php echo $this->lang->line("NAME"); ?></th>
                                                    <th><?php echo $this->lang->line("PRICEBUY"); ?></th>
                                                    <th><?php echo $this->lang->line("PRICESELL"); ?></th>
                                                    <th><?php echo $this->lang->line("STOCKVALUE"); ?></th>
                                                    <th><?php echo $this->lang->line("ACTION"); ?></th>
                                                </tr>
                                            </thead>
                                        </table>
                                    </div>
                                    
<div id="createproductform" style="display: none;">
<form action="" method="post" id="form1" name="form1" enctype="multipart/form-data">
      <div class="modal-body">
       <div class="row">
          <div class="col-lg-12 col-sm-12 col-xs-12">
             <div id="simplewizard" class="wizard" data-target="#simplewizard-steps">
                <ul class="steps">
                    <li data-target="#simplewizardstep1" class="active general_tabs" id="general_tabs1">
                        <?php echo $this->lang->line("GENERAL"); ?><span class="chevron"></span></li>
                    <li data-target="#simplewizardstep2" class="general_tabs" id="general_tabs2">
                        <?php echo $this->lang->line("STOCK"); ?><span class="chevron"></span></li>
                    <li data-target="#simplewizardstep3" class="general_tabs" id="general_tabs3">
                        <?php echo $this->lang->line("PROPORTIES"); ?><span class="chevron"></span></li>
                    <li data-target="#simplewizardstep4" class="general_tabs" id="general_tabs4" style="display: none;">
                        <?php echo $this->lang->line("BREAKDOWN"); ?><span class="chevron"></span></li>
                </ul>
              </div>
          </div>
            <div class="col-md-12 addmargin display_error" style="margin-top: 10px;"></div>                 
            <div class="col-md-12 addmargin success_msg" style="margin-top: 10px;">
                <?php echo $this->lang->line("RECORDADDEDSUCCESSFULLY"); ?></div>
            <div class="col-lg-12 col-sm-12 col-xs-12 general_tabs_div" id="general_tabs1_div" >            
                  <div id="horizontal-form">   
                    <div class="form-group" style="margin-top: 14px;">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right">
                          <?php echo $this->lang->line("REFERENCENO"); ?>. <span class="mandatory">*</span></label>
                        <div class="col-sm-8 addmargin">
                          <input type="text" class="form-control prreference" id="reference" name="reference"
                                 placeholder="<?php echo $this->lang->line("REFERENCENO"); ?>.">
                        </div>
                        <div class="col-sm-1">
                           <i class="fa fa-plus btn" aria-hidden="true" id="reference_id"></i>
                        </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("BARCODE"); ?> <span class="mandatory">*</span></label>
                        <div class="col-sm-8 addmargin">
                          <input type="text" class="form-control" id="code" name="code" placeholder="<?php echo $this->lang->line("BARCODE"); ?>">
                        </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("NAME"); ?>
                          <span class="mandatory">*</span></label>
                        <div class="col-sm-8 addmargin">
                          <input type="text" class="form-control" id="name" name="name" placeholder="<?php echo $this->lang->line("NAME"); ?>">
                        </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right">
                          <?php echo $this->lang->line("PRODUCTCATEGORY"); ?> <span class="mandatory">*</span></label>
                        <div class="col-sm-8 addmargin">
                          <select name="category" id="category" class="form-control">
                            <option value="">--- <?php echo $this->lang->line("SELECT"); ?> ---</option>
                             <?php foreach($categories as $categories_list) { ?>
                              <option value="<?php echo $categories_list->ID;?>"><?php echo $categories_list->NAME;?></option>
                            <?php } ?>
                          </select>
                        </div>
                    </div>
                      <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right">
                        <?php echo $this->lang->line("TAXCATEGORY"); ?><span class="mandatory">*</span></label>
                        <div class="col-sm-8 addmargin">
                          <select name="taxcat" id="taxcat" class="form-control">
                            <option value="">--- <?php echo $this->lang->line("SELECT"); ?> ---</option> 
                            <?php foreach($tax_categories as $tax_categories_list) { ?>
                              <option value="<?php echo $tax_categories_list->ID;?>"><?php echo $tax_categories_list->NAME;?></option>
                            <?php } ?>                             
                          </select>
                        </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("PRICEBUY"); ?>
                          <span class="mandatory">*</span></label>
                        <div class="col-sm-8 addmargin">
                          <input type="text" class="form-control" id="pricebuy" name="pricebuy"
                                 placeholder="<?php echo $this->lang->line("PRICEBUY"); ?>" onkeypress="javascript:return isNumber(event)">

                        </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("PRICESELL"); ?>
                          <span class="mandatory">*</span></label>
                        <div class="col-sm-4 addmargin">
                          <input type="text" class="form-control" id="sellpricehd" name="sellpricehd"
                                 placeholder="<?php echo $this->lang->line("PRICESELL"); ?>" onkeypress="javascript:return isNumber(event)">
                            <input type="hidden" class="form-control" id="pricesell" name="pricesell" />

                        </div>
                        <div class="col-sm-4 addmargin">
                          <input type="text" class="form-control" id="profit" name="profit" 
                                 placeholder="<?php echo $this->lang->line("PROFITINPERCENTAGE"); ?>" onkeypress="javascript:return isNumber(event)">
                        </div>
                    </div>
                    <div class="form-group">
                       <label for="inputEmail3" class="col-sm-3 control-label no-padding-right">
                           <?php echo $this->lang->line("SELLPRICEPLUSTAX"); ?></label>
                         <div class="col-sm-8 addmargin">
                           <input type="text" class="form-control" id="sellpricewithtax" name="sellpricewithtax"
                                  placeholder="<?php echo $this->lang->line("SELLPRICEPLUSTAX"); ?>" onkeypress="javascript:return isNumber(event)">
                         </div>
                    </div>
                    <div class="form-group">
                      <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("ATTRIBUTES"); ?> </label>
                        <div class="col-sm-8 addmargin">
                           <select name="attributeset_id" id="attributeset_id" class="form-control">
                            <option value="">--- <?php echo $this->lang->line("SELECT"); ?> ---</option>  
                            <?php foreach($attributeset as $attributeset_list) { ?>
                              <option value="<?php echo $attributeset_list->ID;?>"><?php echo $attributeset_list->NAME;?></option>
                            <?php } ?>                            
                          </select>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-12 col-sm-12 col-xs-12 general_tabs_div" id="general_tabs2_div" style="display: none;">            
                  <div id="horizontal-form">                    
                      <div class="form-group">
                        <label for="inputEmail3" class="col-sm-3 control-label no-padding-right">
                            <?php echo $this->lang->line("STOCKCOSTBYYEAR"); ?> </label>
                          <div class="col-sm-8 addmargin">
                            <input type="text" class="form-control" id="stock_cost_by_year" name="stock_cost_by_year"
                                placeholder="<?php echo $this->lang->line("STOCKCOSTBYYEAR"); ?>">
                          </div>
                      </div>
                      <div class="form-group">
                        <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("STOCKVOLUME"); ?></label>
                          <div class="col-sm-8 addmargin">
                            <input type="text" class="form-control" id="stock_volume" name="stock_volume"
                                placeholder="<?php echo $this->lang->line("STOCKVOLUME"); ?>">
                          </div>
                      </div>

                      <div class="form-group">
                        <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("INCATALOG"); ?>
                            </label>
                          <div class="col-sm-1">
                            <div class="checkbox">
                              <label>
                                <input type="checkbox" class="inverted" id="incatalog" name="incat" checked />
                                <span class="text"></span>
                             </label>
                          </div>
                          </div>
                          <div class="col-sm-7 addmargin">
                            <input type="text" class="form-control" id="order" name="catorder"
                                   placeholder="<?php echo $this->lang->line("ORDER"); ?>" disabled="disabled" 
                                   onkeypress="javascript:return isNumber(event)">
                          </div>
                      </div>
                      <div class="form-group">
                        <label for="inputEmail3" class="col-sm-3 control-label no-padding-right">
                            <?php echo $this->lang->line("AUXILARY"); ?></label>
                            <div class="col-sm-8">
                              <div class="checkbox">
                                    <label>
                                        <input type="checkbox" class="inverted" id="auxilary" name="iscom">
                                        <span class="text"></span>
                                    </label>
                                </div>
                            </div>
                      </div>       
                      <div class="form-group">
                        <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("SCALE"); ?></label>
                          <div class="col-sm-8">
                            <div class="checkbox">
                              <label>
                                <input type="checkbox" class="inverted" id="scale" name="isscale">
                                <span class="text"></span>
                             </label>
                          </div>
                          </div>
                      </div> 
                      <input type="checkbox" class="inverted" id="breakdown" style="visibility: hidden;">
<!--                      <div class="form-group">
                        <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php // echo $this->lang->line("BREAKDOWN"); ?></label>
                          <div class="col-sm-8">
                            <div class="checkbox">
                              <label>
                                <input type="checkbox" class="inverted" id="breakdown">
                                <span class="text"></span>
                             </label>
                          </div>
                          </div>
                      </div> -->
                      
                      <div class="form-group">
                            <label for="inputEmail3" class="col-sm-3 control-label no-padding-right"><?php echo $this->lang->line("MATERIAL"); ?></label>
                            <div class="col-sm-8">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" class="inverted" id="ITEMTYPE" name="ITEMTYPE" />
                                        <span class="text"></span>
                                    </label>
                                </div>
                            </div>
                        </div>
                  </div>
            </div>
             <div class="col-lg-12 col-sm-12 col-xs-12 general_tabs_div" id="general_tabs3_div" style="display: none;">            
                <div id="horizontal-form">
                  <div class="form-group" style="margin-top: 10px;"> 
                    <textarea class="form-control propoties_textarea" name="proporties"><?php if(isset($resources[0]->CONTENT)) echo $resources[0]->CONTENT; ?>  </textarea>
                  </div>  
                </div>  
            </div>
            <div class="col-lg-12 col-sm-12 col-xs-12 general_tabs_div" id="general_tabs4_div" style="display: none;">            
                  <div id="horizontal-form">
                    <div class="form-group" style="margin-top: 10px;">  
<!--                    <table id="example7" class="table table-bordered table-striped">
                      <thead>
                        <tr>
                            <th class="addwidth"><?php echo $this->lang->line("BARCODE"); ?></th>
                            <th><?php echo $this->lang->line("CATEGORY"); ?></th>
                            <th><?php echo $this->lang->line("MATERIAL"); ?></th>
                            <th class="addwidth"><?php echo $this->lang->line("UNIT"); ?></th>
                            <th style="width: 10px;">
                              <button onclick="add_brk_dwn();" type="button" id="brk_add" class="">
                                  <i class="fa fa-plus" aria-hidden="true"></i></button>
                            </th>
                        </tr>
                      </thead>
                      <tbody id="tr_brk_dwn"></tbody>
                    </table>-->
                        
                        <table id="example7" class="table table-bordered table-striped">
        <thead>
            <tr>
                <th class="addwidth"><?php echo $this->lang->line("BARCODE"); ?></th>
                <th><?php echo $this->lang->line("MATERIAL"); ?></th>
                <th class="addwidth"><?php echo $this->lang->line("UNIT"); ?></th>
            </tr>
        </thead>
        <tbody id="tr_brk_dwn">
        </tbody>
    </table>
                        
                        
                      <input type="hidden" name="hdcounter" id="hdcounter" value="0" class="hiddenfiled">
                      </div>
                  </div>
                </div>
            </div>    
        </div>
            <div>
        <input type="hidden" name="id" id="id">
        <button type="submit" class="btn btn-success save_btns" id="btn_save" name="save"><?php echo $this->lang->line("SAVE"); ?></button>
        <button type="button" class="btn btn-danger" onclick="viewproductlist()"><?php echo $this->lang->line("CLOSE"); ?></button>
      </div>
      </div>
  </form>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- /Page Body -->
    </div>
    <!-- /Page Content -->
</div>
<!-- /Page Container -->
<!-- Main Container -->
</div>

<div id="undosubmitmodal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><?php echo $this->lang->line("RESETPURCHASEINVOICE"); ?></h4>
            </div>
            <div class="modal-body">
                <p style="margin-left: 22px;"><?php echo $this->lang->line("AREYOUWANTTOUNDO"); ?>
                    </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="return submitundoInvoiceFunction()">
                    <?php echo $this->lang->line("YES"); ?></button>
                <button type="button" class="btn btn-danger" data-dismiss="modal"><?php echo $this->lang->line("NO"); ?></button>
            </div>
        </div>
    </div>
</div>

<div id="submitmodal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><?php echo $this->lang->line("PURCHASEINVOICE"); ?></h4>
            </div>
            <div class="modal-body">
                <p style="margin-left: 21px;"><?php echo $this->lang->line("AREYOUWANTTOSUBMIT"); ?></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" onclick="return submitInvoiceFunction()"><?php echo $this->lang->line("YES"); ?></button>
                <button type="button" class="btn btn-danger" data-dismiss="modal"><?php echo $this->lang->line("NO"); ?></button>
            </div>
        </div>
    </div>
</div>
<div id="confirmmodal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><?php echo $this->lang->line("PURCHASEINVOICE"); ?></h4>
            </div>
            <div class="modal-body">
                <p><?php echo $this->lang->line("AREYOUSURETODELETE"); ?></p>
            </div>
            <div class="modal-footer">
                <input type="hidden" id="okid" value="">
                <button type="button" class="btn btn-success" onclick="return deletePurchaseInvoiceFunction()">
                    <?php echo $this->lang->line("YES"); ?>
                </button>
                <button type="button" class="btn btn-danger" data-dismiss="modal"><?php echo $this->lang->line("NO"); ?></button>
            </div>
        </div>
    </div>
</div>
<button type="button" class="btn btn-success shiny" id="copymodalbtn" data-toggle="modal" data-target="#copymodal" style="display:none;">gg</button>
<div id="copymodal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><?php echo $this->lang->line("PURCHASEINVOICE"); ?></h4>
            </div>
            <div class="modal-body">
                <p style="margin-left: 21px;color: green;">
                    <?php echo $this->lang->line("INVOICEHASBEENCOPIED"); ?></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-danger" data-dismiss="modal"><?php echo $this->lang->line("OK"); ?></button>
            </div>
        </div>
    </div>
</div>

<!-- Modal -->
<div id="MYMODALLEADCUSTOMER" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">×</button>
                <h4><i class="fa fa-users purple"></i> Suppliers List</h4>
            </div>
            <div class="modal-body" style="padding: 12px;">
                <table class="table table-bordered table-hover table-striped" id="leadsupplierdatatable">
                    <thead class="bordered-blueberry">
                        <tr role="row">
                            <th><?php echo $this->lang->line("NAME"); ?></th>
                            <th><?php echo $this->lang->line("SEARCHKEY"); ?></th>
                            <th><?php echo $this->lang->line("TAXID"); ?></th>
                            <th><?php echo $this->lang->line("EMAIL"); ?></th>
                            <th><?php echo $this->lang->line("PHONE"); ?></th>
                            <th><?php echo $this->lang->line("CURRENTDEBT"); ?></th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- Trigger the modal with a button -->
<!--Bootstrap Date Picker-->
<script src="<?= site_url() ?>assets/js/datetime/bootstrap-datepicker.js"></script>
<script type="text/javascript">
    var purchaseflag    =   'purchaseinvoice';
    var floatpointnum   =   '<?php echo $this->formatslist->getCurrencyDecimalPonts(); ?>';
</script>
<input type="hidden" id="currencyDecimel" name="currencyDecimel" value="<?php echo $this->formatslist->getCurrencyDecimalPonts(); ?>" />
<script type="text/javascript" src="<?= base_url() ?>assets/js/product.js"></script>
<script type="text/javascript" src="<?= base_url() ?>assets/js/common_validation.js"></script>
<script type="text/javascript">
    <?php
    if($editRec->DELIVERYDATE){
    ?>
        $("#deliverydate").datepicker({format: 'dd-mm-yyyy', date: '<?php echo $editRec->DELIVERYDATE; ?>'});
    <?php
    }else{
    ?>
        $("#deliverydate").datepicker({format: 'dd-mm-yyyy', setDate: new Date()});
    <?php
    }
    ?>
    $('#deliverydate').datepicker().on('changeDate', function (ev) { 
        var deliverydate = $(this).val();
        SaveInvoice('', deliverydate);
        $('.datepicker').hide();
    });
    $("#reference").change(function () {
        var reference = $(this).val();
        SaveInvoice('', '', reference);
    });
    $("#COMMENTS").change(function () {
        SaveInvoice('', '', reference);
    });
</script>
<script type="text/javascript">
    //  Table row increment in enter key down Event
    var i = <?php echo $cnt + 1; ?>;
    $(document).ready(function () {
        $("#searchable_filter").find("input[class^=form-control]").attr("placeholder", "<?php echo $this->lang->line("SEARCHYOURKEYWORD"); ?>");
    });
    $(document).ready(function() {
        var table = $('#productsdatatablelist').DataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": base_url+"inventory/purchaseinvoice/productlistall",
            "oLanguage": {
                "sEmptyTable":     "<?php echo $this->lang->line("NOTHINGFOUND"); ?>",
                "sInfo":           "<?php echo $this->lang->line("SHOWINGPAGE"); ?> _START_ <?php echo $this->lang->line("TO"); ?> _END_ <?php echo $this->lang->line("OF"); ?> _TOTAL_ <?php echo $this->lang->line("ENTRIES"); ?>",
                "sInfoEmpty":      "<?php echo $this->lang->line("SHOWINGPAGE"); ?> 0 <?php echo $this->lang->line("TO"); ?> 0 <?php echo $this->lang->line("OF"); ?> 0 <?php echo $this->lang->line("ENTRIES"); ?>",
                "sInfoPostFix":    "",
                "sInfoThousands":  ",",
                "sLengthMenu":     "<?php echo $this->lang->line("SHOW"); ?> _MENU_ <?php echo $this->lang->line("ENTRIES"); ?>",
                "sLoadingRecords": "<?php echo $this->lang->line("LOADING"); ?>",
                "sProcessing":     "<?php echo $this->lang->line("PROCESSING"); ?>",
                "sSearch":         "",
                "sZeroRecords":    "<?php echo $this->lang->line("NORECORDSAVAILABLE"); ?>",
                "oPaginate": {
                    "sFirst":    "<?php echo $this->lang->line("FIRST"); ?>",
                    "sLast":     "<?php echo $this->lang->line("LAST"); ?>",
                    "sNext":     "<?php echo $this->lang->line("NEXT"); ?>",
                    "sPrevious": "<?php echo $this->lang->line("PREVIOUS"); ?>"
                }
            },
            "columnDefs" : [ 
                {"targets": [ 5,6 ],"sortable" : false}
            ],
        });
        $('#searchbarcodepi').on( 'keyup', function (e) {
            if(e.which == 13) {
                table.columns(0).search(this.value).draw();
                $('#searchbarcodepi').select();
            }
        });
        //$('#mainTable tbody tr:last').attr('id');
    });
    $(document).on('keyup', '.lst', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) {
            var a = i - 1;
            var previtemcode = $("#itemcode_" + a).val();
            var previtemid = $("#itemid_" + a).val();
            var x = $(this).attr("data-ind");
            if (previtemcode === '') {
                if (a == x) {
                    viewproductlist();
                    $("#productlist").click();
                }
            } else {
                if (previtemid === '') {
                    if (a == x) {
                        viewproductlist();
                        $("#productlist").click();
                    }
                } else {
                    var htmlcontent = incrememttablerow(i);
                    $('#mainTable').append(htmlcontent);
                    $(this).focus().select();
                    i++;
                }
            }
        }
    });
    // Value insertion into table row in enter key press Event
    $(document).on('keydown', '.inputs', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        $('.inputs').removeClass('editfield');
        $(this).addClass('editfield');
        if (e.type == 'keydown') {
            if (code == 13) {
                gridupdate(this);
            }else if (code == 113) {
                var txtindex    =   $(this).attr("data-ind");
                var itemid      =   $("#itemid_"+txtindex).val();
                if(itemid) {
                    $("#productlist").click();
                    myFunction(itemid);
                    $("#name").focus();
                } else {
                    $("#productlist").click();
                }
            }
        }
    });
    // Value insertion into table row Onchage Event
    $(document).on('change', '.inputs', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        $('.inputs').removeClass('editfield');
        $(this).addClass('editfield');
        var curclassname = this.className;
        var arr = curclassname.split(' ');
        var txtindex = $('.editfield').attr("data-ind");
        chkDiscountvalue(txtindex);
        if (arr[1] != 'itemcode') {
            if (arr[1] == 'itemlinetotal')
                LinePriceAmount();
            else
                lineTotalCalculation();
            SaveInvoiceItems(txtindex);
            setFormattext(txtindex);
        }
    });
    // Update Grid value
    function gridupdate(thispointer) {
        var curclassname = thispointer.className;
        var arr = curclassname.split(' ');
        var itemcode = thispointer.value;
        var txtindex = $('.editfield').attr("data-ind");
        if (arr[1] == 'itemcode') {
            if (itemcode == '') {
                clearAll();
                $('.inputs').removeClass('editfield');
            } else {
                $.ajax({
                    method: "GET",
                    url: "<?php echo base_url('inventory/Products/getproductsfromcode/'); ?>",
                    data: {term: itemcode},
                    dataType: "json"
                }).done(function (msg) {
                    var txtindex = $('.editfield').attr("data-ind");
                    if (msg.length == 0) {
                        clearAll();
                        $('.inputs').removeClass('editfield');
                    } else {
                        $.each(msg, function (index, element) {
                            $('#itemid_' + txtindex).val(element.ID);
                            $('#itemcode_' + txtindex).val(element.CODE);
                            $('#item_' + txtindex).val(element.NAME);
                            $('#price_' + txtindex).val(element.PRICEBUY);
                            var taxId   =   getTaxID(element.TAXCAT);
                            $('#taxid_' + txtindex).val(taxId);
                            var tottax  =   setTaxamount(txtindex);
                            $('#taxamountItem_' + txtindex).html(tottax.toFixed(floatpointnum));
                        });
                        var itmId   =   $('#itemid_' + txtindex).val();
                        lineTotalCalculation();
                        SaveInvoiceItems(txtindex, itmId);
                        
                    }
                });
            }
        } else if (arr[1] == 'itemname') {
            var itemid = $('#itemid_' + txtindex).val();
            var itemcode = $('#itemcode_' + txtindex).val();
            var itemname = $('#item_' + txtindex).val();
            if (itemid == '' || itemname == '' || itemcode == '') {
                clearAll();
                viewproductlist();
                $("#productlist").click();
                var oTable = $('#productsdatatable').dataTable({
                    "destroy": true});
                oTable.fnFilter(itemname);
                $("#searchable_filter").parent().css("width", "100%");
            } else {
                // Code here.
            }
        } else if (arr[1] == 'itemqty') {
            lineTotalCalculation();
            SaveInvoiceItems(txtindex);
        } else if (arr[1] == 'itemprice') {
            lineTotalCalculation();
            SaveInvoiceItems(txtindex);
        } else if (arr[1] == 'itemdiscount') {
            chkDiscountvalue(txtindex);
            lineTotalCalculation();
            SaveInvoiceItems(txtindex);
        } else if (arr[1] == 'itemlinetotal') {
            LinePriceAmount();
            SaveInvoiceItems(txtindex);
        }
        setFormattext(txtindex);
        CheckValueError();
        var index = $('.inputs').index(thispointer)+1;
        $('.inputs').eq(index).focus();
        $('.inputs').eq(index).select();
    }
    // Clear All fields in the table.
    function clearAll() {
        var txtindex = $('.editfield').attr("data-ind");
        $("#itemcode_" + txtindex).val('');
        $("#item_" + txtindex).val('');
        $("#itemid_" + txtindex).val('');
        $("#qty_" + txtindex).val('1');
        $("#price_" + txtindex).val('0');
        $("#discount_" + txtindex).val('0');
        $("#linetotal_" + txtindex).val('0');
    }
    // Table row increment function
    function incrememttablerow(i) {
        html = '<tr id="tablerow_' + i + '">';
        html += '<td class="nopaddingtd"><input type="text" class="inputs itemcode" name="itemcode[]" id="itemcode_' + i + '" data-ind="' + i + '" /><input type="hidden" name="itemid[]" id="itemid_' + i + '" value="" /></td>';
        html += '<td class="nopaddingtd"><input type="text" class="inputs itemname" name="item[]" id="item_' + i + '" data-ind="' + i + '" /></td>';
        html += '<td class="nopaddingtd"><input type="number" class="inputs itemqty text-center" name="qty[]" id="qty_' + i + '" data-ind="' + i + '" value="1" /></td>';
        html += '<td class="nopaddingtd"><input type="number" class="inputs itemprice text-right" name="price[]" id="price_' + i + '" data-ind="' + i + '" value="0" /></td>';
        html += '<td class="nopaddingtd" style="display:none;"><input type="text" class="inputs itemdiscount text-right" name="discount[]" id="discount_' + i + '" data-ind="' + i + '" value="0" /></td>';
        html += '<td class="text-right"><span id="taxamountItem_' + i + '" class="taxamountspan"></span><input type="hidden" name="taxid_' + i + '" id="taxid_' + i + '" value="" /></td>';
        html += '<td class="nopaddingtd"><input type="number" class="inputs itemlinetotal lst text-right" name="linetotal[]" id="linetotal_' + i + '" data-ind="' + i + '" value="0" /></td>';
        html += '<td class="text-center" width="50"><a class="btn btn-danger btn-xs delete" onclick="tablerowremove(' + i + ')"><i class="fa fa-trash-o"></i></a></td>';
        html += '</tr>';
        return html;
    }
    // Line total calculation
    function lineTotalCalculation() {
        var txtindex = $('.editfield').attr("data-ind");
        var qty = $('#qty_' + txtindex).val();
//        if it is purchase return.
        <?php
        if($editRec->STATUS == -1){
        ?>
            qty           =   -1*Math.abs(qty);
            $('#qty_' + txtindex).val(qty);
        <?php
        }
        ?>
        var productDiscount = $('#discount_' + txtindex).val();
        productDiscount    =   -1*Math.abs(productDiscount);
        $('#discount_' + txtindex).val(productDiscount);
        var price = $('#price_' + txtindex).val();
        // Tax amount calculation
        var tottax  =   setTaxamount(txtindex);
        $('#taxamountItem_' + txtindex).html(tottax.toFixed(floatpointnum));
        var TAXAMOUNT = $('#taxamountItem_' + txtindex).html();
        var linepriceamount = ((qty * price) + productDiscount)+parseFloat(TAXAMOUNT);
        $('#linetotal_' + txtindex).val(linepriceamount);
        NetTotalsCalculation();
        $('.inputs').removeClass('editfield');
        
    }
    // Net Totals calculation
    function NetTotalsCalculation() {
        var subtotal = 0;
        var discountamount;
        var freightamount;
        var adjustmentamount;
        var nettotal;
        var taxtotal=0;
        discountamount = $('#discounttxt').val();
        freightamount = $('#freighttxt').val();
        adjustmentamount = $('#adjustmenttxt').val();
        chkTotalDiscountvalue();
        if (discountamount == '') {
            $('#discounttxt').val('0');
            discountamount = $('#discounttxt').val();
        }else{
            discountamount = $('#discounttxt').val();
            <?php
            if($editRec->STATUS == 1){
            ?>
                discountamount    =   -1*Math.abs(discountamount);
            <?php
            } else if($editRec->STATUS == -1){
            ?>
                discountamount    =   Math.abs(discountamount);
            <?php
            }
            ?>
            $('#discounttxt').val(discountamount);
        }
        if (freightamount == '') {
            $('#freighttxt').val('0');
            freightamount = $('#freighttxt').val();
        }
        if (adjustmentamount == '') {
            $('#adjustmenttxt').val('0');
            adjustmentamount = $('#adjustmenttxt').val();
        }
        // Net Total
        $(".itemlinetotal").each(function () {
            subtotal = parseFloat(subtotal) + parseFloat($(this).val());
        });
        $("#subtotalspan").html(numberformat(subtotal));
        // Net Tax Total
        
        $(".taxamountspan").each(function () {
            taxtotal = parseFloat(taxtotal) + parseFloat($(this).html());
//            alert(taxtotal);
        });
//        $("#taxtotalspan").html(numberformat(taxtotal));
        viewInvoiceTaxDetails();
        nettotal = numberformat((parseFloat(subtotal) + parseFloat(freightamount) + parseFloat(adjustmentamount)) + parseFloat(discountamount));
        $("#nettotalspan").html(nettotal);
        SaveInvoice();
    }
    // Check All fields in the table.
    function CheckValueError() {
        var txtindex = $('.editfield').attr("data-ind");
        var itemqty = $('#qty_' + txtindex).val();
        var itemprice = $('#price_' + txtindex).val();
        var itemdiscount = $('#discount_' + txtindex).val();
        var itemlinetotal = $('#linetotal_' + txtindex).val();
        if (itemqty == '')
            $('#qty_' + txtindex).val('1');
        if (itemprice == '')
            $('#price_' + txtindex).val('0');
        if (itemdiscount == '')
            $('#discount_' + txtindex).val('0');
        if (itemlinetotal == '')
            $('#linetotal_' + txtindex).val('0');

    }

    // Line price amount calculation
    function LinePriceAmount() {
        CheckValueError();
        var txtindex = $('.editfield').attr("data-ind");
        var qty = parseFloat($('#qty_' + txtindex).val());
        <?php
        if($editRec->STATUS == -1){
        ?>
            qty           =   -1*Math.abs(qty);
            $('#qty_' + txtindex).val(qty);
        <?php
        }
        ?>
//        var itemdiscount = $('#discount_' + txtindex).val();
//        itemdiscount    =   -1*Math.abs(itemdiscount);
//        $('#discount_' + txtindex).val(itemdiscount);
        var itemlinetotal = parseFloat($('#linetotal_' + txtindex).val());
        var taxrate         =   parseFloat(getTaxrate(txtindex))+1;
        //var price = (parseFloat(itemlinetotal) - parseFloat(itemdiscount) - parseFloat(itemtax)) / parseFloat(qty);
        var price = (itemlinetotal/qty)/taxrate;
        $('#price_' + txtindex).val(price);
        //var itemtax = $('#taxamountItem_' + txtindex).html();
        // Tax amount calculation
        var tottax  =   setTaxamount(txtindex);
        $('#taxamountItem_' + txtindex).html(tottax.toFixed(floatpointnum));
//        var TAXAMOUNT = $('#taxamountItem_' + txtindex).html();
        $('.inputs').removeClass('editfield');
        NetTotalsCalculation();
    }

    // Select Item from the browse List.
    function selectItemgrid(itemId, itemCode, itemName, itemPrice,itemTaxcatId) {
        var txtindex = $('.editfield').attr("data-ind");
        $("#itemcode_" + txtindex).val(itemCode);
        $("#item_" + txtindex).val(itemName);
        $("#itemid_" + txtindex).val(itemId);
        $("#price_" + txtindex).val(itemPrice);
        var taxId   =  getTaxID(itemTaxcatId);
        $("#taxid_" + txtindex).val(taxId);
        var taxamount   =  setTaxamount(txtindex);
        $("#taxamountItem_" + txtindex).val(taxamount.toFixed(floatpointnum));
        lineTotalCalculation();
        SaveInvoiceItems(txtindex, itemId);
        setFormattext(txtindex);
        $('#myModal').modal('hide');
        $("#qty_" + txtindex).focus();
        $("#qty_" + txtindex).select();
        newLineInsert(txtindex);
    }

    // Delete table row
    function tablerowremove(rowid = '', recId = '') {
        if (rowid) {
            if (recId) {
                //
            } else {
                recId = $('#tablerow_' + rowid + '').attr("data-invitemid");
            }
            $('#tablerow_' + rowid + '').remove();
            var invoiceId   =   $("#pinvid").val();
            $.ajax({
                method: "GET",
                url: "<?php echo base_url('inventory/purchaseinvoice/deletepurchaseinvoicelinesrec/'); ?>",
                data: {
                    id: recId,
                    PIID:invoiceId,
                },
                dataType: "json"
            }).done(function (msg) {
                viewInvoiceTaxDetails();
            });
        }
        
        NetTotalsCalculation();
        var tbody = $("#mainTable tbody");
        if (tbody.children().length == 0) {
            var htmlcontent = incrememttablerow(1);
            $('#mainTable').append(htmlcontent);
    }
    }

    // Save Invoice Details
    function SaveInvoice(supplierId = '', deliverydate = '', reference = '') {
        var discounttxt;
        var freighttxt;
        var adjustmenttxt;
        var nettotalspan;
        var supplierId;
        var deliverydate;
        var reference;
        var COMMENTS;
        supplierId      = $("#SUPPLIERID").val();
        COMMENTS      = $("#COMMENTS").val();
        deliverydate    = $("#deliverydate").val();
        reference       = $("#reference").val();
        discounttxt     = parseFloat($("#discounttxt").val());
        freighttxt      = parseFloat($("#freighttxt").val());
        adjustmenttxt   = parseFloat($("#adjustmenttxt").val());
        nettotalspan    = parseFloat($("#nettotalspan").html());
        var invoiceId   =   $("#pinvid").val();
        $.ajax({
            method: "GET",
            url: "<?php echo base_url('inventory/purchaseinvoice/purchaseinvoiceupdate/'); ?>",
            data: {
                id:invoiceId,
                SUPPLIERID: supplierId,
                DELIVERYDATE: deliverydate,
                COMMENTS: COMMENTS,
                REFERENCE: reference,
                DISCOUNT: discounttxt,
                FREIGHTAMOUNT: freighttxt,
                ADJUSTMENT: adjustmenttxt,
                TOTALAMOUNT: nettotalspan
            },
            dataType: "json"
        }).done(function (msg) {
            if (msg.status == 'success') {
                //
            } else {
                //
            }
        });
    }

    // Save Invoice item Details
    function SaveInvoiceItems(txtindex = '', PRODUCT = '') {
        var qty = $('#qty_' + txtindex).val();
        var itemdiscount = $('#discount_' + txtindex).val();
        var price = $('#price_' + txtindex).val();
        var TAXID = $('#taxid_' + txtindex).val();
        var invitemid = $("#itemcode_" + txtindex).closest('tr').attr('data-invitemid');
        if (PRODUCT == '') {
            var PRODUCT = $('#itemid_' + txtindex).val();
        }
        var invoiceId   =   $("#pinvid").val();
        if (PRODUCT) {
            if (txtindex) {
                $.ajax({
                    method: "GET",
                    url: "<?php echo base_url('inventory/purchaseinvoice/managepurchaseinvlines/'); ?>",
                    data: {
                        txtindex: txtindex,
                        id: invitemid,
                        PIID:invoiceId,
                        PRODUCTID: PRODUCT,
                        QUANTITY: qty,
                        //UOM:UOM,
                        UNITPRICE: price,
                        TAXID:TAXID,
                        DISCOUNT: itemdiscount
                    },
                    dataType: "json"
                }).done(function (msg) {
                    var chkinvitemid    =   $("#itemcode_" + msg.txtindex).closest('tr').attr('data-invitemid');
                    if(!chkinvitemid)
                        $("#itemcode_" + msg.txtindex).closest('tr').attr('data-invitemid', msg.id);
                    viewInvoiceTaxDetails();
                });
            }
        }
        newLineInsert(txtindex);
    }

    function newLineInsert(txtindex){
        var b  =	parseInt(txtindex)+1;
        if($("#mainTable #tablerow_"+b).length > 0){
            //
        } else {
            var itemid  =   $("#itemid_"+b).val();
            if(itemid != 'undefined' || itemid != null || itemid !=''){
                var curitemid  =   $("#itemid_"+txtindex).val();
                if(curitemid != ''){
                    var htmlcontent = incrememttablerow(b);
                    $('#mainTable').append(htmlcontent);
                }
            }
        }
    }

    // Submit purchase Invoice
    $("#submit").click(function () {
        
    });

    function submitInvoiceFunction(){
        $(this).hide();
        var invoiceId   =   $("#pinvid").val();
        var rowid = invoiceId;
        if (rowid != 0) {
            var selstatus = <?php echo $editRec->STATUS; ?>;
            if (selstatus == 1 || selstatus == -1) {
                document.location.href = "<?php echo base_url('inventory/purchaseinvoice/submitpi/'); ?>" + rowid;
            } else {
                alert('');
            }
        }
    }


    // Print PI
    $("#printpi").click(function () {
        var rowid = $("#pinvid").val();
        document.location.href = "<?php echo base_url('inventory/purchaseinvoice/printpi/'); ?>" + rowid;
    });

    // Print PO
    $("#printpo").click(function () {
        var rowid = $("#pinvid").val();
        document.location.href = "<?php echo base_url('inventory/purchaseinvoice/printpo/'); ?>" + rowid;
    });

    // Delete Purchase Invoice
    function deletePurchaseInvoiceFunction() {
        var rowid = $("#pinvid").val();
        if (rowid != 0) {
            var selstatus = <?php echo $editRec->STATUS; ?>;
            if (selstatus == 1 || selstatus == -1) {
                document.location.href = "<?php echo base_url('inventory/purchaseinvoice/deletepurchaseinvoicecontroller/'); ?>" + rowid;
            } else {
                alert('<?php echo $this->lang->line("CANOTDELETESUBMITTEDPI"); ?>');
            }
        }
    }

    // Format Number
    function numberformat(num) {
        if (num) {
            return num.toFixed(floatpointnum);
        } else {
            num = 0;
            return num.toFixed(floatpointnum);
        }
    }
    $(document).on('click', '.itemname', function (e) {
        $('.inputs').removeClass('editfield');
        $(this).addClass('editfield');
        viewproductlist();
        $("#productlist").click();
        
    });
    // Set tax amount of the product
    function setTaxamount(txtindex=''){
        var obj = <?php echo json_encode($alltaxes); ?>;
        var taxcat =   $('#taxid_' + txtindex).val();
        for (var i = 0; i < obj.length; i++){
            if (obj[i].ID == taxcat){
                var taxrate    =   obj[i].RATE;
            }
        }
        var qty =   $('#qty_' + txtindex).val();
        var priceVal =   $('#price_' + txtindex).val();
        var taxamount =   (priceVal*(taxrate/100))*qty;
        return taxamount;
    }
    function getTaxID(taxcatID=''){
        var obj = <?php echo json_encode($alltaxes); ?>;
        for (var i = 0; i < obj.length; i++){
            if (obj[i].CATID == taxcatID){
                var taxID    =   obj[i].ID;
            }
        }
        return taxID;
    }
    function getTaxrate(txtindex=''){
        var taxcat =   $('#taxid_' + txtindex).val();
        var obj = <?php echo json_encode($alltaxes); ?>;
        for (var i = 0; i < obj.length; i++){
            if (obj[i].ID == taxcat){
                var taxrate    =   obj[i].RATE;
            }
        }
        taxrate =   taxrate/100;
        return taxrate;
    }
    
    // Check discount vlaue whether it is percentage or fixed.
    function chkDiscountvalue(txtindex){
        var discount        =   $('#discount_' + txtindex).val();
        if (discount.indexOf('%') > -1){
            discount        =   parseFloat(discount.replace('%', ''));
            var qty         =   parseFloat($('#qty_' + txtindex).val());
            var price       =   parseFloat($('#price_' + txtindex).val());
            var linetotal       =   (qty*price);
            var txtdiscount     =   (linetotal*discount)/100;
            $('#discount_' + txtindex).val(numberformat(txtdiscount));
        }
    }
    
    // Check Total Discount vlaue whether it is percentage or fixed.
    function chkTotalDiscountvalue(){
        var discount        =   $('#discounttxt').val();
        if (discount.indexOf('%') > -1){
            discount        =   parseFloat(discount.replace('%', ''));
            var subtotalspan         =   parseFloat($('#subtotalspan').html());
            var txtdiscount     =   numberformat((subtotalspan*discount)/100);
            $('#discounttxt').val(txtdiscount);
        }
    }
    // show invoice tax details
    function viewInvoiceTaxDetails(){
        var pinvid = $("#pinvid").val();
        $.ajax({
            method: "GET",
            url: "<?php echo base_url('inventory/purchaseinvoice/purchaseInvoiceTax/'); ?>",
            data: {
                PIID:pinvid,
            }
        }).done(function (msg1) {
            $("#taxtotalspan").html(msg1);
        });
    }
    // create product
    function createproduct(){
        $("#productlistselect").hide();
        $("#createproductform").show();
        $("#buttoncreateproduct").hide();
        $("#form1")[0].reset();
        $("#id").val('');
        $("#searchbarcodepi").hide();
    }
    // view product list
    function viewproductlist(){
        $("#productlistselect").show();
        $("#createproductform").hide();
        $("#buttoncreateproduct").show();
        $("#searchbarcodepi").show();
    }
    
    // Purchase Return
    $("#purchasereturnbtn").click(function () {
        var invoiceId   =   $("#pinvid").val();
        $.ajax({
            method: "GET",
            url: "<?php echo base_url('inventory/purchaseinvoice/purchasereturn/'); ?>",
            data: { invoiceId:invoiceId }
        }).done(function (msg1) {
            $("#purchasereturnId").val(msg1);
            $("#purchasereturnbtn").hide();
            $("#copypurchasebtn").hide();
            $("#undosubmitbtn").hide();
            $(".returnqtytd").show();
            $(".returnqtylabeltd").show();
            $('.footerpurchaseinvtable').attr('colspan',5);
        });
    });
    
    // Copy Invoice Bill
    $("#copypurchasebtn").click(function () {
        $('.loading-container').removeClass("loading-inactive");
        var invoiceId   =   $("#pinvid").val();
        $.ajax({
            method: "GET",
            url: "<?php echo base_url('inventory/purchaseinvoice/copyinvoice/'); ?>",
            data: { invoiceId:invoiceId }
        }).done(function (msg1) {
            $('.loading-container').addClass("loading-inactive");
            $("#copymodalbtn").click();
        });
    });   
    
    
    /**Purchase return invoice Item**/
    function updatepreturnitem(txtindex='',prId='',prqty='',prPrice='',prtaxid=''){
        var PIID    =   $("#purchasereturnId").val();
        var invitemid   =   $("#retqtytxt_"+txtindex).data("invid");
        prqty           =   -1*Math.abs(prqty);
        if (txtindex) {
            $.ajax({
                method: "GET",
                url: "<?php echo base_url('inventory/purchaseinvoice/managepurchaseinvlines/'); ?>",
                data: {
                    id: invitemid,
                    PIID:PIID,
                    PRODUCTID: prId,
                    QUANTITY: prqty,
                    UNITPRICE: prPrice,
                    TAXID:prtaxid,
                    txtindex:txtindex
                },
                dataType: "json"
            }).done(function (msg) {
                $("#retqtytxt_"+msg.txtindex).data('invid',msg.id);
            });
        }
    }
    
    
    /**Copy Purchase Invoice Item**/
    function updatecopypiitem(txtindex='',prId='',prqty='',prPrice='',prtaxid=''){
        var PIID    =   $("#pinvid").val();
        var invitemid   =   $("#copyqtytxt_"+txtindex).data("invid");
        prqty           =   prqty;
        if (txtindex) {
            $.ajax({
                method: "GET",
                url: "<?php echo base_url('inventory/purchaseinvoice/managepurchaseinvlines/'); ?>",
                data: {
                    id: invitemid,
                    PIID:PIID,
                    PRODUCTID: prId,
                    QUANTITY: prqty,
                    UNITPRICE: prPrice,
                    TAXID:prtaxid,
                    txtindex:txtindex
                },
                dataType: "json"
            }).done(function (msg) {
                $("#copyqtytxt_"+msg.txtindex).data('invid',msg.id);
            });
        }
    }
    
    
    function setFormattext(txtindex){
        $("#price_"+txtindex).val(function() {
            return (+this.value).toFixed(floatpointnum);
        });
        var taxamount   =   parseFloat($("#taxamountItem_"+txtindex).html());
        $("#taxamountItem_"+txtindex).html(taxamount.toFixed(floatpointnum));
        
        $("#linetotal_"+txtindex).val(function() {
            return (+this.value).toFixed(floatpointnum);
        });
    }
    
//    function submitundoInvoiceFunction() {
//        window.location.href = "<?php echo base_url()."inventory/purchaseinvoice/undoinvoice/".$invoiceId; ?>";
//    }
    
    $(document).ajaxStop(function () {
        $('#loading').hide();
    });

    $(document).ajaxStart(function () {
        $('#loading').show();
    }); 

    $(document).ready(function() {
        var table = $('#leadsupplierdatatable').DataTable( {
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": base_url+"inventory/purchaseinvoice/leadsupplierlistall",
            "oLanguage": {
                "sEmptyTable":     "<?php echo $this->lang->line("NOTHINGFOUND"); ?>",
                "sInfo":           "<?php echo $this->lang->line("SHOWINGPAGE"); ?> _START_ <?php echo $this->lang->line("TO"); ?> _END_ <?php echo $this->lang->line("OF"); ?> _TOTAL_ <?php echo $this->lang->line("ENTRIES"); ?>",
                "sInfoEmpty":      "<?php echo $this->lang->line("SHOWINGPAGE"); ?> 0 <?php echo $this->lang->line("TO"); ?> 0 <?php echo $this->lang->line("OF"); ?> 0 <?php echo $this->lang->line("ENTRIES"); ?>",
                "sInfoPostFix":    "",
                "sInfoThousands":  ",",
                "sLengthMenu":     "<?php echo $this->lang->line("SHOW"); ?> _MENU_ <?php echo $this->lang->line("ENTRIES"); ?>",
                "sLoadingRecords": "<?php echo $this->lang->line("LOADING"); ?>",
                "sProcessing":     "<?php echo $this->lang->line("PROCESSING"); ?>",
                "sSearch":         "",
                "sZeroRecords":    "<?php echo $this->lang->line("NORECORDSAVAILABLE"); ?>",
                "oPaginate": {
                    "sFirst":    "<?php echo $this->lang->line("FIRST"); ?>",
                    "sLast":     "<?php echo $this->lang->line("LAST"); ?>",
                    "sNext":     "<?php echo $this->lang->line("NEXT"); ?>",
                    "sPrevious": "<?php echo $this->lang->line("PREVIOUS"); ?>"
                }
            },
            "columnDefs" : [ 
            ],
        });
    });

    function setsupplierdetails(NAME='',ADDRESS='',CITY='',REGION='',COUNTRY='',POSTAL='',PHONE='',EMAIL='',ID='',TAXID='') {
        $("#supplieraddress").html("");
        $("#vendorautocomp").val(NAME+" - "+TAXID);
        if (ADDRESS) {
            $("#supplieraddress").append("<li>" +ADDRESS+ "</li>");
        }
        if (CITY) {
            $("#supplieraddress").append("<li>" +CITY+ "</li>");
        }
        if (REGION) {
            $("#supplieraddress").append("<li>" +REGION+ "</li>");
        }
        if (COUNTRY) {
            $("#supplieraddress").append("<li>" +COUNTRY+ "</li>");
        }
        if (POSTAL) {
            $("#supplieraddress").append("<li>" +POSTAL+ "</li>");
        }
        if (PHONE) {
            $("#supplieraddress").append("<li>" +PHONE+ "</li>");
        }
        if (EMAIL) {
            $("#supplieraddress").append("<li>" +EMAIL+ "</li>");
        }
        $("#SUPPLIERID").val(ID);
        SaveInvoice(ID);
        $('#MYMODALLEADCUSTOMER').modal('hide');
    }

</script>

