<?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 add_pre_journal($type, $trans_no, $act_code, $amount, $tran_date, $currency, $reference, $source_ref='', $rate=1, $event_date='', $doc_date='', 
                         $person_type_id=0, $person_id=0, $person_detail_id=0, $float_id=0, $gl_trans_type_id=0, $comment='') {
    $sql = "INSERT INTO ".TB_PREF."pre_journal(
                        `type`,`trans_no`, `act_code`, `amount`, `currency`, `rate`, `reference`, `source_ref`, `tran_date`,
                        `event_date`, `doc_date`, `person_type_id`, `person_id`, `person_detail_id`, `float_id`, `gl_trans_type_id`,
                        `comment`)
            VALUES(".db_escape($type).","
                    .db_escape($trans_no).","
                    .db_escape($act_code).","
                    .db_escape($amount).","
                    .db_escape($currency).","
                    .db_escape($rate).","
                    .db_escape($reference).","
                    .db_escape($source_ref).","
                    ."'".date2sql($tran_date)."','"
                    . ($event_date === '' ? '1970-01-01' : date2sql($event_date))."','"
                    . ($doc_date === '' ? '1970-01-01' : date2sql($doc_date))."' ," 
                    .db_escape($person_type_id).","
                    .db_escape($person_id).", "
                    .db_escape($person_detail_id).", "
                    .db_escape($float_id).", "
                    .db_escape($gl_trans_type_id).", "
                    .db_escape($comment).")";

    return db_query($sql, 'cannot add pre_journal entry');
}

function update_pre_journal($type, $trans_no, $act_code, $amount, $tran_date, $currency, $reference, $source_ref='', $rate=1, $event_date='', $doc_date='', $person_type_id, 
                            $person_id, $person_detail_id, $float_id, $gl_trans_type_id=0, $comment='') 
{
    $sql = "UPDATE ".TB_PREF."pre_journal SET "
                ."`act_code`=".db_escape($act_code).","
                ."`amount`=".db_escape($amount).","
                ."`currency`=".db_escape($currency).","
                ."`rate`=".db_escape($rate).","
                ."`reference`=".db_escape($reference).","
                ."`source_ref`=".db_escape($source_ref).","
                ."`tran_date`='".date2sql($tran_date)."',"
                ."`event_date`='".($event_date === '' ? '0000-00-00' : date2sql($event_date))."',"
                ."`doc_date`='".($doc_date === '' ? '0000-00-00' : date2sql($doc_date))."',"
                ."`person_type_id`=".db_escape($person_type_id).","
                ."`person_id`=".db_escape($person_id).","
                ."`person_detail_id`=".db_escape($person_detail_id)."," 
                ."`float_id`=".db_escape($float_id)."," 
                ."`gl_trans_type_id`=".db_escape($gl_trans_type_id).", " 
                ."`comment`=".db_escape($comment)." 
            WHERE `type`=".db_escape($type)." AND " ."`trans_no`=".db_escape($trans_no);

    return db_query($sql, 'cannot update pre_journal entry');
}

function delete_pre_journal($type, $trans_no) {
    $sql = "DELETE FROM ".TB_PREF."pre_journal 
            WHERE `type` = ".db_escape($type)." AND `trans_no` = ".db_escape($trans_no);

    return db_query($sql, 'cannot delete pre_journal entry');
}

function get_pre_journal($type, $trans_no) 
{
    $sql = "SELECT * FROM ".TB_PREF."pre_journal 
            WHERE `type` = ".db_escape($type)." AND `trans_no` = ".db_escape($trans_no);

    $myrow = db_query($sql, 'get_pre_journal');
    return db_fetch($myrow);
}

function get_pre_journal_header($type, $trans_no) 
{
    $sql = "SELECT j.*,
                    f.name float_name,
                    u.real_name approver_name 
            FROM ".TB_PREF."pre_journal j 
                LEFT JOIN ".TB_PREF."petty_cash_floats f  
                    ON j.float_id = f.id 
                LEFT JOIN ".TB_PREF."users u   
                    ON j.approved_by = u.id 
            WHERE j.type = ".db_escape($type)." 
                AND j.trans_no = ".db_escape($trans_no);

    $myrow = db_query($sql, 'get_pre_journal_header');
    return db_fetch($myrow);
}

//
// Write headers in debtor_trans for allocation purposes.
// Amount in customer currency
//
function write_cust_pre_journal($trans_type, $trans_no, $branch_id, $date, $ref, $amount, $rate)
{
	// we have to select any branch_id for given AR_act - in fact w
	$sql = "INSERT INTO ".TB_PREF."debtor_trans (trans_no, type, debtor_no, branch_code, tran_date, reference, ov_amount, rate)
		SELECT "
		.db_escape($trans_no).", ".db_escape($trans_type).", debtor_no,"
		.db_escape($branch_id).",'".date2sql($date)."', ".db_escape($ref).","
		.db_escape($amount).", ".db_escape($rate)."
		FROM ".TB_PREF."cust_branch WHERE branch_code=".db_escape($branch_id);

	return db_query($sql, 'cannot write cust_pre_journal');
}

//
// Write headers in supp_trans for allocation purposes.
// Amount in supplier currency
//
function write_supp_pre_journal($trans_type, $trans_no, $supp_id, $date, $ref, $amount, $rate, $src_ref)
{
	$sql = "INSERT INTO ".TB_PREF."supp_trans (trans_no, type, supplier_id, tran_date, reference, ov_amount, rate, supp_reference)
		VALUES (".db_escape($trans_no).", ".db_escape($trans_type).", ".db_escape($supp_id).", '".date2sql($date)."',"
		.db_escape($ref).", ".db_escape($amount).",".db_escape($rate).",".db_escape($src_ref).")";

	return db_query($sql, 'cannot write supp_pre_journal');
}

//--------------------------------------------------------------------------------
// Write/update pre_journal entries.
//
function write_pre_journal_entries(&$cart)
{
	global $Refs;

	$date_ = $cart->tran_date;
	$ref   = $cart->reference;
	$memo_ = $cart->memo_;
	$trans_type = $cart->trans_type;
	$new = $cart->order_id == 0;

	begin_transaction();

	if (!$new)
	{
		$msg = void_transaction($trans_type, $cart->order_id, Today(), _("Document reentered."));
		if ($msg)
		{
			display_error($msg);
			return false;
		}
	}

	$trans_id = $cart->order_id = get_next_trans_no($trans_type);
	$total = $cart->gl_items_total_debit();

	$cart->write_gl();

	add_pre_journal($trans_type, $trans_id, $total,	$date_ , $cart->currency, $ref,
		$cart->source_ref, $cart->rate, $cart->event_date, $cart->doc_date);

	$Refs->save($trans_type, $trans_id, $ref);
	if ($new) {
		add_comments($trans_type, $trans_id, $date_, $memo_);
	} else {
		update_comments($trans_type, $trans_id, null, $memo_);
	}

	add_audit_trail($trans_type, $trans_id, $date_);

	commit_transaction();

	return $trans_id;
}

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

function void_pre_journal_trans($type, $type_no, $use_transaction=true)
{
	if ($use_transaction)
		begin_transaction();

	$sql = "UPDATE ".TB_PREF."pre_journal SET amount=0
		WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);

	db_query($sql, "could not void pre_journal transaction for type=$type and trans_no=$type_no");

	void_bank_trans($type, $type_no, true);

	if ($use_transaction)
		commit_transaction();
}

