
=f.inputs do


 
  = f.hidden_field :p_fournisseur_id, :class => "p_fournisseur_id"
  
  

  .p_customer_sheet_customer


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

  
  


  %table{:style => "width:100%;"}
    %tr
      %td{:style => "width:50%;"}
        = f.input :wish_date, :label => "Date de livraison souhaitée :", :as => :date
      
      %td{:style => "width:50%;"}
        = f.input :ct_creation_date, :label => "Date de commande (si différente de la date de création) :", :as => :date



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

  

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

  %p= link_to_add_fields "Ajouter une ligne", f, :price_lines, {:class => "btn btn-primary", :partial_name => "fournisseur_order_form"}


  
  :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())
    
    
          if $(this).find(".input_price_line_forced_price").is(':checked')
            price_u_ht = parse_field($(this).find(".input_price_line_ct_u_price_ht").val())
            $(this).find(".forced_price_wrapper").show()
          else
            price_u_ht = parse_field($(this).find(".input_price_line_price_u_ht").val())
            $(this).find(".forced_price_wrapper").hide()
      
      
          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))


  
      