	<?php
	/**
	* Created by PhpStorm.
	* User: ahmad
	* Date: 20/08/2019
	* Time: 11:48 AM
	*/

	require_once("../connection.php");
	$content 	= trim(file_get_contents("php://input"));
	$decoded 	= json_decode($content, true);
	$tanggal	= gmdate("Y-m-d");
	
	$setting = mysqli_query($connect,"SELECT * FROM setting");
	$sett=mysqli_fetch_array($setting);



	$id_pelanggan						= $decoded["id_customer"];
	$jatuh_tempo						= $decoded["due_date"];
	$jumlah_pembayaran					= $decoded["payment_amount"];
	$total_order						= $decoded["total_order"];
	$key								= $decoded["key"];
	$tipe_pembayaran					= $decoded["payment_type"];
	$service_charge						= $decoded["service_charge"];
	$pajak								= $decoded["tax"];
	$id_diskon							= $decoded["id_discount"];
	$card								= $decoded["card"];
	$latitude							= $decoded["latitude"];
	$longitude							= $decoded["longitude"];
	$keterangan							= $decoded["note"];
	$id_table							= $decoded["id_table"];

	$getusers	= mysqli_query($connect,"SELECT * FROM users WHERE id_session='$key'");
	$u	   		= mysqli_fetch_array($getusers);
	$jumlah   	= mysqli_num_rows($getusers);
	
	
	$masterakun = mysqli_query($connect,"SELECT * FROM users WHERE phone_number='$u[master]'");
	$m=mysqli_fetch_array($masterakun);
	
	$sumary = mysqli_query($connect,"SELECT * FROM sumary WHERE cashier_id='$u[phone_number]' AND create_at='$tanggal'");
	$ambil=mysqli_num_rows($sumary);
	if ($ambil > 0){
	}else{
		mysqli_query($connect,"INSERT INTO sumary(cashier_id,
								  create_at,
								  id_store,
								  user) 
                    VALUES('$u[phone_number]',
						   '$tanggal',
						   '$u[id_store]',
						   '$m[phone_number]')");
	}



	$querydp = mysqli_query($connect,"SELECT id_sales_data FROM sales_data WHERE user='$m[phone_number]' ORDER BY id_sales_data DESC LIMIT 1");
	$dp=mysqli_fetch_array($querydp);
	
	$datatoko = mysqli_query($connect,"SELECT * FROM store WHERE id_store='$u[id_store]'");
	$dt=mysqli_fetch_array($datatoko);
	
	$datadiskon = mysqli_query($connect,"SELECT * FROM discount WHERE id_discount='$id_diskon'");
	$d=mysqli_fetch_array($datadiskon);
	
	if($d['type']=='prosentase'){
		$diskons=$total_order*$d['nominal']/100;
		$diskon= $diskons;
	}else{
		$diskon=$d['nominal'];
	}
	
	$hargasetelahdiskon=$total_order-$diskon;
	
    $nominalscc=$hargasetelahdiskon*$dt['service_charge']/100;
    $nominalsc=$nominalscc;
	$total_service_charge=$hargasetelahdiskon+$nominalsc;
	
	$nominalpajaks=$total_service_charge*$dt['tax']/100;
	$nominalpajak=$nominalpajaks;
	$total_bayar=$total_service_charge+$nominalpajak;

	if ($jumlah > 0){
	
	$date		= gmdate("dis");
	
	$nama = $dt['name_store'];
	$arr = explode(' ', $nama);
	$singkatan = "";
	foreach($arr as $kata)
	{
	$singkatan .= substr($kata, 0, 1);
	}
	
		$kode= strtoupper($singkatan);
	//$no_invoice	= "$kode-$dp[id_sales_data]$date";
	

	$nourut = $dp['id_sales_data']+1; // kode_barang dengan angka terbesar
	$serial_number = $nourut; // mencetak no urut dengan 4 angka, lihat bedanya jika hanya menggunakan echo $nourut;
	$sn = sprintf("%04s", $nourut) ; // mencetak no urut dengan 4 angka, lihat bedanya jika hanya menggunakan echo $nourut;
	
	$no_invoice	= "$kode-$sn";
	
	
	
	
	
	
	if($tipe_pembayaran==1){
		$pembayaran="cash";
		$status="paid off";
		$dibayar=$jumlah_pembayaran;
		
		$kembalian	= $dibayar-$total_bayar;

	
	
	for($x = 0; $x<count($decoded["product"]); $x++)
        {
	$id_barang			= $decoded["product"][$x]["id_product"];
	$jumlah_barang		= $decoded["product"][$x]["amount_product"];
	$price			= $decoded["product"][$x]["new_price"];
	$catatan			= $decoded["product"][$x]["notes"];



	$barang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$id_barang'");
	$b=mysqli_fetch_array($barang);


	$totalharga	=$price*$jumlah_barang;
	$stok		=$b['stock']-$jumlah_barang;
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$id_barang' AND id_store='$u[id_store]'");
	$s=mysqli_fetch_array($datastok);
    $stoknya		=$s['stock']-$jumlah_barang;

	mysqli_query($connect,"INSERT INTO sales(id_product, amount, id_customer, id_store, user, no_invoice, status, price, totalprice, date, note, rest_stock) 
                    VALUES('$id_barang', '$jumlah_barang', '$id_pelanggan', '$u[id_store]', '$m[phone_number]', '$no_invoice', '$status', '$price', '$totalharga', '$tanggal', '$catatan', '$stoknya')");
  
			mysqli_query($connect,"UPDATE product SET stock   = '$stok' WHERE id_product  = '$id_barang'");
			if($b['have_stock']=='1'){
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$id_barang' AND id_store='$u[id_store]'");
			
			
						
						   mysqli_query($connect,"INSERT INTO history_stock(id_product,
								  id_store,
								  stock,
								  detail,
								  status,
								  date,
                                  user) 
					VALUES('$id_barang',
						   '$u[id_store]',
						   '$jumlah_barang',
						   'Transaction  $no_invoice',
						   '1',
						   '$tanggal',
                           '$m[phone_number]')");
						   
			}
	
		}
		
		mysqli_query($connect,"UPDATE db_table SET status   = '1' WHERE id_table  = '$id_table'");
	

	mysqli_query($connect,"INSERT INTO sales_data(id_customer,
								  no_invoice,
								  date,
								  id_store,
								  payment,
								  totalorder,
								  totalpay,
								  totalprice,
								  changepay,
								  status,
								  due_date,
								  operator,
								  tax,
								  discount,
								  service_charge,
								  id_table,
								  location,
								  shift,
                                  user) 
                    VALUES('$id_pelanggan',
						   '$no_invoice',
						   '$tanggal',
						   '$u[id_store]',
						   '$pembayaran',
						   '$total_bayar',
						   '$dibayar',
						   '$total_order',
						   '$kembalian',
						   '$status',
						   '$jatuh_tempo',
						   '$u[phone_number]',
						   '$nominalpajak',
						   '$diskon',
						   '$nominalsc',
						   '$id_table',
						   '$latitude, $longitude',
						   '$u[shift]',
                           '$m[phone_number]')");
						   
						   
						   
	if($sett['point_system']=='yes'){					   
						   
	$datapelanggan = mysqli_query($connect,"SELECT * FROM customer WHERE id_customer='$id_pelanggan'");
	$p=mysqli_fetch_array($datapelanggan);
	
	if($id_pelanggan == ""){
		
	}else{
		$totalpoint= $total_order/$sett['multiple_point'];
		$point=$p['point']+$totalpoint;
		mysqli_query($connect,"UPDATE customer SET point   = '$point' WHERE id_customer  = '$id_pelanggan'");

		mysqli_query($connect,"INSERT INTO point(id_customer,
								  detail,
								  date,
								  nominal,
								  point,
                                  user) 
                    VALUES('$id_pelanggan',
						   'point for invoice $no_invoice',
						   '$tanggal',
						   '$totalpoint',
						   '$point',
                           '$m[phone_number]')");

	}
	}


	}else if ($tipe_pembayaran==2){
		$pembayaran="non cash";
		$status="paid off";
		$dibayar=$jumlah_pembayaran;
		
		$kembalian	= $jumlah_pembayaran;

	
	
	for($x = 0; $x<count($decoded["product"]); $x++)
        {
	$id_barang			= $decoded["product"][$x]["id_product"];
	$jumlah_barang		= $decoded["product"][$x]["amount_product"];
	$price			= $decoded["product"][$x]["new_price"];
	$catatan			= $decoded["product"][$x]["notes"];



	$barang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$id_barang'");
	$b=mysqli_fetch_array($barang);


	$totalharga	=$price*$jumlah_barang;
	$stok		=$b['stock']-$jumlah_barang;
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$id_barang' AND id_store='$u[id_store]'");
	$s=mysqli_fetch_array($datastok);
    $stoknya		=$s['stock']-$jumlah_barang;

	mysqli_query($connect,"INSERT INTO sales(id_product, amount, id_customer, id_store, user, no_invoice, status, price, totalprice, date, note, rest_stock) 
                    VALUES('$id_barang', '$jumlah_barang', '$id_pelanggan', '$u[id_store]', '$m[phone_number]', '$no_invoice', '$status', '$price', '$totalharga', '$tanggal', '$catatan', '$stoknya')");
  
			mysqli_query($connect,"UPDATE product SET stock   = '$stok' WHERE id_product  = '$id_barang'");
			
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$id_barang' AND id_store='$u[id_store]'");
			
			  mysqli_query($connect,"INSERT INTO history_stock(id_product,
								  id_store,
								  stock,
								  detail,
								  status,
								  date,
                                  user) 
					VALUES('$id_barang',
						   '$u[id_store]',
						   '$jumlah_barang',
						   'Transaction  $no_invoice',
						   '1',
						   '$tanggal',
                           '$m[phone_number]')");
	
		}
		mysqli_query($connect,"UPDATE db_table SET status   = '1' WHERE id_table  = '$id_table'");

	mysqli_query($connect,"INSERT INTO sales_data(id_customer,
								  no_invoice,
								  date,
								  id_store,
								  payment,
								  totalorder,
								  totalpay,
								  totalprice,
								  changepay,
								  status,
								  due_date,
								  operator,
								  tax,
								  discount,
								  service_charge,
								  note,
								  location,
								  shift,
								  id_table,
                                  user) 
                    VALUES('$id_pelanggan',
						   '$no_invoice',
						   '$tanggal',
						   '$u[id_store]',
						   '$pembayaran',
						   '$total_bayar',
						   '$total_bayar',
						   '$total_order',
						   '$kembalian',
						   '$status',
						   '$jatuh_tempo',
						   '$u[phone_number]',
						   '$nominalpajak',
						   '$diskon',
						   '$nominalsc',
						   '$card $keterangan',
						   '$latitude,$longitude',
						   '$u[shift]',
						   '$id_table',
                           '$m[phone_number]')");
						   
	if($sett['point_system']=='yes'){
		$datapelanggan = mysqli_query($connect,"SELECT * FROM customer WHERE id_customer='$id_pelanggan'");
	$p=mysqli_fetch_array($datapelanggan);
	
	if($id_pelanggan == ""){
		
	}else{
		$totalpoint= $total_order/$sett['multiple_point'];
		$point=$p['point']+$totalpoint;
		mysqli_query($connect,"UPDATE customer SET point   = '$point' WHERE id_customer  = '$id_pelanggan'");

		mysqli_query($connect,"INSERT INTO point(id_customer,
								  detail,
								  date,
								  nominal,
								  point,
                                  user) 
                    VALUES('$id_pelanggan',
						   'point for invoice $no_invoice',
						   '$tanggal',
						   '$totalpoint',
						   '$point',
                           '$m[phone_number]')");

	}
	}

	}else if($tipe_pembayaran==3){
		$pembayaran="debt";
		$status="debt";
		$dibayar=$jumlah_pembayaran;

					
					mysqli_query($connect,"UPDATE customer SET debt = '1'
                             WHERE id_customer = '$id_pelanggan'");
					
	    mysqli_query($connect,"INSERT INTO customerdebthistory(id_customer, user, status, no_invoice, nominal, date) 
                    VALUES('$id_pelanggan', '$m[phone_number]', '$status', '$no_invoice', '$total_bayar', '$tanggal')");
		
		$kembalian	= $dibayar-$total_bayar;

	
	
	for($x = 0; $x<count($decoded["product"]); $x++)
        {
	$id_barang			= $decoded["product"][$x]["id_product"];
	$jumlah_barang		= $decoded["product"][$x]["amount_product"];
	$price			= $decoded["product"][$x]["new_price"];
	$catatan			= $decoded["product"][$x]["notes"];



	$barang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$id_barang'");
	$b=mysqli_fetch_array($barang);


	$totalharga	=$price*$jumlah_barang;
	$stok		=$b['stock']-$jumlah_barang;
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$id_barang' AND id_store='$u[id_store]'");
	$s=mysqli_fetch_array($datastok);
    $stoknya		=$s['stock']-$jumlah_barang;

	mysqli_query($connect,"INSERT INTO sales(id_product, amount, id_customer, id_store, user, no_invoice, status, price, totalprice, date, note, rest_stock) 
                    VALUES('$id_barang', '$jumlah_barang', '$id_pelanggan', '$u[id_store]', '$m[phone_number]', '$no_invoice', '$status', '$price', '$totalharga', '$tanggal', '$catatan', '$stoknya')");
  
			mysqli_query($connect,"UPDATE product SET stock   = '$stok' WHERE id_product  = '$id_barang'");
			
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$id_barang' AND id_store='$u[id_store]'");
	  mysqli_query($connect,"INSERT INTO history_stock(id_product,
								  id_store,
								  stock,
								  detail,
								  status,
								  date,
                                  user) 
					VALUES('$id_barang',
						   '$u[id_store]',
						   '$jumlah_barang',
						   'Transaction  $no_invoice',
						   '1',
						   '$tanggal',
                           '$m[phone_number]')");
		}
		
	
mysqli_query($connect,"UPDATE db_table SET status   = '1' WHERE id_table  = '$id_table'");
	mysqli_query($connect,"INSERT INTO sales_data(id_customer,
								  no_invoice,
								  date,
								  id_store,
								  payment,
								  totalorder,
								  totalpay,
								  totalprice,
								  changepay,
								  status,
								  due_date,
								  operator,
								  tax,
								  discount,
								  service_charge,
								  location,
								  shift,
								  id_table,
                                  user) 
                    VALUES('$id_pelanggan',
						   '$no_invoice',
						   '$tanggal',
						   '$u[id_store]',
						   '$pembayaran',
						   '$total_bayar',
						   '$dibayar',
						   '$total_order',
						   '$kembalian',
						   '$status',
						   '$jatuh_tempo',
						   '$u[phone_number]',
						   '$nominalpajak',
						   '$diskon',
						   '$nominalsc',
						   '$latitude, $longitude',
						   '$u[shift]',
						   '$id_table',
                           '$m[phone_number]')");
						   
						   
	if($sett['point_system']=='yes'){
		$datapelanggan = mysqli_query($connect,"SELECT * FROM customer WHERE id_customer='$id_pelanggan'");
	$p=mysqli_fetch_array($datapelanggan);
	
	if($id_pelanggan == ""){
		
	}else{
				   
		$totalpoint= $total_order/$sett['multiple_point'];
		$point=$p['point']+$totalpoint;
		mysqli_query($connect,"UPDATE customer SET point   = '$point' WHERE id_customer  = '$id_pelanggan'");

		mysqli_query($connect,"INSERT INTO point(id_customer,
								  detail,
								  date,
								  nominal,
								  point,
                                  user) 
                    VALUES('$id_pelanggan',
						   'point for invoice $no_invoice',
						   '$tanggal',
						   '$totalpoint',
						   '$point',
                           '$m[phone_number]')");

	}
	}
		
	}else if ($tipe_pembayaran==4){
		$pembayaran="point";
		$status="paid off";
		$dibayar=$jumlah_pembayaran;
		
		$kembalian	= $jumlah_pembayaran;
		
	$datapelanggan = mysqli_query($connect,"SELECT * FROM customer WHERE id_customer='$id_pelanggan'");
	$p=mysqli_fetch_array($datapelanggan);

	
	
	for($x = 0; $x<count($decoded["product"]); $x++)
        {
	$id_barang			= $decoded["product"][$x]["id_product"];
	$jumlah_barang		= $decoded["product"][$x]["amount_product"];
	$price			= $decoded["product"][$x]["new_price"];
	$catatan			= $decoded["product"][$x]["notes"];



	$barang = mysqli_query($connect,"SELECT * FROM product WHERE id_product='$id_barang'");
	$b=mysqli_fetch_array($barang);


	$totalharga	=$price*$jumlah_barang;
	$stok		=$b['stock']-$jumlah_barang;
	$datastok = mysqli_query($connect,"SELECT * FROM stock WHERE id_product='$id_barang' AND id_store='$u[id_store]'");
	$s=mysqli_fetch_array($datastok);
    $stoknya		=$s['stock']-$jumlah_barang;

	mysqli_query($connect,"INSERT INTO sales(id_product, amount, id_customer, id_store, user, no_invoice, status, price, totalprice, date, note, rest_stock) 
                    VALUES('$id_barang', '$jumlah_barang', '$id_pelanggan', '$u[id_store]', '$m[phone_number]', '$no_invoice', '$status', '$price', '$totalharga', '$tanggal', '$catatan', '$stoknya')");
  
			mysqli_query($connect,"UPDATE product SET stock   = '$stok' WHERE id_product  = '$id_barang'");
			
			mysqli_query($connect,"UPDATE stock SET stock   = '$stoknya' WHERE id_product  = '$id_barang' AND id_store='$u[id_store]'");
	
		}
		
	
	if($id_pelanggan == $p['id_customer']){
		$point=$p['point']-$total_bayar;
		mysqli_query($connect,"UPDATE customer SET point   = '$point' WHERE id_customer  = '$id_pelanggan'");
		
		
	mysqli_query($connect,"INSERT INTO point(id_customer,
								  detail,
								  date,
								  nominal,
								  point,
								  status,
                                  user) 
                    VALUES('$id_pelanggan',
						   'payment with points for invoice $no_invoice',
						   '$tanggal',
						   '$total_bayar',
						   '$point',
						   '1',
                           '$m[phone_number]')");

	}


	mysqli_query($connect,"INSERT INTO sales_data(id_customer,
								  no_invoice,
								  date,
								  id_store,
								  payment,
								  totalorder,
								  totalpay,
								  totalprice,
								  changepay,
								  status,
								  due_date,
								  operator,
								  tax,
								  discount,
								  service_charge,
								  note,
								  location,
								  shift,
                                  user) 
                    VALUES('$id_pelanggan',
						   '$no_invoice',
						   '$tanggal',
						   '$u[id_store]',
						   '$pembayaran',
						   '$total_bayar',
						   '$total_bayar',
						   '$total_order',
						   '$kembalian',
						   '$status',
						   '$jatuh_tempo',
						   '$u[phone_number]',
						   '$nominalpajak',
						   '$diskon',
						   '$nominalsc',
						   '$card $keterangan',
						   '$latitude,$longitude',
						   '$u[shift]',
                           '$m[phone_number]')");

	}
	

	
  

	$responses = array(
    'status' 	=> 'true',
    'errCode' 	=> '01',
	'msg' 		=> 'Success'
	);
   
	$responses["data"] = array();

  
	$responses["data"] = array(
	'invoice' 	=> $no_invoice
	);
  
	array_push($responses["data"]);

	echo json_encode($responses);


	}else{
	
	
	$json = array(
    'status' 	=> 'true',
    'errCode' 	=> '00',
	'msg' 		=> 'User Not Found'
	);

    echo json_encode($json);	
	}
	?>