Files
2026-04-15 15:23:57 +07:00

1420 lines
57 KiB
PHP

<?php
class Receive extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->db_inventory = $this->load->database("inventory", true);
$this->db_inventory_log = $this->load->database('inventory_log', true);
$this->db_onex = 'one_aditya';
}
function search()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$username = $this->sys_user['M_UserUsername'];
$search = "%%";
if (isset($prm['search']) && trim($prm["search"]) != '') {
$search = trim($prm["search"]);
$search = '%' . $prm['search'] . '%';
}
$start_date = date("Y-m-d");
if (isset($prm['start_date'])) {
$start_date = $prm["start_date"];
}
$end_date = date("Y-m-d");
if (isset($prm['end_date'])) {
$end_date = $prm["end_date"];
}
$filter_supplier = '';
if (isset($prm['supplier_id']) && intval($prm['supplier_id']) > 0) {
$filter_supplier = ' AND SupplierID = '.$prm['supplier_id'];
}
$order_by = "PurchaseReceiveNumber";
if (isset($prm['order_by'])) {
$order_by = trim($prm["order_by"]);
}
$order_type = "asc";
if (isset($prm['order_type'])) {
$order_type = trim($prm["order_type"]);
}
$order = $order_by.' '.$order_type;
$perpage = 10;
$offset = ($prm['current_page'] - 1) * $perpage ;
$sql = "SELECT COUNT(*) as total FROM
(
SELECT PurchaseReceiveID
FROM `purchasereceive`
JOIN `warehouse` ON PurchaseReceiveWarehouseID = WarehouseID
JOIN purchaseorder ON PurchaseOrderID = PurchaseReceivePurchaseOrderID
JOIN supplier ON SupplierID = PurchaseOrderSupplierID $filter_supplier
WHERE
PurchaseReceiveIsActive = 'Y' AND (PurchaseReceiveDate BETWEEN ? AND ?) AND
( PurchaseReceiveNumber like ? )
) x";
$qry = $this->db_inventory->query($sql,array($start_date, $end_date, $search));
$tot_count = 0;
$tot_page = 0;
if ($qry) {
$tot_count = $qry->row()->total;
$tot_page = ceil($tot_count/$perpage);
} else {
$this->sys_error_db("purchase order count error", $this->db_inventory->last_query());
exit;
}
$rows = array();
$sql = "SELECT PurchaseReceiveID as id,
PurchaseReceiveWarehouseID as warehouse_id,
WarehouseName as warehouse_name,
PurchaseReceivePurchaseOrderID as po_id,
PurchaseOrderNumber as po_number,
SupplierID as supplier_id,
SupplierName as supplier_name,
PurchaseReceiveNumber as numbering,
DATE_FORMAT(PurchaseReceiveDate,'%d-%m-%Y') as purchase_date,
IFNULL(M_UserUsername,'') as user_receive,
PurchaseReceiveIsConfirm as is_confirm,
IFNULL(M_UserUsername,'') as user_confirm,
IF(PurchaseReceiveConfirmDatetime = NULL ,'-',DATE_FORMAT(PurchaseReceiveConfirmDatetime,'%d-%m-%Y %H:%i')) as date_confirm,
PurchaseReceiveNote as note,
'' as details,
replace(replace(replace(R_ReportUrl,'val_1',PurchaseReceiveID),'val_user','{$username}'),'val_tm',DATE_FORMAT(now(),'%d%m%Y%h%m%s')) as report_url
FROM `purchasereceive`
JOIN `warehouse` ON PurchaseReceiveWarehouseID = WarehouseID
JOIN purchaseorder ON PurchaseOrderID = PurchaseReceivePurchaseOrderID
JOIN supplier ON SupplierID = PurchaseOrderSupplierID $filter_supplier
LEFT JOIN $this->db_onex.m_user ON PurchaseReceiveConfirmBy = M_UserID
LEFT JOIN r_report ON R_ReportCode = 'PO' AND R_ReportIsActive = 'Y'
WHERE
PurchaseReceiveIsActive = 'Y' AND (PurchaseReceiveDate BETWEEN ? AND ?) AND
( PurchaseReceiveNumber like ? )
ORDER BY $order_by $order_type
LIMIT ? OFFSET ?";
$qry = $this->db_inventory->query($sql, array($start_date, $end_date, $search, $perpage, $offset));
// echo $this->db_inventory->last_query();
if($qry){
$rows = $qry->result_array();
if(count($rows) > 0){
foreach ($rows as $key => $value) {
$sql = "SELECT PurchaseReceiveDetailID as id,
PurchaseOrderNumber as po_number,
SupplierName as supplier_name,
PurchaseReceiveWarehouseID as warehouse_id,
WarehouseName as warehouse_name,
PurchaseOrderDetailItemID as item_id,
ItemName as item_name,
ItemSKU as item_sku,
PurchaseOrderDetailItemUnitID as unit_id,
ItemUnitName as unit_name,
PurchaseReceiveDetailBatchNo as batch_no,
DATE_FORMAT(PurchaseReceiveDetailED,'%d-%m-%Y') as ed,
PurchaseReceiveDetailStockNumber as stock_number,
PurchaseOrderDetailQty as po_qty,
PurchaseReceiveDetailQty as receive_qty,
PurchaseReceiveDetailQty as qty,
'N' as is_exist,
PurchaseOrderDetailAmount as price,
PurchaseOrderDetailDiscount as discount_percent,
PurchaseOrderDetailDiscountRp as discount_rp,
PurchaseOrderDetailTotal as total,
'N' as selected
FROM `purchasereceivedetail`
JOIN purchaseorderdetail ON PurchaseOrderDetailID = PurchaseReceiveDetailPurchaseOrderDetailID
JOIN purchaseorder ON PurchaseOrderID = PurchaseOrderDetailPurchaseOrderID
JOIN purchasereceive ON PurchaseReceiveID = PurchaseReceiveDetailPurchaseReceiveID
JOIN `warehouse` ON PurchaseReceiveWarehouseID = WarehouseID
JOIN `item` ON PurchaseOrderDetailItemID = ItemID
LEFT JOIN itemunit ON PurchaseOrderDetailItemUnitID = ItemUnitID
JOIN supplier ON SupplierID = PurchaseOrderSupplierID
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ? AND
PurchaseReceiveDetailIsActive = 'Y'
GROUP BY PurchaseReceiveDetailID";
$qry = $this->db_inventory->query($sql, array($value['id']));
//echo $this->db_inventory->last_query();
if($qry){
$rows[$key]['details'] = $qry->result_array();
}
}
}
}else {
//echo $this->db_inventory->last_query();
$this->sys_error_db("purchase order data error", $this->db_inventory->last_query());
exit;
}
$result = array("total_page" => $tot_page, "records" => $rows);
$this->sys_ok($result);
exit;
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}
function save(){
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$warehouseid = $prm["warehouseid"];
$poid = $prm["poid"];
$sdate = $prm["sdate"];
$note = $prm["note"];
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$numbering = '';
$sql = "SELECT `fn_numbering`('PR') as numbering";
$qry = $this->db_inventory->query($sql);
if($qry){
$numbering = $qry->row()->numbering;
}
else{
$this->sys_error_db("get numbering error", $this->db_inventory->last_query());
exit;
}
$last_id = 0;
$sql = "INSERT INTO purchasereceive(
PurchaseReceiveNumber,
PurchaseReceiveDate,
PurchaseReceiveCreatedBy,
PurchaseReceiveWarehouseID,
PurchaseReceivePurchaseOrderID,
PurchaseReceiveNote,
PurchaseReceiveCreated,
PurchaseReceiveLastUpdated,
PurchaseReceiveUserID)
VALUES(
?,?,?,?,?,?,NOW(),NOW(),?)";
$qry = $this->db_inventory->query($sql, array($numbering,$sdate,$userid,$warehouseid,$poid,$note,$userid));
$last_id = 0;
// echo $this->db_inventory->last_query();
if($qry){
$last_id = $this->db_inventory->insert_id();
}
else{
$this->sys_error_db("save purchasereceive error", $this->db_inventory->last_query());
exit;
}
foreach ($prm['details'] as $key => $value) {
$qty = $value['qty'];
$id = $value['id'];
$po_qty = $value['po_qty'];
$batch_no = $value['batch_no'];
$almariid = $value['almariid'];
$rackid = $value['rackid'];
$ed = isset($value['ed'])&&$value['ed'] != ''?substr($value['ed'],4,4).'-'.substr($value['ed'],2,2).'-'.substr($value['ed'],0,2):NULL;
if($qty > 0){
$sql = "INSERT INTO purchasereceivedetail(
PurchaseReceiveDetailPurchaseReceiveID,
PurchaseReceiveDetailPurchaseOrderDetailID,
PurchaseReceiveDetailQtyPO,
PurchaseReceiveDetailQty,
PurchaseReceiveDetailBatchNo,
PurchaseReceiveDetailED,
PurchaseReceiveDetailWarehouseAlmariID,
PurchaseReceiveDetailWarehouseRackID,
PurchaseReceiveDetailCreated,
PurchaseReceiveDetailLastUpdated,
PurchaseReceiveDetailUserID)
VALUES(
?,?,?,?,?,?,?,?,NOW(),NOW(),?)";
$qry = $this->db_inventory->query($sql, array($last_id, $id, $po_qty, $qty,$batch_no,$ed,$almariid,$rackid,$userid));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("save purchasereceive detail error", $this->db_inventory->last_query());
exit;
}
}
}
$id = $last_id;
$sql = "SELECT purchasereceive.*, '' as details
FROM purchasereceive
WHERE PurchaseReceiveID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select purchasereceive error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT *
FROM `purchasereceivedetail`
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ? AND
PurchaseReceiveDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if($qry){
$row_after['details'] = $qry->result_array();
}else{
$this->sys_error_db("select purchasereceive details error", $this->db_inventory->last_query());
exit;
}
$data_log_after = $row_after;
$sql = "INSERT INTO purchasereceive_log (
PurchaseReceiveLogPurchaseReceiveID,
PurchaseReceiveLogStatus,
PurchaseReceiveLogJSONBefore,
PurchaseReceiveLogJSONAfter,
PurchaseReceiveLogUserID,
PurchaseReceiveLogCreated
)
VALUES(
?,?,?,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'ADD',NULL,json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
$result = array(
"message" => ''
);
$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'];
$this->update($prm,$userid);
$result = array(
"message" => ''
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function update($prm,$userid){
$userid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$warehouseid = $prm["warehouseid"];
$poid = $prm["poid"];
$sdate = $prm["sdate"];
$id = $prm["id"];
$note = $prm["note"];
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$sql = "UPDATE purchasereceive SET
PurchaseReceiveNote = ?,
PurchaseReceiveLastUpdated = now(),
PurchaseReceiveUserID = ?
WHERE PurchaseReceiveID = ?
";
$qry = $this->db_inventory->query($sql, array($note, $userid, $id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("update purchasereceive detail error", $this->db_inventory->last_query());
exit;
}
foreach ($prm['details'] as $key => $value) {
$qty = $value['qty'];
$detailid = $value['id'];
$po_qty = $value['po_qty'];
$batch_no = $value['batch_no'];
$almariid = $value['almariid'];
$rackid = $value['rackid'];
$ed = isset($value['ed'])&&$value['ed'] != ''?substr($value['ed'],4,4).'-'.substr($value['ed'],2,2).'-'.substr($value['ed'],0,2):NULL;
if($qty > 0){
$sql = "UPDATE purchasereceivedetail SET
PurchaseReceiveDetailQty = ?,
PurchaseReceiveDetailBatchNo = ?,
PurchaseReceiveDetailED = ?,
PurchaseReceiveDetailWarehouseAlmariID = ?,
PurchaseReceiveDetailWarehouseRackID = ?,
PurchaseReceiveDetailLastUpdated = now(),
PurchaseReceiveDetailUserID = ?
WHERE PurchaseReceiveDetailID = ?
";
$qry = $this->db_inventory->query($sql, array($qty, $batch_no, $ed, $almariid, $rackid,$userid, $detailid));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("update purchasereceive detail error", $this->db_inventory->last_query());
exit;
}
}
}
$sql = "SELECT purchasereceive.*, '' as details
FROM purchasereceive
WHERE PurchaseReceiveID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select purchasereceive error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT *
FROM `purchasereceivedetail`
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ? AND
PurchaseReceiveDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if($qry){
$row_after['details'] = $qry->result_array();
}else{
$this->sys_error_db("select purchasereceive details error", $this->db_inventory->last_query());
exit;
}
$data_log_after = $row_after;
$sql = "INSERT INTO purchasereceive_log (
PurchaseReceiveLogPurchaseReceiveID,
PurchaseReceiveLogStatus,
PurchaseReceiveLogJSONBefore,
PurchaseReceiveLogJSONAfter,
PurchaseReceiveLogUserID,
PurchaseReceiveLogCreated
)
VALUES(
?,?,?,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'EDIT',NULL,json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
return true;
}
function confirm()
{
$userid = $this->sys_user['M_UserID'];
$param = $this->sys_input;
$this->edit($param,$userid);
$this->update_po_detail($param,$userid);
$this->update_stock($param,$userid);
$id = "";
if (isset($param['id'])) {
$id = intval($param["id"]);
}
if ($id == "" || !$id) {
$error = array(
"message" => "id is mandatory",
);
$this->sys_error_db($error);
exit;
}
$sql = "SELECT purchasereceive.*, '' as details
FROM purchasereceive
WHERE PurchaseReceiveID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select purchasereceive error", $this->db_inventory->last_query());
exit;
}
$row = $qry->row_array();
$sql = "SELECT *
FROM `purchasereceivedetail`
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ? AND
PurchaseReceiveDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if($qry){
$row['details'] = $qry->result_array();
}else{
$this->sys_error_db("select purchasereceive details error", $this->db_inventory->last_query());
exit;
}
$data_log_before = $row;
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$sql = "UPDATE purchasereceive
SET PurchaseReceiveIsConfirm = 'Y',
PurchaseReceiveLastUpdated = NOW(),
PurchaseReceiveConfirmDatetime = NOW(),
PurchaseReceiveConfirmBy = ?,
PurchaseReceiveUserID = ?
WHERE PurchaseReceiveID = ?
";
$qry = $this->db_inventory->query($sql, [$userid,$userid,$id]);
if(!$qry){
$this->sys_error_db("confirm purchasereceive error", $this->db_inventory->last_query());
exit;
}
$sql = "SELECT purchasereceive.*, '' as details
FROM purchasereceive
WHERE PurchaseReceiveID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select purchasereceive error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT *
FROM `purchasereceivedetail`
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ? AND
PurchaseReceiveDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if($qry){
$row_after['details'] = $qry->result_array();
}else{
$this->sys_error_db("select purchasereceive details error", $this->db_inventory->last_query());
exit;
}
$data_log_after = $row_after;
$sql = "INSERT INTO purchasereceive_log (
PurchaseReceiveLogPurchaseReceiveID,
PurchaseReceiveLogStatus,
PurchaseReceiveLogJSONBefore,
PurchaseReceiveLogJSONAfter,
PurchaseReceiveLogUserID,
PurchaseReceiveLogCreated
)
VALUES(
?,?,?,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'CONFIRM',json_decode($data_log_before),json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
return true;
}
function update_po_detail($prm,$userid){
$userid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$warehouseid = $prm["warehouseid"];
$poid = $prm["poid"];
$sdate = $prm["sdate"];
$id = $prm["id"];
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
foreach ($prm['details'] as $key => $value) {
$qty = $value['qty'];
$detailid = $value['podetailid'];
$batch_no = $value['batch_no'];
$ed = isset($value['ed'])&&$value['ed'] != ''?substr($value['ed'],4,4).'-'.substr($value['ed'],2,2).'-'.substr($value['ed'],0,2):NULL;
if($qty > 0){
$sqty =
$sql = "UPDATE purchaseorderdetail SET
PurchaseOrderDetailStatus = IF(PurchaseOrderDetailReceiveQty+$qty = PurchaseOrderDetailQty,'D','P'),
PurchaseOrderDetailReceiveQty = PurchaseOrderDetailReceiveQty+$qty
WHERE PurchaseOrderDetailID = ?";
$qry = $this->db_inventory->query($sql, array($detailid));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("update purchaseorderdetail detail error", $this->db_inventory->last_query());
exit;
}
}
}
$x = $this->db_inventory->query("SELECT SUM(PurchaseOrderDetailQty) as qty FROM
purchaseorderdetail
WHERE PurchaseOrderDetailPurchaseOrderID = $poid AND PurchaseOrderDetailIsActive = 'Y'")->row();
$poqty = $x->qty;
$y = $this->db_inventory->query("SELECT SUM(PurchaseOrderDetailReceiveQty) as qty FROM
purchaseorderdetail
WHERE PurchaseOrderDetailPurchaseOrderID = $poid AND PurchaseOrderDetailIsActive = 'Y'")->row();
$receiveqty = $y->qty;
$is_status = 'N';
if($receiveqty > 0 && $receiveqty < $poqty){
$is_status = 'P';
}elseif($receiveqty > 0 && $receiveqty = $poqty){
$is_status = 'D';
}
$sql = "UPDATE purchaseorder SET
PurchaseOrderStatus = '{$is_status}'
WHERE PurchaseOrderID = ?
";
$qry = $this->db_inventory->query($sql, array($poid));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("update purchaseorder error", $this->db_inventory->last_query());
exit;
}
$this->db_inventory->trans_complete();
return true;
}
function update_stock($prm,$userid){
$userid = $this->sys_user['M_UserID'];
$prm = $this->sys_input;
$warehouseid = $prm["warehouseid"];
$poid = $prm["poid"];
$sdate = $prm["sdate"];
$id = $prm["id"];
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
foreach ($prm['details'] as $key => $value) {
$qty = $value['qty'];
$detailid = $value['id'];
$item_id = $value['item_id'];
$item_sku = $value['item_sku'];
$unit_id = $value['unit_id'];
$batch_no = $value['batch_no'];
$almariid = $value['almariid'];
$rackid = $value['rackid'];
$ed = isset($value['ed'])&&$value['ed'] != ''?substr($value['ed'],4,4).'-'.substr($value['ed'],2,2).'-'.substr($value['ed'],0,2):NULL;
$stok_number = '';
$stok_qty = '';
if($qty > 0){
$s_number = $this->db_inventory->query("SELECT `fn_numbering_stock`('{$item_sku}') as stok_number")->row();
$stok_number = $s_number->stok_number;
$sql = "UPDATE purchasereceivedetail SET
PurchaseReceiveDetailStockNumber = '{$stok_number}'
WHERE PurchaseReceiveDetailID = $detailid
";
$qry = $this->db_inventory->query($sql);
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("update purchaseorderdetail detail error", $this->db_inventory->last_query());
exit;
}
//echo $stok_number;
$arr_insert_stock = array(
'StockWarehouseID' => $warehouseid,
'StockWarehouseAlmariID' => $almariid,
'StockWarehouseRackID' => $rackid,
'StockStockNumber' => $stok_number,
'StockItemID' => $item_id,
'StockItemUnitID' => $unit_id,
'StockBatchNo' => $batch_no,
'StockQty' => $qty,
'StockLastUpdated' => date("Y-m-d H:i:s"),
'StockUserID' => $userid
);
$valid_ed = $this->validateDate($ed)?$ed:NULL;
if($valid_ed && $ed != '0000-00-00')
$arr_insert_stock['StockED'] = $ed;
$qry = $this->db_inventory->insert('stock', $arr_insert_stock);
//echo $this->db_inventory->last_query();
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("save mutasidetail error", $this->db_inventory->last_query());
exit;
}
//start insert stockcard
$select_ed_card = "";
if($valid_ed && $ed != '0000-00-00')
$select_ed_card = " AND StockCardED = '{$ed}'";
$sql = "SELECT *
FROM stockcard
WHERE
StockCardWarehouseID = ? AND
StockCardItemID = ? AND
StockCardItemUnitID = ? AND
StockCardBatchNo = ? $select_ed_card
ORDER BY StockCardDatetime DESC
LIMIT 1";
$qry = $this->db_inventory->query($sql,array(
$warehouseid,
$item_id,
$unit_id,
$batch_no
));
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("save stockcard error", $this->db_inventory->last_query());
exit;
}
$last_qty = 0;
if($qry->row_array()){
$last_qty = $qry->row_array()['StockCardAfter'];
}
$arr_insert_stockcard = array(
'StockCardWarehouseID' => $warehouseid,
'StockCardItemID' => $item_id,
'StockCardItemUnitID' => $unit_id,
'StockCardBatchNo' => $batch_no,
'StockCardBefore' => $last_qty,
'StockCardIn' => $qty,
'StockCardOut' => 0,
'StockCardStatus' => 'PRV',
'StockCardReffID' => $detailid,
'StockCardAfter' => $last_qty+intval($qty),
'StockCardDatetime' => date("Y-m-d H:i:s"),
'StockCardUserID' => $userid
);
if($valid_ed && $ed != '0000-00-00')
$arr_insert_stockcard['StockCardED'] = $ed;
$qry = $this->db_inventory->insert('stockcard', $arr_insert_stockcard);
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("insert stockcard error", $this->db_inventory->last_query());
exit;
}
//end insert stockcard
//start insert stocklog
$arr_insert_stock_log = array(
'StockLogWarehouseID' => $warehouseid,
'StockLogWarehouseAlmariID' => $almariid,
'StockLogWarehouseRackID' => $rackid,
'StockLogStockNumber' => $stok_number,
'StockLogItemID' => $item_id,
'StockLogItemUnitID' => $unit_id,
'StockLogBatchNo' => $batch_no,
'StockLogQty' => $qty,
'StockLogDatetime' => date("Y-m-d H:i:s"),
'StockLogUserID' => $userid,
'StockLogReffID' => $detailid,
'StockLogStatus' => 'PRV'
);
if($valid_ed && $ed != '0000-00-00')
$arr_insert_stock_log['StockLogED'] = $ed;
$qry = $this->db_inventory->insert('stocklog', $arr_insert_stock_log);
if(!$qry){
//echo $this->db_inventory->last_query();
$this->sys_error_db("save stocklog error", $this->db_inventory->last_query());
exit;
}
//end insert stocklog
}
}
$this->db_inventory->trans_complete();
return true;
}
function delete()
{
try {
if (!$this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$userid = $this->sys_user['M_UserID'];
$param = $this->sys_input;
$id = "";
if (isset($param['id'])) {
$id = intval($param["id"]);
}
if ($id == "" || !$id) {
$error = array(
"message" => "id is mandatory",
);
$this->sys_error_db($error);
exit;
}
$this->db_inventory->trans_start();
$this->db_inventory->trans_strict(FALSE);
$sql = "UPDATE purchasereceive
SET PurchaseReceiveIsActive = 'N',
PurchaseReceiveLastUpdated = now(),
PurchaseReceiveUserID = ?
WHERE PurchaseReceiveID = ?
";
$qry = $this->db_inventory->query($sql, [$userid,$id]);
if(!$qry){
$this->sys_error_db("delete purchasereceive error", $this->db_inventory->last_query());
exit;
}else{
$sql = "UPDATE purchasereceivedetail
SET PurchaseReceiveDetailIsActive = 'N',
PurchaseReceiveDetailLastUpdated = now(),
PurchaseReceiveDetailUserID = ?
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ?
";
$qry = $this->db_inventory->query($sql, [$userid,$id]);
if(!$qry){
$this->sys_error_db("delete supplier address error", $this->db_inventory->last_query());
exit;
}
}
$sql = "SELECT purchasereceive.*, '' as details
FROM purchasereceive
WHERE PurchaseReceiveID = ? ";
$qry = $this->db_inventory->query($sql, array($id));
//echo $this->db_inventory->last_query();
if(!$qry){
$this->sys_error_db("select purchasereceive error", $this->db_inventory->last_query());
exit;
}
$row_after = $qry->row_array();
$sql = "SELECT *
FROM `purchasereceivedetail`
WHERE
PurchaseReceiveDetailPurchaseReceiveID = ? AND
PurchaseReceiveDetailIsActive = 'Y'";
$qry = $this->db_inventory->query($sql, array($id));
if($qry){
$row_after['details'] = $qry->result_array();
}else{
$this->sys_error_db("select purchasereceive details error", $this->db_inventory->last_query());
exit;
}
$data_log_after = $row_after;
$sql = "INSERT INTO purchasereceive_log (
PurchaseReceiveLogPurchaseReceiveID,
PurchaseReceiveLogStatus,
PurchaseReceiveLogJSONBefore,
PurchaseReceiveLogJSONAfter,
PurchaseReceiveLogUserID,
PurchaseReceiveLogCreated
)
VALUES(
?,?,?,?,?,NOW()
)";
$qry = $this->db_inventory_log->query($sql, array($id,'DELETE',NULL,json_encode($data_log_after),$userid));
if(!$qry){
$this->sys_error_db("insert log error", $this->db_inventory_log->last_query());
exit;
}
$this->db_inventory->trans_complete();
$result = array(
"message" => ''
);
$this->sys_ok($result);
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search_warehouse(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM supplier
WHERE
SupplierName like ?
AND SupplierIsActive = 'Y'";
$query = $this->db_inventory->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_inventory);
exit;
}
$rows = array('id'=>0,'name'=>'Semua');
$sql = "
SELECT SupplierID as id, SupplierName as name
FROM supplier
WHERE
SupplierName like ?
AND SupplierIsActive = 'Y'
ORDER BY SupplierName DESC
";
$query = $this->db_inventory->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
array_push($rows,array('id'=>0,'name'=>'Semua'));
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("supplier rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search_po_form(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM purchaseorder
JOIN supplier ON SupplierID = PurchaseOrderSupplierID
WHERE
PurchaseOrderNumber like ?
AND PurchaseOrderIsActive = 'Y'
AND PurchaseOrderIsConfirm = 'Y'
AND PurchaseOrderStatus NOT IN('D')";
$query = $this->db_inventory->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_inventory);
exit;
}
$rows = array('id'=>0,'name'=>'Cari Purchase Order','sup_name' => '');
$sql = "SELECT 0 as id, 'Cari Purchase Order' as name, '' as sup_name
UNION
SELECT PurchaseOrderID as id, PurchaseOrderNumber as name, SupplierName as sup_name
FROM purchaseorder
JOIN supplier ON SupplierID = PurchaseOrderSupplierID
WHERE
PurchaseOrderNumber like ?
AND PurchaseOrderIsActive = 'Y'
AND PurchaseOrderIsConfirm = 'Y'
AND PurchaseOrderStatus NOT IN('D')
ORDER BY name DESC
";
$query = $this->db_inventory->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
//array_push($rows,array('id'=>0,'name'=>'Pilih Supplier'));
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("supplier rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search_supplier_form(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM supplier WHERE
SupplierName like ?
AND SupplierIsActive = 'Y'";
$query = $this->db_inventory->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_inventory);
exit;
}
$rows = array('id'=>0,'name'=>'Cari Supplier');
$sql = "SELECT 0 as id, 'Cari Supplier' as name
UNION
SELECT SupplierID as id, SupplierName as name
FROM supplier WHERE
SupplierName like ?
AND SupplierIsActive = 'Y'
ORDER BY name DESC
";
$query = $this->db_inventory->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
//array_push($rows,array('id'=>0,'name'=>'Pilih Supplier'));
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("supplier rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search_supplier_filter(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM supplier WHERE
SupplierName like ?
AND SupplierIsActive = 'Y'";
$query = $this->db_inventory->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_inventory);
exit;
}
$rows = array('id'=>0,'name'=>'Cari Supplier');
$sql = "SELECT 0 as id, 'Cari Supplier' as name
UNION
SELECT SupplierID as id, SupplierName as name
FROM supplier WHERE
SupplierName like ?
AND SupplierIsActive = 'Y'
ORDER BY name DESC
";
$query = $this->db_inventory->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
//array_push($rows,array('id'=>0,'name'=>'Pilih Supplier'));
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("supplier rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function search_expedition(){
try {
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$max_rst = 12;
$tot_count =0;
$q = [
'search' => '%'
];
if ($prm['search'] != '')
{
$q['search'] = "%{$prm['search']}%";
}
// QUERY TOTAL
$sql = "SELECT count(*) as total
FROM expedition
WHERE
ExpeditionName like ?
AND ExpeditionIsActive = 'Y'";
$query = $this->db_inventory->query($sql,$q['search']);
//echo $query;
if ($query) {
$tot_count = $query->result_array()[0]["total"];
}
else {
$this->sys_error_db("m_city count",$this->db_inventory);
exit;
}
$rows = array('id'=>0,'name'=>'Semua');
$sql = "
SELECT ExpeditionID as id, ExpeditionName as name
FROM expedition
WHERE
ExpeditionName like ?
AND ExpeditionIsActive = 'Y'
ORDER BY ExpeditionName DESC
";
$query = $this->db_inventory->query($sql, array($q['search']));
if ($query) {
$rows = $query->result_array();
//array_push($rows,array('id'=>0,'name'=>'Semua'));
//echo $this->db_onedev->last_query();
$result = array("total" => $tot_count, "records" => $rows, "total_display" => sizeof($rows));
$this->sys_ok($result);
}
else {
$this->sys_error_db("expedition rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_po_form(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm['id'];
$sql = "SELECT 0 as id,
'Pilih Purchase Order' as name
UNION SELECT PurchaseOrderID as id, PurchaseOrderNumber as name
FROM purchaseorder
JOIN supplier ON SupplierID = PurchaseOrderSupplierID
WHERE
PurchaseOrderSupplierID = $id
AND PurchaseOrderIsActive = 'Y'
AND PurchaseOrderIsConfirm = 'Y'
AND PurchaseOrderStatus NOT IN('D')
ORDER BY id ASC";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->result_array();
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("purchaseorder rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_warehouse(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT 0 as id,
'' as code,
'Pilih Gudang' as name
UNION SELECT WarehouseID as id,
WarehouseCode as code,
WarehouseName as name
FROM warehouse
JOIN warehousealmari ON WarehouseAlmariWarehouseID = WarehouseID AND WarehouseAlmariIsActive = 'Y'
JOIN warehouserack ON WarehouseRackWarehouseAlmariID = WarehouseAlmariID AND WarehouseAlmariIsActive = 'Y'
WHERE
WarehouseIsActive = 'Y' AND WarehouseIsOffice = 'Y'
ORDER BY id ASC";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->result_array();
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("warehouse rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_warehouse_filter(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT 0 as id,
'' as code,
'Semua Gudang' as name
UNION SELECT WarehouseID as id,
WarehouseCode as code,
WarehouseName as name
FROM warehouse
JOIN warehousealmari ON WarehouseAlmariWarehouseID = WarehouseID AND WarehouseAlmariIsActive = 'Y'
JOIN warehouserack ON WarehouseRackWarehouseAlmariID = WarehouseAlmariID AND WarehouseAlmariIsActive = 'Y'
WHERE
WarehouseIsActive = 'Y' AND WarehouseIsOffice = 'Y'
ORDER BY id ASC";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->result_array();
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("warehouse rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_almari(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm["id"];
$sql = "SELECT WarehouseAlmariID as id,
WarehouseAlmariCode as code,
WarehouseAlmariName as name
FROM warehousealmari
WHERE
WarehouseAlmariIsActive = 'Y'
AND WarehouseAlmariWarehouseID = $id
ORDER BY id ASC";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->result_array();
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("warehousealmari rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_data_rack(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm["id"];
$sql = "SELECT WarehouseRackID as id,
WarehouseRackCode as code,
WarehouseRackName as name
FROM warehouserack
WHERE
WarehouseRackIsActive = 'Y'
AND WarehouseRackWarehouseAlmariID = $id
ORDER BY id ASC";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->result_array();
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("warehousealmari rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function check_stock(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$prm = $this->sys_input;
$id = $prm["id"];
$sql = "SELECT SUM(status) as tot_status
FROM(SELECT IF(PurchaseOrderDetailStatus = 'D',1,0) as status
FROM purchasereceivedetail
JOIN purchaseorderdetail ON PurchaseOrderDetailID = PurchaseReceiveDetailPurchaseOrderDetailID
WHERE PurchaseReceiveDetailPurchaseReceiveID = $id)a";
$query = $this->db_inventory->query($sql);
if ($query) {
$row = $query->result_array();
$result = array("records" => $row);
$this->sys_ok($result);
}
else {
$this->sys_error_db("tot_status rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
function get_printout(){
try{
if (! $this->isLogin) {
$this->sys_error("Invalid Token");
exit;
}
$sql = "SELECT *
FROM r_report
WHERE
R_ReportCode = 'MPB' AND R_ReportIsActive = 'Y'";
$query = $this->db_inventory->query($sql);
if ($query) {
$rows = $query->row_array();
$result = array("records" => $rows);
$this->sys_ok($result);
}
else {
$this->sys_error_db("r_report rows",$this->db_inventory);
exit;
}
} catch (Exception $exc) {
$message = $exc->getMessage();
$this->sys_error($message);
}
}
}