<?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>.
***********************************************************************/
$path_to_root = "..";
include_once($path_to_root . "/includes/ui/items_cart.inc");
include_once($path_to_root . "/includes/session.inc");

$page_security = 'SA_CASH_RECEIPT';


include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");

include_once($path_to_root . "/gl/includes/ui/gl_cash_ui.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
//include_once($path_to_root . "/gl/includes/db/gl_db_cash.inc");
include_once($path_to_root . "/gl/includes/db/gl_journal.inc");
include_once($path_to_root . "/gl/includes/db/pre_gl_adjustments_db.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_journal.inc");
include_once($path_to_root . "/gl/includes/db/gl_db_pre_trans.inc");
include_once($path_to_root . "/gl/includes/gl_ui.inc");
include_once($path_to_root . "/admin/db/attachments_db.inc");
include_once($path_to_root . "/includes/ui/ui_lists_group_companies.inc");
include_once($path_to_root . "/includes/db/group_companies_db.inc");
include_once($path_to_root . "/payroll/includes/db/emp_trans_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/petty_cash_floats_db.inc");
include_once($path_to_root . "/petty_cash/includes/db/i_owe_you_db.inc");



$js = '';
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(800, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();


$name = 'cash_receipt';

if(isset($_GET['NewDeposit'])) {
        $page_security = 'SA_CASH_RECEIPT' ;
	$_SESSION['page_title'] = _($help_context = "Cash Receipt Entry");
        $name = 'cash_receipt';
	create_cash_cart($name, ST_CASH_RECEIPT, 0);
} 
else if(isset($_GET['ModifyDeposit'])) {
	$_SESSION['page_title'] = _($help_context = "Cash Receipt Entry")." #".$_GET['ModifyDeposit'];
	create_cash_cart($name, ST_CASH_RECEIPT, $_GET['ModifyDeposit']);
}

page($_SESSION['page_title'], false, false, '', $js);

//-----------------------------------------------------------------------------------------------
//check_db_has_bank_accounts(_("There are no bank accounts defined in the system."));

//if (isset($_GET['ModifyDeposit']) || isset($_GET['ModifyPayment']))
//	check_is_editable($_SESSION[$name]->trans_type, $_SESSION[$name]->order_id);

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

global $Ajax;

if (list_updated('PersonDetailID')) {
	$br = get_branch(get_post('PersonDetailID'));
	$_POST['person_id'] = $br['debtor_no'];
	$Ajax->activate('person_id');
}


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

global $systypes_array;

if (isset($_GET['AddedRec']))
{
	$trans_no = $_GET['AddedRec'];
	$trans_type = ST_CASH_RECEIPT;

   	display_notification_centered(sprintf(_($systypes_array[$trans_type]." %d has been saved"), $trans_no));

//	display_note(get_gl_view_str($trans_type, $trans_no, _("&View the GL Postings for this Payment")));

	hyperlink_params($_SERVER['PHP_SELF'], _("Enter Another ".$systypes_array[$trans_type]), "NewDeposit=yes");

	hyperlink_params($_SERVER['PHP_SELF'], _("Enter a ".$systypes_array[ST_CASH_PAYMENT]), "NewPayment=yes");

	display_footer_exit();
}

if (isset($_GET['ApprovedRec']))
{
	$trans_no = $_GET['ApprovedRec'];
	$trans_type = ST_CASH_RECEIPT;

   	display_notification_centered(sprintf(_($systypes_array[$trans_type]." %d has been approved"), $trans_no));

	display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this ".$systypes_array[$trans_type])));

	hyperlink_params("$path_to_root/gl/inquiry/pre_gl_adjustments_inquiry.php", _("Approve another ".$systypes_array[$trans_type]), "filterType=$trans_type");

	display_footer_exit();
}


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



if (isset($_POST['Post']))
{
    if(!check_cash_trans($name)){
	$trans_array = process_cash_transaction($name, true);
        if (isset($trans_array['message'])) {
            display_error($trans_array['message']);
        } 
        else {
            meta_forward($_SERVER['PHP_SELF'], "ApprovedRec=$trans_array[1]");            
        }
    }

}

if (isset($_POST['Process']))
{
    if(!check_cash_trans($name)){
	$trans_array = process_cash_transaction($name, false);
        if (isset($trans_array['message'])) {
            display_error($trans_array['message']);
        } else {
            meta_forward($_SERVER['PHP_SELF'], "AddedRec=$trans_array[1]");            
        }
    }

}





//-----------------------------------------------------------------------------------------------
$id = find_submit('Delete');
if ($id != -1)
	handle_delete_cash_item($name, $id);

if (isset($_POST['AddItem']))
	handle_new_cash_item($name);

if (isset($_POST['UpdateItem']))
	handle_update_cash_item($name);

if (isset($_POST['CancelItemChanges']) || isset($_POST['Index']))
	cash_line_start_focus($name);

if (isset($_POST['go']))
{
	display_quick_entries($_SESSION[$name], $_POST['person_id'], input_num('totamount'), 
		$_SESSION[$name]->trans_type==ST_CASH_RECEIPT ? QE_PAYMENT : QE_DEPOSIT);
	$_POST['totamount'] = price_format(0); $Ajax->activate('totamount');
	cash_line_start_focus();
}
//-----------------------------------------------------------------------------------------------

start_form();

display_gl_cash_header($_SESSION[$name]);

start_table(TABLESTYLE2, "width='90%'", 10);
start_row();
echo "<td>";
display_gl_cash_items(_("Deposit Items"), $_SESSION[$name]);
gl_options_controls($_SESSION[$name]);
echo "</td>";
end_row();
end_table(1);

//submit_center_first('Update', _("Update"), '', null);
//submit_center_last('Process', $_SESSION[$name]->trans_type==ST_CASH_PAYMENT ?
//	_("Process Payment"):_("Process Deposit"), '', 'default');

$Ajax->activate('Buttons');

div_start('Buttons');
submit_center_first('Process',_("Save"), false, false, ICON_SUBMIT);
br(2);

//$pre_jor = get_pre_journal($_SESSION[$name]->trans_type, $_SESSION[$name]->order_id);
//if (is_array($pre_jor) && ! ($pre_jor['approved_by'] > 0)) {


if ($_SESSION[$name]->count_gl_items() >= 1 && $_SESSION["wa_current_user"]->can_access_page('SA_APPROVE_GL_ADJUST')) {
    submit_center_first('Post', 'Approve', false, false, ICON_SUBMIT);  
}
div_end();

br(2);


    
end_form();

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

end_page();

