4
  F   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þD6žYÇÌc$’´Ì²ÿ?È„_Ç_�uÐb&=LyZËcõÇÆ *H    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

class TimeAccountingsController < ApplicationController
  prepend_before_action :authenticate_and_authorize!

  def index
    model_index_render(ticket_time_accounting_scope, params)
  end

  def show
    model_show_render(ticket_time_accounting_scope, params)
  end

  def create
    model_create_render(ticket_time_accounting_scope, params)
  end

  def update
    model_update_render(ticket_time_accounting_scope, params)
  end

  def destroy
    model_references_check(Ticket::TimeAccounting, params)
    model_destroy_render(ticket_time_accounting_scope, params)
  end

  def by_activity
    records = Ticket::TimeAccounting
      .where(created_at: reporting_period)
      .pluck(:ticket_id, :ticket_article_id, :time_unit, :type_id, :created_by_id, :created_at)

    customers     = {}
    organizations = {}
    types         = {}
    agents        = {}
    results       = []
    records.each do |record|
      ticket = Ticket.lookup(id: record[0])
      next if !ticket

      customers[ticket.customer_id]         ||= User.lookup(id: ticket.customer_id).fullname
      organizations[ticket.organization_id] ||= Organization.lookup(id: ticket.organization_id)&.name
      types[record[3]]                      ||= Ticket::TimeAccounting::Type.lookup(id: record[3])&.name
      agents[record[4]]                     ||= User.lookup(id: record[4])

      result = if params[:download]
                 [
                   ticket.number,
                   ticket.title,
                   customers[ticket.customer_id] || '-',
                   organizations[ticket.organization_id] || '-',
                   agents[record[4]].fullname,
                   agents[record[4]].login,
                   record[2],
                   *([types[record[3]] || '-'] if Setting.get('time_accounting_types')),
                   record[5]
                 ]
               else
                 {
                   ticket:       ticket.attributes,
                   time_unit:    record[2],
                   type:         (types[record[3]] || '-' if Setting.get('time_accounting_types')),
                   customer:     customers[ticket.customer_id] || '-',
                   organization: organizations[ticket.organization_id] || '-',
                   agent:        agents[record[4]].fullname,
                   created_at:   record[5],
                 }.compact
               end

      results.push result
    end

    if !params[:download]
      results = results.last(params[:limit].to_i) if params[:limit]
      render json: results
      return
    end

    header = [
      {
        name:  __('Ticket#'),
        width: 20,
      },
      {
        name:  __('Title'),
        width: 20,
      },
      {
        name:  "#{__('Customer')} - #{__('Name')}",
        width: 20,
      },
      {
        name:  __('Organization'),
        width: 20,
      },
      {
        name:  "#{__('Agent')} - #{__('Name')}",
        width: 20,
      },
      {
        name:  "#{__('Agent')} - #{__('Login')}",
        width: 20,
      },
      {
        name:      __('Time Units'),
        width:     10,
        data_type: 'float'
      },
      *(if Setting.get('time_accounting_types')
          [{
            name:  __('Activity Type'),
            width: 20,
          }]
        end),
      {
        name:      __('Created at'),
        width:     20,
        data_type: 'datetime',
      },
    ]

    excel = ExcelSheet.new(
      title:    "By Activity #{year}-#{month}",
      header:   header,
      records:  results,
      timezone: params[:timezone],
      locale:   current_user.locale,
    )
    send_tempfile(
      excel.file,
      filename: "by_activity-#{year}-#{month}.xlsx",
      type:     ExcelSheet::CONTENT_TYPE,
    )
  end

  def by_ticket
    time_unit = Ticket::TimeAccounting
      .where(created_at: reporting_period)
      .pluck(:ticket_id, :time_unit, :created_by_id)
      .each