.qi_header
  
  .right
    =raw @p_customer_sheet.state_html
    
    =#debug @p_customer_sheet.archive_now
    
    -if !@p_customer_sheet.archived or @p_customer_sheet.state == "commande"
      = link_to i(:pencil), edit_admin_p_customer_sheet_path(@p_customer_sheet), :remote => false
  
  
  %h1
    Ventes
    %span
      =link_to "Offres",admin_p_customer_sheets_path
  
  

.qi_row
  .qi_pannel.qi_plain.padding
    
    %h3 Client
    
    -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}
    
    
    %table.table{:style => ""}
      %tr
        %td{:style => "border:0"}
          %h3 Adresse de facturation
      
          =render(:partial => "admin/particulars/particular_detail", :locals => {:particular => @p_customer_sheet.particular_bill}) if @p_customer_sheet.particular_bill
      
        %td{:style => "border:0"}
          %h3 Adresse de livraison
      
          =render(:partial => "admin/particulars/particular_detail", :locals => {:particular => @p_customer_sheet.particular_send}) if @p_customer_sheet.particular_send
    
    
    -price_line_block = @p_customer_sheet.price_line_block
    
    %p
      Date de commande :
      =l price_line_block.creation_date
    
    %p
      Date de livraison souhaitée :
      =l price_line_block.wish_date
    
    %p
      Nombre de colis :
      =price_line_block.nbr_ship
    
    -if price_line_block.customer_ref?
      %p
        Référence commande personnalisée :
        =price_line_block.customer_ref
    
    
    %table.table
      %thead
        %tr
          %th Référence
          %th Qté
          %th Désignation
          %th Qté [Kg]
          %th P.U. HT
          %th TVA
          -if !price_line_block.remise_ecole_ok
            %th 
              Remise
              %br
              Marché
              %br
              annuel
            
            
            -if price_line_block.ca_remise_qte_ok
              %th
                Remise 
                %br
                Qté livrée
                %br
                ="/cde"
          
          
            -if price_line_block.ca_remise_pre_order_ok
              %th
                Remise
                %br
                commande
                %br
                anticipée
          
            %th
              Remise
              %br
              comptant
          
            -if price_line_block.remise_enrobage_ok
              %th
                Remise 
                %br
                enrobage 
          -else
            %th 
              Remise 
              %br
              école  
          
          %th
            P.U. Net
          %th
            Montant HT
      %tbody
        -price_line_block.price_lines.each do |price_line|
          %tr
            %td
              =price_line.ref
            %td.numeraire
              =price_line.qte
            
            %td
              =price_line.title
            
            %td.numeraire
              =price_line.qte * price_line.weight_u
            
            %td.numeraire
              =number_to_currency price_line.ca_price_u_kg_ht
            %td.numeraire
              =price_line.tva_account_value.to_s+"%"
            
            -if !price_line_block.remise_ecole_ok
              %td.numeraire
                =price_line.discount_market_percent.to_s+"%"
                %br
                =number_to_currency price_line.ca_discount_market_ht
              
              -if price_line_block.ca_remise_qte_ok
                %td.numeraire
                  =price_line.discount_qte_percent.to_s+"%"
                  %br
                  =number_to_currency price_line.ca_discount_qte_ht
              
              -if price_line_block.ca_remise_pre_order_ok
                %td.numeraire
                  =price_line.discount_delay_percent.to_s+"%"
                  %br
                  =number_to_currency price_line.ca_discount_delay_ht
              
              %td.numeraire
                =price_line.discount_comptant_percent.to_s+"%"
                %br
                =number_to_currency price_line.ca_discount_comptant_ht
              
              -if price_line_block.remise_enrobage_ok
                %td.numeraire
                  =price_line.discount_enrobage_percent.to_s+"%"
                  %br
                  =number_to_currency price_line.ca_discount_enrobage_ht
            -else
              %td.numeraire
                =price_line.discount_ecole_percent.to_s+"%"
                %br
                =number_to_currency price_line.ca_discount_ecole_ht
            
            %td.numeraire
              
              =number_to_currency price_line.ca_price_u_kg_net_ht
              
            
            
            %td.numeraire
              =number_to_currency price_line.tot_amount_ht
              
      
      %table.table{:style => "width:auto;float:right;"}
        %tr
          %th
            Poids total :
          %td.numeraire
            ="#{price_line_block.weight_tot}Kg" 
        
        %tr
          %th Total remises U. HT :
          %td.numeraire
            =number_to_currency price_line_block.cumul_discount_ht
        %tr
          %th
            Total HT
          %td.numeraire
            =number_to_currency price_line_block.tot_lines_ht
        
       
        
        -if price_line_block.tot_discount_ht != 0.0
          %tr
            %th
              Remise commerciale exceptionnelle (HT
              ="(#{price_line_block.ct_tot_discount_percent}%)"
            %td.numeraire
              =number_to_currency price_line_block.tot_discount_ht
        
        %tr
          %th
            Frais de port HT 
            ="(TVA #{price_line_block.fdp_tva_rate}%)"
          %td.numeraire
            =number_to_currency price_line_block.tot_fdp_ht
          
        %tr
          %th
            Total HT Net
          %td.numeraire
            =number_to_currency price_line_block.tot_amount_ht
        
        %tr
          %th
            Total TVA
          %td.numeraire
            =number_to_currency price_line_block.tot_amount_tva
            
        %tr
          %th
            Total TTC
          %td.numeraire
            =number_to_currency price_line_block.tot_amount_ttc
            
        %tr
          %th
            &nbsp;
          %th
        
        %tr
          %th
            Net à payer
          %td.numeraire
            =number_to_currency price_line_block.tot_amount_ttc
        
      %table.table{:style => "width:auto;"}
        %tr
          %th 
            Taux
            %br
            TVA
          %th Montant HT Net
          %th Montant TVA
        -tva_rates = {}
        
        -price_line_block.price_lines.each do |pl|
        
          -tva_rates[pl.tva_account_value.to_s] = tva_rates[pl.tva_account_value.to_s] || {:montant => 0.0, :tva => 0.0}
          
          -tva_rates[pl.tva_account_value.to_s][:montant] += pl.tot_amount_ht.to_f - (pl.tot_amount_ht.to_f * ((price_line_block.ct_tot_discount_percent and price_line_block.ct_tot_discount_percent != 0.0) ? (price_line_block.ct_tot_discount_percent.to_f/100) : 0.0))
          
          -tva_rates[pl.tva_account_value.to_s][:tva] += pl.tot_amount_tva.to_f  - (pl.tot_amount_tva.to_f * ((price_line_block.ct_tot_discount_percent and price_line_block.ct_tot_discount_percent != 0.0) ? (price_line_block.ct_tot_discount_percent.to_f/100) : 0.0.to_f))
          
           
        
        -tva_rates[price_line_block.fdp_tva_rate.to_s] = tva_rates[price_line_block.fdp_tva_rate.to_s] || {:montant => 0.0, :tva => 0.0}
        
        -tva_rates[price_line_block.fdp_tva_rate.to_s][:montant] += price_line_block.tot_fdp_ht
        
        -tva_rates[price_line_block.fdp_tva_rate.to_s][:tva] += price_line_block.tot_fdp_tva
          
        -tva_rates.each do |key, value|
          %tr
            %td
              ="#{key}%"
            %td
              =number_to_currency value[:montant]
            
            %td
              =number_to_currency value[:tva]
        
    
    %table.table{:style => "width:auto;"}
      %tr
        %th 
          Échéance
        %th Mode de paiement
        %th
          Montant net	
      %tr
        %td
          =price_line_block.payment_delais.to_i
          jours
        %td
          =price_line_block.p_payment_type.name if price_line_block.p_payment_type
        %td
          =number_to_currency price_line_block.tot_amount_ttc
    
    .clear
        
            
    %hr
    -if @p_customer_sheet.demande_type?
      %p
        Demande lors de la saisie :
        %strong=@p_customer_sheet.demande_type
    .right
      
      -if @p_customer_sheet.state == "brouillon" or @p_customer_sheet.state == "offre"# or @p_customer_sheet.state == "commande"
        -if @p_customer_sheet.state == "brouillon"
          %p=link_to "Générer le devis", generate_d_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary"
        %p=#link_to "Générer la facture proforma", generate_fp_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary"  
        %p=link_to "Générer le bon de commande", generate_bc_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-success btn-ap-add"
        %p=link_to "Marquer l'offre comme refusée", reject_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary btn-ap-error "
    
      -if @p_customer_sheet.state == "commande"
        -if price_line_block.nbr_ship
          %p=link_to "Générer le bon de livraison", generate_bl_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary btn-ap-add"
        -else
          %p=link_to "Indiquer le nombre de colis à livrer", edit_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-default"
    
      -if @p_customer_sheet.state == "en livraison" # or @p_customer_sheet.state == "remboursée"  
        -if false or @p_customer_sheet.state == "remboursée"  
          %p= link_to ic(:pencil)+" Modifier l'adresse, le client, ou le montant avant de re-facturer", edit_admin_p_customer_sheet_path(@p_customer_sheet), :remote => false, :class => "btn btn-warning"
        %p=link_to "Générer la facture", generate_f_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary btn-ap-add"
      
    .clear
    %hr    
    %h3 Documents
    %table.table.table-hover.table-striped
      %tr
        %th Code client
        %th Commercial
        %th Client
        %th Type
        
        
        
        %th Numéro
        %th Date
        
        %th Montant HT
        %th TVA
        %th Montant TTC
        %th Soldé ?
        %th Restant dû
        %th.actions
      %tbody#p_customer_sheets_rows
        =render @p_customer_sheet.price_documents.order("date DESC, created_at DESC")
           
    
    -if false
      %h3 Résumé
      -if @p_customer_sheet.cust_ref?
        Référence client :
        %strong=@p_customer_sheet.cust_ref
      
      -if false
        %table

          %tr
            %td
              =number_to_currency @p_customer_sheet.a_ok_total
            %td
              =number_to_currency @p_customer_sheet.a_total_cost_ok

   
      =render :partial => "admin/p_sheet_lines/p_sheet_lines", :locals => {:sheet_lines => @p_customer_sheet.p_sheet_lines, :ok => (@p_customer_sheet.state_ok?) }
    
    
    
      %table.en-tete-table{:style => "width:auto;position:relative;top:-40px;"}
        %tr
          %th Conditions de réglement
       
    
        %tr
      
    
          %td{:style => "width:150px;"}
            -if @p_customer_sheet.p_payment_type
              =@p_customer_sheet.p_payment_type.name
              %br
        
            -if @p_customer_sheet.comptant
              Comptant
            -else
              =@p_customer_sheet.payment_delais.to_i
              j nets
      
       
      
      .right
        %p
          Demande lors de la saisie :
          %strong
            =@p_customer_sheet.demande_type
        -if @p_customer_sheet.state == "brouillon" or @p_customer_sheet.state == "offre" or @p_customer_sheet.state == "commande"
          %p=link_to "Générer le devis", generate_d_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary"
          %p=link_to "Générer la facture proforma", generate_fp_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary"  
          %p=link_to "Générer le bon de commande", generate_bc_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-ap-add"
          %p=link_to "Marquer l'offre comme refusée", reject_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-danger"
      
        -if @p_customer_sheet.state == "commande" and @p_customer_sheet.p_sheet_lines.joins(:p_product).where("bl = 0 and (p_products.p_product_cat_id != 6 OR (p_products.p_product_cat_id = 6 and externe = 1))").count > 0
          %p=link_to "Générer le bon de livraison", generate_bl_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary btn-ap-add"
      
        -if @p_customer_sheet.state == "livrée" or @p_customer_sheet.state == "remboursée"  
          -if @p_customer_sheet.state == "remboursée"  
            %p= link_to ic(:pencil)+" Modifier l'adresse, le client, ou le montant avant de re-facturer", edit_admin_p_customer_sheet_path(@p_customer_sheet), :remote => false, :class => "btn btn-warning"
          %p=link_to "Générer la facture", generate_f_admin_p_customer_sheet_path(@p_customer_sheet), :class => "btn btn-primary btn-ap-add"
        
      .clear
    
    
      %h3 Documents
      =#["brouillon", "offre", "commande", "livrée","facturée", "annulée", "refusée"]
    
      =#debug @p_customer_sheet.p_documents.first.save
    
    
    
      %table.table.table-hover.table-striped
        %tr
          %th Date
          %th Client
          %th Type
          %th Numéro
          %th Total HT
          %th Total TTC
          %th.actions

        %tbody#p_customer_sheets_rows
          =render @p_customer_sheet.p_documents.order("created_at DESC")
  
 