<?php 

function _tf_get_ledger_beneficiary_payer($item, $key, $table_obj) {
	switch ($item["type"]) {
		case "credit":
			return get_fullname($item[$key], true);
			break;

		case "bank_withdrawal":
		case "bank_deposit":
			return pm_get_account_id($item['bank_id']);
			break;
		
		default:
			return $item['beneficiary'];
			break;
	}
	return ($item["type"] == "credit") ? get_fullname($item[$key], true) : $item['beneficiary'];
}

function _tf_get_fee_description($item, $key, $table_obj) {
	if ($item["type"] == "credit") {
		$post = new post;
		if(!isset($table_obj->invoice)) {
			$table_obj->invoice = new stdClass;
		}
		if(isset($table_obj->invoice->{$item['id']})) {
			$invoice = $table_obj->invoice->{$item['id']};
		} else {
			$invoice = new pm_invoice($item['user_id']);
			$invoice->set_invoice_by_number($item['in_ref']);
			// $table_obj->invoice->{$item['id']} = $invoice;
		}
		// if(!$invoice->invoice) {
		// 	$post = new post;
		// 	$post->drop_post($item['id']);
		// }
		return $invoice->description();
	} else {
		return $item['narration'];
	}
}

function _tf_get_ledger_ref($item, $key, $table_obj) {
	if ($item["type"] == "credit") {
		return $item['in_ref'];
	} else {
		return $item['ref'];
	}
}

function _tf_get_ledger_inflow($item, $key, $table_obj) {
	if(!isset($table_obj->temp['balance'])) $table_obj->temp['balance'] = 0;
	if(!isset($table_obj->temp['total_inflow'])) $table_obj->temp['total_inflow'] = 0;
	if($table_obj->query_type === "normalLedger") {
		if ($item["type"] == "credit" || $item["type"] == "income" || $item["type"] == "inflow" || $item["type"] == "bank_withdrawal") {
			$table_obj->temp['balance'] += $item['amount'];
			$table_obj->temp['total_inflow'] += $item['amount'];
			return toMoney($item['amount']);
		} else {
			return "-";
		}
	} else {
		if ($item["type"] == "bank_deposit") {
			$table_obj->temp['balance'] += $item['amount'];
			$table_obj->temp['total_inflow'] += $item['amount'];
			return toMoney($item['amount']);
		} else {
			return "-";
		}
	}
	
}

function _tf_get_ledger_outflow($item, $key, $table_obj) {
	if(!isset($table_obj->temp['balance'])) $table_obj->temp['balance'] = 0;
	if(!isset($table_obj->temp['total_outflow'])) $table_obj->temp['total_outflow'] = 0;
	if($table_obj->query_type === "normalLedger") {
		if ($item["type"] == "credit" || $item["type"] == "income" || $item["type"] == "inflow" || $item["type"] == "bank_withdrawal") {
			return "-";
		} else {
			$table_obj->temp['balance'] -= $item['amount'];
			$table_obj->temp['total_outflow'] += $item['amount'];
			return toMoney($item['amount']);
		}
	} else {
		if ($item["type"] == "bank_deposit") {
			return "-";
		} else {
			$table_obj->temp['balance'] -= $item['amount'];
			$table_obj->temp['total_outflow'] += $item['amount'];
			return toMoney($item['amount']);
		}
	}
	
}

function _tf_get_ledger_balance($item, $key, $table_obj) {
	return toMoney($table_obj->temp['balance']);
}

function _tf_get_invoice_total($item, $key, $table_obj) {
	if(!is_serialized($item['items'])) {
		$item['items'] = base64_decode($item['items']);
	}
	$invoice_items = is_serialized($item['items']) ? unserialize($item['items']) : array();
	$total = 0;
	if(_count($invoice_items) && is_array($invoice_items)) {
		foreach ($invoice_items as $key => $value) {
			$total += ($value['amount'] * $value['quantity']);
		}
	}
	return toMoney($total);
}

function _tf_get_invoice_status($item, $key, $table_obj) {
	$invoice = new pm_invoice($item['user_id']);
	$invoice->set_invoice_by_number($item['invoice_number']);
	$status = $invoice->status();
	if($status == 'unpaid') {
		return "<span class=\"red\">".$status . "</span><br/><em>bal: ".toMoney($invoice->total(true))."</em>";
	} else return "<span class=\"green\">".$status."</span>";
}

function _tf_pm_get_invoice_description($item, $key, $table_obj) {
	if(!isset($table_obj->invoice[$item['in_ref']])) {
		$invoice = new pm_invoice($item['user_id']);
		$table_obj->invoice[$item['in_ref']] = $invoice;
		$invoice->set_invoice_by_number($item['in_ref']);
	}
	
	return $table_obj->invoice[$item['in_ref']]->description();
}

function _tf_pm_get_invoice_amount($item, $key, $table_obj) {
	if(!isset($table_obj->invoice[$item['in_ref']])) {
		$invoice = new pm_invoice($item['user_id']);
		$table_obj->invoice[$item['in_ref']] = $invoice;
		$invoice->set_invoice_by_number($item['in_ref']);
	}

	return toMoney($table_obj->invoice[$item['in_ref']]->total());
}

function _tf_pm_get_invoice_attachment($item, $key, $table_obj) {
	$files = get_library_files("pop-".$item['in_ref'], $item['user_id']);
	$html = "<ul style=\"padding: 0px;\">";
	$count = is_array($files['files']) ? _count($files['files']) : 0;
	if(isset($files['files']) && $count && is_array($files['files'])) {
		foreach ($files['files'] as $url => $file_meta) {
			$html .= "<li style=\"display: block;\"><a class=\"popmeup\" data-width=700 data-height=500 href=\"".$url."\" target=\"_blank\">".$file_meta['title']."</a></li>";
		}
	}
	$html .= "</ul>";
	return apply_filters("_pm_attachments_filter", $html, $item);
}

function _tf_pm_confirmed_by($item, $key, $table_obj) {
	if(isset($table_obj->fullname[$item[$key]])) {
		return $table_obj->fullname[$item[$key]];
	} else {
		if($fullname = get_fullname($item[$key], true)) {
			$table_obj->fullname[$item[$key]] = $fullname;
			return $table_obj->fullname[$item[$key]];
		} else {
			return $item[$key];
		}
	}
}

function _tf_get_wallet_credit($item, $key, $table_obj) {
	if ($item["type"] == "credit_user") {
		if(!isset($table_obj->temp)) $table_obj->temp = array();
		if(!isset($table_obj->temp['balance'])) $table_obj->temp['balance'] = 0;
		$table_obj->temp['balance'] += $item['amount'];
		return toMoney($item['amount']);
	} else {
		return "-";
	}
}

function _tf_get_wallet_debit($item, $key, $table_obj) {
	if ($item["type"] == "credit_user") {
		return "-";
	} else {
		if(!isset($table_obj->temp)) $table_obj->temp = array();
		if(!isset($table_obj->temp['balance'])) $table_obj->temp['balance'] = 0;
		$table_obj->temp['balance'] -= $item['amount'];
		return toMoney($item['amount']);
	}
}

function _tf_get_wallet_balance($item, $key, $table_obj) {
	if(!isset($table_obj->temp)) $table_obj->temp = array();
	if(!isset($table_obj->temp['balance'])) $table_obj->temp['balance'] = 0;
	return toMoney($table_obj->temp['balance']);
}