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_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()); } } } } 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()); } } } } }