-start_date = Time.parse("2015/06/30")

.container

  
  
  

  .stats
  
    
      
    %p
      Chiffre d'affaire aujourd'hui :
      -total_ht = Order.between(Time.now.beginning_of_day, Time.now.end_of_day).where("unpaid is null").where("payment_type_id = 2").where(:paid => true).sum(:price_ht)
      %strong{:style => "color:green"}
        =number_to_currency total_ht
        HT
      (
      =number_to_currency (total_ht*1.2)
      TTC
      )
    %p
      %strong
        Depuis le :
        =l start_date, :format => :date
    %p
      
      Chiffre d'affaire :
      -total_ht = Order.between(start_date, Time.now.end_of_day).where("unpaid is null").where("payment_type_id = 2", ((start_date).beginning_of_day)).where(:paid => true).sum(:price_ht)
      %strong{:style => "color:green"}
        =number_to_currency total_ht
        HT
      (
      =number_to_currency (total_ht*1.2)
      TTC
      )
    
    
    <div>
    <canvas id="ca_stats" height="200" width="600"></canvas>
    </div>

    -labels = []
    -datas_ht = []
    -datas_ttc = []
  
    -date = start_date.end_of_day
    
    
    -while date <= Time.now.end_of_day
      -labels << l(date, :format => "%a %d %b")
      -ht = Order.between(start_date,date).where("unpaid is null").where("payment_type_id = 2", ((start_date).beginning_of_day)).where(:paid => true).sum(:price_ht)
      
      -datas_ht << ht
      -datas_ttc << (ht * 1.2)
      -date = date.tomorrow.end_of_day




    <script>
    var lineChartData2 = {
    =raw"labels : #{labels.to_json},"
    datasets : [
    {
    label: "Chiffre HT",
    fillColor : "rgba(22,156,67,0.1)",
    strokeColor : "rgba(22,156,67,1)",
    pointColor : "rgba(22,156,67,1)",
    pointStrokeColor : "#fff",
    pointHighlightFill : "#fff",
    pointHighlightStroke : "rgba(220,220,220,1)",
    =raw"data : #{datas_ht.to_json}"

    },
    {
    label: "Chiffre TTC",
    fillColor : "rgba(20,133,201,0.1)",
    strokeColor : "rgba(20,133,201,1)",
    pointColor : "rgba(20,133,201,1)",
    pointStrokeColor : "#fff",
    pointHighlightFill : "#fff",
    pointHighlightStroke : "rgba(220,220,220,1)",
    =raw"data : #{datas_ttc.to_json}"

    }
    ]

    }


    var ctx2 = document.getElementById("ca_stats").getContext("2d");
    new Chart(ctx2).Line(lineChartData2, {
    responsive: true,
    bezierCurve : false
    });

    


    </script>
    
    
    %p
      TVA :
      %strong= number_to_currency (total_ht*0.2)
    %p
      25% directement commissionné à Alpinea pour commercialisation (ne prend pas en compte les coms non versées "bonus"):
      %br
      %strong= number_to_currency (total_ht*0.25) 
    %p
      Commissions directes les 7 derniers jours :
      -com_direct = Commission.where("created_at >= ?", ((start_date).beginning_of_day)).where(:commission_type_id => 1).sum(:amount)
      =number_to_currency com_direct
    %p
      Commissions binaires les 7 derniers jours :
      -com_binaire =  Commission.where("created_at >= ?", ((start_date).beginning_of_day)).where(:commission_type_id => 2).sum(:amount)
      =number_to_currency com_binaire
    
    %p 
      Cumul des comissions directes & binaires :
      %strong{:style => "color:orange"}= number_to_currency (com_binaire + com_direct)
   
    %p
      Solde point binaires 7 derniers jours (points sur les achats réalisés) :
      
      %strong=points_binaires = Order.points_binaires_between(start_date, Time.now.end_of_day)
    %p 
      20 % des points binaires :
      -com_part = Order.com_part_between(start_date, Time.now.end_of_day)
      %strong=number_to_currency com_part
      
    %p 
      Cumul comissions directes, binaires & participation :
      %strong{:style => "color:red"}= number_to_currency com_part + com_binaire + com_direct
      (
      = (com_part + com_binaire + com_direct - Commission.where("created_at >= ?", ((start_date).beginning_of_day)).where(:annonce_account_id => [1,2,5]).sum(:amount))
      en excluant les 3 premiers comptes
      )
    %p
      Pour contrôle :
      -c = Commission.where("created_at >= ?", ((start_date).beginning_of_day)).where(:commission_type_id => [1,2,3]).sum(:amount) 
      =number_to_currency c
    
    %p
      Soit 
      %strong=number_to_percentage ((com_part + com_binaire + com_direct)*100)/total_ht, precision: 1
      du chiffre d'affaire total HT
      ="."
    %p      
      %strong=number_to_percentage ((com_part + com_binaire + com_direct - Commission.where("created_at >= ?", ((start_date).beginning_of_day)).where(:annonce_account_id => [1,2,5]).sum(:amount))*100)/total_ht, precision: 1
      en ignorant les 3 premiers comptes.
    %p 
      Reste :
      %strong{:style => "color:green"}=number_to_currency (total_ht - (com_part + com_binaire + com_direct))
      
      
    %p
      Marges de sécurité de commissions (50% du CA HT - les coms à verser, servira aux investissements où bonus, où nouvelle commissions) :
      %br
      -securite = (total_ht * 0.5) - (com_part + com_binaire + com_direct)
      =number_to_currency(securite)
    %p
      divisé par deux :
      =number_to_currency(securite/2)
    
    %p
      Nombre de comptes :
      =AnnonceAccount.count
    %p
      Nombre d'inscrits dans le binaire :
      =AnnonceAccount.where("binary_parent_id IS NOT NULL").count
    %p
      Nombre de personnes non inscrite dans le binaire :
      =AnnonceAccount.where("binary_parent_id IS NULL").count
      
    %h3 premier essai, à vérifier ! : 
    %p
      Nombre de parts actives aujourd'hui 
      %strong (pas encore sûr du calcul) :

      - parts = Credit.parts_at(Date.today)
      %strong= parts
    %p 
      Parts pour controle
      -icont = 0.0
      -AnnonceAccount.all.each do |annonce_account|
        -if annonce_account.callif(Time.now)
          -icont = icont + annonce_account.callif(Time.now)[3]
      =icont
      
 
    
    
    %p
      Ce qui donnerait 
      
      %strong=number_to_currency com_part/parts
      par parts (donnée brut :
      =com_part/parts
      ).
      
    %hr
    
    %p Date de dernière distribution de coms
    %table.table
      %tr
        %th
          Date
        %th
          Nombre de parts
        %th
          Prix par parts
        %th
          Montant total
      -amount = 0.0
      -CountPartCom.where(:paid => true).order(:last_day).each do |cpc|
      
        %tr
          %td=l cpc.last_day.end_of_day
          %td=cpc.nbr_parts
          %td=number_to_currency cpc.part_price
          %td=number_to_currency cpc.control_amount
          -amount = amount + cpc.control_amount.to_f
      
      %tr
        %td{:colspan => 2}
        %td
          Montant total :
        %td=number_to_currency amount
    %p 
      Montant des coms parts versée pour controle
      %strong=number_to_currency Commission.where(:commission_type_id => 3).sum(:amount)
    
    %hr
      -last_cpc = CountPartCom.last_paid
      %p
        Date de début pour le calcul des nouvelles parts :
        %strong=start = CountPartCom.last_paid.last_day.tomorrow.beginning_of_day
      
      %p
        Date de fin pour le calcul des nouvelles parts :
        %strong=stop = CountPartCom.order("last_day DESC").first.last_day.end_of_day
    
      %p
        Nombre de parts à la date de fin :
        %strong=nbr_parts = Credit.parts_at(stop)
      
      
     
      
      
      %p
        Points binaires réalisés entre les deux dates :
        %strong=points = Order.points_binaires_between(start, stop)
      %p
        CA réalisé sur la période :
        %strong=number_to_currency ca = Order.between(start, stop).where("unpaid is null").where("payment_type_id = 2").where(:paid => true).sum(:price_ht)
      %p
        Com normale :
        %strong=number_to_currency Order.com_part_between(start, stop)
        (
        
        =number_to_percentage((Order.com_part_between(start, stop)*100)/ca, :precision => 2)
        
        du CA de la période)
        
        %br
        Soit
        =number_to_currency(Order.com_part_between(start, stop) / nbr_parts)
        part parts
        
      %p
        Com à 50% :
        -com = points * 0.5
        %strong=number_to_currency com
        (
        
        =number_to_percentage((com*100)/ca, :precision => 2)
        
        du CA de la période)
        %br
        Soit
        =number_to_currency(com / nbr_parts)
        par parts
      
      %p
        -com = points * 0.7
        
        Com à 70% :
        %strong=number_to_currency com
        (
        
        =number_to_percentage((com*100)/ca, :precision => 2)
        
        du CA de la période)
        
        %br
        Soit
        =number_to_currency(com / nbr_parts)
        par parts
      
      

      
    
  %h3 Evolution du nombre de comptes
  
  .chart_legend
    .pastille{:style => "background:rgba(22,156,67,1);"}
    Nombre de comptes
    
    .pastille{:style => "background:rgba(20,133,201,1);"}
    Nombre de comptes payants
    
    .pastille{:style => "background:rgba(239,75,41,1);"}
    Nombre de comptes gratuits
  
  <div>
  <canvas id="account_stats" height="300" width="600"></canvas>
  </div>

  -labels = []
  -datas = []
  -data_gratuits = []
  -data_payants = []

  -date = start_date.end_of_day
  -while date <= Time.now.end_of_day
    -labels << l(date, :format => "%a %d %b")
    -nbr = AnnonceAccount.where("created_at <= ?", date).count
    -nbr_payants = AnnonceAccount.where("binary_parent_id is not null and created_at <= ?", date).count
    -datas << nbr
    -data_payants << nbr_payants
    -data_gratuits << (nbr - nbr_payants)
    -date = date.tomorrow.end_of_day




  <script>
  var lineChartData = {
  =raw"labels : #{labels.to_json},"
  datasets : [
  {
  label: "Nombre de comptes",
  fillColor : "rgba(22,156,67,0.1)",
  strokeColor : "rgba(22,156,67,1)",
  pointColor : "rgba(22,156,67,1)",
  pointStrokeColor : "#fff",
  pointHighlightFill : "#fff",
  pointHighlightStroke : "rgba(220,220,220,1)",
  =raw"data : #{datas.to_json}"

  },
  {
  label: "Nombre de comptes payants",
  fillColor : "rgba(20,133,201,0.1)",
  strokeColor : "rgba(20,133,201,1)",
  pointColor : "rgba(20,133,201,1)",
  pointStrokeColor : "#fff",
  pointHighlightFill : "#fff",
  pointHighlightStroke : "rgba(220,220,220,1)",
  =raw"data : #{data_payants.to_json}"

  },
  {
  label: "Nombre de comptes gratuits",
  fillColor : "rgba(239,75,41,0.1)",
  strokeColor : "rgba(239,75,41,1)",
  pointColor : "rgba(239,75,41,1)",
  pointStrokeColor : "#fff",
  pointHighlightFill : "#fff",
  pointHighlightStroke : "rgba(220,220,220,1)",
  =raw"data : #{data_gratuits.to_json}"

  }
  ]

  }

  window.onload = function(){
  var ctx = document.getElementById("account_stats").getContext("2d");
  window.myLine = new Chart(ctx).Line(lineChartData, {
  responsive: true,
  bezierCurve : false
  });
  }


  </script>