556 lines
17 KiB
PHP
556 lines
17 KiB
PHP
<?php
|
|
|
|
class Itemunitconvert extends MY_Controller
|
|
{
|
|
var $db_inventory;
|
|
var $db_inventory_log;
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->db_inventory = $this->load->database("inventory", true);
|
|
$this->db_inventory_log = $this->load->database('inventory_log', true);
|
|
}
|
|
|
|
function index()
|
|
{
|
|
echo "Api: Training Playground";
|
|
|
|
}
|
|
|
|
function search()
|
|
{
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}else
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
$search = "";
|
|
if(isset($prm["search"])) {
|
|
$search = trim($prm["search"]);
|
|
if ($search != "") {
|
|
$search = "%" . $prm["search"] . "%";
|
|
}else{
|
|
$search = "%%";
|
|
}
|
|
}
|
|
|
|
$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 itemunitconvert
|
|
JOIN item ON ItemUnitConvertItemID = ItemID
|
|
AND ItemUnitConvertIsActive = 'Y'
|
|
AND ItemName LIKE ?
|
|
JOIN itemunit AS fromitemunit ON ItemUnitConvertFromItemUnitID = fromitemunit.ItemUnitID
|
|
JOIN itemunit AS toitemunit ON ItemUnitConvertToItemUnitID = toitemunit.ItemUnitID";
|
|
|
|
$qry_filter = $this->db_inventory->query($sql_filter, [$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("itemunitconvert count error", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
$sql = "SELECT DISTINCT
|
|
ItemUnitConvertID AS id,
|
|
ItemID,
|
|
ItemName,
|
|
fromitemunit.ItemUnitID AS FromItemUnitID,
|
|
fromitemunit.ItemUnitName AS FromItemUnitName,
|
|
toitemunit.ItemUnitID AS ToItemUnitID,
|
|
toitemunit.ItemUnitName AS ToItemUnitName,
|
|
ItemUnitConvertAmount AS Amount
|
|
FROM itemunitconvert
|
|
JOIN item ON ItemUnitConvertItemID = ItemID
|
|
AND ItemUnitConvertIsActive = 'Y'
|
|
AND ItemName LIKE ?
|
|
JOIN itemunit AS fromitemunit ON ItemUnitConvertFromItemUnitID = fromitemunit.ItemUnitID
|
|
JOIN itemunit AS toitemunit ON ItemUnitConvertToItemUnitID = toitemunit.ItemUnitID
|
|
$q_sort
|
|
LIMIT ? offset ?";
|
|
|
|
$qry = $this->db_inventory->query($sql, array($search, $number_limit, $number_offset));
|
|
// print_r($rows = $qry->result_array());
|
|
if($qry){
|
|
$rows = $qry->result_array();
|
|
}else{
|
|
$this->sys_error_db("Itemunitconvert select error", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
$result = array(
|
|
"total_page" => $tot_page,
|
|
"total_filter" => $tot_count,
|
|
"records" => $rows
|
|
);
|
|
$this->sys_ok($result);
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function searchitemx()
|
|
{
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$sql = "SELECT DISTINCT ItemID as id_item,
|
|
ItemName as name_item
|
|
FROM item
|
|
JOIN itemunitmap ON ItemID = itemUnitMapItemID
|
|
WHERE ItemIsActive = 'Y'";
|
|
|
|
$query = $this->db_inventory->query($sql);
|
|
|
|
$rows = $query->result_array();
|
|
if (!$query) {
|
|
$this->db_inventory->trans_rollback();
|
|
$error = array(
|
|
"message" => $this->db_inventory->error()["message"]
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$result = array(
|
|
"records" => $rows
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
|
|
}
|
|
|
|
// konversi awal
|
|
function fromitemunit()
|
|
{
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
$id = "";
|
|
if (isset($prm['id'])) {
|
|
$id = trim($prm["id"]);
|
|
}
|
|
|
|
$sql = "SELECT ItemUnitID as id_fromitemunit,
|
|
ItemUnitName as name_fromitemunit,
|
|
ItemID as id_item,
|
|
ItemName as name_item
|
|
FROM itemunit
|
|
JOIN itemunitmap ON ItemUnitID = itemUnitMapItemUnitID
|
|
JOIN item ON itemUnitMapItemID = ItemID
|
|
AND ItemUnitIsActive = 'Y' AND ItemID = ?";
|
|
|
|
$qry = $this->db_inventory->query($sql, array($id));
|
|
if(!$qry) {
|
|
$this->db_inventory->trans_rollback();
|
|
$error = array(
|
|
"message" => $this->db_inventory->error()["message"]
|
|
);
|
|
$this->sys_error_db($error);
|
|
exit;
|
|
}
|
|
$rows = $qry->result_array();
|
|
|
|
$result = array(
|
|
"records" => $rows
|
|
);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
// konversi akhir
|
|
function toitemunit()
|
|
{
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
$id = "";
|
|
if (isset($prm['id'])) {
|
|
$id = trim($prm["id"]);
|
|
}
|
|
|
|
$sql = "SELECT ItemUnitID as id_toitemunit,
|
|
ItemUnitName as name_toitemunit,
|
|
ItemID as id_item,
|
|
ItemName as name_item
|
|
FROM itemunit
|
|
JOIN itemunitmap ON ItemUnitID = itemUnitMapItemUnitID
|
|
JOIN item ON itemUnitMapItemID = ItemID
|
|
AND ItemUnitIsActive = 'Y' AND ItemID = ?";
|
|
|
|
$qry = $this->db_inventory->query($sql, array($id));
|
|
if(!$qry) {
|
|
$this->db_inventory->trans_rollback();
|
|
$error = array(
|
|
"message" => $this->db_inventory->error()["message"]
|
|
);
|
|
$this->sys_error_db($error);
|
|
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 save()
|
|
{
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$this->db_inventory->trans_begin();
|
|
$prm = $this->sys_input;
|
|
$userid = $this->sys_user["M_UserID"];
|
|
|
|
// convert
|
|
$id_item = 0;
|
|
$id_fromitemunit = 0;
|
|
$id_toitemunit = 0;
|
|
$amount = 0;
|
|
|
|
if(isset($prm['id_item'])){
|
|
$id_item = trim($prm['id_item']);
|
|
}
|
|
|
|
if(isset($prm['id_fromitemunit'])){
|
|
$id_fromitemunit = trim($prm['id_fromitemunit']);
|
|
}
|
|
|
|
if(isset($prm['id_toitemunit'])){
|
|
$id_toitemunit = trim($prm['id_toitemunit']);
|
|
}
|
|
|
|
if(isset($prm['amount'])){
|
|
$amount = trim($prm['amount']);
|
|
}
|
|
|
|
// sql insert
|
|
$sql = "INSERT INTO itemunitconvert(
|
|
ItemUnitConvertItemID,
|
|
ItemUnitConvertFromItemUnitID,
|
|
ItemUnitConvertToItemUnitID,
|
|
ItemUnitConvertAmount,
|
|
ItemUnitConvertCreated,
|
|
ItemUnitConvertLastUpdated,
|
|
ItemUnitConvertUserID)
|
|
VALUES (?, ?, ?, ?, NOW(), NOW(), ?)";
|
|
|
|
$qry = $this->db_inventory->query($sql, [
|
|
$id_item,
|
|
$id_fromitemunit,
|
|
$id_toitemunit,
|
|
$amount,
|
|
$userid
|
|
]);
|
|
|
|
if(!$qry){
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("Itemunitconvert insert", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
$insert_id = $this->db_inventory->insert_id();
|
|
|
|
$sql_json_before = "SELECT *
|
|
FROM itemunitconvert
|
|
WHERE ItemUnitConvertIsActive = 'Y'
|
|
AND ItemUnitConvertID = ?";
|
|
|
|
$qry_json_before = $this->db_inventory->query($sql_json_before, [
|
|
$insert_id
|
|
]);
|
|
|
|
if (!$qry_json_before) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("itemunitconvert select json", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
$data_by_id = $qry_json_before->row();
|
|
|
|
$json_after_log = json_encode($data_by_id);
|
|
|
|
$sql_insert_log = "INSERT INTO itemunitconvert_log(
|
|
ItemUnitConvertLogStatus,
|
|
ItemUnitConvertLogItemUnitConvertID,
|
|
ItemUnitConvertLogJSONBefore,
|
|
ItemUnitConvertLogJSONAfter,
|
|
ItemUnitConvertLogUserID,
|
|
ItemUnitConvertLogCreated
|
|
) VALUES('ADD',?,NULL,?,?,NOW())";
|
|
|
|
$qry_insert_log = $this->db_inventory_log->query($sql_insert_log, [
|
|
$insert_id,
|
|
$json_after_log,
|
|
$userid
|
|
]);
|
|
|
|
if (!$qry_insert_log) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("insert log error", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
$this->db_inventory->trans_commit();
|
|
$result = array(
|
|
"total" => 1,
|
|
"records" => array("xid" => 0)
|
|
);
|
|
|
|
$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;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$userid = $this->sys_user['M_UserID'];
|
|
$id = $prm["id"];
|
|
|
|
$sql_data = "UPDATE itemunitconvert
|
|
SET ItemUnitConvertItemID = ?,
|
|
ItemUnitConvertFromItemUnitID = ?,
|
|
ItemUnitConvertToItemUnitID = ?,
|
|
ItemUnitConvertAmount = ?,
|
|
ItemUnitConvertLastUpdated = NOW(),
|
|
ItemUnitConvertUserID = ?
|
|
WHERE ItemUnitConvertID = ?";
|
|
|
|
$qry_data = $this->db_inventory->query($sql_data, [
|
|
$prm["id_item"],
|
|
$prm["id_fromitemunit"],
|
|
$prm["id_toitemunit"],
|
|
$prm["amount"],
|
|
$userid,
|
|
$id
|
|
]);
|
|
|
|
if(!$qry_data){
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("Itemunitconvert update", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
// json before
|
|
$sql_json_before = "SELECT *
|
|
FROM itemunitconvert
|
|
WHERE ItemUnitConvertIsActive = 'Y'
|
|
AND ItemUnitConvertID = ?";
|
|
|
|
$qry_json_before = $this->db_inventory->query($sql_json_before, [
|
|
$id
|
|
]);
|
|
|
|
if (!$qry_json_before) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("itemunitconvert select json before, $this->db_inventory");
|
|
exit;
|
|
}
|
|
|
|
$data_before_by_id = $qry_json_before->row();
|
|
|
|
$json_before = json_encode($data_before_by_id);
|
|
|
|
// json after
|
|
$sql_json_after = "SELECT *
|
|
FROM itemunitconvert
|
|
WHERE ItemUnitConvertIsActive = 'Y'
|
|
AND ItemUnitConvertID = ?";
|
|
|
|
$qry_json_after = $this->db_inventory->query($sql_json_after, [
|
|
$id
|
|
]);
|
|
|
|
if (!$qry_json_after) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("itemunitconvert select json after, $this->db_inventory");
|
|
exit;
|
|
}
|
|
|
|
$data_after_by_id = $qry_json_after->row();
|
|
|
|
$json_after = json_encode($data_after_by_id);
|
|
|
|
$sql_insert_log = "INSERT INTO itemunitconvert_log(
|
|
ItemUnitConvertLogStatus,
|
|
ItemUnitConvertLogItemUnitConvertID,
|
|
ItemUnitConvertLogJSONBefore,
|
|
ItemUnitConvertLogJSONAfter,
|
|
ItemUnitConvertLogUserID,
|
|
ItemUnitConvertLogCreated
|
|
) VALUES('EDIT',?,?,?,?,NOW())";
|
|
|
|
$qry_insert_log = $this->db_inventory_log->query($sql_insert_log, [
|
|
$id,
|
|
$json_before,
|
|
$json_after,
|
|
$userid
|
|
]);
|
|
|
|
if (!$qry_insert_log) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("update log error, $this->db_inventory");
|
|
exit;
|
|
}
|
|
|
|
$this->db_inventory->trans_commit();
|
|
$result = array(
|
|
"total" => 1,
|
|
"records" => array("xid" => 0)
|
|
);
|
|
|
|
$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_inventory->trans_begin();
|
|
$prm = $this->sys_input;
|
|
$userid = $this->sys_user['M_UserID'];
|
|
$id = $prm["id"];
|
|
|
|
$sql_data = "UPDATE itemunitconvert
|
|
SET ItemUnitConvertIsActive = 'N',
|
|
ItemUnitConvertLastUpdated = NOW(),
|
|
ItemUnitConvertUserID = ?
|
|
WHERE ItemUnitConvertID = ?";
|
|
|
|
$qry_data = $this->db_inventory->query($sql_data, [
|
|
$userid,
|
|
$id
|
|
]);
|
|
|
|
if(!$qry_data){
|
|
$this->db_inventory->trans_commit();
|
|
$this->sys_error_db("itemunitconvert delete", $this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
// json before
|
|
$sql_json_before = "SELECT *
|
|
FROM itemunitconvert
|
|
WHERE ItemUnitConvertIsActive = 'N'
|
|
AND ItemUnitConvertID = ?";
|
|
|
|
$qry_json_before = $this->db_inventory->query($sql_json_before, [
|
|
$id
|
|
]);
|
|
|
|
if (!$qry_json_before) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("itemunitconvert select json, $this->db_inventory");
|
|
exit;
|
|
}
|
|
|
|
$data_before_by_id = $qry_json_before->row();
|
|
|
|
$json_before = json_encode($data_before_by_id);
|
|
|
|
$sql_insert_log = "INSERT INTO itemunitconvert_log(
|
|
ItemUnitConvertLogStatus,
|
|
ItemUnitConvertLogItemUnitConvertID,
|
|
ItemUnitConvertLogJSONBefore,
|
|
ItemUnitConvertLogJSONAfter,
|
|
ItemUnitConvertLogUserID,
|
|
ItemUnitConvertLogCreated
|
|
) VALUES('DELETE',?,NULL,?,?,NOW())";
|
|
|
|
$qry_insert_log = $this->db_inventory_log->query($sql_insert_log, [
|
|
$id,
|
|
$json_before,
|
|
$userid
|
|
]);
|
|
|
|
if (!$qry_insert_log) {
|
|
$this->db_inventory->trans_rollback();
|
|
$this->sys_error_db("delete log error, $this->db_inventory");
|
|
exit;
|
|
}
|
|
|
|
$this->db_inventory->trans_commit();
|
|
$result = array(
|
|
"total" => 1,
|
|
"records" => array("xid" => 0)
|
|
);
|
|
|
|
$this->sys_ok($result);
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
}
|
|
?>
|