module EducodeSales module SaleTrendsHelper NAMES = %w[计划投标额 计划投标额累计 实际中标额 实际中标额累计] def goal_forecast_quarter(labels, selects, staff_id = nil, property) plan_get = plan_get(staff_id, labels, "quarter", property) actual_get = actual_get(staff_id, labels, "quarter", property) datasets = selects.map.with_index do |select, i| data = if select == NAMES[0] labels.map do |d| quarter = d.split("-").last.to_i year = d.split("-").first.to_i plan_get[[year, quarter]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f end elsif select == NAMES[1] arr = labels.map do |d| quarter = d.split("-").last.to_i year = d.split("-").first.to_i plan_get[[year, quarter]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f end arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } elsif select == NAMES[2] labels.map do |d| quarter = d.split("-").last.to_i year = d.split("-").first.to_i actual_get[[year, quarter]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f end elsif select == NAMES[3] arr = labels.map do |d| quarter = d.split("-").last.to_i year = d.split("-").first.to_i actual_get[[year, quarter]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f end arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } end type = if select == NAMES[3] || select == NAMES[1] 'line' else "bar" end backgroundColor = if select == NAMES[3] || select == NAMES[1] 'rgba(54, 162, 235, 0.2)' else SaleTrend::COLORS[i] end { type: type, label: select, data: data, backgroundColor: backgroundColor, borderColor: SaleTrend::COLORS[i], borderWidth: 1 } end hash_a = { labels: labels, datasets: [datasets[0], datasets[2]] } hash_b = { labels: labels, datasets: [datasets[1], datasets[3]] } [hash_a, hash_b] end def goal_forecast_week(labels, selects, staff_id = nil, property) plan_get = plan_get(staff_id, labels, "week", property) actual_get = actual_get(staff_id, labels, "week", property) datasets = selects.map.with_index do |select, i| data = if select == NAMES[0] labels.map do |d| week = d.split("-").last year = d.split("-").first yearweek = "#{year}#{week}".to_i plan_get[yearweek].to_f end elsif select == NAMES[1] arr = labels.map do |d| week = d.split("-").last year = d.split("-").first yearweek = "#{year}#{week}".to_i plan_get[yearweek].to_f end arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } elsif select == NAMES[2] labels.map do |d| week = d.split("-").last year = d.split("-").first yearweek = "#{year}#{week}".to_i actual_get[yearweek].to_f end elsif select == NAMES[3] arr = labels.map do |d| week = d.split("-").last year = d.split("-").first yearweek = "#{year}#{week}".to_i actual_get[yearweek].to_f end arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } end type = if select == NAMES[0] || select == NAMES[2] 'bar' else "line" end backgroundColor = if select == NAMES[0] || select == NAMES[2] SaleTrend::COLORS[i] else 'rgba(54, 162, 235, 0.2)' end { type: type, label: select, data: data, backgroundColor: backgroundColor, borderColor: SaleTrend::COLORS[i], borderWidth: 1 } end hash_a = { labels: labels, datasets: [datasets[0], datasets[2]] } hash_b = { labels: labels, datasets: [datasets[1], datasets[3]] } [hash_a, hash_b] end def goal_forecast_month(labels, selects, staff_id = nil, property) plan_get = plan_get(staff_id, labels, "month", property) actual_get = actual_get(staff_id, labels, "month", property) datasets = selects.map.with_index do |select, i| data = if select == NAMES[0] labels.map do |d| month = d.split("-").last.to_i year = d.split("-").first.to_i plan_get[[year, month]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f end elsif select == NAMES[1] arr = labels.map do |d| month = d.split("-").last.to_i year = d.split("-").first.to_i plan_get[[year, month]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f end arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } elsif select == NAMES[2] labels.map do |d| month = d.split("-").last.to_i year = d.split("-").first.to_i actual_get[[year, month]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f end elsif select == NAMES[3] arr = labels.map do |d| month = d.split("-").last.to_i year = d.split("-").first.to_i actual_get[[year, month]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f end arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } end type = if select == NAMES[3] || select == NAMES[1] 'line' else "bar" end backgroundColor = if select == NAMES[3] || select == NAMES[1] 'rgba(54, 162, 235, 0.2)' else SaleTrend::COLORS[i] end { type: type, label: select, data: data, backgroundColor: backgroundColor, borderColor: SaleTrend::COLORS[i], borderWidth: 1 } end hash_a = { labels: labels, datasets: [datasets[0], datasets[2]] } hash_b = { labels: labels, datasets: [datasets[1], datasets[3]] } [hash_a, hash_b] end def goal_forecast_year(labels, selects, staff_id = nil, property) plan_get = plan_get(staff_id, labels, "year", property) actual_get = actual_get(staff_id, labels, "year", property) datasets = selects.map.with_index do |select, i| data = if select == NAMES[0] labels.map { |d| plan_get[d.to_i] || 0 } elsif select == NAMES[1] arr = labels.map { |d| plan_get[d.to_i] || 0 } arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } elsif select == NAMES[2] labels.map { |d| actual_get[d.to_i] || 0 } elsif select == NAMES[3] arr = labels.map { |d| actual_get[d.to_i] || 0 } arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) } end type = if select == NAMES[3] || select == NAMES[1] 'line' else "bar" end backgroundColor = if select == NAMES[3] || select == NAMES[1] 'rgba(54, 162, 235, 0.2)' else SaleTrend::COLORS[i] end { type: type, label: select, data: data, backgroundColor: backgroundColor, borderColor: SaleTrend::COLORS[i], borderWidth: 1 } end hash_a = { labels: labels, datasets: [datasets[0], datasets[2]] } hash_b = { labels: labels, datasets: [datasets[1], datasets[3]] } [hash_a, hash_b] end private def plan_get(staff_id, time_range, type, property) # budget_amount 预算额 start_time = case type when "month" time_range.first + "-01" when "year" time_range.first + "-01-01" when "week" time_range.first.split("-").first + "-01-01" when "quarter" time_range.first.split("-").first + "-01-01" end end_time = case type when "month" time_range.last + "-31" when "year" time_range.last + "-12-31" when "week" time_range.last.split("-").first + "-12-31" when "quarter" time_range.last.split("-").first + "-12-31" end staff_id = staff_id.present? ? Array(staff_id) : nil data = Business.joins(:last_follow_up) .where("educode_sales_follow_ups.invitation_at >= ? and educode_sales_follow_ups.invitation_at <= ? #{staff_id.present? ? "AND educode_sales_follow_ups.staff_id in (#{staff_id.join(",")})" : ""}", start_time, end_time) if property.present? data = data.joins(department: [school: :school_tags]).where("school_tags.id = ?", property) end case type when "week" data.select("educode_sales_follow_ups.*, yearweek(educode_sales_follow_ups.invitation_at) as week").group("yearweek(educode_sales_follow_ups.invitation_at)").sum(:budget_amount) when "quarter" data = data.select("educode_sales_follow_ups.*, quarter(educode_sales_follow_ups.invitation_at) as quarter, year(educode_sales_follow_ups.invitation_at) as year") data.group_by { |d| [d.year, d.quarter] } when "month" data = data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.invitation_at) as year, month(educode_sales_follow_ups.invitation_at) as month") data.group_by { |d| [d.year, d.month] } when "year" data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.invitation_at) as year").group("year(educode_sales_follow_ups.invitation_at)").sum(:budget_amount) end end def actual_get(staff_id, time_range, type, property) start_time = case type when "month" time_range.first + "-01" when "year" time_range.first + "-01-01" when "week" time_range.first.split("-").first + "-01-01" when "quarter" time_range.first.split("-").first + "-01-01" end end_time = case type when "month" time_range.last + "-31" when "year" time_range.last + "-12-31" when "week" time_range.last.split("-").first + "-12-31" when "quarter" time_range.last.split("-").first + "-12-31" end # actual_amount 合同额 staff_id = staff_id.present? ? Array(staff_id) : nil data = Business.joins(:last_follow_up) .where("educode_sales_follow_ups.bidded_date >= ? and educode_sales_follow_ups.bidded_date <= ? #{staff_id.present? ? "AND educode_sales_follow_ups.staff_id in (#{staff_id.join(",")})" : ""}", start_time, end_time) if property.present? data = data.joins(department: [school: :school_tags]).where("school_tags.id = ?", property) end case type when "week" data.select("educode_sales_follow_ups.*, yearweek(educode_sales_follow_ups.bidded_date) as week").group("yearweek(educode_sales_follow_ups.bidded_date)").sum(:actual_amount) when "quarter" data = data.select("educode_sales_follow_ups.*, quarter(educode_sales_follow_ups.bidded_date) as quarter, year(educode_sales_follow_ups.bidded_date) as year") data.group_by { |d| [d.year, d.quarter] } when "month" data = data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.bidded_date) as year, month(educode_sales_follow_ups.bidded_date) as month") data.group_by { |d| [d.year, d.month] } when "year" data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.bidded_date) as year").group("year(educode_sales_follow_ups.bidded_date)").sum(:actual_amount) end end end end