<?php

function display_employee_petty_cash_outstanding_filtering_criteria() {
    start_table(TABLESTYLE_NOBORDER);
        start_row();

            group_companies_list_cells("Company:", 'company_code', null, false, true);
            group_company_employees_simple_list_cells("Employee:", 'employee_code', $_POST['company_code'], null, 'Please select an employee', true);
            date_cells(_("From:"), 'from_date', '', null, -14, 0, 0, null, true);
            date_cells(_("To:"), 'to_date', '', null, 0, 0, 0, null, true);
        start_row();
        end_row();
            petty_cash_floats_list_cells('Float:', 'float_id', null, '', true);
            project_list_cells('Project:', 'project_id', null, '', true);
            yesno_list_cells("Display Completed:", 'batch_complete_filter', null, "Yes", "No", true);
            yesno_list_cells("Outstanding:", 'outstanding_calculation', null, "Approved expenses only", "Include released expenses", true);
            submit_cells('RefreshInquiry', _("Display"),'',_('Refresh Inquiry'), 'default');
        
        end_row();
    end_table();
}

function display_iou_batch_zero($employee, $from_date, $to_date, &$emp_req_amount, &$emp_iou_payments, &$emp_outstanding, $type, $display_completed, 
                                $float_id, $project_id, &$k, &$i, $th, $ob_amount) {
    global $systypes_array; 
    
    $iou_numbers = get_epco_batch_zero_iou_numbers($from_date, $to_date, $employee, $type, $display_completed, $float_id, $project_id);
        if (! (db_num_rows($iou_numbers) > 0)) {
            return;
        }
        
        start_row();
            label_cell($systypes_array[ST_I_OWE_YOU].'s (With No Expenses)', 'colspan=16 align=center style=background-color:#0ec9ba');
        end_row();
        
        
        while ($iou_number = db_fetch($iou_numbers)) {
            $iou_no = $iou_number["iou_id"];

            $iou_data = get_epco_batch_zero_iou_payment_details($iou_no);
            if (! (db_num_rows($iou_data) > 0)) {
                return;
            }
        

        
            $pre_iou_id = 0;
            while ($iou_header = db_fetch($iou_data)) {
                if ($i >= 15) {
                    table_header($th);
                    
                    $i = 0;
                }
                
                $iou_id = $iou_header['id'];
                
                alt_table_row_color($k);
                    label_cell(sql2date($iou_header['tran_date']), 'align=center');
                    label_cell($iou_header['float_name']);
                    label_cell($iou_header['project_name']);
                    label_cell(get_petty_cash_trans_view_str(ST_I_OWE_YOU, $iou_id), 'align=center');
                    label_cell('');
                    ($pre_iou_id != $iou_id) ? amount_cell($iou_header['amount']) : label_cell('');
                    
                    if ($iou_header['type'] > 0) {
                        label_cell($systypes_array[$iou_header['type']]);
                    } else {
                        label_cell('');
                    }
                    
                    if ($iou_header['trans_no'] > 0) {
                        label_cell(get_trans_view_str($iou_header['type'], $iou_header['trans_no']), 'align=center');
                    } else {
                        label_cell('');
                    }
                    label_cell(sql2date($iou_header['pay_date']), 'align=center');
                    
                    $payment = $iou_header['pay_amount'];
                    amount_cell($payment);
                    
                    label_cell(''); label_cell(''); 
                    $emp_iou_payments += $payment;
                    
                    if ($pre_iou_id != $iou_id) {
                        $emp_req_amount += $iou_header['amount'];
                    } 
                    $emp_outstanding += $payment;
                    
                    label_cell(''); 
                    
//                    amount_cell($emp_outstanding);
                    amount_cell($payment);
                    amount_cell($emp_outstanding);
                    label_cell(get_yes_no_name($iou_header['batch_complete']), 'align=center');
                end_row();
                
                $i++;
                
                $pre_iou_id = $iou_id;
            }
        }
}

function display_iou_batch_not_zero($employee, $from_date, $to_date, &$emp_req_amount, &$emp_iou_payments, &$emp_batch_pymnts, &$emp_expences, 
                                    &$emp_outstanding, $type, $display_completed, $float_id, $project_id, &$k, &$i, $th, $ob_amount, 
                                    $outstanding_calculation) {
    
    global $approval_statuses, $systypes_array; 
     
    $iou_id = $current_batch_no =  $previous_batch_no = 0;
    
    $iou_batches_result = get_epco_iou_batch_numbers($from_date, $to_date, $employee, $type, $display_completed, $float_id, $project_id);
        if (! (db_num_rows($iou_batches_result) > 0)) {
            return;
        }
        
        start_row();
            label_cell($systypes_array[ST_I_OWE_YOU].'s (With Expenses)', 'colspan=16 align=center style=background-color:#0ec9ba');
        end_row();
        
        $pre_iou_id = $pre_batch_no = 0;
    
        while ($iou_batches = db_fetch($iou_batches_result)) {
            $iou_batch_no = $iou_batches['batch_no'];
            
            $iou_batch_row = get_rep_iou_batch_record($iou_batch_no);
            $iou_batch_expenses_amount = $iou_batch_row['amount'];
        
            $total_batch_payment = 0;
            $iou_data = get_epco_iou_batch_payment_details($iou_batch_no);
                $db_num_rows = db_num_rows($iou_data);
                while ($iou_header = db_fetch($iou_data)) { 
                    
                    if ($i >= 15) {
                        table_header($th);
                        
                        $i = 0;
                    }
                    $current_batch_no = $iou_header['batch_no'];
                    
                    if ($pre_batch_no != $current_batch_no) {
                        $emp_expences += $iou_batch_expenses_amount;
                    }
                    
                    $iou_id = $iou_header['id'];
                    $iou_amount = $iou_header['amount'];
                    
                    alt_table_row_color($k);
                        label_cell(sql2date($iou_header['tran_date']), 'align=center');
                        label_cell($iou_header['float_name']);
                        label_cell($iou_header['project_name']);
                        label_cell(get_petty_cash_trans_view_str(ST_I_OWE_YOU, $iou_id), 'align=center');
                        label_cell(get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $current_batch_no), 'align=center');
                        ($pre_iou_id != $iou_id) ? amount_cell($iou_amount) : label_cell('');
                        
                        if ($iou_header['type'] > 0) {
                            label_cell($systypes_array[$iou_header['type']]);
                        } else {
                            label_cell('');
                        }

                        if ($iou_header['trans_no'] > 0) {
                            label_cell(get_trans_view_str($iou_header['type'], $iou_header['trans_no']), 'align=center');
                        } else {
                            label_cell('');
                        }
                        label_cell(sql2date($iou_header['pay_date']), 'align=center');

                        $iou_total_payment = $iou_header['pay_amount'];
                        amount_cell($iou_total_payment);
                    
                        if($outstanding_calculation == 1){ 
                            if ($iou_batch_row['approval_status'] == AS_APPROVED) {
                                $emp_outstanding += $iou_total_payment;
                            }
                        }else{ 
                            if(in_array($iou_batch_row['approval_status'], array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER, AS_APPROVED))){
                                $emp_outstanding += $iou_total_payment;
                            }
                        }
                        $total_batch_payment += $iou_total_payment;
                        $emp_iou_payments += $iou_total_payment;
                    
                        
                        $db_num_rows -= 1;
                        
                            $emp_batch_pymnts += $total_batch_payment;
                            
                            if ($pre_iou_id != $iou_id) {
                                $emp_req_amount += $iou_amount;
                            }
                            
                            if ($db_num_rows == 0) {
                                amount_cell($total_batch_payment);
                                amount_cell($iou_batch_expenses_amount);
                                label_cell($approval_statuses[$iou_batch_row['approval_status']], 'align=center');

                                if($outstanding_calculation == 1){ 
                                    if ($iou_batch_row['approval_status'] == AS_APPROVED) {
                                        $emp_outstanding -= $iou_batch_expenses_amount;
                                    }
                                }else{ 
                                    if(in_array($iou_batch_row['approval_status'], array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER, AS_APPROVED))){
                                        $emp_outstanding -= $iou_batch_expenses_amount;
                                    }
                                }
//                                amount_cell($emp_outstanding);
                                $batch_outstanding = $total_batch_payment - $iou_batch_expenses_amount;
                                amount_cell($batch_outstanding);
                            } else {
                                label_cell(''); label_cell(''); label_cell(''); 
//                                amount_cell($emp_outstanding);
                                label_cell(''); 
                            }

                            if ($db_num_rows == 0) {
                                amount_cell($emp_outstanding);
                            } else {
                                label_cell('');
                            }
                            label_cell(get_yes_no_name($iou_batch_row['complete']), 'align=center');

                        
                    end_row();
                        
                    $i++;
                    
                    $pre_iou_id = $iou_id;
                    $pre_batch_no = $current_batch_no;
                }
        }
}

function display_petty_expenses($employee, $from_date, $to_date, &$emp_req_amount, &$emp_iou_payments, &$emp_batch_pymnts, &$emp_expences, 
                                &$emp_outstanding, $type, $display_completed, $float_id, $project_id, &$k, &$i, $th, $ob_amount, 
                                $outstanding_calculation) {
    
    global $approval_statuses, $systypes_array; 
     
        $batch_data = get_epco_petty_cash_batch_numbers($employee, $from_date, $to_date, false, $type, $display_completed, $float_id, $project_id);
            if (! (db_num_rows($batch_data) > 0)) {
                return;
            }
            
            start_row();
                label_cell($systypes_array[ST_PETTY_CASH].'s', 'colspan=16 align=center style=background-color:#0ec9ba');
            end_row();
            
        while ($pet_batch = db_fetch($batch_data)) {
        $iou_batch_no = $pet_batch['batch_no'];
            
            $pet_data = get_epco_petty_cash_batch_payment_details($iou_batch_no);
                while ($pet_header = db_fetch($pet_data)) {
                    if ($i >= 15) {
                        table_header($th);
                        
                        $i = 0;
                    }
                    
                    $current_batch_no = $pet_header['batch_no'];
                    $petty_cash_amount = $pet_header['amount'];
                    
                    alt_table_row_color($k);
                        label_cell(sql2date($pet_header['tran_date']), 'align=center');
                        label_cell($pet_header['float_name']);
                        label_cell($pet_header['project_name']);
                        label_cell(get_petty_cash_trans_view_str(ST_PETTY_CASH, $pet_header['id']), 'align=center');
                        label_cell(get_petty_cash_trans_view_str(ST_I_OWE_YOU_BATCH, $current_batch_no), 'align=center');
                        amount_cell($petty_cash_amount);

                        if ($pet_header['type'] > 0) {
                            label_cell($systypes_array[ST_PETTY_CASH_PAYMENT]);
                        } else {
                            label_cell('');
                        }

                        if ($pet_header['trans_no'] > 0) {
                            label_cell(get_petty_cash_trans_view_str(ST_PETTY_CASH_PAYMENT, $pet_header['trans_no']), 'align=center');
                        } else {
                            label_cell('');
                        }
                        label_cell(sql2date($pet_header['pay_date']), 'align=center');

                        $payment = ($pet_header['pay_amount']);
                        amount_cell($payment);
                    
                        $emp_req_amount += $petty_cash_amount;

                        $emp_iou_payments += $payment;
                        $emp_batch_pymnts += $payment;                    
                    
                        $batch_expences = $pet_header['batch_total']; 
                        $emp_expences += $batch_expences; 
                        amount_cell($payment);
                        amount_cell($batch_expences);
                        label_cell($approval_statuses[$pet_header['approval_status']], 'align=center');

                        if($outstanding_calculation == 1){ 
                            if ($pet_header['approval_status'] == AS_APPROVED) {
                                $outstanding_amount = $payment - $batch_expences;     
                            }else{
                                $outstanding_amount = $payment;
                            }
                        }else{ 
                            if(in_array($pet_header['approval_status'], array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER, AS_APPROVED))){
                                $outstanding_amount = $payment - $batch_expences;
                            }else{
                                $outstanding_amount = $payment;
                            }
                        }
                                
                        amount_cell($outstanding_amount);
                        amount_cell($outstanding_amount);
                        label_cell(get_yes_no_name($pet_header['complete']), 'align=center');
                        
                        $emp_outstanding += $outstanding_amount; 
                    
                    end_row();
                        
                    $i++;
                }
                
        }
}

function display_employee_petty_cash_outstanding($company_code, $employee_code, $batch_complete_filter, $from_date_, $to_date_, 
                                                 $outstanding_calculation, $float_id, $project_id) {
    $from_date = date2sql($from_date_);
    $to_date = date2sql($to_date_);
    
    
    if(!(strlen($employee_code) > 2)){
        display_error("Please select an employee.");
        return;
    }
    
    start_table(TABLESTYLE, "width=99%");

        $th = array(_("IOU Date"), _("Float"), _("Project"), _("IOU. No"), _("Batch No"), _("Req. Amount"), _("Pay. Type"), _("Pay. No"), _("Pay. Date"), 
                    _("Payments"), _("Batch Payments"), _("Expences"), _("Batch Status"), _("Outstaning"), _("Balance"), _("Completed"));

        table_header($th);
    
        $k = $i = 0;
        $display_completed = $batch_complete_filter;
        $iou_type = get_listvalue_by_code('PETTY_CASH_TYPES', 'I_OWE_YOU');
        $pet_type = get_listvalue_by_code('PETTY_CASH_TYPES', 'PETTY_CASH');

        $opening_balance_date_ = add_days($from_date_, -1);
        $opening_balance = get_emp_petty_cash_ob($employee_code, $display_completed, INFINITE_START_DATE, date2sql($opening_balance_date_), 
                                                 $iou_type['id'], $pet_type['id'], $outstanding_calculation, $float_id, $project_id);
    
        start_row();
//            label_cell(text_bold("Opening Balance"), 'align=right colspan=13 style=background-color:#f3f7b0');
            label_cell(text_bold("Opening Balance"), 'align=right colspan=14');
            amount_cell($opening_balance, true);
            label_cell('');
        end_row();
        
        $emp_req_amount = $emp_iou_payments = $emp_outstanding = $emp_batch_pymnts = $emp_expences = 0;
        
                                
        display_iou_batch_zero($employee_code ,$from_date, $to_date, $emp_req_amount, $emp_iou_payments, $emp_outstanding, $iou_type['id'], 
                               $display_completed, $float_id, $project_id, $k, $i, $th, $opening_balance);
        
                        
        display_iou_batch_not_zero($employee_code, $from_date, $to_date, $emp_req_amount, $emp_iou_payments, $emp_batch_pymnts, $emp_expences, 
                                   $emp_outstanding, $iou_type['id'], $display_completed, $float_id, $project_id, $k, $i, $th, $opening_balance, 
                                   $outstanding_calculation);              
                        
                        

        display_petty_expenses($employee_code, $from_date, $to_date, $emp_req_amount, $emp_iou_payments, $emp_batch_pymnts, $emp_expences, 
                               $emp_outstanding, $pet_type['id'], $display_completed, $float_id, $project_id, $k, $i, $th, $opening_balance, 
                               $outstanding_calculation);
                        
                        
            start_row();
                label_cell(text_bold("Totals"), 'align=right colspan=5');
                amount_cell($emp_req_amount, true);
                label_cell('', 'colspan=3');
                amount_cell($emp_iou_payments, true);
                label_cell('');
                amount_cell($emp_expences, true); 
                label_cell('', 'colspan=2');
                amount_cell($emp_outstanding, true); 
//                amount_cell($emp_outstanding + $total_balance, true); 
                label_cell('');    
            end_row();                

            start_row();
                label_cell(text_bold("Closing Balance"), 'align=right colspan=14');
                amount_cell($emp_outstanding + $opening_balance, true);
                label_cell('');   
            end_row();

    end_table(1);
}