Files
BE_IBL/application/controllers/one-bb/Bloodbaginventory.php
2026-04-15 15:23:57 +07:00

504 lines
17 KiB
PHP

<?php
class Bloodbaginventory extends MY_Controller
{
var $db;
function __construct()
{
parent::__construct();
}
function index()
{
echo "API : INVENTORY KANTONG DARAH";
// $cek = $this->db->query("select database() as current_db")->result();
// print_r($cek);
}
function search()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$search = "%%";
if (isset($prm["search"])) {
$search = trim($prm["search"]);
if ($search != "") {
$search = "%" . $prm["search"] . "%";
} else {
$search = "%%";
}
}
$bagnumber = "";
if (isset($prm["bagnumber"])) {
$bagnumber = $prm["bagnumber"];
}
$sortBy = $prm["sortBy"];
$sortStatus = $prm["sortStatus"];
if($sortBy){
$q_sort = "ORDER BY ".$sortBy." ".$sortStatus;
}
$number_offset = 0;
$number_limit = 10;
if($prm["current_page"] > 0) {
$number_offset = ($prm["current_page"] - 1) * $number_limit;
}
$sql_filter = "SELECT count(*) as total FROM one_bb.td_baginventory
JOIN one_bb.td_source ON Td_BagInventoryTd_SourceID = Td_SourceID
AND Td_SourceIsActive = 'Y'
JOIN one_bb.td_bloodtype ON Td_BagInventoryTd_BloodTypeID = Td_BloodTypeID
AND Td_BloodTypeIsActive = 'Y'
JOIN one_bb.td_bloodrhesus ON Td_BagInventoryTd_BloodRhesusID = Td_BloodRhesusID
AND Td_BloodRhesusIsActive = 'Y'
WHERE Td_BagInventoryIsActive = 'Y' AND Td_BagInventoryNumber LIKE ?";
$qry_filter = $this->db->query($sql_filter, array($search));
$tot_count = 0;
$tot_page = 0;
if ($qry_filter) {
$tot_count = $qry_filter->result_array()[0]["total"];
$tot_page = ceil($tot_count/$number_limit);
} else {
$this->sys_error_db(" inventory kantong count", $this->db);
exit;
}
$sql_data = "SELECT Td_BagInventoryID,
Td_BagInventoryDate,
Td_SourceID,
Td_SourceName,
Td_SourceIsPMI,
Td_BagInventoryNumber,
Td_BagInventoryVolume,
IF(Td_BagInventoryExpDate ='0000-00-00' OR Td_BagInventoryExpDate is NULL, '', DATE_FORMAT(Td_BagInventoryExpDate, '%d-%m-%Y')) as expdate,
Td_BloodTypeID,
Td_BloodRhesusID,
Td_BloodTypeCode,
Td_BloodRhesusCode,
Td_BagInventoryIsUsed,
Td_BagInventoryUsedDate,
Td_BagInventoryUsedUserID
FROM one_bb.td_baginventory
JOIN one_bb.td_source ON Td_BagInventoryTd_SourceID = Td_SourceID
AND Td_SourceIsActive = 'Y'
JOIN one_bb.td_bloodtype ON Td_BagInventoryTd_BloodTypeID = Td_BloodTypeID
AND Td_BloodTypeIsActive = 'Y'
JOIN one_bb.td_bloodrhesus ON Td_BagInventoryTd_BloodRhesusID = Td_BloodRhesusID
AND Td_BloodRhesusIsActive = 'Y'
WHERE Td_BagInventoryIsActive = 'Y' AND Td_BagInventoryNumber LIKE ?
$q_sort LIMIT ? OFFSET ?";
$qry_data = $this->db->query($sql_data, array($search, $number_limit, $number_offset));
// $last_qry = $this->db->last_query();
// print_r($last_qry);
if ($qry_data) {
$rows = $qry_data->result_array();
} else {
$this->sys_error_db("inventory kantong select", $this->db);
exit;
}
$result = array(
"total" => $tot_page,
"total_filter" => $tot_count,
"records" => $rows
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_source()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT Td_SourceID as sourceid,
Td_SourceName as sourceName,
Td_SourceIsPMI
FROM one_bb.td_source
WHERE Td_SourceIsActive = 'Y'";
$qry = $this->db->query($sql);
$last_qry = $this->db->last_query();
if (!$qry) {
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
$rows = $qry->result_array();
$result = array(
"records" => $rows
);
$this->sys_ok($result);
exit;
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_bloodtype()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT Td_BloodTypeID as bloodtypeid,
Td_BloodTypeCode as bloodTypeCode,
Td_BloodTypeName
FROM one_bb.td_bloodtype
WHERE Td_BloodTypeIsActive = 'Y'";
$qry = $this->db->query($sql);
$last_qry = $this->db->last_query();
if (!$qry) {
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
$rows = $qry->result_array();
$result = array(
"records" => $rows
);
$this->sys_ok($result);
exit;
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_rhesus()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT Td_BloodRhesusID as bloodrhesusid,
Td_BloodRhesusCode as bloodRhesusCode ,
Td_BloodRhesusName
FROM one_bb.td_bloodrhesus
WHERE Td_BloodRhesusIsActive = 'Y'";
$qry = $this->db->query($sql);
$last_qry = $this->db->last_query();
if (!$qry) {
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
$rows = $qry->result_array();
$result = array(
"records" => $rows
);
$this->sys_ok($result);
exit;
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function add()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$this->db->trans_begin();
$prm = $this->sys_input;
$userid = $this->sys_user['M_UserID'];
$bagnumber = "";
if (isset($prm['bagnumber'])) {
$bagnumber = trim($prm["bagnumber"]);
}
$bagvolume = "";
if (isset($prm['bagvolume'])) {
$bagvolume = trim($prm["bagvolume"]);
}
$bagexpdate = date("Y-m-d");
if (isset($prm['bagexpdate'])) {
$bagexpdate = trim($prm["bagexpdate"]);
}
$bagtypeid = "";
if (isset($prm['bagtypeid'])) {
$bagtypeid = trim($prm["bagtypeid"]);
}
$bagrhesusid = "";
if (isset($prm['bagrhesusid'])) {
$bagrhesusid = trim($prm["bagrhesusid"]);
}
$sourceid = "";
if (isset($prm['sourceid'])) {
$sourceid = trim($prm["sourceid"]);
}
$isPMI = "";
if (isset($prm['isPMI'])) {
$isPMI = trim($prm["isPMI"]);
}
$sql = "SELECT Td_SourceID,
Td_SourceName,
Td_SourceIsPMI
FROM one_bb.td_source
WHERE Td_SourceIsActive = 'Y'
AND Td_SourceID = ?";
$qry = $this->db->query($sql, array($sourceid));
if ($qry) {
$rows = $qry->result_array();
} else {
$this->sys_error_db("select error source", $this->db);
exit;
}
if (count($rows) > 0) {
$isPMI = $rows[0]["Td_SourceIsPMI"];
// print_r($isPMI);
// exit;
if ($isPMI == 'N') {
// Td_SourceIsPMI = N
$sql_insert = "INSERT INTO one_bb.td_baginventory(
Td_BagInventoryDate,
Td_BagInventoryTd_SourceID,
Td_BagInventoryNumber,
Td_BagInventoryVolume,
Td_BagInventoryExpDate,
Td_BagInventoryTd_BloodTypeID,
Td_BagInventoryTd_BloodRhesusID,
Td_BagInventoryIsUsed,
Td_BagInventoryUsedDate,
Td_BagInventoryUsedUserID,
Td_BagInventoryIsActive,
Td_BagInventoryCreated,
Td_BagInventoryLastUpdated) VALUES(
NOW(),
?,
(select one_bb.fn_numbering('BAG')),
?,
?,
?,
?,
'N',
NOW(),
?,
'Y',
NOW(),
NOW())";
$qry_insert = $this->db->query($sql_insert, array(
$sourceid,
$bagvolume,
$bagexpdate,
$bagtypeid,
$bagrhesusid,
$userid
));
if (!$qry_insert) {
$this->db->trans_rollback();
$this->sys_error_db("save baginventory error", $this->db);
exit;
}
} else {
// Td_SourceIsPMI = Y
$sql_insert = "INSERT INTO one_bb.td_baginventory(
Td_BagInventoryDate,
Td_BagInventoryTd_SourceID,
Td_BagInventoryNumber,
Td_BagInventoryVolume,
Td_BagInventoryExpDate,
Td_BagInventoryTd_BloodTypeID,
Td_BagInventoryTd_BloodRhesusID,
Td_BagInventoryIsUsed,
Td_BagInventoryUsedDate,
Td_BagInventoryUsedUserID,
Td_BagInventoryIsActive,
Td_BagInventoryCreated,
Td_BagInventoryLastUpdated) VALUES(
NOW(),
?,
?,
?,
?,
?,
?,
'N',
NOW(),
?,
'Y',
NOW(),
NOW())";
$qry_insert = $this->db->query($sql_insert, array(
$sourceid,
$bagnumber,
$bagvolume,
$bagexpdate,
$bagtypeid,
$bagrhesusid,
$userid
));
if (!$qry_insert) {
$this->db->trans_rollbakc();
$this->sys_error_db("save baginventory error", $this->db);
exit;
}
}
}
// $this->db->trans_rollback();
$this->db->trans_commit();
$result = array(
"total" => 1
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function edit()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$this->db->trans_begin();
$prm = $this->sys_input;
$userid = $this->sys_user['M_UserID'];
$sourceid = "";
if (isset($prm['sourceid'])) {
$sourceid = trim($prm["sourceid"]);
}
$number = "";
if (isset($prm['number'])) {
$number = trim($prm["number"]);
}
$volume = "";
if (isset($prm['volume'])) {
$volume = trim($prm["volume"]);
}
$expdate = date("Y-m-d");
if (isset($prm['expdate'])) {
$expdate = trim($prm["expdate"]);
}
$bloodtypeid = "";
if (isset($prm['bloodtypeid'])) {
$bloodtypeid = trim($prm["bloodtypeid"]);
}
$bloodrhesusid = "";
if (isset($prm['bloodrhesusid'])) {
$bloodrhesusid = trim($prm["bloodrhesusid"]);
}
$id = "";
if (isset($prm['id'])) {
$id = trim($prm["id"]);
}
$sql = "UPDATE one_bb.td_baginventory
SET Td_BagInventoryDate = NOW(),
Td_BagInventoryTd_SourceID = ?,
Td_BagInventoryNumber = ?,
Td_BagInventoryVolume = ?,
Td_BagInventoryExpDate = ?,
Td_BagInventoryTd_BloodTypeID = ?,
Td_BagInventoryTd_BloodRhesusID = ?,
Td_BagInventoryUsedUserID = ?,
Td_BagInventoryLastUpdated = NOW()
WHERE Td_BagInventoryID = ?";
$qry = $this->db->query($sql, array(
$sourceid,
$number,
$volume,
$expdate,
$bloodtypeid,
$bloodrhesusid,
$userid,
$id
));
if (!$qry) {
$this->db->trans_rollback();
$this->sys_error_db("update baginventory error", $this->db);
exit;
}
$this->db->trans_commit();
$result = array(
"total" => 1,
"affected_rows" => $this->db->affected_rows()
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function delete()
{
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$this->db->trans_begin();
$prm = $this->sys_input;
$userid = $this->sys_user['M_UserID'];
$id = "";
if (isset($prm['id'])) {
$id = trim($prm["id"]);
}
$sql = "UPDATE one_bb.td_baginventory
SET Td_BagInventoryUsedUserID = ?,
Td_BagInventoryIsActive = 'N',
Td_BagInventoryLastUpdated = NOW()
WHERE Td_BagInventoryID = ?";
$qry = $this->db->query($sql, array($userid, $id));
$last_qry = $this->db->last_query();
if (!$qry) {
$this->db->trans_rollback();
$error = array(
"message" => $this->db->error()["message"],
"sql" => $last_qry
);
$this->sys_error_db($error, $this->db);
exit;
}
$this->db->trans_commit();
$result = array(
"affected_rows" => $this->db->affected_rows()
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}
?>