<?php

/* =======================================================\
  |                        Church                          |
  |--------------------------------------------------------|
  \======================================================= */

function db_has_member_contributions() {
    return check_empty_result("SELECT COUNT(*) FROM " . TB_PREF . "person_contributions");
}

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

function write_member_contribution(Contribution $cont) {
    global $Refs;
    if (!$cont->id) {
        $sql = "INSERT INTO " . TB_PREF . "person_contributions (`ref`,`per_ID`,`type`,`date`,`due_date`,`bank`,`memo`,`inactive`) VALUES ("
                . db_escape($cont->ref) . ", "
                . db_escape($cont->per_ID) . ", "
                . db_escape($cont->type) . ", '"
                . date2sql($cont->date) . "', '"
                . date2sql($cont->due_date) . "', "
                . db_escape($cont->bank) . ", "
                . db_escape($cont->memo) . ", "
                . db_escape($cont->inactive)
                . ")";
    } else {
        $sql = "UPDATE " . TB_PREF . "person_contributions SET 
                `ref` = " . db_escape($cont->ref) . ",
                `per_ID` = " . db_escape($cont->per_ID) . ",
                `type` = " . db_escape($cont->type) . ",
                `date` = '" . date2sql($cont->date) . "',
                `due_date` = '" . date2sql($cont->due_date) . "',
                `bank` = " . db_escape($cont->bank) . ",
                `memo` = " . db_escape($cont->memo) . ",
                `inactive` = " . db_escape($cont->inactive) . "
                WHERE `id` = " . db_escape($cont->id);
    }

    db_query($sql, _('Could not write Member Contribution data'));
    if (!$cont->id) {
        $cont->id = db_insert_id();
    }

    void_member_cont_items($cont->id);
    void_member_pledge_items($cont->id);
    void_gl_trans($cont->type, $cont->id);
    $total_pay = 0;
    if (count($cont->allocs)) {
        foreach ($cont->allocs as $alloc) {
            $alloc->contribution_id = $cont->id;
            if ($cont->type == ST_MEMBER_CONT && $alloc->amount != 0) {
                write_member_cont_item($alloc);
                $project = get_projects($alloc->project);
                add_gl_trans(ST_MEMBER_CONT, $cont->id, $cont->date, $project['gl_account'], $project['proj_id'], 0, $cont->memo, (-1 * $alloc->amount), null, PT_MEMBER, $cont->per_ID);

                if ($alloc->id) {
                    $pledge_item = get_member_pledge_item($alloc->id);
                    write_member_pledge_item(new Alloc($alloc->id, $pledge_item['contribution_id'], $pledge_item['project'], $pledge_item['amount'], ($alloc->amount + $pledge_item['paid_amt']), 0, $pledge_item['amount'],$pledge_item['cont_group']));
                }
                $total_pay += $alloc->amount;
            } else if ($alloc->amount != 0) {
                write_member_pledge_item($alloc);
            }
        }
    }

    if ($cont->type == ST_MEMBER_CONT && $total_pay != 0) {
        $bank_gl_account = get_bank_gl_account($cont->bank);
        $bank = get_bank_account($cont->bank);

        /* Bank account entry first */
        add_gl_trans(ST_MEMBER_CONT, $cont->id, $cont->date, $bank_gl_account, 0, 0, '', $total_pay, $bank['bank_curr_code'], PT_MEMBER, $cont->per_ID);
        /* now enter the bank_trans entry */
        add_bank_trans(ST_MEMBER_CONT, $cont->id, $cont->bank, $cont->ref, $cont->date, $total_pay, PT_MEMBER, $cont->per_ID);
    }

    add_comments($cont->type, $cont->id, $cont->date, $cont->memo);

    $Refs->save($cont->type, $cont->id, $cont->ref);
    return $cont->id;
}

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

function get_member_contributions($id = 0, $per_ID = '', $cart = 0, $type = '', $bank = '', $start_date = '', $end_date = '',$project = '') {

    $where = '';
    if ($id)
        $where .= " AND t.id=" . db_escape($id);
    if (!empty($per_ID))
        $where .= " AND t.per_ID=" . db_escape($per_ID);
    if (!empty($type))
        $where .= " AND t.type=" . db_escape($type);
    if (is_date($start_date) && is_date($end_date))
        $where .= " AND t.date between '" . date2sql($start_date) . "' and '" . date2sql($end_date)."'";
    if (!empty($bank))
        $where .= " AND t.bank=" . db_escape($bank);
    
    
    if ($id) {
        $sql = "SELECT * FROM " . TB_PREF . "person_contributions t WHERE 1 ";
        $result = db_query($sql . $where, _('Could not get member data.'));
        $row = db_fetch($result);
        $cont = new Contribution();
        $cont->bank = $row['bank'];
        $cont->date = $row['date'];
        $cont->due_date = $row['due_date'];
        $cont->id = $row['id'];
        $cont->inactive = $row['inactive'];
        $cont->memo = $row['memo'];
        $cont->per_ID = $row['per_ID'];
        $cont->type = $row['type'];

        if ($row['type'] == ST_MEMBER_CONT) {
            $allocs = db_query("SELECT * FROM " . TB_PREF . "contribution_items "
                    . " WHERE !inactive and contribution_id=$id ", _('Could not get member transation data.'));
        } else {
            $allocs = db_query("SELECT * FROM " . TB_PREF . "pledge_items "
                    . " WHERE !inactive and contribution_id=$id ", _('Could not get member transation data.'));
        }
        $exclude = Array();
        while ($myrow = db_fetch($allocs)) {
            $cont->allocs[] = new Alloc($myrow['id'], $myrow['contribution_id'], $myrow['project'], $myrow['amount'], isset($myrow['paid_amt']) ? $myrow['paid_amt'] : 0, $myrow['inactive'], isset($myrow['pledged_amt']) ? $myrow['pledged_amt'] : 0,$myrow['cont_group']);
            $exclude[] = $myrow['project'];
        }

        $projects = db_query(get_projects(false, false, false, $exclude));
        while ($myrow = db_fetch($projects)) {
            $cont->allocs[] = new Alloc(0, $id, $myrow['proj_id'], 0, 0, 0, 0,'');
        }
        return $cont;
    } else if ($cart) {
        $cont = new Contribution();
        if (!empty($per_ID)) {
            $allocs = db_query("SELECT p.* FROM " . TB_PREF . "pledge_items p"
                    . " LEFT JOIN " . TB_PREF . "person_contributions c on p.contribution_id=c.id "
                    . " WHERE !c.inactive and !p.inactive and (p.amount-p.paid_amt)!=0 and per_ID=$per_ID ", _('Could not get member transation data.'));
            $exclude = Array();
            while ($myrow = db_fetch($allocs)) {
                $exclude[] = $myrow['project'];
                $cont->allocs[] = new Alloc($myrow['id'], $myrow['contribution_id'], $myrow['project'], $myrow['amount'], $myrow['paid_amt'], $myrow['inactive'], $myrow['amount'],$myrow['cont_group']);
            }

            $projects = db_query(get_projects(false, false, false, $exclude));
            while ($myrow = db_fetch($projects)) {
                $cont->allocs[] = new Alloc(0, $id, $myrow['proj_id'], 0, 0, 0, 0,'');
            }
        }
        return $cont->allocs;
    } else {
        if (!empty($project))
            $where .= " AND c.project=" . db_escape($project);
        $sql = "SELECT t.type,t.id,t.ref,concat(m.per_FirstName,' ',m.per_LastName) as name,d.proj_name,t.date,"
                . " b.bank_account_name, c.amount FROM " . TB_PREF . "contribution_items c "
                . " right join " . TB_PREF . "person_contributions t on t.id=c.contribution_id "
                . " inner join " . TB_PREF . "person_per m on t.per_ID=m.per_ID "
                . " inner join " . TB_PREF . "projects d on d.proj_id=c.project "
                . " inner join " . TB_PREF . "bank_accounts b on t.bank=b.id WHERE !t.inactive  " .$where
                . " and type=".ST_MEMBER_CONT
                . " ";
        return $sql . $where;
    }
}

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

function get_member_cont($per_ID, $proj_id) {
    $sql = "SELECT * FROM " . TB_PREF . "pledge_items p"
            . " inner join " . TB_PREF . "contribution_items c on c. WHERE 1 ";
    $result = db_query($sql . $where, _('Could not get member data.'));
    $row = db_fetch($result);
}



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

function delete_member_contribution($id) {
    $sql = "DELETE FROM " . TB_PREF . "person_contributions WHERE id = " . db_escape($id);
    db_query($sql, _('Could not delete member Contribution data.'));
}

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

