<?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>.
 * ********************************************************************* */
$page_security = 'SA_MMBR_CONT';
$path_to_root = "../../..";
include_once($path_to_root . "/modules/church/includes/entity/contribution.php");
include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/banking.inc");
include_once($path_to_root . "/modules/church/includes/church_db.inc");
include_once($path_to_root . "/modules/church/includes/church_ui.inc");
include_once($path_to_root . '/modules/kanban/includes/kanban_db.inc');
include_once($path_to_root . '/modules/kanban/includes/kanban_ui.inc');
add_access_extensions();

$js = "";
if ($SysPrefs->use_popup_windows) {
    $js .= get_js_open_window(900, 500);
}
if (user_use_date_picker()) {
    $js .= get_js_date_picker();
}
add_js_file('payalloc.js');

$type = ST_MEMBER_CONT;
if (isset($_POST['type'])) {
    $type = $_POST['type'];
}
if (isset($_GET['type'])) {
    $type = $_GET['type'];
}

if (isset($_GET['per_ID'])) {
    $_POST['per_ID'] = $_GET['per_ID'];
}


if (!isset($_POST['bank_account'])) { // first page call
    $_SESSION['alloc'] = new Contribution();
    $_SESSION['alloc']->type = $type;
    if (isset($_GET['id'])) {
        $_SESSION['alloc'] = get_member_contributions($_GET['id'], '', 0);
        $type = $_SESSION['alloc']->type;
    }
}


if (!isset($_POST['per_ID'])) {
    $_SESSION['alloc']->allocs = get_member_contributions(0, get_post('per_ID'), 1, get_post('type'));
}

page(_($help_context = $systypes_array[$type] . " Entry"), false, false, "", $js);
//----------------------------------------------------------------------------------------------

if (!db_has_members()) {
    display_error(_("There are no customers defined in the system."), true);
}

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

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



if (isset($_GET['AddedID'])) {
    $payment_no = $_GET['AddedID'];

    display_notification_centered(_("The Member Contribution has been successfully entered."));
    display_sms_notifications();
    //submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');
    //submenu_view(_("&View this Customer Payment"), ST_CUSTPAYMENT, $payment_no);

    submenu_option(_("Enter Another &Member Contribution"), "/modules/church/manage/addcontributions.php?type=80");
    submenu_option(_("Enter a &Member Pledge"), "/modules/church/manage/addcontributions.php?type=81");
    display_note(get_gl_view_str($type, $payment_no, _("&View the GL Journal Entries for this Member Contribution")));

    display_footer_exit();
} elseif (isset($_GET['UpdatedID'])) {
    $payment_no = $_GET['UpdatedID'];

    display_notification_centered(_("The Member Contribution has been successfully updated."));
    display_sms_notifications();
    //submenu_print(_("&Print This Receipt"), ST_CUSTPAYMENT, $payment_no."-".ST_CUSTPAYMENT, 'prtopt');

    submenu_option(_("Enter Another &Member Contribution"), "/modules/church/manage/addcontributions.php?type=80");
    submenu_option(_("Enter a &Member Pledge"), "/modules/church/manage/addcontributions.php?type=81");
    display_note(get_gl_view_str($type, $payment_no, _("&View the GL Journal Entries for this Member Contribution")));
    display_footer_exit();
}

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

function can_process() {
    global $Refs, $type;

    get_post_data();
    if (!get_post('per_ID')) {
        display_error(_("There is no Member selected."));
        set_focus('per_ID');
        return false;
    }

    if (!isset($_POST['date']) || !is_date($_POST['date'])) {
        display_error(_("The entered date is invalid. Please enter a valid date for the payment."));
        set_focus('date');
        return false;
    } elseif (!is_date_in_fiscalyear($_POST['date'])) {
        display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
        set_focus('date');
        return false;
    }

    if (!check_reference($_POST['ref'], $type, @$_POST['id'])) {
        set_focus('ref');
        return false;
    }

    if (isset($_POST["TotalNumberOfAllocs"]))
        return check_cont_allocations();
    else
        return true;
}

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

if (get_post('AddPaymentItem') && can_process()) {

    new_doc_date($_POST['date']);

    $new_pmt = !$_SESSION['alloc']->id;
    //Chaitanya : 13-OCT-2011 - To support Edit feature
    $payment_no = write_member_contribution($_SESSION['alloc']);
    if ($payment_no) {
        $sms = new SMS();
        $member = new Member();
        $member = get_members($_SESSION['alloc']->per_ID);
        $data = (array) $member;
        if (strlen($member->per_HomePhone) >= 10) {
            $data['bank'] = $_SESSION['alloc']->bank;
            $data['date'] = sql2date($_SESSION['alloc']->date);
            $data['due_date']= sql2date($_SESSION['alloc']->due_date);
            $data['memo']= $_SESSION['alloc']->memo;
            $data['per_ID']= $_SESSION['alloc']->per_ID;
            $data['type']= $_SESSION['alloc']->type;
            $data['ref']= $_SESSION['alloc']->ref;
            if (count($_SESSION['alloc']->allocs)) {
                foreach ($_SESSION['alloc']->allocs as $alloc) {
                    $alloc->contribution_id = $_SESSION['alloc']->id;
                    if ($_SESSION['alloc']->type == ST_MEMBER_CONT && $alloc->amount != 0) {
                        $project = get_projects($alloc->project);
                        $data['proj_id'] = $project['proj_id'];
                        $data['proj_name'] = $project['proj_name'];
                        $data['proj_amt'] = price_format($project['proj_amt']);
                        $data['amount'] = $alloc->amount;
                        $data['phone_contacts'] = $member->per_HomePhone;
                        $data['trans_type'] = ST_MEMBER_CONT;
                        $sms->send_template_notification($data);
                    }
                }
            }
        }
    }
    //exit();
    unset($_SESSION['alloc']);
    meta_forward($_SERVER['PHP_SELF'], $new_pmt ? "type=$type&AddedID=$payment_no" : "type=$type&UpdatedID=$payment_no");
}

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

function get_post_data() {
    $_SESSION['alloc']->bank = $_POST['bank_account'];
    $_SESSION['alloc']->date = $_POST['date'];
    $_SESSION['alloc']->due_date = $_POST['due_date'];
    $_SESSION['alloc']->memo = $_POST['memo'];
    $_SESSION['alloc']->per_ID = $_POST['per_ID'];
    $_SESSION['alloc']->type = $_POST['type'];
    $_SESSION['alloc']->ref = $_POST['ref'];
    $_SESSION['alloc']->inactive = 0;
}

//----------------------------------------------------------------------------------------------
$new = 1;

// To support Edit feature
if (isset($_GET['id']) && $_GET['id'] > 0) {
    $_POST['id'] = $_GET['id'];

    $new = 0;
    $_SESSION['alloc'] = get_member_contributions($_POST['id'], '', 1);
    $_POST['bank_account'] = $_SESSION['alloc']->bank;
    $_POST['date'] = sql2date($_SESSION['alloc']->date);
    $_POST['due_date'] = sql2date($_SESSION['alloc']->due_date);
    $_POST['memo'] = $_SESSION['alloc']->memo;
    $_POST['per_ID'] = $_SESSION['alloc']->per_ID;
    $_POST['type'] = $_SESSION['alloc']->type;
    $_POST['ref'] = $_SESSION['alloc']->ref;
    $Ajax->activate('alloc_tbl');
}

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

$new = !$_SESSION['alloc']->id;
start_form();

hidden('id', $_SESSION['alloc']->id);
hidden('type', $type);

start_outer_table(TABLESTYLE2, "width='60%'", 5);

table_section(1);

member_list_row(_("From Member:"), 'per_ID', null, _('Select Member'), true);

bank_accounts_list_row(_("Into Bank Account:"), 'bank_account', null, FALSE);

if (list_updated('per_ID')) {
    $_SESSION['alloc']->allocs = get_member_contributions(0, get_post('per_ID'), 1);
    $Ajax->activate('alloc_tbl');
}


table_section(2);
date_row(_("Date :"), 'date', '', true, 0, 0, 0, null, true);
if ($type == ST_MEMBER_PLEDGE) {
    date_row(_("Due Date:"), 'due_date', '', true, 0, 0, 0, null, true);
} else {
    hidden('due_date');
}
ref_row(_("Reference:"), 'ref', '', null, '', $type);
textarea_row(_("Memo:"), 'memo', null, 22, 4);
$comp_currency = get_company_currency();
$_SESSION['alloc']->currency = $bank_currency = get_bank_account_currency($_POST['bank_account']);


end_outer_table(1);

div_start('alloc_tbl');
show_allocatable_contributions(TRUE);
div_end();


if ($new)
    submit_center('AddPaymentItem', _("Add Contribution"), true, '', 'default');
else
    submit_center('AddPaymentItem', _("Update Contribution"), true, '', 'default');

br();

end_form();
end_page();
