816 lines
35 KiB
PHP
816 lines
35 KiB
PHP
<?php
|
|
class Prepurchase 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;
|
|
|
|
$search = "%%";
|
|
if (isset($prm['search']) && trim($prm["search"]) != '') {
|
|
$search = trim($prm["search"]);
|
|
$search = '%' . $prm['search'] . '%';
|
|
}
|
|
|
|
$filter_supplier = '';
|
|
if (isset($prm['supplier_id']) && intval($prm['supplier_id']) > 0) {
|
|
$filter_supplier = ' AND SupplierID = '.$prm['supplier_id'];
|
|
}
|
|
$order_by = "PurchasePrepareNumber";
|
|
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 PurchasePrepareID as id
|
|
FROM `purchaseprepare`
|
|
JOIN `supplierpaymentterms` ON PurchasePrepareSupplierPaymentTermsID = SupplierPaymentTermsID
|
|
JOIN `supplier` ON SupplierPaymentTermsSupplierID = SupplierID $filter_supplier
|
|
WHERE
|
|
PurchasePrepareIsActive = 'Y' AND
|
|
( PurchasePrepareNumber like ? )
|
|
) x";
|
|
$qry = $this->db_inventory->query($sql,array($search));
|
|
$tot_count = 0;
|
|
$tot_page = 0;
|
|
if ($qry) {
|
|
$tot_count = $qry->row()->total;
|
|
$tot_page = ceil($tot_count/$perpage);
|
|
} else {
|
|
//echo $this->db_inventory->last_query();
|
|
$this->sys_error_db("purchase order count error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
|
|
$rows = array();
|
|
$sql = "SELECT PurchasePrepareID as id,
|
|
SupplierID as supplier_id,
|
|
SupplierName as supplier_name,
|
|
SupplierPaymentTermsID as agreement_id,
|
|
SupplierPaymentTermsName as agreement_name,
|
|
SupplierPaymentTermsDescription as agreement_description,
|
|
SupplierPaymentTermsStartDate as agreement_start_date,
|
|
SupplierPaymentTermsEndDate as agreement_end_date,
|
|
PurchasePrepareNumber as code,
|
|
PurchasePrepareStatus as status,
|
|
'' as details
|
|
FROM `purchaseprepare`
|
|
JOIN `supplierpaymentterms` ON PurchasePrepareSupplierPaymentTermsID = SupplierPaymentTermsID
|
|
JOIN `supplier` ON SupplierPaymentTermsSupplierID = SupplierID $filter_supplier
|
|
WHERE
|
|
PurchasePrepareIsActive = 'Y' AND
|
|
( PurchasePrepareNumber like ? )
|
|
ORDER BY $order_by $order_type
|
|
LIMIT ? OFFSET ?";
|
|
$qry = $this->db_inventory->query($sql, array($search, $perpage, $offset));
|
|
//echo $this->db_inventory->last_query();
|
|
if($qry){
|
|
$rows = $qry->result_array();
|
|
|
|
$result = array("total_page" => $tot_page, "records" => $rows);
|
|
$this->sys_ok($result);
|
|
exit;
|
|
}
|
|
else{
|
|
//echo $this->db_inventory->last_query();
|
|
$this->sys_error_db("purchase order select error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function get_details(){
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
|
|
$sql = "SELECT PurchasePrepareDetailID as id,
|
|
PurchasePrepareDetailItemID as item_id,
|
|
ItemName as item_name,
|
|
ItemUnitName as unit_name,
|
|
PurchasePrepareDetailQty as qty,
|
|
GROUP_CONCAT(DISTINCT CONCAT(MutasiRequestReceiveMutasiRequestNumber,'^',MutasiRequestReceiveNumber,'^',M_BranchName)) as request_numbers,
|
|
MutasiRequestReceiveDate as request_date,
|
|
MutasiRequestReceiveEstimatedDelivery as estimated_delivery,
|
|
'' as info
|
|
FROM purchasepreparedetail
|
|
JOIN purchasepreparedetailrequest ON PurchasePrepareDetailRequestPurchasePrepareDetailID = PurchasePrepareDetailID AND
|
|
PurchasePrepareDetailRequestIsActive = 'Y'
|
|
JOIN mutasirequestreceive ON PurchasePrepareDetailRequestMutasiRequestReceiveID = MutasiRequestReceiveID AND
|
|
MutasiRequestReceiveIsActive = 'Y'
|
|
JOIN companyaddress ON MutasiRequestReceiveCompanyAddressID = CompanyAddressID
|
|
JOIN company ON CompanyAddressCompanyID = CompanyID
|
|
JOIN $this->db_onex.m_branch ON CompanyM_BranchID = M_BranchID
|
|
JOIN item ON PurchasePrepareDetailItemID = ItemID
|
|
JOIN itemunit ON PurchasePrepareDetailItemUnitID = ItemUnitID
|
|
WHERE
|
|
PurchasePrepareDetailPurchasePrepareID = ? AND PurchasePrepareDetailIsActive = 'Y'
|
|
GROUP BY PurchasePrepareDetailID
|
|
|
|
";
|
|
$query = $this->db_inventory->query($sql,array($prm['id']));
|
|
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
if($rows){
|
|
foreach ($rows as $key => $value) {
|
|
$arr_koma = explode(',',$value['request_numbers']);
|
|
$arr_requests = [];
|
|
foreach ($arr_koma as $k => $v) {
|
|
$arr_topi = explode('^',$v);
|
|
array_push($arr_requests,array('mutasi_number'=>$arr_topi[0],'receive_number'=>$arr_topi[1],'branch_name'=>$arr_topi[2]));
|
|
}
|
|
$rows[$key]['info'] = $arr_requests;
|
|
}
|
|
}
|
|
//echo $this->db_onedev->last_query();
|
|
$result = array( "records" => $rows);
|
|
$this->sys_ok($result);
|
|
}
|
|
else {
|
|
$this->sys_error_db("purchasepreparedetail rows",$this->db_inventory);
|
|
exit;
|
|
}
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function search_supplier(){
|
|
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']}%";
|
|
}
|
|
|
|
$sql = "
|
|
SELECT SupplierID as id, SupplierName as name, '' as terms
|
|
FROM supplier
|
|
WHERE
|
|
SupplierName like ?
|
|
AND SupplierIsActive = 'Y'
|
|
ORDER BY SupplierName ASC
|
|
";
|
|
$query = $this->db_inventory->query($sql, array($q['search']));
|
|
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
array_push($rows,array('id'=>0,'name'=>'Semua Supplier'));
|
|
//echo $this->db_onedev->last_query();
|
|
$result = array( "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 get_suppliers(){
|
|
try {
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$prm = $this->sys_input;
|
|
|
|
$sql = "
|
|
SELECT SupplierID as id, SupplierName as name
|
|
FROM mutasirequestreceive
|
|
JOIN supplierpaymenttermsdetail ON MutasiRequestReceiveItemID = SupplierPaymentTermsDetailItemID AND
|
|
SupplierPaymentTermsDetailIsActive = 'Y'
|
|
JOIN supplierpaymentterms ON SupplierPaymentTermsSupplierPaymentTermsID = SupplierPaymentTermsID
|
|
JOIN supplier ON SupplierPaymentTermsSupplierID = SupplierID
|
|
WHERE
|
|
MutasiRequestReceiveStatus NOT IN('N','D','C') AND MutasiRequestReceiveIsActive = 'Y'
|
|
GROUP BY SupplierID
|
|
ORDER BY SupplierName ASC
|
|
";
|
|
$query = $this->db_inventory->query($sql, array($q['search']));
|
|
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
array_push($rows,array('id'=>0,'name'=>'Semua Supplier'));
|
|
//echo $this->db_onedev->last_query();
|
|
$result = array( "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 get_terms(){
|
|
try{
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$id = 0;
|
|
if(isset($prm['id']) && intval($prm) > 0){
|
|
$id = $prm['id'];
|
|
$sql = "SELECT SupplierPaymentTermsID as id,
|
|
SupplierPaymentTermsCode as code,
|
|
SupplierPaymentTermsName as name,
|
|
SupplierPaymentTermsDescription as description,
|
|
DATE_FORMAT(SupplierPaymentTermsStartDate,'%d-%m-%Y') as start_date,
|
|
DATE_FORMAT(SupplierPaymentTermsEndDate,'%d-%m-%Y') as end_date
|
|
FROM supplierpaymentterms
|
|
WHERE
|
|
SupplierPaymentTermsSupplierID = ? AND SupplierPaymentTermsIsActive = 'Y'
|
|
ORDER BY SupplierPaymentTermsID DESC";
|
|
$query = $this->db_inventory->query($sql,$id);
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
$result = array("records" => $rows);
|
|
$this->sys_ok($result);
|
|
}
|
|
else {
|
|
$this->sys_error_db("supplierpaymentterms rows",$this->db_inventory);
|
|
exit;
|
|
}
|
|
}
|
|
else{
|
|
$this->sys_error("Invalid Param ID");
|
|
exit;
|
|
}
|
|
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function get_request_details(){
|
|
try{
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$filter_agreement = '';
|
|
if(isset($prm['agreement_id']) && intval($prm['agreement_id']) > 0){
|
|
$filter_agreement = " AND SupplierPaymentTermsID = {$prm['agreement_id']}";
|
|
}
|
|
$sql = "SELECT MutasiRequestReceiveItemID as item_id,
|
|
MutasiRequestReceiveItemUnitID as unit_id,
|
|
SUM(MutasiRequestReceiveRequestQty) as qty,
|
|
ItemName as item_name,
|
|
ItemUnitName as unit_name,
|
|
CONCAT(ItemName,' : ',ItemUnitName) as itemunit_name,
|
|
'Belum disetting' as supplier_name,
|
|
'0' as supplier_id,
|
|
'0' as agreement_id,
|
|
'Belum disetting' as agreement_name,
|
|
0 as total,
|
|
GROUP_CONCAT(DISTINCT MutasiRequestReceiveID) as mutasireceiveids
|
|
FROM mutasirequestreceive
|
|
JOIN item ON MutasiRequestReceiveItemID = ItemID
|
|
JOIN itemunit ON MutasiRequestReceiveItemUnitID = ItemUnitID
|
|
WHERE
|
|
MutasiRequestReceiveStatus NOT IN('N','D','C') AND MutasiRequestReceiveIsActive = 'Y'
|
|
GROUP BY MutasiRequestReceiveItemID ";
|
|
$query = $this->db_inventory->query($sql);
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
foreach ($rows as $key => $value) {
|
|
$sql =" SELECT IFNULL(SupplierID,0) as supplier_id,
|
|
IFNULL(SupplierName,'Belum disetting') as supplier_name,
|
|
IFNULL(SupplierPaymentTermsID,'Belum disetting') as agreement_id,
|
|
IFNULL(SupplierPaymentTermsName,'Belum disetting') as agreement_name,
|
|
IFNULL(SupplierPaymentTermsDetailTotal,0) as total
|
|
FROM supplierpaymenttermsdetail
|
|
JOIN supplierpaymentterms ON SupplierPaymentTermsSupplierPaymentTermsID = SupplierPaymentTermsID
|
|
$filter_agreement
|
|
JOIN supplier ON SupplierPaymentTermsSupplierID = SupplierID
|
|
WHERE
|
|
SupplierPaymentTermsDetailIsActive = 'Y' AND
|
|
SupplierPaymentTermsDetailItemID = ?
|
|
ORDER BY SupplierPaymentTermsDetailTotal ASC
|
|
LIMIT 1
|
|
";
|
|
$query = $this->db_inventory->query($sql,array($value['item_id']));
|
|
if($query){
|
|
$row_price = $query->row_array();
|
|
$rows[$key]['supplier_name'] = $row_price['supplier_id'];
|
|
$rows[$key]['supplier_name'] = $row_price['supplier_name'];
|
|
$rows[$key]['agreement_id'] = $row_price['agreement_id'];
|
|
$rows[$key]['agreement_name'] = $row_price['agreement_name'];
|
|
$rows[$key]['total'] = $row_price['total'];
|
|
}
|
|
else{
|
|
$this->sys_error_db("get min price",$this->db_inventory);
|
|
exit;
|
|
}
|
|
}
|
|
$result = array("records" => $rows);
|
|
$this->sys_ok($result);
|
|
}
|
|
else {
|
|
$this->sys_error_db("supplierpaymentterms rows {$value['item_id']}",$this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function get_supplier_agreement_list(){
|
|
try{
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
|
|
if($prm['detail'] && count($prm['detail']) > 0){
|
|
$data_supplier_agreement = [];
|
|
$filter_item = "";
|
|
foreach ($prm['detail'] as $key => $value) {
|
|
if($key == 0)
|
|
$filter_item = $value['item_id'];
|
|
$filter_item .= " OR {$value['item_id']}";
|
|
}
|
|
$filter_item = "AND ({$filter_item})";
|
|
$sql = "SELECT
|
|
SupplierName as name,
|
|
SupplierID as id,
|
|
GROUP_CONCAT(DISTINCT CONCAT(SupplierPaymentTermsID,'^',SupplierPaymentTermsName)) as concat_agreements,
|
|
'' as agreements
|
|
FROM mutasirequestreceive
|
|
JOIN item ON MutasiRequestReceiveItemID = ItemID $filter_item
|
|
JOIN itemunit ON MutasiRequestReceiveItemUnitID = ItemUnitID
|
|
JOIN supplierpaymenttermsdetail ON SupplierPaymentTermsDetailItemID = ItemID
|
|
JOIN supplierpaymentterms ON SupplierPaymentTermsSupplierPaymentTermsID = SupplierPaymentTermsID
|
|
JOIN supplier ON SupplierPaymentTermsSupplierID = SupplierID
|
|
WHERE
|
|
MutasiRequestReceiveStatus NOT IN('N','D','C') AND MutasiRequestReceiveIsActive = 'Y'
|
|
GROUP BY SupplierID ";
|
|
$query = $this->db_inventory->query($sql);
|
|
if(!$query){
|
|
$this->sys_error_db("get min price",$this->db_inventory);
|
|
exit;
|
|
}
|
|
else{
|
|
$data_supplier_agreement = $query->result_array();
|
|
foreach ($data_supplier_agreement as $k => $v) {
|
|
$terms = array();
|
|
$string_terms = explode(',',$v['concat_agreements']);
|
|
//print_r($string_terms);
|
|
foreach ($string_terms as $k_terms => $v_terms) {
|
|
$arr_terms = explode('^',$v_terms);
|
|
//if($k_terms == 0)
|
|
//print_r($arr_terms);
|
|
array_push($terms,array('id'=>$arr_terms[0],'name'=>$arr_terms[1]));
|
|
}
|
|
$data_supplier_agreement[$k]['agreements'] = $terms;
|
|
}
|
|
|
|
}
|
|
|
|
$result = array("records" => $data_supplier_agreement);
|
|
$this->sys_ok($result);
|
|
}else{
|
|
$this->sys_error("Invalid Parameters");
|
|
exit;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
function get_request_detail_dialog(){
|
|
try{
|
|
if (! $this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
|
|
$prm = $this->sys_input;
|
|
$filter_agreement = '';
|
|
if(isset($prm['agreement_id']) && intval($prm['agreement_id']) > 0){
|
|
$filter_agreement = " AND SupplierPaymentTermsID = {$prm['agreement_id']}";
|
|
}
|
|
$sql = "SELECT MutasiRequestReceiveItemID as item_id,
|
|
MutasiRequestReceiveItemUnitID as unit_id,
|
|
SUM(MutasiRequestReceiveRequestQty) as qty,
|
|
ItemName as item_name,
|
|
ItemUnitName as unit_name,
|
|
CONCAT(ItemName,' : ',ItemUnitName) as itemunit_name,
|
|
'Belum disetting' as supplier_name,
|
|
'0' as supplier_id,
|
|
'0' as agreement_id,
|
|
'Belum disetting' as agreement_name,
|
|
0 as total,
|
|
GROUP_CONCAT(DISTINCT MutasiRequestReceiveID) as mutasireceiveids
|
|
FROM mutasirequestreceive
|
|
JOIN item ON MutasiRequestReceiveItemID = ItemID
|
|
JOIN itemunit ON MutasiRequestReceiveItemUnitID = ItemUnitID
|
|
WHERE
|
|
MutasiRequestReceiveStatus NOT IN('N','D','C') AND MutasiRequestReceiveIsActive = 'Y'
|
|
GROUP BY MutasiRequestReceiveItemID ";
|
|
$query = $this->db_inventory->query($sql);
|
|
if ($query) {
|
|
$rows = $query->result_array();
|
|
foreach ($rows as $key => $value) {
|
|
$sql =" SELECT IFNULL(SupplierID,0) as supplier_id,
|
|
IFNULL(SupplierName,'Belum disetting') as supplier_name,
|
|
IFNULL(SupplierPaymentTermsID,'Belum disetting') as agreement_id,
|
|
IFNULL(SupplierPaymentTermsName,'Belum disetting') as agreement_name,
|
|
IFNULL(SupplierPaymentTermsDetailTotal,0) as total
|
|
FROM supplierpaymenttermsdetail
|
|
JOIN supplierpaymentterms ON SupplierPaymentTermsSupplierPaymentTermsID = SupplierPaymentTermsID
|
|
$filter_agreement
|
|
JOIN supplier ON SupplierPaymentTermsSupplierID = SupplierID
|
|
WHERE
|
|
SupplierPaymentTermsDetailIsActive = 'Y' AND
|
|
SupplierPaymentTermsDetailItemID = ?
|
|
ORDER BY SupplierPaymentTermsDetailTotal ASC
|
|
LIMIT 1
|
|
";
|
|
$query = $this->db_inventory->query($sql,array($value['item_id']));
|
|
if($query){
|
|
$row_price = $query->row_array();
|
|
$rows[$key]['supplier_name'] = $row_price['supplier_id'];
|
|
$rows[$key]['supplier_name'] = $row_price['supplier_name'];
|
|
$rows[$key]['agreement_id'] = $row_price['agreement_id'];
|
|
$rows[$key]['agreement_name'] = $row_price['agreement_name'];
|
|
$rows[$key]['total'] = $row_price['total'];
|
|
}
|
|
else{
|
|
$this->sys_error_db("get min price",$this->db_inventory);
|
|
exit;
|
|
}
|
|
}
|
|
$result = array("records" => $rows);
|
|
$this->sys_ok($result);
|
|
}
|
|
else {
|
|
$this->sys_error_db("supplierpaymentterms rows {$value['item_id']}",$this->db_inventory);
|
|
exit;
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
|
|
function save(){
|
|
try {
|
|
if (!$this->isLogin) {
|
|
$this->sys_error("Invalid Token");
|
|
exit;
|
|
}
|
|
$userid = $this->sys_user['M_UserID'];
|
|
$prm = $this->sys_input;
|
|
|
|
$this->db_inventory->trans_start();
|
|
$this->db_inventory->trans_strict(FALSE);
|
|
|
|
$numbering = '';
|
|
$sql = "SELECT `fn_numbering`('PRE') 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 purchaseprepare(
|
|
PurchasePrepareNumber,
|
|
PurchasePrepareSupplierPaymentTermsID,
|
|
PurchasePrepareCreated,
|
|
PurchasePrepareLastUpdated,
|
|
PurchasePrepareUserID
|
|
)
|
|
VALUES(
|
|
?,?,NOW(),NOW(),?
|
|
)";
|
|
$param_insert = array(
|
|
$numbering,
|
|
$prm['agreement'][0],
|
|
$userid
|
|
);
|
|
$qry = $this->db_inventory->query($sql, $param_insert);
|
|
$last_id = 0;
|
|
if($qry){
|
|
$last_id = $this->db_inventory->insert_id();
|
|
}
|
|
else{
|
|
$this->sys_error_db("save purchaseprepare error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
|
|
if(count($prm['details'])>0){
|
|
foreach ($prm['details'] as $key => $value) {
|
|
$sql = "INSERT INTO purchasepreparedetail(
|
|
PurchasePrepareDetailPurchasePrepareID,
|
|
PurchasePrepareDetailItemID,
|
|
PurchasePrepareDetailItemUnitID,
|
|
PurchasePrepareDetailQty,
|
|
PurchasePrepareDetailUserID,
|
|
PurchasePrepareDetailCreated,
|
|
PurchasePrepareDetailLastUpdated
|
|
)
|
|
VALUES(
|
|
?,?,?,?,?,NOW(),NOW()
|
|
)";
|
|
$param_insert_detail = array(
|
|
$last_id,
|
|
$value['item_id'],
|
|
$value['unit_id'],
|
|
$value['qty'],
|
|
$userid
|
|
);
|
|
$qry = $this->db_inventory->query($sql,$param_insert_detail);
|
|
if(!$qry){
|
|
//echo $this->db_inventory->last_query();
|
|
$this->sys_error_db("save purchasepreparedetail error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
else{
|
|
$last_detail_id = $this->db_inventory->insert_id();
|
|
$mutasireveiveids = explode(',',$value['mutasireceiveids']);
|
|
foreach ($mutasireveiveids as $k => $v) {
|
|
$sql = "INSERT INTO purchasepreparedetailrequest(
|
|
PurchasePrepareDetailRequestPurchasePrepareDetailID,
|
|
PurchasePrepareDetailRequestMutasiRequestReceiveID,
|
|
PurchasePrepareDetailRequestUserID,
|
|
PurchasePrepareDetailRequestCreated,
|
|
PurchasePrepareDetailRequestLastUpdated
|
|
)
|
|
VALUES(
|
|
?,?,?,NOW(),NOW()
|
|
)";
|
|
$param_insert_detail_request = array(
|
|
$last_detail_id,
|
|
$v,
|
|
$userid
|
|
);
|
|
$qry = $this->db_inventory->query($sql,$param_insert_detail_request);
|
|
if(!$qry){
|
|
//echo $this->db_inventory->last_query();
|
|
$this->sys_error_db("save purchasepreparedetailrequest error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$id = $last_id;
|
|
$sql = "SELECT purchaseprepare.*, '' as details
|
|
FROM purchaseprepare
|
|
WHERE PurchasePrepareID = ? ";
|
|
$qry = $this->db_inventory->query($sql, array($id));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
$this->sys_error_db("select purchaseorder error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
$row_after = $qry->row_array();
|
|
$sql = "SELECT *, '' as details
|
|
FROM `purchasepreparedetail`
|
|
WHERE
|
|
PurchasePrepareDetailPurchasePrepareID = ? AND
|
|
PurchasePrepareDetailIsActive = 'Y'";
|
|
$qry = $this->db_inventory->query($sql, array($id));
|
|
if($qry){
|
|
$details = $qry->result_array();
|
|
foreach ($details as $k_log => $v_log) {
|
|
$sql = "SELECT *
|
|
FROM `purchasepreparedetailrequest`
|
|
WHERE
|
|
PurchasePrepareDetailRequestPurchasePrepareDetailID = ? AND
|
|
PurchasePrepareDetailRequestIsActive = 'Y'";
|
|
$qry = $this->db_inventory->query($sql, array($v_log['PurchasePrepareDetailID']));
|
|
if($qry){
|
|
$details[$k_log]['details'] = $qry->result_array();
|
|
}else{
|
|
$this->sys_error_db("select purchasepreparedetailrequest error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
}
|
|
$row_after['details'] = $details;
|
|
}else{
|
|
$this->sys_error_db("select purchasepreparedetail error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
|
|
$data_log_after = $row_after;
|
|
|
|
$sql = "INSERT INTO purchaseprepare_log (
|
|
PurchasePrepareLogPurchasePrepareID,
|
|
PurchasePrepareLogStatus,
|
|
PurchasePrepareLogJsonBefore,
|
|
PurchasePrepareLogJsonAfter,
|
|
PurchasePrepareLogUserID,
|
|
PurchasePrepareLogCreated
|
|
)
|
|
VALUES(
|
|
?,?,NULL,?,?,NOW()
|
|
)";
|
|
$qry = $this->db_inventory_log->query($sql, array($id,'ADD',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 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 = "SELECT * FROM purchaseprepare WHERE PurchasePrepareID = ? AND PurchasePrepareStatus = 'N'";
|
|
$qry = $this->db_inventory->query($sql, [$id]);
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
$this->sys_error_db("select purchaseprepare error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
if($qry->row()){
|
|
$sql = "UPDATE purchaseprepare
|
|
SET
|
|
PurchasePrepareIsActive = 'N',
|
|
PurchasePrepareLastUpdated = now(),
|
|
PurchasePrepareUserID = ?
|
|
WHERE PurchasePrepareID = ?
|
|
";
|
|
$qry = $this->db_inventory->query($sql, [$userid,$id]);
|
|
if(!$qry){
|
|
$this->sys_error_db("delete purchaseprepare error", $this->db_inventory->last_query());
|
|
exit;
|
|
}else{
|
|
$sql = "UPDATE purchasepreparedetail
|
|
SET
|
|
PurchasePrepareDetailIsActive = 'N',
|
|
PurchasePrepareDetailLastUpdated = now(),
|
|
PurchasePrepareDetailUserID = ?
|
|
WHERE
|
|
PurchasePrepareDetailPurchasePrepareID = ?
|
|
";
|
|
$qry = $this->db_inventory->query($sql, [$userid,$id]);
|
|
if(!$qry){
|
|
$this->sys_error_db("delete request mutasidetail error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
}
|
|
|
|
$sql = "SELECT purchaseprepare.*, '' as details
|
|
FROM purchaseprepare
|
|
WHERE PurchasePrepareID = ? ";
|
|
$qry = $this->db_inventory->query($sql, array($id));
|
|
//echo $this->db_inventory->last_query();
|
|
if(!$qry){
|
|
$this->sys_error_db("select purchaseprepare error", $this->db_inventory->last_query());
|
|
exit;
|
|
}
|
|
|
|
|
|
$row_after = $qry->row_array();
|
|
$data_log_after = $row_after;
|
|
|
|
$sql = "INSERT INTO purchaseprepare_log (
|
|
PurchasePrepareLogPurchasePrepareID,
|
|
PurchasePrepareLogStatus,
|
|
PurchasePrepareLogJsonBefore,
|
|
PurchasePrepareLogJsonAfter,
|
|
PurchasePrepareLogUserID,
|
|
PurchasePrepareLogCreated
|
|
)
|
|
VALUES(
|
|
?,?,NULL,?,?,NOW()
|
|
)";
|
|
$qry = $this->db_inventory_log->query($sql, array($id,'DELETE',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);
|
|
}else{
|
|
$message = "Maaf prepurchase sudah digunakan";
|
|
$this->sys_error($message);
|
|
}
|
|
|
|
|
|
} catch (Exception $exc) {
|
|
$message = $exc->getMessage();
|
|
$this->sys_error($message);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|