.qi_header
  .right{:style => "text-align:right;"}
    = link_to i(:pencil), edit_admin_p_commercial_objective_path(@p_commercial_objective), :remote => false

  %h1
    =link_to "Obejctifs", admin_p_commercial_objectives_path
    %span
      Détail d'un objectif
    %span
      =link_to [:admin, @p_commercial_objective] do
        =@p_commercial_objective.p_commercial.long_name



.qi_row
  .qi_pannel.qi_plain.padding
    .h3
      Objectif Annuel :
      -@annual_start = @p_commercial_objective.start_at
      -@annual_stop = @p_commercial_objective.end_at
      -@annual_periode_lines = StatLine.where("date >= ? and date <= ?", @annual_start, @annual_stop)
      
      -@annual_periode_lines = @annual_periode_lines.where(p_commercial_id: @p_commercial_objective.p_commercial_id)
      -@ca_todo = @p_commercial_objective.total_ca
      -@ca_done = @annual_periode_lines.sum(:tot_amount_ht)
    %br
    .table-responsive
      %table.table
        %thead
          %tr
            %th{:scope => "col"}
              ID
            %th{:scope => "col"}
              Objectif €
            %th{:scope => "col"}
              Objectif kg
            %th{:scope => "col"}
              Date de début
            %th{:scope => "col"}
              Date de fin
            %th{:scope => "col"}
              Réalisé (€)
            %th{:scope => "col"}
              Réalisé (%)
            %th{:scope => "col"}
              Reste à faire (€)
            %th{:scope => "col"}
              Reste à faire (%)
        %tbody

          %tr
            %th{:scope => "col"}
              =@p_commercial_objective.id
            %td{:scope => "col"}
              =@p_commercial_objective.total_ca.to_i
              €
            %td
              =@p_commercial_objective.total_weight.to_i
              kg
            %td{:scope => "col"}
              =@p_commercial_objective.start_at
            %td{:scope => "col"}
              =@p_commercial_objective.end_at
            %td{:scope => "col"}
              =number_to_currency @ca_done, :precision => 0
            %td{:scope => "col"}
              #{(@ca_done.to_i / @p_commercial_objective.total_ca.to_i) * 100} %

            %td{:scope => "col"}
              =number_to_currency((@p_commercial_objective.total_ca.to_i - @ca_done.to_i), :precision => 0)

            %td{:scope => "col"}
              #{((@p_commercial_objective.total_ca.to_i - @ca_done.to_i) / @p_commercial_objective.total_ca.to_i) * 100 } %

.qi_row
  .qi_pannel.qi_plain.padding
    .h4
      Objectifs mensuels : Chiffre d'affaires
    %br
    .table-responsive
      %table.table
        %thead
          %tr
            %th{:scope => "col"}
              ID
            %th{:scope => "col"}
              Objectif (%)
            %th{:scope => "col"}
              Date de début
            %th{:scope => "col"}
              Date de fin
            %th{:scope => "col"}
              A réaliser
            %th{:scope => "col"}
              Réalisé
            %th{:scope => "col"}
              Reste à faire (€)
            %th{:scope => "col"}
              Reste à faire (%)
        %tbody
          -@p_commercial_object_months = PCommercialObjectMonth.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_month|
            -@start = object_month.start_at
            -@stop = object_month.end_at
            -@month_periode_lines = StatLine.where("date >= ? and date <= ?", @start, @stop)
            -@month_periode_lines = @month_periode_lines.where(p_commercial_id: object_month.p_commercial_id)
            -@ca_todo = number_to_currency (@p_commercial_objective.total_ca * object_month.objective_percent) / 100
            -@ca_done = number_to_currency (@month_periode_lines.sum(:tot_amount_ht)), :precision => 0


            %tr
              %th{:scope => "col"}
                =object_month.id
              %td{:scope => "col"}
                =object_month.objective_percent
                (%)
              %td{:scope => "col"}
                =object_month.start_at
              %td{:scope => "col"}
                =object_month.end_at
              %td{:scope => "col"}
                #{@ca_todo}

              -if @ca_todo >= @ca_done
                %td.text-danger{:scope => "col"}
                  #{@ca_done}
              -else
                %td.text-success{:scope => "col"}
                  #{@ca_done}
              %td{:scope => "col"}
                #{number_to_currency ((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) - @month_periode_lines.sum(:tot_amount_ht), :precision => 0}

              %td{:scope => "col"}
                #{((((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) - @month_periode_lines.sum(:tot_amount_ht)) / ((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) * 100).to_i} %


.qi_row
  .qi_pannel.qi_plain.padding
    .h4
      Objectifs mensuels : Poids
    %br
    .table-responsive
      %table.table
        %thead
          %tr
            %th{:scope => "col"}
              ID
            %th{:scope => "col"}
              Objectif (%)
            %th{:scope => "col"}
              Date de début
            %th{:scope => "col"}
              Date de fin
            %th{:scope => "col"}
              A réaliser
            %th{:scope => "col"}
              Réalisé
            %th{:scope => "col"}
              Reste à faire (kg)
            %th{:scope => "col"}
              Reste à faire (%)

        %tbody
          -@p_commercial_object_months = PCommercialObjectMonth.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_month|
            -@start = object_month.start_at
            -@stop = object_month.end_at
            -@month_periode_lines = StatLine.where("date >= ? and date <= ?", @start, @stop)
            -@month_periode_lines = @month_periode_lines.where(p_commercial_id: object_month.p_commercial_id)
            -@weight_todo = (@p_commercial_objective.total_weight * object_month.objective_percent) / 100
            -@weight_done = @month_periode_lines.sum(:weight_tot)


            %tr
              %th{:scope => "col"}
                =object_month.id
              %td{:scope => "col"}
                =object_month.objective_percent
                (%)
              %td{:scope => "col"}
                =object_month.start_at
              %td{:scope => "col"}
                =object_month.end_at
              %td{:scope => "col"}
                #{@weight_todo} kg

              -if @weight_todo >= @weight_done
                %td.text-danger{:scope => "col"}
                  #{@weight_done} kg
              -else
                %td.text-success{:scope => "col"}
                  #{@weight_done} kg
              %td
                #{@weight_todo - @weight_done} kg
              %td
                #{(((@weight_todo - @weight_done) / @weight_todo) * 100).to_i} %


.qi_row
  .qi_pannel.qi_plain.padding
    .h4
      Objectifs par marque de produit : Chiffre d'affaires
    %br
    .table-responsive
      %table.table
        %thead
          %tr
            %th{:scope => "col"}
              ID
            %th
              Catégorie
            %th{:scope => "col"}
              Objectif (%)
            %th{:scope => "col"}
              A réaliser
            %th{:scope => "col"}
              Réalisé
            %th{:scope => "col"}
              Reste à faire (€)
            %th{:scope => "col"}
              Reste à faire (%)
        %tbody
        -@p_commercial_object_cats = PCommercialObjectBrand.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_brand|
          -@categorie = SBrand.where(id: object_brand.s_brand_id).each do |brand|
            -@brand_start = @p_commercial_objective.start_at
            -@brand_stop = @p_commercial_objective.end_at
            -@brand_periode_lines = StatLine.where("date >= ? and date <= ?", @brand_start, @brand_stop)
            -@brand_periode_lines = @brand_periode_lines.where(p_commercial_id: object_brand.p_commercial_id, s_brand_id: object_brand.s_brand_id)
            -@brand_ca_todo = (@p_commercial_objective.total_ca * object_brand.objective_percent) / 100
            -@brand_ca_done = (@brand_periode_lines.sum(:tot_amount_ht))

            %tr
              %th{:scope => "col"}
                =object_brand.id
              %td{:scope => "col"}
                =brand.name
              %td{:scope => "col"}
                =object_brand.objective_percent
                (%)
              %td{:scope => "col"}
                #{@brand_ca_todo}
            
              -if @brand_ca_todo >= @brand_ca_done
                %td.text-danger{:scope => "col"}
                  =number_to_currency @brand_ca_done, :precision => 0
              -else
                %td.text-success{:scope => "col"}
                  =number_to_currency @brand_ca_done, :precision => 0
              %td
                #{number_to_currency ((@p_commercial_objective.total_ca * object_brand.objective_percent) / 100) - @brand_periode_lines.sum(:tot_amount_ht), :precision => 0 }
              %td
                #{((((@p_commercial_objective.total_ca * object_brand.objective_percent) / 100) - @brand_periode_lines.sum(:tot_amount_ht)) / ((@p_commercial_objective.total_ca * object_brand.objective_percent) / 100) * 100).to_i} %


.qi_row
  .qi_pannel.qi_plain.padding
    .h4
      Objectifs par marque de produit : Poids
    %br
    .table-responsive
      %table.table
        %thead
          %tr
            %th{:scope => "col"}
              ID
            %th
              Catégorie
            %th{:scope => "col"}
              Objectif (%)
            %th{:scope => "col"}
              A réaliser
            %th{:scope => "col"}
              Réalisé
            %th{:scope => "col"}
              Reste à faire (kg)
            %th{:scope => "col"}
              Reste à faire (%)
        %tbody
        -@p_commercial_object_cats = PCommercialObjectBrand.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_brand|
          -@brand = SBrand.where(id: object_brand.s_brand_id).each do |brand|
            -@brand_start = @p_commercial_objective.start_at
            -@brand_stop = @p_commercial_objective.end_at
            -@brand_periode_lines = StatLine.where("date >= ? and date <= ?", @brand_start, @brand_stop)
            -@brand_periode_lines = @brand_periode_lines.where(p_commercial_id: object_brand.p_commercial_id, s_brand_id: object_brand.s_brand_id)
            -@brand_weight_todo = (@p_commercial_objective.total_weight * object_brand.weight_objective) / 100
            -@brand_weight_done = (@brand_periode_lines.sum(:weight_tot))

            %tr
              %th{:scope => "col"}
                =object_brand.id
              %td{:scope => "col"}
                =brand.name
              %td{:scope => "col"}
                =object_brand.weight_objective
                (%)
              %td{:scope => "col"}
                #{@brand_weight_todo} kg

              -if @brand_weight_todo >= @brand_weight_done
                %td.text-danger{:scope => "col"}
                  #{@brand_weight_done} kg
              -else
                %td.text-success{:scope => "col"}
                  #{@brand_weight_done} kg
              %td
                #{@weight_todo - @weight_done} kg
              %td
                #{(((@weight_todo - @weight_done) / @weight_todo) * 100).to_i} %


.h4{:style => "margin-left:20px;"}
  Objectifs mensuel par catégorie:
-@p_commercial_object_months = PCommercialObjectMonth.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_month|
  -@month_start = object_month.start_at
  -@month_stop = object_month.end_at
  -@month_periode_lines = StatLine.where("date >= ? and date <= ?", @month_start, @month_stop)
  -@month_periode_lines = @month_periode_lines.where(p_commercial_id: object_month.p_commercial_id)
  -@month_ca_todo = number_to_currency (@p_commercial_objective.total_ca * object_month.objective_percent) / 100
  -@month_ca_done = number_to_currency (@month_periode_lines.sum(:tot_amount_ht)), :precision => 0
  -@month_weight_todo = (@p_commercial_objective.total_weight * object_month.weight_objective) / 100
  -@month_weight_done = @month_periode_lines.sum(:weight_tot)

  .h4{:style => "margin-left:20px;"}
    =l(@month_start, :format => "%B").capitalize
  .qi_row
    .qi_pannel.qi_plain.padding
      %br
      .h4
        Chiffre d'affaires
      .table-responsive
        %table.table
          %thead
            %tr
              %th{:scope => "col"}
                ID
              %th{:scope => "col"}
                Objectif (%)
              %th{:scope => "col"}
                Date de début
              %th{:scope => "col"}
                Date de fin
              %th{:scope => "col"}
                A réaliser
              %th{:scope => "col"}
                Réalisé
              %th{:scope => "col"}
                Reste à faire (€)
              %th{:scope => "col"}
                Reste à faire (%)

          %tbody
            %tr
              %th{:scope => "col"}
                =object_month.id
              %td{:scope => "col"}
                =object_month.objective_percent
                (%)
              %td{:scope => "col"}
                =object_month.start_at
              %td{:scope => "col"}
                =object_month.end_at
              %td{:scope => "col"}
                #{@month_ca_todo}

              -if @month_ca_todo >= @month_ca_done
                %td.text-danger{:scope => "col"}
                  #{@month_ca_done}
              -else
                %td.text-success{:scope => "col"}
                  #{@month_weight_done}
              %td
                #{((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) - @month_periode_lines.sum(:tot_amount_ht)} €
              %td
                #{((((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) - @month_periode_lines.sum(:tot_amount_ht)) / ((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) * 100).to_i} %
        .table-responsive
          %table.table
            %thead
              %th{:scope => "col"}
              %th{:scope => "col"}

              %th
                Catégorie
              %th{:scope => "col"}
                Objectif (%)
              %th{:scope => "col"}
                A réaliser
              %th{:scope => "col"}
                Réalisé
              %th{:scope => "col"}
                Reste à faire (€)
              %th{:scope => "col"}
                Reste à faire (%)
            %tbody
              -@p_commercial_object_brands = PCommercialObjectBrand.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_brand|
                -@brand = SBrand.where(id: object_brand.s_brand_id).each do |brand|
                  -@month_brand_periode_lines = @month_periode_lines.where(p_commercial_id: object_brand.p_commercial_id, s_brand_id: object_brand.s_brand_id)
                  -@month_brand_ca_todo = number_to_currency (((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) * object_brand.objective_percent) / 100
                  -@month_brand_ca_done = number_to_currency (@month_brand_periode_lines.sum(:tot_amount_ht)), :precision => 0
                  -@objective_brand_month_percent = (object_month.objective_percent * object_brand.objective_percent) / 100
                  -@months_brand_ca_todo =  ((@p_commercial_objective.total_ca * object_month.objective_percent / 100) * object_brand.objective_percent) / 100
                  -@months_brand_ca_done =  @month_brand_periode_lines.sum(:tot_amount_ht)

                  %tr
                    %th{:scope => "col"}
                      / =object_brand.id
                    %td{:scope => "col"}

                    %td{:scope => "col"}
                      =brand.name
                    %td{:scope => "col"}
                      #{@objective_brand_month_percent}
                      (%)
                    %td{:scope => "col"}
                      #{@month_brand_ca_todo}

                    -if @month_brand_ca_todo > @month_brand_ca_done
                      %td.text-danger{:scope => "col"}
                        #{@month_brand_ca_done}
                    -else
                      %td.text-success{:scope => "col"}
                        #{@month_brand_ca_done}
                    %td
                      =number_to_currency ((((@p_commercial_objective.total_ca * object_month.objective_percent) / 100) * object_brand.objective_percent) / 100 - @month_brand_periode_lines.sum(:tot_amount_ht)), :precision => 0
                    %td
                      #{(((@months_brand_ca_todo - @months_brand_ca_done) / @months_brand_ca_todo) * 100).to_i} %
  .qi_row
    .qi_pannel.qi_plain.padding
      .h4
        Poids
      .table-responsive
        %table.table
          %thead
            %tr
              %th{:scope => "col"}
                ID
              %th{:scope => "col"}
                Objectif (%)
              %th{:scope => "col"}
                Date de début
              %th{:scope => "col"}
                Date de fin
              %th{:scope => "col"}
                A réaliser
              %th{:scope => "col"}
                Réalisé
              %th{:scope => "col"}
                Reste à faire (kg)
              %th{:scope => "col"}
                Reste à faire (%)

          %tbody
            %tr
              %th{:scope => "col"}
                =object_month.id
              %td
                =object_month.weight_objective
                (%)
              %td{:scope => "col"}
                =object_month.start_at
              %td{:scope => "col"}
                =object_month.end_at
              %td{:scope => "col"}
                #{@month_weight_todo} kg

              -if @month_weight_todo >= @month_weight_done
                %td.text-danger{:scope => "col"}
                  #{@month_weight_done} kg
              -else
                %td.text-success{:scope => "col"}
                  #{@month_weight_done} kg
              %td{:scope => "col"}
                #{@month_weight_todo - @month_weight_done} kg
              %td{:scope => "col"}
                #{(((@month_weight_todo - @month_weight_done) / @month_weight_todo) * 100).to_i} %

        .table-responsive
          %table.table
            %thead
              %th{:scope => "col"}
              %th{:scope => "col"}

              %th
                Catégorie
              %th{:scope => "col"}
                Objectif (%)
              %th{:scope => "col"}
                A réaliser
              %th{:scope => "col"}
                Réalisé
              %th{:scope => "col"}
                Reste à faire (kg)
              %th{:scope => "col"}
                Reste à faire (%)
            %tbody
              -@p_commercial_object_brands = PCommercialObjectBrand.where(p_commercial_objective_id: @p_commercial_objective.id).each do |object_brand|
                -@brand = SBrand.where(id: object_brand.s_brand_id).each do |brand|
                  -@month_brand_periode_lines = @month_periode_lines.where(p_commercial_id: object_brand.p_commercial_id, s_brand_id: object_brand.s_brand_id)
                  -@month_brand_weight_todo = (((@p_commercial_objective.total_weight * object_month.weight_objective) / 100) * object_brand.objective_percent) / 100
                  -@month_brand_weight_done = (@month_brand_periode_lines.sum(:weight_tot))
                  -@objective_brand_month_weight_objective = (object_month.weight_objective * object_brand.weight_objective) / 100

                  %tr
                    %th{:scope => "col"}
                      / =object_brand.id
                    %td{:scope => "col"}

                    %td{:scope => "col"}
                      =brand.name
                    %td
                      #{@objective_brand_month_weight_objective}
                      (%)
                    %td{:scope => "col"}
                      #{@month_brand_weight_todo} kg

                    -if @month_brand_weight_todo > @month_brand_weight_done
                      %td.text-danger{:scope => "col"}
                        #{@month_brand_weight_done} kg
                    -else
                      %td.text-success{:scope => "col"}
                        #{@month_brand_weight_done} kg
                    %td{:scope => "col"}
                      #{@month_brand_weight_todo - @month_brand_weight_done} kg
                    %td{:scope => "col"}
                      #{(((@month_brand_weight_todo - @month_brand_weight_done) / @month_brand_weight_todo) * 100 ).to_i} %
    %hr





