<?php $user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}
foreach($editinvoice as $key=>$item){
     $company=$item->company_name;
    $from=$item->bill_from;
    $custom_unite=$item->custom_unit;
    $to=$item->bill_to;
    $id=$item->id;
    $invoice_id=$item->invoice_id;
     $invoice_date=date("Y-m-d",strtotime($item->invoice_date));
    $qty=unserialize($item->invoice_qty_info);
     $unit=unserialize($item->invoice_unit_info);
     $des=unserialize($item->invoice_des_info);
     $amount=unserialize($item->invoice_amount_info);
     $tax=unserialize($item->invoice_tax_info);
    $total=$item->total;
   $from_company=$item->from_company;
    $sub_total=$item->sub_total;
    $condition=$item->condition;
     $paytype=$item->pay_type;
    $invoice_name=$item->invoice_name;
}

?>
<!DOCTYPE html>

<html lang="en">

<?php
$this->load->view('head.php');
?>



<script>
    function tax(taxrate, textname) {
        //var modal = document.getElementById('myModal');
        var current_id = $('#current_item').val();
        $('#invoice_items_attributes_' + current_id + '_tax').text(textname);
        $('#invoice_items_attributes_' + current_id + '_tax_name').val(textname);
        $('#invoice_items_attributes_' + current_id + '_tax_rate').val(taxrate);

        $('#invoice_items_attributes_0_quantity').trigger('change');
        $('.close-window-link').trigger("click");
        if (taxrate) {
            //modal.style.display = "none";
            $('.close-window-link').trigger("click");
        }
        // $("#myModal .close").click();
    }

    function addtax(id) {
        $('#current_item').val(id);
        var text = $('#invoice_items_attributes_' + id + '_tax').text();
        var taxrate = parseFloat($('#invoice_items_attributes_' + id + '_tax_rate').val());

        if (taxrate == 20) {
            $('.tax20').text('Selected');
            $('.tax10').text('Ajoute');
            $('.tax5').text('Ajoute');
        }
        if (taxrate == 10) {
            $('.tax10').text('Selected');
            $('.tax20').text('Ajoute');
            $('.tax5').text('Ajoute');
        }
        if (taxrate == '5.5') {
            $('.tax5').text('Selected');
            $('.tax10').text('Ajoute');
            $('.tax20').text('Ajoute');
        }
        /*else{
        $('.tax5').text('Selected'); 
            $('.tax10').text('Select');
            $('.tax20').text('Select');
        }*/



    }
</script>

<body>
    <div id="wrapper" class="newinvoice">
        <div class="admin-title">
            <div class="row">
                <?php $this->load->view('header.php'); ?>
            </div>
        </div>

        <div class="content-area">

            <div class="row">
             
                    <div class="admin-menu">
                        <?php $this->load->view('sidebar.php'); ?>
                    </div>
              


                          <div class="content-menu">
                            <ul class="nav nav-tabs">

                                <li><a class="btn-menu" href="<?php echo base_url('invoice/details');?>">All Invoice</a></li>
                                <li><a class="btn-menu" href="<?php echo base_url('invoice/create');?>">New Invoice</a></li>

                            </ul>
                          
                        </div>
                     <div class="panel-heading headingnone">
                            <h2 class="transac">Edit Invoice</h2>


                        </div>
                <div class="main-content invoiceform">
                   
                       

                        <div class="page white shadow" style="border-top: 0; padding: 20px 10px;">




                            <div class="page" style="padding: 0 15px; border-top:0; background-color: #FFF5CD;">


                                <form method="post" action="<?php echo site_url('invoice/value'); ?>" onsubmit="return checkForm(this); " target="_blank" enctype="multipart/form-data" accept-charset="utf-8" >
                                <input type="hidden" name="invoice[invoice_type]" id="invoice_type" value="ADVANCED" />

                                <input type="hidden" name="redirect_page" id="redirect_page" value="" />
                                <input type="hidden" name="edit_id" value="<?php echo $id; ?>" />
                                <input type="hidden" name="save_clicked" id="save_clicked" value="" />

                                <input type="hidden" name="ref" id="ref" />

                                <input type="hidden" name="last_updated_item_id" id="last_updated_item_id" value="" />
                                <input type="hidden" name="last_updated_object_id" id="last_updated_object_id" value="" />

                                <br/>

                                <div class="row">


                                    <div class="col-xs-8 col-md-8 form-group">
                                        <label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Nom de l'entreprise</label>
                                        <textarea class="form-control" placeholder="Your Company" rows="5" style="height: 8em;" tabindex="1" maxlength="5000" name="invoice[fromcompany]]" id="invoice_from"><?php echo $from_company; ?>
                                         </textarea>
                                        <label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Adresse</label>
                                        <textarea class="form-control" rows="5" style="height: 8em;" tabindex="1" maxlength="5000" name="invoice[from]" id="invoice_from"> <?php echo $from; ?></textarea>


                                        <label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Nom de l'entreprise</label>
                                        <textarea class="form-control" rows="5" style="height: 5em;" tabindex="1" maxlength="5000" name="invoice[company]"><?php echo $company; ?></textarea>
                                    </div>

                                 <!--   <div class="col-xs-4 col-md-3 col-md-offset-1">
                                        <div id="logo" style="padding: 25px 0;">

                                            <div class="edit_profile">
                                                <h5 style="margin-left: 15px;">Profile picture</h5>
                                                <?php /*$logo='';
    $type=$this->db->query("select logo  from user 
      where user_id=$user_id");
    foreach ($type->result() as $key => $logo)
      $logo=$logo->logo; 
    $defult="logo.jpg";
    if($logo){
      echo '<img src='.base_url().'uploads/'.$logo.' alt="Smiley face" class="profile"  >';
    }
    else{
      echo '<img src='.base_url().'uploads/'.$defult.' alt="Smiley face" class="profile"  >'; 
    }*/
    ?>

                                            </div>
                                            
                                            <input type="button" id="loadimage" value="Upload Photo" onclick="document.getElementById('file').click();" style="    margin-left: 62px;" />
                                            <input type="file" style="display:none;" id="file" name="file" />

                                            <i class="fa fa-times icon-large clear-logo acts-as-link fa-lg red" title="Clear Logo" style="font-size: 1.666em; position: absolute; top: 10px; right: 20px;"></i>


                                        </div>
                                    </div>-->

                                </div>


                                <div class="row">

                                    <div class="col-xs-8 col-md-8 form-group">
                                        <label for="invoice_billing"><i class="fa fa-female icon"></i><i class="fa fa-male icon"></i>&nbsp;&nbsp;Adresse</label>
                                        <textarea class="form-control" rows="5" style="height: 8em;" tabindex="2" maxlength="5000" name="invoice[billing]" id="invoice_billing"><?php echo $to; ?>
    </textarea>
                                    </div>

                                    <div class="col-xs-4 col-md-3 col-md-offset-1 form-group">

                                        <label for="invoice_number"><i class="fa"></i>&nbsp;Type</label>
                                        <input class="form-control" tabindex="4" maxlength="100" size="100" type="text" value="<?php echo $invoice_name; ?>" name="invoice_name" /> <br>



                                        <label for="invoice_number"><!--<i class="fa fa-search icon"></i>-->&nbsp;Numéro</label>
                                        <input class="form-control" value="<?php echo $invoice_id; ?>" tabindex="4" maxlength="100" size="100" type="text" value="101" name="invoice[number]" id="invoice_number" /><br/>

                                        <label for="invoice_date_issued">Date</label>
                                        <input class="form-control" size="16" type="date" id="date" name="date" value="<?php echo $invoice_date; ?>">

                                    </div>

                                </div>




                                <hr/>

                                <div class="row">


                                    <div class="col-xs-2 col-lg-1 form-group">
                                        <b>Qty</b>
                                    </div>

                                    <div class="col-xs-2 col-lg-3 form-group">
                                        <b>Description</b>
                                    </div>

                                    <div class="col-xs-2 col-lg-2 form-group">

                                        <select name="taxunit" class="form-control unitcheck  invoicecalculate" required="" onchange="ttcselect(this)">
       
		    <option value="H.T" <?php if($custom_unite=='H.T') {echo "selected" ;} ?> >Unit H.T</option>
		    <option value="T.T.C" <?php if($custom_unite=='T.T.C') {echo "selected" ;} ?> >Unit T.T.C</option>
		</select>
                                    </div>

                                    <div class="col-xs-2  form-group">
                                        <b>Montant</b>
                                    </div>

                                    <div class="col-xs-1 form-group"></div>

                                    <div class="col-xs-2 form-group text-center">
                                        <b>TVA</b>
                                    </div>

                                </div>



                                <div id="items" class="increase">

                                    <input type="hidden" id="counter" value="<?php echo (count($unit));?>">

                                    <?php for ($q=0;$q<count($unit);$q++) { ?>
                                    <input type="hidden" name="invoice[items_attributes][<?php echo $q; ?>][id]" id="invoice_items_attributes_<?php echo $q; ?>_id" value="" />
                                    <div class="row append" data="<?php echo $q; ?>">

                                        <div class="col-xs-2 col-lg-1 form-group">
                                            <input type="text" name="invoice_item[<?php echo $q; ?>][qty]" id="invoice_items_attributes_<?php echo $q; ?>_quantity" placeholder="Qty" class="form-control quantity qty2 invoicecalculate" value="<?php echo $qty[$q] ;?>" autocomplete="off" />
                                        </div>


                                        <div class="col-xs-2 col-lg-3 form-group">
                                            <textarea name="invoice_item[<?php echo $q; ?>][description]" id="invoice_items_attributes_<?php echo $q; ?>_description" placeholder="Description" class="form-control description" rows="2" tabindex="28304754" maxlength="5000"><?php if($des[$q]!=null) {echo $des[$q] ;}?></textarea>
                                        </div>

                                        <div class="col-xs-2 form-group"> 
                                            <input type="text" name="invoice_item[<?php echo $q; ?>][unitprice]" id="invoice_items_attributes_<?php echo $q; ?>_unitprice" value="<?php echo number_format($unit[$q],2) ;?>" class="form-control unitprice invoicecalculate" autocomplete="off" />
                                        </div>

                                        <div class="col-xs-2 form-group">
                                            <input type="text" name="invoice_item[<?php echo $q; ?>][amount]" id="invoice_items_attributes_<?php echo $q; ?>_amount" value="<?php echo number_format($amount[$q],2) ;?>" class="form-control amount invoicecalculate price2" />
                                        </div>


                                        <div class="col-xs-1 form-group text-center">

                                        </div>


                                        <div class="col-xs-2 form-group">
                                            <input type="hidden" name="invoice_item[<?php echo $q; ?>][taxname]" id="invoice_items_attributes_<?php echo $q; ?>_tax_name" value="" />
                                            <input type="hidden" name="invoice_item[<?php echo $q; ?>][taxrate]" id="invoice_items_attributes_<?php echo $q; ?>_tax_rate" value="<?php echo $tax[$q] ;?>" />





                                            <button type="button" id="invoice_items_attributes_<?php echo $q; ?>_tax" class="btn btn-default btn-gold show-modal tax-rate-name" data-index="0" style="width: 100%;" data-toggle="modal" data-target="#myModal" onclick="addtax(<?php echo $q ;?>)">T.V.A <?php echo $tax[$q] ;?> %</button>
                                        </div>

                                        <a href="javascript:void(0);" class="remove_button"><i class="fa fa-times delete-item acts-as-link red" style="font-size: 1.666em;margin-left: 7px;" title="Delete Item"></i></a>



                                    </div>
                                    <?php }   ?>
                                    <input type="hidden" name="taxrate" id="taxval" />
                                    <input type="hidden" name="subtotal" id="subtotal" value="<?php echo $sub_total; ?>" />
                                    <input type="hidden" name="total" id="total" value="<?php echo $total; ?>" />

                                </div>



                                <div class="row">




                                    <div class="col-xs-12 col-md-12 form-group">
                                        <a href="javascript:void(0);" class="btn btn-default btn-gold" id="add-item" class="add_button" style="width: 100%;"><i class='fa fa-plus'></i>&nbsp;&nbsp;Ajoute</a>
                                        <!--<button name="button" type="button" class="btn btn-default btn-gold" id="add-item" style="width: 100%;"><i class='fa fa-plus'></i>&nbsp;&nbsp;Add New Item</button>-->
                                    </div>


                                </div>
                                <div class="row">







                                </div>

                                <br/>


                                <div id="total">

                                    <div class="row">
                                        <div class="col-xs-3 col-md-3 ">
                                            <label for="invoice_date_issued">Mode Paiment: </label>
                                            <br>
                                            <input type="checkbox" name="type" value="CB" <?php if($paytype=="CB" ) {echo "checked";} ?> >CB<br>
                                            <input type="checkbox" name="type" value="VIREMENT" <?php if($paytype=="VIREMENT" ) {echo "checked";} ?> > VIREMENT<br>
                                            <input type="checkbox" name="type" value="Checque" <?php if($paytype=="Checque" ) {echo "checked";} ?> >CHEQUE<br>
                                            <input type="checkbox" name="type" value="ESPECES" <?php if($paytype=="ESPECES" ) {echo "checked";} ?> > ESPECES<br>
                                            <input type="checkbox" name="type" value="TR" <?php if($paytype=="TR" ) {echo "checked";} ?> > TR
                                            <!--<button name="button" type="button" class="btn btn-default btn-gold" id="add-item" style="width: 100%;"><i class='fa fa-plus'></i>&nbsp;&nbsp;Add New Item</button>-->
                                        </div>
                                        <div class="col-xs-6 col-md-6 form-group">
                                            <div class="col-xs-5"></div>
                                            <div class="col-xs-4">
                                                <h4 class="bold"> Total H.T</h4>
                                            </div>

                                        </div>
                                        <div class="col-xs-3 text-right">
                                            <h4 class="bold subtotal">
                                                <?php echo number_format($sub_total,2); ?>&nbsp;€</h4>
                                        </div>
                                        <!--  <div class="row">
        <div class="col-xs-3" style="margin-left: 206px;"><h4 class="bold">T.V.A <span class="taxrt">10%</span></h4><input type="hidden" name="taxrte[]" value="10"></div>
            <div class="col-xs-3 text-right"><h4 class="bold taxval">2.00 €</h4><input type="hidden" name="taxprice[]" value="2"></div></div>-->
                                    </div>

                                    <div class="appndtax">
                                    </div>

                                    <div class="row">
                                        <div class="col-xs-5">&nbsp;</div>
                                        <div class="col-xs-4">
                                            <h2 class="bold" style="padding-top: 10px;">
                                                Total T.T.C
                                            </h2>
                                        </div>



                                        <input type="hidden" name="invoice[currency_format]" id="invoice_currency_format" value="%s %n" />

                                        <div class="col-xs-3 text-right">
                                            <h2 class="bold" style="padding-top: 10px;">

                                                <a class='currency-formats nolink show-modal-currency-format dotted' title="Change Currency &amp; Formatting" data-toggle="tooltip" data-placement="left">
				<span class="totalrate"><?php echo number_format($total,2); ?>&nbsp;€</span>
			</a>

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

                                    <br>

                                </div>


                                <div class="row">
                                    <div class="col-xs-12 form-group">

                                        <textarea class="form-control" placeholder="Optional" rows="5" style="height: 8em;" tabindex="20" maxlength="5000" name="invoice[footer_1]" id="invoice_footer_1">
<?php echo $condition ; ?></textarea>
                                    </div>
                                </div>


                              

                                <input type="submit" class="btn btn-primary" style="width: 48%" value="Update">
                                &nbsp;
                                <input type="submit" class="btn btn-primary" style="width: 48%" value="Copy" name="copy">
                                <!--<a class="btn btn-primary" style="width: 100%" onclick="pdfupdate()" href="<?php// echo base_url('invoice/pdfview/'.$id.'');?>" target="_blank" >Update</a>-->



                                </form>


                                <div class="modal fade light-blue-modal in" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
                                    <div class="modal-dialog">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close close-big choose-last-tax" data-dismiss="modal" aria-hidden="true">×</button>
                                                <h4 class="modal-title text-center black">Sélectionner une TVA</h4>
                                            </div>
                                            <div class="modal-body">


                                                <div id="tax-rates">




                                                    <br>

                                                    <div class="row bold">


                                                        <div class="col-xs-3"><strong>Name</strong></div>

                                                        <div class="col-xs-3 text-center"><strong>TVA %</strong></div>

                                                        <div class="col-xs-4 text-center">Selected?</div>

                                                        <div class="col-xs-2 text-center"></div>

                                                    </div>

                                                    <br>


                                                    <div class="row row-highlight">

                                                        <a class="choose-tax acts-as-link darkblue bold" data-index="1006019" data-name="Twenty 20.0%">
                                                            <div class="col-xs-3 pad underline">T.V.A</div>
                                                            <div class="col-xs-3 pad text-center">20.0 %</div>
                                                        </a>
                                                        <div class="col-xs-4 pad text-center">
                                                            <a class="choose-second-tax btn btn-info btn-sm tax20" data-index="1006022" data-name="TVA 5.5%" style="    margin-bottom: 5px;" onclick="tax(20,'Twenty 20.0%')">
			Ajoute 
			</a>
                                                        </div>




                                                    </div>


                                                    <div class="row row-highlight">

                                                        <a class="choose-tax acts-as-link darkblue bold" data-index="1006022" data-name="TVA 5.5%">
                                                            <div class="col-xs-3 pad underline">T.V.A</div>
                                                            <div class="col-xs-3 pad text-center">5.5 %</div>
                                                        </a>

                                                        <div class="col-xs-4 pad text-center">
                                                            <a class="choose-second-tax btn btn-info btn-sm tax5" data-index="1006022" data-name="TVA 5.5%" style="    margin-bottom: 5px;" onclick="tax(5.5,'TVA 5.5%')">
			Ajoute 
			</a>
                                                        </div>



                                                    </div>
                                                    <div class="row row-highlight">

                                                        <a class="choose-tax acts-as-link darkblue bold" data-index="1006022" data-name="TVA 10%">
                                                            <div class="col-xs-3 pad underline">T.V.A</div>
                                                            <div class="col-xs-3 pad text-center">10.0 %</div>
                                                        </a>

                                                        <div class="col-xs-4 pad text-center">
                                                            <a class="choose-second-tax btn btn-info btn-sm tax10" data-index="1006022" data-name="TVA 10%" onclick="tax(10,'TVA 10%')">
			Ajoute
			</a>
                                                        </div>



                                                    </div>

                                                    <input id="current_item" value="" type="hidden" autocomplete="off" />

                                                </div>

                                            </div>

                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-link close-window-link" data-dismiss="modal">Close</button>
                                            </div>

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





                                <!-- /.modal -->





                            </div>

                        </div>


                        <br/>




                

                </div>




            </div>

            <script src="https://acme.invoicehome.com/assets/application-479e98a06b335fc94a5abd3ac0f4ffd7ba83a2a85379b8f24a3ff1c7e885a08a.js"></script>

            <script src="https://js.stripe.com/v3/"></script>
        </div>
        <?php $this->load->view('footer.php'); ?>
        <script>
            /*  function ttcselect(that){
                        if(that.value=='Unit T.T.C'){
                            $('#taxunitcheck').fadeOut('slow');
                             $('.taxunitc').fadeOut('slow');
                        }
                        if(that.value=='Unit H.T'){
                            $('#taxunitcheck').fadeIn('slow');
                             $('.taxunitc').fadeIn('slow');
                        }
                    }*/
        </script>
        <script>
            function pdfupdate() {
                $('#my-form').submit();
            }
        </script>
        <script>
            $(document).ready(function() {
                //var html = '</div>';
                //alert('hi');

                var item = $("#counter").val();
                var counter = item;
                $("#add-item").click(function() {

                    //   $(".increase").append(html);


                    var inputitem = '';
                    //$('.append').each(function() {
                    // counter++;
                    // alert(counter);
                    var inputitem = '<div class="row append" data="' + counter + '"><div class="col-xs-2 col-lg-1 form-group"><input type="text" name="invoice_item[' + counter + '][qty]" id="invoice_items_attributes_' + counter + '_quantity"  placeholder="Qty" class="form-control quantity invoicecalculate qty' + counter + '" value="1" /></div><div class="col-xs-2 col-lg-3 form-group"><textarea name="invoice_item[' + counter + '][description]" id="invoice_items_attributes_' + counter + '_description" placeholder="Description" class="form-control description" rows="2" tabindex="28304754" maxlength="5000"></textarea></div><div class="col-xs-2 form-group"><input type="text" name="invoice_item[' + counter + '][unitprice]" id="invoice_items_attributes_' + counter + '_unitprice"  placeholder="Unit Price"  class="form-control unitprice invoicecalculate price' + counter + '" /></div><div class="col-xs-2 form-group"><input type="text" name="invoice_item[' + counter + '][amount]" id="invoice_items_attributes_' + counter + '_amount"  placeholder="Amount"  class="form-control amount invoicecalculate price' + counter + '" /></div><div class="col-xs-1 form-group text-center"></div><div class="col-xs-2 form-group taxunitc"><input type="hidden" name="invoice_item[' + counter + '][taxname]" id="invoice_items_attributes_' + counter + '_tax_name" value="" /><input type="hidden" name="invoice_item[' + counter + '][taxrate]" id="invoice_items_attributes_' + counter + '_tax_rate" value="0" /><button type="button" id="invoice_items_attributes_' + counter + '_tax" class="btn btn-default btn-gold show-modal tax-rate-name", data-index="0" style="width: 100%;" data-toggle="modal"  data-target="#myModal" onclick="addtax(' + counter + ');" >Add TVA</button>  </div><a href="javascript:void(0);" class="remove_button"><i class="fa fa-times delete-item acts-as-link red" style="font-size: 1.666em;margin-left: 7px;" title="Delete Item"></i></a></div>';
                    counter++;
                    /* var newItem = '<li>';
                     newItem += '<a href= "' + $(this).find('a').attr('href') + '" ';
                     newItem += 'data-slide="' + counter + '">';
                     newItem += '<img src="' + $(this).find('img').attr('src') + '" alt="' + $(this).find('img').attr('alt') + '"/>';
                     newItem += '</a></li>';*/


                    //});

                    var unitcheck = $(".unitcheck option:selected").val();

                    $('.increase').append(inputitem);




                });
                //$('.demo').append(html);    


            });
        </script>
        <script>
            $(document).ready(function() {


                calculateInvoice();
                var addButton = $('#add-item');
                var wrapper = $('.increase');

                /* $("#add-item").click(function(){
                    
                      $(".increase").append('<div class="row "><div class="col-xs-2 col-lg-1 form-group"><input type="text" name="invoice[quantity]" id="invoice_items_attributes_0_quantity"  placeholder="Qty" class="form-control quantity qty2" onkeypress="anotherinvoice()" onkeyup="anotherinvoice()"  /></div><div class="col-xs-2 col-lg-3 form-group"><textarea name="des[description]" id="invoice_items_attributes_0_description" placeholder="Description" class="form-control description" rows="2" tabindex="28304754" maxlength="5000"></textarea></div><div class="col-xs-2 form-group"><input type="text" name="invoice[]" id="invoice_items_attributes_0_amount"  placeholder="Amount" onkeypress="anotherinvoice()" onkeyup="anotherinvoice()" class="form-control amount price2"  /></div><div class="col-xs-1 form-group text-center"></div><div class="col-xs-2 form-group"><input type="text" name="tax" class="form-control amount tax2" placeholder="Add Tax" onkeypress="anotherinvoice()" onkeyup="anotherinvoice()"><span class="tax">%</span></div><a href="javascript:void(0);" class="remove_button"><i class="fa fa-times delete-item acts-as-link red" style="font-size: 1.666em;margin-left: 7px;" title="Delete Item"></i></a></div>');
                  });*/


                //Once remove button is clicked
                $(wrapper).on('click', '.remove_button', function(e) {
                    e.preventDefault();
                    $(this).parent('div').remove();
                    calculateInvoice(); //Remove field html
                    x--; //Decrement field counter
                });

                $(document).on('keyup change', '.invoicecalculate', function() {
                    calculateInvoice();
                });

                function calculateInvoice() {

                    var total = 0;
                    var subtotal = 0;
                    var percent = 0;
                    var tax = 0;
                    var ttctotal = 0;
                    var taxArray = {
                        '5.5': 0,
                        '10': 0,
                        '20': 0
                    };
                    var taxaArray = {
                        '1.2': 0,
                        '1.1': 0,
                        '1.055': 0
                    };
                    $('#items > .row').each(function() {
                        var id = $(this).attr('data');

                        var qty = parseFloat($('#invoice_items_attributes_' + id + '_quantity').val());
                        var unitprice = parseFloat($('#invoice_items_attributes_' + id + '_unitprice').val());
                        var taxrate = parseFloat($('#invoice_items_attributes_' + id + '_tax_rate').val());
                        var taxname = $('#invoice_items_attributes_' + id + '_tax_name').val();
                        var unitcheck = $(".unitcheck option:selected").val();

                        if (!isNaN(qty) && !isNaN(unitprice) && unitcheck == 'H.T') {
                            var total_amount = qty * unitprice;
                            $('#invoice_items_attributes_' + id + '_amount').val(total_amount.toFixed(2));

                            var taxamount = 0;
                            if (!isNaN(taxrate) && taxrate > 0) {
                                taxamount = (total_amount * taxrate / 100);
                                tax += taxamount;

                                taxArray[taxrate] += taxamount;
                            }
                            subtotal += total_amount;
                            total = subtotal + tax;
                        }
                        if (!isNaN(qty) && !isNaN(unitprice) && unitcheck == 'T.T.C') {
                            if (taxrate == 20) {
                                taxrate = 1.2;
                            }
                            if (taxrate == 10) {
                                taxrate = 1.1;
                            }
                            if (taxrate == 5.5) {
                                taxrate = 1.055;
                            }
                            var total_amount = qty * unitprice;
                            $('#invoice_items_attributes_' + id + '_amount').val(total_amount.toFixed(2));

                            var taxamount = 0;
                            if (!isNaN(taxrate) && taxrate > 0) {
                                taxamount = (total_amount - (total_amount / taxrate));
                                tax += taxamount;

                                taxaArray[taxrate] += taxamount;
                            }
                            subtotal += total_amount - taxamount;
                            ttctotal += total_amount;
                            total = ttctotal;
                        }
                    });


                    $(".taxval").text(percent.toFixed(2) + ' €');

                    $(".totalrate").text(total.toFixed(2) + ' €');
                    $(".subtotal").text(subtotal.toFixed(2) + ' €');

                    $('.appndtax').html('');

                    $.each(taxArray, function(i, j) {

                        if (!isNaN(j) && j > 0) {
                            $('.appndtax').append('<div class="row"><div class="col-xs-5"></div><div class="col-xs-4"><h4 class="bold">T.V.A <span class="taxrt">' + i + '%</span></h4><input type="hidden" name="taxrte[]" value="' + i + '"></div><div class="col-xs-3 text-right"><h4 class="bold taxval">' + j.toFixed(2) + ' €</h4><input type="hidden" name="taxprice[]" value="' + j + '"></div></div>');
                        }
                    });
                    $.each(taxaArray, function(i, j) {

                        if (!isNaN(j) && j > 0) {
                            if (i == 1.2) {
                                i = 20;
                            }
                            if (i == 1.1) {
                                i = 10;
                            }
                            if (i == 1.055) {
                                i = 5.5;
                            }
                            $('.appndtax').append('<div class="row"><div class="col-xs-5"></div><div class="col-xs-4"><h4 class="bold">T.V.A <span class="taxrt">' + i + '%</span></h4><input type="hidden" name="taxrte[]" value="' + i + '"></div><div class="col-xs-3 text-right"><h4 class="bold taxval">' + j.toFixed(2) + ' €</h4><input type="hidden" name="taxprice[]" value="' + j + '"></div></div>');
                        }
                    });
                    $("#total").val(total);
                    $("#subtotal").val(subtotal);
                }
            });
        </script>
        <script>
            function invoice(id) {
                var total = 0;
                var subtotal = 0;
                var percent = 0;
                var tax = 0;
                // var oldsubtotal= parseFloat($("#subtotal").val());
                var amount = ($(".price" + id).val());
                var qty = parseFloat($(".qty" + id).val());
                var tax2 = $(".tax" + id).val();


                var amount = $(".price" + id).val();
                var qty = $(".qty" + id).val();

                tax = parseFloat($(".taxrate" + id).val());
                if (!tax) {
                    tax = 0;
                }



                if (qty && amount) {
                    subtotal = parseFloat(qty * amount);
                } else {
                    subtotal = (amount);
                }
                if (qty && amount && tax) {
                    var percentage = parseFloat(amount * tax * qty);
                    percent = percentage / 100;
                    total = percent + subtotal;
                } else {
                    total = subtotal;
                }


                $(".taxval").text(percent.toFixed(2) + ' €');

                $(".totalrate").text(total.toFixed(2) + ' €');
                $(".subtotal").text(subtotal.toFixed(2) + ' €');
                $(".taxrt").text(tax.toFixed(2));
                $("#taxval").val(percent.toFixed(2));

                $("#total").val(total.toFixed(2));
                $("#subtotal").val(subtotal.toFixed(2));
                $("#taxx").val(tax.toFixed(2));

                //anotherinvoice(subtotal);

            }
        </script>

        <script>
            $('document').ready(function() {
                $('textarea').each(function() {
                    $(this).val($(this).val().trim());
                });
            });
        </script>
</body>

</html>