add: tests

This commit is contained in:
Michael 2024-08-12 15:55:03 +02:00
parent eebf859afa
commit ce7f620984
16 changed files with 681 additions and 152 deletions

View file

@ -2,49 +2,61 @@
namespace App\Http\Controllers\CsvController\CsvCostCalc;
use App\Utils\utils;
use Illuminate\Support\Facades\Log;
use RuntimeException;
class CsvAgentCost
{
private $internal_percentage_to_deduct;
private $CsvMinuteCostCalc;
public function __construct()
{
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
$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();
}
private function preapare_agents_columns(array $array, array $company_agents)
function prepare_agents_columns(array $array, array $company_agents)
{
//Add all agents columns
foreach ($company_agents as $company_agent) {
$array[$company_agent] = '';
$array[$company_agent . ' costo'] = 0;
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() );
}
return $array;
}
private function calculate_agents_cost(array $array, array $agents_time, float $minute_cost)
function calculate_agents_cost(array $array, array $agents_time, float $minute_cost)
{
$minute_cost = utils::round_up_to_two_decimals($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 / 60 * $minute_cost);
$array[$name] = gmdate('H:i:s', $agent_time);
$array[$name . ' costo'] = $agent_time / 60 * $minute_cost;
$array[$name . ' costo'] = $agent_cost;
}
return $array;
}
function agent_cost_calc(array $array, array $company_agents, array $agents_time)
{
$array = $this->preapare_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;
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);
}
}
}

View file

@ -4,6 +4,7 @@ namespace App\Http\Controllers\CsvController\CsvCostCalc;
use Exception;
use Illuminate\Support\Facades\Log;
use App\Utils\utils;
class CsvMinuteCostCalc
{
@ -27,7 +28,7 @@ class CsvMinuteCostCalc
private function calculate_minute_cost(int $hourly_cost)
{
return $hourly_cost / 60;
return utils::round_up_to_two_decimals($hourly_cost / 60);
}
private function extract_string_from_label($label)
@ -35,7 +36,8 @@ class CsvMinuteCostCalc
return strtolower(substr($label, strpos($label, '/') + 1));
}
function select_correct_cost($requested_by, $priority)
function
select_correct_cost($requested_by, $priority)
{
$requested_by = $this->extract_string_from_label($requested_by);
$priority = $this->extract_string_from_label($priority);

View file

@ -1,20 +1,21 @@
<?php
namespace App\Http\Controllers\CsvController\CsvCostCalc;
use function Utils\round_up_to_two_decimals;
class CsvTotalCostCalc
{
private $CsvMinuteCostCalc;
public function __construct()
{
$this->CsvMinuteCostCalc = new CsvMinuteCostCalc();
}
function total_time_cost(array $array, int $total_time)
{
$minute_cost = $this->CsvMinuteCostCalc->select_correct_cost($array['Request By'], $array['Priority']);
$total_cost = $total_time / 60 * $minute_cost;
$total_cost = round_up_to_two_decimals($total_time / 60 * $minute_cost);
$array['Tempo totale'] = gmdate('H:i:s', $total_time);
$array['Costo totale'] = $total_cost;
return $array;