<?php

function write_raw_material_issue_header($type, $trans_no, $reference, $project_id, $company_code, $location, $comments, $date, $source_id=0) {
    global $Refs, $systypes_array;
     
        if ($trans_no > 0) {
            $af_row = update_raw_material_issue_header($type, $trans_no, $reference, $company_code, $location, $comments, $date);
                $in_up = 'updated';
        } else {
            $trans_no = get_next_trans_no($type);
            $af_row = insert_raw_material_issue_header($type, $trans_no, $reference, $project_id, $company_code, $location, $comments, $date, $source_id);
                $in_up = 'saved';
        }
    
        if (! ($af_row > 0)) {
            return array('success' => false, 'message' => "Could not be $in_up ".$systypes_array[$type].".");
        }

        $Refs->save($type, $trans_no, $reference);

        return array('success' => true, 'trans_no' => $trans_no, 'message' => $systypes_array[$type]." has been $in_up.");
}

function write_raw_material_return_header_by_type_trans_no($type, $dti_trans_no, $reference, $project_id, $company_code, $location, $comments, $date) {
    global $Refs;
     
        $trans_no = get_next_trans_no($type);
        $inser_id = insert_raw_material_issue_header($type, $trans_no, $reference, $project_id, $company_code, $location, $comments, $date, $dti_trans_no);
            if (! ($inser_id > 0)) {
                return 0;
            }
    
        $Refs->save($type, $trans_no, $reference);

        return $trans_no;
}
     
function insert_raw_material_issue_header($type, $trans_no, $reference, $project_id, $company_code, $location, $comments, $date, $source_id=0) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."raw_material_issues
                      ( type, trans_no, reference, project_id, 
                        company_code, loc_code, comments, 
                        tran_date, created_by, created_on, source_id )         
                VALUES( ".db_escape($type).", ".db_escape($trans_no).", ".db_escape($reference).", ".db_escape($project_id).", 
                        ".db_escape($company_code).", ".db_escape($location).", ".db_escape($comments).", 
                        ".db_escape(date2sql($date)).", ".db_escape($created_by).", ".db_escape($created_on).", ".db_escape($source_id).")";

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

function update_raw_material_issue_header($type, $trans_no, $reference, $company_code, $location, $comments, $date) {   
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."raw_material_issues
                SET reference = ".db_escape($reference).",
                    loc_code = ".db_escape($location).",                          
                    tran_date = ".db_escape(date2sql($date)).",
                    comments = ".db_escape($comments).", 
                    company_code = ".db_escape($company_code).", 
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE type = ".db_escape($type)."    
                    AND trans_no = ".db_escape($trans_no);    

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

function update_raw_material_issue_header_by_type_trans_no($type, $trans_no, $reference, $company_code, $movement_type, $linked_field_type, $location, 
                                                         $comments, $date) {   
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."raw_material_issues
                SET reference = ".db_escape($reference).",
                    loc_code = ".db_escape($location).",                          
                    tran_date = ".db_escape(date2sql($date)).",
                    comments = ".db_escape($comments).", 
                    company_code = ".db_escape($company_code).", 
                    movement_type = ".db_escape($movement_type).", 
                    linked_field_type = ".db_escape($linked_field_type).", 
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE type = ".db_escape($type)."     
                    AND trans_no = ".db_escape($trans_no);    

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

function write_raw_material_issue_line($trans_no, $line_id, $type, $location, $stock_id, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                       $employee_code, $site_id, $line_comment, $task_id, $source_line_id=0) {
    if ($line_id > 0) {
        return update_raw_material_issue_line($line_id, $location, $stock_id, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                              $employee_code, $site_id, $line_comment, $task_id, $source_line_id);
    } else {
        return insert_raw_material_issue_line($trans_no, $type, $location, $stock_id, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                              $employee_code, $site_id, $line_comment, $task_id, $source_line_id);
    }
}

function update_raw_material_issue_line($line_id, $location, $stock_id, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, $employee_code, 
                                        $site_id, $line_comment, $task_id, $source_line_id=0) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."raw_material_issue_details
                SET loc_code = ".db_escape($location).",
                    stock_id = ".db_escape($stock_id).", 
                    tran_unit = ".db_escape($tran_unit).", 
                    tran_qty = ".db_escape($tran_qty).", 
                    qty = ".db_escape($qty).",
                    conversion_factor = ".db_escape($conversion_factor).",
                    standard_cost = ".db_escape($standard_cost).",
                    employee_code = ".db_escape($employee_code).",
                    site_id = ".db_escape($site_id).",
                    line_comment = ".db_escape($line_comment).",
                    task_id = ".db_escape($task_id).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on).",  
                    source_line_id = ".db_escape($source_line_id)." 
                WHERE id = ".db_escape($line_id);

            db_query($sql, 'update_raw_material_issue_line');
            return db_num_affected_rows();
}
        
function insert_raw_material_issue_line($trans_no, $type, $location, $stock_id, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                        $employee_code, $site_id, $line_comment, $task_id, $source_line_id=0) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."raw_material_issue_details
                    (   trans_no, type, loc_code, stock_id, 
                        tran_unit, tran_qty, qty, conversion_factor, 
                        standard_cost, employee_code, task_id, site_id, 
                        line_comment, created_by, created_on, source_line_id)        
                VALUE ( ".db_escape($trans_no).", ".db_escape($type).", ".db_escape($location).", ".db_escape($stock_id).", 
                        ".db_escape($tran_unit).", ".db_escape($tran_qty).", ".db_escape($qty).", ".db_escape($conversion_factor).", 
                        ".db_escape($standard_cost).", ".db_escape($employee_code).", ".db_escape($task_id).", ".db_escape($site_id).", 
                        ".db_escape($line_comment).", ".db_escape($created_by).", ".db_escape($created_on).", ".db_escape($source_line_id)." )";      

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

function get_raw_material_issue_header_record($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."raw_material_issues    
            WHERE id = ".db_escape($id);
    
        $result = db_query($sql, "get_raw_material_issue_header_record");
        return db_fetch($result);
}

function get_raw_material_issue_header($id) {
    $sql = "SELECT sa.*,
                l.location_name,
                pm.description project_description,
                u.real_name requestor_name 
               
            FROM ".TB_PREF."raw_material_issues sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.id = ".db_escape($id);
    
        $result = db_query($sql, "get_raw_material_issue_header");
        return db_fetch($result);
}

function get_raw_material_issue_header_type_trans_no($type, $trans_no) {
    $sql = "SELECT sa.*,
                l.location_name,
                pm.description project_description,
                u.real_name requestor_name 
               
            FROM ".TB_PREF."raw_material_issues sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.type = ".db_escape($type)." 
                AND sa.trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, "get_raw_material_issue_header_type_trans_no");
        return db_fetch($result);
}

function get_raw_material_return_header_with_issue_header($iss_trans_no) {
    $sql = "SELECT iss.*,
                l.location_name issue_location_name,
                pm.description project_description,
                u.real_name requestor_name, 
                l2.location_name return_location_name,
                u2.real_name return_user_name,
                rt.comments return_comments,
                rt.tran_date return_tran_date, 
                rt.approval_status return_approval_status, 
                rt.loc_code return_loc_code 
            FROM ".TB_PREF."raw_material_issues iss 
                JOIN ".TB_PREF."projects_master pm 
                    ON iss.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON iss.loc_code = l.loc_code 
                JOIN ".TB_PREF."users u 
                    ON iss.created_by = u.id 
                LEFT JOIN ".TB_PREF."raw_material_issues rt  
                    ON iss.trans_no = rt.source_id  
                        AND rt.type = ".db_escape(ST_RAW_MATERIAL_RETURN)." 
                LEFT JOIN ".TB_PREF."locations l2  
                    ON rt.loc_code = l2.loc_code 
                LEFT JOIN ".TB_PREF."users u2  
                    ON rt.created_by = u2.id 
            WHERE iss.type = ".db_escape(ST_RAW_MATERIAL_ISSUE)." 
                AND iss.trans_no = ".db_escape($iss_trans_no);
    
        $result = db_query($sql, "get_raw_material_return_header_with_issue_header");
        return db_fetch($result);
}

function get_raw_material_return_header_with_issue_header_record_by_return_trans_no($ret_trans_no) {
    $sql = "SELECT rt.*, 
                    iss.tran_date iss_tran_date 
            FROM ".TB_PREF."raw_material_issues rt  
                JOIN ".TB_PREF."raw_material_issues iss  
                    ON iss.trans_no = rt.source_id  
            WHERE iss.type = ".db_escape(ST_RAW_MATERIAL_ISSUE)." 
                AND rt.type = ".db_escape(ST_RAW_MATERIAL_RETURN)." 
                AND rt.trans_no = ".db_escape($ret_trans_no);
    
        $result = db_query($sql, "get_raw_material_return_header_with_issue_header_record_by_return_trans_no");
        return db_fetch($result);
}

function get_raw_material_issue_header_record_type_trans_no($type, $trans_no) {
    $sql = "SELECT * 
            FROM ".TB_PREF."raw_material_issues    
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, "get_raw_material_issue_header_record_type_trans_no");
        return db_fetch($result);
}

function get_raw_material_return_headers_by_issue_trans_no($req_trans_no, $order_by='sa.tran_date, sa.id') {
    $sql = "SELECT sa.*,
                l.location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."raw_material_issues sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code 
                LEFT JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.source_id = ".db_escape($req_trans_no)." 
                AND sa.type = ".db_escape(ST_RAW_MATERIAL_RETURN);
    
        if (strlen($order_by) > 0) {
            $sql .= " ORDER BY $order_by ";
        }
    
        return db_query($sql, "get_raw_material_return_headers_by_issue_trans_no");
}
 
function get_tool_request_detail_line_id($selected_id) {
    $sql = "SELECT ard.*,
                    am.description stock_description,
                    am.stock_id,
                    un.name unit_name,
                    ps.description site_description 
                    pt.description task_description 
            FROM ".TB_PREF."raw_material_issue_details ard 
                JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr  
                JOIN ".TB_PREF."locations l 
                    ON ard.loc_code = l.loc_code 
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE ard.id = ".db_escape($selected_id);
    
        $result = db_query($sql, 'get_tool_request_detail_line_id');
        return db_fetch($result);
}

function delete_raw_material_issue_details_record($selected_id) {
    $sql = "DELETE 
            FROM ".TB_PREF."raw_material_issue_details 
            WHERE id = ".db_escape($selected_id);
    
        db_query($sql, 'delete_raw_material_issue_details_record');
        return db_num_affected_rows();
}

function get_raw_material_issue_details($trans_no, $type, $order_by='ard.id') {
    $sql = "SELECT ard.*,
                am.description stock_description,
                am.stock_id,
                un.name unit_name,
                ps.description site_description, 
                pt.description task_description 
            FROM ".TB_PREF."raw_material_issue_details ard 
                JOIN ".TB_PREF."locations l 
                    ON ard.loc_code = l.loc_code 
                JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr  
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE ard.trans_no = ".db_escape($trans_no)." 
                AND ard.type = ".db_escape($type);
                    
        if (strlen($order_by) > 0) {
            $sql .= " ORDER BY $order_by ";
        }
        
        return db_query($sql, "get_raw_material_issue_details");
}

function get_raw_material_issue_and_return_details($dti_trans_no, $dtr_trans_no, $order_by='ard.id') {
    $sql = "SELECT ard.*,
                am.description stock_description,
                am.stock_id,
                un.name unit_name,
                ps.description site_description, 
                pt.description task_description,
                rtd.id rtn_id,
                rtd.trans_no rtn_trans_no,
                rtd.tran_unit rtn_tran_unit,
                rtd.tran_qty rtn_tran_qty,
                rtd.qty rtn_qty,
                rtd.site_id rtn_site_id,
                rtd.line_comment rtn_line_comment,
                un2.name rtn_unit_name,
                l2.location_name rtn_location_name 
            FROM ".TB_PREF."raw_material_issue_details ard 
                JOIN ".TB_PREF."locations l 
                    ON ard.loc_code = l.loc_code 
                JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr 
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
                    
                LEFT JOIN ".TB_PREF."raw_material_issue_details rtd  
                    ON ard.id = rtd.source_line_id   
                        AND rtd.type = ".db_escape(ST_RAW_MATERIAL_RETURN)." 
                        AND rtd.trans_no = ".db_escape($dtr_trans_no)." 
                LEFT JOIN ".TB_PREF."item_units un2   
                    ON rtd.tran_unit = un2.abbr 
                LEFT JOIN ".TB_PREF."locations l2  
                    ON rtd.loc_code = l2.loc_code 
            WHERE ard.trans_no = ".db_escape($dti_trans_no)." 
                AND ard.type = ".db_escape(ST_RAW_MATERIAL_ISSUE);
    
        if (strlen($order_by) > 0) {
            $sql .= " ORDER BY $order_by ";
        }
    
        return db_query($sql, "get_raw_material_issue_and_return_details");
}

function get_raw_material_issue_details_record($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."raw_material_issue_details  
            WHERE id = ".db_escape($id);
    
        $result = db_query($sql, "get_raw_material_issue_details_record");
        return db_fetch($result);
}

function get_raw_material_issue_details_lines_count($trans_no, $type) {
    $sql = "SELECT COUNT(id) lines_count 
            FROM ".TB_PREF."raw_material_issue_details 
            WHERE trans_no = ".db_escape($trans_no)." 
                AND type = ".db_escape($type);
    
        $result = db_query($sql, 'get_raw_material_issue_details_lines_count');
        return db_result_value($result, 'lines_count');
}

function update_tool_request_status($trans_no, $new_status_id, $row_state)
{
    $sql = "UPDATE ".TB_PREF."raw_material_issues
            SET approval_status = ".db_escape($new_status_id).",
                row_state = ". db_new_row_state($row_state)." 
            WHERE id=".db_escape($trans_no)." AND row_state = ".db_escape($row_state);
            
    db_query($sql, "update_tool_request_fully_returned");
    
    return db_num_affected_rows();
}

function get_sql_for_raw_material_issues_inquiry($trans_no, $reference, $project_id, $from_date, $to_date, $loc_code) {
    $sql = "SELECT  sa.id,
                    sa.trans_no,
                    sa.reference,
                    sa.type,
                    l.location_name,
                    sa.tran_date,
                    pm.description project_description,
                    sa.comments,
                    sa.approval_status 
                    
            FROM ".TB_PREF."raw_material_issues sa  
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code        
            WHERE sa.type = ".db_escape(ST_RAW_MATERIAL_ISSUE);
    
        if ($trans_no > 0){
            $sql .= " AND sa.trans_no = ".db_escape($trans_no);
        } else {
        
            if (strlen($reference) > 0) {
                $sql .= " AND sa.reference = ".db_escape($reference);
            }

            if ($project_id > 0) {
                $sql .= " AND sa.project_id = ".db_escape($project_id);
            }

            $sql .= " AND sa.tran_date >= ".db_escape(date2sql($from_date))." AND sa.tran_date <= ".db_escape(date2sql($to_date)); 

            if ($loc_code != '') {        
                $sql .= " AND sa.loc_code = ".db_escape($loc_code);
            }
        }
    
        $sql .= " ORDER BY sa.tran_date, sa.id";
    
    return $sql;
}

function update_raw_material_issue_header_approval_status($trans_no, $type, $pre_status, $new_status) {
    $approved_by = $_SESSION['wa_current_user']->user;
    $approved_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."raw_material_issues 
                SET approval_status = ".db_escape($new_status).",
                    approved_by = ".db_escape($approved_by).", 
                    approved_on = ".db_escape($approved_on)." 
                WHERE trans_no = ".db_escape($trans_no)."   
                    AND type = ".db_escape($type)."   
                    AND approval_status = ".db_escape($pre_status);    

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

function update_raw_material_return_header_approval_status($trans_no, $pre_status, $new_status) {
    $approved_by = $_SESSION['wa_current_user']->user;
    $approved_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."raw_material_issues
                SET approval_status = ".db_escape($new_status).",
                    approved_by = ".db_escape($approved_by).", 
                    approved_on = ".db_escape($approved_on)." 
                WHERE trans_no = ".db_escape($trans_no)."   
                    AND approval_status = ".db_escape($pre_status);    

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

function get_raw_material_issue_line_returned_total($rmi_id, $not_rtn_id=0) {
    $sql = "SELECT SUM(qty) returned_total 
            FROM ".TB_PREF."raw_material_issue_details 
            WHERE source_line_id = ".db_escape($rmi_id)." 
                AND type = ".db_escape(ST_RAW_MATERIAL_RETURN);
    
        if ($not_rtn_id > 0) {
            $sql .= " AND id <> ".db_escape($not_rtn_id);
        }
    
        $result = db_query($sql, 'get_raw_material_issue_line_returned_total');
        return db_result_value($result, 'returned_total');
}

function update_raw_material_issue_line_returned_total($id) {
    $returned_total_ = get_raw_material_issue_line_returned_total($id);
    $returned_total = abs($returned_total_);
    
    $sql = "UPDATE ".TB_PREF."raw_material_issue_details 
            SET returned_quantity = ".db_escape($returned_total)." 
            WHERE id = ".db_escape($id);
    
        db_query($sql, 'update_raw_material_issue_line_returned_total');
        return db_num_affected_rows();
}

function get_sql_for_raw_material_returns_inquiry($trans_no, $reference, $project_id, $from_date, $to_date, $loc_code) {
    $sql = "SELECT  sa.id,
                    sa.trans_no,
                    sa.reference,
                    sa.type,
                    l.location_name,
                    sa.tran_date,
                    pm.description project_description,
                    sa.comments,
                    sa.approval_status 
            FROM ".TB_PREF."raw_material_issues sa  
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code        
            WHERE sa.type = ".db_escape(ST_RAW_MATERIAL_RETURN);
    
        if ($trans_no > 0){
            $sql .= " AND sa.trans_no = ".db_escape($trans_no);
        } else {
            if (strlen($reference) > 0) {
                $sql .= " AND sa.reference = ".db_escape($reference);
            }

            if($project_id > 0){
                $sql .= " AND sa.project_id = ".db_escape($project_id);
            }

            $sql .= " AND sa.tran_date >= ".db_escape(date2sql($from_date))." AND sa.tran_date <= ".db_escape(date2sql($to_date)); 

            if ($loc_code != '') {        
                $sql .= " AND sa.loc_code = ".db_escape($loc_code);
            }
        }
    
        $sql .= " ORDER BY sa.tran_date, sa.id";
    
    return $sql;
}

?>