


=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
  
  
  .content
    %h3 Client
    
    =form.inputs do
    

      = form.hidden_field :p_customer_id, :class => "p_customer_id"
  
    
    .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 :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
  
  
  %table{:style => "width:100%;"}
    %td{:style => "text-align:right;"}
      Total HT articles :
    %td.price_td
      .price_emp.p_sheet_lines_tot_ht
      
  
  = form.input :fdp_force_price, :label => "Frais de port :" , :input_html => {:id => "fdp_value"}
  
  
  -if true
    %p 
      %strong Totaux
  
    %table{:style => "width:100%;"}
      %td{:style => "text-align:right;"}
        Total HT :
      %td.price_td
        .price_emp.p_sheet_lines_tot_ht_fdp
  
    %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
  
  
  .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;
    
  }



:scss

  .product_stock_form{
  td{
    vertical-align:top;
  .form-control, .form-group{
  margin-bottom:0}
  }

  }
  





:coffeescript
  tva = 1.2
  @update_sheet_line_tot = ->
    
    tot_sheet = 0.0
    $(".p_sheet_lines_form").find(".product_stock_form").each ->
      tot = 0.0
      tot = parseFloat($(this).find(".price_tot").data("price"))
    
      tot_sheet += tot
    

    $(".p_sheet_lines_tot_ht").html(accounting.formatMoney(tot_sheet))
    
    fdp = parseFloat($("#fdp_value").val())
    fdp = fdp || 0.0

    tot_sheet_fdp = (tot_sheet + fdp)
    
    $(".p_sheet_lines_tot_ht_fdp").html(accounting.formatMoney(tot_sheet_fdp))
    
    
    
    $(".p_sheet_lines_tot_tva").html(accounting.formatMoney(tot_sheet_fdp*(tva-1.0)))
    $(".p_sheet_lines_tot_ttc").html(accounting.formatMoney(tot_sheet_fdp*tva))
    #alert(tot_sheet)

  @udpate_sheet_lines = ->
    $(".p_sheet_line_field").each -> 
      price = parseFloat($(this).find(".price").find("input").val())
      price_tot = 0
      qte_tot = 0
      $(this).find(".color_line").each ->
        qte = 0
        $(this).find("input.qte").each ->
          qte += parseFloat($(this).val()) if parseFloat($(this).val()) 
        price_color = qte * price
        $(this).find(".price_color_tot").html(accounting.formatMoney(price_color))
        $(this).find(".qte_color_tot").html(qte)
      
      
        qte_tot += qte
    
      $(this).find(".qte_tot").html(qte_tot)
      $(this).find(".price_tot").html(accounting.formatMoney(qte_tot*price))
      $(this).find(".price_tot").attr("data-price",qte_tot*price)
      $(this).find(".price_tot").data("price",qte_tot*price)

:coffeescript
  
  
  udpate_sheet_lines()
  update_sheet_line_tot()
  
  $("#cart_form").change ->
    udpate_sheet_lines()
    update_sheet_line_tot()

  $("#cart_form").keyup ->
    udpate_sheet_lines()
    update_sheet_line_tot()
