{extends file="$layouts_admin"}


{block name="content"}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="row">

<!--
    <div class="col-lg-12">
        <div class="mb-3">
            <label for="exampleInputEmail1">{$_L['Unique Invoice URL']}:</label>
            <input type="text" class="form-control mb-3" id="invoice_public_url" onClick="this.setSelectionRange(0, this.value.length)" value="{$_url}client/iview/{$d['id']}/token_{$d['vtoken']}">
        </div>
    </div>
-->

    <div class="text-center mb-3">
        <div class="btn-group flex-wrap" role="group">

             <div class="btn-group" role="group">
                <a   target="_blank" class="print_ btn btn-dark  btn-sm"><i class="fal fa-print"></i>  <span class="d-none d-md-inline">{$_L['Print']}</span></a>

            </div>

 
        </div>
    </div>


    <div class="col-lg-12"  id="application_ajaxrender" class="cuerpo_contenido">
        <div class="panel" style="box-shadow: none;border:none;">
           <!-- <div class="panel-hdr">
                <h2 class="d-none d-md-inline">
                    Invoice 
                </h2>
                  
                <input type="hidden" name="iid" value="{$d['id']}" id="iid">
                 

                <div class="panel-toolbar">

                </div>

            </div>-->

            <div class="panel-container">
                <div class="panel-content">

                    <div class="invoice">
                        <header class="clearfix">
                            <div class="row">
                                <div class="col-sm-6 mt-md">
                                    <h2 class="h2 mt-none mb-sm text-dark text-bold">
                                        {if $d['type'] == 'Credit Note'}

                                        {{__('CREDIT NOTE')}}

                                        {else}

                                        {$_L['INVOICE']}

                                        {/if}


                                    </h2>
                                    <h4 class="h4 m-none text-dark text-bold">#{$factura_maestra['factura_maestra_id']}</h4>

                                    {if $factura_maestra['estado_pago'] == 1}
                                        <h3 class="alert alert-warning">Earring</h3>
                                    {elseif $factura_maestra['estado_pago'] == 2}
                                        <h3 class="alert alert-success">Paid</h3>                                 
                                    {/if}

 
                                    {if isset($d['title']) && $d['title'] != ''}
                                    <h4>{$d['title']}</h4>
                                    <hr>
                                    {/if}

                                    {if $config['invoice_receipt_number'] eq '1' && $d['receipt_number'] neq ''}
                                    <h4>{$_L['Receipt Number']}: {$d['receipt_number']}</h4>
                                    <hr>
                                    {/if}


                                    {if !empty($config['invoice_items_purchasing'])}

                                    <div class="p-3 rounded-3 mb-4" style="background: #C6DAFF;">



                                        <div class="row">
                                            <div class="col-4">
                                                <h5>Purchase Info</h5>
                                                {if $d['purchase_status']}
                                                <span class="badge bg-info text-white">{$d['purchase_status']}</span>
                                                {/if}
                                            </div>
                                            <div class="col-8 text-end">

                                                <div class="btn-group" role="group" aria-label="Button group with nested dropdown">
                                                    <div class="btn-group" role="group">
                                                        <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                                                            Purchase Status
                                                        </button>
                                                        <ul class="dropdown-menu">
                                                            <li class="dropdown-item set_invoice_purchase_status" data-status="Purchased"><a href="javascript:;">{__('Purchased')}</a></li>
                                                            <li class="dropdown-item set_invoice_purchase_status" data-status="On Hold"><a href="javascript:;">{__('On Hold')}</a></li>
                                                            <li class="dropdown-item set_invoice_purchase_status" data-status="Arrived"><a href="javascript:;">{__('Arrived')}</a></li>
                                                            <li class="dropdown-item set_invoice_purchase_status" data-status="Collected"><a href="javascript:;">{__('Collected')}</a></li>
                                                        </ul>
                                                    </div>
                                                    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#purchaseInfo"><i class="fal fa-plus"></i> {__('Purchase Info')}</button>
                                                </div>


                                            </div>
                                        </div>

                                        {if !empty($d['purchase_id'])}
                                        <p class="mb-1"><strong>Purchase ID:</strong> {$d['purchase_id']}</p>
                                        {/if}

                                        {if !empty($d['purchase_date'])}
                                        <p class="mb-1"><strong>Purchase Date:</strong> {$d['purchase_date']}</p>
                                        {/if}

                                        {if !empty($d['purchase_cost'])}
                                        <p class="mb-1"><strong>Purchase Cost:</strong> {formatCurrency($d['purchase_cost'],$d['currency'])}</p>
                                        {/if}

                                        {if !empty($d['purchase_staff_id']) && !empty($staffs[$d['purchase_staff_id']])}
                                        <p class="mb-1"><strong>Staff:</strong> {$staffs[$d['purchase_staff_id']]->fullname}</p>
                                        {/if}

                                        {if !empty($d['purchase_notes'])}
                                        <p class="mb-1"><strong>Notes:</strong> {$d['purchase_notes']}</p>
                                        {/if}

                                    </div>

                                    {/if}

                                    {if !empty($config['invoice_items_shipping'])}

                                    <div class="p-3 rounded-3 mb-4" style="background: #C6DAFF;">

                                        <div class="row">
                                            <div class="col-3">
                                                <h5>Shipping Info</h5>
                                                {if $d['shipping_status']}
                                                <span class="badge bg-info text-white">{$d['shipping_status']}</span>
                                                {/if}
                                            </div>
                                            <div class="col-9 text-end">

                                                <div class="btn-group" role="group" aria-label="Button group with nested dropdown">
                                                    <div class="btn-group" role="group">
                                                        <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                                                            Shipping Status
                                                        </button>
                                                        <ul class="dropdown-menu">
                                                            {if $d['shipping_status'] !== 'In Progress'}
                                                            <li class="dropdown-item set_invoice_shipping_status" data-status="In Progress"><a href="javascript:;">{__('In Progress')}</a></li>
                                                            {/if}

                                                            {if $d['shipping_status'] !== 'Shipped'}
                                                            <li class="dropdown-item set_invoice_shipping_status" data-status="Shipped"><a href="javascript:;">{__('Shipped')}</a></li>
                                                            {/if}

                                                        </ul>
                                                    </div>
                                                    <a href="{$_url}invoices/delivery-label/{$d['id']}" target="_blank" class="btn btn-primary">{{__('Delivery Label')}}</a>
                                                    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#shippingInfo"><i class="fal fa-plus"></i> {__('Shipping Info')}</button>
                                                </div>

                                            </div>
                                        </div>

                                        {if !empty($d['shipping_date'])}
                                        <p class="mb-1"><strong>Shipping Date:</strong> {$d['shipping_date']}</p>
                                        {/if}

                                        {if !empty($d['shipping_tracking_number'])}
                                        <p class="mb-1"><strong>Tracking Number:</strong> {$d['shipping_tracking_number']}</p>
                                        {/if}

                                        {if !empty($d['shipping_weight'])}
                                        <p class="mb-1"><strong>Weight:</strong> {$d['shipping_weight']}</p>
                                        {/if}

                                        {if !empty($d['shipping_cost'])}
                                        <p class="mb-1"><strong>Shipping Cost:</strong> {$d['shipping_cost']}</p>
                                        {/if}

                                        {if !empty($d['shipping_notes'])}
                                        <p class="mb-1"><strong>Notes:</strong> {$d['shipping_notes']}</p>
                                        {/if}

                                    </div>

                                    {/if}


                                </div>
                                <div class="col-sm-6 text-end mt-md mb-md">

                                    <div class="ib">
                                        <img src="{$app_url}storage/system/{$config['logo_default']}" alt="Logo" style="margin-bottom: 15px; width: 200px;">
                                    </div>

                                    <address class="ib mr-xlg">
                                        <strong>{$config['CompanyName']}</strong>
                                        <br>
                                        {$config['caddress']}

                                        {if isset($config['vat_number']) && ($config['vat_number'] != '')}

                                        <br>
                                        <strong>{sp_config_or_default('label_tax_number',$_L['Vat number'])}:</strong> {$config['vat_number']}

                                        {/if}


                                    </address>

                                </div>
                            </div>
                        </header>
                        <div class="bill-info">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="bill-to">
                                    
                                       <!-- <p class="h5 mb-xs text-dark text-semibold">
                                            <strong>
                                                Invoiced To
                                            </strong>
                                        </p>-->
                                        <address>
                                            <strong>Payment date:</strong>
                                            {$fecha_cobro}
                                            <br>
                                            <strong>Monthly price:</strong>
                                            {$valor_mensual}
                                             
                                        </address>
                                     
                                    </div>
                                </div>
                                
                                <div class="col-md-6">
                                    <div class="bill-data text-end">
                                        <p class="mb-none">
                                            <span class="text-dark">Invoice Date:</span>
                                            <span class="value">{date( $config['df'], strtotime($factura_maestra['fecha']))}</span>
                                        </p>
                                        <p class="mb-none">
                                            <span class="text-dark">Date from:</span>
                                            <span class="value">{date( $config['df'], strtotime($factura_maestra['periodo_desde']))}</span>
                                        </p>
                                        <p class="mb-none">
                                            <span class="text-dark">Date until:</span>
                                            <span class="value">{date( $config['df'], strtotime($factura_maestra['periodo_hasta']))}</span>
                                        </p>
                                        <h2>
                                            
                                            {$_L['Invoice Total']}:
 
                                            {$sys_config['value']}{number_format($factura_maestra['total'], 2, '.', ',')}
                                        
                                        </h2>
                                        
                                    </div>
                                </div>
                            </div>
                        </div>

 

                        <div class="table-responsive">


                            {if $factura_maestra['forma_pago'] == '1'}

                            <table class="table table-bordered invoice-items">
                                <thead>
                                    <tr class="text-dark">
                                        <th id="cell-id" class="text-semibold">#</th>
                                        <th id="cell-id" class="text-semibold">Username</th>
                                        <th id="cell-item" class="text-semibold">Date from</th>
                                        <th class="text-semibold text-center">Date until</th>
                                        <th id="cell-price" class="text-center text-semibold">Final date</th>
                                        <th id="cell-qty" class="text-end text-semibold">Days of period</th>
                                        <th class="text-end">Days to invoice</th>
                                        <th id="cell-total" class="text-end text-semibold">Price of the day</th>
                                        <th id="cell-total" class="text-end text-semibold">Amount to be paid</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    {foreach $detalle_facturacion as $item}
                                    <tr>
                                        <td class="text-start">{$item['detalle_facturacion_id']}</td>
                                        <td class="text-start">{$item['username']}</td>
                                        <td class="text-start">{$item['periodo_desde']}</td>
                                        <td class="text-center">{$item['periodo_hasta']}</td>
                                        <td class="text-center">{$item['fecha_final']}</td>
                                        <td class="text-center">{$item['dias_del_periodo']}</td>
                                        <td class="text-center">{$item['dias_a_facturar']}</td>
                                        <td class="text-center">{$item['valor_dia']}</td>
                                        <td class="text-center">{number_format($item['valor_a_pagar'], 2, '.', ',')}</td>
                                             
                           
                                    </tr>
                                    {/foreach}
                                </tbody>
                            </table>

                            {else}

                             <table class="table table-bordered invoice-items">
                                <thead>
                                    <tr class="text-dark">
                                        <th id="cell-id" class="text-semibold">#</th>
                                        <th id="cell-item" class="text-semibold">Date from</th>
                                        <th class="text-semibold text-center">Date until</th>
                                        <th id="cell-price" class="text-center text-semibold">Final date</th>
                                        <th id="cell-qty" class="text-end text-semibold">Days of period</th>
                                        <th class="text-end">Days to invoice</th>
                                        <th id="cell-total" class="text-end text-semibold">Price of the day</th>
                                        <th id="cell-total" class="text-end text-semibold">Amount to be paid</th>
                                    </tr>
                                </thead>
                                <tbody>

                                    {foreach $detalle_facturacion as $item}
                                    <tr>
                                        <td class="text-start">{$item['detalle_facturacion_id']}</td>
                                        <td class="text-start">{$item['periodo_desde']}</td>
                                        <td class="text-center">{$item['periodo_hasta']}</td>
                                        <td class="text-center">{$item['fecha_final']}</td>
                                        <td class="text-center">{$item['dias_del_periodo']}</td>
                                        <td class="text-center">{$item['dias_a_facturar']}</td>
                                        <td class="text-center">{$item['valor_dia']}</td>
                                        <td class="text-center">{number_format($item['valor_a_pagar'], 2, '.', ',')}</td>
                                             
                           
                                    </tr>
                                    {/foreach}
                                </tbody>
                            </table>

                            {/if}

                        </div>

                        <div class="invoice-summary">
                            <div class="row">
                                <div class="col-md-4 offset-md-8">
                                    <table class="table h5 text-dark">
                                        <tbody>
                                            <tr class="b-top-none">
                                                <td colspan="2">Sub-Total</td>
                                                <td class="text-end">{$sys_config["value"]}{number_format($factura_maestra['sub_total'], 2, '.', ',')}</td>
                                            </tr>
                                            
                                            <tr>
                                                <td colspan="2">{$_L['Total']}</td>
                                                <td class="text-end">{$sys_config["value"]}{number_format($factura_maestra['total'], 2, '.', ',')}</td>
                                            </tr>
                                            
 
                                             
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
 
                </div>
            </div>


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


 
<div id="modal_add_item" class="modal fade" tabindex="-1"  role="dialog" aria-hidden="true">

    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">
                    {$_L['New Document']}
                </h4>
                <button type="button" class="close btn btn-danger" data-bs-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true"><i class="fal fa-times"></i></span>
                </button>
            </div>
            <div class="modal-body">
                <form>
                    <div class="mb-3">
                        <label for="doc_title">{$_L['Title']}</label>
                        <input type="text" class="form-control" id="doc_title" name="doc_title">

                    </div>

                    <div class="checkbox">
                        <label>
                            <input type="checkbox" id="is_global" name="is_global"> {$_L['Available for all Customers']}
                        </label>
                    </div>





                </form>

                <hr>

                <form action="" class="dropzone" id="upload_container">

                    <div class="dz-message">
                        <h3> <i class="fal fa-cloud-upload"></i>  {$_L['Drop File Here']}</h3>
                        <br />
                        <span class="note">{$_L['Click to Upload']}</span>
                    </div>

                </form>
                <hr>
                <h4>{$_L['Server Config']}:</h4>
                <p>{$_L['Upload Maximum Size']}: {$upload_max_size}</p>
                <p>{$_L['POST Maximum Size']}: {$post_max_size}</p>

            </div>
            <div class="modal-footer">
                <input type="hidden" name="file_link" id="file_link" value="">
                <button type="button" data-bs-dismiss="modal" class="btn btn-danger">{$_L['Close']}</button>
                <button type="button" id="btn_add_file" class="btn btn-primary">{$_L['Submit']}</button>
            </div>
        </div>
    </div>


</div>

{if !empty($config['invoice_items_purchasing'])}

<div class="modal fade" id="purchaseInfo" tabindex="-1" aria-labelledby="purchaseInfoLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
           <form method="post" action="{$base_url}invoices/save-purchase-info">
               <div class="modal-header">
                   <h1 class="modal-title fs-5" id="purchaseInfoLabel">Purchase Info</h1>
                   <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
               </div>
               <div class="modal-body">

                   <div class="mb-3">
                       <label for="purchase_id">Purchase ID</label>
                       <input type="text" class="form-control" id="purchase_id" name="purchase_id" value="{$d['purchase_id']}">
                   </div>

                   <div class="row">
                       <div class="col-md-6">
                           <div class="mb-3">
                               <label for="purchase_date">Purchase Date</label>
                               <input type="text" class="form-control" id="purchase_date" datepicker
                               data-date-format="yyyy-mm-dd" data-auto-close="true" name="purchase_date"

                               {if !empty($d['purchase_date'])}
                               value="{$d['purchase_date']}"
                               {else}
                               value="{date('Y-m-d')}"
                               {/if}


                               >
                           </div>
                       </div>
                       <div class="col-md-6">
                           <div class="mb-3">
                               <label for="purchase_cost">Purchase Cost</label>
                               <input type="text" class="form-control" id="purchase_cost" name="purchase_cost" value="{$d['purchase_cost']}">
                           </div>
                       </div>
                   </div>

                   <div class="mb-3">
                       <label for="staff_id">{__('Staff')}</label>
                       <select name="staff_id" id="staff_id" class="form-select">
                           <option value="0">{__('Select Staff')}</option>
                           {foreach $staffs as $staff}
                           <option value="{$staff['id']}"
                           {if $staff['id'] == $d['purchase_staff_id']}
                           selected="selected"
                           {else}
                           {if $staff['id'] == $user['id']}
                           selected="selected"
                           {/if}
                           {/if}
                           >{$staff['fullname']}</option>
                           {/foreach}
                       </select>
                   </div>

                   <div class="mb-3">
                       <label for="purchase_notes">Purchase Notes</label>
                       <textarea class="form-control" id="purchase_notes" rows="5" name="purchase_notes">{$d['purchase_notes']}</textarea>
                   </div>


               </div>
               <div class="modal-footer">
                   <input type="hidden" name="invoice_id" value="{$d['id']}">
                   <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                   <button type="submit" class="btn btn-primary">Save</button>
               </div>
           </form>
       </div>
   </div>
</div>

{/if}

{if !empty($config['invoice_items_shipping'])}

<div class="modal fade" id="shippingInfo" tabindex="-1" aria-labelledby="shippingInfoLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="{$base_url}invoices/save-shipping-info">
                <div class="modal-header">
                    <h1 class="modal-title fs-5" id="shippingInfoLabel">Shipping Info</h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">

                    <div class="mb-3">
                        <label for="shipping_weight">Weight</label>
                        <input type="text" class="form-control" id="shipping_weight" name="shipping_weight" value="{$d['shipping_weight']}">
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="shipping_date">Shipping Date</label>
                                <input type="text" class="form-control" id="shipping_date" datepicker
                                data-date-format="yyyy-mm-dd" data-auto-close="true" name="shipping_date"

                                {if !empty($d['shipping_date'])}
                                value="{$d['shipping_date']}"
                                {else}
                                value="{date('Y-m-d')}"
                                {/if}


                                >
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="mb-3">
                                <label for="shipping_cost">Shipping Cost</label>
                                <input type="text" class="form-control" id="shipping_cost" name="shipping_cost" value="{$d['shipping_cost']}">
                            </div>
                        </div>
                    </div>


                    <div class="mb-3">
                        <label for="shipping_notes">Shipping Notes</label>
                        <textarea class="form-control" id="shipping_notes" rows="5" name="shipping_notes">{$d['shipping_notes']}</textarea>
                    </div>


                </div>
                <div class="modal-footer">
                    <input type="hidden" name="invoice_id" value="{$d['id']}">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

{/if}

 

<input type="hidden" id="_lan_msg_confirm" value="{$_L['are_you_sure']}">
<input type="hidden" id="admin_email" value="{$user->username}">




{/block}

{block name="script"}


<script>
 
 
  
    Dropzone.autoDiscover = false;
    $(document).ready(function () {

 
        $(document).on('click', '.enlaces_pago', function(e){
          e.preventDefault();
          let id = $(this).data('data1');
          let enlace= $("#enlaces_pago"+id).attr("href");
          var aux = document.createElement("input");
          aux.setAttribute("value", enlace);
          document.body.appendChild(aux);
          aux.select();
          document.execCommand("copy");
          document.body.removeChild(aux);
          toastr.success('Copy');
      });

         


    $(".print_").on('click',  function(e){
          e.preventDefault();
         

        window.print();
      });


      var _url = $("#_url").val();

      var $modal = $('#cloudonex_body');


      var sysrender = $modal;

      $('.amount').autoNumeric('init', {

        aSign: '{$config['currency_code']}',
        dGroup: {$config['thousand_separator_placement']},
        aPad: {$config['currency_decimal_digits']},
        pSign: '{$config['currency_symbol_position']}',
        aDec: '{$config['dec_point']}',
        aSep: '{$config['thousands_sep']}',
        vMax: '9999999999999999.00',
        vMin: '-9999999999999999.00'

    });

      var iid = $("#iid").val();
      sysrender.on('click', '#add_payment', function(e){
        e.preventDefault();

        {if $d['type'] === 'Credit Note'}
        let url = base_url + 'transactions/modal-expense/' + iid;
        {else}
        let url = base_url + 'invoices/add-payment/' + iid;
        {/if}

        $.fancybox.open({
            src  :  url,
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $(".datepicker").datepicker();
                    $("#account").select2({

                    });
                    $("#cats").select2({

                    });
                    $("#pmethod").select2({

                    });
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });


    });


    sysrender.on('click', '#mail_invoice_created', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/mail_invoice_/' + iid + '/created',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').redactor();
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });



    });

    sysrender.on('click', '#mail_invoice_reminder', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/mail_invoice_/' + iid + '/reminder',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').redactor();
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });


    });

    sysrender.on('click', '#mail_invoice_overdue', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/mail_invoice_/' + iid + '/overdue',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').redactor();
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });







    });

    sysrender.on('click', '#mail_invoice_confirm', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/mail_invoice_/' + iid + '/confirm',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').redactor();
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });





    });

    sysrender.on('click', '#mail_invoice_refund', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/mail_invoice_/' + iid + '/refund',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').redactor();
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });


    });



    sysrender.on('click', '#sms_invoice_created', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/sms_invoice_/' + iid + '/created',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').countSms('#sms-counter');
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });




    });

    sysrender.on('click', '#sms_invoice_reminder', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/sms_invoice_/' + iid + '/reminder',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').countSms('#sms-counter');
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });



    });

    sysrender.on('click', '#sms_invoice_overdue', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/sms_invoice_/' + iid + '/overdue',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').countSms('#sms-counter');
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });


    });

    sysrender.on('click', '#sms_invoice_confirm', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/sms_invoice_/' + iid + '/confirm',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').countSms('#sms-counter');
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });



    });

    sysrender.on('click', '#sms_invoice_refund', function(e){
        e.preventDefault();
        var iid = $("#iid").val();

        $.fancybox.open({
            src  :  base_url + 'invoices/sms_invoice_/' + iid + '/refund',
            type : 'ajax',
            opts : {
                afterShow : function( instance, current ) {
                    $('#message').countSms('#sms-counter');
                },
                touch: false,
                autoFocus: false,
                keyboard: false,
            },
        });



    });




    $modal.on('click', '#btnModalSMSSend', function(event){

        event.preventDefault();

        $.post(base_url + 'sms/init/send_invoice', {


            message: $('#message').val(),
            to: $("#sms_to").val(),
            from: $("#sms_from").val(),
            invoice_id: $("#smsInvoiceId").val()

        }).done(function (data) {

            toastr.success(data);
        });

    });



    $modal.on('click', '#send', function(e){

        e.preventDefault();

        var attach_pdf = 'No';

        if($("#attach_pdf").prop('checked') == true){
            attach_pdf = 'Yes';
        }



        $.post(base_url + 'invoices/send_email', $('#email_form').serialize()).done(function (data) {
            toastr.success(data);
        });

    });

    $modal.on('click', '#save_payment', function(e){
        e.preventDefault();

        {if $d['type'] === 'Credit Note'}

        let save_payment_url = base_url + 'transactions/expense-post/' + iid;
        {else}
        let save_payment_url = base_url + 'invoices/add-payment-post/' + iid;
        {/if}

        $.post(save_payment_url, $("#form_add_payment").serialize())

        .done(function (data) {


            if ($.isNumeric(data)) {
                location.reload();
            }
            else {
                toastr.error(data);

            }
        });


    });

    $("#mark_paid").click(function (e) {
        e.preventDefault();


        app.confirm($("#_lan_msg_confirm").val(), function(result) {
            if(result){
                var iid = $("#iid").val();
                $.post(  _url + "invoices/markpaid", { iid: iid })
                .done(function( data ) {
                    location.reload();
                });
            }
        });

    });

    

     


    $("#mark_unpaid").click(function (e) {
        e.preventDefault();


        app.confirm($("#_lan_msg_confirm").val(), function(result) {
            if(result){
                var iid = $("#iid").val();
                $.post(  _url + "invoices/markunpaid", { iid: iid })
                .done(function( data ) {
                    location.reload();
                });
            }
        });

    });

    $("#mark_cancelled").click(function (e) {
        e.preventDefault();
        app.confirm($("#_lan_msg_confirm").val(), function(result) {
            if(result){
                var iid = $("#iid").val();
                $.post(  _url + "invoices/markcancelled", { iid: iid })
                .done(function( data ) {
                    location.reload();
                });
            }
        });

    });

    $("#mark_partially_paid").click(function (e) {
        e.preventDefault();
        app.confirm($("#_lan_msg_confirm").val(), function(result) {
            if(result){
                var iid = $("#iid").val();
                $.post(  _url + "invoices/markpartiallypaid", { iid: iid })
                .done(function( data ) {
                    location.reload();
                });
            }
        });

    });



    $modal.on('click', '#send_bcc_to_admin', function(e){

        e.preventDefault();


        $("#bccemail").val($("#admin_email").val());

    });

    $modal.on('hidden.bs.modal', function () {
        location.reload();
    });


    $('[data-bs-toggle="tooltip"]').tooltip();

    var $btn_add_file = $("#btn_add_file");

    var $file_link = $("#file_link");

    var upload_resp;




    var ib_file = new Dropzone("#upload_container",
    {
        url: _url + "documents/upload/",
        maxFiles: 1
    }
    );


    ib_file.on("sending", function() {

        $btn_add_file.prop('disabled', true);

    });

    ib_file.on("success", function(file,response) {

        $btn_add_file.prop('disabled', false);

        upload_resp = response;

        if(upload_resp.success == 'Yes'){

            toastr.success(upload_resp.msg);
            $file_link.val(upload_resp.file);


        }
        else{
            toastr.error(upload_resp.msg);
        }


    });




    var $doc_title = $("#doc_title");


    $btn_add_file.on('click', function(e) {
        e.preventDefault();


        $.post( _url + "documents/post/", { title: $doc_title.val(), file_link: $file_link.val(), rid: iid, rtype: 'invoice' })
        .done(function( data ) {

            if ($.isNumeric(data)) {

                location.reload();

            }

            else {
                toastr.error(data);
            }




        });


    });

    const set_invoice_status = document.querySelectorAll('.set_invoice_status');


    if (set_invoice_status){
        set_invoice_status.forEach((item) => {
            item.addEventListener('click', (e) => {
                e.preventDefault();
                const status = item.getAttribute('data-status');
                const invoice_id = '{$d['id']}';
                app.confirm($("#_lan_msg_confirm").val(), function(result) {
                    if(result){

                        $.post(  base_url + "invoices/set-status", {
                            status: status,
                            invoice_id: invoice_id,
                        })
                        .done(function( data ) {
                            location.reload();
                        });
                    }
                });

            });
        });
    }


    const set_invoice_purchase_status = document.querySelectorAll('.set_invoice_purchase_status');

    if (set_invoice_purchase_status) {
        set_invoice_purchase_status.forEach((item) => {
            item.addEventListener('click', (e) => {
                e.preventDefault();
                const status = item.getAttribute('data-status');
                const invoice_id = '{$d['id']}';
                app.confirm($("#_lan_msg_confirm").val(), function(result) {
                    if(result){

                        $.post(  base_url + "invoices/set-purchase-status", {
                            status: status,
                            invoice_id: invoice_id,
                        })
                        .done(function( data ) {
                            location.reload();
                        });
                    }
                });

            });
        });
    }

    const set_invoice_shipping_status = document.querySelectorAll('.set_invoice_shipping_status');

    if (set_invoice_shipping_status) {
        set_invoice_shipping_status.forEach((item) => {
            item.addEventListener('click', (e) => {
                e.preventDefault();
                const status = item.getAttribute('data-status');
                const invoice_id = '{$d['id']}';
                app.confirm($("#_lan_msg_confirm").val(), function(result) {
                    if(result){

                        $.post(  base_url + "invoices/set-shipping-status", {
                            status: status,
                            invoice_id: invoice_id,
                        })
                        .done(function( data ) {
                            location.reload();
                        });
                    }
                });

            });
        });
    }



});



</script>
{/block}
