


=semantic_form_for [:admin, @p_customer_sheet], :remote => false, :html => {:id => "cart_form"} do |form|

  .content
    %h3 Client
    
    =form.inputs do
    
    
      =hidden_field_tag :last_p_customer_id, @p_customer_sheet.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
               }
            });
            
            
            
          } );
      
      
      
      -if false
        .qi_autocomplete
          %input.qi_autocomplete_input.form-control{:type => "text"}
          
          .qi_list
        
        = form.hidden_field :p_customer_id, :id => "p_customer_id"
      
        :coffeescript
          $(document).on "click", ".autocomplete_line", ->
          
            id = $(this).data('value')
          
            $(this).closest(".qi_autocomplete").next("input").val(id)
          
            $(this).closest(".qi_list").hide()
            $(this).closest(".qi_autocomplete").find(".qi_autocomplete_input").val("")
          
            change_address = false
          
            if (parseInt($("#last_p_customer_id").val()) != parseInt(id))
              change_address = true
          
          
            $.ajax
              url:"/admin/p_customers/"+id+"/autocomplete_apercu.js", 
              type : "GET",
              data : {search : $(this).val(),  change_address : change_address}
          
            $("#last_p_customer_id").val(id)
          
            return false
          
          $(".qi_autocomplete_input").keyup ->
            input = $(this)
            $.ajax
              url:"/admin/p_customers/autocomplete.js", 
              type : "GET",
              data : {search : $(this).val()}
              
        
        
      
        :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 @p_customer_sheet.p_customer and @p_customer_sheet.p_customer.particular
        =render :partial => "admin/p_customers/apercu", :locals => {:particular => @p_customer_sheet.p_customer.particular}
    
    .addresses.row
      .columns.span_6
        %h3 Adresse de facturation
    
        -if @p_customer_sheet.p_customer
          =form.inputs do
            = form.input :particular_bill_id, :collection => @p_customer_sheet.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 @p_customer_sheet.p_customer
          =form.inputs do
            = form.input :particular_send_id, :collection => @p_customer_sheet.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
    
    -if false
      %h3 Dates de livraison
      %table{:style => "width:100%;"}
        %tr
          %td{:style => "width:50%;vertical-align:top;"}
            = form.input :wish_start, :label => "Début de date de livraison :", :as => :date_select
          %td{:style => "width:50%;vertical-align:top;"}  
            = form.input :wish_stop, :label => "Fin de date de livraison :", :as => :date_select
      
      
    
    
    .p_sheet_lines_form
      =form.semantic_fields_for :p_sheet_lines do |form|
        =render :partial => "admin/p_sheet_lines/form", :locals => {:form => form}
    
    %p= link_to_add_fields "Ajouter une ligne", form, :p_sheet_lines
  
  = form.input :fdp_force_price, :label => "Frais de port (calculés automatiquement si vides) :" 
  
  
  -if false
    %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
  
  
  %hr
  %h4 Paiements
  
  = 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"}
  
  
  
  = form.input :comptant, :label => "Paiement comptant nécessaire ?" 
  
  = 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 :"
  
  = form.input :payment_fin_de_mois, :label => "Fin de mois ?"
  
  
  
  .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_sheet_line_tot = ->
    tot_sheet = 0.0
    $(".p_sheet_lines_form").find(".product_stock_form").each ->
      tot = 0.0
      tot = parseFloat($(this).find(".sheet_line_qte").val()) * parseFloat($(this).find(".sheet_line_price").val())
    
      $(this).find(".sheet_line_tot").html(accounting.formatMoney(tot))
      $(this).find(".sheet_line_tot_ttc").html(accounting.formatMoney(tot*tva))
    
      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_tot()
       
          
  $(window).on "keyup", $("form.p_customer_sheet"), ->
    update_sheet_line_tot()


:scss

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

  }
  

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

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