<?php

function display_salary_advance_details($sad_trans_no) {
    global $payroll_approval_statuses;
    
    $row = get_salary_advance_details($sad_trans_no);
        start_outer_table(TABLESTYLE2, "width=60%");

            table_section(1, "50%");
                label_row(_("Salary Advance #:"), get_payroll_trans_view_str(ST_SALARY_ADVANCE, $row['id']), "class='tableheader2'");
                label_row(_("Employee:"), get_employee_formatted_long_name($row['employee_code']), "class='tableheader2'");
                label_row(_("Type:"), get_listvalue_name_by_id($row['type_id']), "class='tableheader2'");
                label_row(_("Due Date:"), sql2date($row['due_date']), "class='tableheader2'");

            table_section(2, "50%");
                label_row(_("Salary Advance:"), number_format($row['salary_advance'], 2), "class='tableheader2'");
                label_row(_("Approval Status:"), $payroll_approval_statuses[$row['status_code']], "class='tableheader2'");
                label_row(_("Comment:"), nl2br($row['comments']), "class='tableheader2'");
                label_row(_("Float:"), petty_cash_floats_list('float_id', null, ''), "class='tableheader2'");

        end_outer_table(2);
}

function can_save_salary_advance_payment($sad_trans_no) {
    $row = get_salary_advance_details($sad_trans_no);
    
        if (! (isset($row['id']) && $row['id'] > 0)) {
            return array('success' => false, 'message' => "Could not find Salary advance record #$sad_trans_no.");
        } elseif ($row['status_code'] == "D") {
            return array('success' => false, 'message' => "Salary advance #$sad_trans_no has been already processed.");
        } elseif ($row['status_code'] != "A") {
            return array('success' => false, 'message' => "Salary advance #$sad_trans_no has not been approved yet.");
        }
        
        if (! ($_POST['float_id'] > 0)) {
            return array('success' => false, 'message' => "Please select a float.");
        }
        
            return array('success' => true, 'message' => "", 'sal_adv_row' => $row);
}

function save_salary_advance_payment($sad_trans_no) {
    $cn_sv_msg = can_save_salary_advance_payment($sad_trans_no);
        if ($cn_sv_msg['success'] === false) {
            return $cn_sv_msg;
        }
        
        $sal_adv_row = $cn_sv_msg['sal_adv_row'];
        $row_state = $sal_adv_row['row_state'];
        $trans_type = ST_FLOAT_PAYMENT;
        $float_id = $_POST['float_id'];
    
        begin_transaction();
        
            $msg = add_salary_advance_payment($trans_type, $sad_trans_no, $float_id, $sal_adv_row);
                if ($msg['success'] !== true) {
                    return $msg;
                }
            
            $af_row = update_salary_advance_approval_status($sad_trans_no, 'D', array("'A'"), $row_state);
                if ($af_row != 1) {
                    return array('success' => false, 'message' => "Could not update.");
                }
        
        commit_transaction();
        
            meta_forward($_SERVER['PHP_SELF'], "ApprovedID=".$msg["trans_no"]);
}

function add_salary_advance_payment($trans_type, $source_trans_no, $float_id, $sal_adv_row) {   
    global $Refs, $systypes_array; 
     
    $amount = $sal_adv_row['salary_advance'] * -1;
    $tran_date_ = sql2date($sal_adv_row['due_date']);
    $is_return = 1;
    
    $reference = get_next_reference_for_auto_generation($trans_type);
    $trans_no = get_next_trans_no($trans_type);
    
        $rec_id = add_petty_cash_float_adjustment($reference, $is_return, $float_id, $tran_date_, $amount, $sal_adv_row['comments'], $trans_type, $trans_no, 
                                                  ST_SALARY_ADVANCE,$source_trans_no);
            if (! ($rec_id > 0)) {  
                cancel_transaction();
                
                return array("success" => false, "message" => $systypes_array[$trans_type]." could not be added."); 
            }
    
        $rec_id_2 = add_to_petty_cash_float($trans_type, $trans_no, $float_id, $amount, $tran_date_);        
            if (! ($rec_id_2 > 0)) {  
                cancel_transaction();
                
                return array("success" => false, "message" => $systypes_array[$trans_type]." could not be added."); 
            }
        
        $af_row = update_petty_cash_float_adjustment_approved($trans_type, $trans_no); 
            if ($af_row != 1) {
                cancel_transaction();
                
                return array("success"=>false, "message" => "Could not approved ".$systypes_array[$trans_type].".");
            }

        $Refs->save($trans_type, $trans_no, $reference); 
        
        return array("success" => true, "trans_no" => $trans_no, "message" => '');
}

function view_salary_advance_payment($source_type, $source_trans_no) {
    global $systypes_array, $document_approval_statuses;
    
    start_table(TABLESTYLE, "width=75%");

        $th = array(_("Trans #"), _("Type"), _("Date"), _("Float"), _('Amount'), _('Approval Status'));  
        table_header($th);

        $k = $amount_total = 0; 
            $result = get_petty_cash_float_adjustments_by_source($source_type, $source_trans_no);    
                while ($row = db_fetch($result)) {
                    alt_table_row_color($k);
                        label_cell(get_petty_cash_trans_view_str($row['type'], $row['trans_no']), 'align=center');
                        label_cell($systypes_array[$row['type']]);
                        label_cell(sql2date($row["tran_date"]), 'align=center');
                        label_cell($row["float_name"]);
                        $amount = $row["amount"];
                        label_cell(number_format($amount, 2), 'align=right');                         
                        label_cell($document_approval_statuses[$row['approval_status']], 'align=center');
                    end_row();  
                    
                    $amount_total += $amount;
                }
                
                start_row(); 
                    label_cell(text_bold("Total"), 'align=right colspan=4');
                    amount_cell($amount_total, true); 
                    label_cell("");
                end_row();

    end_table(2);
}

?>