Class: SalesCommission

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/sales_commission.rb

Overview

== Schema Information

Table name: sales_commissions
Database name: primary

id :integer not null, primary key
co_cm_bd :float
cogs :float
commission_date :date not null
employee_rank :integer not null
local_sales_rep :boolean
net_base :float
num_orders :integer not null
profit :float
sales :float
state :string
techs_present :integer
tier1 :float
tier2 :float
tier3 :float
tier4 :float
tier5 :float
tier6 :float
tier7 :float
tier8 :float
total :float
created_at :datetime not null
updated_at :datetime not null
creator_id :integer
emp_comp_id :integer not null
employee_id :integer not null
updater_id :integer

Indexes

by_empid_comm_date (employee_id,commission_date)
idx_state_commission_date (state,commission_date)
index_sales_commissions_on_commission_date (commission_date)
index_sales_commissions_on_emp_comp_id (emp_comp_id)
index_sales_commissions_on_employee_rank (employee_rank)
index_sales_commissions_on_local_sales_rep (local_sales_rep)
index_sales_commissions_on_techs_present (techs_present)

Constant Summary collapse

COMMISSION_TIER =
[
  ['Profitability equal or less than 60%', 1],
  # ['Profitability between 65% and 69.99%',2],
  ['Profitability more than 60%', 3],
  # ['New business activation',4],
  ['Local Rep Engagement', 5],
  # ['Commission for specific products',5],
  ['Tech Agent Engagement (Full or Shared)', 6],
  ['Ferguson Sales Support', 7],
  ['Sales Support Rep Engagement', 8]
]

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #creator, #should_not_save_version, #stamp_record, #updater

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.all_states_for_selectObject



964
965
966
# File 'app/models/sales_commission.rb', line 964

def self.all_states_for_select
  state_machines[:state].states.map(&:name)
end

.populate_sales_commission(start_date, end_date) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/sales_commission.rb', line 105

def self.populate_sales_commission(start_date, end_date)
  delete_scdetail_sql = <<-SQL
    delete from sales_commission_details
    where commission_date between '#{start_date}' and '#{end_date}'
    and sales_commission_id in (select id from sales_commissions
                                where commission_date between '#{start_date}' and '#{end_date}'
                                and state = 'draft')
  SQL
  ActiveRecord::Base.connection.execute(delete_scdetail_sql)

  delete_net_base_sql = <<-SQL
    delete from sales_commission_net_bases
    where commission_date between '#{start_date}' and '#{end_date}'
    and sales_commission_id in (select id from sales_commissions
                                where commission_date between '#{start_date}' and '#{end_date}'
                                and state = 'draft')
  SQL
  ActiveRecord::Base.connection.execute(delete_net_base_sql)

  delete_net_base_detail_sql = <<-SQL
    delete from sales_commission_net_base_details
    where commission_date between '#{start_date}' and '#{end_date}'
    and sales_commission_id in (select id from sales_commissions
                                where commission_date between '#{start_date}' and '#{end_date}'
                                and state = 'draft')
  SQL
  ActiveRecord::Base.connection.execute(delete_net_base_detail_sql)

  delete_sql = <<-SQL
    delete from sales_commissions
    where commission_date between '#{start_date}' and '#{end_date}'
    and state = 'draft'
  SQL
  ActiveRecord::Base.connection.execute(delete_sql)

  sql = <<-SQL
    select distinct employee_id
    from commission_rates
    where employee_id not in (select distinct employee_id
                              from sales_commissions
                              where commission_date between '#{start_date}' and '#{end_date}'
                              and state <> 'draft')
    order by employee_id
  SQL
  sql_result = ActiveRecord::Base.connection.execute(sql).to_a

  if sql_result.empty? == false
    employee_ids = sql_result.map { |r| r['employee_id'].to_i }.join(',')
    sales_rep_data(start_date, end_date, employee_ids)
  end

  employees_in_draft = SalesCommission.where(commission_date: start_date..end_date).where(state: 'draft').select(:id, :employee_id, :emp_comp_id)
  employees_in_draft.each do |ed|
    sales_rep_data_detail(start_date, end_date, ed[:employee_id], ed[:id])
    sales_rep_net_base(start_date, end_date, ed[:employee_id], ed[:emp_comp_id], ed[:id])
    sales_rep_net_base_detail(start_date, end_date, ed[:employee_id], ed[:id])
  end
end

.repopulate_net_base_tables(date, employee_id, emp_comp_id, sc_id, updater_id) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/sales_commission.rb', line 164

def self.repopulate_net_base_tables(date, employee_id, emp_comp_id, sc_id, updater_id)
  delete_scdetail_sql = <<-SQL
    delete from sales_commission_details
    where sales_commission_id = #{sc_id}
  SQL
  ActiveRecord::Base.connection.execute(delete_scdetail_sql)

  delete_net_base_sql = <<-SQL
    delete from sales_commission_net_bases
    where sales_commission_id = #{sc_id}
  SQL
  ActiveRecord::Base.connection.execute(delete_net_base_sql)

  update_sales_rep_data_detail(date, employee_id, sc_id, updater_id)
  update_sales_rep_net_base(date, employee_id, emp_comp_id, sc_id, updater_id)
end

.sales_rep_data(start_date, end_date, employee_ids) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'app/models/sales_commission.rb', line 181

def self.sales_rep_data(start_date, end_date, employee_ids)
  prior_start_date = end_date.months_ago(6).beginning_of_month
  prior_end_date = end_date.months_ago(6).end_of_month

  sql = <<-SQL
    with xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
        and month = #{end_date.month.to_i}

    ), data (gl_date,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,cogs_value,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,case when sales_value = 0 then 0 else cogs_value end as cogs_value,
            tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        --and resource_type = 'SO'
        union all
        select gl_date,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,
        ((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
        (case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value,
            null,null,null,null,null,null,null,null,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'

    ), bd_data as (
        select distinct employee_id,reference_number,order_id,net_base,total,case when net_base = 0 then 0 else (total / net_base) end as prev_commission_rate
      from sales_commission_details
      where order_id in (select order_id
                    from view_sales_net_bases
                    where gl_date between '#{start_date}' and '#{end_date}'
                    and employee_id in (#{employee_ids})
                    and resource_type not in ('SO')
                    union all
                    select order_id
                    from view_sales_net_bases
                    where state = 'unpaid'
                    and resource_type = 'SO'
                    and employee_id in (#{employee_ids})
                    and due_date between '#{prior_start_date}' and '#{prior_end_date}')

    ), data_xrate (gl_date,employee_id,emp_comp_id,channel,order_id,sales,cogs,profit,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,employee_id,emp_comp_id,channel,order_id,
            case when company_id = 1 then sales_value * 1
                  when company_id = 2 then sales_value * cad_to_usd else 0 end as sales,
            case when company_id = 1 then cogs_value * 1
                  when company_id = 2 then cogs_value * cad_to_usd else 0 end as cogs,
            case when company_id = 1 then (sales_value - cogs_value) * 1
                  when company_id = 2 then (sales_value - cogs_value) * cad_to_usd else 0 end as profit,
            case when company_id = 1 and tier1 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier1 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier1,
            case when company_id = 1 and tier2 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier2 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier2,
            case when company_id = 1 and tier3 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier3 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier3,
            case when company_id = 1 and tier4 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier4 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier4,
            case when company_id = 1 and tier5 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier5 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier5,
            case when company_id = 1 and tier6 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier6 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier6,
            case when company_id = 1 and tier7 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier7 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier7,
            case when company_id = 1 and tier8 = true then (sales_value - cogs_value) * 1
                  when company_id = 2 and tier8 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier8,
            techs_present,local_sales_rep,sales_support_rep
        from data,xrate

    ), com_rate (employee_id,comm_tier1,comm_tier2,comm_tier3,comm_tier4,comm_tier5,comm_tier6,comm_tier7,comm_tier8) as (
        select employee_id,
            sum(case when commission_tier_id = 1 then commission_rate else 0 end) as comm_tier1,
            sum(case when commission_tier_id = 2 then commission_rate else 0 end) as comm_tier2,
            sum(case when commission_tier_id = 3 then commission_rate else 0 end) as comm_tier3,
            sum(case when commission_tier_id = 4 then commission_rate else 0 end) as comm_tier4,
            sum(case when commission_tier_id = 5 then commission_rate else 0 end) as comm_tier5,
            sum(case when commission_tier_id = 6 then commission_rate else 0 end) as comm_tier6,
            sum(case when commission_tier_id = 7 then commission_rate else 0 end) as comm_tier7,
            sum(case when commission_tier_id = 8 then commission_rate else 0 end) as comm_tier8
        from commission_rates
        where is_active = true
        group by employee_id

    ), data_total (employee_id,channel,profit_t) as (
        select employee_id,channel,sum(profit) as profit_t
        from data_xrate
        group by employee_id,channel

    ), unique_channel as (
        select distinct employee_id,channel,emp_comp_id
        from data_xrate
        where employee_id in (#{employee_ids})

    ), data_order (gl_date,employee_rank,channel_num,channel2,employee_id,emp_comp_id,channel,reference_number,order_id,sales,cogs,profit,co_cm_bd,net_base,tier1_amount,comm_tier1,tier1,tier2_amount,comm_tier2,tier2,tier3_amount,comm_tier3,tier3,tier4_amount,comm_tier4,tier4,tier5_amount,comm_tier5,tier5,tier6_amount,comm_tier6,tier6,tier7_amount,comm_tier7,tier7,tier8_amount,comm_tier8,tier8,total,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,(DENSE_RANK() OVER (order by uc.employee_id)) as employee_rank,
        (DENSE_RANK() OVER (order by uc.channel)) as channel_num,
        ((DENSE_RANK() OVER (order by uc.employee_id))::varchar || (DENSE_RANK() OVER (order by dx.channel))::varchar || '-' || dx.channel) as channel2,
        uc.employee_id,uc.emp_comp_id,uc.channel,o.reference_number,dx.order_id,dx.sales,dx.cogs,dx.profit,
        coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0) as co_cm_bd,dx.profit as net_base,
        (((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) as tier1_amount,comm_tier1,((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (comm_tier1 / 100)) as tier1,
        (((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier2) as tier2_amount,comm_tier2,((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier2) * (comm_tier2 / 100)) as tier2,
        (((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) as tier3_amount,comm_tier3,((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (comm_tier3 / 100)) as tier3,
        (((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier4) as tier4_amount,comm_tier4,((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier4) * (comm_tier4 / 100)) as tier4,
        (((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier5) as tier5_amount,comm_tier5,((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier5) * (comm_tier5 / 100)) as tier5,
        (((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) as tier6_amount,case when techs_present = 2 then comm_tier6 / techs_present else comm_tier6 end as comm_tier6,
        case when techs_present = 0 then 0 else (((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (comm_tier6 / 100)) / techs_present) end as tier6,
        (((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) as tier7_amount,comm_tier7,((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (comm_tier7 / 100)) as tier7,
        (((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) as tier8_amount,comm_tier8,((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (comm_tier8 / 100)) as tier8,
        (((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (comm_tier1 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier2) * (comm_tier2 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (comm_tier3 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier4) * (comm_tier4 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier5) * (comm_tier5 / 100)) +
        case when techs_present = 0 then 0 else (((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (comm_tier6 / 100)) / techs_present) end +
        ((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (comm_tier7 / 100)) +
        coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0) +
        ((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (comm_tier8 / 100)) ) as total,
        techs_present,local_sales_rep,sales_support_rep
        from unique_channel uc
        left join data_xrate dx on uc.employee_id = dx.employee_id and uc.channel = dx.channel
        left join com_rate cr on dx.employee_id = cr.employee_id
        left join bd_data bd on uc.employee_id = bd.employee_id and dx.order_id = bd.order_id
        left join data_total da on uc.employee_id = da.employee_id and uc.channel = da.channel
        left join orders o on dx.order_id = o.id
        order by dx.employee_id,dx.channel,dx.profit desc

    )
    insert into sales_commissions (commission_date,employee_rank,employee_id,emp_comp_id,num_orders,sales,cogs,profit,co_cm_bd,net_base,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,total,state,created_at,updated_at)
    select '#{end_date}'::date as commission_date,employee_rank,employee_id,emp_comp_id,
    count(distinct order_id) as num_orders,sum(coalesce(sales,0)) as sales,sum(coalesce(cogs,0)) as cogs,sum(coalesce(profit,0)) as profit,sum(coalesce(co_cm_bd,0)) as co_cm_bd,sum(coalesce(net_base,0)) as net_base,
    sum(coalesce(tier1,0)) as tier1,sum(0) /*sum(coalesce(tier2,0))*/ as tier2,sum(coalesce(tier3,0)) as tier3,sum(0) /*sum(coalesce(tier4,0))*/ as tier4,sum(coalesce(tier5,0)) as tier5,
    sum(coalesce(tier6,0)) as tier6,sum(coalesce(tier7,0)) as tier7,sum(coalesce(tier8,0)) as tier8,sum(coalesce(total,0)) as total,'draft' as state,timezone('America/Chicago', now()),timezone('America/Chicago', now())
    from data_order dr
    where dr.employee_id in (#{employee_ids})
    --and gl_date is not null
    group by employee_rank,employee_id,emp_comp_id;
  SQL

  ActiveRecord::Base.connection.execute(sql)
end

.sales_rep_data_detail(start_date, end_date, employee_id, sc_id) ⇒ Object



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'app/models/sales_commission.rb', line 476

def self.sales_rep_data_detail(start_date, end_date, employee_id, sc_id)
  prior_start_date = end_date.months_ago(6).beginning_of_month
  prior_end_date = end_date.months_ago(6).end_of_month

  sql = <<-SQL
    with xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
        and month = #{end_date.month.to_i}

    ), data (gl_date,customer_id,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,cogs_value,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,customer_id,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,case when sales_value = 0 then 0 else coalesce(cogs_value,0) end as cogs_value,
            tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        and employee_id = #{employee_id}
        --and resource_type = 'SO'
        union all
        select gl_date,customer_id,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,
        ((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
        (case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value,
            null,null,null,null,null,null,null,null,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and employee_id = #{employee_id}
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'

    ), data_xrate (gl_date,employee_id,emp_comp_id,channel,order_id,sales,cogs,profit,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,employee_id,emp_comp_id,channel,order_id,
        case when company_id = 1 then sales_value * 1
          when company_id = 2 then sales_value * cad_to_usd else 0 end as sales,
        case when company_id = 1 then cogs_value * 1
          when company_id = 2 then cogs_value * cad_to_usd else 0 end as cogs,
        case when company_id = 1 then (sales_value - cogs_value) * 1
          when company_id = 2 then (sales_value - cogs_value) * cad_to_usd else 0 end as profit,
        case when company_id = 1 and tier1 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier1 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier1,
        case when company_id = 1 and tier2 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier2 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier2,
        case when company_id = 1 and tier3 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier3 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier3,
        case when company_id = 1 and tier4 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier4 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier4,
        case when company_id = 1 and tier5 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier5 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier5,
        case when company_id = 1 and tier6 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier6 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier6,
        case when company_id = 1 and tier7 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier7 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier7,
        case when company_id = 1 and tier8 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier8 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier8,
        techs_present,local_sales_rep,sales_support_rep
        from data,xrate

    ), bd_data as (
        select employee_id,reference_number,order_id,net_base,total,case when net_base = 0 then 0 else (total / net_base) end as prev_commission_rate
      from sales_commission_details
      where order_id in (select order_id
              from view_sales_net_bases
              where gl_date between '#{start_date}' and '#{end_date}'
              and employee_id = #{employee_id}
              and resource_type not in ('SO')
              union all
              select order_id
              from view_sales_net_bases
              where state = 'unpaid'
              and resource_type = 'SO'
              and employee_id = #{employee_id}
              and due_date between '#{prior_start_date}' and '#{prior_end_date}')

    ), com_rate (employee_id,tier1_comm,tier2_comm,tier3_comm,tier4_comm,tier5_comm,tier6_comm,tier7_comm,tier8_comm) as (
        select employee_id,
        sum(case when commission_tier_id = 1 then commission_rate else 0 end) as tier1_comm,
        sum(case when commission_tier_id = 2 then commission_rate else 0 end) as tier2_comm,
        sum(case when commission_tier_id = 3 then commission_rate else 0 end) as tier3_comm,
        sum(case when commission_tier_id = 4 then commission_rate else 0 end) as tier4_comm,
        sum(case when commission_tier_id = 5 then commission_rate else 0 end) as tier5_comm,
        sum(case when commission_tier_id = 6 then commission_rate else 0 end) as tier6_comm,
        sum(case when commission_tier_id = 7 then commission_rate else 0 end) as tier7_comm,
        sum(case when commission_tier_id = 8 then commission_rate else 0 end) as tier7_comm
        from commission_rates
        where employee_id = #{employee_id}
        and is_active = true
        group by employee_id

    ), unique_channel as (
        select distinct employee_id,channel,emp_comp_id
        from data_xrate
        where employee_id = #{employee_id}

    ), data_total (employee_id,channel,profit_t) as (
        select employee_id,channel,sum(profit) as profit_t
        from data_xrate
        group by employee_id,channel

    )
    insert into sales_commission_details (commission_date,sales_commission_id,gl_date,employee_id,emp_comp_id,customer_id,customer_name,rank,channel,reference_number,order_id,sales,cogs,profit,co_cm_bd,net_base,tier1_amount,tier1_comm,tier1,tier2_amount,tier2_comm,tier2,tier3_amount,tier3_comm,tier3,tier4_amount,tier4_comm,tier4,tier5_amount,tier5_comm,tier5,tier6_amount,tier6_comm,tier6,tier7_amount,tier7_comm,tier7,tier8_amount,tier8_comm,tier8,total,techs_present,local_sales_rep,sales_support_rep,created_at,updated_at)
    select '#{end_date}'::date as commission_date,#{sc_id} as sales_commission_id,coalesce(dx.gl_date,'#{end_date}'::date) as gl_date,uc.employee_id,uc.emp_comp_id,coalesce(o.customer_id,0) as customer_id,coalesce(full_name,'') as name,(row_number() over (partition by uc.channel order by uc.channel,dx.profit desc)) as rank,uc.channel,coalesce(o.reference_number,'BD') as reference_number,coalesce(dx.order_id,0) as order_id,coalesce(dx.sales,0) as sales,coalesce(dx.cogs,0) as cogs,coalesce(dx.profit,0) as profit,
    coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0) as co_cm_bd,(dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) as net_base,
    coalesce((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1),0) as tier1_amount,coalesce(tier1_comm,0) as tier1_comm,coalesce(((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (tier1_comm / 100)),0) as tier1,
    0 as tier2_amount,0 as tier2_comm,0 as tier2,
    /*coalesce((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier2),0) as tier2_amount,coalesce(tier2_comm,0) as tier2_comm,coalesce(((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier2) * (tier2_comm / 100)),0) as tier2,*/
    coalesce((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3),0) as tier3_amount,coalesce(tier3_comm,0) as tier3_comm,coalesce(((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (tier3_comm / 100)),0) as tier3,
    0 as tier4_amount,0 as tier4_comm,0 as tier4,
    /*coalesce((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier4),0) as tier4_amount,coalesce(tier4_comm,0) as tier4_comm,coalesce(((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier4) * (tier4_comm / 100)),0) as tier4,*/
    /*0 as tier5_amount,0 as tier5_comm,0 as tier5,*/
    coalesce((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier5),0) as tier5_amount,coalesce(tier5_comm,0) as tier5_comm,coalesce(((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier5) * (tier5_comm / 100)),0) as tier5,
    coalesce((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6),0) as tier6_amount,coalesce(case when techs_present = 2 then tier6_comm / techs_present else tier6_comm end,0) as tier6_comm,case when techs_present = 0 then 0 else coalesce(((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (tier6_comm / 100)),0) / techs_present end as tier6,
    /*0 as tier7_amount,0 as tier7_comm,0 as tier7,*/
    coalesce((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7),0) as tier7_amount,coalesce(tier7_comm,0) as tier7_comm,coalesce(((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (tier7_comm / 100)),0) as tier7,
    coalesce((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8),0) as tier8_amount,coalesce(tier8_comm,0) as tier8_comm,coalesce(((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (tier8_comm / 100)),0) as tier8,
    coalesce(((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (tier1_comm / 100)) +
    /*((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier2) * (tier2_comm / 100)) +*/
    ((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (tier3_comm / 100)) +
    /*((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier4) * (tier4_comm / 100)) +*/
    ((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier5) * (tier5_comm / 100)) +
    case when techs_present = 0 then 0 else ((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (tier6_comm / 100)) / techs_present end +
    ((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (tier7_comm / 100)) +
    ((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (tier8_comm / 100)) +
    coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0),0) as total,
    techs_present,local_sales_rep,sales_support_rep,timezone('America/Chicago', now()) as created_at,timezone('America/Chicago', now()) as updated_at
    from unique_channel uc
    left join data_xrate dx on uc.employee_id = dx.employee_id and uc.channel = dx.channel
    left join com_rate cr on dx.employee_id = cr.employee_id
    left join bd_data bd on uc.employee_id = bd.employee_id and dx.order_id = bd.order_id
    left join data_total da on uc.employee_id = da.employee_id and uc.channel = da.channel
    left join orders o on dx.order_id = o.id
    left join parties p on o.customer_id = p.id
    order by uc.channel,dx.profit desc;
  SQL

  ActiveRecord::Base.connection.execute(sql)
end

.sales_rep_net_base(start_date, end_date, employee_id, emp_comp_id, sc_id) ⇒ Object



754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
# File 'app/models/sales_commission.rb', line 754

def self.sales_rep_net_base(start_date, end_date, employee_id, emp_comp_id, sc_id)
  prior_start_date = end_date.months_ago(6).beginning_of_month
  prior_end_date = end_date.months_ago(6).end_of_month

  sql = <<-SQL
    with data (sorted,code,type,emp_comp_id,company_id,num_orders,sales_value,cogs_value) as (
        select 1 as sorted,'SO' as code,'Sales' as type,#{emp_comp_id} as emp_comp_id,1 as currency,0 as num_orders,0 as sales_value,0 as cogs_value union all
        select 2,'SO','Sales',#{emp_comp_id},2,0,0,0 union all
        select 3,'CO','Credit',#{emp_comp_id},1,0,0,0 union all
        select 4,'CO','Credit',#{emp_comp_id},2,0,0,0 union all
        select 5,'CM','Credit-Add',#{emp_comp_id},1,0,0,0 union all
        select 6,'CM','Credit-Add',#{emp_comp_id},2,0,0,0 union all
        select 7,'BD','Unpaid',#{emp_comp_id},1,0,0,0 union all
        select 8,'BD','Unpaid',#{emp_comp_id},2,0,0,0
        union all
        select 0,resource_type,case when resource_type = 'SO' then 'Sales'
                                    when resource_type = 'CO' then 'Credit' else 'Credit-Add' end as type,
              emp_comp_id,company_id,count(case when resource_type in ('CO','CM') then resource_id else order_id end),sum(sales_value) as sales_value,sum(case when sales_value = 0 then 0 else cogs_value end) as cogs_value
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        and employee_id = #{employee_id}
        group by resource_type,emp_comp_id,company_id
        union all
        select 0,'BD','Unpaid' as type,
              emp_comp_id,company_id,count(order_id),sum((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
              sum(case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and employee_id = #{employee_id}
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'
        group by emp_comp_id,company_id

    ), xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
      and month = #{end_date.month.to_i}

    )
    insert into sales_commission_net_bases (commission_date,sales_commission_id,employee_id,emp_comp_id,sorted,sales_code,sales_type,company_id,num_orders,sales,cogs,profit,rate,total,currency,created_at,updated_at)
    select '#{end_date}'::date as commission_date,#{sc_id} as sales_commission_id,#{employee_id} as employee_id,emp_comp_id,max(sorted) as sorted,code as sales_code,type as sales_type,
    company_id,sum(num_orders) as num_orders,sum(sales_value) as sales,sum(coalesce(cogs_value,0)) as cogs,(sum(sales_value) - sum(coalesce(cogs_value,0))) as profit,
    case when emp_comp_id = 2 and company_id = 1 then usd_to_cad
        when emp_comp_id = 2 and company_id = 2 then 1
        when emp_comp_id = 1 and company_id = 1 then 1
        when emp_comp_id = 1 and company_id = 2 then cad_to_usd else 0 end rate,
    case when emp_comp_id = 2 and company_id = 1 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * usd_to_cad
        when emp_comp_id = 2 and company_id = 2 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * 1
        when emp_comp_id = 1 and company_id = 1 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * 1
        when emp_comp_id = 1 and company_id = 2 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * cad_to_usd else 0 end as total,
    case when emp_comp_id = 1 then 'USD' else 'CAD' end as currency,timezone('America/Chicago', now()) as created_at,timezone('America/Chicago', now()) as updated_at
    from data,xrate
    group by code,type,company_id,emp_comp_id,usd_to_cad,cad_to_usd
    order by sorted;
  SQL

  ActiveRecord::Base.connection.execute(sql)
end

.sales_rep_net_base_detail(start_date, end_date, employee_id, sc_id) ⇒ Object



883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'app/models/sales_commission.rb', line 883

def self.sales_rep_net_base_detail(start_date, end_date, employee_id, sc_id)
  prior_start_date = end_date.months_ago(6).beginning_of_month
  prior_end_date = end_date.months_ago(6).end_of_month

  sql = <<-SQL
    with data(gl_date,customer_id,emp_comp_id,code,order_id,company_id,sales_value,cogs_value) as (
        select gl_date,customer_id,emp_comp_id,resource_type as code,
        case when resource_type in ('CO','CM') then resource_id else order_id end as order_id,
        company_id,sales_value,case when sales_value = 0 then 0 else cogs_value end as cogs_value
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        and employee_id = #{employee_id}
        union all
        select gl_date,customer_id,emp_comp_id,'BD',order_id,company_id,((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
        (case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and employee_id = #{employee_id}
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'

    ), xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
        and month = #{end_date.month.to_i}

    ), data_rate(gl_date,customer_id,emp_comp_id,code,order_id,company_id,sales_value,cogs_value,profit,rate,total) as (
        select gl_date,customer_id,emp_comp_id,code,order_id,company_id,sales_value,cogs_value,(sales_value - cogs_value) as profit,
            case when emp_comp_id = 2 and company_id = 1 then usd_to_cad
                  when emp_comp_id = 2 and company_id = 2 then 1
                  when emp_comp_id = 1 and company_id = 1 then 1
                  when emp_comp_id = 1 and company_id = 2 then cad_to_usd else 0 end rate,
            case when emp_comp_id = 2 and company_id = 1 then (sales_value - cogs_value) * usd_to_cad
                  when emp_comp_id = 2 and company_id = 2 then (sales_value - cogs_value) * 1
                  when emp_comp_id = 1 and company_id = 1 then (sales_value - cogs_value) * 1
                  when emp_comp_id = 1 and company_id = 2 then (sales_value - cogs_value) * cad_to_usd else 0 end as total
        from data,xrate
    )
    insert into sales_commission_net_base_details (commission_date,sales_commission_id,employee_id,emp_comp_id,rank,gl_date,customer_id,customer_name,sales_code,reference_number,reference_id,company_id,sales,cogs,profit,rate,total,currency,created_at,updated_at,removed)
    select '#{end_date}'::date as commission_date,#{sc_id} as sales_commission_id,#{employee_id} as employee_id,emp_comp_id,(row_number() over (partition by code order by profit desc)) as rank,dr.gl_date,dr.customer_id,full_name as customer_name,code as sales_code,
    case when code in ('CO','CM') then cm.reference_number else o.reference_number end as reference_number,
    order_id as reference_id,dr.company_id,sales_value as sales,coalesce(cogs_value,0) as cogs,profit,rate,dr.total,
    case when emp_comp_id = 1 then 'USD' else 'CAD' end as currency,timezone('America/Chicago', now()) as created_at,timezone('America/Chicago', now()) as updated_at, false as removed
    from data_rate dr
    left join orders o on dr.order_id = o.id
    left join credit_memos cm on dr.order_id = cm.id
    left join parties p on dr.customer_id = p.id;
  SQL

  ActiveRecord::Base.connection.execute(sql)
end

.sortedActiveRecord::Relation<SalesCommission>

A relation of SalesCommissions that are sorted. Active Record Scope

Returns:

See Also:



61
# File 'app/models/sales_commission.rb', line 61

scope :sorted, -> { order('commission_date desc, employee_id') }

.update_sales_rep_data(date, employee_id, sc_id) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'app/models/sales_commission.rb', line 325

def self.update_sales_rep_data(date, employee_id, sc_id)
  start_date = date.beginning_of_month
  end_date = date
  prior_start_date = end_date.months_ago(6).beginning_of_month
  prior_end_date = end_date.months_ago(6).end_of_month

  sql = <<-SQL
    with xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
        and month = #{end_date.month.to_i}

    ), data (gl_date,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,cogs_value,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,case when sales_value = 0 then 0 else cogs_value end as cogs_value,
        tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        --and resource_type = 'SO'
        and employee_id = #{employee_id}
        and order_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code = 'SO')
        union all
        select gl_date,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,
        ((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
        (case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value,
            null,null,null,null,null,null,null,null,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and employee_id = #{employee_id}
        and order_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code = 'SO')
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'

    ), bd_data as (
        select distinct employee_id,reference_number,order_id,net_base,total,case when net_base = 0 then 0 else (total / net_base) end as prev_commission_rate
        from sales_commission_details
        where order_id in (select order_id
                          from view_sales_net_bases
                          where gl_date between '#{start_date}' and '#{end_date}'
                          and employee_id = #{employee_id}
                          and resource_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code in ('CO','CM'))
                          and order_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code = 'SO')
                          and resource_type not in ('SO')
                          union all
                          select order_id
                          from view_sales_net_bases
                          where state = 'unpaid'
                          and resource_type = 'SO'
                          and employee_id = #{employee_id}
                          and order_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code = 'BD')
                          and due_date between '#{prior_start_date}' and '#{prior_end_date}')

    ), data_xrate (gl_date,employee_id,emp_comp_id,channel,order_id,sales,cogs,profit,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,employee_id,emp_comp_id,channel,order_id,
        case when company_id = 1 then sales_value * 1
          when company_id = 2 then sales_value * cad_to_usd else 0 end as sales,
        case when company_id = 1 then cogs_value * 1
          when company_id = 2 then cogs_value * cad_to_usd else 0 end as cogs,
        case when company_id = 1 then (sales_value - cogs_value) * 1
          when company_id = 2 then (sales_value - cogs_value) * cad_to_usd else 0 end as profit,
        case when company_id = 1 and tier1 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier1 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier1,
        case when company_id = 1 and tier2 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier2 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier2,
        case when company_id = 1 and tier3 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier3 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier3,
        case when company_id = 1 and tier4 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier4 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier4,
        case when company_id = 1 and tier5 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier5 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier5,
        case when company_id = 1 and tier6 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier6 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier6,
        case when company_id = 1 and tier7 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier7 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier7,
        case when company_id = 1 and tier8 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier8 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier8,
        techs_present,local_sales_rep,sales_support_rep
        from data,xrate

    ), com_rate (employee_id,comm_tier1,comm_tier2,comm_tier3,comm_tier4,comm_tier5,comm_tier6,comm_tier7,comm_tier8) as (
        select employee_id,
        sum(case when commission_tier_id = 1 then commission_rate else 0 end) as comm_tier1,
        sum(case when commission_tier_id = 2 then commission_rate else 0 end) as comm_tier2,
        sum(case when commission_tier_id = 3 then commission_rate else 0 end) as comm_tier3,
        sum(case when commission_tier_id = 4 then commission_rate else 0 end) as comm_tier4,
        sum(case when commission_tier_id = 5 then commission_rate else 0 end) as comm_tier5,
        sum(case when commission_tier_id = 6 then commission_rate else 0 end) as comm_tier6,
        sum(case when commission_tier_id = 7 then commission_rate else 0 end) as comm_tier7,
        sum(case when commission_tier_id = 8 then commission_rate else 0 end) as comm_tier8
        from commission_rates
        where is_active = true
        group by employee_id

    ), data_total (employee_id,channel,profit_t) as (
        select employee_id,channel,sum(profit) as profit_t
        from data_xrate
        group by employee_id,channel

    ), unique_channel as (
        select distinct employee_id,channel,emp_comp_id
        from data_xrate
        where employee_id = #{employee_id}

    ), data_order (gl_date,employee_rank,channel_num,channel2,employee_id,emp_comp_id,channel,reference_number,order_id,sales,cogs,profit,co_cm_bd,net_base,tier1_amount,comm_tier1,tier1,tier2_amount,comm_tier2,tier2,tier3_amount,comm_tier3,tier3,tier4_amount,comm_tier4,tier4,tier5_amount,comm_tier5,tier5,tier6_amount,comm_tier6,tier6,tier7_amount,comm_tier7,tier7,tier8_amount,comm_tier8,tier8,total,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,(DENSE_RANK() OVER (order by uc.employee_id)) as employee_rank,
        (DENSE_RANK() OVER (order by uc.channel)) as channel_num,
        ((DENSE_RANK() OVER (order by uc.employee_id))::varchar || (DENSE_RANK() OVER (order by dx.channel))::varchar || '-' || dx.channel) as channel2,
        uc.employee_id,uc.emp_comp_id,uc.channel,o.reference_number,dx.order_id,dx.sales,dx.cogs,dx.profit,
        coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0) as co_cm_bd,dx.profit as net_base,
        (((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) as tier1_amount,comm_tier1,((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (comm_tier1 / 100)) as tier1,
        (((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier2) as tier2_amount,comm_tier2,((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier2) * (comm_tier2 / 100)) as tier2,
        (((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) as tier3_amount,comm_tier3,((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (comm_tier3 / 100)) as tier3,
        (((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier4) as tier4_amount,comm_tier4,((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier4) * (comm_tier4 / 100)) as tier4,
        (((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier5) as tier5_amount,comm_tier5,((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier5) * (comm_tier5 / 100)) as tier5,
        (((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) as tier6_amount,case when techs_present = 2 then comm_tier6 / techs_present else comm_tier6 end as comm_tier6,
        case when techs_present = 0 then 0 else (((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (comm_tier6 / 100)) / techs_present) end as tier6,
        (((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) as tier7_amount,comm_tier7,((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (comm_tier7 / 100)) as tier7,
        (((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) as tier8_amount,comm_tier8,((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (comm_tier8 / 100)) as tier8,
        (((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (comm_tier1 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier2) * (comm_tier2 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (comm_tier3 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier4) * (comm_tier4 / 100)) +
        ((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier5) * (comm_tier5 / 100)) +
        case when techs_present = 0 then 0 else (((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (comm_tier6 / 100)) / techs_present) end +
        ((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (comm_tier7 / 100)) +
        coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0) +
        ((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (comm_tier8 / 100)) ) as total,
        techs_present,local_sales_rep,sales_support_rep
        from unique_channel uc
        left join data_xrate dx on uc.employee_id = dx.employee_id and uc.channel = dx.channel
        left join com_rate cr on dx.employee_id = cr.employee_id
        left join bd_data bd on uc.employee_id = bd.employee_id and dx.order_id = bd.order_id
        left join data_total da on uc.employee_id = da.employee_id and uc.channel = da.channel
        left join orders o on dx.order_id = o.id
        order by dx.employee_id,dx.channel,dx.profit desc

    )
    select '#{end_date}'::date as commission_date,employee_rank,employee_id,emp_comp_id,count(distinct order_id) as num_orders,sum(coalesce(sales,0)) as sales,
    sum(coalesce(cogs,0)) as cogs,sum(coalesce(profit,0)) as profit,sum(coalesce(co_cm_bd,0)) as co_cm_bd,sum(coalesce(net_base,0)) as net_base,
    sum(coalesce(tier1,0)) as tier1,sum(0) /*sum(coalesce(tier2,0))*/ as tier2,sum(coalesce(tier3,0)) as tier3,sum(0) /*sum(coalesce(tier4,0))*/ as tier4,
    sum(coalesce(tier5,0)) as tier5,sum(coalesce(tier6,0)) as tier6,sum(coalesce(tier7,0)) as tier7,
    sum(coalesce(tier8,0)) as tier8,sum(coalesce(total,0)) as total,'draft' as state,timezone('America/Chicago', now()) as updated_at
    from data_order dr
    group by employee_rank,employee_id,emp_comp_id;
  SQL

  result = ActiveRecord::Base.connection.execute(sql).to_a.map { |r| r.symbolize_keys }
end

.update_sales_rep_data_detail(date, employee_id, sc_id, updater_id) ⇒ Object



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'app/models/sales_commission.rb', line 614

def self.update_sales_rep_data_detail(date, employee_id, sc_id, updater_id)
  start_date = date.beginning_of_month
  end_date = date
  prior_start_date = end_date.months_ago(6).beginning_of_month
  prior_end_date = end_date.months_ago(6).end_of_month

  sql = <<-SQL
    with xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
        and month = #{end_date.month.to_i}

    ), data (gl_date,customer_id,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,cogs_value,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,customer_id,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,sales_value,case when sales_value = 0 then 0 else coalesce(cogs_value,0) end as cogs_value,
            tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        and employee_id = #{employee_id}
        --and resource_type = 'SO'
        union all
        select gl_date,customer_id,employee_id,channel,order_id,emp_comp_id,company_id,customer_id,
        ((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
        (case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value,
            null,null,null,null,null,null,null,null,techs_present,local_sales_rep,sales_support_rep
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and employee_id = #{employee_id}
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'

    ), data_xrate (gl_date,employee_id,emp_comp_id,channel,order_id,sales,cogs,profit,tier1,tier2,tier3,tier4,tier5,tier6,tier7,tier8,techs_present,local_sales_rep,sales_support_rep) as (
        select gl_date,employee_id,emp_comp_id,channel,order_id,
        case when company_id = 1 then sales_value * 1
          when company_id = 2 then sales_value * cad_to_usd else 0 end as sales,
        case when company_id = 1 then cogs_value * 1
          when company_id = 2 then cogs_value * cad_to_usd else 0 end as cogs,
        case when company_id = 1 then (sales_value - cogs_value) * 1
          when company_id = 2 then (sales_value - cogs_value) * cad_to_usd else 0 end as profit,
        case when company_id = 1 and tier1 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier1 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier1,
        case when company_id = 1 and tier2 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier2 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier2,
        case when company_id = 1 and tier3 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier3 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier3,
        case when company_id = 1 and tier4 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier4 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier4,
        case when company_id = 1 and tier5 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier5 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier5,
        case when company_id = 1 and tier6 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier6 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier6,
        case when company_id = 1 and tier7 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier7 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier7,
        case when company_id = 1 and tier8 = true then (sales_value - cogs_value) * 1
          when company_id = 2 and tier8 = true then (sales_value - cogs_value) * cad_to_usd else 0 end as tier8,
        techs_present,local_sales_rep,sales_support_rep
        from data,xrate

    ), bd_data as (
        select employee_id,reference_number,order_id,net_base,total,case when net_base = 0 then 0 else (total / net_base) end as prev_commission_rate
      from sales_commission_details
      where order_id in (select order_id
              from view_sales_net_bases
              where gl_date between '#{start_date}' and '#{end_date}'
              and employee_id = #{employee_id}
              and resource_type not in ('SO')
              union all
              select order_id
              from view_sales_net_bases
              where state = 'unpaid'
              and resource_type = 'SO'
              and employee_id = #{employee_id}
              and due_date between '#{prior_start_date}' and '#{prior_end_date}')

    ), com_rate (employee_id,tier1_comm,tier2_comm,tier3_comm,tier4_comm,tier5_comm,tier6_comm,tier7_comm,tier8_comm) as (
        select employee_id,
        sum(case when commission_tier_id = 1 then commission_rate else 0 end) as tier1_comm,
        sum(case when commission_tier_id = 2 then commission_rate else 0 end) as tier2_comm,
        sum(case when commission_tier_id = 3 then commission_rate else 0 end) as tier3_comm,
        sum(case when commission_tier_id = 4 then commission_rate else 0 end) as tier4_comm,
        sum(case when commission_tier_id = 5 then commission_rate else 0 end) as tier5_comm,
        sum(case when commission_tier_id = 6 then commission_rate else 0 end) as tier6_comm,
        sum(case when commission_tier_id = 7 then commission_rate else 0 end) as tier7_comm,
        sum(case when commission_tier_id = 8 then commission_rate else 0 end) as tier7_comm
        from commission_rates
        where employee_id = #{employee_id}
        and is_active = true
        group by employee_id

    ), unique_channel as (
        select distinct employee_id,channel,emp_comp_id
        from data_xrate
        where employee_id = #{employee_id}

    ), data_total (employee_id,channel,profit_t) as (
        select employee_id,channel,sum(profit) as profit_t
        from data_xrate
        group by employee_id,channel

    )
    insert into sales_commission_details (commission_date,sales_commission_id,gl_date,employee_id,emp_comp_id,customer_id,customer_name,rank,channel,reference_number,order_id,sales,cogs,profit,co_cm_bd,net_base,tier1_amount,tier1_comm,tier1,tier2_amount,tier2_comm,tier2,tier3_amount,tier3_comm,tier3,tier4_amount,tier4_comm,tier4,tier5_amount,tier5_comm,tier5,tier6_amount,tier6_comm,tier6,tier7_amount,tier7_comm,tier7,tier8_amount,tier8_comm,tier8,total,techs_present,local_sales_rep,sales_support_rep,created_at,updated_at)
    select '#{end_date}'::date as commission_date,#{sc_id} as sales_commission_id,coalesce(dx.gl_date,'#{end_date}'::date) as gl_date,uc.employee_id,uc.emp_comp_id,coalesce(o.customer_id,0) as customer_id,coalesce(full_name,'') as name,(row_number() over (partition by uc.channel order by uc.channel,dx.profit desc)) as rank,uc.channel,coalesce(o.reference_number,'BD') as reference_number,coalesce(dx.order_id,0) as order_id,coalesce(dx.sales,0) as sales,coalesce(dx.cogs,0) as cogs,coalesce(dx.profit,0) as profit,
    coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0) as co_cm_bd,(dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) as net_base,
    coalesce((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1),0) as tier1_amount,coalesce(tier1_comm,0) as tier1_comm,coalesce(((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (tier1_comm / 100)),0) as tier1,
    0 as tier2_amount,0 as tier2_comm,0 as tier2,
    /*coalesce((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier2),0) as tier2_amount,coalesce(tier2_comm,0) as tier2_comm,coalesce(((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier2) * (tier2_comm / 100)),0) as tier2,*/
    coalesce((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3),0) as tier3_amount,coalesce(tier3_comm,0) as tier3_comm,coalesce(((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (tier3_comm / 100)),0) as tier3,
    0 as tier4_amount,0 as tier4_comm,0 as tier4,
    /*coalesce((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier4),0) as tier4_amount,coalesce(tier4_comm,0) as tier4_comm,coalesce(((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier4) * (tier4_comm / 100)),0) as tier4,*/
    /*0 as tier5_amount,0 as tier5_comm,0 as tier5,*/
    coalesce((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier5),0) as tier5_amount,coalesce(tier5_comm,0) as tier5_comm,coalesce(((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier5) * (tier5_comm / 100)),0) as tier5,
    coalesce((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6),0) as tier6_amount,coalesce(case when techs_present = 2 then tier6_comm / techs_present else tier6_comm end,0) as tier6_comm,case when techs_present = 0 then 0 else coalesce(((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (tier6_comm / 100)),0) / techs_present end as tier6,
    /*0 as tier7_amount,0 as tier7_comm,0 as tier7,*/
    coalesce((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7),0) as tier7_amount,coalesce(tier7_comm,0) as tier7_comm,coalesce(((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (tier7_comm / 100)),0) as tier7,
    coalesce((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8),0) as tier8_amount,coalesce(tier8_comm,0) as tier8_comm,coalesce(((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (tier8_comm / 100)),0) as tier8,
    coalesce(((((case when dx.profit = 0 then 0 else tier1/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier1) * (tier1_comm / 100)) +
    /*((((case when dx.profit = 0 then 0 else tier2/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier2) * (tier2_comm / 100)) +*/
    ((((case when dx.profit = 0 then 0 else tier3/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier3) * (tier3_comm / 100)) +
    /*((((case when dx.profit = 0 then 0 else tier4/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier4) * (tier4_comm / 100)) +*/
    ((((case when dx.profit = 0 then 0 else tier5/dx.profit end) * coalesce(((case when profit_t = 0 then 0 else dx.profit/profit_t end) * bd.total),0)) + tier5) * (tier5_comm / 100)) +
    case when techs_present = 0 then 0 else ((((case when dx.profit = 0 then 0 else tier6/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier6) * (tier6_comm / 100)) / techs_present end +
    ((((case when dx.profit = 0 then 0 else tier7/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier7) * (tier7_comm / 100)) +
    ((((case when dx.profit = 0 then 0 else tier8/dx.profit end) * coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0)) + tier8) * (tier8_comm / 100)) +
    coalesce((prev_commission_rate * (dx.profit + coalesce((case when profit_t = 0 then 0 else dx.profit/profit_t end),0))),0),0) as total,
    techs_present,local_sales_rep,sales_support_rep,timezone('America/Chicago', now()) as created_at,timezone('America/Chicago', now()) as updated_at
    from unique_channel uc
    left join data_xrate dx on uc.employee_id = dx.employee_id and uc.channel = dx.channel
    left join com_rate cr on dx.employee_id = cr.employee_id
    left join bd_data bd on uc.employee_id = bd.employee_id and dx.order_id = bd.order_id
    left join data_total da on uc.employee_id = da.employee_id and uc.channel = da.channel
    left join orders o on dx.order_id = o.id
    left join parties p on o.customer_id = p.id
    order by uc.channel,dx.profit desc;
  SQL

  ActiveRecord::Base.connection.execute(sql)
end

.update_sales_rep_net_base(date, employee_id, emp_comp_id, sc_id, updater_id) ⇒ Object



816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'app/models/sales_commission.rb', line 816

def self.update_sales_rep_net_base(date, employee_id, emp_comp_id, sc_id, updater_id)
  start_date = date.beginning_of_month
  end_date = date
  prior_start_date = date.months_ago(6).beginning_of_month
  prior_end_date = date.months_ago(6).end_of_month

  sql = <<-SQL
    with data (sorted,code,type,emp_comp_id,company_id,num_orders,sales_value,cogs_value) as (
        select 1 as sorted,'SO' as code,'Sales' as type,#{emp_comp_id} as emp_comp_id,1 as currency,0 as num_orders,0 as sales_value,0 as cogs_value union all
        select 2,'SO','Sales',#{emp_comp_id},2,0,0,0 union all
        select 3,'CO','Credit',#{emp_comp_id},1,0,0,0 union all
        select 4,'CO','Credit',#{emp_comp_id},2,0,0,0 union all
        select 5,'CM','Credit-Add',#{emp_comp_id},1,0,0,0 union all
        select 6,'CM','Credit-Add',#{emp_comp_id},2,0,0,0 union all
        select 7,'BD','Unpaid',#{emp_comp_id},1,0,0,0 union all
        select 8,'BD','Unpaid',#{emp_comp_id},2,0,0,0
        union all
        select 0,resource_type,case when resource_type = 'SO' then 'Sales'
                                    when resource_type = 'CO' then 'Credit' else 'Credit-Add' end as type,
              emp_comp_id,company_id,count(case when resource_type in ('CO','CM') then resource_id else order_id end),sum(sales_value) as sales_value,sum(case when sales_value = 0 then 0 else cogs_value end) as cogs_value
        from view_sales_net_bases
        -- modify here if we need to add missiing orders manually
        where gl_date between '#{start_date}' and '#{end_date}'
        and employee_id = #{employee_id}
        and resource_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code in ('CO','CM'))
        and order_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code = 'SO')
        group by resource_type,emp_comp_id,company_id
        union all
        select 0,'BD','Unpaid' as type,
              emp_comp_id,company_id,count(order_id),sum((case when unpaid_amount > sales_value then sales_value else unpaid_amount end) * -1) as sales_value,
              sum(case when unpaid_amount is null then (case when sales_value = 0 then 0 else cogs_value * -1 end) else (case when cogs_value > unpaid_amount then (case when sales_value = 0 then 0 else (unpaid_amount * (unpaid_amount / sales_value)) end * -1) else cogs_value * -1 end) end) as cogs_value
        from view_sales_net_bases
        where state = 'unpaid'
        and resource_type = 'SO'
        and employee_id = #{employee_id}
        and due_date between '#{prior_start_date}' and '#{prior_end_date}'
        and order_id not in (select reference_id from sales_commission_net_base_details where sales_commission_id = #{sc_id} and removed = true and sales_code = 'BD')
        group by emp_comp_id,company_id

    ), xrate (usd_to_cad,cad_to_usd) as (
        select usd_to_cad,cad_to_usd
        from xrate_averages xr
        inner join analytic_date_time_dimensions dt on xr.effective_date = dt.date
        where year = #{end_date.year.to_i}
        and month = #{end_date.month.to_i}

    )
    insert into sales_commission_net_bases (commission_date,sales_commission_id,employee_id,emp_comp_id,sorted,sales_code,sales_type,company_id,num_orders,sales,cogs,profit,rate,total,currency,created_at,updated_at,updater_id)
    select '#{end_date}'::date as commission_date,#{sc_id} as sales_commission_id,#{employee_id} as employee_id,emp_comp_id,max(sorted) as sorted,code as sales_code,type as sales_type,
    company_id,sum(num_orders) as num_orders,sum(sales_value) as sales,sum(coalesce(cogs_value,0)) as cogs,(sum(sales_value) - sum(coalesce(cogs_value,0))) as profit,
    case when emp_comp_id = 2 and company_id = 1 then usd_to_cad
        when emp_comp_id = 2 and company_id = 2 then 1
        when emp_comp_id = 1 and company_id = 1 then 1
        when emp_comp_id = 1 and company_id = 2 then cad_to_usd else 0 end rate,
    case when emp_comp_id = 2 and company_id = 1 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * usd_to_cad
        when emp_comp_id = 2 and company_id = 2 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * 1
        when emp_comp_id = 1 and company_id = 1 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * 1
        when emp_comp_id = 1 and company_id = 2 then (sum(sales_value) - sum(coalesce(cogs_value,0))) * cad_to_usd else 0 end as total,
    case when emp_comp_id = 1 then 'USD' else 'CAD' end as currency,timezone('America/Chicago', now()) as created_at,timezone('America/Chicago', now()) as updated_at, #{updater_id} as updater_id
    from data,xrate
    group by code,type,company_id,emp_comp_id,usd_to_cad,cad_to_usd
    order by sorted;
  SQL

  ActiveRecord::Base.connection.execute(sql)
end

.with_employee_nameActiveRecord::Relation<SalesCommission>

A relation of SalesCommissions that are with employee name. Active Record Scope

Returns:

See Also:



65
66
67
# File 'app/models/sales_commission.rb', line 65

scope :with_employee_name, -> {
  where("sales_commissions.employee_id in (select parties.id from parties where type = 'Employee' and inactive = false)").select('sales_commissions.*, (select full_name from parties where id = employee_id) as employee_name')
}

.with_rateActiveRecord::Relation<SalesCommission>

A relation of SalesCommissions that are with rate. Active Record Scope

Returns:

See Also:



62
63
64
# File 'app/models/sales_commission.rb', line 62

scope :with_rate, -> {
  select("coalesce((select usd_to_cad from xrate_averages where emp_comp_id = 2 and to_char(commission_date,'YYYY') = to_char(effective_date,'YYYY') and to_char(commission_date,'MM') = to_char(effective_date,'MM')),1) as rate_usd_to_cad")
}

Instance Method Details

#notify_sales_commission_to_pending_accounting_reviewObject



956
957
958
# File 'app/models/sales_commission.rb', line 956

def notify_sales_commission_to_pending_accounting_review
  InternalMailer.notify_sales_commission_to_pending_accounting_review(self).deliver_later
end

#notify_sales_commission_to_pending_pay_outObject



960
961
962
# File 'app/models/sales_commission.rb', line 960

def notify_sales_commission_to_pending_pay_out
  InternalMailer.notify_sales_commission_to_pending_pay_out(self).deliver_later
end

#notify_sales_commission_to_pending_rep_approvalObject



952
953
954
# File 'app/models/sales_commission.rb', line 952

def notify_sales_commission_to_pending_rep_approval
  InternalMailer.notify_sales_commission_to_pending_rep_approval(self).deliver_later
end

#sales_commission_detailsActiveRecord::Relation<SalesCommissionDetail>

Returns:

  • (ActiveRecord::Relation<SalesCommissionDetail>)

See Also:



59
# File 'app/models/sales_commission.rb', line 59

has_many :sales_commission_details

#versions_for_audit_trail(_params = {}) ⇒ Object



938
939
940
# File 'app/models/sales_commission.rb', line 938

def versions_for_audit_trail(_params = {})
  RecordVersion.where(item_type: 'SalesCommission', item_id: id)
end

#versions_for_state_trackerObject



942
943
944
945
946
947
948
949
950
# File 'app/models/sales_commission.rb', line 942

def versions_for_state_tracker
  where_sql = "object_changes ? 'state'"
  select_sql = %q{
    id, object_changes #>> '{"state",1}' as state, created_at,
    (lag(object_changes #>> '{"state",1}') OVER (ORDER BY id desc)) as new_state,
    (lag(created_at) OVER (ORDER BY id desc)) as new_state_created_at
  }
  versions_for_audit_trail.where(where_sql).select(select_sql).order('id, created_at')
end