<?php
$userdb = "wahpet_user";
$passdb = "7bwt=~~QeOR3";
$servername = "localhost";
$dbname = "wahpet_poncol";

ini_set('session.cache_limiter','public');
session_cache_limiter(false);

ini_set('max_execution_time', 60);

$conn = new mysqli($servername, $userdb, $passdb, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

date_default_timezone_set("Asia/Jakarta");
output_buffering : "4096";

function globaldate($d){
	$datetime = strtotime($d);
	$date_output = date("m/d/Y g:i A", $datetime);
	echo $date_output;
	}

	function is_odd($number) {
	  return $number & 1; // 0 = even, 1 = odd
	}


function getip()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
function dtosql($date){
	$exp = explode('/',$date);
	if(count($exp) == 3) {
		$date = $exp[2].'-'.$exp[1].'-'.$exp[0];
	}
	return $date;
}
 
function dtostr($date){
	$exp = explode('-',$date);
	if(count($exp) == 3) {
		$date = $exp[2].'/'.$exp[1].'/'.$exp[0];
	}
	return $date;
	}

function iddate($date){  
        $BulanIndo = array("Januari", "Februari", "Maret",  
                           "April", "Mei", "Juni",  
                           "Juli", "Agustus", "September",  
                           "Oktober", "November", "Desember");  
      
        $tahun = substr($date, 0, 4);  
        $bulan = substr($date, 5, 2);  
        $tgl   = substr($date, 8, 2);  
          
        $result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;       
        return($result);  
}  

function iddate2($date){  
        $BulanIndo = array("Jan", "Feb", "Mar",  
                           "Apr", "Mei", "Jun",  
                           "Jul", "Aug", "Sep",  
                           "Okt", "Nov", "Des");  
      
        $tahun = substr($date, 0, 4);  
        $bulan = substr($date, 5, 2);  
        $tgl   = substr($date, 8, 2);  
          
        $result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;       
        return($result);  
}  

function transno($nomer){

	if(strlen($nomer)==1){
			$nomer = "0000".$nomer;
		}
	elseif(strlen($nomer)==2){
			$nomer = "000".$nomer;
		}
	elseif(strlen($nomer)==3){
			$nomer = "00".$nomer;
		}
	elseif(strlen($nomer)==4){
			$nomer = "0".$nomer;
		}
	return $nomer;
}


function transno3($nomer){

	if(strlen($nomer)==1){
			$nomer = "00".$nomer;
		}
	elseif(strlen($nomer)==2){
			$nomer = "0".$nomer;
		}
	return $nomer;
}


function enc($txt){
	$text = md5(md5(md5($txt)));
	return $text;
	}
	
$tgl = date('d');
$bln = date('m');
$thn = date('Y');

$tanggal = date('Y-m-d');
$limit = 13;
$adjacents = 3;

$userip = getip();
function format_angka($a) {
$a = str_replace(",","",$a);
return $a;
}


$page 	= $_REQUEST['page'];
$q 		= $_REQUEST['q'];
$f1 	= $_REQUEST['f1'];
$f2 	= $_REQUEST['f2'];
$f3 	= $_REQUEST['f3'];

if(!$page){$page = 1;}

$ar_month = array("","I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII");
$xbln = $bln + 0;
$page_filter = "?q=".$q."&page=".$page."&f1=".$f1."&f2=".$f2;





$sqlstat = "SELECT * FROM order_status";
$stat_conn = $conn->query($sqlstat) or die($sqlstat);
while($stat = $stat_conn->fetch_assoc()){;
	$status_id = $stat['status_autoid'];
	$status_desc[$status_id] =  $stat['status_style'];
	$_status_desc[$status_id] =  $stat['status_desc'];

}

$sqlship = "SELECT * FROM shipping_type";
$ship_conn = $conn->query($sqlship) or die($sqlship);
while($ship = $ship_conn->fetch_assoc()){;
	$ship_id 				= $ship['ship_autoid'];
	$ship_desc[$ship_id] 	=  $ship['ship_desc'];
}

$sqlpaym = "SELECT * FROM payment";
$paym_conn = $conn->query($sqlpaym) or die($sqlpaym);
while($paym = $paym_conn->fetch_assoc()){;
	$paym_id 				= $paym['paym_autoid'];
	$paym_desc[$paym_id] 	=  $paym['paym_name'];
}

$sqlpc = "SELECT * FROM product_category";
$pc_conn = $conn->query($sqlpc) or die($sqlpc);
while($pc = $pc_conn->fetch_assoc()){;
	$_pc_autoid 			= $pc['pc_autoid'];
	$_pc_name[$_pc_autoid] 	= $pc['pc_name'];
}

$sqlsc = "SELECT * FROM product_subcategory";
$sc_conn = $conn->query($sqlsc) or die($sqlsc);
while($sc = $sc_conn->fetch_assoc()){;
	$_sc_autoid 			= $sc['sc_autoid'];
	$_sc_name[$_sc_autoid] 	= $sc['sc_name'];
	$_pc_autoid[$_sc_autoid] = $sc['pc_autoid'];
}


$sqlsc = "SELECT * FROM mst_customer";
$sc_conn = $conn->query($sqlsc) or die($sqlsc);
while($sc = $sc_conn->fetch_assoc()){;
	$_cust_id 			= $sc['cust_autoid'];
	$_cust_name[$_cust_id] 	= $sc['cust_name'];
}


// web config
$sqlconfig = "SELECT * FROM web_config WHERE web_id = 1";
$config_conn = $conn->query($sqlconfig) or die($sqlconfig);
$rsconfig = $config_conn->fetch_assoc();

	$web_name 			= $rsconfig['web_name'];
	$web_title 			= $rsconfig['web_title'];
	$web_logo 			= $rsconfig['web_image_logo'];
	$web_addr 			= $rsconfig['web_addr'];
	$web_contact_mail 	= $rsconfig['web_contact_mail'];
	$web_contact_wa 	= $rsconfig['web_contact_wa'];
	
	$web_mail_addr 		= $rsconfig['web_mail_addr'];
	$web_mail_name 		= $rsconfig['web_mail_name'];
	$web_mail_passwd 	= $rsconfig['web_mail_passwd'];
	$web_mail_host 		= $rsconfig['web_mail_host'];



function save_order_log($order_id,$status,$log_status_desc,$user,$user_type,$conn){
	
	$user = $conn->real_escape_string($user);

	$sqlchk = "SELECT COUNT(*) as num FROM order_log WHERE order_autoid = '$order_id' AND log_status = '$status'";
	$chk_con = $conn->query($sqlchk) or die($sqlchk);
	$rschk = $chk_con->fetch_assoc();
	if($rschk['num']>0){return false;}

	$insert = "INSERT INTO order_log 
				(order_autoid,log_status,log_status_desc,log_date,log_user,log_user_type) 
				VALUES 
				('$order_id','$status','$log_status_desc',CURRENT_TIMESTAMP(),'$user','$user_type')";
	$conn->query($insert) or die($insert);
}


$email_header = '
	<div>
	<img src="https://educarindo.co.id/image/logo.png" style="height:50px;float:left;margin-right:10px;">
	<h1>'.$web_name.'</h1>
	<hr>
	</div>';
	
function get_stok_info($id,$outlet,$conn){

	if($outlet=="0"){
		$sqlstok = "SELECT SUM(log_inqty - log_outqty) as stok FROM invlog WHERE pm_autoid = '$id' AND outlet_id = '0'";		
		}else{
		$sqlstok = "SELECT SUM(log_inqty - log_outqty) as stok FROM invlog WHERE pm_autoid = '$id' AND outlet_id = '$outlet'";		
			}

	$stok_con = $conn->query($sqlstok) or die($sqlstok);
	$rstok = $stok_con->fetch_assoc();	
	$stok_qty = $rstok['stok'];

				 
	return $stok_qty;


}

function get_latest_price($id,$outlet,$conn){
if(!$outlet){$outlet = 0;}
	$sqlstok = "SELECT * FROM invlog WHERE pm_autoid = '$id' AND outlet_id = '0' AND (curr_qty > 0 AND log_inqty > 0) order by log_refdate ASC";		
	$stok_con = $conn->query($sqlstok) or die($sqlstok);
	$rstok = $stok_con->fetch_assoc();	
	
	$price = $rstok['log_price'];
	
	return $price;
}

?>