


=semantic_form_for [:admin, @avoir], :url => save_avoir_admin_p_document_path() ,:remote => false, :html => {:id => "cart_form"} do |form|
  
  
  = form.hidden_field :label
  = form.hidden_field :doc_ref_id
  = form.hidden_field :doc_ref_number
  = form.hidden_field :p_document_type_id
  = form.hidden_field :element_type
  = form.hidden_field :element_id
  = form.hidden_field :p_customer_id
  
  
  .content
    %h3 
      Client : 
      %strong= @avoir.p_customer.show_name
    -if false
      %h3 Client

      =form.inputs do
  
        = form.input :p_commercial_id, :label => "Commercial :", :include_blank => true, :collection => PCommercial.order(:name).all, :as => :select, :member_label => :long_name if @avoir.id
  
  

        =hidden_field_tag :last_p_customer_id, @avoir.p_customer_id
  
  
        %label Client :
        %input.p_customer_autocomplete_input.form-control{:type => "text", :value => ("#{form.object.p_customer.show_name}" if form.object.p_customer)}
 

        = form.hidden_field :p_customer_id, :class => "p_customer_id"
  
        :javascript
          $( function() {
        
        

              $('.p_customer_autocomplete_input').autocomplete({
                 source: function( request, response ) {
                         $.ajax( {
                     
                     
                           url: "/admin/p_customers/autocomplete.json",
                           dataType: "json",
                           data: {
                             search: request.term,
                      
                           },
                           success: function(data){
                     
                             arr = jQuery.map( data, function( item ) {
                                                 return {
                                                     label: item.show_name,
                                                     value: item.show_name,
                                                     id: item.id
                                                 }
                             });
                       
                             response(arr)
             
                           }
                     
                         } );
                       },
                 minLength: 2,
                 select: function( event, ui ) {
     
                   $(this).next(".p_customer_id").val(ui.item.id)
             
                   var change_address = false;
      
                   if (parseInt($("#last_p_customer_id").val()) != parseInt(ui.item.id)){
                     change_address = true;
      
                   }
             
                   $.ajax({
                     url:"/admin/p_customers/"+ui.item.id+"/autocomplete_apercu.js", 
                     type : "GET",
                     data : {  change_address : change_address}
             
                   });
      
                   $("#last_p_customer_id").val(ui.item.id);
             
             
                   //return false
                 }
              });
        
        
        
            } );
  
  
          :scss
            .qi_autocomplete{
              position:relative;
      
              .qi_autocomplete_input{
                width:100%;
                box-sizing:border-box;
        
        
              }
      
              .qi_list{
                position:absolute;
                left:1px;
                right:1px;
                background:rgba(255,255,255,0.9);
                box-shadow:0 0 5px rgba(0,0,0,0.3);
        
                a{
                  display:block;
                  padding:5px 7px;
                  box-sizing:border-box;
                  border-bottom:1px solid rgba(0,0,0,0.2);
          
                  &:hover{
                    text-decoration:none;
                  }
                }
              }
            }
  
      .p_customer_sheet_customer


        -if @avoir.p_customer and @avoir.p_customer.particular
          =render :partial => "admin/p_customers/apercu", :locals => {:particular => @avoir.p_customer.particular}

    .addresses.row
      .columns.span_6
        %h3 Adresse de facturation

        -if @avoir.p_customer
          =form.inputs do
            = form.input :particular_bill_id, :collection => @avoir.p_customer.particulars.all, :as => :select, :member_label => :address_line, :include_blank => false, :label => "Adresse de facturation"
        -else
          =form.inputs do
            = form.input :particular_bill_id, :collection => [], :as => :select, :include_blank => false, :label => "Adresse de facturation"
    
      .columns.span_6
        %h3 Adresse de livraison

        -if @avoir.p_customer
          =form.inputs do
            = form.input :particular_send_id, :collection => @avoir.p_customer.particulars.all, :as => :select, :member_label => :address_line, :include_blank => false, :label => "Adresse de livraison"
        -else
          =form.inputs do
            = form.input :particular_send_id, :collection => [], :as => :select, :include_blank => false, :label => "Adresse de facturation"
        
      .clear



    = form.input :p_payment_type_id, :label => "Type de paiement :", :include_blank => false, :collection => PPaymentType.order(:name).all, :as => :select, :input_html => {:id => "payment_type_id_select"} if @avoir.id


    -if @avoir.id
      %hr
      %h4 Paiements

      = form.input :comptant, :label => "Paiement comptant nécessaire ?" 

      -if false
        = form.input :acompte, :label => "Acompte nécessaire ?" 
        = form.input :acompte_percent, :label => "Pourcentage d'acompte :" 

      = form.input :payment_delais, :label => "Délais jour de paiement :"

      -if false
        = form.input :payment_fin_de_mois, :label => "Fin de mois ?"

    = form.input :cust_ref, :label => "Référence commande client :" 



    .p_sheet_lines_form
      =form.semantic_fields_for :p_sheet_lines do |form|
        =render :partial => "admin/p_sheet_lines/avoir_form", :locals => {:form => form}

    %p= link_to_add_fields "Ajouter une ligne", form, :p_sheet_lines, {:partial => "admin/p_sheet_lines/avoir_form"}


  %p 
    %strong Totaux

  %table{:style => "width:100%;"}
    %td{:style => "text-align:right;"}
      Total HT :
    %td.price_td
      .price_emp.p_sheet_lines_tot_ht

  %table{:style => "width:100%;"}
    %td{:style => "text-align:right;"}
      TVA :
    %td.price_td
      .price_emp.p_sheet_lines_tot_tva
  
  %table{:style => "width:100%;"}
    %td{:style => "text-align:right;"}
      Total TTC :
    %td.price_td
      .price_emp.p_sheet_lines_tot_ttc


  .alert.alert-warning
    Attention, le total de l'acompte devrait toujours être négatif à la saisie.
  .right.actions=form.submit "sauvegarder", :class => "btn btn-primary"
  .clear

:scss
  .price_emp{
    padding: 6px 12px;
    font-size: 14px;
    line-height: 1.42857;
    color: #555555;
    height:34px;

  }

  .price_td{
    width:170px;
    text-align:right;

  }

:coffeescript
  tva = 1.2
  @update_fields_showed = -> 
    $(".p_sheet_lines_form").find(".product_stock_form").each ->
      if $(this).find(".custom_check_box").prop('checked') == true
        $(this).find(".custom_part").show()
        $(this).find(".no_custom_part").hide()
      else
        $(this).find(".custom_part").hide()
        $(this).find(".no_custom_part").show()
      
  @update_sheet_line_tot = ->
    tot_sheet = 0.0
    $(".p_sheet_lines_form").find(".product_stock_form").each ->
      tot = 0.0
      pht = parseFloat($(this).find(".sheet_line_price").val())
      tot = parseFloat($(this).find(".sheet_line_qte").val()) * pht

      $(this).find(".sheet_line_tot").html(accounting.formatMoney(tot))
      $(this).find(".sheet_line_tot_ttc").html(accounting.formatMoney(tot*tva))
      #$(this).find(".sheet_line_price_ttc").val((Math.round( (pht*1.2) * 1000 )/ 1000 ))

      tot_sheet += tot

    $(".p_sheet_lines_tot_ht").html(accounting.formatMoney(tot_sheet))
    $(".p_sheet_lines_tot_tva").html(accounting.formatMoney(tot_sheet*(tva-1.0)))
    $(".p_sheet_lines_tot_ttc").html(accounting.formatMoney(tot_sheet*tva))
  
  
  @update_sheet_line_tva = ->
    tot_sheet = 0.0
    $(".p_sheet_lines_form").find(".product_stock_form").each ->
      tot = 0.0
      pht = parseFloat($(this).find(".sheet_line_price").val())
  
      $(this).find(".sheet_line_price_ttc").val((Math.round( (pht*tva) * 1000 )/ 1000 ))
      
  @update_sheet_line_ttc = (input) ->
    val = (Math.round( (parseFloat(input.val())/ tva) * 100000 )/ 100000 )
    input.closest(".product_stock_form").find(".sheet_line_price").val(val)
  
  update_sheet_line_tot()
  update_sheet_line_tva()
  update_fields_showed()
  
  $("form").on "change", ->
    update_fields_showed()
    
  $("form").on "keyup", ".sheet_line_qte, .sheet_line_price", ->
    update_sheet_line_tot()
    update_sheet_line_tva()

  $("form").on "keyup", ".sheet_line_price_ttc", ->
    update_sheet_line_ttc($(this))
    update_sheet_line_tot()
    