<?php
$id=intval($_POST['id']);
$purchase=$db->get_rowData('purchase','id',$id);
$products=[];
if(!empty($purchase)){
	$details=$db->selectAll('purchase_details','where purchase_id='.$id);
	$supplier=$smt->supplierInfoByID($purchase['supplier_id']);
	$units=$smt->getAllUnit();
	foreach($details as $d){
		$p=$smt->productInfoByID($d['product_id']);
		$products[]=[
			'id'		=> $d['product_id'],
			'title'		=> $p['title'],
			'unit'		=> $units[$p['unit_id']]['title'],
			'quantity'	=> $d['quantity'],
			'unit_price'=> $d['unit_price'],
			'total'		=> $general->numberFormat($d['quantity']*$d['unit_price'])
		];
	}
	$purchase_truk=$db->selectAll('purchase_truk','where purchase_id='.$id);
	$jArray['truk']=[];
	foreach($purchase_truk as $pt){
		$truk=$smt->truk_info_by_id($pt['truk_id']);
		$jArray['truk'][]=[
			'truk'	=> $truk['title'],
			'pay_by'=> $pt['pay_by'],
			'rent'	=> $general->numberFormat($pt['rent'])
		];
	}
	$purchase_additional_cost=$db->selectAll('purchase_additional_cost','where purchase_id='.$id);
	$jArray['ade']=[];
	foreach($purchase_additional_cost as $pt){
		$truk=$smt->truk_info_by_id($pt['truk_id']);
		$ade=$smt->additional_cost_info_by_id($pt['additional_expense_id']);
		$jArray['ade'][]=[
			'truk'	=> $truk['title'],
			'ade'	=> $ade['title'],
			'pay_by'=> $pt['pay_by'],
			'amount'=> $general->numberFormat($pt['amount'])
		];
	}
	$jArray['status']=1;
	$jArray['info']=[
		'supplier'		=> $supplier['name'],
		'invoice_no'	=> $purchase['invoice_no'],
		'date'			=> date('d/m/Y',$purchase['date']),
		'challan_date'	=> date('d/m/Y',$purchase['challan_date']),
		'challan_no'	=> $purchase['challan_no'],
		'sub_total'		=> $general->numberFormat($purchase['sub_total']),
		'discount'		=> $general->numberFormat($purchase['discount']),
		'VAT'			=> $general->numberFormat($purchase['VAT']),
		'AIT'			=> $general->numberFormat($purchase['AIT']),
		'total'			=> $general->numberFormat($purchase['total']),
		'remarks'		=> $purchase['remarks'],
		'createdOn'		=> $general->make_date($purchase['createdOn'],'time'),
		'createdBy'		=> $db->userInfoByID($purchase['createdBy'])['name']
	];
	$jArray['products']=$products;
}