
<?php

session_start();
@ob_start();
include "../../config/koneksi.php";

$module=$_GET['module'];
$act=$_GET['act'];

if ($module=='sales' AND $act=='add'){
	if($_SESSION['nama'] == 'KTR2'){
		$dtt = new DateTime($_POST['sls']);
		$dt1 = date_format($dtt, 'Y-m-d');
	}
	else{
		$dt1 = date("Y-m-d");
	}
	$produk = $_POST['produk'];
	$qty 	= $_POST['qty'];
	$harga 	= $_POST['price'];
	$disc 	= $_POST['disc'];
	$kasir 	= $_SESSION['nama'];
	include "updatestok2.php";
	//$stokk = mysql_result(mysql_query("select (IFNULL(sum(stok),0) + IFNULL(sum(stoktax),0)) from qprodukall where id = $produk"),0);
	$stokk = mysql_result(mysql_query("select IFNULL(sum(qty),0)  from qqtmpproduk where produk = $produk"),0);
	if ($stokk >= $qty){
		mysql_query("INSERT INTO tmpjualcsfm(produk,qty,harga,kasir,disc) VALUES('$produk','$qty','$harga','$kasir','$disc')");
		header('location:add.php');
	}else{	
		echo "<script>alert('Stok tidak mencukupi.'); window.location = 'add.php'</script>";
	}


}elseif ($module=='sales' AND $act=='edittambah'){
	$billno = $_GET['id'];
	$produk = $_POST['produk'];
	$qty 	= $_POST['qty'];
	$harga 	= $_POST['price'];
	$disc 	= $_POST['disc'];
	$kasir 	= $_SESSION['nama'];
	$ucode  = $_GET['ucode'];


	mysql_query("INSERT INTO jualcsfmdetail(billno,produk,qty,harga,disc,ucode) VALUES('$billno','$produk','$qty','$harga','$disc','$ucode')");
	header('location:edit.php?id='.$billno.'&ucode='.$ucode);


}elseif ($module=='view' AND $act=='penjualancs'){
	$awal = $_POST['awal'];
	$akhir = $_POST['akhir'];
	header('location:data.php?tglawal='.$awal.'&tglakhir='.$akhir);

}elseif ($module=='sales' AND $act=='delitem'){
	$id=$_GET['id'];	
	mysql_query("Delete from tmpjualcsfm where id = $id");
	header('location:add.php');
}elseif ($module=='sales' AND $act=='diskon'){
	$kasir=$_SESSION['nama'];;	
	$promo=$_POST['promo'];
	$promo = str_replace(".","",$promo);
	$promo = str_replace(",","",$promo);
	mysql_query("Update tmpjualcsfm set diskon = '$promo'  where kasir = '$kasir'");
	header('location:add.php');
}elseif ($module=='sales' AND $act=='filter'){
	$bank=$_POST['bank'];
	header('location:pembayaran.php?bank='.$bank);
}elseif ($module=='sales' AND $act=='edit'){
	$id=$_GET['id'];
	$page=$_GET['page'];
	$ucode=$_GET['ucode'];
	
	$qty = $_POST['qty'];
	$harga = $_POST['harga'];
	$disc = $_POST['disc'];
	mysql_query("Update jualcsfmdetail set qty = '$qty',harga = '$harga',disc = '$disc' where id = $id");
	header('location:edit.php?id='.$page.'&ucode='.$ucode);
}elseif ($module=='sales' AND $act=='edittgl'){
	$page=$_GET['page'];
	$ucode=$_GET['ucode'];
	
	$tgl = $_POST['tgl'];
	mysql_query("Update jualcsfm set billdate = '$tgl' where billno = $page and ucode = $ucode");
	header('location:edit.php?id='.$page.'&ucode='.$ucode);

}elseif ($module=='sales' AND $act=='editcust'){
	$page=$_GET['page'];
	$ucode=$_GET['ucode'];
	$cust = $_POST['cust'];
	mysql_query("Update jualcsfm set cust = '$cust' where billno = $page  and ucode = $ucode");
	header('location:edit.php?id='.$page.'&ucode='.$ucode);

}elseif ($module=='promo' AND $act=='edit'){
	$promo=$_POST['promo'];
	$promo = str_replace(".","",$promo);
	$promo = str_replace(",","",$promo);
	$id=$_GET['id'];
	mysql_query("Update tmpjualcsfm set promo = '$promo' where id = $id");
	header('location:add.php');
}elseif ($module=='sales' AND $act=='delinv'){
	$id=$_GET['id'];	
	$ucode=$_GET['ucode'];
	$kasir=$_SESSION['nama'];	
	mysql_query("update jualcsfm set status = 'D' where billno = $id and ucode = '$ucode'");
	
	mysql_query("insert into delinv(modul,ucode,billno,delby) values('Penjualan FM','$ucode','$id','$kasir')");
	header('location:data.php');
}elseif ($module=='sales' AND $act=='addall'){
	$dt1 = $_POST['tgl'];
	$cust 	= $_POST['cust'];
	$ket 	= $_POST['ket'];
	$kasir 	= $_SESSION['nama'];
	$diskon	= mysql_result(mysql_query("select diskon from tmpjualcsfm where kasir = '$kasir' limit 1"),0);
	
	//$piutang = mysql_result(mysql_query("select sum(grandtotal) from qpenjualantax where cust = $cust and byrby = ''"),0);
	//$limit   = mysql_result(mysql_query("select lim from customer where id = $cust "),0);
	//$totorder =  mysql_result(mysql_query("select sum(qty * (harga - (harga * disc / 100))) from tmpjualcsfm where kasir = '$kasir'"),0) + $diskon ;
	
	if(1 ==2 ){
		header('location:cekpiutang.php?cust='.$cust);
	}else{
			$ucode = substr($_POST['tgl'],2,2).substr($_POST['tgl'],5,2);
			$cekk  = mysql_result(mysql_query("select max(billno) from jualcsfm where ucode = '$ucode' order by billdate desc, billno desc limit 1 "),0);
			mysql_query("INSERT INTO jualcsfm(ucode,billdate,cust,ket,kasir,diskon) VALUES('$ucode','$dt1','$cust','$ket','$kasir','$diskon')");
			//echo"INSERT INTO jualcsfm(billdate,cust,ket,kasir,diskon) VALUES('$dt1','$cust','$ket','$kasir','$diskon')";
			sleep(1);
			$aab = mysql_insert_id();
			$billno	= mysql_result(mysql_query("select max(billno) from jualcsfm where ucode = '$ucode' order by billdate desc, billno desc limit 1 "),0);
			if($cekk + 1 > $billno){
				$billno = $cekk + 1 ;
			}
			
			$cek = mysql_query("Select * from tmpjualcsfm where kasir = '$kasir' order by id asc");
			while ($r=mysql_fetch_array($cek)){	
				$produk = $r['produk'];
				$qty = $r['qty'];
				$harga = $r['harga'];
				$disc = $r['disc'];

				mysql_query("INSERT INTO jualcsfmdetail(billno,produk,qty,harga,disc,ucode) VALUES('$billno','$produk','$qty','$harga','$disc','$ucode')");

			}
			mysql_query("delete from tmpjualcsfm where kasir = '$kasir'");

			include "updatestok2.php";
			header('location:data.php');
	}
}
elseif ($module=='sales' AND $act=='validasibyr'){
	$id = $_GET[id];
	$dtV = date("Y-m-d H:i:s");
	$kasir = $_SESSION['nama'];
	
	mysql_query("Update jualcsfm set byrby = '$kasir', valbyr = '$dtV',bayar = 'Y' where billno = $id");
	header('location:pembayaran.php');

}elseif ($module=='sales' AND $act=='validasibyrwh'){
	$id = $_GET[id];
	$dtV = date("Y-m-d H:i:s");
	$kasir = $_SESSION['nama'];
	
	mysql_query("Update jualcsfm set byrby2 = '$kasir', valbyr2 = '$dtV',bayar2 = 'Y' where billno = $id");
	header('location:pembayaran.php');

}elseif ($module=='sales' AND $act=='validasikrm'){
	$id = $_GET[id];
	$dtV = date("Y-m-d H:i:s");
	$kasir = $_SESSION['nama']; 
	
	mysql_query("Update jualcsfm set krmby = '$kasir', valkrm = '$dtV',kirim = 'Y' where billno = $id");
	header('location:data.php');

}

elseif ($module=='sales' AND $act=='profile'){
	$uid=$_SESSION['idm'];
	$pass = $_POST['pass1'];
	mysql_query("update admins set pass = '$pass' where pk = $uid");
	header('location:profile.php');

}

elseif ($module=='sales' AND $act=='ap1'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol1 = 'Y' where billno = $id");
	header('location:crm.php');

}
elseif ($module=='sales' AND $act=='editdiskonn'){
	$id = $_GET['id'];
	$ucode = $_GET['ucode'];
	$diskon = $_POST['diskonn'];
	mysql_query("Update jualcsfm set diskon = '$diskon' where billno = $id");
	header('location:edit.php?id='.$id.'&ucode='.$ucode);

}elseif ($module=='sales' AND $act=='ap2'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol2 = 'Y' where billno = $id");
	header('location:crm.php');

}
elseif ($module=='sales' AND $act=='uap2'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol2 = 'N' where billno = $id");
	header('location:crm.php');

}elseif ($module=='sales' AND $act=='ap3'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol3 = 'Y' where billno = $id");
	header('location:crm.php');

}
elseif ($module=='sales' AND $act=='uap3'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol3 = 'N' where billno = $id");
	header('location:crm.php');

}elseif ($module=='sales' AND $act=='ap4'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol4 = 'Y' where billno = $id");
	header('location:crm.php');

}
elseif ($module=='sales' AND $act=='uap4'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol4 = 'N' where billno = $id");
	header('location:crm.php');

}elseif ($module=='sales' AND $act=='ap5'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol5 = 'Y' where billno = $id");
	header('location:crm.php');

}
elseif ($module=='sales' AND $act=='uap5'){
	$id = $_GET[id];
	mysql_query("Update jualcs set fol5 = 'N' where billno = $id");
	header('location:crm.php');

}elseif ($module=='sales' AND $act=='delitemss'){
	$id = $_GET[id];
	$page = $_GET['page'];
	$ucode = $_GET['ucode'];
	$kasir 	= $_SESSION['nama'];
	$produk	= mysql_result(mysql_query("select produk from jualcsfmdetail where id = $id "),0);
	$qty	= mysql_result(mysql_query("select qty from jualcsfmdetail where id = $id "),0);
	$harga	= mysql_result(mysql_query("select harga from jualcsfmdetail where id = $id "),0);
	
	mysql_query("INSERT INTO deldata(modul,kasir,produk,qty,harga) VALUES('SalesFM','$kasir','$produk','$qty','$harga')");
	mysql_query("delete from jualcsfmdetail where id = $id");
	header('location:edit.php?id='.$page.'&ucode='.$ucode);

}

?>
