require_dependency "educode_sales/application_controller" module EducodeSales class ProjectChartsController < ApplicationController # authorize_resource class: false def trends end # 销售额分析 def sales_analysis respond_to do |format| format.html do end format.js do x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id count_type = params[:count_type] || "actual_amount" stage_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id) s_stage_ids = Common.where(clazz: '商机阶段', name: ['已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id) @goal_count_range = params[:goal_count_range] || "month" goal_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%m") }.uniq sale_names = ['已中标', '已签单', '已回款'] @goal_count_data = month_sale_chart(goal_default_dates, sale_names, SaleTrend::COLORS, x, stage_ids, s_stage_ids, count_type, ["#{Time.now.year}-01", "#{Time.now.year}-#{Time.now.month}"]) end end end private def month_sale_chart(dates, names, colors, x, stage_ids, s_stage_ids, count_type, range) begin_at = range[0] end_at = range[1] data_1 = EducodeSales::Business.joins(:last_follow_up). where("educode_sales_follow_ups.clazz_id != ?", x). where("educode_sales_follow_ups.stage_id IN (?)", stage_ids). where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", begin_at, end_at). select("SUM(#{count_type}) AS count_type, DATE_FORMAT(bidded_date, '%Y-%m') AS month"). group("DATE_FORMAT(bidded_date, '%Y-%m')"). order("DATE_FORMAT(bidded_date, '%Y-%m')") data_1_hash = {} data_1_total_hash = {} data_1.each do |d| data_1_hash[d.month] = d.count_type.round(2) end last_month = 0 last_sum = 0 dates.each do |month| # 只累计当前年份 if last_month != month.split("-")[0] last_sum = 0 end data_1_total_hash[month] = ((data_1_hash[month] || 0) + last_sum).round(2) last_sum += data_1_hash[month] || 0 last_month = month.split("-")[0] end data_2 = EducodeSales::Business.joins(:last_follow_up). where("educode_sales_follow_ups.clazz_id != ?", x). where("educode_sales_follow_ups.stage_id IN (?)", s_stage_ids). where("educode_sales_follow_ups.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", begin_at, end_at). select("SUM(#{count_type}) AS count_type, DATE_FORMAT(signed_date, '%Y-%m') AS month"). group("DATE_FORMAT(signed_date, '%Y-%m')"). order("DATE_FORMAT(signed_date, '%Y-%m')") data_2_hash = {} data_2_total_hash = {} data_2.each do |d| data_2_hash[d.month] = d.count_type.round(2) end last_month = 0 last_sum = 0 dates.each do |month| # 只累计当前年份 if last_month != month.split("-")[0] last_sum = 0 end data_2_total_hash[month] = ((data_2_hash[month] || 0) + last_sum).round(2) last_sum += data_2_hash[month] || 0 last_month = month.split("-")[0] end data_3 = EducodeSales::Business.joins(last_follow_up: :money_plans). where("educode_sales_follow_ups.clazz_id != ?", x). where.not("educode_sales_money_plans.clazz!= ?", 1). where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ?", begin_at, end_at). select("SUM(amount) AS count_type, DATE_FORMAT(date_at, '%Y-%m') AS month"). group("DATE_FORMAT(date_at, '%Y-%m')"). order("DATE_FORMAT(date_at, '%Y-%m')") data_3_hash = {} data_3.each do |d| data_3_hash[d.month] = d.count_type.round(2) end data_3_total_hash = {} last_month = 0 last_sum = 0 dates.each do |month| # 只累计当前年份 if last_month != month.split("-")[0] last_sum = 0 end data_3_total_hash[month] = ((data_3_hash[month] || 0) + last_sum).round(2) last_sum += data_3_hash[month] || 0 last_month = month.split("-")[0] end { labels: dates, datasets: names.map.with_index do |name, i| { label: name, data: dates.map { |d| case i when 0 data_1_hash[d] || 0 when 1 data_2_hash[d] || 0 else data_3_hash[d] || 0 end }, data1: dates.map { |d| case i when 0 data_1_total_hash[d] || 0 when 1 data_2_total_hash[d] || 0 else data_3_total_hash[d] || 0 end }, backgroundColor: colors[i], borderColor: colors[i], borderWidth: 1 } end } end end end