public function deleteSoftwareType($id)
 {
     if (Session::has('username') && Session::get('user_type') == "Root") {
         if (!is_numeric($id) || !SoftwareType::find($id)) {
             return Redirect::to("settings/assets/softwaretypes");
         } else {
             if (count(SoftwareType::find($id)->softwareassets) > 0) {
                 return Redirect::to("settings/assets/softwaretypes");
             }
         }
         $softwareType = SoftwareType::find($id);
         $desc = "(" . Session::get('user_type') . ") " . "<strong>" . Session::get('username') . "</strong> has deleted software type <strong>" . $softwareType->software_type . "</strong>.";
         //Log the changes made
         $newLog = new UserLog();
         $newLog->description = $desc;
         $newLog->user_id = Session::get('user_id');
         $newLog->type = "System";
         $newLog->save();
         $softwareType->delete();
         return Redirect::to("settings/assets/softwaretypes");
     } else {
         return Redirect::to("/");
     }
 }
 public function submitAssetUpdate()
 {
     if (Session::has('username') && (Session::get('user_type') == "Root" || Session::get('user_type') == "Admin")) {
         $input = Input::all();
         $software = Software::find($input["id"]);
         if (!$software) {
             return Redirect::to("assets/software");
         }
         if (Session::get("user_type") != "Root" && $software->status == "Lost") {
             return Redirect::to("assets/software/update/" . $software->id);
         }
         if ($input["action"] == "") {
             return Redirect::to('assets/software/update/' . $input["id"])->with('message', "Please select an action to commit.");
         } else {
             if ($input["action"] == "transfer") {
                 return Redirect::to("assets/software/transferasset/" . $input["id"]);
             } else {
                 if ($input["action"] == "lost") {
                     return Redirect::to("assets/software/lostasset/" . $input["id"]);
                 }
             }
         }
         if (Session::has("secure")) {
             $product_key = trim($input["product_key"] != null) ? trim($input["product_key"]) : null;
         }
         $warranty_start = $input["warranty_start"] != null ? $input["warranty_start"] : "1994-04-16";
         $notes = Input::get("notes") != null ? trim(Input::get("notes")) : "";
         $validator = Validator::make(array("software asset tag" => $input["asset_tag"], "laptop" => $input["serial_number"], "software type" => $input["software_type"], "warranty start date" => $input["warranty_start"], "warranty end date" => $input["warranty_end"], "status" => $input["asset_status"]), array("software asset tag" => "required", "laptop" => "exists:tbl_assets,serial_number", "software type" => "required|exists:tbl_software_types,id", "warranty start date" => "required_with:warranty end date|date:Y-m-d", "warranty end date" => "date:Y-m-d|after:" . $warranty_start, "status" => "required|in:Available,PWU,Retired,Test Case"));
         if ($validator->fails()) {
             Input::flash();
             return Redirect::to('assets/software/update/' . $input["id"])->with('message', $validator->messages()->first());
         } else {
             if ($software->asset_tag != $input["asset_tag"] && (Software::where("asset_tag", "=", $input["asset_tag"])->first() || Asset::where("asset_tag", "=", $input["asset_tag"])->first())) {
                 Input::flash();
                 return Redirect::to('assets/software/update/' . $input["id"])->with('message', "Asset tag already exists in the database. This field should be unique.");
             } else {
                 if (Session::has("secure") && Session::get("user_type") == "Root" && empty(trim($input["product_key"]))) {
                     Input::flash();
                     return Redirect::to('assets/software/update/' . $input["id"])->with('message', "Product key is required.");
                 } else {
                     if (!empty(trim($input["serial_number"])) && !Asset::where("serial_number", "=", $input["serial_number"])->whereHas("classification", function ($q) {
                         $q->where("name", "=", "Laptops");
                     })->first()) {
                         Input::flash();
                         return Redirect::to('assets/software/update/' . $input["id"])->with('message', "Cannot assign software asset to non-laptop assets. Please check the serial number and try again.");
                     } else {
                         //These variables are used to track if anything has been changed.
                         $isChanged = false;
                         //Boolean variable that checks if anything has been changed. Changes to true when a change has been detected.
                         $changes = array();
                         //Collects the names of the columns that have been changed.
                         $index = 0;
                         //Provides the index number of $changes array.
                         if ($input["asset_tag"] != $software->asset_tag) {
                             // 					$oldAssetTag = !empty($software->asset_tag) ? $software->asset_tag : "none";
                             // 					$newAssetTag = !empty(trim($input["asset_tag"])) ? trim($input["asset_tag"]) : "none";
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) asset tag (from <strong>" . $software->asset_tag . "</strong> to <strong>" . trim($input["asset_tag"]) . "</strong>)<br/>";
                             $index += 1;
                         }
                         if (Session::has("secure") && Session::get("user_type") == "Root" && $input["product_key"] != $software->product_key) {
                             $isChanged = true;
                             //$changes[$index] = 1+$index.".) product key (from <strong>".$software->product_key."</strong> to <strong>".$input["product_key"]."</strong>)<br/>";
                             $changes[$index] = 1 + $index . ".) product key (changes have been hidden).<br/>";
                             $index += 1;
                         }
                         if ($input["serial_number"] != $software->assigned_to_serial_number) {
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) laptop assignment (from <strong>" . $software->assigned_to_serial_number . "</strong> to <strong>" . $input["serial_number"] . "</strong>)<br/>";
                             $index += 1;
                         }
                         if ($input["location"] != $software->location) {
                             $newLocation = $input["location"];
                             $newLocation = empty(trim($newLocation)) ? "none" : $newLocation;
                             $oldLocation = empty($software->location) ? "none" : $software->location;
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) location (from <strong>" . $oldLocation . "</strong> to <strong>" . $newLocation . "</strong>)<br/>";
                             $index += 1;
                         }
                         if ($input["software_type"] != $software->software_type_id) {
                             $newSoftwareType = SoftwareType::find($input["software_type"]);
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) software type (from <strong>" . $software->type->software_type . "</strong> to <strong>" . $newSoftwareType->software_type . "</strong>)<br/>";
                             $index += 1;
                         }
                         if ($input["warranty_start"] != $software->warranty_start) {
                             $oldWarrantyStart = !empty($software->warranty_start) ? $software->warranty_start : "none";
                             $newWarrantyStart = !empty(trim($input["warranty_start"])) ? trim($input["warranty_start"]) : "none";
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) warranty start date (from <strong>" . $oldWarrantyStart . "</strong> to <strong>" . $newWarrantyStart . "</strong>)<br/>";
                             $index += 1;
                         }
                         if ($input["warranty_end"] != $software->warranty_end) {
                             $oldWarrantyEnd = !empty($software->warranty_end) ? $software->warranty_end : "none";
                             $newWarrantyEnd = !empty(trim($input["warranty_end"])) ? trim($input["warranty_end"]) : "none";
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) warranty end date (from <strong>" . $oldWarrantyEnd . "</strong> to <strong>" . $newWarrantyEnd . "</strong>)<br/>";
                             $index += 1;
                         }
                         if ($input["asset_status"] != $software->status) {
                             $isChanged = true;
                             $changes[$index] = 1 + $index . ".) software asset status (from <strong>" . $software->status . "</strong> to <strong>" . $input["asset_status"] . "</strong>)<br/>";
                             $index += 1;
                         }
                         if ($notes != $software->notes) {
                             $isChanged = true;
                             $oldNotes = !empty($software->notes) ? $software->notes : "none";
                             $newNotes = !empty($software) ? $notes : "none";
                             $changes[$index] = 1 + $index . ".) notes (from <strong>" . $oldNotes . "</strong> to <strong>" . $newNotes . "</strong>)<br/>";
                             $index += 1;
                         }
                         if (!$isChanged) {
                             Input::flash();
                             return Redirect::to('assets/software/update/' . $software->id)->with('info', "Nothing has changed. </3");
                         } else {
                             //Save updates
                             if (Session::has("secure") && Session::get("user_type") == "Root") {
                                 $software->asset_tag = $input["asset_tag"];
                                 $software->product_key = trim($input["product_key"]);
                                 $software->assigned_to_serial_number = trim($input["serial_number"]) != null ? trim($input["serial_number"]) : null;
                                 $software->location = !empty(trim($input["location"])) ? trim($input["location"]) : null;
                                 $software->software_type_id = $input["software_type"];
                                 $software->warranty_start = trim($input["warranty_start"]) != null ? trim($input["warranty_start"]) : null;
                                 $software->warranty_end = trim($input["warranty_end"]) ? trim($input["warranty_end"]) : null;
                                 $software->status = $input["asset_status"];
                                 $software->notes = !empty($notes) ? $notes : null;
                                 $software->save();
                             } else {
                                 $software->asset_tag = $input["asset_tag"];
                                 $software->assigned_to_serial_number = trim($input["serial_number"]) != null ? trim($input["serial_number"]) : null;
                                 $software->location = !empty(trim($input["location"])) ? trim($input["location"]) : null;
                                 $software->software_type_id = $input["software_type"];
                                 $software->warranty_start = trim($input["warranty_start"]) != null ? trim($input["warranty_start"]) : null;
                                 $software->warranty_end = trim($input["warranty_end"]) ? trim($input["warranty_end"]) : null;
                                 $software->status = $input["asset_status"];
                                 $software->notes = !empty($notes) ? $notes : null;
                                 $software->save();
                             }
                             $changesMade = implode($changes, "");
                             $desc = "(" . Session::get('user_type') . ") " . "<strong>" . Session::get('username') . "</strong> has updated software asset <strong>" . $software->asset_tag . "'s</strong> information. These are the fields that have been modified:<br/>" . $changesMade;
                             //Log the changes made
                             $newLog = new UserLog();
                             $newLog->description = $desc;
                             $newLog->user_id = Session::get('user_id');
                             $newLog->type = "System";
                             $newLog->save();
                             //Parallel logging to asset logs
                             $softwareLog = new SoftwareLog();
                             $softwareLog->software_id = $software->id;
                             $softwareLog->user_id = Session::get("user_id");
                             $softwareLog->description = $desc;
                             $softwareLog->transaction = "Updates";
                             $softwareLog->save();
                             return Redirect::to('assets/software/update/' . $software->id)->with('success', "You have successfully updated the software asset information.");
                         }
                     }
                 }
             }
         }
     } else {
         return Redirect::to("/");
     }
 }