gitea_issues_exporter/app/Http/Controllers/CsvController/CsvCostCalc/CsvAgentCost.php

62 lines
2.3 KiB
PHP

<?php
namespace App\Http\Controllers\CsvController\CsvCostCalc;
use App\Utils\utils;
use Illuminate\Support\Facades\Log;
use RuntimeException;
class CsvAgentCost
{
public function __construct()
{
$this->internal_percentage_to_deduct = (float) getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT', null);
$this->partner_organitation = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
$this->CsvMinuteCostCalc = new CsvMinuteCostCalc();
}
function prepare_agents_columns(array $array, array $company_agents)
{
try {
//Add all agents columns
foreach ($company_agents as $company_agent) {
$array[$company_agent] = '';
$array[$company_agent . ' costo'] = 0;
}
return $array;
} catch (\Exception $e) {
Log::error('prepare_agents_columns E-AGENT-COLUMNS - ' . $e->getMessage());
throw new RuntimeException("E-AGENT-COLUMNS - " . $e->getMessage() );
}
}
function calculate_agents_cost(array $array, array $agents_time, float $minute_cost)
{
foreach ($agents_time as $name => $agent_time) {
//Identify agents involved in the issue
$agent_cost = utils::round_up_to_two_decimals($agent_time * $minute_cost / 60);
$array[$name] = gmdate('H:i:s', $agent_time);
$array[$name . ' costo'] = $agent_cost;
}
return $array;
}
function agent_cost_calc(array $array, array $company_agents, array $agents_time)
{
try {
if(!is_int($this->internal_percentage_to_deduct) && !is_float($this->internal_percentage_to_deduct)) throw new RuntimeException("agent_cost_calc E-AGENT-PERCENTAGE - Internal percentage is not set or NaN");
$array = $this->prepare_agents_columns($array, $company_agents);
//Calculate cost, subtract a % from the minute_cost
$minute_cost = $this->CsvMinuteCostCalc->select_correct_cost($array['Request By'], $array['Priority']);
$minute_cost = $minute_cost - ($minute_cost / 100 * $this->internal_percentage_to_deduct);
$array = $this->calculate_agents_cost($array, $agents_time, $minute_cost);
return $array;
} catch (\Exception $e) {
Log::error($e->getMessage());
throw new RuntimeException($e);
}
}
}