<?php

function write_personal_material_requisition_header($type, &$trans_no, &$id, $reference, $project_id, $loc_code, $to_loc_code, $comments, $date, $department_id, $source_type=-1, 
                                                    $source_trans_no=0, &$row_state=0) {
    global $Refs;
     
        if ($id > 0) {
            $af_row = update_personal_material_requisition_header($id, $reference, $loc_code, $to_loc_code, $department_id, $comments, $date, $row_state);
                if (! ($af_row > 0)) {
                    return 0;
                }
            $row_state = db_new_row_state($row_state);  
        } else {
            $trans_no = get_next_trans_no($type);
            $id = insert_personal_material_requisition_header($type, $trans_no, $reference, $project_id, $loc_code, $to_loc_code, $department_id, $comments, $date, $source_type, 
                                                              $source_trans_no);
        }
    
        $Refs->save($type, $trans_no, $reference);

        if (! ($id > 0)) {
            return 0;
        }

        return $id;
}

function insert_personal_material_requisition_header($type, $trans_no, $reference, $project_id, $loc_code, $to_loc_code, $department_id, $comments, $date, $source_type=-1, 
                                                     $source_trans_no=0) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."job_material_requisitions
                      ( type, trans_no, reference, project_id, 
                        loc_code, to_loc_code, department_id, comments, tran_date, 
                        source_type, source_trans_no, created_by, created_on)         
                VALUES( ".db_escape($type).", ".db_escape($trans_no).", ".db_escape($reference).", ".db_escape($project_id).", 
                        ".db_escape($loc_code).", ".db_escape($to_loc_code).", ".db_escape($department_id).", ".db_escape($comments).", ".db_escape(date2sql($date)).", 
                        ".db_escape($source_type).", ".db_escape($source_trans_no).", ".db_escape($created_by).", ".db_escape($created_on).")";

            db_query($sql, 'insert_personal_material_requisition_header');
            return db_insert_id();        
}

function update_personal_material_requisition_header($id, $reference, $loc_code, $to_loc_code, $department_id, $comments, $date, $row_state=0) {   
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."job_material_requisitions
                SET reference = ".db_escape($reference).",
                    loc_code = ".db_escape($loc_code).",                          
                    to_loc_code = ".db_escape($to_loc_code).",                          
                    department_id = ".db_escape($department_id).",                          
                    tran_date = ".db_escape(date2sql($date)).",
                    comments = ".db_escape($comments).", 
                    row_state = ".db_new_row_state($row_state).", 
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE id = ".db_escape($id)."   
                    AND row_state = ".db_escape($row_state);  

            db_query($sql, "update_personal_material_requisition_header");
            return db_num_affected_rows();
}

function get_personal_material_requisition_line_remain_qty_array($source_base_qty, $source_line_id=0, $tran_qty=0, $conversion_factor=1, $line_id=0, $dec=4, $returned_qty=0) {
    
    $source_tran_qty = $source_base_qty / $conversion_factor;
            
        $total_transferred_req_qty_ = get_job_material_issue_lines_total_qty(ST_PERSONAL_MATERIAL_TRANSFER, $source_line_id, array(AS_APPROVED, AS_COMPLETED));
        $total_transferred_req_qty = abs($total_transferred_req_qty_);

        $ap_array_2 = array(AS_NEW, AS_WITH_THE_APPROVER, AS_AWAITING_APPROVAL, AS_BACK_TO_ORIGINATOR);
        $total_transferred_alloc_qty_ = get_job_material_issue_lines_total_qty(ST_PERSONAL_MATERIAL_TRANSFER, $source_line_id, $ap_array_2, $line_id);
        $total_transferred_alloc_qty = abs($total_transferred_alloc_qty_);
        
        $processed_qty = $total_transferred_req_qty + $total_transferred_alloc_qty - $returned_qty;
        
            $old_processed_tran_qty = $processed_qty / $conversion_factor;
            $old_rem_trn_qty = $source_tran_qty - $old_processed_tran_qty;

            $new_rem_trn_qty = $old_rem_trn_qty - $tran_qty;
                
            return array('old_rem_trn_qty' => number_format($old_rem_trn_qty, $dec), 'new_rem_trn_qty' => number_format($new_rem_trn_qty, $dec));
}

function check_all_personal_material_requisition_lines_fully_processed($type, $trans_no) {
    $sql = "SELECT qty, SUM(issue_req_alloc_qty + issue_req_qty - returned_quantity) total_qty 
            FROM ".TB_PREF."job_material_requisition_details 
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no)." 
            GROUP BY trans_id 
            HAVING total_qty < qty ";
            
        $result = db_query($sql, "check_all_personal_material_requisition_lines_fully_processed");
            if (db_num_rows($result) > 0) {
                return false;
            } 
            
        return true;
}

?>