<?php
/**********************************************************************
    Copyright (C) FrontAccounting, LLC.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/

//--------------------------------------------------------------------------------------

function get_sql_for_view_transactions($filtertype, $from, $to, &$trans_ref)
{
	$db_info = get_systype_db_info($filtertype);

	if ($db_info == null)
		return "";

	$table_name = $db_info[0];
	$type_name = $db_info[1];
	$trans_no_name = $db_info[2];
	$trans_ref = $db_info[3];
	$trans_date = $db_info[4];

	$sql = "SELECT t.$trans_no_name as trans_no";

	if ($trans_ref)
		$sql .= " ,t.$trans_ref as ref ";
	else
		$sql .= ", r.reference as ref";
	if ($trans_date)
		$sql .= ",t.$trans_date as trans_date";
 	if ($type_name)
		$sql .= ", t.$type_name as type";
	$sql .= " FROM $table_name t LEFT JOIN ".TB_PREF."voided v ON"
		." t.$trans_no_name=v.id AND v.type=".db_escape($filtertype);

	$sql .= " WHERE ISNULL(v.`memo_`)";
	if ($from != null && $to != null)
	{
		$sql .= " AND t.$trans_no_name >= ".db_escape($from). "
			AND  t.$trans_no_name <= ".db_escape($to);
		if ($type_name != null)
			$sql .= " AND t.`$type_name` = ".db_escape($filtertype);
	}
	elseif ($type_name != null)
		$sql .= " AND t.`$type_name` = ".db_escape($filtertype);

	// the ugly hack below is necessary to exclude old gl_trans records lasting after edition,
	// otherwise old data transaction can be retrieved instead of current one.
	if ($table_name==TB_PREF.'gl_trans')
		$sql .= " AND t.`amount` <> 0";

	$sql .= " GROUP BY ".($type_name ? "t.$type_name," : '')." t.$trans_no_name";
	$sql .= " ORDER BY t.$trans_no_name DESC";
	return $sql;
}

function transaction_exists($trans_type, $trans_no)
{
	return db_num_rows(db_query(get_sql_for_view_transactions($trans_type, $trans_no, $trans_no, $dummy)));
}

//
//	Returns counterparty (supplier/customer) name for selected transaction.
//
function get_counterparty_name($trans_type, $trans_no, $full=true)
{
	switch($trans_type)
	{
		case ST_SALESORDER:
		case ST_SALESQUOTE:
			$sql = "SELECT order.customer_id as person_id, debtor.name as name
			FROM ".TB_PREF."sales_orders order, ".TB_PREF."debtors_master debtor
			WHERE order_no=".db_escape($trans_no)." AND trans_type=".db_escape($trans_type)
			." AND order.debtor_no=debtor.debtor_no";
			break;

		case ST_SALESINVOICE :
		case ST_CUSTCREDIT :
		case ST_CUSTPAYMENT :
		case ST_CUSTDELIVERY :
			$sql = "SELECT trans.debtor_no as person_id, debtor.name as name
			FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
			WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
			." AND trans.debtor_no=debtor.debtor_no";
			break;

		case ST_PURCHORDER :
			$sql = "SELECT order.supplier_id as person_id, supp.supp_name as name
			FROM ".TB_PREF."purch_orders order, ".TB_PREF."suppliers supp
			WHERE order_no=".db_escape($trans_no)
			." AND order.supplier_id=supp.supplier_id";
			break;

		case ST_SUPPINVOICE :
		case ST_SUPPCREDIT :
		case ST_SUPPAYMENT :
			$sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
			FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
			WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
			." AND trans.supplier_id=supp.supplier_id";
			break;

		case ST_SUPPRECEIVE :
			$sql = "SELECT trans.supplier_id as person_id, supp.supp_name as name
			FROM ".TB_PREF."grn_batch trans, ".TB_PREF."suppliers supp
			WHERE id=".db_escape($trans_no)
			." AND trans.supplier_id=supp.supplier_id";
			break;

		case ST_BANKPAYMENT :
		case ST_BANKDEPOSIT :
			$sql = "SELECT trans.debtor_no as person_id, debtor.name as name
			FROM ".TB_PREF."debtor_trans trans, ".TB_PREF."debtors_master debtor
			WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
			." AND trans.debtor_no=debtor.debtor_no
			UNION
				SELECT trans.supplier_id as person_id, supp.supp_name as name
			FROM ".TB_PREF."supp_trans trans, ".TB_PREF."suppliers supp
			WHERE trans_no=".db_escape($trans_no)." AND type=".db_escape($trans_type)
			." AND trans.supplier_id=supp.supplier_id";
			break;

		case ST_JOURNAL:	// FIXME - this one can have multiply counterparties of various types depending on person_type_id

		default: 
		/*	// internal operations
		case ST_WORKORDER :
		case ST_INVADJUST : // GRN/DN returns ?
		case ST_BANKTRANSFER :
		case ST_LOCTRANSFER :
		case ST_MANUISSUE :
		case ST_MANURECEIVE :
		case ST_COSTUPDATE :
		*/
			return null;
	}

	$result = db_query($sql, 'cannot retrieve counterparty name');
	if (db_num_rows($result))
	{
		$row = db_fetch($result);
		return sprintf("[%05s] %s", $row['person_id'], $row['name']);
	}

	return '';
}


//-----------------------------------------------------------------------------------------
//	Returns next transaction number.
//	Used only for transactions stored in tables without autoincremented key.
//

function get_next_trans_no ($trans_type){

	$st = get_systype_db_info($trans_type);

	if (!($st && $st[0] && $st[2])) {
		// this is in fact internal error condition.
		display_error('Internal error: invalid type passed to get_next_trans_no()');
		return 0;
	}
	$sql1 = "SELECT MAX(`$st[2]`) as last_no FROM $st[0]";
	if ($st[1] != null)
		 $sql1 .= " WHERE `$st[1]`=".db_escape($trans_type);

	// check also in voided transactions (some transactions like location transfer are removed completely)
	$sql2 = "SELECT MAX(`id`) as last_no FROM ".TB_PREF."voided WHERE `type`=".db_escape($trans_type);

	$sql = "SELECT max(last_no) last_no FROM ($sql1 UNION $sql2) a";
    $result = db_query($sql,"The next transaction number for $trans_type could not be retrieved");
    $myrow = db_fetch_row($result);

    return $myrow[0] + 1;
}


//-----------------------------------------------------------------------------

function get_systype_db_info($type)
{
	switch ($type)
	{
        case     ST_JOURNAL      : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
        case     ST_BANKPAYMENT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
        case     ST_BANKDEPOSIT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
        case     3               : return null;
        case     ST_BANKTRANSFER : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
        case     ST_SALESINVOICE : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_CUSTCREDIT   : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_CUSTPAYMENT  : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_CUSTDELIVERY : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_LOCTRANSFER  : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
        case     ST_INVADJUST    : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
        case     ST_PURCHORDER   : return array("".TB_PREF."purch_orders", null, "order_no", "reference", "ord_date");
        case     ST_SUPPINVOICE  : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_SUPPCREDIT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_SUPPAYMENT   : return array("".TB_PREF."supp_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_SUPPRECEIVE  : return array("".TB_PREF."grn_batch", null, "id", "reference", "delivery_date");
        case     ST_WORKORDER    : return array("".TB_PREF."workorders", null, "id", "wo_ref", "released_date");
        case     ST_MANUISSUE    : return array("".TB_PREF."wo_issues", null, "issue_no", "reference", "issue_date");
        case     ST_MANURECEIVE  : return array("".TB_PREF."wo_manufacture", null, "id", "reference", "date_");
        case     ST_SALESORDER   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
        case     31              : return array("".TB_PREF."service_orders", null, "order_no", "cust_ref", "date");
        case     ST_SALESQUOTE   : return array("".TB_PREF."sales_orders", "trans_type", "order_no", "reference", "ord_date");
        case	 ST_DIMENSION    : return array("".TB_PREF."dimensions", null, "id", "reference", "date_");
        case     ST_COSTUPDATE   : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
            
        case     ST_TRANSFERREQ    : return array("".TB_PREF."material_transfer_requisitions",  null, "mtr_no", "reference", "created_on");
        case     ST_REQTRANSFER    : return array("".TB_PREF."material_requisition_transfers",  null, "mrt_no", "reference", "created_on");
        case     ST_RAW_MATERIAL_ISSUE  : return array("".TB_PREF."raw_material_issues", "type", "trans_no", "reference", "tran_date");
        case     ST_RAW_MATERIAL_RETURN  : return array("".TB_PREF."raw_material_issues", "type", "trans_no", "reference", "tran_date");
        case     ST_PROD_ADD        : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
        case     ST_PROD_RETURN        : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
        case     ST_PURCH_REQ       : return array("".TB_PREF."purchase_requisitions", null, "pr_no", "reference", null);
        case     ST_POINTSUPDATE    : return array("".TB_PREF."benefits", null, "tran_reference", "reference", "ord_date");
        case     ST_PAYROLL    : return array("".TB_PREF."gl_trans", "type", "type_no", null, "tran_date");
        case     ST_SALARY_ADVANCE_PAYMENT    : return array("".TB_PREF."salary_deductions", "id", null, null, null);
        //case     ST_POS_CUSTOMER_ACCOUNT_PAYMENT  : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_STOCK_COUNT    : return array("".TB_PREF."stock_counts", null, "reference", "reference", null);
        case     ST_MATERIAL_REQ    : return array("".TB_PREF."material_requisitions",  null, "mr_no", "reference", null);
        case     ST_MATERIAL_ISSUE    : return array("".TB_PREF."issue_materials",  null, "im_no", "reference", null);
        case     ST_INVENTORY_RECEIPT    : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date");
        case     ST_INVENTORY_ISSUE    : return array("".TB_PREF."stock_moves", "type", "trans_no", "reference", "tran_date"); 
        case     ST_LOT_ADJUSTMENT        : return array("".TB_PREF."lot_moves", "type", "tran_no");   
        case     ST_SERIAL_NO_ADJUSTMENT        : return array("".TB_PREF."serial_no_moves", "type", "tran_no"); 
        case     ST_PR_ISSUE        : return array("".TB_PREF."purchase_requisition_issues", null, "pri_no", "reference"); 
        case     ST_PR_RETURN        : return array("".TB_PREF."purchase_requisition_returns", null, "prr_no", "reference"); 
        case     ST_STOCK_RESERVATION        : return array("".TB_PREF."stock_reservations", null, "id", "reference"); 
        case     ST_STOCK_RELEASE        : return array("".TB_PREF."stock_reservation_releases", null, "id", "reference"); 
        case     ST_I_OWE_YOU        : return array("".TB_PREF."i_owe_you", null, "id", "reference"); 
        case     ST_PETTY_CASH_FLOAT_REIMBUR       : return array("".TB_PREF."i_owe_you_float_reimbursements", null, "id", "reference"); 
        case     ST_PETTY_CASH_FLOAT_ADJ       : return array("".TB_PREF."petty_cash_float_adjustments", null, "id", "reference"); 
        case     ST_PROJECT_PAYMENT_APPLICATION       : return array("".TB_PREF."project_payment_applications", null, "order_no", "reference"); 
        case     ST_SUPPLIER_TRANSPORT_INVOICES       : return array("".TB_PREF."supplier_transport_invoices", null, "id", "reference"); 
        case     ST_SUBCONTRACTOR_INVOICE       : return array("".TB_PREF."subcontractor_invoices", null, "order_no", "reference");
        case     ST_PROJECT_DISPATCH_REQUEST       : return array("".TB_PREF."project_dispatch_requests", null, "id", "reference");
        case     ST_TOOL_REQUEST       : return array("".TB_PREF."tool_requests", null, "id", "reference");
        case     ST_TOOL_ISSUE       : return array("".TB_PREF."tool_issues", null, "id", "reference");
        case     ST_PETTY_CASH       : return array("".TB_PREF."petty_cash_header", null, "id", "reference");
        case     ST_PROJECT_FLOAT_ADJ       : return array("".TB_PREF."project_float_adjustments", null, "id", "reference");
        case     ST_PROJECT_ADVANCE_BATCH       : return array("".TB_PREF."project_advance_batches", null, "id", "reference");
        case     ST_SUBCONTRACTOR_PURCH_ORDER       : return array("".TB_PREF."subcontractor_purchase_orders", null, "id", "reference");
        case     ST_SUBCONTRACTOR_CREDIT_NOTE       : return array("".TB_PREF."subcontractor_credit_notes", null, "order_no", "reference");
        case     ST_PROJECT_INVOICE       : return array("".TB_PREF."project_invoices", null, "order_no", "reference");
        case     ST_PROJECT_CREDIT_NOTE       : return array("".TB_PREF."project_credit_notes", null, "order_no", "reference");
        case     ST_MISCELLANEOUS_PROJECT_COST       : return array("".TB_PREF."miscellaneous_project_costs", null, "id", "reference");
        case     ST_UTILITY_BILLS       : return array("".TB_PREF."utility_bills", null, "id", "reference", "date");
        case     ST_UTILITY_BILL_CREDIT_NOTE       : return array("".TB_PREF."utility_bill_credit_notes", null, "id", "reference", "date");
	case     ST_TOOL_RETURN       : return array("".TB_PREF."tool_returns", null, "id", "reference");
        case     ST_COMP_LOAN_REPAYMENT       : return array("".TB_PREF."company_loan_payments", null, "id", "batch_ref");
        case     ST_STOCK_ASSET_TRANSFER       : return array("".TB_PREF."stock_asset_transfers", null, "id", "reference");
        case     ST_MONTHLY_PROJECT_COSTS       : return array("".TB_PREF."monthly_project_costs", null, "id", "reference");
        case     ST_INVENTORY_RECEIPT_REQUEST  : return array("".TB_PREF."stock_adjustment_requests", "type", "id", "reference");
        case     ST_INVENTORY_ISSUE_REQUEST  : return array("".TB_PREF."stock_adjustment_requests", "type", "id", "reference");
        case     ST_ASSET_TRANSFER_REQUEST  : return array("".TB_PREF."asset_transfer_requests", null, "id", "reference");
        case     ST_ASSET_TRANSFER  : return array("".TB_PREF."asset_transfers", "type", "trans_no", "reference");
        case     ST_ASSET_QUANTITY_ADJUSTMENT_REQUEST  : return array("".TB_PREF."asset_quantity_adjustment_requests", null, "id", "reference");
        case     ST_ASSET_QUANTITY_ADJUSTMENT  : return array("".TB_PREF."asset_quantity_adjustments", null, "id", "reference");
        case     ST_CUSTOMER_PAYMENT_RETURN   : return array("".TB_PREF."debtor_trans", "type", "trans_no", "reference", "tran_date");
        case     ST_SITE_MATERIAL_RETURN  : return array("".TB_PREF."site_material_returns", "", "id", "reference");
        case     ST_CASH_PAYMENT  : return array(TB_PREF."pre_journal", "type", "trans_no", 'reference');
        case     ST_CASH_RECEIPT  : return array(TB_PREF."pre_journal", "type", "trans_no", 'reference');
        case     ST_GL_CREDIT  : return array(TB_PREF."pre_journal", "type", "trans_no", 'reference');
        case     ST_GL_DEBIT  : return array(TB_PREF."pre_journal", "type", "trans_no", 'reference');
        case     ST_ITEM_COSTUPDATE  : return array(TB_PREF."stock_item_cost_amendments", "trans_type", "trans_no", 'reference');
        case     ST_PETTY_CASH_ITEM_RECEIPT  : return array("".TB_PREF."petty_cash_stock_item_receipt_details", "type", "trans_no");
        case     ST_IOU_BANKPAYMENT  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
        case     ST_IOU_BANKPAYMENT_RETURN  : return array("".TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
        case     ST_PETTY_CASH_ITEM_ISSUE  : return array("".TB_PREF."petty_cash_item_issues", "type", "trans_no", "reference");
        case     ST_MATERIAL_TRANSFER_RECEIPT  : return array("".TB_PREF."material_requisition_transfers", "type", "mrt_no", "reference");
        case     ST_DAILY_ITEM_ISSUE  : return array("".TB_PREF."daily_item_issues", "type", "trans_no", "reference");
        case     ST_DAILY_ITEM_RETURN  : return array("".TB_PREF."daily_item_issues", "type", "trans_no", "reference");
        case     ST_DAILY_TOOL_ISSUE  : return array("".TB_PREF."daily_tool_issues", "type", "trans_no", "reference");
        case     ST_DAILY_TOOL_RETURN  : return array("".TB_PREF."daily_tool_issues", "type", "trans_no", "reference");
        case     ST_PAYMENT_VOUCHER  : return array("".TB_PREF."payment_vouchers", null, "id", "reference");
        case     ST_PAYMENT_RECEIPT  : return array("".TB_PREF."payment_receipts", null, "id", "reference");
        case     ST_BATCH_BANK_TRANSFER_REQ  : return array("".TB_PREF."batch_bank_transfer_header", "type", "trans_no", "reference");
        case     ST_BATCH_ITEM_ISSUE  : return array("".TB_PREF."batch_item_issues", "type", "trans_no", "reference");
        case     ST_JOB_COST_ADDITION  : return array("".TB_PREF."job_cost_additions", "type", "trans_no", "reference");
        case     ST_JOB_COST_DEDUCTION  : return array("".TB_PREF."job_cost_additions", "type", "trans_no", "reference");
        case     ST_ASSET_REQUISITION_TRANSFER  : return array("".TB_PREF."asset_transfers", "type", "trans_no", "reference");
        case     ST_ASSET_TRANSFER_RECEIPT  : return array("".TB_PREF."asset_transfers", "type", "trans_no", "reference");
        case     ST_POS_INVOICE  : return array("".TB_PREF."pos_invoices", "type", "trans_no", "reference");
        case     ST_POS_CREDIT_NOTE  : return array("".TB_PREF."pos_invoices", "type", "trans_no", "reference");
        case     ST_UTILITY_BILL_STOCK_RECEIPT  : return array("".TB_PREF."utility_bill_stock_receipts", "type", "trans_no", "reference");
        case     ST_FLOAT_PAYMENT : return array("".TB_PREF."petty_cash_float_adjustments", "type", "trans_no", "reference");
        case     ST_FLOAT_RECEIPT : return array("".TB_PREF."petty_cash_float_adjustments", "type", "trans_no", "reference");
        case     ST_POS_ITEM_ISSUE  : return array("".TB_PREF."daily_item_issues", "type", "trans_no", "reference");  
        case     ST_POS_ITEM_RETURN  : return array("".TB_PREF."daily_item_issues", "type", "trans_no", "reference");  
        case     ST_POS_CUSTOMER_PAYMENT  : return array("".TB_PREF."pos_customer_payments", "type", "trans_no", "reference");
        case     ST_POS_CUSTOMER_RETURN  : return array("".TB_PREF."pos_customer_payments", "type", "trans_no", "reference");
        case     ST_FLOAT_TRANSFER : return array("".TB_PREF."petty_cash_float_adjustments", "type", "trans_no", "reference");
	}

	display_db_error("invalid type ($type) sent to get_systype_db_info", "", true);
}



function get_systypes()
{
	$sql = "SELECT * FROM ".TB_PREF."sys_types";
	$result = db_query($sql, "could not query systypes table");
	return $result;
}
