<?php
$path_to_root = "../../..";
include_once($path_to_root . "/modules/api/session-custom.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/types.inc");
include_once($path_to_root . "/admin/db/company_db.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/entity/contribution.php");
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');

//echo date2sql(('20/11/2018'));die();
if (true) {
    
    $res = json_decode(file_get_contents('php://input'));
    $ssd_member = get_member_by_phone(substr(trim($res->MSISDN),-9));
    $ref = explode("-", $res->BillRefNumber);
    $group = '';
    if (isset($ref[1])) {
        $group = $ref[1];
    }
    
    $prefs = get_company_prefs();
    
    //$how = user_date_format();
    $_SESSION["wa_current_user"]->prefs->date_format = $SysPrefs->dflt_date_fmt;
    //echo 'USER FORMAT : '.$_SESSION["wa_current_user"]->prefs->date_format();
    //echo 'CONFIG FORMAT : '.$SysPrefs->dflt_date_fmt;
    $AccountNumber = '';
    if(empty($ref[0]) || !isset($ref[0])){
        $project = get_projects($prefs['project_suspense_account']);
    } else {
        $AccountNumber = $ref[0];
        $project = get_project_by_ref($ref[0]);
        if(empty($project['proj_id'])){
            $project = get_projects($prefs['project_suspense_account']);
        }
    }
    
    if(!empty($ssd_member['per_ID'])){
        $person = $ssd_member['per_ID'];
    } else {
        $person = $prefs['member_suspense_account'];
    }
    
    $_SESSION['alloc'] = new Contribution();
    $_SESSION['alloc']->type = ST_MEMBER_CONT;
    $_SESSION['alloc']->bank = get_company_pref('mpesa_api_bank_account');
    $_SESSION['alloc']->date = date('d/m/Y');
    $_SESSION['alloc']->due_date = date('d/m/Y');
    $_SESSION['alloc']->memo = $res->TransactionType . ', ' . $res->MSISDN . ', ' . $res->TransTime .
            ', ' . $res->FirstName . ', ' . $res->MiddleName . ', ' . $res->LastName . ', TRANS ID : ' . $res->BillRefNumber;
    $_SESSION['alloc']->per_ID = $person;
    $_SESSION['alloc']->ref = $Refs->get_next(ST_MEMBER_CONT, null, array('date' => Today()));
    $_SESSION['alloc']->inactive = 0;
    $_SESSION['alloc']->allocs[] = new Alloc('', '', $project['proj_id'], $res->TransAmount, 0, 0, 0, $group);
    
}

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

if (TRUE) {

    $payment_no = write_member_contribution($_SESSION['alloc']);
    if ($payment_no) {
        $sms = new SMS();
        $member = new Member();
        $member = get_members($_SESSION['alloc']->per_ID);
        $member->per_HomePhone = '+'.$res->MSISDN;
        if(empty($ssd_member['per_ID'])){
            $member->per_FirstName = $res->FirstName;
            $member->per_LastName = $res->LastName;
            $member->per_MiddleName = $res->MiddleName;
        }
        $data = (array) $member;
        if (strlen($member->per_HomePhone) >= 10) {
            $data['bank'] = $_SESSION['alloc']->bank;
            $data['date'] = ($_SESSION['alloc']->date);
            $data['due_date'] = ($_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'] = $AccountNumber;
                        $data['proj_amt'] = price_format($project['proj_amt']);
                        $data['amount'] = price_format($alloc->amount);
                        $data['phone_contacts'] = $member->per_HomePhone;
                        $data['trans_type'] = ST_MEMBER_CONT;
                        $sms->send_template_notification($data);
                    }
                }
            }
        }
    }
}



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

header("Content-Type:application/json");
echo '{"ResultCode":0, "ResultDesc":"sucessful Confirmation", "ThirdPartyTransID": 0}';
?>


