<?php

function  display_print_receipts_selecting_parameters() {
    start_table(TABLESTYLE_NOBORDER);
    
        start_row();
            $re_trans_array = build_receipt_transaction_types_array();
            custom_values_list_cells('Trans Type:', 'trans_type_id', $re_trans_array, null, 'Select a transaction type', true);
//            value_list_cells('Voucher Type:', 'filtering_receipt_type_id', 'RECEIPT_VOUCHER_TYPES', null, 'Select a receipt 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('Receipt No:', 'filtering_receipt_no', 12, 20);
            
            submit_cells("Search", 'Search');
        end_row();
        
    end_table(2);
}

function build_receipt_transaction_types_array() {
    global $systypes_array;
    
    $re_trans_array = array();
    
        $re_trans_array[ST_BANKDEPOSIT] = $systypes_array[ST_BANKDEPOSIT];
        $re_trans_array[ST_CASH_RECEIPT] = $systypes_array[ST_CASH_RECEIPT];
        $re_trans_array[ST_CUSTPAYMENT] = $systypes_array[ST_CUSTPAYMENT];
        
        return $re_trans_array;
}

function display_print_receipts_details($trans_type_id, $from_date, $to_date, $printed, $filtering_receipt_no) {
    global $systypes_array;
    
    div_start('trans_tbl');

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

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

        $j = 1;     $k = 0; //row colour counter
        
        $result = get_receipts_details($trans_type_id, $from_date, $to_date, $printed, $filtering_receipt_no);	
            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"]));
                    label_cell(get_comments_string($row["type"], $trans_no));

                    if ($row['receipt_type_id'] > 0) {
                        label_cell($row["receipt_name"]);
                        label_cell(sql2date($row["receipt_date"]));
                        label_cell($row["receipt_code"]);
                        
                        $_POST['receipt_date_'.$trans_no] = sql2date($row["receipt_date"]);
                        $_POST['receipt_reference_'.$trans_no] = $row["receipt_reference"];
                        $_POST['receipt_type_id_'.$trans_no] = $row["receipt_type_id"];
                        
                        hidden('receipt_date_'.$trans_no, sql2date($row["receipt_date"]));
                        hidden('receipt_reference_'.$trans_no, $row["receipt_reference"]);
                        hidden('receipt_type_id_'.$trans_no, $row["receipt_type_id"]);
                    } else {
                        value_list_cells('', 'receipt_type_id_'.$trans_no, 'RECEIPT_VOUCHER_TYPES', null, 'Select a receipt type', true);
                        $_POST['receipt_date_'.$trans_no] = sql2date($row["trans_date"]);
                        date_cells('', 'receipt_date_'.$trans_no, null, null, 0, 0, 0, null, true);
                        text_cells_ex(null, 'receipt_reference_'.$trans_no, 8, 8, null, null, null, null, true);
                    }
                    
                    $selected_rec_tp = isset($_POST['receipt_type_id_'.$trans_no]) ? $_POST['receipt_type_id_'.$trans_no] : -1;
                    $selected_rec_ref = isset($_POST['receipt_reference_'.$trans_no]) ? $_POST['receipt_reference_'.$trans_no] : '';
                    if ($selected_rec_tp > 0 || $row["rec_id"] > 0) {
                        $order_no = array("type" => $row['type'], 
                                          "trans_no" => $trans_no, 
                                          "receipt_type_id" => $selected_rec_tp,
                                          "receipt_date" => $_POST['receipt_date_'.$trans_no],
                                          "receipt_reference" => $_POST['receipt_reference_'.$trans_no]);
                        
                        $print_cheque_text = 'Print';
                        $print_link = print_document_link($order_no, $print_cheque_text, true, ST_PAYMENT_RECEIPT);
                        
                        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_receipts($type, $trans_no, $receipt_type_id, $receipt_date_, $receipt_reference) {
    global $systypes_array;
    
    $receipt_date = date2sql($receipt_date_);
    
    $pv_row = get_receipt_record($trans_no, $type);
        if (is_array($pv_row)) {
            
            $receipt_prefix = $pv_row['receipt_prefix'];
            $receipt_code = $pv_row['receipt_code'];
            $receipt_date = $pv_row['receipt_date'];

            update_receipt($pv_row['id'], $receipt_prefix, $receipt_code, $receipt_date); 
            
        } else {
            
            if (! ($receipt_type_id > 0)) {
                return array('success' => false, 'message' => 'Please select a receipt type.');
            }
            if (! is_date($receipt_date_)) {
                return array('success' => false, 'message' => 'Entered date is not valid.');
            }
            
            $trans_row = get_receipt_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));
            
            $receipt_count = get_receipts_count_from_to_dates($first_day, $last_day);
            $receipt_count += 1;
            
            $vtp = get_list_value($receipt_type_id);
            $vou_prefix = substr($vtp['text1'], 0, 3);
            
            $receipt_no = get_next_trans_no(ST_PAYMENT_RECEIPT); 
            
            if (strlen($receipt_reference) > 0) {
                $result = get_receipt_by_filters(0, 0, 0, $receipt_type_id, 0, '', $receipt_reference);
                    if (db_num_rows($result) > 0) {
                        $row = db_fetch($result);
                        return array('success' => false, 'message' => 'Entered reference already used in Receipt No. '.$row['receipt_code'].' 
                                                                       ('.$systypes_array[$row['type']].' #'.$row['trans_no'].').');
                    }
                    
                    $receipt_code = $receipt_reference;
            }
            else{

                if ($receipt_count < 10) {
                    $receipt_count_code = "000$receipt_count";
                } elseif ($receipt_count < 100) {
                    $receipt_count_code = "00$receipt_count";
                } elseif ($receipt_count < 1000) {
                    $receipt_count_code = "0$receipt_count";
                }
                

                $receipt_code = $vou_prefix.'/'.$year_code.'/'.$receipt_count_code;

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

            }
            
            add_receipt($receipt_no, $type, $trans_no, $trans_date, $receipt_type_id, $receipt_date, $receipt_reference, $receipt_count, $receipt_code, $vou_prefix);
//            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_receipts() {
    if (! ($_POST['trans_type_id'] > 0)) {
        return;
    }
   
       $result = get_receipts_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['receipt_type_id_'.$trans_no]);
                unset($_POST['receipt_date_'.$trans_no]);
                unset($_POST['receipt_reference_'.$trans_no]);
            }
}

?>