Files
BE_IBL/application/controllers/tools/one-ts/Api.php
2026-04-15 15:23:57 +07:00

432 lines
13 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 staff_detail($type, $id, $sdate, $edate)
{
if ($type == "client") {
$sql = "SELECT project_name item, sum(unit_amount) as s_hour
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
AND employee_id = '$id'
GROUP BY item
ORDER BY item ";
} else {
//project
$sql = "SELECT
if (regexp_substr(task_name,'(#\\\\w+)') = '', 'other', regexp_substr(task_name,'(#\\\\w+)') )
item, sum(unit_amount) as s_hour
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
AND employee_id = '$id'
GROUP BY item
ORDER BY item ";
}
$qry = $this->db->query($sql);
$this->check_error($qry, "get project_name");
$rows = $qry->result_array();
if (count($rows) == 0) {
$result = array(
'item' => ["No Data Found"],
'itemHours' => [0],
'hours' => [0],
);
header("Content-Type: application/json");
echo json_encode($result);
return;
}
$itemName = [];
$itemHours = [];
$hours = [];
$total_val = 0.0;
// Hitung total jam
foreach ($rows as $itm) {
$total_val += $itm['s_hour'];
}
// Tambahkan data ke array
foreach ($rows as $itm) {
$formatted = number_format(($itm['s_hour'] / $total_val) * 100, 2, '.', '');
$itemName[] = $itm['item'];
$itemHours[] = $formatted;
$hours[] = number_format($itm['s_hour'], 2);
}
$result = array(
'itemNames' => $itemName,
'itemHours' => $itemHours,
'hours' => $hours
);
header("Content-Type: application/json");
echo json_encode($result);
}
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}'
AND unit_amount > 0
GROUP BY project_name
ORDER BY project_name ";
$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 = "WITH categories AS (
SELECT 'Support' AS category, 1 AS sort_order
UNION ALL SELECT 'Point Reward', 2
UNION ALL SELECT 'Accounting', 3
UNION ALL SELECT 'Pola Kelainan', 4
UNION ALL SELECT 'Satu Sehat', 5
UNION ALL SELECT 'Inventory', 6
UNION ALL SELECT 'Meeting', 7
UNION ALL SELECT 'Other', 8
)
SELECT
c.category,
COALESCE(SUM(ot.unit_amount), 0) AS s_hour
FROM categories c
LEFT JOIN (
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'
WHEN task_name LIKE '%#meeting%' THEN 'Meeting'
ELSE 'Other'
END AS category,
unit_amount
FROM one_support.odoo_timesheet
WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'
AND project_id = 70
) ot ON c.category = ot.category
GROUP BY c.category, c.sort_order
ORDER BY c.sort_order";
$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(
'taskCategory' => ["No Data Found"],
'taskHours' => [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;
$other_hour = 0.0; // Inisialisasi variabel other_hour
// 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'];
if (is_null($formatted) || is_nan($formatted)) {
$formatted = 0; // Jika null atau NaN, set jadi 0
}
$projectHours[] = $formatted;
$hours[] = number_format($itm['s_hour'], 2);
} else {
$other_val += floatval($formatted);
$other_hour += floatval($itm['s_hour']); // Konversi ke float untuk akurasi
}
$count++;
}
// Pastikan other_val tidak null atau NaN
// Tambahkan data 'Lainnya' jika ada
if ($other_val > 0) {
$projectNames[] = 'Lainnya';
$projectHours[] = floatval($other_val);
$hours[] = number_format($other_hour, 2); // Format nilai 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 all($sdate, $edate)
{
// $sql = "SELECT project_name, sum(unit_amount) as s_hour
// FROM one_support.odoo_timesheet
// WHERE date_trx BETWEEN '{$sdate}' AND '{$edate}'";
// get total hour and employee between start and end date
$sql = "SELECT
COUNT(employee_name) as n_employee,
IFNULL(SUM(s_hour), 0) as total
FROM
(SELECT employee_name,
project_name, sum(unit_amount) as s_hour
FROM one_support.odoo_timesheet
WHERE
date_trx BETWEEN '{$sdate}' AND '{$edate}'
GROUP BY employee_name
) as x";
$qry = $this->db->query($sql);
$this->check_error($qry, "get project_name");
$rows = $qry->result_array();
if (count($rows) == 0) {
$this->chart_error("No data found");
}
// get total weekdays between start and end date
$sql_weekdays = "SELECT
COUNT(day_code) as tot_weekday
FROM
(SELECT
WEEKDAY(date_trx) as day_code
FROM
one_support.odoo_timesheet
WHERE
date_trx BETWEEN '{$sdate}' AND '{$edate}'
GROUP BY date_trx
) as x
WHERE x.day_code != 6 AND x.day_code != 5";
$wk_qry = $this->db->query($sql_weekdays);
$this->check_error($wk_qry, "get project_name");
$row_wk = $wk_qry->result_array();
if (count($row_wk) == 0) {
$this->chart_error("No data found");
}
// calc percentage total hour / n_weekday * 8 * n_employee
$week_hour = $row_wk[0]['tot_weekday'] * 8 * $rows[0]['n_employee'];
$value = ($rows[0]['total'] / $week_hour) * 100;
$formatted = number_format($value, 2, '.', '');
if (is_nan(floatval($formatted)) || is_infinite(floatval($formatted))) {
$formatted = 0;
}
$result = [
'value' => $formatted
];
// 3. encapsulate in config attribute and json encode
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;
}
}