3040 lines
132 KiB
PHP
3040 lines
132 KiB
PHP
<?php
|
|
|
|
class Piutang extends MY_Controller
|
|
{
|
|
var $db;
|
|
public function index()
|
|
{
|
|
echo "Piutang API";
|
|
}
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
// $this->db = $this->load->database("default", true);
|
|
$this->db->query("use one_dash");
|
|
}
|
|
|
|
function corss()
|
|
{
|
|
global $_SERVER;
|
|
if (isset($_SERVER["HTTP_ORIGIN"])) {
|
|
header("Access-Control-Allow-Origin: " . $_SERVER["HTTP_ORIGIN"]);
|
|
} else {
|
|
header("Access-Control-Allow-Origin: */*");
|
|
}
|
|
header("Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS");
|
|
header(
|
|
"Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization"
|
|
);
|
|
if (
|
|
isset($_SERVER["REQUEST_METHOD"]) &&
|
|
$_SERVER["REQUEST_METHOD"] == "OPTIONS"
|
|
) {
|
|
http_response_code(200);
|
|
echo json_encode("OK");
|
|
exit();
|
|
}
|
|
}
|
|
|
|
public function getClassificationNumber($number_param_x)
|
|
{
|
|
$this->corss();
|
|
// $number_fix = 0;
|
|
// if ($number_param < 0) {
|
|
// $number_fix = $number_param * -1;
|
|
// } else {
|
|
// $number_fix = $number_param;
|
|
// }
|
|
|
|
$number_param = abs($number_param_x);
|
|
$string = "Puluhan";
|
|
|
|
if ($number_param >= 1000000000) {
|
|
// echo "Angka ini dalam miliar";
|
|
$number_format = $number_param / 1000000000;
|
|
$formatted_number_fix = number_format($number_format, 2);
|
|
$string = ($number_param_x >= 0 ? '' : '-') . $formatted_number_fix . " M";
|
|
} elseif ($number_param >= 1000000) {
|
|
// echo "Angka ini dalam juta";
|
|
$number_format = $number_param / 1000000;
|
|
$formatted_number_fix = number_format($number_format, 2);
|
|
$string = ($number_param_x >= 0 ? '' : '-') . $formatted_number_fix . " Juta";
|
|
} elseif ($number_param >= 1000) {
|
|
// echo "Angka ini dalam ribuan";
|
|
$number_format = $number_param / 1000;
|
|
$formatted_number_fix = number_format($number_format, 2);
|
|
$string = ($number_param_x >= 0 ? '' : '-') . $formatted_number_fix . " Ribu";
|
|
} elseif ($number_param >= 100) {
|
|
$number_format = $number_param / 100;
|
|
$formatted_number_fix = number_format($number_format, 2);
|
|
$string = ($number_param_x >= 0 ? '' : '-') . $formatted_number_fix . " Ratus";
|
|
} else {
|
|
// echo "Angka ini dalam ratusan";
|
|
$number_format = $number_param / 10;
|
|
$formatted_number_fix = number_format($number_format, 2);
|
|
$string = ($number_param_x >= 0 ? '' : '-') . $formatted_number_fix . " Puluh";
|
|
}
|
|
|
|
return $string;
|
|
}
|
|
|
|
public function list_color_bar_chart_v1($index, $list_scope)
|
|
{
|
|
$this->corss();
|
|
// diurutkan S_RegionalName ASC
|
|
$listColor = [
|
|
"#100003",
|
|
"#39000B",
|
|
"#49000F",
|
|
"#610014",
|
|
"#720017",
|
|
"#6A0000",
|
|
"#820000",
|
|
"#9B0000",
|
|
"#B30000",
|
|
"#C00",
|
|
"#D40026",
|
|
"#EC0030",
|
|
"#E40000",
|
|
"#FC0000",
|
|
"#FF0606",
|
|
"#FF1E1E",
|
|
"#FF4747",
|
|
"#FF2E2E",
|
|
"#FF4747",
|
|
"#FF5757",
|
|
"#FF5F5F",
|
|
"#FF7070",
|
|
"#FF8080",
|
|
"#FF9090",
|
|
];
|
|
|
|
// define default color grey jika list scope != listColor
|
|
if ($list_scope != count($listColor)) {
|
|
if ($list_scope > count($listColor)) {
|
|
$greyCount = count($list_scope) - count($listColor);
|
|
$greyColors = array_fill(0, $greyCount, "#888888");
|
|
$resultColor = array_merge($listColor, $greyColors);
|
|
$listColor = $resultColor;
|
|
} else {
|
|
$listColor[$index];
|
|
}
|
|
} else {
|
|
$listColor[$index];
|
|
}
|
|
|
|
return $listColor[$index];
|
|
}
|
|
|
|
public function list_color_bar_chart($index, $list_scope)
|
|
{
|
|
$this->corss();
|
|
// diurutkan S_RegionalName ASC
|
|
$listColor = [
|
|
"#D00000",
|
|
"#FFBA08",
|
|
"#CBFF8C",
|
|
"#1B998B",
|
|
"#3185FC",
|
|
"#5D2E8C",
|
|
"#FF7B9C",
|
|
"#4D86A5",
|
|
"#CF0BF1",
|
|
"#12E2F1",
|
|
"#DDA1BA",
|
|
"#706997",
|
|
"#C3C4E9",
|
|
"#1A3D84",
|
|
"#98B29D",
|
|
"#333333",
|
|
"#104E53",
|
|
"#BC9E68",
|
|
"#654E1B",
|
|
"#6E1653",
|
|
"#F6D67F",
|
|
"#FADFDB",
|
|
"#FF9B85",
|
|
// "#8FE388",
|
|
"#46237A",
|
|
];
|
|
|
|
// define default color grey jika list scope != listColor
|
|
if ($list_scope != count($listColor)) {
|
|
if ($list_scope > count($listColor)) {
|
|
$greyCount = count($list_scope) - count($listColor);
|
|
// $countListColor = count($listColor);
|
|
// $greyCount = intval($list_scope) - intval($countListColor);
|
|
$greyColors = array_fill(0, $greyCount, "#888888");
|
|
$resultColor = array_merge($listColor, $greyColors);
|
|
$listColor = $resultColor;
|
|
} else {
|
|
$listColor[$index];
|
|
}
|
|
} else {
|
|
$listColor[$index];
|
|
}
|
|
|
|
return $listColor[$index];
|
|
}
|
|
|
|
public function getQuarterByFilter($waktu)
|
|
{
|
|
// Quarter diawali Q
|
|
// Month diawali M
|
|
|
|
if ($waktu == "Q1") {
|
|
$quarterResult = "1";
|
|
} elseif ($waktu == "Q2") {
|
|
$quarterResult = "2";
|
|
} elseif ($waktu == "Q3") {
|
|
$quarterResult = "3";
|
|
} elseif ($waktu == "Q4") {
|
|
$quarterResult = "4";
|
|
} else {
|
|
$quarterResult = "";
|
|
}
|
|
|
|
$result = $quarterResult;
|
|
|
|
return $result;
|
|
}
|
|
|
|
// national start
|
|
public function nat_total_xold1($debug = "")
|
|
{
|
|
$this->corss();
|
|
try {
|
|
// if (!$this->isLogin) {
|
|
// $this->sys_error("Invalid Token");
|
|
// exit;
|
|
// }
|
|
// $param = $this->sys_input;
|
|
|
|
$date_now = date('Y-m-d');
|
|
$end_date_year_min_1 = date('Y-m-d', strtotime($date_now . ' -1 year'));
|
|
|
|
// tahun berjalan
|
|
$sql_tahun_berjalan_a = "SELECT ifnull(SUM(`Nat_SalesTotal`),0) AS a
|
|
FROM `nat_sales`
|
|
WHERE `Nat_SalesYear` = year('$date_now')
|
|
AND Nat_SalesIsActive = 'Y' ";
|
|
|
|
$qry_tahun_berjalan_a = $this->db->query($sql_tahun_berjalan_a, []);
|
|
if (!$qry_tahun_berjalan_a) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_tahun_berjalan_a = $this->db->last_query();
|
|
$rst_tahun_berjalan_a = $qry_tahun_berjalan_a->result_array();
|
|
|
|
$sql_tahun_berjalan_b = "SELECT ifnull(SUM(Nat_PaymentTotal),0) AS b
|
|
FROM nat_payment
|
|
WHERE Nat_PaymentOrderYear = year('$date_now')
|
|
AND Nat_PaymentIsActive = 'Y' ";
|
|
|
|
$qry_tahun_berjalan_b = $this->db->query($sql_tahun_berjalan_b, []);
|
|
if (!$qry_tahun_berjalan_b) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_tahun_berjalan_b = $this->db->last_query();
|
|
$rst_tahun_berjalan_b = $qry_tahun_berjalan_b->result_array();
|
|
|
|
$rst_piutang_berjalan = $rst_tahun_berjalan_a[0]['a'] - $rst_tahun_berjalan_b[0]['b'];
|
|
|
|
// year min 1
|
|
$sql_year_min_1_a = "SELECT ifnull(SUM(`Nat_SalesTotal`),0) AS a
|
|
FROM `nat_sales`
|
|
WHERE `Nat_SalesYear` = year('$date_now') - 1
|
|
AND Nat_SalesDay <= '$end_date_year_min_1'
|
|
AND Nat_SalesIsActive = 'Y' ";
|
|
|
|
$qry_year_min_1_a = $this->db->query($sql_year_min_1_a, []);
|
|
if (!$qry_year_min_1_a) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_year_min_1_a = $this->db->last_query();
|
|
$rst_year_min_1_a = $qry_year_min_1_a->result_array();
|
|
|
|
$sql_year_min_1_b = "SELECT ifnull(SUM(Nat_PaymentTotal),0) AS b
|
|
FROM nat_payment
|
|
WHERE Nat_PaymentOrderYear = year('$date_now') - 1
|
|
AND Nat_PaymentDay <= '$end_date_year_min_1'
|
|
AND Nat_PaymentIsActive = 'Y' ";
|
|
|
|
$qry_year_min_1_b = $this->db->query($sql_year_min_1_b, []);
|
|
if (!$qry_year_min_1_b) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_year_min_1_b = $this->db->last_query();
|
|
$rst_year_min_1_b = $qry_year_min_1_b->result_array();
|
|
|
|
$rst_piutang_year_min_1 = $rst_year_min_1_a[0]['a'] - $rst_year_min_1_b[0]['b'];
|
|
|
|
// calculate piutang nat total
|
|
$diff_total_piutang = ($rst_piutang_berjalan) -
|
|
($rst_piutang_year_min_1);
|
|
$diff_persen = 0.0;
|
|
$keterangan = "-";
|
|
|
|
$formatted_number = $this->getClassificationNumber($diff_total_piutang);
|
|
|
|
// kalau tahun lalu 0 maka 100%
|
|
if (($rst_piutang_year_min_1) == 0) {
|
|
$diff_persen = 100;
|
|
} else {
|
|
// $persentase = (($tahun_sekarang - $tahun_lalu) / $tahun_lalu) * 100;
|
|
$diff_persen_calculate = ($diff_total_piutang / ($rst_piutang_year_min_1)) * 100;
|
|
$diff_persen = number_format($diff_persen_calculate, 0);
|
|
}
|
|
|
|
// set keterangan & prefix
|
|
if ($rst_piutang_year_min_1 < 1) {
|
|
$keterangan = "-";
|
|
} else {
|
|
if (($rst_piutang_berjalan) > ($rst_piutang_year_min_1)) {
|
|
$keterangan = "up";
|
|
} else {
|
|
if (($rst_piutang_year_min_1) > ($rst_piutang_berjalan)) {
|
|
$keterangan = "down";
|
|
}
|
|
}
|
|
}
|
|
|
|
$total_piutang_fix = "Rp. " . $formatted_number;
|
|
|
|
$result = array(
|
|
"total" => $total_piutang_fix,
|
|
"persen" => $diff_persen . " %",
|
|
"keterangan" => $keterangan
|
|
);
|
|
|
|
if ($debug != "") {
|
|
$result = array(
|
|
"piutang_berjalan" => $rst_piutang_berjalan,
|
|
"piutang_year_min_1" => $rst_piutang_year_min_1,
|
|
"piutang_berjalan_abs" => abs($rst_piutang_berjalan),
|
|
"piutang_year_min_1_abs" => abs($rst_piutang_year_min_1),
|
|
"total_reformat" => $diff_total_piutang,
|
|
"total" => $total_piutang_fix,
|
|
"persen" => $diff_persen . " %",
|
|
"keterangan" => $keterangan,
|
|
"last_qry_tahun_berjalan_a" => $last_qry_tahun_berjalan_a,
|
|
"last_qry_tahun_berjalan_b" => $last_qry_tahun_berjalan_b,
|
|
"last_qry_year_min_1_a" => $last_qry_year_min_1_a,
|
|
"last_qry_year_min_1_b" => $last_qry_year_min_1_b
|
|
);
|
|
|
|
echo "<pre>";
|
|
print_r($result);
|
|
echo "</pre>";
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_total($debug = "", $filter_scope_debug = "", $filter_waktu_debug = "")
|
|
{
|
|
$this->corss();
|
|
try {
|
|
// if (!$this->isLogin) {
|
|
// $this->sys_error("Invalid Token");
|
|
// exit;
|
|
// }
|
|
// $param = $this->sys_input;
|
|
|
|
$filter_scope = "";
|
|
$filter_waktu = "";
|
|
|
|
// untuk debug
|
|
if ($debug != "") {
|
|
// $filter_scope = $filter_scope_debug;
|
|
$filter_scope = ($filter_scope_debug == "0" || $filter_scope_debug == 0) ? "" : $filter_scope_debug;
|
|
$filter_waktu = $filter_waktu_debug;
|
|
}
|
|
|
|
// PAKE POST DARI FLUTTER
|
|
$prm = $this->sys_input;
|
|
if (isset($prm['filter_scope'])) {
|
|
$filter_scope = ($prm["filter_scope"] == "0" || $prm["filter_scope"] == 0) ? "" : $prm["filter_scope"];
|
|
}
|
|
|
|
if (isset($prm['filter_waktu'])) {
|
|
$filter_waktu = $prm["filter_waktu"];
|
|
}
|
|
|
|
$date_now = date('Y-m-d');
|
|
$end_date_year_min_1 = date('Y-m-d', strtotime($date_now . ' -1 year'));
|
|
|
|
$where_tahun_berjalan_a = "`Nat_SalesYear` = year('$date_now')
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
|
|
$where_tahun_berjalan_b = "Nat_PaymentOrderYear = year('$date_now')
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
$where_end_date_year_min_1_a = "`Nat_SalesYear` = year('$date_now') - 1
|
|
AND Nat_SalesDay <= DATE(NOW() - INTERVAL 1 YEAR)
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
|
|
$where_end_date_year_min_1_b = "Nat_PaymentOrderYear = year('$date_now') - 1
|
|
AND Nat_PaymentDay <= DATE(NOW() - INTERVAL 1 YEAR)
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
$join_filter_tahun_berjalan_a = "";
|
|
$join_filter_tahun_berjalan_b = "";
|
|
$join_filter_end_date_year_min_1_a = "";
|
|
$join_filter_end_date_year_min_1_b = "";
|
|
|
|
// filter start
|
|
if ($filter_scope != "") {
|
|
$join_filter_tahun_berjalan_a = "JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND S_RegionalID = $filter_scope";
|
|
|
|
$join_filter_tahun_berjalan_b = "JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND S_RegionalID = $filter_scope";
|
|
|
|
$join_filter_end_date_year_min_1_a = "JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND S_RegionalID = $filter_scope";
|
|
|
|
$join_filter_end_date_year_min_1_b = "JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND S_RegionalID = $filter_scope";
|
|
}
|
|
|
|
if ($filter_waktu != "") {
|
|
if ($filter_waktu != 'YTD') {
|
|
// bukan Year To Date
|
|
if (strpos($filter_waktu, 'Q') !== false || strpos($filter_waktu, 'q') !== false) {
|
|
$quarterResult = $this->getQuarterByFilter($filter_waktu);
|
|
if ($quarterResult != '-') {
|
|
$where_tahun_berjalan_a = "
|
|
Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
";
|
|
|
|
$where_end_date_year_min_1_a = "
|
|
Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
";
|
|
|
|
// $where_tahun_berjalan_b .= " AND Nat_PaymentQuarter = ($quarterResult)";
|
|
// $where_end_date_year_min_1_b .= " AND Nat_PaymentQuarter = ($quarterResult)";
|
|
|
|
$where_tahun_berjalan_b = "
|
|
Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = year('$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
";
|
|
|
|
$where_end_date_year_min_1_b = "
|
|
Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = year('$date_now') - 1
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
";
|
|
}
|
|
}
|
|
} else {
|
|
if ($filter_waktu == "YTD") {
|
|
// jika Year To Date
|
|
// klu berjalan, 1 januari 2023 - 4 oktober 2023 ( date now )
|
|
// klu end date year - 1 , 1 januari 2022 - (date now - 1)
|
|
$berjalan_a = date('Y-01-01');
|
|
|
|
$end_date_a = date('Y-01-01', strtotime('-1 year'));
|
|
$end_date_b = date('Y-m-d', strtotime('-1 year'));
|
|
|
|
// berjalan
|
|
// Nat_SalesDay >= '2023-01-01' AND Nat_SalesDay <= '2023-10-04'
|
|
// Nat_SalesYear` = year('$date_now')
|
|
// AND Nat_SalesDay >= '$berjalan_a' AND Nat_SalesDay <= '$date_now'
|
|
$where_tahun_berjalan_a = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
AND (`Nat_SalesDay` >= '$berjalan_a' AND `Nat_SalesDay` <= '$date_now')";
|
|
|
|
// end date year - 1
|
|
// Nat_SalesDay >= '2022-01-01' AND Nat_SalesDay <= '2022-10-04'
|
|
|
|
$where_end_date_year_min_1_a = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now') - 1
|
|
AND (`Nat_SalesDay` >= '$end_date_a' AND `Nat_SalesDay` <= DATE(NOW() - INTERVAL 1 YEAR))";
|
|
|
|
$where_tahun_berjalan_b = " Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now')
|
|
AND (`Nat_PaymentOrderDay` >= '$berjalan_a' AND `Nat_PaymentOrderDay` <= '$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
AND (`Nat_PaymentDay` >= '$berjalan_a' AND `Nat_PaymentDay` <= '$date_now')
|
|
";
|
|
|
|
$where_end_date_year_min_1_b = " Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now') - 1
|
|
AND (`Nat_PaymentOrderDay` >= '$end_date_a' AND `Nat_PaymentOrderDay` <= DATE(NOW() - INTERVAL 1 YEAR))
|
|
AND `Nat_PaymentYear` = year('$date_now') - 1
|
|
AND (`Nat_PaymentDay` >= '$end_date_a' AND `Nat_PaymentDay` <= DATE(NOW() - INTERVAL 1 YEAR))
|
|
";
|
|
}
|
|
}
|
|
}
|
|
|
|
// tahun berjalan
|
|
$sql_tahun_berjalan_a = "SELECT ifnull(SUM(`Nat_SalesTotal`),0) AS a
|
|
FROM `nat_sales`
|
|
$join_filter_tahun_berjalan_a
|
|
WHERE $where_tahun_berjalan_a";
|
|
|
|
$qry_tahun_berjalan_a = $this->db->query($sql_tahun_berjalan_a, []);
|
|
if (!$qry_tahun_berjalan_a) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_tahun_berjalan_a = $this->db->last_query();
|
|
$rst_tahun_berjalan_a = $qry_tahun_berjalan_a->result_array();
|
|
|
|
$sql_tahun_berjalan_b = "SELECT ifnull(SUM(Nat_PaymentTotal),0) AS b
|
|
FROM nat_payment
|
|
$join_filter_tahun_berjalan_b
|
|
WHERE $where_tahun_berjalan_b ";
|
|
|
|
$qry_tahun_berjalan_b = $this->db->query($sql_tahun_berjalan_b, []);
|
|
if (!$qry_tahun_berjalan_b) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_tahun_berjalan_b = $this->db->last_query();
|
|
$rst_tahun_berjalan_b = $qry_tahun_berjalan_b->result_array();
|
|
|
|
$rst_piutang_berjalan = $rst_tahun_berjalan_a[0]['a'] - $rst_tahun_berjalan_b[0]['b'];
|
|
|
|
// year min 1
|
|
$sql_year_min_1_a = "SELECT ifnull(SUM(`Nat_SalesTotal`),0) AS a
|
|
FROM `nat_sales`
|
|
$join_filter_end_date_year_min_1_a
|
|
WHERE $where_end_date_year_min_1_a ";
|
|
|
|
$qry_year_min_1_a = $this->db->query($sql_year_min_1_a, []);
|
|
if (!$qry_year_min_1_a) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_year_min_1_a = $this->db->last_query();
|
|
$rst_year_min_1_a = $qry_year_min_1_a->result_array();
|
|
|
|
$sql_year_min_1_b = "SELECT ifnull(SUM(Nat_PaymentTotal),0) AS b
|
|
FROM nat_payment
|
|
$join_filter_end_date_year_min_1_b
|
|
WHERE $where_end_date_year_min_1_b ";
|
|
|
|
$qry_year_min_1_b = $this->db->query($sql_year_min_1_b, []);
|
|
if (!$qry_year_min_1_b) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_year_min_1_b = $this->db->last_query();
|
|
$rst_year_min_1_b = $qry_year_min_1_b->result_array();
|
|
|
|
$rst_piutang_year_min_1 = $rst_year_min_1_a[0]['a'] - $rst_year_min_1_b[0]['b'];
|
|
|
|
// calculate piutang nat total
|
|
$diff_total_piutang = ($rst_piutang_berjalan) -
|
|
($rst_piutang_year_min_1);
|
|
$diff_persen = 0.0;
|
|
$keterangan = "-";
|
|
|
|
// $formatted_number = $this->getClassificationNumber($diff_total_piutang);
|
|
$formatted_number = $this->getClassificationNumber($rst_piutang_berjalan);
|
|
|
|
// kalau tahun lalu 0 maka 100%
|
|
if (($rst_piutang_year_min_1) == 0) {
|
|
$diff_persen = 100;
|
|
} else {
|
|
// $persentase = (($tahun_sekarang - $tahun_lalu) / $tahun_lalu) * 100;
|
|
$diff_persen_calculate = ($diff_total_piutang / ($rst_piutang_year_min_1)) * 100;
|
|
$diff_persen = number_format($diff_persen_calculate, 0);
|
|
}
|
|
|
|
$total_piutang_fix = "Rp. " . $formatted_number;
|
|
|
|
$result = array(
|
|
"total" => $total_piutang_fix,
|
|
"persen" => $diff_persen . " %",
|
|
"keterangan" => $keterangan,
|
|
"last_qry_tahun_berjalan_a" => $last_qry_tahun_berjalan_a,
|
|
"last_qry_tahun_berjalan_b" => $last_qry_tahun_berjalan_b,
|
|
"last_qry_year_min_1_a" => $last_qry_year_min_1_a,
|
|
"last_qry_year_min_1_b" => $last_qry_year_min_1_b
|
|
);
|
|
|
|
if ($debug != "") {
|
|
$result = array(
|
|
"piutang_berjalan" => $rst_piutang_berjalan,
|
|
"piutang_year_min_1" => $rst_piutang_year_min_1,
|
|
"piutang_berjalan_abs" => abs($rst_piutang_berjalan),
|
|
"piutang_year_min_1_abs" => abs($rst_piutang_year_min_1),
|
|
"total_reformat" => $total_piutang_fix,
|
|
"total" => $total_piutang_fix,
|
|
"persen" => $diff_persen . " %",
|
|
"keterangan" => $keterangan,
|
|
"last_qry_tahun_berjalan_a" => $last_qry_tahun_berjalan_a,
|
|
"last_qry_tahun_berjalan_b" => $last_qry_tahun_berjalan_b,
|
|
"last_qry_year_min_1_a" => $last_qry_year_min_1_a,
|
|
"last_qry_year_min_1_b" => $last_qry_year_min_1_b
|
|
);
|
|
|
|
echo "<pre>";
|
|
print_r($result);
|
|
echo "</pre>";
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_total_piutang_bar_chart_no_group_oldv1($debug = "", $filter_scope_debug = "", $filter_waktu_debug = "")
|
|
{
|
|
$this->corss();
|
|
// total pendapatan nat_sales
|
|
// *) di tambah dengan tahun - 1
|
|
// di berikan diff dalam persen , jika sebelumnya 0 ( belum ada data ) di kasih 100
|
|
|
|
try {
|
|
// if (!$this->isLogin) {
|
|
// $this->sys_error("Invalid Token");
|
|
// exit;
|
|
// }
|
|
|
|
$date_now = date('Y-m-d');
|
|
|
|
$filter_scope = "";
|
|
$filter_waktu = "";
|
|
$join_filter_tahun_berjalan = "";
|
|
$join_filter_end_date_year_min_1 = "";
|
|
$where_tahun_berjalan = " `Nat_SalesYear` = YEAR('$date_now')
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
$where_year_min_1 = " Nat_PaymentOrderYear = year('$date_now')
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
// untuk debug
|
|
if ($debug != "") {
|
|
// $filter_scope = $filter_scope_debug;
|
|
$filter_scope = ($filter_scope_debug == "0" || $filter_scope_debug == 0) ? "-" : $filter_scope_debug;
|
|
$filter_waktu = $filter_waktu_debug;
|
|
}
|
|
|
|
// PAKE POST DARI FLUTTER
|
|
$prm = $this->sys_input;
|
|
if (isset($prm['filter_scope'])) {
|
|
$filter_scope = ($prm["filter_scope"] == "0" || $prm["filter_scope"] == 0) ? "-" : $prm["filter_scope"];
|
|
}
|
|
|
|
if (isset($prm['filter_waktu'])) {
|
|
$filter_waktu = $prm["filter_waktu"];
|
|
}
|
|
|
|
// filter start
|
|
if ($filter_scope != "-") {
|
|
$join_filter_tahun_berjalan = " AND S_RegionalID = $filter_scope";
|
|
$join_filter_end_date_year_min_1 = " AND S_RegionalID = $filter_scope";
|
|
}
|
|
|
|
if ($filter_waktu != "") {
|
|
if ($filter_waktu != 'YTD') {
|
|
// bukan Year To Date
|
|
|
|
// menentukan apakah filter Quarter ?
|
|
if (strpos($filter_waktu, 'Q') !== false || strpos($filter_waktu, 'q') !== false) {
|
|
$quarterResult = $this->getQuarterByFilter($filter_waktu);
|
|
if ($quarterResult != '-') {
|
|
$where_tahun_berjalan = " Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND `Nat_SalesYear` = YEAR('$date_now')";
|
|
// $where_year_min_1 = " Nat_PaymentIsActive = 'Y'
|
|
// AND Nat_PaymentQuarter = ($quarterResult)
|
|
// AND Nat_PaymentOrderYear = year('$date_now')";
|
|
|
|
$where_year_min_1 = " Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = year('$date_now')";
|
|
}
|
|
}
|
|
} else {
|
|
if ($filter_waktu == "YTD") {
|
|
// jika Year To Date
|
|
// klu berjalan, 1 januari 2023 - 4 oktober 2023 ( date now )
|
|
// klu end date year - 1 , 1 januari 2022 - (date now - 1)
|
|
$berjalan_a = date('Y-01-01');
|
|
|
|
$end_date_a = date('Y-01-01', strtotime('-1 year'));
|
|
$end_date_b = date('Y-m-d', strtotime('-1 year'));
|
|
|
|
// berjalan
|
|
// Nat_SalesDay >= '2023-01-01' AND Nat_SalesDay <= '2023-10-04'
|
|
// Nat_SalesYear` = year('$date_now')
|
|
// AND Nat_SalesDay >= '$berjalan_a' AND Nat_SalesDay <= '$date_now'
|
|
$where_tahun_berjalan = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
AND (`Nat_SalesDay` >= '$berjalan_a' AND `Nat_SalesDay` <= '$date_now')";
|
|
|
|
$where_year_min_1 = " AND Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now')
|
|
AND (`Nat_PaymentOrderDay` >= '$berjalan_a'
|
|
AND `Nat_PaymentOrderDay` <= '$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
AND (`Nat_PaymentDay` >= '$berjalan_a'
|
|
AND `Nat_PaymentDay` <= <= '$date_now')
|
|
";
|
|
}
|
|
}
|
|
}
|
|
|
|
// $param = $this->sys_input;
|
|
|
|
$sql_regional_list = "select S_RegionalID, S_RegionalName
|
|
from s_regional
|
|
where S_RegionalIsActive = 'Y'
|
|
order by S_RegionalName ASC";
|
|
$qry_regional_list = $this->db->query($sql_regional_list);
|
|
if (!$qry_regional_list) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_regional_list = $this->db->last_query();
|
|
$rst_regional_list = $qry_regional_list->result_array();
|
|
|
|
// nat_sales
|
|
$sql_nat_sales = "SELECT ifnull(SUM(`Nat_SalesTotal`), 0) AS a,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM `nat_sales`
|
|
JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_tahun_berjalan
|
|
WHERE $where_tahun_berjalan
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_sales = $this->db->query($sql_nat_sales);
|
|
if (!$qry_nat_sales) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_sales = $this->db->last_query();
|
|
$rst_nat_sales = $qry_nat_sales->result_array();
|
|
|
|
// nat_payment
|
|
$sql_nat_payment = "SELECT ifnull(SUM(Nat_PaymentTotal),0) AS b,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM nat_payment
|
|
JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_end_date_year_min_1
|
|
WHERE $where_year_min_1
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_payment = $this->db->query($sql_nat_payment);
|
|
if (!$qry_nat_payment) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_payment = $this->db->last_query();
|
|
$rst_nat_payment = $qry_nat_payment->result_array();
|
|
|
|
$list_scope = [];
|
|
$value_of_scope = [];
|
|
$result = [];
|
|
$list_color = [];
|
|
$hasil_tmp = [];
|
|
|
|
// klu nat_sales tidak kosong di push
|
|
if (count($rst_nat_sales) > 0) {
|
|
foreach ($rst_nat_sales as $key => $value) {
|
|
$hasil_tmp[] = [
|
|
"alias" => "a",
|
|
"value" => $value['a'],
|
|
"S_RegionalID" => $value['S_RegionalID'],
|
|
"S_RegionalName" => $value['S_RegionalName']
|
|
];
|
|
}
|
|
}
|
|
|
|
// klu nat_payment tidak kosong di push
|
|
if (count($rst_nat_payment) > 0) {
|
|
foreach ($rst_nat_payment as $key => $value) {
|
|
$hasil_tmp[] = [
|
|
"alias" => "b",
|
|
"value" => $value['b'],
|
|
"S_RegionalID" => $value['S_RegionalID'],
|
|
"S_RegionalName" => $value['S_RegionalName']
|
|
];
|
|
}
|
|
}
|
|
|
|
// grup S_RegionalID dari array hasil_tmp
|
|
$grouped = [];
|
|
foreach ($hasil_tmp as $item) {
|
|
$alias = $item["alias"];
|
|
$S_RegionalID = $item["S_RegionalID"];
|
|
$value = $item["value"];
|
|
$S_RegionalName = $item['S_RegionalName'];
|
|
|
|
if (!isset($grouped[$S_RegionalID][$alias])) {
|
|
$grouped[$S_RegionalID][$alias] = [
|
|
"value" => 0,
|
|
"S_RegionalName" => $item["S_RegionalName"],
|
|
];
|
|
}
|
|
|
|
// sum group by berdasarkan alias dan regional
|
|
$grouped[$S_RegionalID][$alias]["value"] += $value;
|
|
}
|
|
|
|
// hitung selisih a - b
|
|
$hasil_baru = [];
|
|
foreach ($grouped as $S_RegionalID => $vx) {
|
|
// klu a & b set
|
|
if (isset($vx["a"]) && isset($vx["b"])) {
|
|
$value = $vx["a"]["value"] - $vx["b"]["value"];
|
|
$hasil_baru[] = [
|
|
"S_RegionalID" => $S_RegionalID,
|
|
"S_RegionalName" => $vx["a"]["S_RegionalName"],
|
|
"value" => $value,
|
|
];
|
|
} else {
|
|
// klu b tidak set
|
|
if (isset($vx['a']) && !isset($vx['b'])) {
|
|
$value = $vx["a"]["value"] - 0;
|
|
$hasil_baru[] = [
|
|
"S_RegionalID" => $S_RegionalID,
|
|
"S_RegionalName" => $vx["a"]["S_RegionalName"],
|
|
"value" => $value,
|
|
];
|
|
} else {
|
|
$value = 0;
|
|
$hasil_baru[] = [
|
|
"S_RegionalID" => $S_RegionalID,
|
|
"S_RegionalName" => $vx["a"]["S_RegionalName"],
|
|
"value" => $value,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
// check kalau sudah ada value di array hasil_baru maka tidak ditambahkan
|
|
// klu blm ada di tambahkan dengan nilai value 0
|
|
if (count($rst_regional_list) > 0) {
|
|
// Buat array asosiatif sementara yang akan digunakan untuk melacak S_RegionalID yang sudah ada
|
|
$existingRegionalIDs = array_column($hasil_baru, 'S_RegionalID');
|
|
|
|
foreach ($rst_regional_list as $key => $value) {
|
|
// Periksa apakah S_RegionalID sudah ada dalam $existingRegionalIDs
|
|
if (!in_array($value['S_RegionalID'], $existingRegionalIDs)) {
|
|
$hasil_baru[] = [
|
|
"value" => 0,
|
|
"S_RegionalID" => $value['S_RegionalID'],
|
|
"S_RegionalName" => $value['S_RegionalName']
|
|
];
|
|
|
|
// Tambahkan S_RegionalID baru ke $existingRegionalIDs
|
|
$existingRegionalIDs[] = $value['S_RegionalID'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ekstrak S_RegionalName ke dalam array terpisah untuk pengurutan
|
|
$S_RegionalName = array_column($hasil_baru, 'S_RegionalName');
|
|
|
|
// Urutkan $hasil_baru berdasarkan S_RegionalName
|
|
array_multisort($S_RegionalName, SORT_ASC, $hasil_baru);
|
|
|
|
$new_regional_list = [];
|
|
if (count($rst_regional_list) > 0) {
|
|
foreach ($rst_regional_list as $key => $value) {
|
|
$new_regional_list[] = $value['S_RegionalName'];
|
|
}
|
|
}
|
|
|
|
$new_list_scope = array_merge($new_regional_list, $list_scope);
|
|
$new_list_scope = array_unique($new_list_scope);
|
|
|
|
// // masukkan ke array value_of_scope yg fix
|
|
if (count($hasil_baru) > 0) {
|
|
foreach ($hasil_baru as $key => $vx) {
|
|
$string_val = $vx['value'];
|
|
$value_of_scope[] = "$string_val";
|
|
// list color
|
|
$list_color[] = $this->list_color_bar_chart($key, count($new_list_scope));
|
|
}
|
|
|
|
// // TESTING COLOR DEFAULT
|
|
// $x1 = [
|
|
// 'Bali Raya',
|
|
// 'Balikpapan Raya',
|
|
// 'Bandung Raya',
|
|
// 'Cirebon Raya',
|
|
// 'Jakarta Raya',
|
|
// 'Madiun Raya',
|
|
// 'Magelang Raya',
|
|
// 'Makassar Raya',
|
|
// 'Manado Raya',
|
|
// 'Medan Raya',
|
|
// 'Padang Raya',
|
|
// 'Palembang Raya',
|
|
// 'Pekanbaru Raya',
|
|
// 'Pontianak Raya',
|
|
// 'Prospek',
|
|
// 'Salatiga Raya',
|
|
// 'Semarang Raya',
|
|
// 'Sima Jember',
|
|
// 'Sima Kediri',
|
|
// 'Sima Malang',
|
|
// 'Surabaya Raya',
|
|
// 'Tasik Raya',
|
|
// 'Tegal Raya',
|
|
// 'Yogyaraya',
|
|
// 'Bali Raya',
|
|
// 'Balikpapan Raya',
|
|
// 'Bandung Raya',
|
|
// 'Cirebon Raya',
|
|
// 'Jakarta Raya',
|
|
// 'Madiun Raya',
|
|
// 'Magelang Raya',
|
|
// 'Makassar Raya',
|
|
// 'Manado Raya',
|
|
// 'Medan Raya',
|
|
// 'Padang Raya',
|
|
// 'Palembang Raya',
|
|
// 'Pekanbaru Raya',
|
|
// 'Pontianak Raya',
|
|
// 'Prospek',
|
|
// 'Salatiga Raya',
|
|
// 'Semarang Raya',
|
|
// 'Sima Jember',
|
|
// 'Sima Kediri',
|
|
// 'Sima Malang',
|
|
// 'Surabaya Raya',
|
|
// 'Tasik Raya',
|
|
// 'Tegal Raya',
|
|
// 'Yogyaraya',
|
|
// ];
|
|
// for ($i = 0; $i < count($x1); $i++) {
|
|
// // $value_of_scope[] = $rst_tahun_berjalan[$i]['total_sales_tahun_berjalan'];
|
|
// // $list_scope[] = $rst_tahun_berjalan[$i]['S_RegionalName'];
|
|
// $list_color[] = $this->list_color_bar_chart($i, count($x1));
|
|
// }
|
|
}
|
|
|
|
$result = array(
|
|
"value_of_scope" => $value_of_scope,
|
|
"list_scope" => $new_list_scope,
|
|
"bar_chart_color" => $list_color,
|
|
"last_qry_nat_sales" => $last_qry_nat_sales,
|
|
"last_qry_nat_payment" => $last_qry_nat_payment,
|
|
"last_qry_regional_list" => $last_qry_regional_list,
|
|
);
|
|
|
|
// $result = array(
|
|
// "value_of_scope" => [],
|
|
// "list_scope" => [],
|
|
// "bar_chart_color" => [],
|
|
// );
|
|
|
|
if ($debug != "") {
|
|
$result = array(
|
|
"rst_nat_sales" => $rst_nat_sales,
|
|
"rst_nat_payment" => $rst_nat_payment,
|
|
"hasil_tmp" => $hasil_tmp,
|
|
"hasil_baru" => $hasil_baru,
|
|
"value_of_scope" => $value_of_scope,
|
|
"list_scope" => $new_list_scope,
|
|
"bar_chart_color" => $list_color,
|
|
);
|
|
|
|
echo "<pre>";
|
|
print_r($result);
|
|
echo "</pre>";
|
|
exit;
|
|
}
|
|
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_total_piutang_bar_chart_no_group($debug = "", $filter_scope_debug = "", $filter_waktu_debug = "")
|
|
{
|
|
$this->corss();
|
|
|
|
try {
|
|
$date_now = date('Y-m-d');
|
|
$date_now = date('Y-m-d');
|
|
$filter_waktu = "";
|
|
$value_of_scope = [];
|
|
$list_color = [];
|
|
$result = [];
|
|
$list_scope = [];
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
$regID = "0";
|
|
|
|
if (isset($prm['filter_waktu'])) {
|
|
$filter_waktu = $prm["filter_waktu"];
|
|
}
|
|
|
|
if (isset($prm['filter_scope'])) {
|
|
$regID = ($prm["filter_scope"] == "0" || $prm["filter_scope"] == 0) ? "-" : $prm["filter_scope"];
|
|
}
|
|
|
|
|
|
$sql_regional_list = "select S_RegionalID, S_RegionalName
|
|
from s_regional
|
|
where S_RegionalIsActive = 'Y'
|
|
order by S_RegionalName ASC";
|
|
$qry_regional_list = $this->db->query($sql_regional_list);
|
|
if (!$qry_regional_list) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_regional_list = $this->db->last_query();
|
|
$rst_regional_list = $qry_regional_list->result_array();
|
|
|
|
if (count($rst_regional_list) > 0) {
|
|
foreach ($rst_regional_list as $key => $vx) {
|
|
$list_scope[] = $vx['S_RegionalName'];
|
|
}
|
|
}
|
|
|
|
$sql_all_scope_default_value = "SELECT S_RegionalName, 0 as nat_sales_berjalan,
|
|
S_RegionalID,
|
|
0 as nat_payment_berjalan,
|
|
0 as piutang_berjalan
|
|
FROM s_regional
|
|
WHERE S_RegionalIsActive = 'Y'";
|
|
|
|
$qry_all_scope_default_value = $this->db->query($sql_all_scope_default_value);
|
|
if (!$qry_all_scope_default_value) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
|
|
$last_qry_all_scope_default_value = $this->db->last_query();
|
|
$rst_piutang_all_scope_default_value = $qry_all_scope_default_value->result_array();
|
|
|
|
if ($filter_waktu != "") {
|
|
if ($filter_waktu != 'YTD') {
|
|
// bukan Year To Date
|
|
|
|
if (strpos($filter_waktu, 'Q') !== false || strpos($filter_waktu, 'q') !== false) {
|
|
$quarterResult = $this->getQuarterByFilter($filter_waktu);
|
|
if ($quarterResult != '-') {
|
|
$where_tahun_berjalan = "
|
|
AND Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND `Nat_SalesYear` = YEAR('$date_now')
|
|
";
|
|
|
|
|
|
$where_year_min_1 = "
|
|
AND Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = year('$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
";
|
|
$sql_piutang_berjalan = "
|
|
SELECT a.S_RegionalName, a.nat_sales_berjalan,
|
|
b.S_RegionalID,
|
|
b.nat_payment_berjalan,
|
|
(a.nat_sales_berjalan - ifnull(b.nat_payment_berjalan,0) ) as piutang_berjalan
|
|
from (
|
|
SELECT
|
|
ifnull(
|
|
SUM(`Nat_SalesTotal`),
|
|
0
|
|
) AS nat_sales_berjalan,
|
|
S_RegionalID as S_RegionalID,
|
|
S_RegionalName as S_RegionalName
|
|
FROM
|
|
`nat_sales`
|
|
JOIN m_branch ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
WHERE
|
|
Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND `Nat_SalesYear` = YEAR('$date_now')
|
|
GROUP BY
|
|
S_RegionalID
|
|
ORDER BY
|
|
S_RegionalName ASC
|
|
) as a
|
|
LEFT JOIN (
|
|
SELECT
|
|
ifnull(
|
|
SUM(Nat_PaymentTotal),
|
|
0
|
|
) AS nat_payment_berjalan,
|
|
S_RegionalID as S_RegionalID,
|
|
S_RegionalName as S_RegionalName
|
|
FROM
|
|
nat_payment
|
|
JOIN m_branch ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
WHERE
|
|
Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = year('$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
GROUP BY
|
|
S_RegionalID
|
|
ORDER BY
|
|
S_RegionalName ASC
|
|
) as b
|
|
ON a.S_RegionalID = b.S_RegionalID
|
|
";
|
|
|
|
$qry_piutang_berjalan = $this->db->query($sql_piutang_berjalan);
|
|
if (!$qry_piutang_berjalan) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_piutang_berjalan = $this->db->last_query();
|
|
$rst_piutang_berjalan = $qry_piutang_berjalan->result_array();
|
|
|
|
if (count($rst_piutang_berjalan) > 0) {
|
|
// foreach ($rst_piutang_all_scope_default_value as &$value) {
|
|
// $SRegionalID = $value['S_RegionalID'];
|
|
|
|
// $foundInBerjalan = false;
|
|
// foreach ($rst_piutang_berjalan as $berjalan) {
|
|
// if ($berjalan['S_RegionalID'] == $SRegionalID) {
|
|
// $foundInBerjalan = true;
|
|
// $value['piutang_berjalan'] = $berjalan['piutang_berjalan'];
|
|
// break;
|
|
// }
|
|
// }
|
|
|
|
// if (!$foundInBerjalan) {
|
|
// $value['piutang_berjalan'] = 0;
|
|
// }
|
|
// }
|
|
|
|
foreach ($rst_piutang_all_scope_default_value as $key => $value) {
|
|
$SRegionalID = $value['S_RegionalID'];
|
|
|
|
$foundInBerjalan = false;
|
|
foreach ($rst_piutang_berjalan as $berjalan) {
|
|
if ($berjalan['S_RegionalID'] == $SRegionalID) {
|
|
$foundInBerjalan = true;
|
|
$rst_piutang_all_scope_default_value[$key]['piutang_berjalan'] = $berjalan['piutang_berjalan'];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$foundInBerjalan) {
|
|
$rst_piutang_all_scope_default_value[$key]['piutang_berjalan'] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
$S_RegionalName = array_column($rst_piutang_all_scope_default_value, 'S_RegionalName');
|
|
|
|
// Urutkan $hasil_baru berdasarkan S_RegionalName
|
|
array_multisort($S_RegionalName, SORT_ASC, $rst_piutang_all_scope_default_value);
|
|
|
|
if (count($rst_piutang_all_scope_default_value) > 0) {
|
|
foreach ($rst_piutang_all_scope_default_value as $key => $vx) {
|
|
$string_val = $vx['piutang_berjalan'];
|
|
$value_of_scope[] = "$string_val";
|
|
// list color
|
|
$list_color[] = $this->list_color_bar_chart($key, count($list_scope));
|
|
}
|
|
}
|
|
|
|
$result = array(
|
|
"value_of_scope" => $value_of_scope,
|
|
"list_scope" => $list_scope,
|
|
"bar_chart_color" => $list_color,
|
|
"last_qry_piutang_berjalan_Q1" => $last_qry_piutang_berjalan,
|
|
);
|
|
|
|
$this->sys_ok($result);
|
|
}
|
|
}
|
|
} else {
|
|
if ($filter_waktu == "YTD") {
|
|
// jika Year To Date
|
|
// klu berjalan, 1 januari 2023 - 4 oktober 2023 ( date now )
|
|
// klu end date year - 1 , 1 januari 2022 - (date now - 1)
|
|
$berjalan_a = date('Y-01-01');
|
|
|
|
$end_date_a = date('Y-01-01', strtotime('-1 year'));
|
|
$end_date_b = date('Y-m-d', strtotime('-1 year'));
|
|
|
|
// berjalan
|
|
// Nat_SalesDay >= '2023-01-01' AND Nat_SalesDay <= '2023-10-04'
|
|
// Nat_SalesYear` = year('$date_now')
|
|
// AND Nat_SalesDay >= '$berjalan_a' AND Nat_SalesDay <= '$date_now'
|
|
// $where_tahun_berjalan = " AND Nat_SalesIsActive = 'Y'
|
|
// AND `Nat_SalesYear` = year('$date_now')
|
|
// AND (`Nat_SalesDay` >= '$berjalan_a'
|
|
// AND `Nat_SalesDay` <= '$date_now')
|
|
// ";
|
|
|
|
// $where_year_min_1 = " AND Nat_PaymentIsActive = 'Y'
|
|
// AND `Nat_PaymentOrderYear` = year('$date_now')
|
|
// AND (`Nat_PaymentOrderDay` >= '$berjalan_a'
|
|
// AND `Nat_PaymentOrderDay` <= '$date_now')
|
|
// AND `Nat_PaymentYear` = year('$date_now')
|
|
// AND (`Nat_PaymentDay` >= '$berjalan_a'
|
|
// AND `Nat_PaymentDay` <= '$date_now')
|
|
// ";
|
|
|
|
$sql_piutang_berjalan = "
|
|
SELECT a.S_RegionalName, a.nat_sales_berjalan,
|
|
b.S_RegionalID,
|
|
b.nat_payment_berjalan,
|
|
(a.nat_sales_berjalan - b.nat_payment_berjalan) as piutang_berjalan
|
|
from (
|
|
SELECT
|
|
ifnull(
|
|
SUM(`Nat_SalesTotal`),
|
|
0
|
|
) AS nat_sales_berjalan,
|
|
S_RegionalID as S_RegionalID,
|
|
S_RegionalName as S_RegionalName
|
|
FROM
|
|
`nat_sales`
|
|
JOIN m_branch ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
WHERE
|
|
Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
AND (
|
|
`Nat_SalesDay` >= '$berjalan_a'
|
|
AND `Nat_SalesDay` <= '$date_now'
|
|
)
|
|
GROUP BY
|
|
S_RegionalID
|
|
ORDER BY
|
|
S_RegionalName ASC
|
|
) as a
|
|
LEFT JOIN (
|
|
SELECT
|
|
ifnull(
|
|
SUM(Nat_PaymentTotal),
|
|
0
|
|
) AS nat_payment_berjalan,
|
|
S_RegionalID as S_RegionalID,
|
|
S_RegionalName as S_RegionalName
|
|
FROM
|
|
nat_payment
|
|
JOIN m_branch ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
WHERE
|
|
Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now')
|
|
AND (
|
|
`Nat_PaymentOrderDay` >= '$berjalan_a'
|
|
AND `Nat_PaymentOrderDay` <= '$date_now'
|
|
)
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
AND (
|
|
`Nat_PaymentDay` >= '$berjalan_a'
|
|
AND `Nat_PaymentDay` <= '$date_now'
|
|
)
|
|
GROUP BY
|
|
S_RegionalID
|
|
ORDER BY
|
|
S_RegionalName ASC
|
|
) as b
|
|
ON a.S_RegionalID = b.S_RegionalID
|
|
";
|
|
|
|
$qry_piutang_berjalan = $this->db->query($sql_piutang_berjalan);
|
|
if (!$qry_piutang_berjalan) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_piutang_berjalan = $this->db->last_query();
|
|
$rst_piutang_berjalan = $qry_piutang_berjalan->result_array();
|
|
|
|
// if(count($rst_piutang_berjalan) > 0 ){
|
|
// foreach ($rst_piutang_berjalan as $key => $vx) {
|
|
// $string_val = $vx['piutang_berjalan'];
|
|
// $value_of_scope[] = "$string_val";
|
|
// // list color
|
|
// $list_color[] = $this->list_color_bar_chart($key, count($list_scope));
|
|
// }
|
|
// }
|
|
|
|
if (count($rst_piutang_berjalan) > 0) {
|
|
$foundInBerjalan = false;
|
|
|
|
// foreach ($rst_piutang_all_scope_default_value as &$value) {
|
|
// $SRegionalID = $value['S_RegionalID'];
|
|
|
|
// foreach ($rst_piutang_berjalan as $berjalan) {
|
|
// if ($berjalan['S_RegionalID'] == $SRegionalID) {
|
|
// $foundInBerjalan = true;
|
|
// $value['piutang_berjalan'] = $berjalan['piutang_berjalan'];
|
|
// break;
|
|
// }
|
|
// }
|
|
|
|
// if (!$foundInBerjalan) {
|
|
// $value['piutang_berjalan'] = 0;
|
|
// }
|
|
// }
|
|
|
|
foreach ($rst_piutang_all_scope_default_value as $key => $value) {
|
|
$SRegionalID = $value['S_RegionalID'];
|
|
|
|
$foundInBerjalan = false;
|
|
foreach ($rst_piutang_berjalan as $berjalan) {
|
|
if ($berjalan['S_RegionalID'] == $SRegionalID) {
|
|
$foundInBerjalan = true;
|
|
$rst_piutang_all_scope_default_value[$key]['piutang_berjalan'] = $berjalan['piutang_berjalan'];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!$foundInBerjalan) {
|
|
$rst_piutang_all_scope_default_value[$key]['piutang_berjalan'] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
$S_RegionalName = array_column($rst_piutang_all_scope_default_value, 'S_RegionalName');
|
|
|
|
// Urutkan $hasil_baru berdasarkan S_RegionalName
|
|
array_multisort($S_RegionalName, SORT_ASC, $rst_piutang_all_scope_default_value);
|
|
|
|
if (count($rst_piutang_all_scope_default_value) > 0) {
|
|
foreach ($rst_piutang_all_scope_default_value as $key => $vx) {
|
|
$string_val = $vx['piutang_berjalan'];
|
|
$value_of_scope[] = "$string_val";
|
|
// list color
|
|
$list_color[] = $this->list_color_bar_chart($key, count($list_scope));
|
|
}
|
|
}
|
|
|
|
$result = array(
|
|
"value_of_scope" => $value_of_scope,
|
|
"list_scope" => $list_scope,
|
|
"bar_chart_color" => $list_color,
|
|
"last_qry_piutang_berjalan_YTD" => $last_qry_piutang_berjalan,
|
|
"rst_piutang_all_scope_default_value" => $rst_piutang_all_scope_default_value
|
|
);
|
|
|
|
$this->sys_ok($result);
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_total_piutang_bar_chart_no_group_quarter($filter_scope_prm, $filter_waktu_prm)
|
|
{
|
|
$this->corss();
|
|
// total pendapatan nat_sales
|
|
// *) di tambah dengan tahun - 1
|
|
// di berikan diff dalam persen , jika sebelumnya 0 ( belum ada data ) di kasih 100
|
|
|
|
try {
|
|
// if (!$this->isLogin) {
|
|
// $this->sys_error("Invalid Token");
|
|
// exit;
|
|
// }
|
|
|
|
$date_now = date('Y-m-d');
|
|
|
|
$filter_scope = $filter_scope_prm;
|
|
$filter_waktu = $filter_waktu_prm;
|
|
$join_filter_tahun_berjalan = "";
|
|
$join_filter_end_date_year_min_1 = "";
|
|
$where_tahun_berjalan = " `Nat_SalesYear` = YEAR('$date_now')
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
$where_year_min_1 = " Nat_PaymentOrderYear = year('$date_now')
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
// PAKE POST DARI FLUTTER
|
|
// $prm = $this->sys_input;
|
|
// if (isset($prm['filter_scope'])) {
|
|
// $filter_scope = ($prm["filter_scope"] == "0" || $prm["filter_scope"] == 0) ? "-" : $prm["filter_scope"];
|
|
// }
|
|
|
|
// if (isset($prm['filter_waktu'])) {
|
|
// $filter_waktu = $prm["filter_waktu"];
|
|
// }
|
|
|
|
// filter start
|
|
if ($filter_scope != "-") {
|
|
$join_filter_tahun_berjalan = " AND S_RegionalID = $filter_scope";
|
|
$join_filter_end_date_year_min_1 = " AND S_RegionalID = $filter_scope";
|
|
}
|
|
|
|
if (strpos($filter_waktu, 'Q') !== false || strpos($filter_waktu, 'q') !== false) {
|
|
$quarterResult = $this->getQuarterByFilter($filter_waktu);
|
|
if ($quarterResult != '-') {
|
|
$where_tahun_berjalan = " Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND `Nat_SalesYear` = YEAR('$date_now')";
|
|
|
|
$where_year_min_1 = " Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = year('$date_now')";
|
|
}
|
|
}
|
|
|
|
// $param = $this->sys_input;
|
|
|
|
$sql_regional_list = "select S_RegionalID, S_RegionalName
|
|
from s_regional
|
|
where S_RegionalIsActive = 'Y'
|
|
order by S_RegionalName ASC";
|
|
$qry_regional_list = $this->db->query($sql_regional_list);
|
|
if (!$qry_regional_list) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_regional_list = $this->db->last_query();
|
|
$rst_regional_list = $qry_regional_list->result_array();
|
|
|
|
// nat_sales
|
|
$sql_nat_sales = "SELECT ifnull(SUM(`Nat_SalesTotal`), 0) AS a,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM `nat_sales`
|
|
JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_tahun_berjalan
|
|
WHERE $where_tahun_berjalan
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_sales = $this->db->query($sql_nat_sales);
|
|
if (!$qry_nat_sales) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_sales = $this->db->last_query();
|
|
$rst_nat_sales = $qry_nat_sales->result_array();
|
|
|
|
// nat_payment
|
|
$sql_nat_payment = "SELECT ifnull(SUM(Nat_PaymentTotal),0) AS b,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM nat_payment
|
|
JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_end_date_year_min_1
|
|
WHERE $where_year_min_1
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_payment = $this->db->query($sql_nat_payment);
|
|
if (!$qry_nat_payment) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_payment = $this->db->last_query();
|
|
$rst_nat_payment = $qry_nat_payment->result_array();
|
|
|
|
$list_scope = [];
|
|
$value_of_scope = [];
|
|
$result = [];
|
|
$list_color = [];
|
|
$hasil_tmp = [];
|
|
|
|
// klu nat_sales tidak kosong di push
|
|
if (count($rst_nat_sales) > 0) {
|
|
foreach ($rst_nat_sales as $key => $value) {
|
|
$hasil_tmp[] = [
|
|
"alias" => "a",
|
|
"value" => $value['a'],
|
|
"S_RegionalID" => $value['S_RegionalID'],
|
|
"S_RegionalName" => $value['S_RegionalName']
|
|
];
|
|
}
|
|
}
|
|
|
|
// klu nat_payment tidak kosong di push
|
|
if (count($rst_nat_payment) > 0) {
|
|
foreach ($rst_nat_payment as $key => $value) {
|
|
$hasil_tmp[] = [
|
|
"alias" => "b",
|
|
"value" => $value['b'],
|
|
"S_RegionalID" => $value['S_RegionalID'],
|
|
"S_RegionalName" => $value['S_RegionalName']
|
|
];
|
|
}
|
|
}
|
|
|
|
// grup S_RegionalID dari array hasil_tmp
|
|
$grouped = [];
|
|
foreach ($hasil_tmp as $item) {
|
|
$alias = $item["alias"];
|
|
$S_RegionalID = $item["S_RegionalID"];
|
|
$value = $item["value"];
|
|
$S_RegionalName = $item['S_RegionalName'];
|
|
|
|
if (!isset($grouped[$S_RegionalID][$alias])) {
|
|
$grouped[$S_RegionalID][$alias] = [
|
|
"value" => 0,
|
|
"S_RegionalName" => $item["S_RegionalName"],
|
|
];
|
|
}
|
|
|
|
// sum group by berdasarkan alias dan regional
|
|
$grouped[$S_RegionalID][$alias]["value"] += $value;
|
|
}
|
|
|
|
// hitung selisih a - b
|
|
$hasil_baru = [];
|
|
foreach ($grouped as $S_RegionalID => $vx) {
|
|
// klu a & b set
|
|
if (isset($vx["a"]) && isset($vx["b"])) {
|
|
$value = $vx["a"]["value"] - $vx["b"]["value"];
|
|
$hasil_baru[] = [
|
|
"S_RegionalID" => $S_RegionalID,
|
|
"S_RegionalName" => $vx["a"]["S_RegionalName"],
|
|
"value" => $value,
|
|
];
|
|
} else {
|
|
// klu b tidak set
|
|
if (isset($vx['a']) && !isset($vx['b'])) {
|
|
$value = $vx["a"]["value"] - 0;
|
|
$hasil_baru[] = [
|
|
"S_RegionalID" => $S_RegionalID,
|
|
"S_RegionalName" => $vx["a"]["S_RegionalName"],
|
|
"value" => $value,
|
|
];
|
|
} else {
|
|
$value = 0;
|
|
$hasil_baru[] = [
|
|
"S_RegionalID" => $S_RegionalID,
|
|
"S_RegionalName" => $vx["a"]["S_RegionalName"],
|
|
"value" => $value,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
// check kalau sudah ada value di array hasil_baru maka tidak ditambahkan
|
|
// klu blm ada di tambahkan dengan nilai value 0
|
|
if (count($rst_regional_list) > 0) {
|
|
// Buat array asosiatif sementara yang akan digunakan untuk melacak S_RegionalID yang sudah ada
|
|
$existingRegionalIDs = array_column($hasil_baru, 'S_RegionalID');
|
|
|
|
foreach ($rst_regional_list as $key => $value) {
|
|
// Periksa apakah S_RegionalID sudah ada dalam $existingRegionalIDs
|
|
if (!in_array($value['S_RegionalID'], $existingRegionalIDs)) {
|
|
$hasil_baru[] = [
|
|
"value" => 0,
|
|
"S_RegionalID" => $value['S_RegionalID'],
|
|
"S_RegionalName" => $value['S_RegionalName']
|
|
];
|
|
|
|
// Tambahkan S_RegionalID baru ke $existingRegionalIDs
|
|
$existingRegionalIDs[] = $value['S_RegionalID'];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Ekstrak S_RegionalName ke dalam array terpisah untuk pengurutan
|
|
$S_RegionalName = array_column($hasil_baru, 'S_RegionalName');
|
|
|
|
// Urutkan $hasil_baru berdasarkan S_RegionalName
|
|
array_multisort($S_RegionalName, SORT_ASC, $hasil_baru);
|
|
|
|
$new_regional_list = [];
|
|
if (count($rst_regional_list) > 0) {
|
|
foreach ($rst_regional_list as $key => $value) {
|
|
$new_regional_list[] = $value['S_RegionalName'];
|
|
}
|
|
}
|
|
|
|
$new_list_scope = array_merge($new_regional_list, $list_scope);
|
|
$new_list_scope = array_unique($new_list_scope);
|
|
|
|
// // masukkan ke array value_of_scope yg fix
|
|
if (count($hasil_baru) > 0) {
|
|
foreach ($hasil_baru as $key => $vx) {
|
|
$string_val = $vx['value'];
|
|
$value_of_scope[] = "$string_val";
|
|
// list color
|
|
$list_color[] = $this->list_color_bar_chart($key, count($new_list_scope));
|
|
}
|
|
}
|
|
|
|
$result = array(
|
|
"value_of_scope" => $value_of_scope,
|
|
"list_scope" => $new_list_scope,
|
|
"bar_chart_color" => $list_color,
|
|
"last_qry_nat_sales" => $last_qry_nat_sales,
|
|
"last_qry_nat_payment" => $last_qry_nat_payment,
|
|
"last_qry_regional_list" => $last_qry_regional_list,
|
|
);
|
|
|
|
return $this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
return $this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_total_piutang_bar_chart_no_group_x($debug = "", $filter_scope_debug = "", $filter_waktu_debug = "")
|
|
{
|
|
$this->corss();
|
|
// total pendapatan nat_sales
|
|
// *) di tambah dengan tahun - 1
|
|
// di berikan diff dalam persen , jika sebelumnya 0 ( belum ada data ) di kasih 100
|
|
|
|
try {
|
|
// if (!$this->isLogin) {
|
|
// $this->sys_error("Invalid Token");
|
|
// exit;
|
|
// }
|
|
|
|
$date_now = date('Y-m-d');
|
|
|
|
$filter_scope = "";
|
|
$filter_waktu = "";
|
|
$join_filter_tahun_berjalan = "";
|
|
$join_filter_end_date_year_min_1 = "";
|
|
|
|
$where_tahun_berjalan_a = "`Nat_SalesYear` = year('$date_now')
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
|
|
$where_tahun_berjalan_b = "Nat_PaymentOrderYear = year('$date_now')
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
$where_end_date_year_min_1_a = "`Nat_SalesYear` = year('$date_now') - 1
|
|
AND Nat_SalesDay <= DATE(NOW() - INTERVAL 1 YEAR)
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
|
|
$where_end_date_year_min_1_b = "Nat_PaymentOrderYear = year('$date_now') - 1
|
|
AND Nat_PaymentDay <= DATE(NOW() - INTERVAL 1 YEAR)
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
// untuk debug
|
|
if ($debug != "") {
|
|
// $filter_scope = $filter_scope_debug;
|
|
$filter_scope = ($filter_scope_debug == "0" || $filter_scope_debug == 0) ? "-" : $filter_scope_debug;
|
|
$filter_waktu = $filter_waktu_debug;
|
|
}
|
|
|
|
// PAKE POST DARI FLUTTER
|
|
$prm = $this->sys_input;
|
|
if (isset($prm['filter_scope'])) {
|
|
$filter_scope = ($prm["filter_scope"] == "0" || $prm["filter_scope"] == 0) ? "-" : $prm["filter_scope"];
|
|
}
|
|
|
|
if (isset($prm['filter_waktu'])) {
|
|
$filter_waktu = $prm["filter_waktu"];
|
|
}
|
|
|
|
// filter start
|
|
if ($filter_scope != "-") {
|
|
$join_filter_tahun_berjalan = " AND S_RegionalID = $filter_scope";
|
|
$join_filter_end_date_year_min_1 = " AND S_RegionalID = $filter_scope";
|
|
}
|
|
|
|
if ($filter_waktu != "") {
|
|
if ($filter_waktu != 'YTD') {
|
|
// bukan Year To Date
|
|
|
|
// menentukan apakah filter Quarter ?
|
|
if (strpos($filter_waktu, 'Q') !== false || strpos($filter_waktu, 'q') !== false) {
|
|
$quarterResult = $this->getQuarterByFilter($filter_waktu);
|
|
if ($quarterResult != '-') {
|
|
|
|
// $where_tahun_berjalan = " Nat_SalesIsActive = 'Y'
|
|
// AND Nat_SalesQuarter = ($quarterResult)
|
|
// AND `Nat_SalesYear` = YEAR('$date_now')";
|
|
|
|
// $where_year_min_1 = " Nat_PaymentIsActive = 'Y'
|
|
// AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
// AND Nat_PaymentOrderYear = year('$date_now')";
|
|
|
|
// nat_sales berjalan
|
|
$where_tahun_berjalan_a = " Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND Nat_SalesYear = YEAR('$date_now')";
|
|
|
|
// nat_sales h-1
|
|
$where_end_date_year_min_1_a = " Nat_SalesIsActive = 'Y'
|
|
AND Nat_SalesQuarter = ($quarterResult)
|
|
AND Nat_SalesYear = YEAR('$date_now') - 1";
|
|
|
|
// nat_payment berjalan
|
|
$where_tahun_berjalan_b = " Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = YEAR('$date_now')";
|
|
|
|
// nat_payment h-1
|
|
$where_end_date_year_min_1_b = " Nat_PaymentIsActive = 'Y'
|
|
AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
AND Nat_PaymentOrderYear = YEAR('$date_now') - 1";
|
|
}
|
|
}
|
|
} else {
|
|
if ($filter_waktu == "YTD") {
|
|
// jika Year To Date
|
|
// klu berjalan, 1 januari 2023 - 4 oktober 2023 ( date now )
|
|
// klu end date year - 1 , 1 januari 2022 - (date now - 1)
|
|
$berjalan_a = date('Y-01-01');
|
|
|
|
$end_date_a = date('Y-01-01', strtotime('-1 year'));
|
|
$end_date_b = date('Y-m-d', strtotime('-1 year'));
|
|
|
|
// nat_sales berjalan
|
|
$where_tahun_berjalan_a = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
AND (`Nat_SalesDay` >= '$berjalan_a' AND `Nat_SalesDay` <= '$date_now')";
|
|
|
|
$where_end_date_year_min_1_a = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now') - 1
|
|
AND (`Nat_SalesDay` >= '$end_date_a'
|
|
AND `Nat_SalesDay` <= DATE(NOW() - INTERVAL 1 YEAR))";
|
|
|
|
// nat_payment berjalan
|
|
$where_tahun_berjalan_b = " Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now')
|
|
AND (`Nat_PaymentOrderDay` >= '$berjalan_a' AND `Nat_PaymentOrderDay` <= '$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
AND (`Nat_PaymentDay` >= '$berjalan_a' AND `Nat_PaymentDay` <= '$date_now')
|
|
";
|
|
|
|
$where_end_date_year_min_1_b = " Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now') - 1
|
|
AND (`Nat_PaymentOrderDay` >= '$end_date_a' AND `Nat_PaymentOrderDay` <= DATE(NOW() - INTERVAL 1 YEAR))
|
|
AND `Nat_PaymentYear` = year('$date_now') - 1
|
|
AND (`Nat_PaymentDay` >= '$end_date_a' AND `Nat_PaymentDay` <= DATE(NOW() - INTERVAL 1 YEAR))
|
|
";
|
|
}
|
|
}
|
|
}
|
|
|
|
// $param = $this->sys_input;
|
|
|
|
$sql_regional_list = "select S_RegionalID, S_RegionalName
|
|
from s_regional
|
|
where S_RegionalIsActive = 'Y'
|
|
order by S_RegionalName ASC";
|
|
$qry_regional_list = $this->db->query($sql_regional_list);
|
|
if (!$qry_regional_list) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_regional_list = $this->db->last_query();
|
|
$rst_regional_list = $qry_regional_list->result_array();
|
|
|
|
// TAHUN BERJALAN
|
|
|
|
// nat_sales berjalan
|
|
$sql_nat_sales_tahun_berjalan = "SELECT
|
|
ifnull(SUM(`Nat_SalesTotal`), 0) AS nat_sales_berjalan,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM `nat_sales`
|
|
JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_tahun_berjalan
|
|
WHERE $where_tahun_berjalan_a
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_sales_tahun_berjalan = $this->db->query($sql_nat_sales_tahun_berjalan);
|
|
if (!$qry_nat_sales_tahun_berjalan) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_sales_tahun_berjalan = $this->db->last_query();
|
|
$rst_nat_sales_tahun_berjalan = $qry_nat_sales_tahun_berjalan->result_array();
|
|
|
|
// nat_payment berjalan
|
|
$sql_nat_payment_tahun_berjalan = "SELECT
|
|
ifnull(SUM(Nat_PaymentTotal),0) AS nat_payment_berjalan,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM nat_payment
|
|
JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_end_date_year_min_1
|
|
WHERE $where_tahun_berjalan_b
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_payment_tahun_berjalan = $this->db->query($sql_nat_payment_tahun_berjalan);
|
|
if (!$qry_nat_payment_tahun_berjalan) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_payment_tahun_berjalan = $this->db->last_query();
|
|
$rst_nat_payment_tahun_berjalan = $qry_nat_payment_tahun_berjalan->result_array();
|
|
|
|
// YEAR MIN 1
|
|
$sql_nat_sales_year_min_1 = "SELECT
|
|
ifnull(SUM(`Nat_SalesTotal`), 0) AS nat_sales_h_min_1,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM `nat_sales`
|
|
JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_tahun_berjalan
|
|
WHERE $where_end_date_year_min_1_a
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_sales_year_min_1 = $this->db->query($sql_nat_sales_year_min_1);
|
|
if (!$qry_nat_sales_year_min_1) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_sales_year_min_1 = $this->db->last_query();
|
|
$rst_nat_sales_year_min_1 = $qry_nat_sales_year_min_1->result_array();
|
|
|
|
$sql_nat_payment_year_min_1 = "SELECT
|
|
ifnull(SUM(Nat_PaymentTotal),0) AS nat_payment_h_min_1,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM nat_payment
|
|
JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_end_date_year_min_1
|
|
WHERE $where_end_date_year_min_1_b
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_payment_year_min_1 = $this->db->query($sql_nat_payment_year_min_1);
|
|
if (!$qry_nat_payment_year_min_1) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_payment_year_min_1 = $this->db->last_query();
|
|
$rst_nat_payment_year_min_1 = $qry_nat_payment_year_min_1->result_array();
|
|
|
|
// HITUNG SELISIH BERJALAN
|
|
$hasil_tmp_berjalan = [];
|
|
$list_scope = [];
|
|
$value_of_scope_berjalan = [];
|
|
$value_of_scope_h_min_1 = [];
|
|
$value_of_scope = [];
|
|
$result = [];
|
|
|
|
// if(count($rst_nat_sales_tahun_berjalan) > 0) {
|
|
// foreach ($rst_nat_sales_tahun_berjalan as $key => $value) {
|
|
// $hasil_tmp_berjalan[] = [
|
|
// "alias" => "nat_sales_berjalan",
|
|
// "value" => $value['nat_sales_berjalan'],
|
|
// "S_RegionalID" => $value['S_RegionalID'],
|
|
// "S_RegionalName" => $value['S_RegionalName']
|
|
// ];
|
|
// }
|
|
// }
|
|
|
|
// if(count($rst_nat_payment_tahun_berjalan) > 0) {
|
|
// foreach ($rst_nat_payment_tahun_berjalan as $key => $value) {
|
|
// $hasil_tmp_berjalan[] = [
|
|
// "alias" => "nat_payment_tahun_berjalan",
|
|
// "value" => $value['nat_payment_tahun_berjalan'],
|
|
// "S_RegionalID" => $value['S_RegionalID'],
|
|
// "S_RegionalName" => $value['S_RegionalName']
|
|
// ];
|
|
// }
|
|
// }
|
|
|
|
|
|
$combinedDataBerjalan = [];
|
|
|
|
foreach ($rst_nat_sales_tahun_berjalan as $sales) {
|
|
$combinedDataBerjalan[$sales['S_RegionalID']] = [
|
|
'S_RegionalName' => $sales['S_RegionalName'],
|
|
'nat_sales_berjalan' => $sales['nat_sales_berjalan'],
|
|
'nat_payment_berjalan' => 0, // Inisialisasi dengan 0
|
|
];
|
|
}
|
|
|
|
foreach ($rst_nat_payment_tahun_berjalan as $payment) {
|
|
if (isset($combinedDataBerjalan[$payment['S_RegionalID']])) {
|
|
$combinedDataBerjalan[$payment['S_RegionalID']]['nat_payment_berjalan'] = $payment['nat_payment_berjalan'];
|
|
} else {
|
|
$combinedDataBerjalan[$payment['S_RegionalID']] = [
|
|
'S_RegionalName' => $payment['S_RegionalID'],
|
|
'nat_sales_berjalan' => 0, // Inisialisasi dengan 0
|
|
'nat_payment_berjalan' => $payment['nat_payment_berjalan'],
|
|
];
|
|
}
|
|
}
|
|
|
|
// hitung selisih berjalan
|
|
foreach ($combinedDataBerjalan as $regionalID => $data) {
|
|
$selisih = $data['nat_sales_berjalan'] - $data['nat_payment_berjalan'];
|
|
// $result[] = [
|
|
// 'S_RegionalID' => $regionalID,
|
|
// 'S_RegionalName' => $data['S_RegionalName'],
|
|
// 'selisih_sales_payment' => $selisih,
|
|
// ];
|
|
|
|
$value_of_scope_berjalan[] = $selisih;
|
|
}
|
|
|
|
// H min 1
|
|
$combinedDataHMin1 = [];
|
|
|
|
foreach ($rst_nat_sales_year_min_1 as $sales) {
|
|
$combinedDataHMin1[$sales['S_RegionalID']] = [
|
|
'S_RegionalName' => $sales['S_RegionalName'],
|
|
'nat_sales_h_min_1' => $sales['nat_sales_h_min_1'],
|
|
'nat_payment_h_min_1' => 0, // Inisialisasi dengan 0
|
|
];
|
|
}
|
|
|
|
foreach ($rst_nat_payment_year_min_1 as $payment) {
|
|
if (isset($combinedDataHMin1[$payment['S_RegionalID']])) {
|
|
$combinedDataHMin1[$payment['S_RegionalID']]['nat_payment_h_min_1'] = $payment['nat_payment_h_min_1'];
|
|
} else {
|
|
$combinedDataHMin1[$payment['S_RegionalID']] = [
|
|
'S_RegionalName' => $payment['S_RegionalID'],
|
|
'nat_sales_h_min_1' => 0, // Inisialisasi dengan 0
|
|
'nat_payment_h_min_1' => $payment['nat_payment_h_min_1'],
|
|
];
|
|
}
|
|
}
|
|
|
|
// hitung selisih berjalan
|
|
foreach ($combinedDataHMin1 as $regionalID => $data) {
|
|
$selisih = $data['nat_sales_h_min_1'] - $data['nat_payment_h_min_1'];
|
|
// $result[] = [
|
|
// 'S_RegionalID' => $regionalID,
|
|
// 'S_RegionalName' => $data['S_RegionalName'],
|
|
// 'selisih_sales_payment' => $selisih,
|
|
// ];
|
|
|
|
$value_of_scope_h_min_1[] = $selisih;
|
|
}
|
|
|
|
// COLOR
|
|
$new_regional_list = [];
|
|
if (count($rst_regional_list) > 0) {
|
|
foreach ($rst_regional_list as $key => $value) {
|
|
$new_regional_list[] = $value['S_RegionalName'];
|
|
}
|
|
}
|
|
|
|
$new_list_scope = array_merge($new_regional_list, $list_scope);
|
|
$new_list_scope = array_unique($new_list_scope);
|
|
|
|
for ($i = 0; $i < count($value_of_scope_berjalan); $i++) {
|
|
$value_of_scope[] = strval($value_of_scope_berjalan[$i] - $value_of_scope_h_min_1[$i]);
|
|
$list_color[] = $this->list_color_bar_chart($i, count($new_list_scope));
|
|
}
|
|
|
|
|
|
// $result = array(
|
|
// "value_of_scope" => $value_of_scope,
|
|
// "list_scope" => $new_list_scope,
|
|
// "bar_chart_color" => $list_color,
|
|
// "last_qry_nat_sales" => $last_qry_nat_sales,
|
|
// "last_qry_nat_payment" => $last_qry_nat_payment,
|
|
// "last_qry_regional_list" => $last_qry_regional_list,
|
|
// );
|
|
|
|
$result = [
|
|
"last_qry_nat_sales_tahun_berjalan" => $sql_nat_sales_tahun_berjalan,
|
|
"last_qry_nat_payment_tahun_berjalan" => $sql_nat_payment_tahun_berjalan,
|
|
"last_qry_nat_sales_year_min_1" => $sql_nat_sales_year_min_1,
|
|
"last_qry_nat_payment_year_min_1" => $sql_nat_payment_year_min_1,
|
|
"value_scope_berjalan" => $value_of_scope_berjalan,
|
|
"value_of_scope_h_min_1" => $value_of_scope_h_min_1,
|
|
"value_of_scope" => $value_of_scope,
|
|
"bar_chart_color" => $list_color,
|
|
"list_scope" => $new_list_scope,
|
|
];
|
|
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_total_piutang_bar_chart_no_group_sebelum($debug = "", $filter_scope_debug = "", $filter_waktu_debug = "")
|
|
{
|
|
$this->corss();
|
|
// total pendapatan nat_sales
|
|
// *) di tambah dengan tahun - 1
|
|
// di berikan diff dalam persen , jika sebelumnya 0 ( belum ada data ) di kasih 100
|
|
|
|
try {
|
|
// if (!$this->isLogin) {
|
|
// $this->sys_error("Invalid Token");
|
|
// exit;
|
|
// }
|
|
|
|
$date_now = date('Y-m-d');
|
|
|
|
$filter_scope = "";
|
|
$filter_waktu = "";
|
|
$join_filter_tahun_berjalan = "";
|
|
$join_filter_end_date_year_min_1 = "";
|
|
|
|
$where_tahun_berjalan_a = "`Nat_SalesYear` = year('$date_now')
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
|
|
$where_tahun_berjalan_b = "Nat_PaymentOrderYear = year('$date_now')
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
$where_end_date_year_min_1_a = "`Nat_SalesYear` = year('$date_now') - 1
|
|
AND Nat_SalesDay <= DATE(NOW() - INTERVAL 1 YEAR)
|
|
AND Nat_SalesIsActive = 'Y'";
|
|
|
|
$where_end_date_year_min_1_b = "Nat_PaymentOrderYear = year('$date_now') - 1
|
|
AND Nat_PaymentDay <= DATE(NOW() - INTERVAL 1 YEAR)
|
|
AND Nat_PaymentIsActive = 'Y'";
|
|
|
|
// untuk debug
|
|
if ($debug != "") {
|
|
// $filter_scope = $filter_scope_debug;
|
|
$filter_scope = ($filter_scope_debug == "0" || $filter_scope_debug == 0) ? "-" : $filter_scope_debug;
|
|
$filter_waktu = $filter_waktu_debug;
|
|
}
|
|
|
|
// PAKE POST DARI FLUTTER
|
|
$prm = $this->sys_input;
|
|
if (isset($prm['filter_scope'])) {
|
|
$filter_scope = ($prm["filter_scope"] == "0" || $prm["filter_scope"] == 0) ? "-" : $prm["filter_scope"];
|
|
}
|
|
|
|
if (isset($prm['filter_waktu'])) {
|
|
$filter_waktu = $prm["filter_waktu"];
|
|
}
|
|
|
|
// filter start
|
|
if ($filter_scope != "-") {
|
|
$join_filter_tahun_berjalan = " AND S_RegionalID = $filter_scope";
|
|
$join_filter_end_date_year_min_1 = " AND S_RegionalID = $filter_scope";
|
|
}
|
|
|
|
if ($filter_waktu != "") {
|
|
if ($filter_waktu != 'YTD') {
|
|
// bukan Year To Date
|
|
|
|
// menentukan apakah filter Quarter ?
|
|
if (strpos($filter_waktu, 'Q') !== false || strpos($filter_waktu, 'q') !== false) {
|
|
$quarterResult = $this->getQuarterByFilter($filter_waktu);
|
|
if ($quarterResult != '-') {
|
|
|
|
return $this->nat_total_piutang_bar_chart_no_group_quarter($filter_scope, $filter_waktu);
|
|
|
|
// $where_tahun_berjalan = " Nat_SalesIsActive = 'Y'
|
|
// AND Nat_SalesQuarter = ($quarterResult)
|
|
// AND `Nat_SalesYear` = YEAR('$date_now')";
|
|
|
|
// $where_year_min_1 = " Nat_PaymentIsActive = 'Y'
|
|
// AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
// AND Nat_PaymentOrderYear = year('$date_now')";
|
|
|
|
// // nat_sales berjalan
|
|
// $where_tahun_berjalan_a = " Nat_SalesIsActive = 'Y'
|
|
// AND Nat_SalesQuarter = ($quarterResult)
|
|
// AND Nat_SalesYear = YEAR('$date_now')";
|
|
|
|
// // nat_sales h-1
|
|
// $where_end_date_year_min_1_a = " Nat_SalesIsActive = 'Y'
|
|
// AND Nat_SalesQuarter = ($quarterResult)
|
|
// AND Nat_SalesYear = YEAR('$date_now') - 1";
|
|
|
|
// // nat_payment berjalan
|
|
// $where_tahun_berjalan_b = " Nat_PaymentIsActive = 'Y'
|
|
// AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
// AND Nat_PaymentOrderYear = YEAR('$date_now')";
|
|
|
|
// // nat_payment h-1
|
|
// $where_end_date_year_min_1_b = " Nat_PaymentIsActive = 'Y'
|
|
// AND Nat_PaymentOrderQuarter = ($quarterResult)
|
|
// AND Nat_PaymentOrderYear = YEAR('$date_now') - 1";
|
|
}
|
|
}
|
|
} else {
|
|
if ($filter_waktu == "YTD") {
|
|
// jika Year To Date
|
|
// klu berjalan, 1 januari 2023 - 4 oktober 2023 ( date now )
|
|
// klu end date year - 1 , 1 januari 2022 - (date now - 1)
|
|
$berjalan_a = date('Y-01-01');
|
|
|
|
$end_date_a = date('Y-01-01', strtotime('-1 year'));
|
|
$end_date_b = date('Y-m-d', strtotime('-1 year'));
|
|
|
|
// nat_sales berjalan
|
|
$where_tahun_berjalan_a = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now')
|
|
AND (`Nat_SalesDay` >= '$berjalan_a' AND `Nat_SalesDay` <= '$date_now')";
|
|
|
|
$where_end_date_year_min_1_a = " Nat_SalesIsActive = 'Y'
|
|
AND `Nat_SalesYear` = year('$date_now') - 1
|
|
AND (`Nat_SalesDay` >= '$end_date_a'
|
|
AND `Nat_SalesDay` <= DATE(NOW() - INTERVAL 1 YEAR))";
|
|
|
|
// nat_payment berjalan
|
|
$where_tahun_berjalan_b = " Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now')
|
|
AND (`Nat_PaymentOrderDay` >= '$berjalan_a' AND `Nat_PaymentOrderDay` <= '$date_now')
|
|
AND `Nat_PaymentYear` = year('$date_now')
|
|
AND (`Nat_PaymentDay` >= '$berjalan_a' AND `Nat_PaymentDay` <= '$date_now')
|
|
";
|
|
|
|
$where_end_date_year_min_1_b = " Nat_PaymentIsActive = 'Y'
|
|
AND `Nat_PaymentOrderYear` = year('$date_now') - 1
|
|
AND (`Nat_PaymentOrderDay` >= '$end_date_a' AND `Nat_PaymentOrderDay` <= DATE(NOW() - INTERVAL 1 YEAR))
|
|
AND `Nat_PaymentYear` = year('$date_now') - 1
|
|
AND (`Nat_PaymentDay` >= '$end_date_a' AND `Nat_PaymentDay` <= DATE(NOW() - INTERVAL 1 YEAR))
|
|
";
|
|
}
|
|
}
|
|
}
|
|
|
|
// $param = $this->sys_input;
|
|
|
|
$sql_regional_list = "select S_RegionalID, S_RegionalName
|
|
from s_regional
|
|
where S_RegionalIsActive = 'Y'
|
|
order by S_RegionalName ASC";
|
|
$qry_regional_list = $this->db->query($sql_regional_list);
|
|
if (!$qry_regional_list) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_regional_list = $this->db->last_query();
|
|
$rst_regional_list = $qry_regional_list->result_array();
|
|
|
|
// TAHUN BERJALAN
|
|
|
|
// nat_sales berjalan
|
|
$sql_nat_sales_tahun_berjalan = "SELECT
|
|
ifnull(SUM(`Nat_SalesTotal`), 0) AS nat_sales_berjalan,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM `nat_sales`
|
|
JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_tahun_berjalan
|
|
WHERE $where_tahun_berjalan_a
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_sales_tahun_berjalan = $this->db->query($sql_nat_sales_tahun_berjalan);
|
|
if (!$qry_nat_sales_tahun_berjalan) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_sales_tahun_berjalan = $this->db->last_query();
|
|
$rst_nat_sales_tahun_berjalan = $qry_nat_sales_tahun_berjalan->result_array();
|
|
|
|
// nat_payment berjalan
|
|
$sql_nat_payment_tahun_berjalan = "SELECT
|
|
ifnull(SUM(Nat_PaymentTotal),0) AS nat_payment_berjalan,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM nat_payment
|
|
JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_end_date_year_min_1
|
|
WHERE $where_tahun_berjalan_b
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_payment_tahun_berjalan = $this->db->query($sql_nat_payment_tahun_berjalan);
|
|
if (!$qry_nat_payment_tahun_berjalan) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_payment_tahun_berjalan = $this->db->last_query();
|
|
$rst_nat_payment_tahun_berjalan = $qry_nat_payment_tahun_berjalan->result_array();
|
|
|
|
// YEAR MIN 1
|
|
$sql_nat_sales_year_min_1 = "SELECT
|
|
ifnull(SUM(`Nat_SalesTotal`), 0) AS nat_sales_h_min_1,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM `nat_sales`
|
|
JOIN m_branch
|
|
ON Nat_SalesM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_tahun_berjalan
|
|
WHERE $where_end_date_year_min_1_a
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_sales_year_min_1 = $this->db->query($sql_nat_sales_year_min_1);
|
|
if (!$qry_nat_sales_year_min_1) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_sales_year_min_1 = $this->db->last_query();
|
|
$rst_nat_sales_year_min_1 = $qry_nat_sales_year_min_1->result_array();
|
|
|
|
$sql_nat_payment_year_min_1 = "SELECT
|
|
ifnull(SUM(Nat_PaymentTotal),0) AS nat_payment_h_min_1,
|
|
S_RegionalID as S_RegionalID, S_RegionalName as S_RegionalName
|
|
FROM nat_payment
|
|
JOIN m_branch
|
|
ON Nat_PaymentM_BranchID = M_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional
|
|
ON M_BranchS_RegionalID = S_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
$join_filter_end_date_year_min_1
|
|
WHERE $where_end_date_year_min_1_b
|
|
GROUP BY S_RegionalID
|
|
ORDER BY S_RegionalName ASC";
|
|
|
|
$qry_nat_payment_year_min_1 = $this->db->query($sql_nat_payment_year_min_1);
|
|
if (!$qry_nat_payment_year_min_1) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry_nat_payment_year_min_1 = $this->db->last_query();
|
|
$rst_nat_payment_year_min_1 = $qry_nat_payment_year_min_1->result_array();
|
|
|
|
// HITUNG SELISIH BERJALAN
|
|
$hasil_tmp_berjalan = [];
|
|
$list_scope = [];
|
|
$value_of_scope_berjalan = [];
|
|
$value_of_scope_h_min_1 = [];
|
|
$value_of_scope = [];
|
|
$result = [];
|
|
|
|
// if(count($rst_nat_sales_tahun_berjalan) > 0) {
|
|
// foreach ($rst_nat_sales_tahun_berjalan as $key => $value) {
|
|
// $hasil_tmp_berjalan[] = [
|
|
// "alias" => "nat_sales_berjalan",
|
|
// "value" => $value['nat_sales_berjalan'],
|
|
// "S_RegionalID" => $value['S_RegionalID'],
|
|
// "S_RegionalName" => $value['S_RegionalName']
|
|
// ];
|
|
// }
|
|
// }
|
|
|
|
// if(count($rst_nat_payment_tahun_berjalan) > 0) {
|
|
// foreach ($rst_nat_payment_tahun_berjalan as $key => $value) {
|
|
// $hasil_tmp_berjalan[] = [
|
|
// "alias" => "nat_payment_tahun_berjalan",
|
|
// "value" => $value['nat_payment_tahun_berjalan'],
|
|
// "S_RegionalID" => $value['S_RegionalID'],
|
|
// "S_RegionalName" => $value['S_RegionalName']
|
|
// ];
|
|
// }
|
|
// }
|
|
|
|
|
|
$combinedDataBerjalan = [];
|
|
|
|
foreach ($rst_nat_sales_tahun_berjalan as $sales) {
|
|
$combinedDataBerjalan[$sales['S_RegionalID']] = [
|
|
'S_RegionalName' => $sales['S_RegionalName'],
|
|
'nat_sales_berjalan' => $sales['nat_sales_berjalan'],
|
|
'nat_payment_berjalan' => 0, // Inisialisasi dengan 0
|
|
];
|
|
}
|
|
|
|
foreach ($rst_nat_payment_tahun_berjalan as $payment) {
|
|
if (isset($combinedDataBerjalan[$payment['S_RegionalID']])) {
|
|
$combinedDataBerjalan[$payment['S_RegionalID']]['nat_payment_berjalan'] = $payment['nat_payment_berjalan'];
|
|
} else {
|
|
$combinedDataBerjalan[$payment['S_RegionalID']] = [
|
|
'S_RegionalName' => $payment['S_RegionalID'],
|
|
'nat_sales_berjalan' => 0, // Inisialisasi dengan 0
|
|
'nat_payment_berjalan' => $payment['nat_payment_berjalan'],
|
|
];
|
|
}
|
|
}
|
|
|
|
// hitung selisih berjalan
|
|
foreach ($combinedDataBerjalan as $regionalID => $data) {
|
|
$selisih = $data['nat_sales_berjalan'] - $data['nat_payment_berjalan'];
|
|
// $result[] = [
|
|
// 'S_RegionalID' => $regionalID,
|
|
// 'S_RegionalName' => $data['S_RegionalName'],
|
|
// 'selisih_sales_payment' => $selisih,
|
|
// ];
|
|
|
|
$value_of_scope_berjalan[] = $selisih;
|
|
}
|
|
|
|
// H min 1
|
|
$combinedDataHMin1 = [];
|
|
|
|
foreach ($rst_nat_sales_year_min_1 as $sales) {
|
|
$combinedDataHMin1[$sales['S_RegionalID']] = [
|
|
'S_RegionalName' => $sales['S_RegionalName'],
|
|
'nat_sales_h_min_1' => $sales['nat_sales_h_min_1'],
|
|
'nat_payment_h_min_1' => 0, // Inisialisasi dengan 0
|
|
];
|
|
}
|
|
|
|
foreach ($rst_nat_payment_year_min_1 as $payment) {
|
|
if (isset($combinedDataHMin1[$payment['S_RegionalID']])) {
|
|
$combinedDataHMin1[$payment['S_RegionalID']]['nat_payment_h_min_1'] = $payment['nat_payment_h_min_1'];
|
|
} else {
|
|
$combinedDataHMin1[$payment['S_RegionalID']] = [
|
|
'S_RegionalName' => $payment['S_RegionalID'],
|
|
'nat_sales_h_min_1' => 0, // Inisialisasi dengan 0
|
|
'nat_payment_h_min_1' => $payment['nat_payment_h_min_1'],
|
|
];
|
|
}
|
|
}
|
|
|
|
// hitung selisih berjalan
|
|
foreach ($combinedDataHMin1 as $regionalID => $data) {
|
|
$selisih = $data['nat_sales_h_min_1'] - $data['nat_payment_h_min_1'];
|
|
// $result[] = [
|
|
// 'S_RegionalID' => $regionalID,
|
|
// 'S_RegionalName' => $data['S_RegionalName'],
|
|
// 'selisih_sales_payment' => $selisih,
|
|
// ];
|
|
|
|
$value_of_scope_h_min_1[] = $selisih;
|
|
}
|
|
|
|
// COLOR
|
|
$new_regional_list = [];
|
|
if (count($rst_regional_list) > 0) {
|
|
foreach ($rst_regional_list as $key => $value) {
|
|
$new_regional_list[] = $value['S_RegionalName'];
|
|
}
|
|
}
|
|
|
|
$new_list_scope = array_merge($new_regional_list, $list_scope);
|
|
$new_list_scope = array_unique($new_list_scope);
|
|
|
|
for ($i = 0; $i < count($value_of_scope_berjalan); $i++) {
|
|
$value_of_scope[] = strval($value_of_scope_berjalan[$i] - $value_of_scope_h_min_1[$i]);
|
|
$list_color[] = $this->list_color_bar_chart($i, count($new_list_scope));
|
|
}
|
|
|
|
|
|
// $result = array(
|
|
// "value_of_scope" => $value_of_scope,
|
|
// "list_scope" => $new_list_scope,
|
|
// "bar_chart_color" => $list_color,
|
|
// "last_qry_nat_sales" => $last_qry_nat_sales,
|
|
// "last_qry_nat_payment" => $last_qry_nat_payment,
|
|
// "last_qry_regional_list" => $last_qry_regional_list,
|
|
// );
|
|
|
|
$result = [
|
|
"last_qry_nat_sales_tahun_berjalan" => $sql_nat_sales_tahun_berjalan,
|
|
"last_qry_nat_payment_tahun_berjalan" => $sql_nat_payment_tahun_berjalan,
|
|
"last_qry_nat_sales_year_min_1" => $sql_nat_sales_year_min_1,
|
|
"last_qry_nat_payment_year_min_1" => $sql_nat_payment_year_min_1,
|
|
"value_scope_berjalan" => $value_of_scope_berjalan,
|
|
"value_of_scope_h_min_1" => $value_of_scope_h_min_1,
|
|
"value_of_scope" => $value_of_scope,
|
|
"bar_chart_color" => $list_color,
|
|
"list_scope" => $new_list_scope,
|
|
];
|
|
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
// national end
|
|
|
|
public function reg_total()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$regID = "0";
|
|
if (isset($prm['reg_id'])) {
|
|
$regID = $prm["reg_id"];
|
|
}
|
|
|
|
$sql = "SELECT (a - b) AS total_piutang FROM (
|
|
SELECT SUM(`Nat_SalesTotal`) AS a
|
|
FROM `nat_sales`
|
|
JOIN m_branch ON M_BranchID = Nat_SalesM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON S_RegionalID = M_BranchS_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND S_RegionalID = ?
|
|
WHERE `Nat_SalesYear` = year(curdate())
|
|
AND Nat_SalesIsActive = 'Y'
|
|
) z, (
|
|
SELECT SUM(Nat_PaymentTotal) AS b
|
|
FROM nat_payment
|
|
JOIN m_branch ON M_BranchID = Nat_PaymentM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON S_RegionalID = M_BranchS_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND S_RegionalID = ?
|
|
WHERE Nat_PaymentOrderYear = year(curdate())
|
|
AND Nat_PaymentIsActive = 'Y') b";
|
|
$qry = $this->db->query($sql, [$regID, $regID]);
|
|
if (!$qry) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$rst = $qry->result_array();
|
|
$result = array(
|
|
"total_piutang" => abs($rst[0]['total_piutang']),
|
|
"last_qry" => $last_qry
|
|
);
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
public function branch_total()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$branchID = "0";
|
|
if (isset($prm['branch_id'])) {
|
|
$branchID = $prm["branch_id"];
|
|
}
|
|
|
|
$sql = "SELECT (a - b) AS total_piutang FROM (
|
|
SELECT SUM(`Nat_SalesTotal`) AS a
|
|
FROM `nat_sales`
|
|
JOIN m_branch ON M_BranchID = Nat_SalesM_BranchID
|
|
AND M_BranchID = ?
|
|
AND M_BranchIsActive = 'Y'
|
|
WHERE `Nat_SalesYear` = year(curdate())
|
|
AND Nat_SalesIsActive = 'Y'
|
|
) z, (
|
|
SELECT SUM(Nat_PaymentTotal) AS b
|
|
FROM nat_payment
|
|
JOIN m_branch ON M_BranchID = Nat_PaymentM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
AND M_BranchID = ?
|
|
WHERE Nat_PaymentOrderYear = year(curdate())
|
|
AND Nat_PaymentIsActive = 'Y') b";
|
|
$qry = $this->db->query($sql, [$branchID, $branchID]);
|
|
if (!$qry) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$rst = $qry->result_array();
|
|
$result = array(
|
|
"total_piutang" => abs($rst[0]['total_piutang']),
|
|
"last_qry" => $last_qry
|
|
);
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
public function nat_3_month_piutang()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$month = "0";
|
|
if (isset($prm['month'])) {
|
|
$month = explode(",", $prm["month"]);
|
|
}
|
|
$sql_reg = "SELECT
|
|
S_RegionalID,
|
|
S_RegionalName,
|
|
MONTH(curdate()) as curr_month
|
|
FROM s_regional
|
|
WHERE S_RegionalIsActive = 'Y'";
|
|
$qry_reg = $this->db->query($sql_reg, []);
|
|
if (!$qry_reg) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$arr_reg = $qry_reg->result_array();
|
|
$sql_sales = "SELECT S_RegionalID ,
|
|
SUM(`Nat_SalesTotal`) AS total_sales,
|
|
DATE_FORMAT(Nat_SalesDay, '%Y-%m' ) as bulan,
|
|
DATE_FORMAT(Nat_SalesDay, '%m' ) as month
|
|
FROM `nat_sales`
|
|
JOIN m_branch ON M_BranchID = Nat_SalesM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON S_RegionalID = M_BranchS_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND Nat_SalesDay + interval 3 month > curdate()
|
|
AND Nat_SalesIsActive = 'Y'
|
|
GROUP BY S_RegionalID, bulan";
|
|
$qry_sales = $this->db->query($sql_sales, []);
|
|
if (!$qry_sales) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$arr_sales = $qry_sales->result_array();
|
|
|
|
$sql_payment = "SELECT S_RegionalID ,
|
|
SUM(Nat_PaymentTotal) AS total_payment ,
|
|
DATE_FORMAT(Nat_PaymentOrderDay , '%m' ) as month,
|
|
DATE_FORMAT(Nat_PaymentOrderDay , '%Y-%m' ) as bulan
|
|
FROM nat_payment
|
|
JOIN m_branch ON M_BranchID = Nat_PaymentM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON S_RegionalID = M_BranchS_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND Nat_PaymentOrderDay + interval 3 month > curdate()
|
|
AND Nat_PaymentIsActive = 'Y'
|
|
GROUP BY S_RegionalID, bulan";
|
|
$qry_payment = $this->db->query($sql_payment, []);
|
|
if (!$qry_payment) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$arr_payment = $qry_payment->result_array();
|
|
$arr_month = [
|
|
date("m", strtotime("-3 Months")),
|
|
date("m", strtotime("-2 Months")),
|
|
date("m", strtotime("-1 Months"))
|
|
];
|
|
|
|
$perregional = array();
|
|
for ($i = 0; $i < count($arr_reg); $i++) {
|
|
$regional = $arr_reg[$i];
|
|
$perbulan = array();
|
|
for ($j = 0; $j < count($arr_month); $j++) {
|
|
$a = array();
|
|
for ($k = 0; $k < count($arr_sales); $k++) {
|
|
|
|
$monthName = date('M', mktime(0, 0, 0, $month[$j], 10));
|
|
$a = array(
|
|
"regional_id" => $regional['S_RegionalID'],
|
|
"month" => $arr_month[$j],
|
|
"bulan" => $monthName,
|
|
"total_sales" => "0",
|
|
"total_payment" => "0",
|
|
"value" => "0"
|
|
);
|
|
}
|
|
array_push($perbulan, $a);
|
|
}
|
|
$z = array(
|
|
"regional_id" => $regional['S_RegionalID'],
|
|
"regional_name" => $regional['S_RegionalName'],
|
|
"dataChart" => $perbulan,
|
|
|
|
);
|
|
array_push($perregional, $z);
|
|
}
|
|
|
|
//assign sales
|
|
for ($i = 0; $i < count($perregional); $i++) {
|
|
$regional = $perregional[$i];
|
|
for ($j = 0; $j < count($regional); $j++) {
|
|
$perbulan = $regional['dataChart'];
|
|
for ($k = 0; $k < count($perbulan); $k++) {
|
|
$data = $perbulan[$k];
|
|
for ($l = 0; $l < count($arr_sales); $l++) {
|
|
$sales = $arr_sales[$l];
|
|
if (
|
|
$data['regional_id'] == $sales['S_RegionalID']
|
|
&& intval($data['month']) == intval($sales['month'])
|
|
) {
|
|
// $perregional[$i]['dataChart'][$k]['total_sales'] = "a";
|
|
$perregional[$i]['dataChart'][$k]['total_sales'] = $sales['total_sales'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//assign payment & count piutang
|
|
for ($i = 0; $i < count($perregional); $i++) {
|
|
$regional = $perregional[$i];
|
|
for ($j = 0; $j < count($regional); $j++) {
|
|
$perbulan = $regional['dataChart'];
|
|
for ($k = 0; $k < count($perbulan); $k++) {
|
|
$data = $perbulan[$k];
|
|
for ($l = 0; $l < count($arr_payment); $l++) {
|
|
$payment = $arr_payment[$l];
|
|
if (
|
|
$data['regional_id'] == $payment['S_RegionalID']
|
|
&& intval($data['month']) == intval($payment['month'])
|
|
) {
|
|
$perregional[$i]['dataChart'][$k]['total_payment'] = $payment['total_payment'];
|
|
}
|
|
}
|
|
$perregional[$i]['dataChart'][$k]['value'] =
|
|
floatval($perregional[$i]['dataChart'][$k]['total_sales']) - floatval($perregional[$i]['dataChart'][$k]['total_payment']);
|
|
}
|
|
}
|
|
}
|
|
$resultt = array(
|
|
"sales" => $arr_sales,
|
|
"payment" => $arr_sales,
|
|
);
|
|
|
|
|
|
// $this->sys_ok($resultt);
|
|
$this->sys_ok($perregional);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
public function reg_3_month_piutang()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$month = "0";
|
|
if (isset($prm['month'])) {
|
|
$month = explode(",", $prm["month"]);
|
|
}
|
|
$regID = "0";
|
|
if (isset($prm['reg_id'])) {
|
|
$regID = $prm["reg_id"];
|
|
}
|
|
$sqlbranch = "SELECT M_BranchID,
|
|
M_BranchName
|
|
FROM m_branch
|
|
WHERE M_BranchIsActive = 'Y'";
|
|
$qrybranch = $this->db->query($sqlbranch, []);
|
|
if (!$qrybranch) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$arrbranch = $qrybranch->result_array();
|
|
|
|
$sql_sales = "SELECT M_BranchID ,
|
|
SUM(`Nat_SalesTotal`) AS total_sales,
|
|
DATE_FORMAT(Nat_SalesDay, '%Y-%m' ) as bulan,
|
|
DATE_FORMAT(Nat_SalesDay, '%m' ) as month
|
|
FROM `nat_sales`
|
|
JOIN m_branch ON M_BranchID = Nat_SalesM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON S_RegionalID = M_BranchS_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND Nat_SalesDay + interval 3 month > curdate()
|
|
AND Nat_SalesIsActive = 'Y'
|
|
GROUP BY M_BranchID, bulan";
|
|
$qry_sales = $this->db->query($sql_sales, []);
|
|
if (!$qry_sales) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$arr_sales = $qry_sales->result_array();
|
|
|
|
$sql_payment = "SELECT M_BranchID ,
|
|
SUM(Nat_PaymentTotal) AS total_payment ,
|
|
DATE_FORMAT(Nat_PaymentOrderDay , '%m' ) as month,
|
|
DATE_FORMAT(Nat_PaymentOrderDay , '%Y-%m' ) as bulan
|
|
FROM nat_payment
|
|
JOIN m_branch ON M_BranchID = Nat_PaymentM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
JOIN s_regional ON S_RegionalID = M_BranchS_RegionalID
|
|
AND S_RegionalIsActive = 'Y'
|
|
AND Nat_PaymentOrderDay + interval 3 month > curdate()
|
|
AND Nat_PaymentIsActive = 'Y'
|
|
GROUP BY M_BranchID, bulan";
|
|
$qry_payment = $this->db->query($sql_payment, []);
|
|
if (!$qry_payment) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$arr_payment = $qry_payment->result_array();
|
|
$arr_month = [
|
|
date("m", strtotime("-3 Months")),
|
|
date("m", strtotime("-2 Months")),
|
|
date("m", strtotime("-1 Months"))
|
|
];
|
|
$perbranch = array();
|
|
for (
|
|
$i = 0;
|
|
$i < count($arrbranch);
|
|
$i++
|
|
) {
|
|
$branch = $arrbranch[$i];
|
|
$perbulan = array();
|
|
for ($j = 0; $j < count($arr_month); $j++) {
|
|
$a = array();
|
|
for ($k = 0; $k < count($arr_sales); $k++) {
|
|
|
|
$monthName = date('M', mktime(0, 0, 0, $month[$j], 10));
|
|
$a = array(
|
|
"branch_id" => $branch['M_BranchID'],
|
|
"month" => $arr_month[$j],
|
|
"bulan" => $monthName,
|
|
"total_sales" => "0",
|
|
"total_payment" => "0",
|
|
"value" => "0"
|
|
);
|
|
}
|
|
array_push($perbulan, $a);
|
|
}
|
|
$z = array(
|
|
"branch_id" => $branch['M_BranchID'],
|
|
"branch_name" => $branch['M_BranchName'],
|
|
"dataChart" => $perbulan,
|
|
);
|
|
array_push($perbranch, $z);
|
|
}
|
|
|
|
for ($i = 0; $i < count($perbranch); $i++) {
|
|
$branch = $perbranch[$i];
|
|
for ($j = 0; $j < count($branch); $j++) {
|
|
$perbulan = $branch['dataChart'];
|
|
for ($k = 0; $k < count($perbulan); $k++) {
|
|
$data = $perbulan[$k];
|
|
for ($l = 0; $l < count($arr_sales); $l++) {
|
|
$sales = $arr_sales[$l];
|
|
if (
|
|
$data['branch_id'] == $sales['M_BranchID']
|
|
&& intval($data['month']) == intval($sales['month'])
|
|
) {
|
|
// $perbranch[$i]['dataChart'][$k]['total_sales'] = "a";
|
|
$perbranch[$i]['dataChart'][$k]['total_sales'] = $sales['total_sales'];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//assign payment & count piutang
|
|
for ($i = 0; $i < count($perbranch); $i++) {
|
|
$branch = $perbranch[$i];
|
|
for ($j = 0; $j < count($branch); $j++) {
|
|
$perbulan = $branch['dataChart'];
|
|
for ($k = 0; $k < count($perbulan); $k++) {
|
|
$data = $perbulan[$k];
|
|
for ($l = 0; $l < count($arr_payment); $l++) {
|
|
$payment = $arr_payment[$l];
|
|
if (
|
|
$data['branch_id'] == $payment['M_BranchID']
|
|
&& intval($data['month']) == intval($payment['month'])
|
|
) {
|
|
$perbranch[$i]['dataChart'][$k]['total_payment'] = $payment['total_payment'];
|
|
}
|
|
}
|
|
$perbranch[$i]['dataChart'][$k]['value'] =
|
|
floatval($perbranch[$i]['dataChart'][$k]['total_sales']) - floatval($perbranch[$i]['dataChart'][$k]['total_payment']);
|
|
}
|
|
}
|
|
}
|
|
|
|
$result = array(
|
|
"total_piutang" => "a",
|
|
"last_qry" => $last_qry
|
|
);
|
|
$this->sys_ok($perbranch);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
public function branch_3_month_piutang()
|
|
{
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
$month = "0";
|
|
if (isset($prm['month'])) {
|
|
$month = explode(",", $prm["month"]);
|
|
}
|
|
$branchID = "0";
|
|
if (isset($prm['branch_id'])) {
|
|
$branchID = $prm["branch_id"];
|
|
}
|
|
|
|
$sql = "SELECT (a - b) AS total_piutang FROM (
|
|
SELECT SUM(`Nat_SalesTotal`) AS a
|
|
FROM `nat_sales`
|
|
JOIN m_branch ON M_BranchID = Nat_SalesM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
AND M_BranchID = ?
|
|
WHERE `Nat_SalesYear` = year(curdate())
|
|
AND Nat_SalesMonth IN ?
|
|
AND Nat_SalesIsActive = 'Y'
|
|
) z, (
|
|
SELECT SUM(Nat_PaymentTotal) AS b
|
|
FROM nat_payment
|
|
JOIN m_branch ON M_BranchID = Nat_PaymentM_BranchID
|
|
AND M_BranchIsActive = 'Y'
|
|
AND M_BranchID = ?
|
|
WHERE Nat_PaymentOrderYear = year(curdate())
|
|
AND Nat_PaymentOrderMonth IN ?
|
|
AND Nat_PaymentIsActive = 'Y') b";
|
|
$qry = $this->db->query($sql, [$branchID, $month, $branchID, $month]);
|
|
if (!$qry) {
|
|
$error = array(
|
|
"message" => $this->db->error()["message"],
|
|
"sql" => $this->db->last_query()
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$last_qry = $this->db->last_query();
|
|
$rst = $qry->result_array();
|
|
$result = array(
|
|
"total_piutang" => abs($rst[0]['total_piutang']),
|
|
"last_qry" => $last_qry
|
|
);
|
|
$this->sys_ok($result);
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
}
|