<script src="<?php echo site_url(); ?>assets/js/plugin/datatables1_10_7/jquery.dataTables.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.colReorder.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.colVis.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.tableTools.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script src="<?php echo site_url(); ?>assets/js/plugin/datatable-responsive/datatables.responsive.min.js"></script>

<style>
    body {
        background: rgb(204, 204, 204);
    }

    page {
        background: white;
        display: block;
        margin: 0 auto;
        margin-bottom: 0.5cm;
        box-shadow: 0 0 0.5cm rgba(0, 0, 0, 0.5);
    }

    page[size="A4"] {
        width: 21cm;
        height: 29.7cm;
        padding: 25px;
        padding-top: 20px;
        overflow-y: scroll;
    }

    .col {
        text-align: center;
    }

    table.table.table-condensed tr td:nth-child(2) {
        font-weight: bold;
    }

    table.table.table-condensed td {
        border-top: 0px !Important;
        padding: 0px;
    }

    #invoice_table {
        text-align: right;
    }

    #invoice_table tfoot td:nth-child(2) {
        text-align: left;
        font-weight: bold;
    }

    #invoice_table tfoot td:nth-child(3) {
        font-weight: bold;
    }

    #invoice_table tfoot tr:nth-child(1) td {
        border-width: 2px !important;
        border-color: #b5b5b5;
    }

    #invoice_table tfoot tr:nth-last-child(1) td:nth-child(3),
    #invoice_table tfoot tr:nth-last-child(1) td:nth-child(2) {
        background: #f1f1f1;
    }

    #invoice_table,
    #payment_table {
        border: 1px solid #dedede;
        text-align: right !important;
    }

    #payment_table tfoot {
        font-weight: bold;
    }

    #payment_table tfoot tr td:nth-child(3),
    #payment_table tfoot tr td:nth-child(2) {
        background: #f1f1f1;
    }
</style>
<!-- MAIN PANEL -->
<div id="main" role="main">

    <!-- MAIN CONTENT -->
    <div id="content">
        <page size="A4">
            <form action="<?php echo site_url(); ?>admin/invoices/createInvoice" method="POST" id="createInvoiceForm" autocomplete="OFF">
                <div class="row">
                    <div class="col col-lg-12 col-lg-12 col-sm-12">
                        <img src="https://www.callup2.co/assets/img/logo_he.jpg" width="400" />
                    </div>
                    <div class="col col-lg-12 col-md-12 col-sm-12">
                        <p>
                            <b>
                                אנחנו איתך בע''מ | עוסק מורשה מספר 515319804
                            </b>
                        </p>
                        <p>
                            <i class="fa fa-home"></i>
                            שועפט, ירושלים 97500
                            <i class="fa fa-phone"></i>
                            052-2222237
                        </p>
                        <hr />
                    </div>
                    <div class="col col-lg-4 col-md-12 col-sm-12"></div>
                    <div class="col col-lg-4 col-md-12 col-sm-12">
                        <h2>
                            מסוף
                        </h2>
                        <div class="container_inner" style="width: 100%; margin: auto; margin-bottom: 30px; ">
                            <select name="main_type" class="select2" id="main_type">
                                <option value="1">wewithu</option>
                                <option value="2">callup</option>
                            </select>
                        </div>
                    </div>
                    <div class="col col-lg-4 col-md-12 col-sm-12"></div>
                    <div class="col col-lg-6 col-md-12 col-sm-12">
                        <h2>
                            <?php echo $this->lang->line('select_document_type'); ?>
                        </h2>
                        <div class="container_inner" style="width: 100%; margin: auto; margin-bottom: 30px; ">
                            <select name="invoice_type" class="select2" id="invoice_type">
                                <option value="1"><?php echo $this->lang->line('invoicing_type'); ?></option>
                                <?php if (empty($payment)) { ?>
                                    <option value="2"><?php echo $this->lang->line('invoicing_tax'); ?></option>
                                    <option value="3"><?php echo $this->lang->line('invoicing_Promotional'); ?></option>
                                <?php }; ?>
                            </select>
                        </div>
                    </div>
                    <div class="col col-lg-6 col-md-12 col-sm-12" id="canceled_document_wrap" style="display:none;">
                        <h2>
                            <?php echo $this->lang->line('select_canceled_document'); ?>
                        </h2>
                        <div class="container_inner" style=" width: 100%; margin: auto; margin-bottom: 30px; ">
                            <select name="canceled_document" class="" id="canceled_document">
                            </select>
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-12 col-sm-12" id="customer_details" style="clear: both;">
                        <table class="table table-condensed">
                            <tbody>
                                <tr>
                                    <td>
                                        <?php echo $this->lang->line('send_to'); ?>
                                    </td>
                                    <td style=" width: 200px; ">
                                        <?php if (!empty($payment) && $payment->detail_id == 0) { ?>
                                            <select name="invoice_for" class="select2" id="invoice_for">
                                                <option value=""><?php echo $this->lang->line('select2'); ?></option>
                                                <option value="temp_customer"><?php echo $this->lang->line('customer_Casual'); ?></option>
                                                <?php /*
            							        <optgroup class="companies_list_from" label="חברות">
                                                    <?php foreach($companies as $company){ ?>
                                                        <option value="<?php echo $company->id; ?>"><?php echo $company->name; ?></option>
                                                    <?php }; ?>
                                                </optgroup>
                                                */ ?>
                                                <optgroup class="companies_list_from" label="<?php echo $this->lang->line('customers'); ?>">
                                                    <?php foreach ($customers as $customer) { ?>
                                                        <option <?php if (!empty($payment) && $payment->customer_id == $customer->customerId) {
                                                                    echo 'selected="selected"';
                                                                }; ?> rel="customer" value="<?php echo $customer->customerId; ?>"><?php echo $customer->customerName; ?></option>
                                                    <?php }; ?>
                                                </optgroup>
                                            </select>
                                        <?php } else if (!empty($payment) && $payment->detail_id > 0) { ?>
                                            <input type="text" value="<?php echo $customerData->customerName; ?>" readonly="readonly" name="invoice_for" class="form-control" placeholder="" />
                                        <?php } else { ?>
                                            <select name="invoice_for" class="select2" id="invoice_for">
                                                <option value=""><?php echo $this->lang->line('select2'); ?></option>
                                                <option value="temp_customer"><?php echo $this->lang->line('customer_Casual'); ?></option>
                                                <?php /*
            							        <optgroup class="companies_list_from" label="חברות">
                                                    <?php foreach($companies as $company){ ?>
                                                        <option value="<?php echo $company->id; ?>"><?php echo $company->name; ?></option>
                                                    <?php }; ?>
                                                </optgroup>
                                                */ ?>
                                                <optgroup class="companies_list_from" label="<?php echo $this->lang->line('customers'); ?>">
                                                    <?php foreach ($customers as $customer) { ?>
                                                        <option <?php if (!empty($payment) && $payment->customer_id == $customer->customerId) {
                                                                    echo 'selected="selected"';
                                                                }; ?> rel="customer" value="<?php echo $customer->customerId; ?>"><?php echo $customer->customerName; ?></option>
                                                    <?php }; ?>
                                                </optgroup>

                                                <optgroup class="companies_list_from" label="<?php echo $this->lang->line('customers_Casuals'); ?>">
                                                    <?php foreach ($temp_clients as $temp_client) { ?>
                                                        <option <?php if (!empty($payment) && $payment->detail_id == $temp_client->detail_id) {
                                                                    echo 'selected="selected"';
                                                                }; ?> rel="temp_customer" value="<?php echo $temp_client->detail_id; ?>"><?php echo $temp_client->customer_name; ?> - <?php echo $temp_client->customer_id_number; ?></option>
                                                    <?php }; ?>
                                                </optgroup>
                                            </select>
                                        <?php }; ?>
                                    </td>
                                </tr>
                                <tr class="main_details">
                                    <td colspan="2" class="details" id="customer_phone_number">
                                        <?php if (!empty($payment)) { ?>
                                            <?php echo $customerData->phoneNumber; ?>
                                        <?php }; ?>
                                    </td>
                                </tr>
                                <tr class="main_details">
                                    <td colspan="2" class="details" id="customer_address">
                                        <?php if (!empty($payment)) { ?>
                                            <?php echo $customerData->address; ?>
                                        <?php }; ?>
                                    </td>
                                </tr>
                                <tr class="main_details">
                                    <td colspan="2" class="details" id="customer_email">
                                        <?php if (!empty($payment)) { ?>
                                            <?php echo $customerData->email; ?>
                                        <?php }; ?>
                                    </td>
                                </tr>
                                <tr class="temp_details">
                                    <td><?php echo $this->lang->line('customer_name'); ?></td>
                                    <td><input type="text" value="" name="customer_name" class="form-control" placeholder="" /></td>
                                </tr>
                                <tr class="temp_details">
                                    <td><?php echo $this->lang->line('ID'); ?></td>
                                    <td> <input type="text" value="" name="customer_id_number" class="form-control" placeholder="" /></td>
                                </tr>
                                <tr class="temp_details">
                                    <td><?php echo $this->lang->line('phone_number'); ?></td>
                                    <td><input type="text" value="" name="customer_phone" class="form-control" placeholder="" /></td>
                                </tr>
                                <tr class="temp_details">
                                    <td><?php echo $this->lang->line('customer_number'); ?></td>
                                    <td><input type="text" value="" name="customer_file_no" class="form-control" placeholder="" /></td>
                                </tr>
                                <tr class="temp_details">
                                    <td><?php echo $this->lang->line('Last_4_digits'); ?></td>
                                    <td><input type="text" value="" name="customer_4_digits" class="form-control" placeholder="" /></td>
                                </tr>
                                <tr class="temp_details">
                                    <td><?php echo $this->lang->line('email'); ?></td>
                                    <td><input type="text" value="" name="customer_email" class="form-control" placeholder="" /></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col-lg-3"></div>
                    <div class="col-lg-3 col-md-12 col-sm-12" id="main_invoice_details">
                        <table class="table table-condensed">
                            <tbody>
                                <tr>
                                    <td><?php echo $this->lang->line('date'); ?></td>
                                    <td><?php echo date('d/m/Y', strtotime('now')); ?></td>
                                </tr>
                                <tr>
                                    <td><?php echo $this->lang->line('customer_number'); ?></td>
                                    <td class="details" id="customerId">
                                        <?php if (!empty($payment)) { ?>
                                            <?php echo $customerData->customerId; ?>
                                        <?php }; ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td><?php echo $this->lang->line('number_deals'); ?></td>
                                    <td class="details" id="customer_inv_id">
                                        <?php if (!empty($payment)) { ?>
                                            <?php echo $customerData->invID; ?>
                                        <?php }; ?>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col col-lg-12 col-md-12 col-sm-12" id="invoice_details">
                        <table class="table" id="invoice_table">
                            <thead>
                                <tr>
                                    <th colspan="3" style=" width: 200px; "><?php echo $this->lang->line('description'); ?></th>
                                    <th><?php echo $this->lang->line('sum'); ?></th>
                                    <th><?php echo $this->lang->line('vat_type'); ?></th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="items">
                                    <td colspan="3" style="width: 60%;">
                                        <input type="text" name="items[item_description][]" class="form-control" value="<?php echo $this->lang->line('Communication_services'); ?>" />
                                    </td>
                                    <td>
                                        <input type="text" value="" name="items[item_price][]" class="item_price form-control" />
                                    </td>
                                    <td>
                                        <label class="select">
                                            <select name="items[invoice_items_type][]" class="invoice_items_type form-control">
                                                <option value="0"><?php echo $this->lang->line('select2'); ?></option>
                                                <option value="1"><?php echo $this->lang->line('includes_vat'); ?></option>
                                                <option value="2"><?php echo $this->lang->line('not_includes_vat'); ?></option>
                                            </select>
                                            <i></i>
                                        </label>
                                    </td>
                                </tr>

                                <tr id="btns_group">
                                    <td colspan="5" style="text-align:center;">
                                        <a href="#0" class="btn btn-success btn-circle" id="add_item">
                                            <i class="fa fa-plus"></i>
                                        </a>
                                    </td>
                                </tr>

                            </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="3"></td>
                                    <td><?php echo $this->lang->line('total_before_vat'); ?></td>
                                    <td><input type="text" value="0" name="total_before_vat" id="total_before_vat" class="form-control" readonly /></td>
                                </tr>
                                <tr>
                                    <td colspan="3"></td>
                                    <td><?php echo $this->lang->line('vat_17'); ?></td>
                                    <td>
                                        <input type="text" value="0" name="vat" id="vat" class="form-control" readonly />
                                    </td>
                                </tr>
                                <tr>
                                    <td colspan="3"></td>
                                    <td><?php echo $this->lang->line('total_shekel'); ?></td>
                                    <td><input type="text" value="0" name="total" id="total" class="form-control" readonly /></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <div class="col col-lg-12 col-md-12 col-sm-12" id="payments_details">
                        <table class="table" id="payment_table">
                            <thead>
                                <th><?php echo $this->lang->line('cashbox'); ?></th>
                                <th><?php echo $this->lang->line('payment_method'); ?></th>
                                <th><?php echo $this->lang->line('date_maturity'); ?></th>
                                <th><?php echo $this->lang->line('bank'); ?></th>
                                <th><?php echo $this->lang->line('branch_4digits'); ?></th>
                                <th><?php echo $this->lang->line('account_validity'); ?></th>
                                <th><?php echo $this->lang->line('auth_number'); ?></th>
                                <th><?php echo $this->lang->line('total'); ?></th>
                            </thead>
                            <tbody>
                                <tr class="payments">
                                    <input type="hidden" value="<?php if (!empty($payment)) {
                                                                    echo $payment->payment_id;
                                                                }; ?>" name="payment_id" />
                                    <td style=" width: 100px; ">
                                        <label class="select">
                                            <select name="payments[payment_box][]" class="payment_box form-control">
                                                <?php foreach ($payment_boxes as $payment_box) { ?>
                                                    <option <?php if (!empty($payment) && $payment->payment_box == $payment_box->box_id) {
                                                                echo 'selected="selected"';
                                                            }; ?> value="<?php echo $payment_box->box_id; ?>"><?php echo $payment_box->name; ?></option>
                                                <?php }; ?>
                                            </select>
                                            <i></i>
                                        </label>
                                    </td>
                                    <td style=" width: 100px; ">
                                        <label class="select">
                                            <select name="payments[payment_type][]" class="payment_type form-control">
                                                <?php foreach ($payment_types as $payment_type) { ?>
                                                    <option <?php if (!empty($payment) && $payment->payment_type == $payment_type->type_id) {
                                                                echo 'selected="selected"';
                                                            }; ?> value="<?php echo $payment_type->type_id; ?>"><?php echo $payment_type->name; ?></option>
                                                <?php }; ?>
                                            </select>
                                            <i></i>
                                        </label>
                                    </td>
                                    <td style=" width: 100px; ">
                                        <input type="text" value="<?php if (!empty($payment)) {
                                                                        echo date('d-m-Y', strtotime($payment->payment_date));
                                                                    } else {
                                                                        echo date('d-m-Y');
                                                                    }; ?>" name="payments[payment_date][]" class="form-control datepicker payment_date" />
                                    </td>
                                    <td><input type="text" value="<?php if (!empty($payment)) {
                                                                        echo $payment->bank_visa;
                                                                    }; ?>" name="payments[bank_visa][]" class="form-control" /></td>
                                    <td><input type="text" value="<?php if (!empty($payment)) {
                                                                        echo $payment->branch_digits;
                                                                    }; ?>" name="payments[branch_digits][]" class="form-control" /></td>
                                    <td><input type="text" value="<?php if (!empty($payment)) {
                                                                        echo $payment->account_card;
                                                                    }; ?>" name="payments[account_card][]" class="form-control" /></td>
                                    <td><input type="text" value="<?php if (!empty($payment)) {
                                                                        echo $payment->reference_id;
                                                                    }; ?>" name="payments[reference_id][]" class="form-control" /></td>
                                    <td><input type="text" value="<?php if (!empty($payment)) {
                                                                        echo $payment->total;
                                                                    }; ?>" name="payments[total_payment][]" class="total_payment form-control" /></td>
                                </tr>

                                <tr id="btns_group_payments">
                                    <td colspan="8" style="text-align:center;">
                                        <a href="#0" class="btn btn-success btn-circle" id="add_payment_line">
                                            <i class="fa fa-plus"></i>
                                        </a>
                                    </td>
                                </tr>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="3"></td>
                                    <td colspan="3"><?php echo $this->lang->line('total_receipt_shekel'); ?></td>
                                    <td colspan="2"><input type="text" value="<?php if (!empty($payment)) {
                                                                                    echo $payment->total;
                                                                                }; ?>" id="total_payments" name="total_payments" class="form-control" readonly /></td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    <div class="col col-lg-12 col-lg-12 col-sm-12">
                        <div class="row">
                            <div class="col-lg-4"></div>
                            <div class="col-lg-4"></div>
                            <div class="col-lg-4">
                                <a href="#0" class="btn btn-success btn-block" id="save_invoice">
                                    <?php echo $this->lang->line('invoicing_save'); ?> 
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </page>
    </div>

</div>

<style>
    #s2id_canceled_document {
        width: 100%;
    }

    tr.temp_details {
        display: none;
    }

    ul.select2-result-sub {
        padding-right: 5px !important;
    }

    .select2-result-sub li {
        border-right: 4px solid #dadada;
    }

    .select2-results li.select2-result-with-children>.select2-result-label {
        font-weight: bold;
        background: #F44336;
        color: white;
    }

    .remove_this_item,
    .remove_this_payment {
        position: absolute;
        right: 0px;
        margin-top: 15px;
    }

    .remove_this_item:active,
    .remove_this_payment:active {
        position: absolute;
        top: auto !important;
        left: auto !important;
        right: 0px !important;
    }
</style>

<script type="text/javascript">
    $(document).ready(function() {

        var total_before_vat = 0;
        var vat = 0;
        var total = 0;

        $('.datepicker').datepicker({
            dateFormat: "dd-mm-yy",
            isRTL: true,
            prevText: '<i class="fa fa-chevron-left"></i>',
            nextText: '<i class="fa fa-chevron-right"></i>',
        });
        $('#add_item').on('click', function() {
            var $table = $('#invoice_table');
            var new_line = $table.find('tr.items').first().clone();
            new_line.find(".item_price").val("");
            new_line.find('.invoice_items_type option[value="0"]').prop('selected', true);
            new_line.find('.invoice_items_type').removeAttr("disabled");
            new_line.append('<a href="#0" class="remove_this_item btn btn-danger btn-xs"><i class="fa fa-times"></i></a>');
            new_line.insertBefore('#btns_group');
        });
        $('#add_payment_line').on('click', function() {
            var $table = $('#payment_table');
            var new_line = $table.find('tr.payments').first().clone();
            new_line.find("input.total_payment").val("");
            new_line.append('<a href="#0" class="remove_this_payment btn btn-danger btn-xs"><i class="fa fa-times"></i></a>');
            new_line.insertBefore('#btns_group_payments');
        });
        $(document).on('change', '.invoice_items_type', function() {

            var type = $(this).val();
            var priceElement = $(this).parent().parent().parent().find('.item_price');
            var price = $(this).parent().parent().parent().find('.item_price').val();
            var total_before_vat = $('#total_before_vat').val();
            var vat = $('#vat').val();
            var total = $('#total').val();
            var sumInvoice = 0;

            $('#total').val("");

            if (type == 1) {
                //Calc Price Before Vat
                total_before_vat_current = Number(price) / 1.17;
                total_before_vat = Number(total_before_vat) + Number(total_before_vat_current);
                priceElement.val(total_before_vat_current.toFixed(2));

                $('.item_price').each(function() {
                    sumInvoice += Number($(this).val());
                });
                $('#total_before_vat').val(Math.round(sumInvoice).toFixed(2));

                //Calc Vat
                vat = Number(sumInvoice) * 0.17;
                $('#vat').val(Math.round(vat).toFixed(2));
                //Calc Total
                total = Number(vat) + Number(sumInvoice);
                $('#total').val(Math.round(total).toFixed(2));
            }

            if (type == 2) {
                //Calc Price Before Vat
                total_before_vat = Number(total_before_vat) + Number(price);
                $('#total_before_vat').val(Math.round(total_before_vat).toFixed(2));

                $('.item_price').each(function() {
                    sumInvoice += Number($(this).val());
                });
                $('#total_before_vat').val(Math.round(sumInvoice).toFixed(2));

                //Calc Vat
                vat = Number(sumInvoice) * 0.17;
                $('#vat').val(Math.round(vat).toFixed(2));
                //Calc Total
                total = Number(vat) + Number(sumInvoice);
                $('#total').val(Math.round(total).toFixed(2));
            }

        });
        $(document).on('change', '.item_price', function() {
            $(this).parent().parent().find('.invoice_items_type option[value="0"]').prop('selected', true);
        });
        $(document).on('click', '.remove_this_item', function() {

            var item_price = $(this).parent().find('.item_price').val();
            var total_before_vat = Number(parseFloat($('#total_before_vat').val())) - Number(item_price);
            var vat = Number(total_before_vat) * 0.17;
            var total = Number(total_before_vat) + Number(vat);

            $('#total_before_vat').val(total_before_vat.toFixed(2));
            $('#vat').val(vat.toFixed(2));
            $('#total').val(total.toFixed(2));

            $(this).parent().remove();
        });
        $(document).on('click', '.remove_this_payment', function() {

            var item_price = $(this).parent().find('.total_payment').val();
            var total = Number($('#total_payments').val() - Number(item_price));
            $('#total_payments').val(total);

            $(this).parent().remove();
        });
        $(document).on('change', '.total_payment', function() {
            var sum = 0;
            $('.total_payment').each(function() {
                sum += Number($(this).val());
                $('#total_payments').val(sum.toFixed(2));
            });
        });
        $('#save_invoice').on('click', function() {

            var invoiceTotal = Number($('#total').val());
            var paymentTotal = Number($('#total_payments').val());
            var invoicefor = $('#invoice_for').val();
            console.log(invoiceTotal);
            console.log(paymentTotal);
            if (invoiceTotal == paymentTotal) {
                $('#createInvoiceForm').submit();
            } else {
                if (!invoicefor || invoicefor == '') {
                    $('#invoice_for').addClass("has-error");
                    $('#s2id_invoice_for').addClass("has-error");
                    $.smallBox({
                        title: "נא לבחור לקוח",
                        content: "נא לבדוק את הלקוח הנבחר",
                        color: "#F44336",
                        iconSmall: "fa fa-thumbs-down bounce animated",
                        timeout: 2500
                    });
                } else {
                    if ($('#invoice_type').val() == 1 || $('#invoice_type').val() == 3) {
                        $('#total').addClass("has-error");
                        $('#total_payments').addClass("has-error");
                        $.smallBox({
                            title: "סכום שגוי",
                            content: "נא לבדוק את הסכומים",
                            color: "#F44336",
                            iconSmall: "fa fa-thumbs-down bounce animated",
                            timeout: 2500
                        });
                    } else {
                        $('#createInvoiceForm').submit();
                    }
                }
            }
        });
        $('#invoice_for').on('change', function() {
            if ($(this).find(':selected').val() != 'temp_customer') {
                var data = {};
                data.customerId = $(this).val();
                data.relation = $('#invoice_for option:selected').attr("rel");
                $('.temp_details').hide();
                $('.main_details').show();
                $.post(root + 'admin/invoices/getCustomerDetailsforInvoice', data, function(d) {
                    if (d.result == true) {
                        $('#customer_phone_number').html(d.phoneNumber);
                        $('#customer_address').html(d.address);
                        $('#customer_email').html(d.email);
                        $('#customerId').html(d.customerId);
                        $('#customer_inv_id').html(d.invID);
                        $('#canceled_document').select2('destroy');
                        $("#canceled_document option").each(function() {
                            $(this).remove();
                        });
                        if ($('#invoice_type').val() == 4) {
                            $.each(d.invoices, function(i) {
                                $('#canceled_document').append('<option value="' + d.invoices[i].invoice_id + '" rel-price="' + d.invoices[i].total + '"> חשבונית מספר: ' + d.invoices[i].invoice_id + ' - סכום: ' + d.invoices[i].total + '₪ </option>');
                            });
                            $('#canceled_document_wrap').show();
                            $('#canceled_document').select2();
                        }
                    } else {
                        $('#customer_phone_number').html("");
                        $('#customer_address').html("");
                        $('#customer_email').html("");
                        $('#customerId').html("");
                        $('#customer_inv_id').html("");
                    }

                }, 'json');
            } else {
                $('.main_details').hide();
                $('.temp_details').show();
                $('#customer_phone_number').html("");
                $('#customer_address').html("");
                $('#customer_email').html("");
                $('#customerId').html("");
                $('#customer_inv_id').html("");
            }
        });
        $(document).on('change', '#canceled_document', function() {
            var val = $('#canceled_document').val();
            var price = $('#canceled_document option:selected').attr("rel-price");
            $('tr.items:first').find('.item_price').val(Number(price));
        });
        $('#invoice_type').on('change', function() {
            var val = $(this).val();
            if (val == 2 || val == 4) {
                $('#payments_details').hide();
            } else {
                $('#payments_details').show();
                $('#canceled_document_wrap').hide();
            }
        });
    });
</script>