diff --git a/public_html/assets/internal_api.php b/public_html/assets/internal_api.php index fb1fbef..1960050 100644 --- a/public_html/assets/internal_api.php +++ b/public_html/assets/internal_api.php @@ -5,6 +5,8 @@ require_once getenv("DOCUMENT_ROOT")."/assets/conf.inc.php"; require_once getenv("DOCUMENT_ROOT")."/assets/dbCon.php"; +include getenv("DOCUMENT_ROOT")."/include/session_include.php"; +include_once getenv("DOCUMENT_ROOT")."/lib/erp_api.php"; error_reporting(E_ALL); ini_set('display_errors', '1'); @@ -371,7 +373,31 @@ class API extends CONF { public function updtPoint(){ try { - qry("UPDATE tbl_customer SET c_geolat=".$_POST['lat'].", c_geolon=".$_POST['lon']." WHERE c_uid=".(int)$_POST['id']); + qry("UPDATE tbl_customer SET c_geolat=".$_POST['lat'].", c_geolon=".$_POST['lon'].", c_is_transfer='N' WHERE c_uid=".(int)$_POST['id']); + + // ==================== + // Integration to ERP + // ==================== + $erp = new ErpApi(); + + try { + $columns = array(); + $values = array(); + $columns[] = "c_accountno"; + $columns[] = "c_geolat"; + $columns[] = "c_geolon"; + $values[] = $_POST['accountno']; + $values[] = $_POST['lat']; + $values[] = $_POST['lon']; + $lguserid = $_SESSION['ss_UID']; + $erp->updateCustomerFromMis($columns, $values, $lguserid); + + // 성공 시 c_is_transfer='Y' + qry("UPDATE tbl_customer SET c_is_transfer = 'Y' WHERE c_uid = ".(int)$_POST['id']); + + } catch (Exception $e) { + // 여기서는 아무것도 안함. + } $this->response($this->json(array("markerIndex"=>$_POST['marker_index'], "lat"=>$_POST['lat'], "lon"=>$_POST['lon'], "name"=>$_POST['name'], "qty"=>$_POST['qty'], "property"=>$_POST['property'])), 200); } catch(Exception $e) { @@ -384,9 +410,34 @@ class API extends CONF { try { qry("UPDATE tbl_customer SET c_location='".addslashes($_POST['location'])."', - c_comment_ri='".addslashes($_POST['comment'])."' + c_comment_ri='".addslashes($_POST['comment'])."', + c_is_transfer='N' WHERE c_uid=".(int)$_POST['id']); + // ==================== + // Integration to ERP + // ==================== + $erp = new ErpApi(); + + try { + $columns = array(); + $values = array(); + $columns[] = "c_accountno"; + $columns[] = "c_location"; + $columns[] = "c_comment_ri"; + $values[] = $_POST['accountno']; + $values[] = addslashes($_POST['location']); + $values[] = addslashes($_POST['comment']); + $lguserid = $_SESSION['ss_UID']; + $erp->updateCustomerFromMis($columns, $values, $lguserid); + + // 성공 시 c_is_transfer='Y' + qry("UPDATE tbl_customer SET c_is_transfer = 'Y' WHERE c_uid = ".(int)$_POST['id']); + + } catch (Exception $e) { + // 여기서는 아무것도 안함. + } + $this->response($this->json(array("result"=>"success")), 200); } catch(Exception $e) { $error = array($e->getMessage()); @@ -956,6 +1007,7 @@ class API extends CONF { try { $sqShortInfo = qry("SELECT c_uid, + c_accountno, c_name, c_paymenttype, c_comment_ri, @@ -1015,6 +1067,7 @@ class API extends CONF { $result = array( "id" => $_POST['id'], + "accountno" => $rstShortInfo['c_accountno'], "name" => $rstShortInfo['c_name'], "payment_type" => $rstShortInfo['c_paymenttype'], "paymentstring" => $d_paystatusSTR, @@ -1055,38 +1108,93 @@ class API extends CONF { if ($rstCheckOrder['rowcnt'] > 0) { $this->response($this->json(array("msg"=>"This order already exists.", "c_index"=>$_POST['c_index'], "c_return"=>"0")), 200); + return; } - - // 오늘 날짜 - $today = new DateTime(date("Y-m-d")); - //$today = new DateTime('20240425'); - // 지정한 날짜 - $searchDate = new DateTime($_POST['orderdate']); - // 두 날짜 간 차이 계산 - $interval = $today->diff($searchDate); + // 고객 정보 조회 + $customer_uid = (int)$_POST['customer_uid']; + $sqCus = qry("SELECT * FROM tbl_customer WHERE c_uid = {$customer_uid}"); + if (db_num_rows($sqCus) === 0) { + throw new Exception("Customer not found"); + } + $customer = fetch_array($sqCus); - //$estquantity = (c_fullquantity - c_fullquantitydaily + (".(int)$interval->days." * c_fullquantitydaily)); - - qry("INSERT INTO tbl_daily ( - d_orderdate, d_driveruid, d_customeruid, d_accountno, - d_name, d_paymenttype, d_cycle, d_rate, d_form_eu, d_form_corsia, d_maincontainer, d_container, d_location, d_address, - d_city, d_postal, d_oil_2y, d_oil_1y, d_oil_0y, d_fullcycle, d_fullcycleforced, d_fullcycleflag, - d_lastpickupdate, d_lastpickupquantity, d_lastpaiddate, - d_estquantity, - d_createruid, d_createddate, d_status) - - SELECT '".$_POST['orderdate']."', '".$_POST['driveruid']."', c_uid, c_accountno, - c_name, c_paymenttype, c_paymentcycle, c_rate, c_form_eu, c_form_corsia, c_maincontainer, c_container, c_location, c_address, - c_city, c_postal, 0, 0, 0, c_fullcycle, c_fullcycleforced, c_fullcycleflag, - c_lastpickupdate, c_lastpickupquantity, c_lastpaiddate, - IFNULL((c_fullquantity - c_fullquantitydaily + (".(int)$interval->days." * c_fullquantitydaily)),0), - '".$_POST['d_createruid']."', '".date('YmdHis')."', 'A' - FROM tbl_customer - WHERE c_uid=".(int)$_POST['customer_uid']); + // 예상 수량 계산 + $today = new DateTime(date("Y-m-d")); + $searchDate = new DateTime($_POST['orderdate']); + $interval = $today->diff($searchDate); + $days = (int)$interval->days; + $estquantity = max( + 0, + ($customer['c_fullquantity'] - $customer['c_fullquantitydaily']) + ($days * $customer['c_fullquantitydaily']) + ); + + // insert into tbl_daily + $c_name = addslashes($customer['c_name']); + qry(" + INSERT INTO tbl_daily ( + d_orderdate, d_driveruid, d_customeruid, d_accountno, + d_name, d_paymenttype, d_cycle, d_rate, + d_estquantity, d_createruid, d_createddate, d_status + ) VALUES ( + '{$_POST['orderdate']}', + '{$_POST['driveruid']}', + {$customer_uid}, + '{$customer['c_accountno']}', + '{$c_name}', + '{$customer['c_paymenttype']}', + '{$customer['c_paymentcycle']}', + '{$customer['c_rate']}', + {$estquantity}, + '{$_POST['d_createruid']}', + '".date("YmdHis")."', + 'A' + ) + "); - qry("UPDATE tbl_customer SET c_orderdate='".$_POST['orderdate']."', c_orderflag = 1 WHERE c_uid = ".(int)$_POST['customer_uid']); - - $this->response($this->json(array("msg"=>"Order has been successfully added.", "c_index"=>$_POST['c_index'])), 200); + // d_uid 가져오기 + $sqId = qry("SELECT MAX(d_uid) AS d_uid FROM tbl_daily"); + $rtId = fetch_array($sqId); + $d_uid = $rtId['d_uid']; + + // ==================== + // Integration to ERP + // ==================== + $columns = array(); + $columns[] = "d_ordertype"; + $columns[] = "d_orderdate"; + $columns[] = "d_driveruid"; + $columns[] = "d_accountno"; + $columns[] = "d_paymenttype"; + $columns[] = "d_cycle"; + $columns[] = "d_rate"; + $columns[] = "d_status"; + $columns[] = "d_estquantity"; + + // Data + $values = array(); + $values[] = "N"; + $values[] = $_POST['orderdate']; + $values[] = $_POST['driveruid']; + $values[] = $customer['c_accountno']; + $values[] = $customer['c_paymenttype']; + $values[] = $customer['c_paymentcycle']; + $values[] = $customer['c_rate']; + $values[] = "A"; + $values[] = $estquantity; + + // + $erp = new ErpApi(); + $erp->createDailyOrderFromMis($columns, $values, $_POST['d_createruid']); + + // 성공시 + qry("UPDATE tbl_daily SET d_is_transfer = 'Y' WHERE d_uid = ".(int)$d_uid); + + // customer 상태 업데이트 (이건 ERP에서는 트리거로) + qry("UPDATE tbl_customer SET c_orderdate='".$_POST['orderdate']."', c_orderflag = 1 WHERE c_uid = ".(int)$_POST['customer_uid']); + + // + $this->response($this->json(array("msg"=>"Order has been successfully added.", "c_index"=>$_POST['c_index'], "c_return"=>1)), 200); + return; } catch(Exception $e) { $error = array($e->getMessage()); $this->response($this->json($error), 417); @@ -1111,31 +1219,58 @@ class API extends CONF { AND d_status = 'F' ORDER BY d_orderdate DESC LIMIT 1"); $rstrmInfo = fetch_array($sqrmInfo); + // 완료(F) 데이터가 없으면 customer 마지막값 초기화 + if (empty($rstrmInfo)) { + qry("UPDATE tbl_customer SET c_lastpickupdate = '', c_lastpickupquantity = '', c_lastpaiddate = '' WHERE c_uid = '".$_POST['id']."'"); + } else { + // tbl_daily 의 마지막 데이터중 cash 이고 paid 면 c_lastpaiddate + // 그렇지 않으면 다시 tbl_daily 에서 cash 이고 paid 인 마지막 데이터를 가져옴. 없으면 null 처리 + if (($rstrmInfo['d_paymenttype'] ?? '') === "CA" && ($rstrmInfo['d_paystatus'] ?? '') === "P") { + $addQry = ", c_lastpaiddate = '".($rstrmInfo['d_visitdate'] ?? '')."' "; + } else { + $sqrmInfoLast = qry("SELECT + d_visitdate + FROM tbl_daily + WHERE + d_customeruid = '".$_POST['id']."' + AND d_status = 'F' + AND d_paymenttype = 'CA' AND d_paystatus = 'P' + ORDER BY d_orderdate DESC LIMIT 1"); + $rstrmInfoLast = fetch_array($sqrmInfoLast); - // tbl_daily 의 마지막 데이터중 cash 이고 paid 면 c_lastpaiddate - // 그렇지 않으면 다시 tbl_daily 에서 cash 이고 paid 인 마지막 데이터를 가져옴. 없으면 null 처리 - if ($rstrmInfo['d_paymenttype']== "CA" && $rstrmInfo['d_paystatus'] == "P") $addQry = ", c_lastpaiddate = '".$rstrmInfo['d_visitdate']."' "; - else { - $sqrmInfoLast = qry("SELECT - d_visitdate - FROM tbl_daily - WHERE - d_customeruid = '".$_POST['id']."' - AND d_status = 'F' - AND d_paymenttype = 'CA' AND d_paystatus = 'P' - ORDER BY d_orderdate DESC LIMIT 1"); - $rstrmInfoLast = fetch_array($sqrmInfoLast); + if (!empty($rstrmInfoLast) && !empty($rstrmInfoLast['d_visitdate'])) { + $addQry = ", c_lastpaiddate = '".$rstrmInfoLast['d_visitdate']."' "; + } else { + $addQry = ", c_lastpaiddate = '' "; + } + } + // last pickup 업데이트 + qry("UPDATE tbl_customer + SET c_lastpickupdate='".$rstrmInfo['d_orderdate']."', c_lastpickupquantity = '".$rstrmInfo['d_quantity']."' ". $addQry ." + WHERE c_uid = '".$_POST['id']."'"); + } + + // ==================== + // Integration to ERP + // 나머지는 trigger 처리 + // ==================== + $columns = array(); + $values = array(); + $columns[] = "d_accountno"; + $columns[] = "d_orderdate"; + $columns[] = "d_status"; + $values[] = $_POST['accountno']; + $values[] = $_POST['orderdate']; + $values[] = "D"; + $lguserid = $_POST['createruid']; + // ERP로 전송 (payload 내부에서 자동 매핑) + $erp = new ErpApi(); + $erp->updateDailyOrderFromMis($columns, $values, $lguserid); - if ($rstrmInfoLast && $rstrmInfoLast["d_visitdate"] == !'') $addQry = ", c_lastpaiddate = '".$rstrmInfoLast['d_visitdate']."' "; - else $addQry = ", c_lastpaiddate = '' "; - } - - - qry("UPDATE tbl_customer - SET c_lastpickupdate='".$rstrmInfo['d_orderdate']."', c_lastpickupquantity = '".$rstrmInfo['d_quantity']."' ". $addQry ." - WHERE c_uid = '".$_POST['id']."'"); - - + // 성공 시 플래그 & 로그 (지웠음) + // $jdb->uQuery("tbl_daily", ["d_is_transfer"], ["Y"], " WHERE d_uid = '$d_uid'"); + + // $this->response($this->json(array("msg"=>"Order has been successfully removed.", "c_index"=>$_POST['c_index'])), 200); } catch(Exception $e) { $error = array($e->getMessage()); @@ -1255,10 +1390,16 @@ class API extends CONF { "d_note" => $_POST['note'] ); - if ($_POST['paymenttype'] == 'CA') $paystatusSTR = $_POST['paystatus']; - else $paystatusSTR = ""; - - if(strlen($_POST['uid']) > 0) { //update + if ($_POST['paymenttype'] == 'CA') $paystatusSTR = $_POST['paystatus']; + else $paystatusSTR = ""; + + // for integration + $d_uid = null; + $columns = array(); + $values = array(); + + // + if(strlen($_POST['uid']) > 0) { //update qry("UPDATE tbl_daily SET d_orderdate = '".$_POST['visitdate']."', d_driveruid = '".$_POST['driveruid']."', @@ -1271,7 +1412,8 @@ class API extends CONF { d_visit = 'Y', d_visitdate = '".$_POST['visitdate']."000000', d_payeename = '".$_POST['payeename']."', - d_status='F' + d_status='F', + d_is_transfer='N' WHERE d_uid='".(int)$_POST['uid']."'"); $sqNote = qry("SELECT n_uid FROM tbl_note WHERE n_dailyuid = '".(int)$_POST['uid']."'"); @@ -1294,7 +1436,7 @@ class API extends CONF { } } - + $d_uid = (int)$_POST['uid']; }else{ //new @@ -1304,6 +1446,29 @@ class API extends CONF { AND d_customeruid = '".$_POST['customeruid']."'"); if(db_num_rows($sqData) <= 0) { + // 1) 고객 정보 먼저 조회 + $sqCus = qry(" + SELECT + c_paymenttype, c_paymentcycle, c_rate, + c_fullquantity, c_fullquantitydaily + FROM tbl_customer + WHERE c_uid = '".(int)$_POST['customeruid']."' + "); + $customer = fetch_array($sqCus); + + // 2) 예상수량 계산 + $estqty = max( + 0, + ($customer['c_fullquantity'] - $customer['c_fullquantitydaily']) + + ($interval->days * $customer['c_fullquantitydaily']) + ); + // 3) set values to erp + $columns[] = "d_cycle"; $values[] = $customer['c_paymentcycle']; + $columns[] = "d_rate"; $values[] = $customer['c_rate']; + $columns[] = "d_estquantity"; $values[] = $estqty; + $columns[] = "d_ordertype"; $values[] = "N"; + + // 기존 insert 문 그대로 사용 qry("INSERT INTO tbl_daily ( d_orderdate, d_driveruid, d_customeruid, d_accountno, d_name, @@ -1334,6 +1499,7 @@ class API extends CONF { AND d_orderdate='".$_POST['visitdate']."' ORDER BY d_uid DESC LIMIT 1 "); $rstDailyUid = fetch_array($sqDailyUid); + $d_uid = $rstDailyUid['d_uid']; if(strlen(trim($_POST['note'])) > 0){ qry("INSERT INTO tbl_note ( @@ -1361,7 +1527,9 @@ class API extends CONF { $setFilenameNew = str_replace("T_", "", $setFilename); rename($folderPath."/".$setFilename, $folderPath."/".$setFilenameNew); qry("UPDATE tbl_daily SET d_payeesign='".$setFilenameNew."' - WHERE d_uid = '".$rstDailyUid['d_uid']."' "); + WHERE d_uid = '".$rstDailyUid['d_uid']."' "); + // for integration + $columns[] = "d_payeesign"; $values[] = $setFilenameNew; } } @@ -1418,10 +1586,41 @@ class API extends CONF { ".$add_sludge." c_orderflag = 0 WHERE c_uid = '".$_POST['customeruid']."' ". $addWhereQry ." "); + // ==================== + // Integration to ERP + // ==================== + $columns[] = "d_accountno"; $values[] = $_POST['customerno']; // c_accountno + $columns[] = "d_orderdate"; $values[] = $_POST['visitdate']; // yyyymmdd (ERP에서 LocalDate로 변환) + $columns[] = "d_driveruid"; $values[] = $_POST['driveruid']; // external driver id (MIS) + $columns[] = "d_quantity"; $values[] = $_POST['quantity']; + $columns[] = "d_sludge"; $values[] = $_POST['sludge']; + $columns[] = "d_paymenttype"; $values[] = $_POST['paymenttype']; + $columns[] = "d_payamount"; $values[] = $_POST['payamount']; + $columns[] = "d_payeename"; $values[] = $_POST['payeename']; + $columns[] = "d_paystatus"; $values[] = $paystatusSTR; + $columns[] = "d_status"; $values[] = "F"; // 완료 상태 + $columns[] = "d_visit"; $values[] = "Y"; // 방문 상태 + $columns[] = "d_inputdate"; $values[] = date('YmdHis'); // 입력 시간 + if(strlen(trim($_POST['note'])) > 0){ + $columns[] = "d_pickupnote"; $values[] = $_POST['note']; + } + // ERP로 전송 + $erp = new ErpApi(); + $lguserid = $_POST['createruid']; + if(strlen($_POST['uid']) > 0) { + $erp->updateDailyOrderFromMis($columns, $values, $lguserid); + } else { + $erp->createDailyOrderFromMis($columns, $values, $lguserid); + } + // 성공 시 플래그 & 로그 + qry("UPDATE tbl_daily SET d_is_transfer = 'Y' WHERE d_uid = ".(int)$d_uid); + + // $this->response($this->json(array("msg"=>"Input has been successfully saved.")), 200); //$this->response($this->json(array("result"=>$result)), 200); + return; } catch(Exception $e) { $error = array($e->getMessage()); $this->response($this->json($error), 417); @@ -1489,7 +1688,6 @@ class API extends CONF { } } - } $api = new API; diff --git a/public_html/doc/customer_detail.php b/public_html/doc/customer_detail.php index 339e441..427a178 100644 --- a/public_html/doc/customer_detail.php +++ b/public_html/doc/customer_detail.php @@ -26,7 +26,7 @@ if ($mode == "create" || $mode == "") { } else if ($mode == "update") { $btnName = "UPDATE"; - $admTag = "DISABLED"; + //$admTag = "DISABLED"; } // User data query @@ -1055,7 +1055,12 @@ $(document).ready(function() Account No - > + + + + + + Status @@ -2293,7 +2298,7 @@ $(document).ready(function(){
- +
diff --git a/public_html/doc/customer_process.php b/public_html/doc/customer_process.php index 116d98e..3f2a232 100644 --- a/public_html/doc/customer_process.php +++ b/public_html/doc/customer_process.php @@ -2,6 +2,7 @@ include getenv("DOCUMENT_ROOT")."/include/session_include.php"; @include getenv("DOCUMENT_ROOT")."/config/config_shopInfo.php"; +include_once getenv("DOCUMENT_ROOT") . "/lib/erp_api.php"; $goStr = "switched=$switched&page=$page&key_word=$key_word&column=$column&sorting_type=$sorting_type&switch=$switch&cstatus=$cstatus"; @@ -50,8 +51,12 @@ if ($actionStr == "CUSTOMERINFO" && $mode == "delete") { } - $jdb->nQuery("UPDATE tbl_customer SET c_status='D' WHERE c_uid = '$c_uid'", "delete error"); - $jdb->CLOSE(); + $jdb->nQuery("UPDATE tbl_customer SET c_status='D', c_is_transfer = 'N' WHERE c_uid = '$c_uid'", "delete error"); + + // ==================== + // Integration to ERP + // ==================== + // 화면에서 기능이 없음 $msg = "Deleted successfully."; $urlSTR = "/index_intranet.php?view=customer_list&$goStr"; @@ -83,10 +88,9 @@ if ($actionStr == "CUSTOMERINFO") { if($mode == "create") { //$columns[] = "c_uid"; - $columns[] = "c_accountno"; $columns[] = "c_createddate"; } - + $columns[] = "c_accountno"; //$columns[] = "c_gid"; $columns[] = "c_form_us"; $columns[] = "c_form_eu"; @@ -140,19 +144,18 @@ if ($actionStr == "CUSTOMERINFO") { $columns[] = "c_expoilmonth"; $columns[] = "c_hstno"; $columns[] = "c_identcode"; + // integration flag + $columns[] = "c_is_transfer"; //////////// // data //////////// if($mode == "create") { //$values[] = $c_uid; - $values[] = $c_accountno; $values[] = date("YmdHis"); } - - + $values[] = $c_accountno; //$values[] = $c_gid; - $values[] = $c_form_us; $values[] = str_replace("-", "", trim($c_form_eu)); $values[] = str_replace("-", "", trim($c_form_corsia)); @@ -226,7 +229,8 @@ if ($actionStr == "CUSTOMERINFO") { $values[] = $c_expoilmonth; $values[] = str_replace("\\", "", trim($c_hstno)); $values[] = str_replace("\\", "", trim($c_identcode)); - + // c_is_transfer + $values[] = "N"; //for ($i=0; $i < count($columns); $i++) //echo "[$columns[$i]][$values[$i]]
"; @@ -244,9 +248,6 @@ if ($actionStr == "CUSTOMERINFO") { addLog ("add", "CUSTOMER DETAIL", "CREATE", $lguserid, "", $c_uid); - $func -> modalMsg ($msg, "/index_intranet.php?view=customer_detail&mode=update&c_uid=$c_uid&$goStr"); - exit(); - //$query = "select max(uid) from tbl_members "; //$user_id = $jdb->rQuery($query, "max query error"); } @@ -318,12 +319,35 @@ if ($actionStr == "CUSTOMERINFO") { addLog ("add", "CUSTOMER DETAIL - SLUDGE, RATE, PAYMENTCYCLE, PAYMENTTYPE", "UPDATE", $lguserid, $qry_driverr, $c_uid); } - - - $func -> modalMsg ($msg, "/index_intranet.php?view=customer_detail&mode=update&c_uid=$c_uid&$goStr"); - exit(); } + // ==================== + // Integration to ERP + // ==================== + $erp = new ErpApi(); + try { + + if ($mode == "create") { + // MIS → ERP CREATE (payload 매핑은 ErpApi 내부에서 자동 처리) + $erp->createCustomerFromMis($columns, $values, $lguserid); + } + else if ($mode == "update") { + // MIS → ERP UPDATE + $erp->updateCustomerFromMis($columns, $values, $lguserid); + } + + // 성공 시 c_is_transfer='Y' + $jdb->uQuery("tbl_customer", ["c_is_transfer"], ["Y"], " WHERE c_uid = '$c_uid'"); + + // + $func->modalMsg($msg, "/index_intranet.php?view=customer_detail&mode=update&c_uid=$c_uid&$goStr"); + exit(); + + } catch (Exception $e) { + $msg = "Check Integration Log."; + $func->modalMsg($msg, "/index_intranet.php"); + exit(); + } $msg = "Invalid data. Please try again."; $func -> modalMsg ($msg, ""); diff --git a/public_html/doc/map.php b/public_html/doc/map.php index 14b8b9f..c8df288 100644 --- a/public_html/doc/map.php +++ b/public_html/doc/map.php @@ -345,10 +345,11 @@