<?php
if ($IsEdit && $check_lock != '') { ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong>Danger: </strong><?php echo $check_lock ?>
    </div>
    <?php
    return;
}
if ($IsEdit && empty($edit_arr['main']['invoice_id'])) {
    ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong>Danger: </strong>you can not edit this station invoice! 
        <br>
        This station invoice is not available.
    </div>
    <?php
    die();
}
?>
<style type="text/css">
    #InventoryPurchaseTable .TotalTD{
        width: auto;
        display: inline-block;
        text-align: center;
        font-size: 20px;
        font-weight: bold;
    }
    .wrong{
        border: 1px solid rgba(255, 0, 0, 0.2);
        background: rgba(255, 0, 0, 0.05);
    }
    .DropDownForForms > .dropdown-menu{
        width:100%;
        height:200px;
        max-height:200px;
        overflow-y:scroll;
    }
    .DropDownForForms > .dropdown-menu{
        height:auto;
        cursor: pointer;
        overflow-y:auto;
    }
    .DropDownForForms > button{
        position:relative;
        z-index:999;
    }
    .DropDownForForms > button > input{
        position: absolute;
        left:5;
        top: 0;
        width:90%;
        cursor: pointer;
        -webkit-user-select:none;
        -moz-user-select:none;
        user-select:none;
        padding: 0;
        border: 0;
        background: transparent;
    }


    .datepicker thead tr .dow {
        color:#4c0870!important;
    }
    .datepicker table tr td.day:hover {
        background: #4c0870!important;
        color:#fff;
    }
    .datepicker table tr td.active {
        background-color: #4c0870!important;
color:#ed9421 !important;    }
    .datepicker thead tr .datepicker-switch {
        color: #4c0870 !important;
        font-size: 13px;
    }
    .datepicker table tr td span.active {
        background-color: #4c0870!important;
color:#ed9421 !important;    }
    .datepicker table tr td span:hover {
        background-color: #4c0870!important;
color:#ed9421 !important;        color:#fff;
    }
    datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #4c0870!important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    .datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #4c0870!important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    #InventoryPurchaseTable .select2-container .select2-choice {
        background-image: none;
        border-radius: 2px;
        border: 1px solid rgba(0,0,0,0.07);
        padding: 5px 9px 16px;
        transition: border 0.2s linear 0s;
        height: 40px;
        background-color: #e5e5e5;
        width: 150px;
    }
    #InvoicePaymentSetup .nav.nav-tabs a {
        padding: 7px 14px;
        font-size: 11px;
    }
    .InputSpinner{
        width: 60px;
    }
    .InputSpinner > .form-control{
        border-color: rgb(221, 221, 221);
        height:36px;
        margin:0;
        padding:0;
        text-align: center;
    }
    .InputSpinner > .input-group-btn-vertical {
        position: relative;
        white-space: nowrap;
        width: 25px;
        vertical-align: middle;
        display: table-cell;
    }
    .InputSpinner > .input-group-btn-vertical > .btn {
        height:19px;
        display: block;
        float: none;
        width: 100%;
        max-width: 100%;
        padding: 8px;
        margin-left: -1px;
        position: relative;
        border-radius: 0;
    }
    .InputSpinner > .input-group-btn-vertical > .btn:first-child {
        border-top-right-radius: 4px;
    }
    .InputSpinner > .input-group-btn-vertical > .btn:last-child {
        margin-top: -2px;
        border-bottom-right-radius: 4px;
    }
    .InputSpinner > .input-group-btn-vertical i{
        position: absolute;
        top: 50%;
        left: 6px;
        margin-top:-9px;
        font-size: 18px;
    }
    .InputSpinner .input-group-btn-vertical .btn.btn-default{
        font-size: 10px;
        padding: 6px 10px;
    }
    #InvoicePaymentSetup .table tbody > tr:last-child > td{
        border:0;
    }

    .select2-container > .select2-choice {
        width: 100% !important;
    }

    .invoice{
        padding-top:0 !important;
    }

    .right-div .form-group{
        margin-bottom:0px;
    }
    .AddRow{
        background: #e5e5e5;
        padding: 10px;
        border-radius: 6px;
        margin-bottom: 10px;
    }
    .title-color{
        color:#eb9220;
    }
</style>
<div id="PurchaseDisabler" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:2099;display:none"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>
<form method="POST" action="javascript:void(0);" onsubmit="return false" id="InvoiceForm">
    <div class="container-fluid container-fixed-lg">
        <div class="panel panel-default">
            <div class="panel-body">
                <div id = "PurchaseSuccess" class = "hidden"></div>
                <div class = "invoice ">
                    <div class = "container-sm-height">
                        <div class = "row-sm-height">
                            <div class = "col-md-9 col-sm-height sm-no-padding">
                                <div class = "row " style = "display:flex;">
                                    <div class = "col-md-6">
                                        <h2 class = "font-montserrat all-caps hint-text"><?php echo CommonLanguage::getMessage('station_invoice');
?> </h2>
                                    </div>
                                </div>
                                <div class='row form-group'>
                                    <div class="input-group  col-md-4 center-margin pull-left " >
                                        <label><?php print CommonLanguage::getMessage('choose_customer'); ?></label><span class="fa fa-plus-circle m-l-5 openFrame" data-src="Inventory/CustomerList?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input class="search_customer full-width" id="search_customer" name="search_customer" type="hidden" value="<?= $IsEdit ? $edit_arr['main']['customer_id'] : ''; ?>" data-select-name='<?= json_encode(($IsEdit) ? $edit_arr['main']['Customer_Name'] : ""); ?>' required />
                                    </div>
                                    <div class="Account_Code col-md-4">
                                        <label><?php print CommonLanguage::getMessage('accounts'); ?></label><span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input class=" full-width" id="Account_Code" name="Account_Code" type="hidden" value="<?= $IsEdit ? $edit_arr['main']['Account_ID'] : smc_session()['user_accounts']['sales_account_id']; ?>" data-select-name='<?= json_encode($IsEdit ? ($edit_arr['main']['Account_Name'] . ' - ' . $edit_arr['main']['Account_Code']) : smc_session()['user_accounts']['s_name']); ?>' required />
                                    </div>
                                    <div class="clearfix"></div>
                                </div>
                                <br>

                            </div>
                            <div class="right-div col-md-4 col-sm-height col-bottom sm-no-padding sm-p-b-20" style="vertical-align: top;">


                                <div  style="display: flex">
                                    <div  class="form-group " style="width: 48%;margin-right:2%;">
                                        <label><?php print CommonLanguage::getMessage('Date'); ?></label>
                                        <input id="invoice_date" name="invoice_date" class="form-control" value="<?php echo ($IsEdit ? $edit_arr['main']['invoice_date'] : $today); ?>" required />
                                    </div>

                                </div>
                                <div  id="depot" class="form-group " style="display: flex;">
                                    <div style="width: 48%;margin-right:2%;" >
                                        <label><?php print CommonLanguage::getMessage('Depot'); ?></label>
                                        <span class="fa fa-plus-circle m-l-5 openFrame" data-src="Stock/ListDepot?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span>
                                        <input required id="Depot_Loc" name="Depot_Loc" class="full-width Depot_Loc" type="hidden" <?php echo $permissions->hasPermission('change_depot') ? '' : 'disabled' ?> data-old="<?php echo $IsEdit ? $edit_arr['main']['depot_id'] : ''; ?>" value="<?= $IsEdit ? $edit_arr['main']['depot_id'] : (smc_session()['user_accounts']['depot_id'] ? smc_session()['user_accounts']['depot_id'] : smc_session()['default_depot_id']) ?>" data-select-name='<?= json_encode($IsEdit ? $all_depots[$edit_arr['main']['depot_id']]['Depot_Name'] : (smc_session()['user_accounts']['Depot_Name'] ? smc_session()['user_accounts']['Depot_Name'] : smc_session()['default_depot_name'])) ?>' />
                                    </div>
                                    <div style="width: 50%;" >
                                        <div  class="form-group " >
                                            <label><?php print CommonLanguage::getMessage('exchange_rate'); ?></label>
                                            <input type="number" step="any" min="0" id="current_rate" name="current_rate" class="form-control full-width current_rate" <?php echo ($DefaultCurrencyID == smc_session()['local_currency']) ? 'disabled' : '' ?>  value="<?php echo ($IsEdit ) ? $edit_arr['main']['current_rate'] : '' ?>" />
                                        </div>
                                    </div>
                                </div>

                                <div id="CurrencyIDContainer" class=" m-t-10" >
                                    <div class="form-group form-material floating ">
                                        <div class="input-group-btn DropDownForForms Closable" id="CurrencyDropDown" data-symbol="<?php echo $IsEdit ? $edit_arr['main']['Currency_Symbol'] : ''; ?>">
                                            <button type="button" class="btn btn-primary btn-block dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                                <span><strong><?php echo $IsEdit ? ($edit_arr['main']['Currency_Name'] . ' - ' . $edit_arr['main']['Currency_Symbol']) : smc_session()['local_currency_name'] . ' - ' . smc_session()['local_currency_code'] ?></strong></span>
                                                <input type="hidden"  value="<?php echo $IsEdit ? $edit_arr['main']['currency_id'] : smc_session()['local_currency'] ?>" class="form-control ignore" onkeyup="return false" onkeydown="return false" name="Currency_ID" id="Currency_ID" required/>
                                            </button>
                                            <ul class="dropdown-menu">
                                                <?php echo $CurList ?>
                                                <li class="divider"></li>
                                                <li><a class="LoadMore">More..</a></li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                                <div   class="form-group " style="display: flex;">
                                    <!--<div class="col-md-12">-->
                                    <div style="width: 48%;margin-right:2%;" >
                                        <label><?php echo CommonLanguage::getMessage('job') ?></label>
                                        <input type="hidden" class="full-width" id="job_id" value="<?php echo $IsEdit ? $edit_arr['main']['job_id'] : '' ?>" data-select-name="<?= $IsEdit ? $edit_arr['main']['job_name'] : '' ?>"/>
                                    </div>
                                    <div   class="form-group " style="width:50%">
                                        <label><?php print CommonLanguage::getMessage('employee'); ?></label>
                                        <input  id="PIC_ID" name="PIC_ID" class="full-width PIC_ID" type="hidden" value="<?= ($IsEdit && !empty($edit_arr['main']['employee_id'])) ? $edit_arr['main']['employee_id'] : '' ?>" data-select-name='<?= json_encode(($IsEdit && !empty($edit_arr['main']['Employee_Name'])) ? $edit_arr['main']['Employee_Name'] : '') ?>' />
                                    </div>
                                </div>
                                <br>
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="clearfix"></div>
                    </div>
                    <h3 class="bold title-color"><?php echo CommonLanguage::getMessage('pumps') ?></h3>
                    <div class="col-md-12 AddRow m-b-10 form-group pump-div"  >
                        <div class="col-md-12  m-b-10 form-group"  >
                            <div class="row">
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('pump') ?></label></div>
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('old_counter') ?></label></div>
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('new_counter') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('diff') ?></label></div>
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('unit_price') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('vat') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('vat_percent') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('total') ?></label></div>
                            </div>
                        </div>
                        <?php
                        foreach ($all_pumps as $pump) {
                            //on edit we get all pumps so if it is disabled and not exist in edit_arr don't load it
                            if ($IsEdit && !isset($edit_arr['pump'][$pump['pump_id']]) && $pump['Status_ID'] == 0)
                                continue;
                            ?>
                            <div class="col-md-12 pump-row  "  >
                                <div class="row">
                                    <div class="col-md-2"><span class="semi-bold title-color pum_label" style="margin-top:0;" data-id="<?php echo $pump['pump_id'] ?>" data-uom="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['uom_id'] : $pump['UOM_ID'] ?>" data-prod="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['product_id'] : $pump['Product_ID'] ?>" data-is-service="<?php echo $pump['is_service'] ?>"><?php echo $pump['pump_name'] . ' ( ' . ( ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? ($edit_arr['pump'][$pump['pump_id']]['Product_Name'] . ' - ' . $edit_arr['pump'][$pump['pump_id']]['UOM_Name']) : ($pump['Product_Name'] . ' - ' . $pump['UOM_Name'])) . ' )' ?></span></div>
                                    <div class="col-md-2"><input type="number" class="form-control old_counter" <?php echo $pump['old_counter'] == 0 ? '' : 'readonly' ?> min="0" step="any" value="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['old_counter'] : $pump['old_counter'] ?>"/></div>
                                    <div class="col-md-2"><input type="number" class="form-control new_counter" min="0" step="any" value="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['new_counter'] : '' ?>"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control diff" readonly min="0" step="any" value="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['counter_diff'] : '' ?>"/></div>
                                    <div class="col-md-2"><input type="number" class="form-control pump_unit_price" min="0" step="any" value="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['unit_price'] : $pump['price'] ?>"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control pump_vat" min="0" step="any" value="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['vat'] : '' ?>"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control pump_vat_percent" min="0" step="any" value="<?php echo ($IsEdit && isset($edit_arr['pump'][$pump['pump_id']])) ? $edit_arr['pump'][$pump['pump_id']]['vat_percent'] : ($pump['Product_VAT'] == 1 ? $pump['vat_percent'] : '') ?>"/></div>
                                    <div class="col-md-1"><h5 class="total_div"><strong><span class="pump-total" pump-total="0">0</span>&nbsp;<span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></strong></h5></div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                    <h3 class="title-color bold"><?php echo CommonLanguage::getMessage('bone') ?></h3>

                    <div class="col-md-12 AddRow m-b-10 form-group bone-div"  >
                        <div class="col-md-12  m-b-10 form-group"  >
                            <div class="row">
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('bone_type') ?></label></div>
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('product') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('liters') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('service') ?></label></div>
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('unit_price') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('vat') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('vat_percent') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('total') ?></label></div> </div>
                        </div>
                        <?php
                        if ($IsEdit && !empty($edit_arr['coupon'])) {
                            foreach ($edit_arr['coupon'] as $coupon) {
                                ?>
                                <div class="col-md-12 bone-row m-b-10 " data-is-service="<?php echo $coupon['is_service'] ?>" >
                                    <div class="row">
                                        <div class="col-md-2"><input type="hidden" class="bone_type_id full-width" value="<?php echo $coupon['bon_type_id'] ?>" data-select-name="<?= $coupon['bone_type_name'] ?>"/></div>
                                        <div class="col-md-2"><input type="hidden" class="bone_product_id full-width" value="<?php echo $coupon['product_id'] ?>" data-select-name="<?= ($coupon['Product_Name'] . ' - ' . $coupon['Product_Code']) ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control liters" min="0" step="any" value="<?php echo $coupon['current_quantity'] ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control service" min="0" step="any" value="<?php echo $coupon['service_price'] ?>" disabled/></div>
                                        <div class="col-md-2"><input type="number" class="form-control bone_unit_price" min="0" step="any" value="<?php echo $coupon['unit_price'] ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control bone_vat" min="0" step="any" value="<?php echo $coupon['vat_amount'] ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control bone_vat_percent" min="0" step="any" value="<?php echo $coupon['vat_percent'] ?>"/></div>
                                        <div class="col-md-1"><input type="number"  class="form-control bone-total" value="<?php echo $coupon['total_price'] ?>" bone-total="<?php echo $coupon['total_price'] ?>" min="0" step="any"/></div>
                                        <div class="col-md-1">
                                            <span class="fa fa-plus-circle m-l-5 " style="color:#8a7dbe;cursor:pointer"></span>
                                            <span class="fa fa-minus-circle m-l-5 " style="color:#ed9421;cursor:pointer"></span>
                                        </div>
                                    </div>
                                </div>
                                <?php
                            }
                        } else {
                            ?>
                            <div class="col-md-12 bone-row m-b-10 "  >
                                <div class="row">
                                    <div class="col-md-2"><input type="hidden" class="bone_type_id full-width"/></div>
                                    <div class="col-md-2"><input type="hidden" class="bone_product_id full-width"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control liters" min="0" step="any"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control service" min="0" step="any" disabled/></div>
                                    <div class="col-md-2"><input type="number" class="form-control bone_unit_price" min="0" step="any"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control bone_vat" min="0" step="any"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control bone_vat_percent" min="0" step="any"/></div>
                                    <div class="col-md-1"><input type="number"  class="form-control bone-total" bone-total="0" min="0" step="any"/></div>
                                    <div class="col-md-1">
                                        <span class="fa fa-plus-circle m-l-5 " style="color:#8a7dbe;cursor:pointer"></span>
                                        <span class="fa fa-minus-circle m-l-5 " style="color:#ed9421;cursor:pointer"></span>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                    <h3 class="title-color bold"><?php echo CommonLanguage::getMessage('products') ?></h3>

                    <div class="col-md-12 AddRow m-b-10 form-group prod-div"  >
                        <div class="col-md-12  m-b-10 form-group"  >
                            <div class="row">
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('product') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('qty') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('UOM') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('unit_price') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('disc') ?></label></div>
                                <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('description') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('vat') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('vat_percent') ?></label></div>
                                <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('total') ?></label></div> </div>
                        </div>
                        <?php
                        if ($IsEdit && !empty($edit_arr['products'])) {
                            foreach ($edit_arr['products'] as $prod) {
                                ?>
                                <div class="col-md-12 prod-row  "  data-is-service="<?php echo $prod['is_service'] ?>">
                                    <div class="row">
                                        <div class="col-md-2"><input type="hidden" class="inv_product_id full-width" value="<?php echo $prod['product_id'] ?>" data-select-name="<?= ($prod['Product_Name'] . ' - ' . $prod['Product_Code']) ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control prod_qty" min="0" step="any" value="<?php echo $prod['current_quantity'] ?>"/></div>                                
                                        <div class="col-md-1"><input type="hidden" class="uom_id full-width" value="<?php echo $prod['uom_id'] ?>" data-select-name="<?= $prod['UOM_Name'] ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control prod_unit_price full-width"  min="0" step="any" value="<?php echo $prod['unit_price'] ?>"/></div>

                                        <div class="col-md-1"><input type="number" class="form-control prod_disc" min="0" step="any" value="<?php echo $prod['discount_percent'] ?>"/></div>
                                        <div class="col-md-2"><input type="text" class="form-control prod_desc" value="<?php echo $prod['description'] ?>" /></div>
                                        <div class="col-md-1"><input type="number" class="form-control prod_vat" min="0" step="any" value="<?php echo $prod['vat_amount'] ?>"/></div>
                                        <div class="col-md-1"><input type="number" class="form-control prod_vat_percent" min="0" step="any" value="<?php echo $prod['vat_percent'] ?>"/></div>
                                        <div class="col-md-1"><h5 class="total_div"><strong><span class="prod-total" prod-total="0">0</span>&nbsp;<span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></strong></h5></div>
                                        <div class="col-md-1">
                                            <span class="fa fa-plus-circle m-l-5 " style="color:#8a7dbe;cursor:pointer"></span>
                                            <span class="fa fa-minus-circle m-l-5 " style="color:#ed9421;cursor:pointer"></span>
                                        </div>
                                    </div>
                                </div>
                                <?php
                            }
                        } else {
                            ?>
                            <div class="col-md-12 prod-row  "  >
                                <div class="row">
                                    <div class="col-md-2"><input type="hidden" class="inv_product_id full-width"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control prod_qty" min="0" step="any"/></div>                                
                                    <div class="col-md-1"><input type="hidden" class="uom_id full-width"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control prod_unit_price full-width"  min="0" step="any"/></div>

                                    <div class="col-md-1"><input type="number" class="form-control prod_disc" min="0" step="any"/></div>
                                    <div class="col-md-2"><input type="text" class="form-control prod_desc" /></div>
                                    <div class="col-md-1"><input type="number" class="form-control prod_vat" min="0" step="any"/></div>
                                    <div class="col-md-1"><input type="number" class="form-control prod_vat_percent" min="0" step="any"/></div>
                                    <div class="col-md-1"><h5 class="total_div"><strong><span class="prod-total" prod-total="0">0</span>&nbsp;<span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></strong></h5></div>
                                    <div class="col-md-1">
                                        <span class="fa fa-plus-circle m-l-5 " style="color:#8a7dbe;cursor:pointer"></span>
                                        <span class="fa fa-minus-circle m-l-5 " style="color:#ed9421;cursor:pointer"></span>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>
                    <br>
                    <div class="container-sm-height" >
                        <div class="row row-sm-height b-a b-grey">
                            <div class="col-sm-6 col-sm-height   clearfix" style="border-left: 1px solid #eee;border-right: 1px solid #eee;padding-right:0">
                                <div class="col-sm-12  " style="padding-right:0">
                                    <textarea id="invoice_note" name="invoice_note" class="form-control NoteTextArea" placeholder="<?php print CommonLanguage::getMessage('enter_note'); ?> ..." aria-invalid="false"><?php echo $IsEdit ? $edit_arr['main']['description'] : '' ?></textarea>
                                </div>
                                <!--                                <div class="col-sm-12  " style="padding-right:0">
<h5 class="font-montserrat all-caps small no-margin hint-text bold"><?php print CommonLanguage::getMessage('payment'); ?><span class="fa fa-plus-circle m-l-5 openFrame" data-src="ACC/viewlibrary/1?is_pop=1" style="color:#8a7dbe;cursor:pointer"></span></h5>
<div id="InvoicePaymentSetup" class="no-print">
<div class="Body">
<div class="">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#SetPaidFormTab_1"><i class="fa fa-money"></i>&nbsp;&nbsp;<?php print CommonLanguage::getMessage('cash'); ?></a></li>
<li><a data-toggle="tab" href="#SetPaidFormTab_2"><i class="fa fa-file-text-o"></i>&nbsp;&nbsp;<?php print CommonLanguage::getMessage('check'); ?></a></li>
<li><a data-toggle="tab" href="#SetPaidFormTab_3"><i class="fa fa-cc-visa"></i>&nbsp;&nbsp;<?php print CommonLanguage::getMessage('credit_card'); ?></a></li>
</ul>
<div class="tab-content" style="padding:0;overflow:visible">
<div id="SetPaidFormTab_1" class="tab-pane fade in active">
<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="width: 65%;"><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('amount'); ?></span></td>
<td>
<div class="input-group InputSpinner" style="width:120px">
<input type="text" class="form-control" value="<?php echo ($IsEdit && array_key_exists('Cash', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cash']['Transaction_Amount'] : '0' ?>" onblur="InputMustBeAtleastN(this, 0);" />
<div class="input-group-btn-vertical">
<button class="btn btn-default more" type='button'><i class="fa fa-caret-up"></i></button>
<button class="btn btn-default" type='button'><i class="fa fa-caret-down"></i></button>
</div>
</div>
</td>
</tr>
<tr>
<td><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('Type'); ?></span></td>
<td style="text-align:center">
<input class="cash_account full-width" id="cash_account" name="cash_account" type="hidden"  value="<?php echo ($IsEdit && array_key_exists('Cash', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cash']['Account_ID'] : smc_session()['user_accounts']['cash_account_id'] ?>" data-select-name='<?php echo ($IsEdit && array_key_exists('Cash', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cash']['P_Name'] : smc_session()['user_accounts']['c_name'] ?>' />
</td>
</tr>
</tbody>
</table>
</div>
<div id="SetPaidFormTab_2" class="tab-pane fade in">
<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="width: 65%;"><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('amount'); ?></span></td>
<td>
<div class="input-group InputSpinner" style="width:120px">
<input type="text" class="form-control" value="<?php echo ($IsEdit && array_key_exists('Cheque', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cheque']['Transaction_Amount'] : "0" ?>" onblur="InputMustBeAtleastN(this, 0);" />
<div class="input-group-btn-vertical">
<button class="btn btn-default more" type='button'><i class="fa fa-caret-up"></i></button>
<button class="btn btn-default" type='button'><i class="fa fa-caret-down"></i></button>
</div>
</div>
</td>
</tr>
<tr>
<td><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('Type'); ?></span></td>
<td style="text-align:center">
<input class="check_account full-width" id="check_account" name="check_account" type="hidden" value="<?php echo ($IsEdit && array_key_exists('Cheque', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cheque']['Account_ID'] : smc_session()['user_accounts']['cheque_account_id'] ?>" data-select-name='<?php echo ($IsEdit && array_key_exists('Cheque', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cheque']['P_Name'] : "" ?>' />
</td>
</tr>
<tr>
<td><span class="SpanInTableInPayment"> <?php print CommonLanguage::getMessage('check'); ?>#</span></td>
<td style="text-align:center"><input type="text" class="form-control CheckNumValue" value="<?php echo ($IsEdit && array_key_exists('Cheque', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cheque']['check_nb'] : '' ?>" /></td>
</tr>
<tr>
<td><span class="SpanInTableInPayment"> <?php print CommonLanguage::getMessage('check_due_date'); ?></span></td>
<td style="text-align:center"><input type="text" class="form-control check_due_date" value="<?php echo ($IsEdit && array_key_exists('Cheque', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cheque']['due_date'] : '' ?>"/></td>
</tr>
<tr>
<td><span class="SpanInTableInPayment"> <?php print CommonLanguage::getMessage('value_date'); ?></span></td>
<td style="text-align:center"><input type="text" class="form-control value_date" value="<?php echo ($IsEdit && array_key_exists('Cheque', $edit_arr['Payment'])) ? $edit_arr['Payment']['Cheque']['value_date'] : '' ?>"/></td>
</tr>
</tbody>
</table>
</div>
<div id="SetPaidFormTab_3" class="tab-pane fade in">
<table class="table" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="width: 65%;"><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('amount'); ?></span></td>
<td>
<div class="input-group InputSpinner" style="width:120px">
<input type="text" class="form-control" value="<?php echo ($IsEdit && array_key_exists('Credit_Card', $edit_arr['Payment'])) ? $edit_arr['Payment']['Credit_Card']['Transaction_Amount'] : "" ?>" onblur="InputMustBeAtleastN(this, 0);" />
<div class="input-group-btn-vertical">
<button class="btn btn-default more" type='button'><i class="fa fa-caret-up"></i></button>
<button class="btn btn-default" type='button'><i class="fa fa-caret-down"></i></button>
</div>
</div>
</td>
</tr>
<tr>
<td><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('Type'); ?></span></td>
<td style="text-align:center">
<div class="input-group-btn DropDownForForms Closable">
<input class="credit_account full-width" id="credit_account" name="credit_account" type="hidden"  value="<?php echo ($IsEdit && array_key_exists('Credit_Card', $edit_arr['Payment'])) ? $edit_arr['Payment']['Credit_Card']['Account_ID'] : "" ?>" data-select-name='<?php echo ($IsEdit && array_key_exists('Credit_Card', $edit_arr['Payment'])) ? $edit_arr['Payment']['Credit_Card']['P_Name'] : "" ?>' />
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>-->
                            </div>
                            <div class="col-sm-3 col-sm-height col-middle clearfix sm-p-b-15" style="text-align:center">

                                <div  class="col-md-12" style="text-align: left;display: inline-block;">
                                    <h5 class="font-montserrat all-caps small no-margin hint-text bold" ><?php print CommonLanguage::getMessage('cash_difference'); ?><input type="number" class="form-control ignore" id="cash_difference" value="<?php echo $IsEdit ? $edit_arr['main']['cash_difference_amount'] : ''; ?>" oninput="update_overall_total()"/> </h5>
                                </div>
                                <div  class="col-md-12" style="text-align: left;display: inline-block;">
                                    <h5 class="font-montserrat all-caps small no-margin hint-text bold" ><?php print CommonLanguage::getMessage('vat_ten'); ?><input class="form-control ignore" readonly id="InvoiceVAT" value="<?php echo $IsEdit ? $edit_arr['main']['invoice_vat'] : '' ?>"/> </h5>
                                </div>
                            </div>
                            <div class="col-sm-3 text-right bg-menu col-sm-height padding-15" style="text-align:left !important">
                                <div style="text-align: left;display: inline-block;">
                                    <h5 class="font-montserrat all-caps small no-margin hint-text text-white bold"><?php print CommonLanguage::getMessage('pump_total'); ?></h5>
                                    <h1 class="no-margin text-white Total"><span class="CurrencyIndicator"></span>&nbsp;<span id="pump_total">0</span></h1>
                                    <h5 class="font-montserrat all-caps small no-margin hint-text text-white bold"><?php print CommonLanguage::getMessage('coupon_total'); ?></h5>
                                    <h1 class="no-margin text-white Total"><span class="CurrencyIndicator"></span>&nbsp;<span id="coupon_total">0</span></h1>
                                    <h5 class="font-montserrat all-caps small no-margin hint-text text-white bold"><?php print CommonLanguage::getMessage('products_total'); ?></h5>
                                    <h1 class="no-margin text-white Total"><span class="CurrencyIndicator"></span>&nbsp;<span id="products_total">0</span></h1>
                                    <h5 class="font-montserrat all-caps small no-margin hint-text text-white bold"><?php print CommonLanguage::getMessage('overall_total'); ?></h5>
                                    <h1 class="no-margin text-white Total"><span class="CurrencyIndicator"></span>&nbsp;<span id="overall_total">0</span></h1>
                                    <h5 class="font-montserrat all-caps small no-margin hint-text text-white bold"><?php print CommonLanguage::getMessage('cash_total'); ?></h5>
                                    <h1 class="no-margin text-white Total"><span class="CurrencyIndicator"></span>&nbsp;<span id="cash_total">0</span></h1>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="container-sm-height" >
                        <button class=" btn btn-primary btn-cons m-b-10 pull-right " id="InvoiceSubmitButton" style="padding-top: 10px;top: 20px;min-width:253px;" type="button">
                            <i class="pg-form"></i>
                            <span class="bold"><?php print CommonLanguage::getMessage('Save'); ?></span>
                        </button>
                        <button class=" btn btn-danger btn-cons m-b-10 pull-right disabled" id="PrintBtn" style="padding-top: 10px;top: 20px;min-width:253px;" type="button">
                            <i class="fa fa-print"></i>
                            <span class="bold"><?php print CommonLanguage::getMessage('print'); ?></span>
                        </button>


                    </div>
                    <hr>

                </div>
            </div>
        </div>
    </div>
</form>


<script>
    var RequiredField = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            negative_value = <?php echo json_encode(CommonLanguage::getMessage('negative_value')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>,
            default_vat = <?php echo json_encode(smc_session()['vat_percent']); ?>,
            IsEditMode = <?php echo $IsEdit ? "true" : "false"; ?>,
            is_actual = <?php echo $is_actual ? "true" : "false"; ?>,
            ID_Edit = IsEditMode ? <?php echo json_encode($id); ?> : '',
            max_discount = <?php echo json_encode(smc_session()['user_accounts']['max_discount']); ?>,
            local_currency = <?php echo json_encode(smc_session()['local_currency']); ?>,
            inv_nb = <?php echo json_encode(CommonLanguage::getMessage('purchase') . ' #'); ?>;

    var overall_total = 0, pump_total = 0, prod_total = 0, bon_total = 0, total_vat = 0, pump_vat = 0, prod_vat = 0, bon_vat = 0, service_total = 0;
    $(function () {
        $('#InvoiceForm').on('focus', 'input[type=number]', function (e) {
            $(this).on('mousewheel.disableScroll', function (e) {
                e.preventDefault()
            });
        });
        build_select2($(".search_customer"), ("xhr/customerSearch") + (IsEditMode ? "?is_edited=" + $(".search_customer").val() : "?Status_ID=1"))
        build_select2($("#Account_Code"), ("xhr/accIDSearch?type=" + ('sales')))
        build_select2($(".Depot_Loc"), "xhr/depotSearch")
        build_select2($("#cash_account"), ("xhr/accIDSearch?type=cash"))
        build_select2($("#check_account"), ("xhr/accIDSearch?type=check"))
        build_select2($("#credit_account"), ("xhr/accIDSearch?type=credit_card"))
        build_select2($("#job_id"), "ACCAjax/JobSearch")
        build_select2($("input.bone_type_id"), "xhr/CommonSearch?col_id=bone_type_id&col_name=bone_type_name&table=tbl_bone_type")
        build_select2($("input.bone_product_id,input.inv_product_id"), ("xhr/ProductSearch"));
        build_select2($("input.uom_id"), "xhr/productuomSearch?pid=" + $("input.inv_product_id").val())
        build_select2($("input[name=PIC_ID]"), "xhr/employeeSearch")

        //        $(".CurrencyIndicator").html($("#CurrencyDropDown").attr("data-symbol"))

        $("#invoice_date,.check_due_date,.value_date").datepicker({autoclose: true});

        $(".bone-div").on("click", " .fa-plus-circle", function (e) {
            var Parent = $(this).parents(".bone-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.attr("data-is-service", 0)
            NewItem.find("input.bone-total").attr('bone-total', 0);
            NewItem.find("input.bone-total").val(0);
            NewItem.find(".select2-container").remove()
            NewItem.find("input.bone_type_id,input.bone_product_id").val('').attr("data-select-name", "");
            build_select2(NewItem.find("input.bone_product_id"), ("xhr/ProductSearch"))
            build_select2(NewItem.find("input.bone_type_id"), "xhr/CommonSearch?col_id=bone_type_id&col_name=bone_type_name&table=tbl_bone_type")
            $(".select2-container", NewItem).show()
        })
        $(".bone-div").on("click", ".fa-minus-circle", function (e) {
            if ($(".bone-row").length == 1) {
                var parent = $(this).parents('.bone-row');
                $('input', parent).val('');
                $('input.bone_type_id,input.bone_product_id', parent).trigger('change');
                parent.attr('total_changed', 0);
                update_bon_total();
                return;
            }
            $(this).closest(".bone-row").empty().remove();
            update_bon_total();
        })
        $(".prod-div").on("click", " .fa-plus-circle", function (e) {
            var Parent = $(this).parents(".prod-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.attr("data-is-service", 0)
            NewItem.find(".select2-container").remove()
            NewItem.find("input.inv_product_id,input.uom_id").val('').attr("data-select-name", "");
            build_select2(NewItem.find("input.inv_product_id"), ("xhr/ProductSearch"))
            build_select2(NewItem.find("input.uom_id"), "xhr/productuomSearch?pid=" + NewItem.find("input.inv_product_id").val())
            $(".select2-container", NewItem).show()
        })
        $(".prod-div").on("click", ".fa-minus-circle", function (e) {
            if ($(".prod-row").length == 1) {
                var parent = $(this).parents('.prod-row');
                $('input', parent).val('');
                $('input.inv_product_id', parent).trigger('change');
                $('input.uom_id', parent).trigger('change');
                parent.attr('total_changed', 0)
                parent.attr('disc_amount_changed', 0)
                $('span.prod-total', parent).html('');
                $('span.prod-total', parent).attr('prod-total', 0);
                update_prod_total()
                return;
            }
            $(this).closest(".prod-row").empty().remove();
            update_prod_total();
        })
        $(".prod-div").on("change", "input.inv_product_id", function () {
            var row = $(this).parents(".prod-row");
            row.find(".prod_unit_price").val('')
            row.find("input.uom_id").val('').attr("data-select-name", "")
            build_select2(row.find("input.uom_id"), "xhr/productuomSearch?pid=" + $(this).val())
            $.ajax({
                url: "InventoryAjax/get_product_by_id",
                data: {pid: $(this).val()},
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        $(".prod_vat_percent", row).val(v1.responseJSON['Product_VAT'] ? v1.responseJSON['vat_percent'] : '')
                        row.attr("data-is-service", v1.responseJSON['is_service'])
                        update_prod_total()
                    }
                }
            })
        })

        $(".pump-div").on("keyup", ".pump_unit_price,.pump_vat_percent,.pump_vat", function () {
            update_pump_total()
        })
        $(".pump-div").on("keyup", ".old_counter,.new_counter", function () {
            var row = $(this).parents(".pump-row"),
                    new_counter = parseFloat($(".new_counter", row).val() || 0),
                    old_counter = parseFloat($(".old_counter", row).val() || 0)
            diff = (new_counter - old_counter) <= 0 ? 0 : (new_counter - old_counter);
            $(".diff", row).val(diff)
            update_pump_total()
        })

        $(".bone-div").on("change", ".bone_product_id", function () {
            var row = $(this).parents('.bone-row');
            $.ajax({
                url: "InventoryAjax/get_product_by_id",
                data: {pid: $(this).val(), cid: $("input.bone_type_id", row).val()},
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        $(".service", row).val(v1.responseJSON['service_price'])
                        $(".bone_unit_price", row).val(v1.responseJSON['price'])
                        $(".bone_vat_percent", row).val(v1.responseJSON['Product_VAT'] ? v1.responseJSON['vat_percent'] : '')
                        row.attr("data-is-service", v1.responseJSON['is_service'])

                        update_bon_total()
                    }
                }
            })
        })
        $(".bone-div").on("keyup", ".liters,.bone_unit_price,.bone_vat_percent,.bone_vat,.bone-total", function () {
            var unit_price = parseFloat($(this).parents(".bone-row").find(".bone_unit_price").val() || 0),
                    mytr = $(this).parents(".bone-row");
            mytr.attr("total_changed", 0)
            if ($(this).hasClass("bone-total")) {
                var myval = parseFloat($(this).parents(".bone-row").find(".bone-total").val() || 0),
                        vat_percent = parseFloat(mytr.find(".bone_vat_percent").val()) || 0,
                        vat_amount = myval * (vat_percent / 100) / (1 + (vat_percent / 100)),
                        total = myval - vat_amount;
                $(this).attr("bone-total", myval)
                mytr.attr("total_changed", 1)
                mytr.find(".bone_vat").val(public_round_number(vat_amount))
                $(this).parents(".bone-row").find(".liters").val((total / unit_price))
            }
            update_bon_total()

        })

        $(".bone-div").on("change", ".bone_unit_price,.bone_vat_percent,.bone_vat,.bone-total", function () {
            mytr = $(this).parents(".bone-row");
            mytr.find(".bone_unit_price").val(public_round_number(mytr.find(".bone_unit_price").val()))

            mytr.find(".bone_vat_percent").val(public_round_number(mytr.find(".bone_vat_percent").val()))

            mytr.find(".bone_vat").val(public_round_number(mytr.find(".bone_vat").val()))

            mytr.find(".bone-total").val(public_round_number(mytr.find(".bone-total").val()))


        })

        $(".prod-div").on("change", ".prod_unit_price,.prod_vat_percent,.prod_vat,.prod_qty,.prod_disc ", function () {
            var mytr = $(this).parents('.prod-row');
            mytr.find(".prod_unit_price").val(public_round_number(mytr.find(".prod_unit_price").val()))
            mytr.find(".prod_vat_percent").val(public_round_number(mytr.find(".prod_vat_percent").val()))
            mytr.find(".prod_vat_percent").val(public_round_number(mytr.find(".prod_vat_percent").val()))
            mytr.find(".prod_vat").val(public_round_number(mytr.find(".prod_vat").val()))
            mytr.find(".prod_qty").val(public_round_number(mytr.find(".prod_qty").val()))
            mytr.find(".prod_disc").val(public_round_number(mytr.find(".prod_disc").val()))

        })


        $(".prod-div").on("keyup", ".prod_unit_price,.prod_vat_percent,.prod_vat,.prod_qty,.prod_disc", function () {
            update_prod_total()
        })

        $(".prod-div").on("change", "input.uom_id", function () {
            var row = $(this).parents('.prod-row');
            $.ajax({
                url: "InventoryAjax/get_prod_price",
                data: {uom: $(this).val(), pid: $("input.inv_product_id", row).val()},
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        $(".prod_unit_price", row).val(v1.responseJSON)
                        update_prod_total()
                    }
                }
            })
        })
        if (IsEditMode) {
            update_pump_total()
            update_bon_total()
            update_prod_total()
            update_overall_total()
        }
        $("#CurrencyDropDown li").click(function () {
            $(".CurrencyIndicator").html($(this).attr("MySymbol"))
        })

        $("#InvoiceSubmitButton").on("click", function () {
            //            if (!$("#InvoiceForm").valid()) {
            //                $(document.body).animate({scrollTop: 0}, 800)
            //                return;
            //            }
            if ($("#Currency_ID").val() == '') {
                $("#PurchaseSuccess").attr("class", "alert alert-danger").html(RequiredField)
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }
            var validate = true;
            var data = {
                customer_id: $("#search_customer").val(),
                account_id: $("#Account_Code").val(),
                invoice_date: $("#invoice_date").val(),
                depot_id: $("#Depot_Loc").val(),
                current_rate: $("#current_rate").val(),
                currency_id: $("#Currency_ID").val(),
                job_id: $("#job_id").val(),
                employee_id: $("#PIC_ID").val(),
                is_actual: is_actual ? 1 : 0,
                invoice_vat: $("#InvoiceVAT").val(),
                overall_total: $("#overall_total").html().replace(/,/g, ""),
                description: $("#invoice_note").val(),
                cash_difference: $('#cash_difference').val(),
                pump_total: pump_total,
                prod_total: prod_total,
                bon_total: bon_total,
                service_total: service_total,
                pump_vat: pump_vat,
                prod_vat: prod_vat,
                bon_vat: bon_vat,
                pumps: [],
                coupon: [],
                product: []
            };
            var negative_check = true;
            $(".pump-row").each(function () {
                if ($(this).find(".new_counter").val() < 0 || $(this).find(".pump_unit_price").val() < 0
                        || $(this).find(".pump_vat").val() < 0 || $(this).find(".pump_vat_percent").val() < 0) {
                    negative_check = false;
                }
                if (parseFloat($(this).find(".new_counter").val()) < parseFloat($(this).find(".old_counter").val())) {
                    validate = false;
                    $(this).find(".new_counter").addClass("wrong")
                } else {
                    $(this).find(".new_counter").removeClass("wrong")
                }

                if ($(".diff", $(this)).val() != '' && $(".pump_unit_price", $(this)).val() != '' && $(".diff", $(this)).val() != 0) {
                    data.pumps.push({
                        pump_id: $(this).find(".pum_label").attr("data-id"),
                        uom_id: $(this).find(".pum_label").attr("data-uom"),
                        product_id: $(this).find(".pum_label").attr("data-prod"),
                        old_counter: $(this).find(".old_counter").val(),
                        new_counter: $(this).find(".new_counter").val(),
                        diff: $(this).find(".diff").val(),
                        pump_unit_price: $(this).find(".pump_unit_price").val(),
                        pump_vat: $(this).find(".pump_vat").val() || 0,
                        pump_vat_percent: $(this).find(".pump_vat_percent").val() || 0,
                        total: $(this).find(".pump-total").attr("pump-total"),
                        is_service: $(this).find(".pum_label").attr("data-is-service")
                    })
                }
            })
            $(".bone-row").each(function () {
                if ($(this).find(".liters").val() < 0 || $(this).find(".bone_unit_price").val() < 0
                        || $(this).find(".bone_vat").val() < 0 || $(this).find(".bone_vat_percent").val() < 0
                        || $(this).find(".bone-total").val() < 0) {
                    negative_check = false;
                }
                if ($("input.bone_type_id", $(this)).val() != '' && $("input.bone_product_id ", $(this)).val() != ''
                        && $("input.liters ", $(this)).val() != '' && $("input.liters ", $(this)).val() != 0 && $("input.bone_unit_price ", $(this)).val() != '') {
                    data.coupon.push({
                        bone_type_id: $("input.bone_type_id", $(this)).val(),
                        product_id: $("input.bone_product_id", $(this)).val(),
                        liters: $("input.liters ", $(this)).val(),
                        service: $("input.service ", $(this)).val(),
                        unit_price: $("input.bone_unit_price", $(this)).val(),
                        vat: $(this).find(".bone_vat").val() || 0,
                        vat_percent: $(this).find(".bone_vat_percent").val() || 0,
                        total: $(this).find(".bone-total").attr("bone-total"),
                        is_service : $(this).attr("data-is-service")
                    })
                }
            });
            var is_uom_empty = true;
            $(".prod-row").each(function () {
                if ($(this).find(".prod_qty").val() < 0 || $(this).find(".prod_unit_price").val() < 0
                        || $(this).find(".prod_disc").val() < 0 || $(this).find(".prod_vat").val() < 0
                        || $(this).find(".prod_vat_percent").val() < 0) {
                    negative_check = false;
                }
                if (parseFloat($("input.prod_disc", $(this)).val()) > 100) {
                    validate = false;
                    $("input.prod_disc", $(this)).addClass("wrong")
                } else {
                    $("input.prod_disc", $(this)).removeClass("wrong")
                }
                if ($("input.inv_product_id", $(this)).val() && $("input.prod_qty ", $(this)).val() && $("input.prod_unit_price", $(this)).val() && !$("input.uom_id ", $(this)).val()) {
                    is_uom_empty = false;
                }
                if ($("input.inv_product_id", $(this)).val() != '' && $("input.prod_qty ", $(this)).val() != ''
                        && $("input.prod_qty ", $(this)).val() != 0
                        && $("input.uom_id ", $(this)).val() != '' && $("input.prod_unit_price", $(this)).val() != '') {
                    data.product.push({
                        product_id: $("input.inv_product_id", $(this)).val(),
                        qty: $("input.prod_qty ", $(this)).val(),
                        uom_id: $("input.uom_id", $(this)).val(),
                        unit_price: $("input.prod_unit_price", $(this)).val(),
                        disc: $("input.prod_disc", $(this)).val() || 0,
                        desc: $("input.prod_desc", $(this)).val(),
                        vat: $(this).find(".prod_vat").val() || 0,
                        vat_percent: $(this).find(".prod_vat_percent").val() || 0,
                        total: $(this).find(".prod-total").attr("prod-total"),
                        is_service: $(this).attr("data-is-service")
                    })
                }
            })
            if (IsEditMode) {
                data.invoice_id = ID_Edit;
            }
            //            console.log(data.product.length + '==' + data.coupon.length + '==' + data.pumps.length)
            if ((data.product.length == 0 && data.coupon.length == 0 && data.pumps.length == 0) || !validate) {

                $("#PurchaseSuccess").attr("class", "alert alert-danger").html(RequiredField)
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }
            if (!negative_check) {
                $("#PurchaseSuccess").attr("class", "alert alert-danger").html(negative_value)
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }
            if (!is_uom_empty) {
                $("#PurchaseSuccess").attr("class", "alert alert-danger").html('<?php echo CommonLanguage::getMessage('make_sure_choose_uom'); ?>')
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }
            if ($("#InvoiceForm").valid()) {
                $.ajax({
                    url: IsEditMode ? "InventoryAjax/edit_station_invoice" : "InventoryAjax/add_station_invoice",
                    method: "POST",
                    data: data,
                    dataType: "json",
                    complete: function (v1, v2) {
                        if (v2 == "success") {
                            if (v1.responseJSON[0] == 'OK') {
                                ID_Edit = v1.responseJSON[1];
                                $("#PrintBtn").removeClass("disabled")
                                $("#PurchaseSuccess").attr("class", "alert alert-success").html(Submitted_Successfully)

                                $("#InvoiceSubmitButton").attr("disabled", "disabled")
                            } else {
                                $("#PurchaseSuccess").attr("class", "alert alert-danger").html(v1.Failed_Submit)
                            }
                        } else {
                            $("#PurchaseSuccess").attr("class", "alert alert-danger").html(Failed_Submit)
                        }
                        $(document.body).animate({scrollTop: 0}, 800)
                        $("#PurchaseDisabler").hide();
                    }
                })
            }
        });
        $("#PrintBtn").on("click", function () {
            var myurl = "Inventory/view_station_invoice/" + ID_Edit;
            printExternal(myurl)
        })
    })
    function update_pump_total() {
        pump_total = 0;
        pump_vat = 0;
        $(".pump-row").each(function () {
            var diff = parseFloat($(".diff", $(this)).val() || 0),
                    unit_price = parseFloat($(".pump_unit_price", $(this)).val() || 0),
                    vat_percent = parseFloat($(".pump_vat_percent", $(this)).val() || 0),
                    vat_amount = (diff * unit_price * vat_percent / 100),
                    total = (diff * unit_price) + vat_amount;
            pump_total += total;
            pump_vat += vat_amount;
            $(".pump_vat", $(this)).val(vat_amount)
            $(".pump-total", $(this)).html(AddCommas(public_round_number(total))).attr("pump-total", total)
        })
        update_overall_total()
    }
    function update_bon_total() {
        bon_total = 0;
        bon_vat = 0;
        service_total = 0;
        $(".bone-row").each(function () {
            var liters = parseFloat($(".liters", $(this)).val() || 0),
                    unit_price = parseFloat($(".bone_unit_price", $(this)).val() || 0),
                    service_price = parseFloat($(".service", $(this)).val() || 0),
                    vat_percent = parseFloat($(".bone_vat_percent", $(this)).val() || 0),
                    vat_amount = (liters * unit_price * vat_percent / 100),
                    total_service = service_price * liters,
                    total = (liters * unit_price) + vat_amount;
            bon_total += total;
            bon_vat += vat_amount;
            service_total += total_service;
            $(".bone_vat", $(this)).val(vat_amount)
            if ($(this).attr("total_changed") == 0) {
                $(".bone-total", $(this)).val(total).attr("bone-total", total)
            }

        })
        update_overall_total()
    }
    function update_prod_total() {
        prod_total = 0;
        prod_vat = 0;
        $(".prod-row").each(function () {
            var qty = parseFloat($(".prod_qty", $(this)).val() || 0),
                    unit_price = parseFloat($(".prod_unit_price", $(this)).val() || 0),
                    disc = parseFloat($(".prod_disc", $(this)).val() || 0),
                    disc_amount = qty * unit_price * disc / 100,
                    vat_percent = parseFloat($(".prod_vat_percent", $(this)).val() || 0),
                    vat_amount = (((qty * unit_price) - disc_amount) * vat_percent / 100),
                    total = (qty * unit_price * (100 - disc) / 100) + vat_amount;
            prod_total += total;
            prod_vat += vat_amount;
            $(".prod_vat", $(this)).val(vat_amount)
            $(".prod-total", $(this)).html(AddCommas(public_round_number(total))).attr("prod-total", total)
        })
        update_overall_total()
    }
    function update_overall_total() {
        overall_total = pump_total + prod_total + bon_total;
        total_vat = pump_vat + prod_vat + bon_vat;
        var cash_total = pump_total + prod_total - bon_total + service_total;
        var cash_difference = $('#cash_difference').val() || 0;
        cash_total -= cash_difference;
        $("#pump_total").html(AddCommas(public_round_number(pump_total)))
        $("#products_total").html(AddCommas(public_round_number(prod_total)))
        $("#coupon_total").html(AddCommas(public_round_number(bon_total)))
        $("#overall_total").html(AddCommas(public_round_number(overall_total)))
        $("#cash_total").html(AddCommas(public_round_number(cash_total)))
        $("#InvoiceVAT").val(public_round_number(total_vat))
    }
</script>
