Files
2026-04-27 10:26:26 +07:00

175 lines
7.2 KiB
PHP

<?php
class Mrinsentive extends CI_Controller
{
function __construct() {
parent::__construct();
$this->db = $this->load->database('onedev', true);
}
function rekap($prm_date = "",$prm_staff_id = "") {
$sql = "select * from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
$qry = $this->db->query($sql);
if (! $qry ) {
echo 'Err : ' . print_r($this->db->error(),true) . '\n';
exit;
}
$rows = $qry->result_array();
$branchCode = '';
if (count($rows) > 0 ) {
$branchCode = $rows[0]['M_BranchCode'];
}
if ($branchCode == '') {
echo 'Err : Invalid Branch\n';
exit;
}
$and_staff_id = "";
if ( $prm_staff_id != "" ) {
$and_staff_id = "and M_StaffID = $prm_staff_id ";
}
if ($prm_date == "") {
$prm_date = date("Y-m-d");
}
$month = intval(date("m",strtotime($prm_date)));
$year = intval(date("Y",strtotime($prm_date)));
$year_order = $year;
if ($month == 1 ) $year_order = $year - 1;
$pay_start_date = date("Y-m-26",strtotime($prm_date . " -1 month"));
$pay_end_date = date("Y-m-d",strtotime($pay_start_date . " + 1 month - 1 day"));
$order_end_date = date("Y-m-t",strtotime($pay_end_date . " - 2 month "));
$order_start_date = date("Y-m-01",strtotime($pay_end_date . " - 9 month + 1 day"));
$sql ="select ? SsrMrInsentiveRkM_BranchCode,
sum(`fn_get_payment_marketing`(?,?, T_OrderHeaderID )) SsrMrInsentiveRkPaymentTotal,
month(T_OrderHeaderDate) SsrMrInsentiveRkOrderMonth,
year(T_OrderHeaderDate) SsrMrInsentiveRkOrderYear,
? SsrMrInsentiveRkMonth, ? SsrMrInsentiveRkYear,
M_StaffNIK SsrMrInsentiveRkStaffNIK
from t_orderheader
left join m_company on T_OrderHeaderM_CompanyID = M_CompanyID
left join m_staff on M_CompanyM_StaffID = M_StaffID
left join m_mou on T_OrderHeaderM_MouID = M_MouID and M_MouM_OmzetTypeID = '3'
where
T_OrderHeaderIsActive = 'Y' and M_MouM_OmzetTypeID = '3'
$and_staff_id
and
date(T_OrderHeaderDate) between ? and ?
group by SsrMrInsentiveRkOrderYear, SsrMrInsentiveRkOrderMonth, SsrMrInsentiveRkStaffNIK
";
$qry = $this->db->query($sql,array($branchCode,$pay_start_date, $pay_end_date ,
$month, $year_order,
$order_start_date, $order_end_date));
if (!$qry) {
print_r($this->db->error());
return;
}
$rows = $qry->result_array();
$this->upload("rekap",$rows);
}
function do($prm_date = "",$prm_staff_id = "") {
$sql = "select * from m_branch where M_BranchIsActive = 'Y' and M_BranchIsDefault = 'Y'";
$qry = $this->db->query($sql);
if (! $qry ) {
echo 'Err : ' . print_r($this->db->error(),true) . '\n';
exit;
}
$rows = $qry->result_array();
$branchCode = '';
if (count($rows) > 0 ) {
$branchCode = $rows[0]['M_BranchCode'];
}
if ($branchCode == '') {
echo 'Err : Invalid Branch\n';
exit;
}
$and_staff_id = "";
if ( $prm_staff_id != "" ) {
$and_staff_id = "and M_StaffID = $prm_staff_id ";
}
if ($prm_date == "") {
$prm_date = date("Y-m-d");
}
$month = intval(date("m",strtotime($prm_date)));
$year = intval(date("Y",strtotime($prm_date)));
$year_order = $year;
if ($month == 1 ) $year_order = $year -1;
$end_date = date("Y-m-25",strtotime($prm_date));
$start_date = date("Y-m-01",strtotime($prm_date . " -1 month"));
$sql = "
select ? SsrMrInsentiveM_BranchCode, $month SsrMrInsentiveMonth, $year as SsrMrInsentiveYear,
M_CompanyID SsrMrInsentiveM_CompanyID, M_CompanyNumber SsrMrInsentiveM_CompanyNumber,
M_CompanyName SsrMrInsentiveM_CompanyName, M_StaffNIK SsrMrInsentiveStaffNIK,
sum(T_OrderHeaderTotal) as SsrMrInsentiveTotal,
sum(`fn_get_payment_marketing`(?,?, T_OrderHeaderID )) as SsrMrInsentivePaymentTotal,
DATE_FORMAT(?, '%d/%m/%Y') as SsrMrInsentivePaymentStart,
DATE_FORMAT(?, '%d/%m/%Y') as SsrMrInsentivePaymentEnd,
date_format(T_OrderHeaderDate,'%M %Y') SsrMrInsentiveOrderMonth
from t_orderheader
left join m_company on T_OrderHeaderM_CompanyID = M_CompanyID
left join m_staff on M_CompanyM_StaffID = M_StaffID
left join m_mou on T_OrderHeaderM_MouID = M_MouID
where
T_OrderHeaderIsActive = 'Y' and M_MouM_OmzetTypeID = 3
$and_staff_id
and
YEAR(T_OrderHeaderDate) = ?
AND
MONTH(T_OrderHeaderDate + interval 1 month) = ?
group by M_StaffNik,M_CompanyID
order by M_StaffNIk,M_CompanyID";
$qry = $this->db->query($sql,array($branchCode,$start_date, $end_date , $start_date, $end_date, $year_order, $month));
if (!$qry) {
print_r($this->db->error());
return;
}
$rows = $qry->result_array();
$this->upload("cur",$rows);
}
function upload($func,$x_rows) {
$sql = "select * from conf_systems";
$qry = $this->db->query($sql);
if (! $qry) {
echo "Err : " . print_r($this->db->error(),true);
exit;
}
$rows = $qry->result_array();
if (count($rows) == 0 ) {
echo "Err : No IP Regional";
exit;
}
$ipAddress = $rows[0]["S_SystemIPAddressRegional"];
if ($ipAddress == "" ) {
echo "Err : No IP Regional";
exit;
}
$data = json_encode($x_rows);
$md5 = md5($data);
$param = array("data" => $data, "md5" => $md5);
$jparam = json_encode($param);
$url = "http://$ipAddress/one-api/tools/marketing/r_insentive/$func";
echo "Uploading : Insentive $func , total " . count($x_rows) . " records\n";
echo "To : $url \n";
$j_result = $this->post($url,$jparam);
$result = json_decode($j_result,true);
print_r($result);
}
function post($url,$data) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
$result = curl_exec($ch);
echo $result;
return $result;
}
}