	<?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", time()+60*60*7);



	$no_invoice							= $decoded["no_invoice"];
	$total_order_new						= $decoded["total_order"];
	$key								= $decoded["key"];

	$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 * FROM sales_data WHERE no_invoice='$no_invoice'");
	$dp=mysqli_fetch_array($querydp);
	
	
	$total_order = $dp['totalprice']+$total_order_new;
	
	
	$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='$dp[id_discount]'");
	$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){
		
		if($dp['status']=='debt' OR $dp['status']=='cancel'){
			
	$json = array(
    'status' => 'true',
    'errCode' => '02',
	'msg' => 'Orders cannot be added for debt and cancel transactions'
	);

    echo json_encode($json);	
		}else{
	
	$date		= gmdate("dis");
	
	$nama = $dt['name_store'];
	$arr = explode(' ', $nama);
	$singkatan = "";
	foreach($arr as $kata)
	{
	$singkatan .= substr($kata, 0, 1);
	}
	
	if($dp['status']=='billing'){
	$status="billing";
	}else{
	$status="finish";	
	}
	
	
	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);
	
	
	$sales = mysqli_query($connect,"SELECT * FROM sales WHERE no_invoice='$no_invoice' AND id_product='$id_barang'");
	$s=mysqli_fetch_array($sales);
	
			$amount= $jumlah_barang+$s['amount'];



	$totalharga	=$b['selling_price']*$amount;
	$stok		=$b['stock']-$amount;
    $stoknya	=$b['stock']-$amount;
	
	if($id_barang == $s['id_product']){
		
		mysqli_query($connect,"UPDATE sales SET amount = '$amount',totalprice = '$amount', note ='$catatan', rest_stock = '$stoknya' WHERE no_invoice='$no_invoice' AND id_product = '$id_barang'");

	}else{
		
		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', '$dp[id_customer]', '$u[id_store]', '$m[phone_number]', '$no_invoice', '$dp[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'");
		$query30	=mysqli_query($connect,"UPDATE stock SET stock='$newstock' WHERE id_product ='$id_barang'");
		}
		
		}
	mysqli_query($connect,"UPDATE sales_data SET    totalorder		= '$total_bayar',
													tax		 		= '$nominalpajak',
													service_charge	= '$nominalsc',
													discount		= '$diskon',
													totalprice		= '$total_order'
										WHERE no_invoice 	 = '$no_invoice'");
										
	$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);	
	}
	?>