%head
  :scss
    body{
      font-size:10px;
      font-family:arial, sans-serif;
      
      .table{
        border-collapse:collapse;
        font-size:10px;
        font-family:arial, sans-serif;
        
        td,th{
          border:1px solid gray;
        }
      }
      
      .table{
        td{
          padding:2px 5px;
        }
      }
    }
    
    
    .clear{
      clear:both;
    }
    
    .table{
      width:100%;
    }
    
    td.numeraire{
      white-space: nowrap;
      text-align:right;
    }
    
    
    


-price_line_block = @price_document.price_line_block
%body
  
  %div{:style => "float:right;margin-top:3cm;font-size:13px;margin-bottom:2cm;padding-right:2cm;"}
    
    -if @price_document.label == "Bon de livraison"
      -@bl = true
      -particular_doc = price_line_block.particular_send
      
      
    -else
      -particular_doc = price_line_block.particular_bill
    
    
    -if particular_doc.organisation?
      %strong=particular_doc.organisation
    -if particular_doc.organisation?
      %br
      =particular_doc.com_name
      
    %br
    -if particular_doc.firstname? or particular_doc.name?
      =particular_doc.civilite
      
      =particular_doc.firstname

      =particular_doc.name
      %br

    =particular_doc.address_2

    -if particular_doc.address_3?
      %br
      =particular_doc.address_3
    %br
    =particular_doc.cp

    =particular_doc.city
    %br
    =particular_doc.country
    
    
    
    
  .clear
  
  %p{:style => "float:right;padding:0;margin:0;text-align:right;"}
    %span{:style => "font-size:14pt;font-weight:bold;text-transform:uppercase;"}
      =@price_document.label
    -if @price_document.ref_element_type == "PCustomerSheet" and @price_document.ref_element
      -bc =  @price_document.ref_element.price_documents.where(:cc_label => "Bon de commande").order("date DESC, created_at DESC").first
      -if bc
        %br
        Ref commande : 
        =bc.d_number
  
  %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

  -if particular_doc
    -range_part = price_line_block.particular_send 
    -range_part = range_part.particular_ref if range_part.particular_ref
    
    %p
      Jour(s) de fermeture :
      -oras = []
      -OpenRange.where(:id => (OpenRange.all.ids - range_part.open_ranges.ids)).each do |ora|
        -oras << ora.name
      =oras.join(", ")
      
  %table.table
    %thead
      %tr
        %th Référence
        %th Désignation
        %th Qté [Kg]
        -if !@bl
          %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
            =price_line.title
        
          %td.numeraire
            =price_line.qte * price_line.weight_u
        
          -if !@bl
            %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
          
  
    -if !@bl
      %table.table{:style => "width:auto;float:right;border-top:0;"}
        
        %tr
          %th{:style => "border-top:0;"}
            Poids total :
            &nbsp;&nbsp;
          %td.numeraire{:style => "border-top:0;"}
            ="#{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
    -else
      
    
    
      %table.table{:style => "width:auto;float:right;border-top:0;"}
        
        %tr
          %th{:style => "border-top:0;"}
            Poids total :
            &nbsp;&nbsp;
          %td.numeraire{:style => "border-top:0;"}
            ="#{price_line_block.weight_tot}Kg"
      
      
      
    -if !@bl
      %br
      %br
      %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]
  -if @bl
    %div{:style => "width:60%;"}
      =simple_format price_line_block.bl_comment
    
      
      
    
  -if !@bl
    %br
    %br

    %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