<?php

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

function db_has_member_cont_items() {
    return check_empty_result("SELECT COUNT(*) FROM " . TB_PREF . "contribution_items where !inactive");
}

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

function write_member_cont_item(Alloc $alloc) {
    if (!$alloc->id) {
        $sql = "INSERT INTO " . TB_PREF . "contribution_items (`contribution_id`,`project`,`amount`,`cont_group`,`inactive`) VALUES ("
                . db_escape($alloc->contribution_id) . ", "
                . db_escape($alloc->project) . ", "
                . db_escape($alloc->amount) . ", "
                . db_escape($alloc->group) . ", "
                . db_escape($alloc->inactive)
                . ")";
    } else {
        $sql = "UPDATE " . TB_PREF . "contribution_items SET 
                `contribution_id` = " . db_escape($alloc->contribution_id) . ",
                `project` = " . db_escape($alloc->project) . ",
                `amount` = " . db_escape($alloc->amount) . ",
                `cont_group` = " . db_escape($alloc->group) . ",
                `inactive` = " . db_escape($alloc->inactive) . "
                WHERE `id` = " . db_escape($alloc->id);
    }

    db_query($sql, _('Could not write Member Contribution data'));
}

function write_member_pledge_item(Alloc $alloc) {
    if (!$alloc->id) {
        $sql = "INSERT INTO " . TB_PREF . "pledge_items (`contribution_id`,`project`,`amount`,`paid_amt`,`cont_group`,`inactive`) VALUES ("
                . db_escape($alloc->contribution_id) . ", "
                . db_escape($alloc->project) . ", "
                . db_escape($alloc->amount) . ", "
                . db_escape($alloc->paid_amt) . ", "
                . db_escape($alloc->group) . ", "
                . db_escape($alloc->inactive)
                . ")";
    } else {
        $sql = "UPDATE " . TB_PREF . "pledge_items SET 
                `contribution_id` = " . db_escape($alloc->contribution_id) . ",
                `project` = " . db_escape($alloc->project) . ",
                `amount` = " . db_escape($alloc->amount) . ",
                `paid_amt` = " . db_escape($alloc->paid_amt) . ",
                `inactive` = " . db_escape($alloc->inactive) . ",
                `cont_group` = " . db_escape($alloc->group) . "
                WHERE `id` = " . db_escape($alloc->id);
    }

    db_query($sql, _('Could not write Member Contribution data'));
}

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


function void_member_cont_items($contribution_id) {
    $sql = "UPDATE  " . TB_PREF . "contribution_items set inactive=1 WHERE contribution_id = " . db_escape($contribution_id);
    db_query($sql, _('Could not delete member Contribution data.'));
}

function void_member_pledge_items($contribution_id) {
    $sql = "UPDATE  " . TB_PREF . "pledge_items set inactive=1 WHERE contribution_id = " . db_escape($contribution_id);
    db_query($sql, _('Could not delete member Contribution data.'));
}

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

function get_member_pledge_item($id) {
    $sql = "SELECT * FROM " . TB_PREF . "pledge_items WHERE id=" . db_escape($id);
    $result = db_query($sql, _('Could not get member contribution data.'));
    return db_fetch($result);
}
