<?php if ($IsEdit && $check_lock != '') { ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong><?php print CommonLanguage::getMessage('danger'); ?> : </strong><?php echo $check_lock ?>
    </div>
    <?php
    return;
}
if ($IsEdit && empty($edit_arr['Main']['Note_ID'])) { ?>
    <div class="alert alert-danger" role="alert">
        <button class="close" data-dismiss="alert"></button>
        <strong><?php print CommonLanguage::getMessage('danger'); ?>: </strong><?php print CommonLanguage::getMessage(' you_can_not_edit'); ?> <?php echo $IsVendor ? 'Debit Note' : 'Credit Note'; ?>! 
        <br>
        <?php print CommonLanguage::getMessage('this'); ?>    <?php echo $IsVendor ? 'Debit Note' : 'Credit Note'; ?><?php print CommonLanguage::getMessage('is_not_available'); ?> .
    </div>
    <?php
    die();
}
?>
<script src="<?php echo JS_PATH; ?>form_wizard.js"></script>
<script src="<?php echo THEME_PATH; ?>plugins/jquery-validation/js/jquery.validate.min.js"></script>
<style type="text/css">
    #Invoice_Details{
        padding-top:20px;
    }
    .total-purchase {
        font-size: 16px;
        font-weight: bold;
        color: #2b2b2c;
        text-transform: uppercase;
        margin-bottom: 10px;
    }
    #ReturnItemTable td{
        position:relative;
        vertical-align:middle;
    }
    #ReturnItemTable .Quantity,#ReturnItemTable .Total,#ReturnItemTable .UnitPrice{
        text-align:center;
    }
    .EditRow{
        font-size:20px;
        cursor:pointer;
    }
    .EditRow:hover{
        color: rgb(0, 144, 217);
    }
    .table-responsive{
        overflow:visible;
        -webkit-transform: perspective(100px);
        -moz-transform: perspective(100px);
        transform: perspective(100px);
    }
    #ReturnItemTable tr{
        -webkit-transition:-webkit-transform 0.2s linear;
        -moz-transition:-moz-transform 0.2s linear;
        transition:transform 0.2s linear;
    }
    #ReturnItemTable tr.Edit{
        -webkit-transform: scale(1.1) rotate(0);
        -moz-transform: scale(1.1) rotate(0);
        transform: scale(1.1) rotate(0);
    }
    #ReturnItemTable tr.Faded > td{
        opacity:0.5;
        cursor:default;
    }
    #ReturnItemTable tr.Faded,#ReturnItemTable tr.Faded > td{
        background-color:#fff !important;
    }
    #ReturnItemTable tr.Edit > td{
        background-color:#eee !important;
    }
    #ReturnItemTable .Hint{
        position: absolute;
        font-size: 10px;
        font-weight: bold;
        height: 20px;
        line-height: 20px;
        bottom: 2px;
        right: 4px;
        color: rgb(153, 153, 153);
    }
    .datepicker thead tr .dow {
        color:#ed9421 !important;
    }
    .datepicker table tr td.day:hover {
        background: #ed9421 !important;
        color:#fff;
    }
    .datepicker table tr td.active {
        background-color: #ed9421 !important;
    }
    .datepicker thead tr .datepicker-switch {
        color: #ed9421 !important;
        font-size: 13px;
    }
    .datepicker table tr td span.active {
        background-color: #ed9421 !important;
    }
    .datepicker table tr td span:hover {
        background-color: #ed9421 !important;
        color:#fff;
    }
    datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #ed9421 !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    .datepicker thead tr .next:before, .datepicker thead tr .prev:before {
        color: #ed9421 !important;
        font-family: 'FontAwesome';
        font-size: 10px;
    }
    .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;
    }

    .amount {
        background: #4c0870;
        color: #fff;
        font-size: 30px;
        font-family: 'Abel', sans-serif;
        padding: 30px;
    }
    /*    .demo-form-wysiwyg {
            height: 237px;
        }*/
    .nav-tabs>li.active>a, .nav-tabs>li.active>a:hover, .nav-tabs>li.active>a:focus{
        border-color: #e6e6e6;
        color: #fff ;
        background-color: #4c0870;
    }
    #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;
        /*
        -webkit-border-radius: 3px 0px 0px 3px !important;
        -moz-border-radius: 3px 0px 0px 3px !important;
        border-radius: 3px 0px 0px 3px !important;
        */
    }
    .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;
    }
    .bg-master-lighter {
        border:none;
        background-color:#fff;
        border-top:1px solid #ddd;  
    }
    #Note_Description{
        overflow: hidden;
        word-wrap: break-word;
        resize:vertical ;
        background-color:#eee;
        width:100%;
        border: 1px solid #6dc0f9;
    }
    .my_tr{
        border-bottom: 1px solid rgba(0,0,0,0.07);
        padding:10px;
    }
    .wrong{
        border: 1px solid rgba(255, 0, 0, 0.2);
        background: rgba(255, 0, 0, 0.05);
    }
    .rem{
        color:#eb9220;
        cursor:pointer;
        font-weight:700;
    }
    .balance{
        color:red;
        font-weight:700;
    }
</style>
<div class="wrapper">
    <div class="panel invoice">
        <div class="panel-body">
            <div class="row">
                    <div >
                        <h2 class="bold"><?php echo $IsVendor ? CommonLanguage::getMessage('debit_note') : CommonLanguage::getMessage('credit_note') . ($IsEdit ? ' # ' . $edit_arr['Main']['note_nb'] : ''); ?> </h2>
                    </div>
                    <div id="ReturnItemSuccess"></div>

            </div>
            <div class="col-md-8">
                <div class="row">
                    <div class="input-group  col-md-4 center-margin pull-left " >
                        <div class="row">
                            <label><?php print CommonLanguage::getMessage(!$IsVendor ? 'choose_customer' : 'choose_supplier'); ?><span class="red-star">*</span></label><span class="fa fa-plus-circle m-l-5 openFrame" data-src="<?php echo!$IsVendor ? "Inventory/CustomerList?is_pop=1" : "Inventory/ViewVendorList?is_pop=1" ?>" style="color:#4c0870;cursor:pointer"></span>
                            <input class="search_name full-width" id="search_name" name="search_name" type="hidden"  value="<?= $IsEdit ? $edit_arr['Main']['Supplier_Customer_ID'] : ""; ?>" data-select-name='<?= json_encode($IsEdit ? $edit_arr['Person']['Name'] : ""); ?>'/>
                        </div>
                        <div class="row">
                            <div id="SupplierInfoContent" class="<?php echo $IsEdit ? '' : 'hidden' ?>">
                                <strong><span class='Name' id='Customer_Name' data-id='<?php echo $IsEdit ? $edit_arr['Person']['Name'] : '' ?>'><?php echo $IsEdit ? $edit_arr['Person']['Name'] : '' ?></span></strong>
                                <div><strong><?php echo CommonLanguage::getMessage('company'); ?>: </strong><span class='Company'><?php echo $IsEdit ? $edit_arr['Person']['Company'] : '' ?></span></div>
                                <div><strong><?php echo CommonLanguage::getMessage('Email'); ?>: </strong><span class='Email'><?php echo $IsEdit ? $edit_arr['Person']['Email'] : '' ?></span></div>
                                <div><strong><?php echo CommonLanguage::getMessage('Phone'); ?>: </strong><span class='Phone'><?php echo $IsEdit ? $edit_arr['Person']['Phone'] : '' ?></span></div>
                                <div><strong><?php echo CommonLanguage::getMessage('address'); ?>: </strong><span class='Address'><?php echo $IsEdit ? $edit_arr['Person']['Address'] : '' ?></span></div>
                                <div><strong><?php echo CommonLanguage::getMessage('country'); ?>: </strong><?php echo $IsEdit ? $edit_arr['Person']['Country_Name'] : '' ?></div>
                                <div id='Finance_Account' data-id='<?php echo $IsEdit ? $edit_arr['Person']['Account_ID'] : '' ?>'><strong><?php echo CommonLanguage::getMessage('financial_nb'); ?>: </strong><?php echo $IsEdit ? $edit_arr['Person']['Account_Code'] : '' ?></div>
                                <div id='Class_ID' style='display:none;'><?php echo ($IsEdit && !$IsVendor) ? $edit_arr['Person']['Class_ID'] : '' ?></div>
                                <div id='Class_Currency' style='display:none;'><?php echo ($IsEdit && !$IsVendor) ? $edit_arr['Person']['Class_Currency'] : '' ?></div>

                            </div>
                        </div>
                    </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:#4c0870;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'][!$IsVendor ? 'sales_account_id' : 'purchase_account_id']; ?>" data-select-name='<?= json_encode($IsEdit ? $edit_arr['Main']['Account_Name'] : smc_session()['user_accounts'][!$IsVendor ? 's_name' : 'p_name']); ?>' />

                    </div>

                    <div class="col-md-4">
                        <div class="col-md-12">
                            <label><?php print CommonLanguage::getMessage('currency'); ?><span class="red-star">*</span></label>
                            <?php // echo Forms::CurrencyDropDown(1, "complete")  ?>
                            <div id="CurrencyIDContainer" >
                                <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'] : smc_session()['local_currency_symbol']; ?>">
                                        <button type="button" class="btn btn-complete 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_symbol']) ?></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"/>
                                        </button>
                                        <ul class="dropdown-menu">
                                            <?php echo $CurList ?>
                                            <li class="divider"></li>
                                            <li><a class="LoadMore">More..</a></li>
                                        </ul>
                                    </div>

                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <button class="btn btn-complete"  id="get_inv"><?php echo CommonLanguage::getMessage('get_invoices'); ?></button>
                        </div>
                        <div class="col-md-12">
                            <h1 class="no-margin "><?php echo CommonLanguage::getMessage('total_invoices')?></h1>
                            <h1 class="no-margin  Total"><span class="CurrencyIndicator"></span>&nbsp;<span id="InvoicesTotal">0</span></h1>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-4 pull-right">


                <div class="row">

                    <div class="col-xs-12 m-b-10">
                        <div class="total-purchase">
                            <?php print CommonLanguage::getMessage('total'); ?>
                        </div>
                        <div class="amount">
                            <strong><span id='CurrencyIndicator'><?php echo smc_session()['local_currency_symbol'] ?></span>&nbsp;
                                <span id="balance"><?php echo $IsEdit ? CommonFunctions::public_round_number(($edit_arr['Main']['Note_Amount'] + $edit_arr['Person']['Customer_Balance'])) : '0' ?> </span></strong>
                        </div>
                    </div>

                    <div class="col-md-12 col-xs-12 ">
                        <div  class="form-group form-group-default input-group">
                            <label><?php print CommonLanguage::getMessage('Date'); ?></label>
                            <input id="Note_Date" name="Note_Date" class="form-control" value="<?php echo ($IsEdit ? $edit_arr['Main']['Note_Date'] : $today); ?>" />
                            <span class="input-group-addon bg-complete"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                    <div class=" form-group col-md-12 col-xs-12" style="display: flex">  
                        <div style="width: 48%;margin-right:2%;" >
                            <label><?php print CommonLanguage::getMessage('exchange_rate'); ?><span class="red-star">(<?php echo smc_session()['local_currency_symbol'] ?>)</span></label>
                            <input type="number" step="any" min="0" id="current_rate" name="current_rate" class="form-control full-width current_rate"   value="<?php echo $IsEdit ? $edit_arr['Main']['current_rate'] : '' ?>" />
                        </div>
                        <div <?php echo smc_session()['default_setting']['activate_second_currency'] == 1 ? '':'hidden'; ?> style="width: 50%;" >
                            <label><?php print CommonLanguage::getMessage('exchange_rate'); ?><span class="red-star">(<?php echo smc_session()['second_currency']['symbol'] ?>)</span></label>
                            <input type="number" step="any" min="0" id="second_currency_rate" name="second_currency_rate" class="form-control full-width second_currency_rate"   value="<?php echo $IsEdit ? $edit_arr['Main']['second_currency_rate'] : '' ?>" />

                        </div>

                    </div>
                    <div class=" form-group col-md-12 col-xs-12  <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?> " >  
                        <label><?php print CommonLanguage::getMessage('branch'); ?></label>
                        <input class=" full-width" id="branch_id" name="branch_id" type="hidden"  value="<?= $IsEdit ? $edit_arr['Main']['branch_id'] : (empty(smc_session()['user_accounts']['branch_id']) ? smc_session()['default_branch_id'] : smc_session()['user_accounts']['branch_id']) ?>" data-select-name='<?= json_encode($IsEdit ? $edit_arr['Main']['branch_name'] : (empty(smc_session()['user_accounts']['branch_id']) ? smc_session()['default_branch_name'] : smc_session()['user_accounts']['branch_name'])) ?>'/>
                    </div>
                    
                    <div   class="form-group  col-md-12 col-xs-12" style="display: flex;">
                        <div   class="form-group <?php echo smc_session()['default_setting']['activate_accounting_job'] == 1 ? '' : 'hidden' ?>" 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="<?php echo smc_session()['default_setting']['activate_project'] == 1 ? '' : 'hidden' ?>" style="width: 50%;" >
                            <label><?php echo CommonLanguage::getMessage('project') ?></label>
                            <input type="hidden" class="full-width" id="project_id" value="<?php echo ($IsEdit && !empty($edit_arr['Main']['project_id'])) ? $edit_arr['Main']['project_id'] : '' ?>" data-select-name="<?= $IsEdit ? $edit_arr['Main']['project_name'] : '' ?>"/>
                        </div>

                    </div>
                    <div class="col-md-12 col-xs-12 m-b-10">
                        <div class="form-group" >
                            <label><?php echo CommonLanguage::getMessage('amount') ?><span class="red-star">*</span></label>
                            <input  type="text" <?php echo $IsEdit ? '' : 'required' ?> id="Note_Amount" name="Note_Amount" class="form-control Amount inputvalue" value="<?php echo $IsEdit ? $edit_arr['Main']['Note_Amount'] : '' ?>" placeholder="<?php print CommonLanguage::getMessage('amount'); ?>" >
                        </div>
                    </div>
                </div>
            </div>
            <br/>

            <div class="row">
                <div class=" col-md-6 col-xs-6" >
                <span class="red-star">*</span>    <textarea  id="Note_Description" name="Note_Description" rows="9" required="" class=" inputvalue" required placeholder="<?php print CommonLanguage::getMessage('enter_description'); ?>"  ><?php echo $IsEdit ? $edit_arr['Main']['Note_Description'] : '' ?></textarea>
                </div>
                <div class="col-xs-6 col-sm-6  col-middle  clearfix <?php echo ($permissions->hasPermission($IsVendor ? ($is_actual ? 'act_debit_show_pay_box' : 'debit_show_pay_box') : ($is_actual ? 'act_credit_show_pay_box' : 'credit_show_pay_box')) ? '' : 'hidden') ?>" style="border: 1px solid #4c0870;padding-left: 6px;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">
                                                        <!--                                                                    <div class="input-group-btn DropDownForForms Closable">-->
                                                        <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'] : "" ?>' />

                                                        <!--                                                                        <button type="button" class="btn btn-default btn-block dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
        <span><?php echo CommonLanguage::getMessage('select'); ?></span>
        <input type="hidden" value="0" class="form-control" onkeyup="return false" onkeydown="return false"/>
        </button>
        <ul class="dropdown-menu">
                                                        <?php for ($x = 0; $x < count($Checks); $x++) { ?>
                                                                                                                                                                                                                                                                        <li value="<?php echo $Checks[$x]["Account_Code"]; ?>"><a><?php echo $Checks[$x]["Payment_Type_Name"]; ?></a></li>
                                                        <?php } ?>
        </ul>-->
                                                        <!--                                                                    </div>-->
                                                    </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" /></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'] : "" ?>' />

                                                        <!--                                                                        <button type="button" class="btn btn-default btn-block dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
        <span><?php echo CommonLanguage::getMessage('select'); ?></span>
        <input type="hidden" value="0" class="form-control" onkeyup="return false" onkeydown="return false"/>
        </button>
        <ul class="dropdown-menu">
                                                        <?php for ($x = 0; $x < count($CreditCards); $x++) { ?>
                                                                                                                                                                                                                                                                        <li value="<?php echo $CreditCards[$x]["Account_Code"]; ?>"><a><?php echo $CreditCards[$x]["Payment_Type_Name"]; ?></a></li>
                                                        <?php } ?>
        </ul>-->
                                                        <!--                                                                    </div>-->
                                                    </td>
                                                </tr>
        <!--                                                            <tr>
                                                    <td><span class="SpanInTableInPayment"><?php print CommonLanguage::getMessage('approval_code'); ?></span></td>
                                                    <td style="text-align:center"><input type="text" class="form-control CardApproval" /></td>
                                                </tr>-->
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div >
                    <div class="container-sm-height" >
                        <button class=" btn btn-complete btn-cons m-b-10 pull-right " id="ReturnSubmitButton" 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>
                    </div>
                </div>
            </div>
            <br/>
            <br/>
            <br/>
            <div class="row">
                <div class="col-md-12">
                </div>
            </div>
        </div>

    </div>

</div>
<div class="modal fade" data-backdrop="static" id="invoice_modal" data-currency ="<?php echo ($IsEdit) ? $edit_arr['Main']['Currency_ID'] : '' ?> " data-customer ="<?php echo ($IsEdit) ? $edit_arr['Person']['Account_ID'] : '' ?> "  tabindex="-1" role="dialog" aria-hidden="true"style="width:80%;margin:0 auto">
    <div class="modal-dialog ">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" id="close_modal" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i></button>
                <h5><span class="semi-bold"><?php print CommonLanguage::getMessage('invoices'); ?></span></h5>
            </div>
            <div class="modal-body form-group" >
                <div id="InvoicesModalAlert"></div>
                <div class="row row_head" style="border-bottom: 1px solid #eb9220;">
                    <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('invoice') ?>&nbsp;#</label></div>
                    <div class="col-md-1"><label><?php echo CommonLanguage::getMessage('customer') ?></label></div>
                    <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('Date') ?></label></div>
                    <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('net_total') ?></label></div>
                    <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('remaining') ?></label></div>
                    <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('amount') ?></label></div>
                    <div class="col-md-2"><label><?php echo CommonLanguage::getMessage('Balance') ?></label></div>

                </div>
                <div id="inv_body">

                    <?php
                    if (($IsEdit)) {
                        foreach ($edit_arr['pay_inv'] as $inv_pay) {
                            ?>
                            <div class="row m-b-10 my_tr" data-id="<?php echo $inv_pay['Invoice_ID_Auto'] ?>" data-cur = "<?php echo $inv_pay['Invoice_Currency'] ?>">
                                <div class="col-md-1"><?php echo $inv_pay['Invoice_ID'] ?></div>
                                <div class="col-md-1"><?php echo $inv_pay['Customer_Name'] ?></div>
                                <div class="col-md-2"><?php echo $inv_pay['Invoice_Date'] ?></div>
                                <div class="col-md-2"><?php echo CommonFunctions::public_round_number($inv_pay['Invoice_Net_Total']) . ' ' . $edit_arr['Main']['Currency_Symbol'] ?></div>
                                <div class="col-md-2 rem" ><?php echo CommonFunctions::public_round_number(($inv_pay['Remaining_Amount'] + $inv_pay['inv_pay_amount'])) . ' ' . $edit_arr['Main']['Currency_Symbol'] ?></div>
                                <div class="col-md-2"><input type="text" class="form-control" value="<?php echo $inv_pay['inv_pay_amount'] > 0 ? $inv_pay['inv_pay_amount'] : '' ?>" data-max="<?php echo $inv_pay['Remaining_Amount'] + $inv_pay['inv_pay_amount'] ?>" /></div>
                                <div class="col-md-2 balance" ><?php echo CommonFunctions::public_round_number($inv_pay['Remaining_Amount']) . ' ' . $edit_arr['Main']['Currency_Symbol'] ?></div>
                            </div>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
            <div class="modal-footer">
                <div class="">
                    <button class="btn btn-complete pull-right" id="CloseInv" data-dismiss="modal"  > <?php print CommonLanguage::getMessage('continue'); ?> </button>
                    <button class="btn bg-danger white pull-left" id="ClearInv"  > <?php print CommonLanguage::getMessage('clear'); ?>   </button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var Required_Fields = <?php echo json_encode(CommonLanguage::getMessage('Required_Fields')); ?>,
            CompanyLang = <?php echo json_encode(CommonLanguage::getMessage('company')); ?>,
            EmailLang = <?php echo json_encode(CommonLanguage::getMessage('Email')); ?>,
            PhoneLang = <?php echo json_encode(CommonLanguage::getMessage('Phone')); ?>,
            AddressLang = <?php echo json_encode(CommonLanguage::getMessage('address')); ?>,
            CountryLang = <?php echo json_encode(CommonLanguage::getMessage('country')); ?>,
            FinancialNbLang = <?php echo json_encode(CommonLanguage::getMessage('financial_nb')); ?>,
            Failed_Submit = <?php echo json_encode(CommonLanguage::getMessage('Failed_Submit')); ?>,
            Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully')); ?>;
    var IsVendor = <?php echo $IsVendor ? "true" : "false"; ?>,
            IsEditMode = <?php echo $IsEdit ? "true" : "false"; ?>,
            ID_Edit = IsEditMode ? <?php echo json_encode($id); ?> : '',
            local_currency = <?php echo json_encode(smc_session()['local_currency']); ?>,
            second_currency = <?php echo json_encode(smc_session()['default_setting']['second_currency']); ?>,
            is_actual = <?php echo $is_actual; ?>,
            unique_id = <?php echo json_encode(!$IsEdit ? $unique_id : '') ?>;
    $(function () {
        build_select2($("#search_name"), (IsVendor ? "xhr/supplierSearch" : "xhr/customerSearch") + (IsEditMode ? "?is_edited=" + $("#search_name").val() : "?Status_ID=1"));
        build_select2($("#Account_Code"), ("xhr/accIDSearch?type=Payment"))
//        build_select2($("#Account_Code"), ("xhr/accIDSearch?type=" + (IsVendor ? 'purchase' : 'sales')))
        build_select2($("#branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches")

        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($("#project_id"), "ACCAjax/projectSearch")

        $("#ClearInv").on("click", function () {
            $("#inv_body input").val("").trigger("keyup")
        })
        $("#get_inv").on("click", function () {
            $("#ReturnItemSuccess").addClass("hidden")
            if ($("#Finance_Account").attr('data-id') == '' || $("#CurrencyDropDown button input").val() == '' || $("#CurrencyDropDown button input").val() == undefined || $("#CurrencyDropDown button input").val() == 0) {
                $("#ReturnItemSuccess").attr("class", "alert alert-danger").html('<?php echo CommonLanguage::getMessage('Required_Fields') ?>')
                return false;
            }
//            console.log($("#invoice_modal").attr("data-customer").trim()+'==='+$("#Finance_Account").attr('data-id').trim())
//            console.log(($("#invoice_modal").attr("data-customer") != "" && $("#invoice_modal").attr("data-customer").trim() == $("#Finance_Account").attr('data-id').trim()))
//            console.log(($("#invoice_modal").attr("data-currency") != "" && $("#invoice_modal").attr("data-currency").trim() == $("#CurrencyDropDown button input").val().trim()))
            if (($("#invoice_modal").attr("data-customer") != "" && $("#invoice_modal").attr("data-customer").trim() == $("#Finance_Account").attr('data-id').trim())
                    && ($("#invoice_modal").attr("data-currency") != "" && $("#invoice_modal").attr("data-currency").trim() == $("#CurrencyDropDown button input").val().trim())) {
                $("#invoice_modal").modal("show")
//                                alert()
                return false
            }
            //            alert()
            $("#InvoicesTotal").html('0')
            $("#invoice_modal").attr("data-customer", $("#Finance_Account").attr('data-id'))
            $("#invoice_modal").attr("data-currency", $("#CurrencyDropDown button input").val())
            $.ajax({
                url: "ACCAjax/getUnpaidInvoicesByCustomer",
                method: "GET",
                data: {account: $("#Finance_Account").attr('data-id'), currency: $("#CurrencyDropDown button input").val(), is_actual: (is_actual ? 1 : 0), is_receipt: (IsVendor ? 0 : 1), type: (IsVendor ? 'deb' : 'cre')},
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == 'success') {
                        $("#inv_body").empty()
                        var data = v1.responseJSON;
                        //inv_body
                        var TR = '';
                        var symbol = $("#CurrencyDropDown ul li[value=" + $("#CurrencyDropDown button input").val() + "]").attr("mysymbol");
                        for (var i = 0; i < data.length; i++) {
                            TR += '<div class="row m-b-10 my_tr" data-id="' + data[i]['Invoice_ID_Auto'] + '" data-cur = "' + data[i]['Invoice_Currency'] + '">' +
                                    '<div class="col-md-1">' + data[i]['Invoice_ID'] + '</div>' +
                                    '<div class="col-md-1">' + data[i]['Customer_Name'] + '</div>' +
                                    '<div class="col-md-2">' + data[i]['Invoice_Date'] + '</div>' +
                                    '<div class="col-md-2">' + AddCommas(public_round_number(parseFloat(data[i]['Invoice_Net_Total']))) + ' ' + symbol + '</div>' +
                                    '<div class="col-md-2 rem" >' + AddCommas(public_round_number(parseFloat(data[i]['Remaining_Amount']))) + ' ' + symbol + '</div>' +
                                    '<div class="col-md-2"><input type="text" class="form-control" data-max="' + data[i]['Remaining_Amount'] + '" /></div>' +
                                    '<div class="col-md-2 balance" >' + AddCommas(public_round_number(parseFloat(data[i]['Remaining_Amount']))) + ' ' + symbol + '</div></div>';
                        }
                        TR = $(TR)
                        $("#inv_body").append(TR)

                        $("#invoice_modal").modal("show")

                    }
                }
            })
        })

        $("#inv_body").on("click", ".rem", function () {
            var input = $(this).parents(".my_tr").find("input");
            input.val(input.attr("data-max")).trigger("keyup")
        })
        $("#inv_body").on("keyup", "input", function () {
            var symbol = $("#CurrencyDropDown ul li[value=" + $("#CurrencyDropDown button input").val() + "]").attr("mysymbol");
            var amount = parseFloat($(this).val()) || 0,
                    max_amount = parseFloat($(this).attr("data-max"));
            $(this).parents(".my_tr").find(".balance").html(AddCommas(public_round_number(parseFloat(((max_amount - amount) || 0)))) + ' ' + symbol)

            if (amount > max_amount || amount < 0) {
                $(this).addClass("wrong")

            } else {
                $(this).removeClass("wrong")
            }
            var tr_total = 0, validate = true;
            $("#inv_body input").each(function () {
                tr_total += parseFloat($(this).val()) || 0;
                if ($(this).hasClass("wrong"))
                    validate = false;
            })
            if (!validate) {
                $("#CloseInv").attr("disabled", "disabled")
                $("#close_modal").addClass("hidden")
            } else {
                $("#CloseInv").removeAttr("disabled")
                $("#close_modal").removeClass("hidden")


            }
            $("#InvoicesTotal").html(AddCommas(public_round_number(parseFloat(tr_total))))
        })



        $("#InvoicePaymentSetup .InputSpinner .btn").on('click', function (e) {
            e.preventDefault();
            var Input = $("input", $(this).parent().parent()), V = parseInt(Input.val())
            Input.val($(this).hasClass("more") ? V + 1 : V == 0 ? 0 : V - 1)
            UpdatePurchaseInvoiceTotalPaid()
        })
        $("#InvoicePaymentSetup").find("input.form-control").on("keyup", function () {
            UpdatePurchaseInvoiceTotalPaid()
        })
        $("#search_name").on("change", function () {

            //
            $("#SupplierInfoContent").removeClass('hidden')
            var Data = {
                id: $(this).val(),
                mode: !IsVendor,
                is_actual: is_actual
            }

            $.ajax({
                url: "ACCAjax/addDebitNote",
                data: Data,
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        var Data = (v1.responseJSON);
                        if (!IsVendor) {
                            $("#SupplierInfoContent").empty();
                            var Content = "";
                            Content += "<strong><span class='Name' id='Customer_Name' data-id='" + Data['Name'] + "'>" + Data['Name'] + "</span></strong>";
                            Content += "<div><strong>" + CompanyLang + ": </strong><span class='Company'>" + Data['Company'] + "</span></div>";
                            Content += "<div><strong>" + EmailLang + ": </strong><span class='Email'>" + Data['Email'] + "</span></div>";
                            Content += "<div><strong>" + PhoneLang + ": </strong><span class='Phone'>" + Data['Phone'] + "</span></div>";
                            Content += "<div><strong>" + AddressLang + ": </strong><span class='Address'>" + Data['Address'] + "</span></div>";
                            Content += "<div><strong>" + CountryLang + ": </strong>" + Data['Country_Name'] + "</div>";
                            Content += "<div id='Finance_Account' data-id='" + Data['Account_ID'] + "'><strong>" + FinancialNbLang + ": </strong>" + Data['Account_Code'] + "</div>";
                            $("#SupplierInfoContent").append(Content)
                            $("#Supplier_ID-error").remove()
                            $("#Supplier_ID").removeClass("error").val(Data['Customer_ID']);

                            $("#balance").empty().html(AddCommas(public_round_number(parseFloat((Data['Customer_Balance']) + ""))))

                        } else {
                            $("#SupplierInfoContent").empty();
                            var Content = "";
                            Content += "<strong><span class='Name' id='Supplier_Name' data-id='" + Data['Name'] + "'>" + Data['Name'] + "</span></strong>";
                            Content += "<div><strong>" + CompanyLang + ": </strong><span class='Company'>" + Data['Company'] + "</span></div>";
                            Content += "<div><strong>" + EmailLang + ": </strong><span class='Email'>" + Data['Email'] + "</span></div>";
                            Content += "<div><strong>" + PhoneLang + ": </strong><span class='Phone'>" + Data['Phone'] + "</span></div>";
                            Content += "<div><strong>" + AddressLang + ": </strong><span class='Address'>" + Data['Address'] + "</span></div>";
                            Content += "<div><strong>" + CountryLang + ": </strong>" + Data['Country_Name'] + "</div>";
                            Content += "<div id='Finance_Account' data-id='" + Data['Account_ID'] + "'><strong>" + FinancialNbLang + ": </strong>" + Data['Account_Code'] + "</div>";
                            $("#SupplierInfoContent").html(Content)
                            $("#Supplier_ID-error").remove()
                            $("#Supplier_ID").removeClass("error").val(Data['Supplier_ID']);
                            $("#balance").empty().html(AddCommas(public_round_number(parseFloat((Data['Customer_Balance']) + ""))))


                        }
                    }
                }
            })

        })
        $("#Note_Date").datepicker({autoclose: true});

        $("#CurrencyDropDown li").click(function () {
            $(".CurrencyIndicator").html($(this).attr("MySymbol"))
            if ($(this).attr("value") == local_currency) {
                $("#current_rate").val(1).attr("disabled", "disabled")
            } else
                $("#current_rate").val("").removeAttr("disabled")
            if ($(this).attr("value") == second_currency) {
                $("#second_currency_rate").val(1).attr("disabled", "disabled")
            } else
                $("#second_currency_rate").val("").removeAttr("disabled")
            //

        })

        $("#ReturnSubmitButton").on("click", function () {
            $("#ReturnSubmitButton").attr("disabled", "disabled")
            if ($("#Note_Amount").val() == "" || ($("#Note_Amount").hasClass("Amount") && ($("#Note_Amount").val()).match(/^\s*?([\d]+(\.\d+)?|\.\d{1,2})\s*$/) == null)
                    || $("#Note_Description").val() == '' || $("#search_name").val() == ''
                    || $("#CurrencyDropDown button input").val() == '') {
                $("#ReturnItemSuccess").attr("class", "alert alert-danger").empty().html(Required_Fields);
                $("#ReturnSubmitButton").removeAttr("disabled")
                $(document.body).animate({scrollTop: 0})
                return false;
            }
            var total = 0;
            $("#inv_body input").each(function () {
                total += parseFloat($(this).val()) || 0;
            })
//            console.log(total + '====' + parseFloat($("#Note_Amount").val()))
            if (total > parseFloat($("#Note_Amount").val())) {
                $("#ReturnItemSuccess").attr("class", " alert alert-danger").empty().html('<?php echo CommonLanguage::getMessage('check_pay_amounts') ?>')
                $("#ReturnSubmitButton").removeAttr("disabled")
                $(document.body).animate({scrollTop: 0}, 800)
                return false;
            }
            var Data = {
                Supplier_Customer_ID: $("#search_name").val(),
                Account_Code: $("#Finance_Account").attr('data-id'),
                Note_Date: $("#Note_Date").val(),
                Currency_ID: $("#CurrencyDropDown  input").val(),
                Note_Amount: $("#Note_Amount").val(),
                Note_Description: $("#Note_Description").val(),
                Is_Debit_Note: IsVendor ? 1 : 0,
                Cash_Amount: $("#SetPaidFormTab_1 .InputSpinner > input").eq(0).val(),
                Cash_Account: $("#cash_account").val(),
                Check_Amount: $("#SetPaidFormTab_2 .InputSpinner > input").eq(0).val(),
                Check_Account: $("#check_account").val(),
                Card_Amount: $("#SetPaidFormTab_3 .InputSpinner > input").eq(0).val(),
                Card_Account: $("#credit_account").val(),
                Check_Num: $("#SetPaidFormTab_2 .CheckNumValue").eq(0).val(),
                Card_Code: $("#SetPaidFormTab_3 .CardApproval").eq(0).val(),
                is_actual: is_actual,
                branch_id: $("#branch_id").val(),
                job_id: $("#job_id").val(),
                project_id: $("#project_id").val(),
                sales_purchase_account: $("#Account_Code").val(),
                second_currency_rate: $("#second_currency_rate").val(),
                current_rate: $("#current_rate").val(),
                unique_id: unique_id,
                close_arr: []
            };
            $(".my_tr").each(function () {
                var tr = $(this);
                if (tr.find("input").val() != '' && tr.find("input").val() != 0) {
                    Data.close_arr.push({
                        inv_id: tr.attr("data-id"),
                        inv_amount: tr.find("input").val(),
                        inv_currency: tr.attr("data-cur")
                    })
                }

            })
//            console.log(Data)
//            return;
            if (IsEditMode) {
                Data.Note_ID = ID_Edit;
            }
            $.ajax({
                url: IsEditMode ? "ACCAjax/editDebitNote" : "ACCAjax/addDebitNote",
                method: "POST",
                data: Data,
                complete: function (v, vv) {
                    if (vv == "success") {
                        if (v.responseText === "OK") {

                            $("#ReturnItemSuccess").attr("class", "alert alert-success").empty().html(Submitted_Successfully).show();

                        } else {
                            $("#ReturnItemSuccess").attr("class", "alert alert-danger").empty().html(v.responseText).show();
                            $("#ReturnSubmitButton").removeAttr("disabled")
                        }
                    } else {
                        $("#ReturnItemSuccess").attr("class", "alert alert-danger").empty().html(Failed_Submit).show();
                        $("#ReturnSubmitButton").removeAttr("disabled")

                    }
                    $(document.body).animate({scrollTop: 0})
                }
            })
        })
    })
//    function ReturnUpdateSubmitButtonStatus() {
//        var IsValidated = true;
//        if ($("#Note_Amount").val() == "" || ($("#Note_Amount").hasClass("Amount") && ($("#Note_Amount").val()).match(/^\s*?([\d]+(\.\d+)?|\.\d{1,2})\s*$/) == null) || $("#Note_Description").val() == '') {
//            IsValidated = false;
//        }
//        if (!IsValidated) {
//            $("#ReturnSubmitButton").addClass("disabled")
//        } else {
//            $("#ReturnSubmitButton").removeClass("disabled")
//        }
//    }
    function InputMustBeAtleastN(who, N) {
        if (parseInt(who.value) < N || !parseInt(who.value))
            who.value = N;
        UpdatePurchaseInvoiceTotalPaid();
    }
    function UpdatePurchaseInvoiceTotalPaid() {
        var A1 = parseFloat($("#SetPaidFormTab_1 .InputSpinner > input").eq(0).val()) || 0,
                A2 = parseFloat($("#SetPaidFormTab_2 .InputSpinner > input").eq(0).val()) || 0,
                A3 = parseFloat($("#SetPaidFormTab_3 .InputSpinner > input").eq(0).val()) || 0;
        $("#PurchaseInvoiceTotalPaid").text(A1 + A2 + A3);
    }



</script>
