require_dependency "educode_sales/application_controller" module EducodeSales class SalesDetailsController < ApplicationController def index respond_to do |format| format.js do common = Common.find_by(clazz: 'staff_type', name: '销售') staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11) @staffs = staffs.map { |d| [d.user.real_name, d.id]} gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : [] gon.staffs = staffs.map { |d| {name: d.user.real_name, value: d.id } } bussiness_type = [] if params[:type] # 项目类型 bussiness_type = EducodeSales::Business.include_types(params[:type]) || [] end business_step = [] if params[:step] # 项目阶段 business_step = EducodeSales::Business.include_steps(params[:step]) || [] end gon.business_type = Common.where(clazz: 'business_type').map do |d| {value: d.id, name: d.name, selected: bussiness_type.include?(d.extras)} end gon.business_step = Common.where(clazz: 'business_step', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).order("position").map do |d| {value: d.id, name: d.name, selected: business_step.include?(d.name)} end end format.json do @sales_details = SalesDetail @sales_details = @sales_details.joins(:business).includes(:business, :product_catalog) if params[:q] && params[:q][:signed_date].present? date = params[:q][:signed_date].split(" - ") @sales_details = @sales_details.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1]) end if params[:q] && params[:q][:bidded_date].present? date = params[:q][:bidded_date].split(" - ") @sales_details = @sales_details.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1]) end if params[:q].present? && params[:q][:name].present? @sales_details = @sales_details.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%") end if params[:sort].present? && params[:sort][:field] @sales_details = @sales_details.order("#{params[:sort][:field]} #{params[:sort][:order]}") else @sales_details = @sales_details.order("educode_sales_sales_details.created_at desc") end if params[:q].present? && params[:q][:business_step].present? @sales_details = @sales_details.joins(" JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id ").where("educode_sales_follow_ups.stage_id in (?)", params[:q][:business_step].split(",").map(&:to_i)) end if params[:q].present? && params[:q][:sales_staff_id].present? # 销售经理 @sales_details = @sales_details.joins("LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = educode_sales_businesses.last_follow_up_id"). where("(educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = ?) OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = ?)", params[:q][:sales_staff_id], params[:q][:sales_staff_id]) end if params[:q].present? && params[:q][:department].present? departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id) @sales_details = @sales_details.joins(business: :department).where("departments.id in (?)", departments_ids) end if params[:q].present? && params[:q][:area].present? area = EducodeSales::Common.find(params[:q][:area]).name if @current_admin.is_admin? @sales_details = @sales_details.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", area) else level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level if level == "区域" @sales_details = @sales_details.joins(" JOIN schools ON departments.school_id = schools.id ").where("province = ?", area) else @sales_details = @sales_details.joins(" JOIN departments ON educode_sales_businesses.department_id = departments.id JOIN schools ON departments.school_id = schools.id ").where("province = ?", area) end end end @sales_details = @sales_details.page(params[:page]).per(params[:limit]) end end end def destroy sales_detail = SalesDetail.find(params[:id]) if sales_detail.destroy render_success else render_failure sales_detail end end def edit @sale_detail = SalesDetail.find(params[:id]) @name = [@sale_detail.business.name, @sale_detail.product_catalog.item_clazz, @sale_detail.product_catalog.brand, @sale_detail.product_catalog.specification, @sale_detail.product_catalog.unit, @sale_detail.product_catalog.source_method, @sale_detail.product_catalog.param, @sale_detail.product_catalog.supplier].join(" ") render layout: false end def update sale_detail = SalesDetail.find(params[:id]) if sale_detail.update(sales_detail_params) render_success else render_failure sale_detail end end def upload xlsx = Roo::Spreadsheet.open(params[:file]) ods = xlsx.sheet(0) rows = ods.last_row - 1 rows.times do |r| #行数 next unless ods.row(r+2)[0] business = EducodeSales::Business.find_by(number: ods.row(r+2)[0].to_s.strip) product_catalog = EducodeSales::ProductCatalog.find_by(item_clazz: ods.row(r+2)[7].to_s.strip, brand: ods.row(r+2)[8].to_s.strip, specification: ods.row(r+2)[9].to_s.strip, unit: ods.row(r+2)[10].to_s.strip, source_method: ods.row(r+2)[14].to_s.strip, supplier: ods.row(r+2)[16].to_s.strip) if business.present? && product_catalog.present? SalesDetail.create(staff_id: @current_admin.id, business_id: business.id, product_catalog_id: product_catalog.id, price: ods.row(r+2)[12].to_s.strip, amount: ods.row(r+2)[11].to_s.strip, total_price: ods.row(r+2)[13].to_s.strip, custom_clazz: ods.row(r+2)[15].to_s.strip, delivery_date: ods.row(r+2)[17].to_s.strip) end end render json: { succcess: true} end private def sales_detail_params params.permit(:amount, :price, :total_price, :custom_clazz, :delivery_date, :product_catalog_id) end end end