<?php $user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}


 $invoice_create=intval($invoice_create);
$length=strlen($invoice_create);
  if($length < 5  ){
   $invoice_create=date("Y").'000'.$invoice_create;   
}

  $invoicedynamic= substr($invoice_create, 0, 4);
  $current_date= date("Y");
if($invoicedynamic!==$current_date){
    $invoicedynamic2= substr($invoice_create,4);
      $invoicedynamic=substr_replace($invoicedynamic,$current_date,0).$invoicedynamic2;
}
else{
    $invoicedynamic2= substr($invoice_create,4);
   $invoicedynamic=$current_date.$invoicedynamic2; 
}
 if(isset($address_list[0]->nom)) {$nom= $address_list[0]->nom; } else{
$nom='SARL ESPOIR
COMPTOIR DU BANGAL
ALIMENTATION GENERALE';
 } 
if(isset($address_list[0]->address)){
    $address=$address_list[0]->address; 
}
else{
    $address='148, Rue du Faubaurg Saint-Denis - 75010 Paris
M: 4,5 - RER: D,B,E (Gare du Nord)
Tel: 01 42 05 53 18 - Fax: 01 42 05 53 18
Email: espoir75010@gmail.com';
}
if(isset($address_list[0]->rcsb)){
    $rcs= $address_list[0]->rcsb;
 
}
else{
    $rcs='R.C.S.B 478 737 521';
}

?>

<!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/create');?>">New Invoice</a></li>
                <li><a class="btn-menu"  href="<?php echo base_url('invoice/details');?>">All Invoice</a></li> 
                
              </ul>
                 
            </div>
      <div class="panel-heading headingnone">
              <h2 class="transac">Invoice</h2>
             <button class="btn transaction btn-success" data-toggle="modal" data-target="#mysettingModal" style="float:right;">Setting</button>
             
            </div>
<div class="main-content invoiceform">
   <?php //if(isset($error)) {echo $error;} ?>
	
<div class="page white shadow" style="border-top: 0; padding: 20px 10px;">
<div class="page" style="padding: 0 15px 15px; border-top:0; background-color: #FFF5CD;">
	
<form method="post" action="<?php echo site_url('invoice/value'); ?>" onsubmit="return checkForm(this); " target="_blank" >
<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="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 $nom; ?></textarea>
	
	
		<label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Adresse</label>
<textarea class="form-control" placeholder="Your Company or Name
Address" rows="5" style="height: 8em;" tabindex="1" maxlength="5000" name="invoice[from]" id="invoice_from"><?php echo $address; ?></textarea>
	
	
		<label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Nom de l'entreprises</label>
		<textarea class="form-control" placeholder="Your Company or Name" rows="5" style="height: 5em;" tabindex="1" maxlength="5000" name="invoice[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" placeholder="billing address" rows="5" style="height: 8em;" tabindex="2" maxlength="5000" name="invoice[billing]" id="invoice_billing"></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="Facture" name="invoice_name"  /><br>
		
		<label for="invoice_number"><!--<i class="fa fa-search icon"></i>-->&nbsp;Numéro </label>
		<input class="form-control" placeholder="Facture Number"  type="number" value="<?php   echo $invoicedynamic+1 ;  ?>" 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 date("Y-m-d"); ?>" >

	</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-3 form-group">
		<select name="taxunit" class="form-control unitcheck invoicecalculate" required onchange="ttcselect(this)">
      <!--  <option value="">Select Unit</option>-->
		    <option value="H.T">Unit H.T</option>
		    <option value="T.T.C">Unit T.T.C</option>
		</select>
	</div>
	
	<div class="col-xs-2  form-group">
		<b>Montant</b>
	</div>
	
	<div class="col-xs-2 form-group text-center taxheding">
		<b>TVA</b>
	</div>
	
</div>



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


	<input type="hidden" name="invoice[items_attributes][0][id]" id="invoice_items_attributes_0_id" value="" />

	<div class="row append" data="0">

		<div class="col-xs-2 col-lg-1 form-group">
			<input type="text" name="invoice_item[0][qty]" id="invoice_items_attributes_0_quantity" placeholder="Qty" class="form-control quantity qty2 invoicecalculate" value="1" autocomplete="off" />
		</div>

		<div class="col-xs-2 col-lg-3 form-group">
			<textarea name="invoice_item[0][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 col-lg-3 form-group">
			<input type="text" name="invoice_item[0][unitprice]" id="invoice_items_attributes_0_unitprice"  placeholder="Unit Price" class="form-control unitprice invoicecalculate" autocomplete="off" />
		</div>

		<div class="col-xs-2 form-group">
			<input type="text" name="invoice_item[0][amount]" id="invoice_items_attributes_0_amount"  placeholder="Amount"   class="form-control amount invoicecalculate price2" />
		</div>

		<div class="col-xs-2 form-group" id="taxunitcheck">
			<input type="hidden" name="invoice_item[0][taxname]" id="invoice_items_attributes_0_tax_name" value="" />
			<input type="hidden" name="invoice_item[0][taxrate]" id="invoice_items_attributes_0_tax_rate" value="0" />

			
       <input type="hidden" name="subtotal" id="subtotal" />
    	<input type="hidden" name="total" id="total" />
		<input type="hidden" name="taxrate" id="taxval" />
              
		<button type="button" id="invoice_items_attributes_0_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(0)" >Add TVA</button>
		</div>

	



	</div>




</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" style="width:30%;">
	
	
	
	
	<div class="col-xs-6 col-md-6 form-group">
	  <label for="invoice_date_issued">Payment: </label>
	  <br>
	  <input type="checkbox" name="cb" >CB<br>
	  <input type="checkbox" name="Check" >Checque
		<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 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" class="chk" > CB<br>
	  <input type="checkbox" name="type" value="VIREMENT"  class="chk" > VIREMENT<br>
	  <input type="checkbox" name="type" value="Checque" class="chk" > CHEQUE<br>
	  <input type="checkbox" name="type" value="ESPECES" class="chk"  > ESPECES<br>
	  <input type="checkbox" name="type" value="TR"  class="chk" > TR
	   
		
	</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">0.00</h4></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">0.0</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 $rcs; ?></textarea>
	</div>
</div>
	



		  <input type="submit"  class="btn btn-primary" style="width: 100%" value="Terminé">

	
	
	
</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%')">
			Ajouter 
			</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%')">
			Ajouter
			</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%')">
			Ajouter 
			</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>

	</div>

	
<div class="modal fade in" id="mysettingModal" role="dialog"  >
                      <div class="modal-dialog">
                        <!-- Modal content-->
                        <div class="modal-content">
                          <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal">×</button>
                            <h4 class="modal-title">Address Change</h4>
                          </div>
                          <div class="modal-body">
                    <form id="my-form" method="post" action="<?php echo site_url('invoice/address_setting');?>">
                             
                              <div class="form-group">
                                
                                <div class="form-group type">
                                  
                                 
                                  <select name="select" class="form-control select " required="">
                                 
                                  
                                   <?php 
                                      foreach ($buisness_list as $key => $tabtype){  if ($buisness==$tabtype->id) {?>
                                       
                                    <option class="sel" value="<?php  echo $tabtype->id;?>" ><?php echo $tabtype->type; ?></option>';



                                     <?php  } } ?>
                                     
                                
                             
                                  </select>
                                 
                                  
                                </div>
                              </div>
                              
                            <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="nom" id="invoice_from"><?php echo $nom; ?></textarea>
	
	
		<label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Adresse</label>
		<textarea class="form-control" placeholder="Your Company or Name
Address" rows="5" style="height: 8em;" tabindex="1" maxlength="5000" name="address" id="invoice_from"><?php echo $address; ?></textarea>
	<label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;R.C.S</label>
	<textarea class="form-control" placeholder="Optional" rows="5" style="height: 8em;" tabindex="20" maxlength="5000" name="rcbs" id="invoice_footer_1"><?php echo $rcs; ?></textarea>
	
		
	</div>
                          
		
	
                              <div class="form-group type">
                                  <label for="invoice_from"><i class="fa fa-user icon"></i>&nbsp;&nbsp;Password</label>
                                 
                            <input class="form-control" type="password" name="adminpass" class="admin_pass" required>
                                 
                                  
                                </div>
                              
                             
                           
                       
                          <div class="modal-footer" style="width: 30%;">
                            <input type="submit" class="btn btn-success" value="submit">
                            <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                          </div>
                              </form>
                        </div>
                      </div>
                    </div>
	<script src="https://acme.invoicehome.com/assets/application-479e98a06b335fc94a5abd3ac0f4ffd7ba83a2a85379b8f24a3ff1c7e885a08a.js"></script>

	<script src="https://js.stripe.com/v3/"></script>
    </div>
    <script>
        function ttcselect(that){
          /* if(that.value){
               
             calculateInvoice();  
           }
           */
            
           /* if(that.value=='T.T.C'){
                $('#taxunitcheck').fadeOut('slow');
                 $('.taxunitc').fadeOut('slow');
                $('.taxheding').fadeOut('slow');
            }
            if(that.value=='H.T'){
                $('#taxunitcheck').fadeIn('slow');
                 $('.taxunitc').fadeIn('slow');
                $('.taxheding').fadeIn('slow');
            }*/
        }
    </script>
    <script>
        $(document).ready(function() {
    //var html = '</div>';
            var counter = 1;
            $("#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 col-lg-3 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-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);
                /* if(unitcheck=='T.T.C'){
                  
                   $('.taxunitc').hide();
               }*/
            });
    //$('.demo').append(html);    

  
});
    </script>
    <script>
    
   
$(document).ready(function(){
    
  
     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'){
             //  var taxaArray = {'1.2':0,'10':0,'20':0};
               if(taxrate==20){
                   taxrate=1.2;
               }
                if(taxrate==10){
                   taxrate=1.1;
               }
                if(taxrate==5.5){
                   taxrate=1.055;
               }
                //alert(taxrate);
                 //  $('#invoice_items_attributes_0_tax').attr('data-target','#myttcModal');
               	var total_amount = qty * unitprice;
				$('#invoice_items_attributes_'+id+'_amount').val(total_amount);
				
				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;
                 
            }
	   });
	  
	   //alert(total);alert(percent);alert(subtotal);
	   $(".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>
        function anotherinvoice(sub){
           // alert(id);
            var total=0;
             var subtotal=0;
             var percent=0;
            var anothersub=00;
             
             var amount = ($(".price"+id).val());
             var qty= parseFloat($(".qty"+id).val());
             var tax2=$(".tax"+id).val();
           
            if(amount && sub){
                anothersub=parseFloat(amount * qty)+parseFloat(subtotal);
                //anothersub=anothersubtotal * parseFloat(qty);
            }
            else{
                anothersub=subtotal;
            }
          /*if(tax2 && tax==tax2){
              taxrate=tax;
              $('.taxdiv').remove();
          }
            else if(tax2 && tax!==tax2){
                $('.taxdiv').remove();
               taxrate=tax2; 
                $(".appndtax").append('<div class="col-xs-5 taxviv">&nbsp;</div><div class="col-xs-4"><h4 class="bold">Tax<span class="taxrt2">&nbsp; 0.0</span>%</h4></div><div class="col-xs-3 text-right"><h4 class="bold taxval">0.0</h4></div>');
                $(".taxrt2").text(tax2);
            }*/
        
           
          
             
             $(".subtotal").text(anothersub +' €');
            
          
              
             //$("#subtotal").val(anothersub);
            
        }
    </script>-->
  <script type="text/javascript">

  function checkForm(form)
  {
    
   if (!$('.chk').is(':checked')) { 
        swal({
          title: "Sélectionner le mode de paiement",
         
          type: "warning",
         
          confirmButtonText: 'Ok'
        });
        return false;
    }
   
  }

</script>
</body>
</html>
