<?php

function  display_print_payment_vouchers_selecting_parameters() {
    start_table(TABLESTYLE_NOBORDER);
    
        start_row();
            $pv_trans_array = build_payment_voucher_transaction_types_array();
            custom_values_list_cells('Trans Type:', 'trans_type_id', $pv_trans_array, null, 'Select a transaction type', true);
            date_cells(_("From:"), 'from_date');
            date_cells(_("To:"), 'to_date');
            three_value_list_cells('Printed:', 'printed', null, 'All', 'All', 'Printed', 'Printed', 'Not Printed', 'Not Printed', true);
            text_cells_ex('Voucher No:', 'filtering_voucher_no', 12, 20);
            text_cells_ex('Reference:', 'filtering_voucher_code', 12, 20);
            
            submit_cells("Search", 'Search');
        end_row();
        
    end_table(2);
}

function build_payment_voucher_transaction_types_array() {
    global $systypes_array;
    
    $pv_trans_array = array();
    
        $pv_trans_array[ST_BANKPAYMENT] = $systypes_array[ST_BANKPAYMENT];
        $pv_trans_array[ST_CASH_PAYMENT] = $systypes_array[ST_CASH_PAYMENT];
        $pv_trans_array[ST_SUPPAYMENT] = $systypes_array[ST_SUPPAYMENT];
        $pv_trans_array[ST_BANKTRANSFER] = $systypes_array[ST_BANKTRANSFER];
		 $pv_trans_array[ST_PETTY_CASH_FLOAT_REIMBUR] = $systypes_array[ST_PETTY_CASH_FLOAT_REIMBUR];
        $pv_trans_array[ST_I_OWE_YOU_PAYMENT] = $systypes_array[ST_I_OWE_YOU_PAYMENT];
        
        return $pv_trans_array;
}

function display_print_payment_vouchers_details($trans_type_id, $from_date, $to_date, $printed, $filtering_voucher_no, $filtering_voucher_code) {
    global $systypes_array;
    
    div_start('trans_tbl');

        start_table(TABLESTYLE, "width=98%");

        $th = array(_("Vou. No."), _("Tran. Type"), _("#"), _("Reference"), _("Date"), _("Amount"), _("Person"), _("Memo"), _("Bank Acc"), _("Vouch. Type"), 
                    _("Date"), _("Voucher Reference"), "");
            table_header($th);
            
            if (! ($trans_type_id > 0)) {
                end_table();
                div_end();
                
                display_note('Please select a transation type and voucher type in the table then click print.', 2, 2, 'style=color:red');
                return;
            }

        $j = 1;     $k = 0; //row colour counter
        
        $result = get_payment_vouchers_details($trans_type_id, $from_date, $to_date, $printed, $filtering_voucher_no, $filtering_voucher_code);	
            while ($row = db_fetch($result)) {
                $trans_no = $row["trans_no"];
                
                alt_table_row_color($k);

                    label_cell($row["rec_id"], 'align=center');
                    label_cell($systypes_array[$row["type"]]);
                    label_cell(get_trans_view_str($row["type"], $trans_no));
                    label_cell(get_trans_view_str($row["type"], $trans_no, $row['ref']));
                    label_cell(sql2date($row["trans_date"]));
                    amount_cell(-$row["amount"]);
                    label_cell(payment_person_name($row["person_type_id"],$row["person_id"], true, $row["person_detail_id"]));
                    label_cell(get_comments_string($row["type"], $trans_no));
                    $row["bank_act"] > 0 ? label_cell(get_bank_account_formatted_name($row["bank_act"])) : label_cell('');

                    if ($row['voucher_type_id'] > 0) {
                        label_cell($row["voucher_name"]);
                        label_cell(sql2date($row["voucher_date"]));
//                        label_cell(text_bold($row["voucher_code"])."<br>".$row["voucher_reference"]);
                        label_cell($row["voucher_code"]);
                        
                        $_POST['voucher_date_'.$trans_no] = sql2date($row["voucher_date"]);
                        $_POST['voucher_reference_'.$trans_no] = $row["voucher_reference"];
                        $_POST['voucher_type_id_'.$trans_no] = $row["voucher_type_id"];
                        
                        hidden('voucher_date_'.$trans_no, sql2date($row["voucher_date"]));
                        hidden('voucher_reference_'.$trans_no, $row["voucher_reference"]);
                        hidden('voucher_type_id_'.$trans_no, $row["voucher_type_id"]);
                    } else {
                        value_list_cells('', 'voucher_type_id_'.$trans_no, 'PAYMENT_VOUCHER_TYPES', null, 'Select a voucher type', true);
                        date_cells('', 'voucher_date_'.$trans_no, null, null, 0, 0, 0, null, true);
                        text_cells_ex(null, 'voucher_reference_'.$trans_no, 8, 8, null, null, null, null, true);
                    }
                    
                    $selected_vou_tp = isset($_POST['voucher_type_id_'.$trans_no]) ? $_POST['voucher_type_id_'.$trans_no] : -1;
                    $selected_vou_ref = isset($_POST['voucher_reference_'.$trans_no]) ? $_POST['voucher_reference_'.$trans_no] : '';
//                    if (($selected_vou_tp > 0 && strlen($selected_vou_ref) > 0) || $row["rec_id"] > 0) {
                    if (($selected_vou_tp > 0) || $row["rec_id"] > 0) {
                        $order_no = array("type" => $row['type'], 
                                          "trans_no" => $trans_no, 
                                          "voucher_type_id" => $selected_vou_tp,
                                          "voucher_date" => $_POST['voucher_date_'.$trans_no],
                                          "voucher_reference" => $selected_vou_ref, 
                                          "bank_act" => $row['bank_act']);
                        
                        $print_cheque_text = 'Print';
                        $print_link = print_document_link($order_no, $print_cheque_text, true, ST_PAYMENT_VOUCHER);
                        
                        label_cell($print_link, 'align=center');
                    } else {
                        label_cell('');
                    }

                end_row();

                    if ($j == 12) {
                        $j = 1;
                        table_header($th);
                    }
                    
                    $j++;
            }

        end_table(2);
    div_end();
}


function save_payment_vouchers($type, $trans_no, $voucher_type_id, $voucher_date_, $voucher_reference, $bank_act_id) {
    global $systypes_array;
    
        if (!is_date($voucher_date_)) {
            return array('success' => false, 'message' => 'Entered date is not valid.');
        }
        
        $voucher_date = date2sql($voucher_date_);
            
        $acc = get_bank_account($bank_act_id);
        if (is_array($acc)) {
            $bank_code = $acc['voucher_prefix'];
        } else {
            $bank_code = 'CASH';
        }
    
        $pv_row = get_payment_voucher_record($trans_no, $type);
        if (is_array($pv_row)) {
//            $vtp = get_list_value($pv_row['voucher_type_id']);
//            $vtp_code = substr($vtp['text1'], 0, 3);
//                
//            $voucher_code = $vtp_code.'_'.$bank_code.'_'.$pv_row['voucher_no'];
//            
//            $cve_msg = check_voucher_code_exisits($voucher_code, $pv_row['id']);
//            if ($cve_msg['success'] !== true) {
//                return array('success' => false, 'message' => $cve_msg['message']);
//            }
//            update_payment_voucher($pv_row['id'], $bank_act_id, $bank_code, $voucher_code, $voucher_date);
            
            $voucher_code = $pv_row['voucher_code'];
            update_payment_voucher($pv_row['id'], $bank_act_id, $bank_code, $voucher_code, $voucher_date);
            
        } else {
            
            if (! ($voucher_type_id > 0)) {
                return array('success' => false, 'message' => 'Please select a voucher type.');
            }
            
            $trans_row = get_payment_vouchers_trans_type_record($type, $trans_no);
            $trans_date = $trans_row['trans_date'];
            
            $year_code = substr($trans_date, 2, 2); 

            $first_day = date('Y-01-01', strtotime($trans_date));
            $last_day = date('Y-12-t', strtotime($trans_date));
            
            $vou_count_code = get_payment_vouchers_count_from_to_dates($first_day, $last_day);
            $vou_count_code += 1;
                
            $voucher_no = get_next_trans_no(ST_PAYMENT_VOUCHER); 
                
            if (strlen($voucher_reference) > 0) {
                $result = get_payment_voucher_by_filters(0, 0, 0, $voucher_type_id, 0, '', $voucher_reference);
                    if (db_num_rows($result) > 0) {
                        $row = db_fetch($result);
                        return array('success' => false, 'message' => 'Entered reference already used in Voucher No. '.$row['voucher_code'].' 
                                                                       ('.$systypes_array[$row['type']].' #'.$row['trans_no'].').');
                    }
                    
                $voucher_code = $voucher_reference;
            }
            else{
            
                $vtp = get_list_value($voucher_type_id);
                $vtp_code = substr($vtp['text1'], 0, 3);
            
                if ($vou_count_code < 10) {
                    $vou_count_code = "000$vou_count_code";
                } elseif ($vou_count_code < 100) {
                    $vou_count_code = "00$vou_count_code";
                } elseif ($vou_count_code < 1000) {
                    $vou_count_code = "0$vou_count_code";
                }

                $voucher_code = $vtp_code.'/'.$year_code.'/'.$vou_count_code;

                $cve_msg = check_voucher_code_exisits($voucher_code, 0);
                    if ($cve_msg['success'] !== true) {
                        return array('success' => false, 'message' => $cve_msg['message']);
                    }
                    
            }

                 

            add_payment_voucher($voucher_no, $type, $trans_no, $trans_date, $voucher_type_id, $voucher_date, $voucher_reference, $vou_count_code, $voucher_code, $bank_act_id, 
                            $bank_code);
            
        }
        
        return array('success' => true, 'message' => '');
}

function clear_print_payment_vouchers() {
    if (! ($_POST['trans_type_id'] > 0)) {
        return;
    }
   
       $result = get_payment_vouchers_details($_POST['trans_type_id'], $_POST['from_date'], $_POST['to_date'], 
                                              $_POST['printed']);	
            while ($row = db_fetch($result)) {
                $trans_no = $row["trans_no"];
                
                unset($_POST['voucher_type_id_'.$trans_no]);
                unset($_POST['voucher_date_'.$trans_no]);
                unset($_POST['voucher_reference_'.$trans_no]);
            }
}

?>