add: all
This commit is contained in:
parent
a15319c4d1
commit
eebf859afa
39 changed files with 2742 additions and 937 deletions
84
app/Http/Controllers/CsvController/CsvController.php
Normal file
84
app/Http/Controllers/CsvController/CsvController.php
Normal file
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Error;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Xls;
|
||||
use PhpOffice\PhpSpreadsheet\Writer\Ods;
|
||||
use RuntimeException;
|
||||
|
||||
class CsvController extends Controller
|
||||
{
|
||||
private $csv_data_init;
|
||||
|
||||
private $csv_data_handling;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->csv_data_init = new CsvDataInit();
|
||||
$this->csv_data_handling = new CsvDataHandling();
|
||||
$this->third_party_integrations_allow = env('THIRD_PARTY_INTEGRATIONS_ALLOW', false);
|
||||
}
|
||||
|
||||
function create_columns(array $issue, array $issue_time, array $company_agents = [])
|
||||
{
|
||||
try {
|
||||
$array = $this->csv_data_init->initialize_csv_data($issue);
|
||||
$array = $this->csv_data_init->handle_labels($array, $issue['labels']);
|
||||
$array = $this->csv_data_handling->handle_csv_time($array, $issue, $issue_time, $company_agents);
|
||||
return $array;
|
||||
} catch (Error $e) {
|
||||
Log::error('E-CSV-COLUMNS - ' . $e->getMessage());
|
||||
throw new RuntimeException("E-CSV-GEN");
|
||||
}
|
||||
}
|
||||
|
||||
private function create_calc_document($reader, $file_name, $is_xls = false)
|
||||
{
|
||||
// Determine the appropriate writer based on the file type
|
||||
$file_extension = $is_xls ? '.xls' : '.csv';
|
||||
$writerClass = $is_xls ? Xls::class : Csv::class;
|
||||
// Create the writer instance
|
||||
$writer = new $writerClass($reader);
|
||||
$file_name = $file_name . $file_extension;
|
||||
$file_path = storage_path() . '/app/' . $file_name;
|
||||
$writer->save($file_path);
|
||||
|
||||
//Define header information
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: application/octet-stream');
|
||||
header("Cache-Control: no-cache, must-revalidate");
|
||||
header("Expires: 0");
|
||||
header('Content-Disposition: attachment; filename="' . basename($file_name) . '"');
|
||||
header('Content-Length: ' . filesize($file_path));
|
||||
header('Pragma: public');
|
||||
|
||||
//Terminate from the script
|
||||
return ['file_name' => $file_name, 'file_path' => $file_path];
|
||||
}
|
||||
|
||||
function create_csv(string $issues_params_state, string $from_date, string $to_date, array $data)
|
||||
{
|
||||
try {
|
||||
$file_name = $issues_params_state . "_issues_from_" . $from_date . "_to_" . $to_date;
|
||||
$reader = new Spreadsheet();
|
||||
$spreadsheet = $reader->getActiveSheet();
|
||||
$headers = array_keys($data[0]);
|
||||
$data_new = array_unshift($data, $headers);
|
||||
$spreadsheet->fromArray(
|
||||
$data
|
||||
);
|
||||
// The code is enclosed in a function for eventual support of multiple file types as xls and ods, as of now a csv will be generated
|
||||
$csv_file = $this->create_calc_document($reader, $file_name);
|
||||
// $ods_file = $this->third_party_integrations_allow ? $this->create_calc_document($reader, $file_name) : null;
|
||||
return $csv_file;
|
||||
} catch (Error $e) {
|
||||
Log::error('E-CSV-GEN - ' . $e->getMessage());
|
||||
throw new RuntimeException("E-CSV-GEN");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
class CsvAgentCost
|
||||
{
|
||||
private $internal_percentage_to_deduct;
|
||||
private $CsvMinuteCostCalc;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
|
||||
$this->partner_organitation = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
|
||||
$this->CsvMinuteCostCalc = new CsvMinuteCostCalc();
|
||||
}
|
||||
|
||||
private function preapare_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;
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
private 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
|
||||
$array[$name] = gmdate('H:i:s', $agent_time);
|
||||
$array[$name . ' costo'] = $agent_time / 60 * $minute_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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
class CsvCostCalc
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
|
||||
$this->partner_organitation = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
|
||||
$this->CsvAgentCost = new CsvAgentCost();
|
||||
$this->CsvTotalCostCalc = new CsvTotalCostCalc();
|
||||
$this->CsvSumTotalsCalc = new CsvSumTotalsCalc();
|
||||
}
|
||||
|
||||
|
||||
function total_time_cost(array $array, int $total_time)
|
||||
{
|
||||
return $this->CsvTotalCostCalc->total_time_cost($array, $total_time);
|
||||
}
|
||||
|
||||
function agent_cost_calc(array $array, array $company_agents, array $agents_time)
|
||||
{
|
||||
return $this->CsvAgentCost->agent_cost_calc($array, $company_agents, $agents_time);
|
||||
}
|
||||
|
||||
function sum_costs(array $data, array $company_agents = [])
|
||||
{
|
||||
return $this->CsvSumTotalsCalc->sum_costs($data, $company_agents);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CsvMinuteCostCalc
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->internal_percentage_to_deduct = getenv('PRICE_INTERNAL_PERCENTAGE_TO_DEDUCT');
|
||||
$this->partner_organization = strtolower(getenv('GITEA_PARTNER_ORGANIZATION'));
|
||||
$this->price_partner = [
|
||||
'high' => $this->calculate_minute_cost((int)getenv('PRICE_PARTNER_HIGH')),
|
||||
'normal' => $this->calculate_minute_cost((int)getenv('PRICE_PARTNER_NORMAL')),
|
||||
'low' => $this->calculate_minute_cost((int)getenv('PRICE_PARTNER_LOW')),
|
||||
'0'=>0,
|
||||
];
|
||||
$this->price_client = [
|
||||
'high' => $this->calculate_minute_cost((int)getenv('PRICE_CLIENT_HIGH')),
|
||||
'normal' => $this->calculate_minute_cost((int)getenv('PRICE_CLIENT_NORMAL')),
|
||||
'low' => $this->calculate_minute_cost((int)getenv('PRICE_CLIENT_LOW')),
|
||||
'0'=>0,
|
||||
];
|
||||
}
|
||||
|
||||
private function calculate_minute_cost(int $hourly_cost)
|
||||
{
|
||||
return $hourly_cost / 60;
|
||||
}
|
||||
|
||||
private function extract_string_from_label($label)
|
||||
{
|
||||
return strtolower(substr($label, strpos($label, '/') + 1));
|
||||
}
|
||||
|
||||
function select_correct_cost($requested_by, $priority)
|
||||
{
|
||||
$requested_by = $this->extract_string_from_label($requested_by);
|
||||
$priority = $this->extract_string_from_label($priority);
|
||||
|
||||
$requested_by_partner = str_contains($requested_by, strtolower($this->partner_organization));
|
||||
try {
|
||||
$priority = match ($priority) {
|
||||
'critical', 'high' => 'high',
|
||||
'medium' => 'normal',
|
||||
'low' => 'low',
|
||||
default =>'0'
|
||||
};
|
||||
} catch (Exception $e) {
|
||||
Log::error('E-CSV-MINUTECOSTCALC - '. $e->getMessage());
|
||||
}
|
||||
if ($requested_by_partner) {
|
||||
return $this->price_partner[$priority];
|
||||
}
|
||||
return $this->price_client[$priority];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
class CsvSumTotalsCalc
|
||||
{
|
||||
|
||||
private $calculate_agent_cost;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST');
|
||||
}
|
||||
|
||||
private function sum_total_cost(array $array)
|
||||
{
|
||||
(float)$total = 0;
|
||||
|
||||
foreach ($array as $issue_total) {
|
||||
$issue_total_cost = $issue_total['Costo totale'];
|
||||
$total += $issue_total_cost;
|
||||
}
|
||||
|
||||
$totalSumCsvLine = array(
|
||||
'Progetto' => '',
|
||||
'#' => '',
|
||||
'Titolo' => '',
|
||||
'URL' => '',
|
||||
'Aperto_il' => '',
|
||||
'Chiuso_il' => '',
|
||||
'Etichette' => '',
|
||||
'Kind' => '',
|
||||
'Request By' => '',
|
||||
'Priority' => '',
|
||||
'Tempo totale' => '',
|
||||
'Costo totale' => $total,
|
||||
);
|
||||
|
||||
array_push($array, $totalSumCsvLine);
|
||||
return $array;
|
||||
}
|
||||
|
||||
private function add_agents_csv_line($agent_struct, $last_csv_line)
|
||||
{
|
||||
$agents_csv_line = $last_csv_line;
|
||||
foreach ($agent_struct as $agent_name => $agent_sum) {
|
||||
$agents_csv_line[$agent_name] = '';
|
||||
$agents_csv_line[$agent_name . ' costo'] = $agent_sum;
|
||||
}
|
||||
return $agents_csv_line;
|
||||
}
|
||||
|
||||
private function init_agents_struct($company_agents){
|
||||
return array_combine($company_agents, array_fill(0, count($company_agents), 0));
|
||||
}
|
||||
|
||||
private function extract_agent_cost(array $array,array $agent_struct){
|
||||
$i=0;
|
||||
$issue_length = count( $array );
|
||||
foreach ($array as $issue) {
|
||||
if(++$i == $issue_length) break;
|
||||
foreach ($agent_struct as $agent_name => $agent_sum) {
|
||||
$agent_cost = $issue[$agent_name . ' costo'];
|
||||
$agent_struct[$agent_name] += $agent_cost;
|
||||
}
|
||||
}
|
||||
return $agent_struct;
|
||||
}
|
||||
private function sum_agents_cost($array, $company_agents)
|
||||
{
|
||||
$agent_struct = $this->init_agents_struct($company_agents);
|
||||
$agent_struct = $this->extract_agent_cost($array, $agent_struct);
|
||||
$agents_csv_line = $this->add_agents_csv_line($agent_struct, end($array));
|
||||
$array[key($array)] = $agents_csv_line;
|
||||
return $array;
|
||||
}
|
||||
|
||||
function sum_costs(array $array, $company_agents = [])
|
||||
{
|
||||
$array = $this->sum_total_cost($array);
|
||||
|
||||
if ($this->calculate_agent_cost) {
|
||||
$array = $this->sum_agents_cost($array, $company_agents);
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController\CsvCostCalc;
|
||||
|
||||
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;
|
||||
$array['Tempo totale'] = gmdate('H:i:s', $total_time);
|
||||
$array['Costo totale'] = $total_cost;
|
||||
return $array;
|
||||
}
|
||||
}
|
47
app/Http/Controllers/CsvController/CsvDataHandling.php
Normal file
47
app/Http/Controllers/CsvController/CsvDataHandling.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController;
|
||||
|
||||
use App\Http\Controllers\CsvController\CsvCostCalc\CsvCostCalc;
|
||||
|
||||
class CsvDataHandling
|
||||
{
|
||||
private $calculate_agent_cost;
|
||||
private $csv_cost_calc;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->calculate_agent_cost = env('GITEA_CALCULATE_AGENT_COST');
|
||||
$this->csv_cost_calc = new CsvCostCalc();
|
||||
}
|
||||
|
||||
private function handle_agent_issue($agents_issue_time, $time)
|
||||
{
|
||||
$time_agent = $time['user_name'];
|
||||
!array_key_exists($time_agent, $agents_issue_time) && $agents_issue_time[$time_agent] = 0;
|
||||
$agents_issue_time[$time_agent] += $time['time'];
|
||||
return $agents_issue_time;
|
||||
}
|
||||
|
||||
private function get_issue_total_time(array $issue_time)
|
||||
{
|
||||
$total_issue_time = 0;
|
||||
$agents_issue_time = [];
|
||||
foreach ($issue_time as $time) {
|
||||
$total_issue_time += (int)$time['time'];
|
||||
$this->calculate_agent_cost && $agents_issue_time = $this->handle_agent_issue($agents_issue_time, $time);
|
||||
}
|
||||
|
||||
return [$total_issue_time, $agents_issue_time];
|
||||
}
|
||||
|
||||
function handle_csv_time(array $array, array $issue, array $issue_time, array $company_agents = [])
|
||||
{
|
||||
[$total_time, $agents_time] = $this->get_issue_total_time($issue_time);
|
||||
$array = $this->csv_cost_calc->total_time_cost($array, $total_time);
|
||||
if ($this->calculate_agent_cost) {
|
||||
$array = $this->csv_cost_calc->agent_cost_calc($array, $company_agents, $agents_time);
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
}
|
50
app/Http/Controllers/CsvController/CsvDataInit.php
Normal file
50
app/Http/Controllers/CsvController/CsvDataInit.php
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\CsvController;
|
||||
|
||||
class CsvDataInit
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function get_issue_labels(array $issue)
|
||||
{
|
||||
$labels = '';
|
||||
foreach ($issue['labels'] as $label) {
|
||||
$labels .= $label['name'] . ',';
|
||||
}
|
||||
return $labels;
|
||||
}
|
||||
|
||||
function handle_labels($array, $issue_labels){
|
||||
foreach ($issue_labels as $key => $label) {
|
||||
if (strpos($label['name'],'Kind') !== false) {
|
||||
$array['Kind'] = $label['name'];
|
||||
}
|
||||
if (strpos($label['name'],'RequestBy') !== false) {
|
||||
$array['Request By'] = $label['name'];
|
||||
}
|
||||
if (strpos($label['name'],'Priority') !== false) {
|
||||
$array['Priority'] = $label['name'];
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
function initialize_csv_data(array $issue){
|
||||
return array(
|
||||
'Progetto' => $issue['repository']['name'],
|
||||
'#' => $issue['number'],
|
||||
'Titolo' => $issue['title'],
|
||||
'URL' => $issue['html_url'],
|
||||
'Aperto_il' => $issue['created_at'],
|
||||
'Chiuso_il' => $issue['closed_at'],
|
||||
'Etichette' => $this->get_issue_labels($issue),
|
||||
'Kind' => '',
|
||||
'Request By' => '',
|
||||
'Priority' => '',
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue