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

229 lines
8.5 KiB
PHP

<?php
class Fix_orderdetail_panel extends MY_Controller
{
public function index()
{
echo "Fixing snapshot ss_piutang_test";
}
public function fix($nolab)
{
$sql = "select T_OrderHeaderLabNumber, T_OrderHeaderDate,
JSON_EXTRACT(fn_get_patient_atribute(T_OrderHeaderM_PatientID),'$.patient_fullname') M_PatientName,
T_OrderHeaderID
from t_orderheader
where T_OrderHeaderLabNumber = ?";
$qry = $this->db->query($sql, [$nolab]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "<h1> $nolab tidak ada.</h1><br/>";
exit();
}
$fields = array_keys($rows[0]);
$this->print_table($rows, $fields);
$pHeaderID = $rows[0]["T_OrderHeaderID"];
$sql = "select T_OrderDetailT_TestName,
T_OrderDetailT_TestIsPanelChildren,
T_OrderDetailPrice,
T_OrderDetailPriceForDisc,
T_OrderDetailDisc,
T_OrderDetailDiscAmount,
T_OrderDetailDiscTotal,
T_OrderDetailTotal
from t_orderdetail
where T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestIsPanelChildren = 'Y'";
$qry = $this->db->query($sql, [$pHeaderID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "<h1> $nolab tidak memiliki panel.</h1><br/>";
exit();
}
$fields = array_keys($rows[0]);
$this->print_table($rows, $fields);
$havingIssue = false;
foreach ($rows as $r) {
if ($r["T_OrderDetailTotal"] > 0) {
$havingIssue = true;
}
}
if (!$havingIssue) {
echo "<h1> $nolab tidak ada Order Detail Anakan Panel dengan harga.</h1><br/>";
exit();
}
$sql = "update t_orderdetail
set T_OrderDetailPrice = 0,
T_OrderDetailPriceForDisc = 0,
T_OrderDetailDisc =0,
T_OrderDetailDiscAmount = 0,
T_OrderDetailDiscTotal = 0,
T_OrderDetailTotal = 0
from t_orderdetail
where T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestIsPanelChildren = 'Y'";
$qry = $this->db->query($sql, [$pHeaderID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
echo "<h1> $nolab Fixasi Order Detail Anakan Panel.</h1><br/>";
$sql = "select T_OrderDetailT_TestName,
T_OrderDetailT_TestIsPanelChildren,
T_OrderDetailPrice,
T_OrderDetailPriceForDisc,
T_OrderDetailDisc,
T_OrderDetailDiscAmount,
T_OrderDetailDiscTotal,
T_OrderDetailTotal
from t_orderdetail
where T_OrderDetailT_OrderHeaderID = ?
and T_OrderDetailIsActive = 'Y'
and T_OrderDetailT_TestIsPanelChildren = 'Y'";
$qry = $this->db->query($sql, [$pHeaderID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
$rows = $qry->result_array();
if (count($rows) == 0) {
echo "<h1> $nolab tidak memiliki panel.</h1><br/>";
exit();
}
$fields = array_keys($rows[0]);
$this->print_table($rows, $fields);
}
public function print_table_style()
{
echo "
<style>
th, td {
padding: 15px;
text-align: left;
}
tr:nth-child(even) {background-color: #f2f2f2;}
table {
border: solid 1px ;
min-width:600px;
}
</style>
";
}
public function print_table($rows, $keys)
{
$this->print_table_style();
echo "<table>";
echo "<tr>";
foreach ($keys as $k) {
echo "<td>$k</td>";
}
echo "</tr>\n";
foreach ($rows as $r) {
echo "<tr>";
foreach ($keys as $k) {
echo "<td>" . $r[$k] . "</td>";
}
echo "</tr>";
}
echo "</table>";
}
public function fix_b2($nolab)
{
$sql = "select T_OrderHeaderID , SsPiutangID
from t_orderheader
join ss_piutang on T_OrderHeaderID = SsPiutangT_OrderHeaderID
and SsPiutangType = 'B2'
where T_OrderHeaderLabNumber = ?
";
$qry = $this->db->query($sql, [$nolab]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
$rows = $qry->result_array();
echo "<h5> Fix Ss Piutang Test [B2] for $nolab </h5>";
if (count($rows) == 0) {
echo "No Record.<br/>";
exit();
}
$orderHeaderID = $rows[0]["T_OrderHeaderID"];
$ssPiutangID = $rows[0]["SsPiutangID"];
$sql = "delete from ss_piutang_test where SsPiutangTestSsPiutangID=?";
$qry = $this->db->query($sql, [$ssPiutangID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
$sql = "
insert into ss_piutang_test(SsPiutangTestSsPiutangID, SsPiutangTestT_TestID ,
SsPiutangTestPrice, SsPiutangTestDiscTotal, SsPiutangTestTotal)
select SsPiutangID, T_TestID,
if(T_OrderDetailIsActive ='Y',1,-1) * T_OrderDetailPrice T_OrderDetailPrice,
if(T_OrderDetailIsActive ='Y',1,-1) * T_OrderDetailDiscTotal T_OrderDetailDiscTotal,
if(T_OrderDetailIsActive ='Y',1,-1) * T_OrderDetailTotal T_OrderDetailTotal
from
ss_piutang
join t_orderdetail
on SsPiutangType ='B2'
and SsPiutangID = ?
and SsPiutangT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
and T_OrderDetailT_OrderHeaderID = ?
join t_test on T_OrderDetailT_TestID = T_TestID and T_TestIsPrice = 'Y'";
$qry = $this->db->query($sql, [$ssPiutangID, $orderHeaderID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
//fix ss_piutang_test
$sql = "update
ss_piutang_test
join ss_piutang_packet on SsPiutangTestSsPiutangID = SsPiutangPacketSsPiutangID
join ss_piutang on SsPiutangID = SsPiutangTestSsPiutangID
join t_test on SsPiutangTestT_TestID = T_TestID
join t_packetdetail on SsPiutangPacketT_PacketID = T_PacketDetailT_PacketID
and T_PacketDetailT_TestID = SsPiutangTestT_TestID
and SsPiutangT_OrderHeaderID = ? and T_PacketDetailIsActive = 'Y'
set SsPiutangTestPrice = T_PacketDetailPriceAmount,
SsPiutangTestTotal = T_PacketDetailPriceAmount";
$qry = $this->db->query($sql, [$orderHeaderID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
$sql = "update
ss_piutang_test
join ss_piutang_packet on SsPiutangTestSsPiutangID = SsPiutangPacketSsPiutangID
join ss_piutang on SsPiutangID = SsPiutangTestSsPiutangID
join t_test on SsPiutangTestT_TestID = T_TestID
join t_packetdetail on SsPiutangPacketT_PacketID = T_PacketDetailT_PacketID
and T_PacketDetailT_TestID = SsPiutangTestT_TestID
join t_packet on T_PacketDetailT_PacketID = T_PacketID
and SsPiutangT_OrderHeaderID = ?
set
SsPiutangTestDiscTotal = (T_PacketOriginalPrice - T_PacketPrice) / T_PacketOriginalPrice * T_PacketDetailPrice ,
SsPiutangTestTotal = SsPiutangTestPrice -( (T_PacketOriginalPrice - T_PacketPrice) / T_PacketOriginalPrice * T_PacketDetailPrice )";
$qry = $this->db->query($sql, [$orderHeaderID]);
if (!$qry) {
echo "Error Query : {$this->db->error()["message"]} | {$this->db->last_query()}\n";
exit();
}
echo "<p>Fix $nolab selesai : Check <a href='/one-api/fix/fix_ss_piutang_test/check_b2/${nolab}'>di sini </a></p>";
}
}