<?php

function add_job_order_repair($jo_id, $row_state, $description) {
    $fun_args = func_get_args();
    $args = (object)array_combine(array('jo_id', 'row_state', 'description'), $fun_args);

    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');

        $sql = "INSERT INTO ".TB_PREF."job_order_repairs   
                      ( jo_id, description, 
                        created_by, created_on )         
                VALUES( ".db_escape($jo_id).", ".db_escape($description).", 
                        ".db_escape($created_by).", ".db_escape($created_on).")";

            db_query($sql, "add_job_order_repair");
            $id = db_insert_id();

            if (! ($id > 0)) {
                cancel_transaction();

                return array('success' => false, 'message' => 'Could not add job card repair.');
            }

            $hd_af_row = update_job_order_row_state($jo_id, $row_state);
                if ($hd_af_row != 1) {
                    cancel_transaction();

                    return array('success' => false, 'message' => 'Another user has updated job card. Could not update job card row state.');
                }

	return array('success' => true, 'message' => "", 'id' => $id);
}

function update_job_order_repair($jo_id, $id, $row_state, $description) {
    $fun_args = func_get_args();
    $args = (object)array_combine(array('jo_id', 'id', 'row_state', 'description'), $fun_args);

    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."job_order_repairs  
                SET description = ".db_escape($description).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)." 
                WHERE id = ".db_escape($id);

            db_query($sql, "update_job_order_repair");
            $num_af_row = db_num_affected_rows();

            if ($num_af_row != 1) {
                cancel_transaction();

                return array('success' => false, 'message' => "Could not update job card repair record #$id.");
            }

            $hd_af_row = update_job_order_row_state($jo_id, $row_state);
                if ($hd_af_row != 1) {
                    cancel_transaction();

                    return array('success' => false, 'message' => 'Another user has updated job card. Could not update job card row state.');
                }

        return array('success' => true, 'message' => "");
}

function get_job_order_repairs_details($jo_id) {
    $sql = "SELECT rp.*, cr.real_name created_by_name, ud.real_name updated_by_name   
            FROM ".TB_PREF."job_order_repairs rp 
                LEFT JOIN ".TB_PREF."users cr 
                    ON rp.created_by = cr.id 
                LEFT JOIN ".TB_PREF."users ud 
                    ON rp.updated_by = ud.id 
            WHERE rp.jo_id = ".db_escape($jo_id);

	return db_query($sql, "get_job_order_repairs_details");
}

function get_job_order_repair_record($id) {
    $sql = "SELECT *
            FROM ".TB_PREF."job_order_repairs  
            WHERE id = ".db_escape($id);

	$result = db_query($sql, "get_job_order_repair_record");
        return db_fetch($result);
}

function delete_job_order_repair($id) {
    $sql = "DELETE 
            FROM ".TB_PREF."job_order_repairs  
            WHERE id = ".db_escape($id);

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

?>