146 lines
5.4 KiB
Ruby
146 lines
5.4 KiB
Ruby
class OrganizationsController < ApplicationController
|
|
before_action :require_current_project!
|
|
before_action :set_organization, only: %i[show edit update destroy]
|
|
before_action :load_form_collections, only: %i[new create edit update]
|
|
|
|
def index
|
|
@page_title = "Organizzazioni"
|
|
@sport_options = organizations_for_current_project.where.not(sport: [ nil, "" ]).distinct.order(:sport).pluck(:sport)
|
|
scope = organizations_for_current_project.includes(:assigned_user, :contacts, :opportunities, :tasks, :projects)
|
|
scope = scope.search(params[:q]) if params[:q].present?
|
|
scope = apply_filters(scope)
|
|
scope = apply_sort(scope)
|
|
@pagy, @organizations = pagy(scope, items: 50)
|
|
respond_to do |format|
|
|
format.html
|
|
format.csv { send_data CsvExport.organizations(scope), filename: "organizations-#{Date.current}.csv" }
|
|
end
|
|
end
|
|
|
|
def show
|
|
authorize_organization!(@organization)
|
|
@page_title = @organization.name
|
|
@contacts = @organization.contacts.primary_first
|
|
@opportunities = @organization.opportunities.for_project(current_project).includes(:assigned_user, :project).order(updated_at: :desc)
|
|
@pending_tasks = @organization.tasks.pending.ordered
|
|
@completed_tasks = @organization.tasks.completed.order(completed_at: :desc).limit(10)
|
|
@activities = @organization.activities.includes(:user, :contact, :opportunity).recent_first
|
|
@next_task = @organization.next_pending_task
|
|
@activity = @organization.activities.build(happened_at: Time.current, user: current_user)
|
|
@users = User.active.order(:first_name)
|
|
end
|
|
|
|
def new
|
|
@page_title = "Nuova organizzazione"
|
|
@organization = Organization.new(assigned_user: current_user, country: "Italia", status: "prospect")
|
|
@organization.project_ids = [current_project.id] if current_project
|
|
end
|
|
|
|
def create
|
|
@organization = Organization.new(organization_params)
|
|
if @organization.save
|
|
redirect_to @organization, notice: "Organizzazione creata."
|
|
else
|
|
render :new, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def edit
|
|
authorize_organization!(@organization)
|
|
@page_title = "Modifica #{@organization.name}"
|
|
end
|
|
|
|
def update
|
|
authorize_organization!(@organization)
|
|
if @organization.update(organization_params)
|
|
redirect_to @organization, notice: "Organizzazione aggiornata."
|
|
else
|
|
render :edit, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
authorize_organization!(@organization)
|
|
@organization.destroy!
|
|
redirect_to organizations_path, notice: "Organizzazione eliminata."
|
|
end
|
|
|
|
private
|
|
|
|
def set_organization
|
|
@organization = Organization.find(params[:id])
|
|
end
|
|
|
|
def authorize_organization!(org)
|
|
return if current_user.admin?
|
|
return if org.projects.merge(available_projects).exists?
|
|
|
|
redirect_to organizations_path, alert: "Organizzazione non disponibile per i tuoi progetti." and return
|
|
end
|
|
|
|
def load_form_collections
|
|
@users = User.active.order(:first_name)
|
|
@projects = available_projects
|
|
end
|
|
|
|
def organization_params
|
|
params.require(:organization).permit(
|
|
:name, :legal_name, :organization_type, :sport, :country, :region, :province, :city,
|
|
:address, :website, :source_url, :phone, :email, :vat_number, :notes, :status, :lead_source,
|
|
:assigned_user_id, :list_position, :team_gender, :streaming_status, :commercial_fit, :verified_at,
|
|
project_ids: []
|
|
)
|
|
end
|
|
|
|
def apply_filters(scope)
|
|
scope = scope.where(status: params[:status]) if params[:status].present?
|
|
scope = scope.where(lead_source: params[:lead_source]) if params[:lead_source].present?
|
|
scope = scope.where(organization_type: params[:organization_type]) if params[:organization_type].present?
|
|
scope = scope.where(sport: params[:sport]) if params[:sport].present?
|
|
scope = scope.where(country: params[:country]) if params[:country].present?
|
|
scope = scope.where(region: params[:region]) if params[:region].present?
|
|
scope = scope.where(assigned_user_id: params[:owner]) if params[:owner].present?
|
|
scope = scope.where(team_gender: params[:team_gender]) if params[:team_gender].present?
|
|
scope = scope.where(streaming_status: params[:streaming_status]) if params[:streaming_status].present?
|
|
|
|
if params[:customer] == "yes"
|
|
scope = scope.customers
|
|
elsif params[:customer] == "no"
|
|
scope = scope.where.not(status: %w[active_customer inactive_customer])
|
|
end
|
|
|
|
if params[:pipeline_stage].present?
|
|
scope = scope.joins(:opportunities).where(opportunities: { pipeline_stage: params[:pipeline_stage], project_id: current_project.id }).distinct
|
|
end
|
|
|
|
if params[:ab_variant].present?
|
|
scope = scope.joins(:opportunities).where(opportunities: { ab_variant: params[:ab_variant], project_id: current_project.id }).distinct
|
|
end
|
|
|
|
if params[:overdue_tasks] == "1"
|
|
scope = scope.joins(:tasks).merge(Task.overdue).distinct
|
|
end
|
|
|
|
scope
|
|
end
|
|
|
|
def apply_sort(scope)
|
|
case params[:sort]
|
|
when "name"
|
|
scope.order(:name)
|
|
when "updated"
|
|
scope.order(updated_at: :desc)
|
|
when "created"
|
|
scope.order(created_at: :desc)
|
|
when "lista"
|
|
scope.order(Arel.sql("organizations.list_position ASC NULLS LAST, organizations.name ASC"))
|
|
else
|
|
if current_project&.code == "matchlivetv"
|
|
scope.order(Arel.sql("organizations.list_position ASC NULLS LAST, organizations.name ASC"))
|
|
else
|
|
scope.order(updated_at: :desc)
|
|
end
|
|
end
|
|
end
|
|
end
|