.qi_row
  .qi_pannel.qi_plain.padding
    =f.inputs do


      -if !@p_customer_sheet or (@p_customer_sheet and @p_customer_sheet.state != "commande")
        = f.input :p_commercial_id, :label => "Commercial :", :collection => PCommercial.order(:name).all, :as => :select, :member_label => :long_name if f.object.id
  
  
        = f.hidden_field :p_customer_id, :class => "p_customer_id"
    
    
  
        .p_customer_sheet_customer
  
  
          
  
    
          .addresses.row
            .columns.span_6

      
              -if f.object.p_customer
                =f.inputs do
                  = f.input :particular_bill_id, :collection => f.object.p_customer.particulars.all, :as => :select, :member_label => :address_line, :include_blank => false, :label => "Adresse de facturation"
              -else
                =f.inputs do
                  = f.input :particular_bill_id, :collection => [], :as => :select, :include_blank => false, :label => "Adresse de facturation"
      
            .columns.span_6
  
              -if f.object.p_customer
                =f.inputs do
                  = f.input :particular_send_id, :collection => f.object.p_customer.particulars.all, :as => :select, :member_label => :address_line, :include_blank => false, :label => "Adresse de livraison"
              -else
                =f.inputs do
                  = f.input :particular_send_id, :collection => [], :as => :select, :include_blank => false, :label => "Adresse de livraison"
          
            .clear
  
  
  
  
        = f.input :customer_ref, :label => "Référence commande client :"
  
        = f.input :covid, :label => "Mention covid ?"


.price_lines_form
  =f.semantic_fields_for :price_lines do |f|
    =render :partial => "admin/price_lines/form", :locals => {:form => f}


%p{:style => "margin:0 20px; margin-top:5px"}= link_to_add_fields "Ajouter une ligne", f, :price_lines, {:class => "btn btn-primary"}



 
.qi_row
  .qi_pannel.qi_plain.padding

    %h4 Paiements

    = f.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"}


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

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

    = f.input :ct_payment_delais, :label => "Délais jour de paiement :"

    = f.input :ct_payment_month_end, :label => "Fin de mois ?"

  

  
  :coffeescript
    $(".price_lines_form").sortable
      tolerance: "pointer"
      handle : ".take"
      stop : (evt, ui) ->
        i = 0
        $(this).find(".price_line_form").each ->
          i += 1
          $(this).find(".price_line_position_input").val(i)
  
  
  :coffeescript
    parse_field = (val) ->
      return parseFloat(val)
      
    round_price = (val) ->
      return (Math.round(val*100)/ 100)
      
    update_qi_price_form = (form) ->
  
      tot_lines_ht = 0.0
      tot_lines_tva = 0.0
      tot_lines_ttc = 0.0
  
      weight_tot = 0.0
  
      form.find(".price_line_form").each ->
        #alert($(this).find(".destroy input").val() == "false")
        if ($(this).find(".destroy input").val() == "false")
          qte = parse_field($(this).find(".input_price_line_qte").val())
    
    
      
      
          line_tot_line_ht = round_price(qte * price_u_ht)
          $(this).find(".input_price_line_tot_line_ht").val(line_tot_line_ht)
    
          tot_discount_ht = parse_field($(this).find(".input_price_line_tot_discount_ht").val())
          tva_account_value = parse_field($(this).find(".input_price_line_tva_account_value").val())
    
          tot_amount_ht = round_price(line_tot_line_ht + tot_discount_ht)
          $(this).find(".input_price_line_tot_amount_ht").val(tot_amount_ht)
    
    
          tot_amount_tva = round_price(tot_amount_ht * (tva_account_value/ 100))
          $(this).find(".input_price_line_tot_amount_tva").val(tot_amount_tva)
    
          tot_amount_ttc = round_price(tot_amount_ht + tot_amount_tva)
          $(this).find(".input_price_line_tot_amount_ttc").val(tot_amount_ttc)
    
          tot_lines_ht += round_price(tot_amount_ht)
          tot_lines_tva += round_price(tot_amount_tva)
          tot_lines_ttc += round_price(tot_amount_ttc)
    
    
          weight_u = parse_field($(this).find(".input_price_line_weight_u").val())
    
          line_weight_tot = round_price(weight_u * qte)
    
          $(this).find(".input_price_line_weight_tot").val(line_weight_tot)
      
          weight_tot += line_weight_tot
    
    
    
  
      form.find(".input_price_line_block_tot_lines_ht").val(tot_lines_ht)
  
  
      ct_tot_discount_percent = parse_field(form.find(".input_price_line_block_ct_tot_discount_percent").val())
  
      ct_tot_discount_percent = 0.0 if isNaN(ct_tot_discount_percent)
  
      ct_tot_discount_ht = round_price((ct_tot_discount_percent / -100) * tot_lines_ht)
      ct_tot_discount_tva = round_price((ct_tot_discount_percent / -100) * tot_lines_tva)
      ct_tot_discount_ttc = round_price((ct_tot_discount_percent / -100) * tot_lines_ttc)
  
  
      tot_discount_ht = round_price(ct_tot_discount_ht)
      tot_discount_tva = round_price(ct_tot_discount_tva)
      tot_discount_ttc = round_price(ct_tot_discount_ttc)
  
      form.find(".input_price_line_block_tot_discount_ht").val(tot_discount_ht)
      form.find(".input_price_line_block_tot_discount_tva").val(tot_discount_tva)
      form.find(".input_price_line_block_tot_discount_ttc").val(tot_discount_ttc)
  
  
      #tot_discount_ht = parse_field(form.find(".input_price_line_block_tot_discount_ht").val())
      #tot_discount_tva = parse_field(form.find(".input_price_line_block_tot_discount_tva").val())
      #tot_discount_ttc = parse_field(form.find(".input_price_line_block_tot_discount_ttc").val())
  
      tot_fdp_ht = parse_field(form.find(".input_price_line_block_tot_fdp_ht").val())
      tot_fdp_tva = parse_field(form.find(".input_price_line_block_tot_fdp_tva").val())
      tot_fdp_ttc = parse_field(form.find(".input_price_line_block_tot_fdp_ttc").val())
  
  
      tot_amount_ht = round_price(tot_lines_ht + tot_discount_ht + tot_fdp_ht)
      tot_amount_tva = round_price(tot_lines_tva + tot_discount_tva + tot_fdp_tva)
      tot_amount_ttc = round_price(tot_lines_ttc + tot_discount_ttc + tot_fdp_ttc)
  
      form.find(".input_price_line_block_tot_amount_ht").val(tot_amount_ht)
      form.find(".input_price_line_block_tot_amount_tva").val(tot_amount_tva)
      form.find(".input_price_line_block_tot_amount_ttc").val(tot_amount_ttc)
  
      form.find(".input_price_line_block_weight_tot").val(weight_tot)
   
   
   
    $(".qi_price_form").on "change", ->
      update_qi_price_form($(this))
  
    $(".qi_price_form").on "keyup", ->
      update_qi_price_form($(this))
  
    $(".qi_price_form").each -> 
      update_qi_price_form($(this))


  
    
      
      