	<?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);
	$time   	= gmdate("h:i");
	
	$timestamp = new DateTime(null, new DateTImeZone('Africa/Nairobi'));
	$tanggal = $timestamp->format('Y-m-d');
	$time = $timestamp->format('H:i');




	$id_table							= $decoded["id_table"];
	$id_pelanggan						= $decoded["id_customer"];
	$jatuh_tempo						= $decoded["due_date"];
	$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"];
	$complaint							= $decoded["complaint"];
	$advice								= $decoded["advice"];

	$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);
	
	$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['prosentase']/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";
	
	
	if($dt['typestore']=="Culinary"){
		$status="billing";
	}else{
		$status="process";
	}	
	
	for($x = 0; $x<count($decoded["product"]); $x++)
        {
	$id_barang			= $decoded["product"][$x]["id_product"];
	$jumlah_barang		= $decoded["product"][$x]["amount_product"];
	$catatan			= $decoded["product"][$x]["notes"];



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


	$totalharga	=$b['selling_price']*$jumlah_barang;
	$stok		=$b['stock']-$jumlah_barang;
    $stoknya		=$b['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', '$b[selling_price]', '$totalharga', '$tanggal', '$catatan', '$stoknya')");
  		if($b['have_stock']=='1'){
		mysqli_query($connect,"UPDATE product SET stock   = '$stok' WHERE id_product  = '$id_barang'");
		
						   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,"INSERT INTO sales_data(id_customer,
								  no_invoice,
								  date,
								  id_store,
								  totalorder,
								  totalprice,
								  status,
								  due_date,
								  operator,
								  tax,
								  discount,
								  service_charge,
								  id_table,
								  location,
                                  user) 
                    VALUES('$id_pelanggan',
						   '$no_invoice',
						   '$tanggal',
						   '$u[id_store]',
						   '$total_bayar',
						   '$total_order',
						   '$status',
						   '$jatuh_tempo',
						   '$u[phone_number]',
						   '$nominalpajak',
						   '$diskon',
						   '$nominalsc',
						   '$id_table',
						   '$latitude, $longitude',
                           '$m[phone_number]')");
						   

		mysqli_query($connect,"INSERT INTO history_job(no_invoice,
								  note,
								  operator,
								  status,
								  date,
                                  user) 
					VALUES('$no_invoice',
						   'Recipe $no_invoice created by $u[full_name]',
						   '$status',
						   '$operator',
						   '$tanggal $time',
                           '$m[phone_number]')");
						   
		mysqli_query($connect,"INSERT INTO medical_record(id_customer,
								  no_invoice,
								  complaint,
								  advice,
								  date,
								  operator,
                                  user) 
					VALUES('$id_pelanggan',
						   '$no_invoice',
						   '$complaint',
						   '$advice',
						   '$tanggal $time',
						   '$u[phone_number]',
                           '$m[phone_number]')");				   
						   
  

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

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

	echo json_encode($responses);


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

    echo json_encode($json);	
	}
	?>