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

216 lines
7.0 KiB
PHP

<?php
class Fixsspayment extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->db = $this->load->database("onedev", true);
}
function fix_is_last($orderID)
{
$sql = "update ss_piutang set SsPiutangIsLast = 'N' where SsPiutangT_OrderHeaderID = ?";
$this->db->query($sql, array($orderID));
$sql = "select max(SsPiutangID) mID from ss_piutang where SsPiutangT_OrderHeaderID = ?";
$qry = $this->db->query($sql, array($orderID));
if ($qry) {
$rows = $qry->result_array();
if (count($rows) > 0) {
$id = $rows[0]["mID"];
$sql = "update ss_piutang set SsPiutangIsLast = 'Y' where SsPiutangID = ?";
$this->db->query($sql, array($id));
}
}
}
function fix_point_date($date)
{
$sql = "select t_ordermemberdiscount.*
from t_ordermemberdiscount
join t_orderheader on T_OrderMemberDiscountT_OrderHeaderID = T_OrderHeaderID
and T_OrderHeaderDate >= ? and T_OrderHeaderDate <= ?
and T_OrderHeaderIsActive = 'Y' and T_OrderMemberDiscountIsActive = 'Y'";
if ($date == "") {
$date = date("Y-m-d");
}
$sdate = "$date 00:00:00";
$edate = "$date 23:59:59";
$qry = $this->db->query($sql, [$sdate, $edate]);
if (!$qry) {
echo "No MemberDiscount table";
print_r($this->db->error());
exit;
}
$rows = $qry->result_array();
$this->db->trans_begin();
foreach ($rows as $r) {
$date = date("Y-m-d", strtotime($r["T_OrderMemberDiscountLastUpdated"]));
$hid = $r["T_OrderMemberDiscountT_OrderHeaderID"];
$disc = $r["T_OrderMemberDiscountBruto"] - $r["T_OrderMemberDiscountNetto"];
$disc_pct = $r["T_OrderMemberDiscountPercent"];
$xtotal = $r["T_OrderMemberDiscountNetto"];
$this->fix_point_by_header_id($hid, $disc, $disc_pct, $xtotal, $date);
}
$this->db->trans_commit();
}
function fix_point_by_header_id($hid, $disc, $disc_pct, $xtotal, $date)
{
//update ss_piutang_test
// disc dihitung dari T_OrderDetail Test setelah Discount
//total disc
$sql = "select SsPiutangID,
sum(SsPiutangTestTotal * ? / 100) new_disc
from ss_piutang_test
join ss_piutang on SsPiutangT_OrderHeaderID = ?
and SsPiutangID = SsPiutangTestSsPiutangID
where SsPiutangDate = ?
group by SsPiutangID
";
$qry = $this->db->query($sql, [$disc_pct, $hid, $date]);
if (!$qry) {
echo "ERR: " . print_r($this->db->error());
exit;
}
$rows = $qry->result_array();
$arr_piutang_disc = [];
foreach ($rows as $r) {
$id = $r["SsPiutangID"];
$disc = $r["new_disc"];
$arr_piutang_disc[$id] = floatval($disc);
}
$sql = "select SsPiutangID,SsPiutangTestID,
SsPiutangTestTotal * ? / 100 new_disc, SsPiutangTestTotal total
from ss_piutang_test
join ss_piutang on SsPiutangT_OrderHeaderID = ?
and SsPiutangID = SsPiutangTestSsPiutangID
where SsPiutangDate = ?
";
$qry = $this->db->query($sql, [$disc_pct, $hid, $date]);
if (!$qry) {
echo "ERR: " . print_r($this->db->error());
exit;
}
$rows = $qry->result_array();
$sql_u = "update ss_piutang_test
set SsPiutangTestDiscTotal = SsPiutangTestDiscTotal + ?,
SsPiutangTestTotal = SsPiutangTestTotal - ?
where SsPiutangTestID = ?";
$sql_uh = "update ss_piutang
set SsPiutangDiscount = SsPiutangDiscount + ?,
SsPiutangNet = SsPiutangNet - ?,
SsPiutangTotal = SsPiutangTotal - ?
where SsPiutangID = ?";
$prevSsPiutangID = 0;
foreach ($rows as $r) {
$new_disc = floatval($r["new_disc"]);
$SsPiutangID = $r["SsPiutangID"];
$SsPiutangTestID = $r["SsPiutangTestID"];
if ($SsPiutangID != $prevSsPiutangID) {
$qry = $this->db->query($sql_uh, [
$arr_piutang_disc[$SsPiutangID],
$arr_piutang_disc[$SsPiutangID],
$arr_piutang_disc[$SsPiutangID], $SsPiutangID
]);
if (!$qry) {
echo "Err : " . print_r($this->db->error(), true) . "\n";
$this->db->trans_rollback();
exit;
}
}
$qry = $this->db->query($sql_u, [$new_disc, $new_disc, $SsPiutangTestID]);
if (!$qry) {
echo "Err : " . print_r($this->db->error(), true) . "\n";
$this->db->trans_rollback();
exit;
}
$prevSsPiutangID = $SsPiutangID;
}
}
function fix_date($date)
{
$sql = "select * from ss_piutang where SsPiutangType = 'B2' and SsPiutangDate = ?";
$qry = $this->db->query($sql, array($date));
$rows = array();
if ($qry) {
$rows = $qry->result_array();
foreach ($rows as $r) {
$ssPiutangID = $r["SsPiutangID"];
$ssPiutangTotal = $r["SsPiutangTotal"];
$ssPiutangPayment = $r["SsPiutangPayment"];
$orderID = $r["SsPiutangT_OrderHeaderID"];
$date = $r["SsPiutangDate"];
$sql = "select sum(SsPiutangPayment) as tot
from ss_piutang
where SsPiutangT_OrderHeaderID = ?
and SsPiutangDate < ? ";
$qryd = $this->db->query($sql, array($orderID, $date));
if ($qryd) {
$rows = $qryd->result_array();
$total = $rows[0]["tot"];
$isLunas = "N";
if ($total == $ssPiutangTotal - $ssPiutangPayment) {
$isLunas = "Y";
}
$sql = "update
ss_piutang
set SsPiutangPaymentBefore = ?,
SsPiutangIsLunas = ?
where
SsPiutangID = ? ";
$this->db->query($sql, array($total, $isLunas, $ssPiutangID));
$this->fix_is_last($orderID);
echo "Update $ssPiutangID : $orderID \n";
} else {
print_r($this->db->error());
}
}
}
$this->fix_point_date($date);
}
function fix_all()
{
$sql = "select * from ss_piutang where SsPiutangType = 'B2'";
$qry = $this->db->query($sql);
$rows = array();
if ($qry) {
$rows = $qry->result_array();
foreach ($rows as $r) {
$ssPiutangID = $r["SsPiutangID"];
$ssPiutangTotal = $r["SsPiutangTotal"];
$ssPiutangPayment = $r["SsPiutangPayment"];
$orderID = $r["SsPiutangT_OrderHeaderID"];
$date = $r["SsPiutangDate"];
$sql = "select sum(SsPiutangPayment) as tot
from ss_piutang
where SsPiutangT_OrderHeaderID = ?
and SsPiutangDate < ? ";
$qryd = $this->db->query($sql, array($orderID, $date));
if ($qryd) {
$rows = $qryd->result_array();
$total = $rows[0]["tot"];
$isLunas = "N";
if ($total == $ssPiutangTotal - $ssPiutangPayment) {
$isLunas = "Y";
}
$sql = "update
ss_piutang
set SsPiutangPaymentBefore = ?,
SsPiutangIsLunas = ?
where
SsPiutangID = ? ";
$this->db->query($sql, array($total, $isLunas, $ssPiutangID));
echo "Update $ssPiutangID : $orderID \n";
} else {
print_r($this->db->error());
}
}
}
}
}