<?php


$page_security = $_POST['PARAM_0'] == $_POST['PARAM_1'] ?
	'SA_SUPPTRANSVIEW' : 'SA_SUPPBULKREP';

$path_to_root="..";
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/sales/includes/db/charges_db.inc");

print_supp_invoices();
//----------------------------------------------------------------------------------------------------
//pk
function get_grn_batch_data($from, $to, $supplier){
   
    $fromdate = date2sql($from);
    $todate = date2sql($to);

    $sql = "SELECT * FROM ".TB_PREF."grn_batch WHERE id=".db_escape($from);
    // $sql .= " AND delivery_date >= ".db_escape($fromdate)." AND delivery_date <= ".db_escape($todate);
    if($supplier != '')
    	$sql .= " AND supplier_id=".db_escape($supplier);
    $result = db_query($sql, "");
    return db_fetch($result);
}


function get_grn_items_data($id){
	$sql = " SELECT * FROM ".TB_PREF."grn_items WHERE grn_batch_id =".db_escape($id);
	return db_query($sql, " ");
}

//----------------------------------------------------------------------------------------------------
function print_supp_invoices(){

	global $path_to_root, $SysPrefs;

	include_once($path_to_root . "/reporting/includes/pdf_report.inc");

	$from = $_POST['PARAM_0'];
	$to = $_POST['PARAM_1'];
	$supplier = $_POST['PARAM_2'];
	$comments = $_POST['PARAM_3'];
	$orientation = $_POST['PARAM_4'];
	$destination = $_POST['PARAM_5'];

	if (!$from || !$to) return;

	$orientation = ($orientation ? 'L' : 'P');
	$dec = user_price_dec();

	// display_error($from ."     ".$to);exit;

	//-------------code-Descr-Qty--uom--tax--prc--Disc-Tot--//

	if ($email == 0)
		$rep = new FrontReport(_('Arrival Slip'), "arrival_slip", user_pagesize(), 9, $orientation);
	if ($orientation == 'L')
		recalculate_cols($cols);

	$params =   array( 	0 => $comments,);
						// 2 => array( 'text' => _('Tax Id Only'),'from' => $tid,'to' => ''),
						// 4 => array( 'text' => _('Branch Id'),'branch_id' => $branch_id,'to' => ''),

	$cols = array(4, 60, 290, 430, 490);

	
	// $headers = array(_("Stock ID.")); 
	$aligns = array('center', 'center', 'center', 'center'); //, 'center' //, 'left'
    if ($orientation == 'L')
    	recalculate_cols($cols);

	$range = get_grn_batch_data($from, $to, $supplier);
	$rep->Font();
	$rep->Info($params, $cols, null, $aligns);
	$rep->SetCommonData($range, $branch, $po_order, $baccount, ST_SUPPRECEIVE, $contacts,$grn_item_id);
	$rep->SetHeaderType('Header2');
	$rep->NewPage();
			$summary_start_row = $rep->bottomMargin + (15 * $rep->lineHeight);

	$grn_item = get_grn_items_data($range['id']);
	$s_no = 1;
	$dec = user_price_dec();
	while($row = db_fetch($grn_item)){
		$rep->TextCol(0, 1, $s_no++, -2);
		$description = get_item($row['item_code']);
		$rep->TextCol(1, 2, $row['item_code'] . " - " . $description['description'], -2);
		$rep->TextCol(2, 3, $row['unit_id'], -2);
		$rep->TextCol(3, 4, number_format2($row['qty_recd'], $dec), -2);

		$packing_data="Packing Size : ".$row['pack_size']." \n";
		$packing_data.="Unit : ".$row['pack_unit']." \n";
		$packing_data.=" No of Packing : ".$row['no_of_pack']." \n";
	}

 $rep->NewLine();
		$rep->TextColLines(1, 2, $packing_data, -2);
	$rep->row = $summary_start_row;
	$rep->aligns[2] = 'left';
	$data = charges_trnas($range['id'], ST_SUPPRECEIVE);
	$j = 1;
	while ($myrow2 = db_fetch($data)){
		    $amount = $myrow2['charges_amount'];
			$rep->TextCol(2, 3, _($myrow2['name']), - 2);
			$rep->TextCol(3, 4,	price_format($amount), -2);
			$total_charges += $amount;
	    $j++;
	 $rep->NewLine();
	}

	if ($email == 0)
		$rep->End();
}