/home2/kerimkazan/dvf/ajax.php
<?php
function removeSizeLabel($data)
{
    foreach ($data as &$product) {
        $product['variant'] = str_replace('Beden: ', '', $product['variant']);
    }
    return $data;
}

include('config.php');

if(m_get_session('m_admin')=='' or !m_admin_check())
{
	$result['status'] = false;
	$result['login'] = true;
	
	echo json_encode($result);
	
	exit;
}

if($_POST)

{


switch(m_u_p('type'))

{
	
case 'barcode_scan':


	$result['status'] = false;
	$result['msg'] = 'Ürün bulunamadı!';
	
	$informations = $db->table('products')->where('barcode','=',m_u_p('barcode'))->where('status','=','1')->get();
	if($informations['total_count']>0)
	{
		$stock = $db->select('sum(variant_stock) as stock')->table('product_variants')->where('p_id','=',$informations['data'][0]['id'])->get();
		$stock = $stock['data'][0]['stock'];
		
	
		if($stock=='')
		{
			$stock = 0;
		}
		
		if($stock>0)
		{
			$result['status'] = true;
			$result['id'] = $informations['data'][0]['id'];
			$result['barcode'] = $informations['data'][0]['barcode'];
			$result['name'] = $informations['data'][0]['name'];
			$result['image'] = m_image_url($informations['data'][0]['image']);
		}
		else
		{
			$result['status'] = false;
			$result['msg'] = 'Ürüne Ait Stok Yok!';
		}
	}
	else
	{
		$result['status'] = false;
		$result['msg'] = 'Ürün bulunamadı!';
	}
	
	
	echo json_encode($result);
	
break;


case 'print_forms':
	$saleId = $_POST['saleId'];
	$sale = $db->table('sales')->where('id', '=', $saleId)->get_vars();
	$saleProducts = $db->table('sales_products')->where('s_id', '=', $saleId)->get();

	if ($sale && $saleProducts['total_count'] > 0) {
		$brand = m_setting('brand');
		$address = m_setting('address');
		$phone = m_setting('phone');
		$email = m_setting('email');

		foreach ($saleProducts['data'] as &$product) {
			$productId = $product['p_id'];
			$variant = $db->table('product_variants')->where('id', '=', $product['v_id'])->get_vars();
			$productName = $db->table('products')->where('id', '=', $productId)->get_vars('name');
			$product['name'] = $productName['name'];
			$product['variant'] = $variant['variant_type'] . ': ' . $variant['variant_value'];
		}

		$saleProductsData = removeSizeLabel($saleProducts['data']);

		$response = [
			'status' => true,
			'sale' => $sale,
			'saleProducts' => $saleProductsData,
			'brand' => $brand,
			'address' => $address,
			'phone' => $phone,
			'email' => $email
		];
	} else {
		$response = [
			'status' => false,
			'msg' => 'Satış bulunamadı!'
		];
	}

	echo json_encode($response);

	break;




case 'sale_barcode_scan':


	$result['status'] = false;
	$result['msg'] = 'Ürün bulunamadı!';
	
	$informations = $db->table('products')->where('barcode','=',m_u_p('barcode'))->where('status','=','1')->get();
	if($informations['total_count']>0)
	{
		$info = $informations['data'][0];
		
		$price_type = $info['price_type'];
		$sale_price = $info['sale_price'];
		
		
		if($price_type=='$')
		{
			$sale_price = $sale_price*m_setting('usd_try');
		}
		
		if($price_type=='€')
		{
			$sale_price = $sale_price*m_setting('eur_try');
		}
		$rand = uniqid();
		
		
		$product_variants = $db->table('product_variants')->where('p_id','=',$info['id'])->get();
		
		if($product_variants['total_count']>0)
		{
			$product_variant = '<div><select class="form-select" name="product_variant[]">
			<option value="">Özellik Seçiniz</option>
			';
			
			foreach($product_variants['data'] as $p_variant)
			{
				if($p_variant['variant_stock']>0)
				{
					$product_variant.= '<option value="'.$p_variant['variant_type'].':'.$p_variant['variant_value'].'">'.$p_variant['variant_type'].':'.$p_variant['variant_value'].'</option>';
				}
			}
			
			$product_variant.= '</select></div>';
		}
		else
		{
			$product_variant = '<div><select class="form-select" name="product_variant[]">';
			
			
			$product_variant.= '<option value="">Özellik Yok</option>';
			
			$product_variant.= '</select></div>';
		}
		
		$result['status'] = true;
		$result['html'] = '
		
		<div class="sale_product">
		<input type="hidden" name="product_id[]" value="'.$info['id'].'">
		<input type="hidden" name="product_rand[]" value="'.$rand.'">
		<div class="row">
		
			<div class="col-lg-4">
				<div class="mb-1">
				
				 <label class="form-label fw-bold">Ürün</label>
				 <input type="text" class="form-control" name="product_name[]" value="'.$info['name'].'" readonly>
					
				</div>
			</div>
			<div class="col-lg-2">
				<div class="mb-1">
				
				 <label class="form-label fw-bold">Özellik</label>
				 '.$product_variant.'
					
				</div>
			</div>
			<div class="col-lg-2">
				<div class="mb-1">
				
				 <label class="form-label fw-bold">Miktar</label>
				 <input type="number" class="form-control" name="product_quantity[]" value="1">
					
				</div>
			</div>
			<div class="col-lg-2">
				<div class="mb-1">
				
				 <label class="form-label fw-bold">Fiyat</label>
				 <input type="text" class="form-control" name="product_price[]" value="'.m_currency($sale_price).'" readonly>
					
				</div>
			</div>
			<div class="col-lg-2">
				<div class="mb-1">
				
				 <label class="form-label fw-bold">Tutar</label>
				 <input type="text" class="form-control product_total_'.$rand.'" name="product_total[]" value="'.m_currency($sale_price).'" readonly>
				</div>
			</div>
		
		</div>
		
		<span class="btn btn-sm btn-danger sale_product_remove w-100"><i class="fa fa-trash"></i></span>	
		
		</div>
		
		
		';
	}
	else
	{
		$result['status'] = false;
		$result['msg'] = 'Ürün bulunamadı!';
	}
	
	
	echo json_encode($result);
	
break;	


case 'sale_form_calculate':


	$total = 0;
	$net_total = 0;
	$discount = 0;
	
	$products_html = '';
	$n=0;
	foreach($_POST['product_id'] as $id)
	{
		$quantity = $_POST['product_quantity'][$n];
		$variant = $_POST['product_variant'][$n];
		
		$info = $db->table('products')->where('id','=',$id)->where('status','=','1')->get_vars();
		
		$price_type = $info['price_type'];
		$sale_price = $info['sale_price'];
		
		
		if($price_type=='$')
		{
			$sale_price = $sale_price*m_setting('usd_try');
		}
		
		if($price_type=='€')
		{
			$sale_price = $sale_price*m_setting('eur_try');
		}
		
		$product_total = $sale_price*$quantity;
		
		$variant_info = '';
		
		if($variant!='')
		{
			$variant_info = ' ( '.$variant.' )';
		}
		
		$products_html.= '<tr><td>'.$info['name'].''.$variant_info.'</td><td>'.$quantity.'</td><td>'.m_currency($sale_price).'</td><td>'.m_currency($product_total).'</td></tr>';
		
		$result['products'][] = ['id' => $id,'total_price' => m_currency($product_total), 'rand' => $_POST['product_rand'][$n]];
		
		$total = $total+$product_total;
		
		$n++;
		
		
	}
	
	$discount = m_percent($total,m_u_p('discount'));
	$net_total = $total-$discount;
	$result['discount'] = m_currency($discount);
	$result['total'] = m_currency($total);
	$result['net_total'] = m_currency($net_total);
	$result['products_html'] = $products_html;
	$result['customer'] = m_u_p('customer');
	$result['date'] = date('d/m/Y H:i');
	
	
	
	echo json_encode($result);
	
break;


case 'sale_form':

	if($_POST['product_id'][0]=='')
	{
		$result['status'] = false;
		$result['msg'] = 'Ürün bulunamadı!';
	}
	elseif($_POST['customer']=='')
	{
		$result['status'] = false;
		$result['msg'] = 'Müşteri adını yazınız!';
	}
	else
	{
		$total = 0;
		$net_total = 0;
		$discount = 0;
		
		$products_html = '';
		$n=0;
		foreach($_POST['product_id'] as $id)
		{
			$quantity = $_POST['product_quantity'][$n];
			$variant = $_POST['product_variant'][$n];
			
			$variant_ex = explode(':',$variant);
			
			$stock = $db->select('sum(variant_stock) as stock,id')->table('product_variants')->where('p_id','=',$id)->where('variant_type','=',$variant_ex[0])->where('variant_value','=',$variant_ex[1])->get();
			$stock_id = $stock['data'][0]['id'];
			$stock = $stock['data'][0]['stock'];
			
			
			$info = $db->table('products')->where('id','=',$id)->where('status','=','1')->get_vars();
			
			
			if($stock=='')
			{
				$stock = 0;
			}
			
			if($stock<$quantity)
			{
				$result['status'] = false;
				$result['msg'] = ''.$info['name'].' - '.$variant.' için maksimum stok '.$stock.' adettir!';
				echo json_encode($result);
				exit;
			}
			
			$price_type = $info['price_type'];
			$sale_price = $info['sale_price'];
			
			
			if($price_type=='$')
			{
				$sale_price = $sale_price*m_setting('usd_try');
			}
			
			if($price_type=='€')
			{
				$sale_price = $sale_price*m_setting('eur_try');
			}
			
			$product_total = $sale_price*$quantity;
			
			$variant_info = '';
			
			if($variant!='')
			{
				$variant_info = ' ( '.$variant.' )';
			}
			
			$products_html.= '<tr><td>'.$info['name'].''.$variant_info.'</td><td>'.$quantity.'</td><td>'.m_currency($sale_price).'</td><td>'.m_currency($product_total).'</td></tr>';
			
			$result['products'][] = ['id' => $id,'variant_id' => $stock_id,'price' => $sale_price,'total_price' => $product_total,'variant' => $variant,'quantity' => $quantity, 'rand' => $_POST['product_rand'][$n]];
			
			$total = $total+$product_total;
			
			$n++;
			
			
		}
		
			$discount = m_percent($total,m_u_p('discount'));
			$net_total = $total-$discount;
			$data = [
			'u_id' => m_admin('id'),
			'customer' => m_u_p('customer'),
			'discount' => m_u_p('discount'),
			'total' => $total,
			'discounted' => $discount,
			'total_price' => $net_total
			];
			
			$s_id = $db->table('sales')->insert($data);
			
			foreach($result['products'] as $product)
			{
				$data = [
				's_id' => $s_id,
				'p_id' => $product['id'],
				'v_id' => $product['variant_id'],
				'variant' => $product['variant'],
				'quantity' => $product['quantity'],
				'sale_price' => $product['price'],
				'total_price' => $product['total_price']
				];
				
				$db->table('sales_products')->insert($data);
				
				$db->query("update product_variants set variant_stock=variant_stock-".$product['quantity']." where id='".$product['variant_id']."'");
			}
		
		$result['status'] = true;
	}
	
	
	
	echo json_encode($result);
	
break;



case 'print_form':

	if($_POST['product_id'][0]=='')
	{
		$result['status'] = false;
		$result['msg'] = 'Ürün bulunamadı!';
	}
	elseif($_POST['customer']=='')
	{
		$result['status'] = false;
		$result['msg'] = 'Müşteri adını yazınız!';
	}
	else
	{
		
		$products_html = '';
		$n=0;
		foreach($_POST['product_id'] as $id)
		{
			$quantity = $_POST['product_quantity'][$n];
			$variant = $_POST['product_variant'][$n];
			
			$variant_ex = explode(':',$variant);
			
			$stock = $db->select('sum(variant_stock) as stock,id')->table('product_variants')->where('p_id','=',$id)->where('variant_type','=',$variant_ex[0])->where('variant_value','=',$variant_ex[1])->get();
			$stock_id = $stock['data'][0]['id'];
			$stock = $stock['data'][0]['stock'];
			
			
			$info = $db->table('products')->where('id','=',$id)->where('status','=','1')->get_vars();
			
			
			if($stock=='')
			{
				$stock = 0;
			}
			
			if($stock<$quantity)
			{
				$result['status'] = false;
				$result['msg'] = ''.$info['name'].' '.$variant.' için maksimum stok '.$stock.' adettir!';
				echo json_encode($result);
				exit;
			}
			
		}
		
		$result['status'] = true;
	}
	
	
	
	echo json_encode($result);
	
break;


}

}