<?php defined('BASEPATH') OR exit('No direct script access allowed');

/*
$i=0;
foreach( $itemlist as $items)
{
	foreach ($items as $key => $value)
	{
		if ($i++ == 0)
		{
			printf("[%010s]|", $key );
			echo "\n";
		}

		printf("[%010s]|", $value);
	}
	echo "\n";
}
*/
	// Company Address
	echo $company["compName"] . PHP_EOL;
	// Branch Name
	if (!empty($company["company"]))
	{
		echo $company["company"] . PHP_EOL;
	}

	echo $company["address1"] . ", " . $company["address2"] . PHP_EOL;
	echo $company["city"] . "-" . $company["zip"] . PHP_EOL;
	echo $company["stateName"] . ", " . $company["country"] . PHP_EOL;

	if (!empty($company["phone"]))
	{
		echo "Ph: " . $company["phone"] . PHP_EOL;
	}

	if (!empty($company["mobile"]))
	{
		echo "Mob: " . $company["mobile"] . PHP_EOL;
	}

	if (!empty($company["email"]))
	{
		echo "@: " . $company["email"] . PHP_EOL;
	}

	if (!empty($company["gstin"]))
	{
		echo lang("GSTIN").":" . $company["gstin"] . PHP_EOL;
	}

	if (!empty($company["pan"]))
	{
		echo "Pan: " . $company["pan"] . PHP_EOL;
	}

	echo PHP_EOL;

	echo $titleText . PHP_EOL;
	echo $tNo_text . ": " . $tNo . PHP_EOL;
	echo $tDate_text . ": " . $tDate . PHP_EOL;

	echo "Due Date: " . $dueDate . PHP_EOL;
	echo PHP_EOL;

	if (!empty($billing))
	{
		echo ($billing["addrType"] == "A" ? "Billing & Shipping Address" : "Billing Address") . PHP_EOL;
		echo $billing["name"] . PHP_EOL;

		echo $billing["address1"] . ", " . $billing["address2"] . PHP_EOL;
		echo $billing["city"] . "-" . $billing["zip"] . PHP_EOL;
		echo $billing["stateName"] . ", " . $billing["country"] . PHP_EOL;
		if (!empty($billing["phone"]))
		{
			echo "Ph: " . $billing["phone"] . PHP_EOL;
		}
		if (!empty($billing["mobile"]))
		{
			echo "Mob: " . $billing["mobile"] . PHP_EOL;
		}
		if (!empty($billing["email"]))
		{
			echo "@: " . $billing["email"] . PHP_EOL;
		}
		echo "POS: " . $pos . PHP_EOL;

		if (!empty($contact))
		{
			if (!empty($contact["taxNo"]))
			{
				echo lang("GSTIN"). ":" . $contact["taxNo"] . PHP_EOL;
			}
			if (!empty($contact["perTax"]))
			{
				echo "Pan: " . $contact["perTax"] . PHP_EOL;
			}
		}
		
		echo PHP_EOL;
	}

	if (!empty($shipping) && $shipping["addrType"] == "S")
	{
	 	echo "Shipping Address" . PHP_EOL;

		echo $shipping["name"] . PHP_EOL;

		echo $shipping["address1"] . ", " . $shipping["address2"] . PHP_EOL;
		echo $shipping["city"] . "-" . $shipping["zip"] . PHP_EOL;
		echo $shipping["stateName"] . ", " . $shipping["country"] . PHP_EOL;
		if (!empty($shipping["phone"]))
		{
			echo "Ph: " . $shipping["phone"] . PHP_EOL;
		}
		if (!empty($billing["mobile"]))
		{
			echo "Mob: " . $shipping["mobile"] . PHP_EOL;
		}
		if (!empty($billing["email"]))
		{
			echo "@: " . $shipping["email"] . PHP_EOL;
		}

		echo PHP_EOL;
	}

// print_R($details);
/*
	echo "S.No Item\t\t\tQty\t\tRate\tTax\t\tTxble.Amt\t\t\tAmt".PHP_EOL;
	foreach($details as $key => $val)
	{
		echo ($key + 1) . " ";
		echo str_pad(substr($val["itemName"], 0, 24), 24, " ") . " ";
		// printf("% 20s", substr($val["itemName"], 0, 20)) . " ";
		// echo $val["itemDesc"];
		echo str_pad(((float)$val["qty"]) . " " . $val["unit"], 10, " ", STR_PAD_LEFT) . "\t";
		// echo ((float)$val["qty"]) . " " . $val["unit"] . "\t";
		
		echo str_pad(((float)$val["rate"]), 10, " ", STR_PAD_LEFT) . "\t";

		if (!empty($val["discAmount"]))
		{
			echo $val["discAmount"] . "\t";
		}

		echo $val["taxName"] . "\t";

		if ($includeDiscount)
		{
			$val['discAmount'] = "";
			$val['excDiscAmount'] = "";

			if ($includeTax)
			{
				$val['itemTotal'] += $val["_discAmount"];
			}
			else
			{
				$val['taxableAmount'] += $val["_discAmount"];
			}

			$subTotal += $val['taxableAmount'];
		}
		$val['taxableAmount'] = formatAmount($val['taxableAmount'], $currencyCode, "-", true);
		$val["itemTotal"] = formatAmount($val['itemTotal'], $currencyCode, "-", true);

		echo $val["taxableAmount"] . "\t";
		echo $val["itemTotal"];
		echo PHP_EOL;
	}
*/
	$subTotal = 0;
	$detailContent = "";

	foreach($details as $key => $val)
	{
		$detailContent .= ($key + 1) . ") ";
		$detailContent .= $val["itemName"];
		// echo $val["itemDesc"];
		$detailContent .= "\tQty: " . ((float)$val["qty"]) . " " . $val["unit"];
		$detailContent .= "\tRate: " . ((float)$val["rate"]);
		if (!empty($val["discAmount"]) && (float)($val["discAmount"]))
		{
			$detailContent .= "\tDisc: " . ((float)$val["discAmount"]);
		}
		if (!empty($val["taxName"]))
		{
			$detailContent .= "\tTax: " . $val["taxName"];
		}

		if ($includeDiscount)
		{
			$val['discAmount'] = "";
			$val['excDiscAmount'] = "";

			if ($includeTax)
			{
				$val['itemTotal'] += $val["_discAmount"];
			}
			else
			{
				$val['taxableAmount'] += $val["_discAmount"];
			}
		}

		$subTotal += $val['taxableAmount']; 

		$val['taxableAmount'] = formatAmount($val['taxableAmount'], $currencyCode, "-", true);
		$val["itemTotal"] = formatAmount($val['itemTotal'], $currencyCode, "-", true);
		
		// $detailContent .= "\tTxble.Amt: " . $val["taxableAmount"];
		$detailContent .= "\tAmt: " . $val["itemTotal"];

		// echo 0 ? $detailContent : (str_replace("\t", "\n", $detailContent) . PHP_EOL);
		$detailContent .= PHP_EOL . PHP_EOL;
	}

	echo str_replace("\t", "\n", $detailContent);

	echo "SubTotal: " . formatAmount($subTotal, $currencyCode, "-", true) . PHP_EOL;

	if ($includeDiscount && $discountTotal)
	{
		echo "less Discount " . (empty($discountPercent) ? "" : ((int)$discountPercent . "%")) . ": " . $discountTotal . PHP_EOL;
	}

	if (!empty($tax))
	{
		foreach ($tax as $key => $tax_item)
		{
			echo $tax_item['taxName'] . ' ' . round($tax_item['taxRate'], 2) . "%: ";
			echo formatAmount($tax_item['taxValue'], $currencyCode, "-", true);
			echo PHP_EOL;
		}
	}
	if($roundAmount && (float)$roundAmount)
	{
		echo "Round Off: " . $roundAmount . PHP_EOL;
	}
	if(!empty($tdsPercentage) && $tdsPercentage != 0)
	{
		echo "TDS @ " . $tdsPercentage . "%: " . $tdsAmount . PHP_EOL;
	}

	$totLessCharges = 0;
	echo "Total: " . formatAmount($totAmount + $totLessCharges, $isExport ? $expCurrencyCode : $currencyCode, "-", $isExport ? true : false) . PHP_EOL;

