284 lines
9.0 KiB
PHP
284 lines
9.0 KiB
PHP
<?php
|
|
|
|
class Api extends MY_Controller
|
|
{
|
|
var $url_renderer;
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->url_renderer = "http://devkedungdoro.aplikasi.web.id:3000/chart";
|
|
}
|
|
|
|
function staff_group($sdate, $edate)
|
|
{
|
|
$sql = "SELECT employee_id, employee_name, ROUND(SUM(unit_amount),1) AS s_hour
|
|
FROM one_support.odoo_timesheet
|
|
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
|
|
GROUP BY employee_id";
|
|
|
|
$qry_total_data = $this->db->query($sql);
|
|
$this->check_error($qry_total_data, "get xTotalAll mcu");
|
|
$rows = $qry_total_data->result_array();
|
|
if (count($rows) == 0) {
|
|
$result = array(
|
|
'employeeNames' => ["No Employee Found"],
|
|
'employeeIds' => [0],
|
|
's_hour' => [0]
|
|
);
|
|
header("Content-Type: application/json");
|
|
echo json_encode($result);
|
|
return;
|
|
}
|
|
|
|
$employeeNames = array();
|
|
$employeeIds = array();
|
|
$employeeHours = array();
|
|
|
|
foreach ($rows as $row) {
|
|
$employeeNames[] = $row['employee_name'];
|
|
$employeeIds[] = $row['employee_id'];
|
|
$employeeHours[] = $row['s_hour'];
|
|
}
|
|
|
|
// Remove duplicate entries and reindex arrays
|
|
$employeeNames = array_values(array_unique($employeeNames));
|
|
$employeeIds = array_values(array_unique($employeeIds));
|
|
$employeeHours = array_values($employeeHours);
|
|
|
|
// Prepare response
|
|
$response = array(
|
|
'employeeNames' => $employeeNames,
|
|
'employeeIds' => $employeeIds,
|
|
's_hour' => $employeeHours
|
|
);
|
|
|
|
// Set header and output JSON
|
|
header('Content-Type: application/json');
|
|
echo json_encode($response);
|
|
}
|
|
|
|
function project_group($sdate, $edate)
|
|
{
|
|
// Query untuk mengambil data dari database
|
|
$sql = "SELECT project_name, sum(unit_amount) as s_hour
|
|
FROM one_support.odoo_timesheet
|
|
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
|
|
GROUP BY project_name
|
|
ORDER BY s_hour DESC";
|
|
$qry = $this->db->query($sql);
|
|
$this->check_error($qry, "get project_name");
|
|
$rows = $qry->result_array();
|
|
|
|
// Periksa apakah ada data
|
|
if (count($rows) == 0) {
|
|
$result = array(
|
|
'projectNames' => ["No Data Found"],
|
|
'projectHours' => [0],
|
|
'hours' => [0]
|
|
);
|
|
header("Content-Type: application/json");
|
|
echo json_encode($result);
|
|
return;
|
|
}
|
|
|
|
// Inisialisasi array untuk nama dan jam proyek
|
|
$projectNames = [];
|
|
$projectHours = [];
|
|
$hours = [];
|
|
$total_val = 0.0;
|
|
|
|
// Hitung total jam
|
|
foreach ($rows as $itm) {
|
|
$total_val += $itm['s_hour'];
|
|
}
|
|
|
|
$other_val = 0.0;
|
|
$limit = count($rows); // Limit jumlah item yang ditampilkan secara langsung
|
|
$count = 0;
|
|
|
|
// Tambahkan data ke array
|
|
foreach ($rows as $itm) {
|
|
$formatted = number_format(($itm['s_hour'] / $total_val) * 100, 2, '.', '');
|
|
if ($count < $limit) {
|
|
$projectNames[] = $itm['project_name'];
|
|
$projectHours[] = $formatted;
|
|
$hours[] = number_format($itm['s_hour'], 2);
|
|
} else {
|
|
$other_val += floatval($formatted);
|
|
$other_hour += number_format($itm['s_hour'], 2);
|
|
;
|
|
}
|
|
$count++;
|
|
}
|
|
|
|
// Tambahkan data 'Lainnya' jika ada
|
|
if ($other_val > 0) {
|
|
$projectNames[] = 'Lainnya';
|
|
$projectHours[] = floatval($other_val);
|
|
$hours[] = $other_hour;
|
|
}
|
|
|
|
// Output data dalam format JSON
|
|
$result = array(
|
|
'projectNames' => $projectNames,
|
|
'projectHours' => $projectHours,
|
|
'hours' => $hours
|
|
);
|
|
|
|
header("Content-Type: application/json");
|
|
echo json_encode($result);
|
|
}
|
|
|
|
function project_pramita_sup($sdate, $edate)
|
|
{
|
|
// Query untuk mengambil data dari database
|
|
$sql = "SELECT
|
|
CASE
|
|
WHEN task_name LIKE '%[#%' THEN 'Support'
|
|
WHEN task_name LIKE '%#accounting%' THEN 'Accounting'
|
|
WHEN task_name LIKE '%#support%' THEN 'Support'
|
|
WHEN task_name LIKE '%#point%' THEN 'Point Reward'
|
|
WHEN task_name LIKE '%#mcu%' THEN 'Pola Kelainan'
|
|
WHEN task_name LIKE '%#sehat%' THEN 'Satu Sehat'
|
|
WHEN task_name LIKE '%#inventory%' THEN 'Inventory'
|
|
ELSE 'Other'
|
|
END AS category,
|
|
SUM(unit_amount) AS s_hour
|
|
FROM one_support.odoo_timesheet
|
|
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}' AND project_id = 70
|
|
GROUP BY category
|
|
ORDER BY s_hour DESC";
|
|
$qry = $this->db->query($sql);
|
|
$this->check_error($qry, "get task_name");
|
|
$rows = $qry->result_array();
|
|
|
|
// Periksa apakah ada data
|
|
if (count($rows) == 0) {
|
|
$result = array(
|
|
'projectNames' => ["No Data Found"],
|
|
'projectHours' => [0],
|
|
'hours' => [0]
|
|
);
|
|
header("Content-Type: application/json");
|
|
echo json_encode($result);
|
|
return;
|
|
}
|
|
|
|
// Inisialisasi array untuk nama dan jam proyek
|
|
$projectNames = array();
|
|
$projectHours = array();
|
|
$hours = [];
|
|
$total_val = 0.0;
|
|
|
|
// Hitung total jam
|
|
foreach ($rows as $itm) {
|
|
$total_val += $itm['s_hour'];
|
|
}
|
|
|
|
$other_val = 0.0;
|
|
$limit = count($rows); // Limit jumlah item yang ditampilkan secara langsung
|
|
$count = 0;
|
|
|
|
// Tambahkan data ke array
|
|
foreach ($rows as $itm) {
|
|
$formatted = number_format(($itm['s_hour'] / $total_val) * 100, 2, '.', '');
|
|
if ($count < $limit) {
|
|
$projectNames[] = $itm['category'];
|
|
$projectHours[] = $formatted;
|
|
$hours[] = number_format($itm['s_hour'], 2);
|
|
} else {
|
|
$other_val += floatval($formatted);
|
|
$other_hour += number_format($itm['s_hour'], 2);
|
|
}
|
|
$count++;
|
|
}
|
|
|
|
// Tambahkan data 'Lainnya' jika ada
|
|
if ($other_val > 0) {
|
|
$projectNames[] = 'Lainnya';
|
|
$projectHours[] = floatval($other_val);
|
|
$hours[] = $other_hour;
|
|
}
|
|
|
|
// Output data dalam format JSON
|
|
$result = array(
|
|
'taskCategory' => $projectNames,
|
|
'taskHours' => $projectHours,
|
|
'hours' => $hours
|
|
);
|
|
|
|
header("Content-Type: application/json");
|
|
echo json_encode($result);
|
|
}
|
|
function coba()
|
|
{
|
|
echo ("ADA");
|
|
}
|
|
|
|
function check_error($qry, $stage)
|
|
{
|
|
if (!$qry) {
|
|
$errMsg = $stage . "<br/>" . $this->db->error()["messge"];
|
|
print_r($errMsg);
|
|
$this->chart_error($errMsg);
|
|
}
|
|
}
|
|
function chart_error($msg)
|
|
{
|
|
$param = array(
|
|
'title' => array(
|
|
'show' => true,
|
|
'textStyle' => array(
|
|
'color' => 'grey',
|
|
'fontSize' => 20
|
|
),
|
|
'text' => $msg,
|
|
'left' => 'center',
|
|
'top' => 'center'
|
|
),
|
|
'xAxis' => array(
|
|
'show' => false
|
|
),
|
|
'yAxis' => array(
|
|
'show' => false
|
|
),
|
|
'series' => array()
|
|
);
|
|
header("Content-Type: image/png");
|
|
$config = ["config" => $param];
|
|
$j_param = json_encode($config);
|
|
echo $this->post($this->url_renderer, $j_param);
|
|
exit;
|
|
}
|
|
|
|
function post($url, $data)
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, j120);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Content-Type: application/json",
|
|
"Content-Length: " . strlen($data),
|
|
]);
|
|
$result = curl_exec($ch);
|
|
if (curl_errno($ch) > 0) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => curl_error($ch),
|
|
];
|
|
}
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
if ($httpCode != 200) {
|
|
return [
|
|
"status" => "ERR",
|
|
"message" => "Http Response : $httpCode",
|
|
];
|
|
}
|
|
return $result;
|
|
}
|
|
}
|
|
|