2024-07-22 14:17:00 +02:00
< ? php
namespace App\Http\Controllers\CsvController\CsvCostCalc ;
2024-08-12 15:55:03 +02:00
use App\Utils\utils ;
use Illuminate\Support\Facades\Log ;
use RuntimeException ;
2024-07-22 14:17:00 +02:00
class CsvAgentCost
{
public function __construct ()
{
2024-08-12 15:55:03 +02:00
$this -> internal_percentage_to_deduct = ( float ) getenv ( 'PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT' , null );
2024-07-22 14:17:00 +02:00
$this -> partner_organitation = strtolower ( getenv ( 'GITEA_PARTNER_ORGANIZATION' ));
$this -> CsvMinuteCostCalc = new CsvMinuteCostCalc ();
}
2024-08-12 15:55:03 +02:00
function prepare_agents_columns ( array $array , array $company_agents )
2024-07-22 14:17:00 +02:00
{
2024-08-12 15:55:03 +02:00
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 () );
2024-07-22 14:17:00 +02:00
}
}
2024-08-12 15:55:03 +02:00
function calculate_agents_cost ( array $array , array $agents_time , float $minute_cost )
2024-07-22 14:17:00 +02:00
{
foreach ( $agents_time as $name => $agent_time ) {
//Identify agents involved in the issue
2024-08-14 12:46:25 +02:00
$agent_cost = utils :: round_up_to_two_decimals ( $agent_time * $minute_cost / 60 );
2024-07-22 14:17:00 +02:00
$array [ $name ] = gmdate ( 'H:i:s' , $agent_time );
2024-08-12 15:55:03 +02:00
$array [ $name . ' costo' ] = $agent_cost ;
2024-07-22 14:17:00 +02:00
}
return $array ;
}
function agent_cost_calc ( array $array , array $company_agents , array $agents_time )
{
2024-08-12 15:55:03 +02:00
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 );
}
2024-07-22 14:17:00 +02:00
}
}