public function submitNewAssetModel()
 {
     if (Session::has('username') && (Session::get('user_type') == "Root" || Session::get('user_type') == "Admin")) {
         $validator = Validator::make(array("model name" => Input::get("name"), "asset type" => Input::get("asset_class_id")), array("model name" => "required", "asset type" => "required|exists:tbl_asset_classifications,id"));
         if ($validator->fails()) {
             Input::flash();
             return Redirect::to('settings/assets/addassetmodel')->with('message', $validator->messages()->first());
         } else {
             if (Model::where("name", "=", trim(Input::get("name")))->where("classification_id", "=", Input::get("asset_class_id"))->first()) {
                 Input::flash();
                 return Redirect::to('settings/assets/addassetmodel')->with('message', "Asset model already exists for the same asset type.");
             } else {
                 $classification = AssetClassification::find(Input::get("asset_class_id"));
                 $desc = "(" . Session::get('user_type') . ") " . "<strong>" . Session::get('username') . "</strong> has added a new asset model: <strong>" . Input::get("name") . "</strong> for <strong>" . strtolower($classification->name) . "</strong>.";
                 $assetModel = new Model();
                 $assetModel->name = Input::get("name");
                 $assetModel->classification_id = Input::get("asset_class_id");
                 $assetModel->save();
                 //Log the changes made
                 $newLog = new UserLog();
                 $newLog->description = $desc;
                 $newLog->user_id = Session::get('user_id');
                 $newLog->type = "System";
                 $newLog->save();
                 return Redirect::to('settings/assets/addassetmodel')->with('success', "You have successfully added a new asset model for " . strtolower($classification->name) . ".");
             }
         }
     } else {
         return Redirect::to("/");
     }
 }
 public function submitAssetUpdate()
 {
     if (Session::has('username') && (Session::get('user_type') == "Root" || Session::get('user_type') == "Admin")) {
         $input = Input::all();
         $asset = Asset::where("id", "=", $input["id"])->whereHas("classification", function ($query) {
             $query->where("type", "=", "Network");
         })->first();
         if (!$asset) {
             return Redirect::to("assets/network");
         }
         if (Session::get("user_type") != "Root" && $asset->status == "Lost") {
             return Redirect::to("assets/network");
         }
         if ($input["action"] == "") {
             return Redirect::to('assets/network/update/' . $input["asset_key"] . "/" . $input["id"])->with('message', "Please select an action to commit.");
         } else {
             if ($input["action"] == "transfer") {
                 return Redirect::to("assets/network/transferasset/" . $input["id"]);
             } else {
                 if ($input["action"] == "lost") {
                     return Redirect::to("assets/network/lostasset/" . $input["id"]);
                 }
             }
         }
         $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("asset tag" => $input["asset_tag"], "serial_number" => $input["serial_number"], "model" => $input["model_id"], "warranty start date" => $input["warranty_start"], "warranty end date" => $input["warranty_end"], "asset type" => $input["asset_class_id"], "status" => $input["asset_status"]), array("asset tag" => "required", "serial_number" => "required", "model" => "exists:tbl_asset_models,id", "warranty start date" => "required_with:warranty end date|date:Y-m-d", "warranty end date" => "date:Y-m-d|after:" . $warranty_start, "asset type" => "required|exists:tbl_asset_classifications,id", "status" => "required|in:Available,For Repair,Installed,Lost,Retired"));
         if ($validator->fails()) {
             Input::flash();
             return Redirect::to('assets/network/update/' . $input["asset_key"] . "/" . $input["id"])->with('message', $validator->messages()->first());
         } else {
             if ($input["asset_tag"] != null && (Asset::where("asset_tag", "=", $input["asset_tag"])->first() && $asset->asset_tag != $input["asset_tag"])) {
                 Input::flash();
                 return Redirect::to('assets/network/update/' . $input["asset_key"] . "/" . $input["id"])->with('message', "Asset tag already exists in the database. This field should be unique.");
             } else {
                 if (Asset::where("serial_number", "=", $input["serial_number"])->first() && $asset->serial_number != $input["serial_number"]) {
                     Input::flash();
                     return Redirect::to('assets/network/update/' . $input["asset_key"] . "/" . $input["id"])->with('message', "Serial number already exists in the database. This field should be unique.");
                 } 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"] != $asset->asset_tag) {
                         $isChanged = true;
                         $changes[$index] = 1 + $index . ".) asset tag (from <strong>" . $asset->asset_tag . "</strong> to <strong>" . $input["asset_tag"] . "</strong>)<br/>";
                         $index += 1;
                     }
                     if ($input["serial_number"] != $asset->serial_number) {
                         $isChanged = true;
                         $changes[$index] = 1 + $index . ".) serial number (from <strong>" . $asset->serial_number . "</strong> to <strong>" . $input["serial_number"] . "</strong>)<br/>";
                         $index += 1;
                     }
                     if ($notes != $asset->notes) {
                         $isChanged = true;
                         $oldNotes = !empty($asset->notes) ? $asset->notes : "none";
                         $newNotes = !empty($notes) ? $notes : "none";
                         $changes[$index] = 1 + $index . ".) notes (from <strong>" . $oldNotes . "</strong> to <strong>" . $newNotes . "</strong>)<br/>";
                         $index += 1;
                     }
                     if ($input["model_id"] != $asset->model_id) {
                         $isChanged = true;
                         $newModelName = Model::find($input["model_id"]);
                         $newModel = !$newModelName ? "none" : $newModelName->name;
                         $oldModel = empty($asset->model->name) ? "none" : $asset->model->name;
                         $changes[$index] = 1 + $index . ".) model (from <strong>" . $oldModel . "</strong> to <strong>" . $newModel . "</strong>)<br/>";
                         $index += 1;
                     }
                     if ($input["location"] != $asset->location) {
                         $isChanged = true;
                         $newLocation = $input["location"];
                         $newLocation = empty(trim($newLocation)) ? "none" : $newLocation;
                         $oldLocation = empty($asset->location) ? "none" : $asset->location;
                         $changes[$index] = 1 + $index . ".) asset location (from <strong>" . $oldLocation . "</strong> to <strong>" . $newLocation . "</strong>)<br/>";
                         $index += 1;
                     }
                     if ($input["warranty_start"] != $asset->warranty_start) {
                         $oldWarrantyStart = !empty($asset->warranty_start) ? $asset->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"] != $asset->warranty_end) {
                         $oldWarrantyEnd = !empty($asset->warranty_end) ? $asset->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_class_id"] != $asset->classification_id) {
                         $isChanged = true;
                         $newClassification = AssetClassification::find($input["asset_class_id"]);
                         $changes[$index] = 1 + $index . ".) asset type (from <strong>" . $asset->classification->name . "</strong> to <strong>" . $newClassification->name . "</strong>)<br/>";
                         $index += 1;
                     }
                     if ($input["asset_status"] != $asset->status) {
                         $isChanged = true;
                         $changes[$index] = 1 + $index . ".) status (from <strong>" . $asset->status . "</strong> to <strong>" . $input["asset_status"] . "</strong>)<br/>";
                         $index += 1;
                     }
                     if (!$isChanged) {
                         Input::flash();
                         return Redirect::to('assets/network/update/' . $input["asset_key"] . '/' . $asset->id)->with('info', "Nothing has changed. </3");
                     } else {
                         //Save updates
                         $asset->asset_tag = $input["asset_tag"];
                         $asset->serial_number = trim($input["serial_number"]) != null ? trim($input["serial_number"]) : null;
                         $asset->notes = !empty($notes) ? $notes : null;
                         $asset->model_id = $input["model_id"] != null ? $input["model_id"] : null;
                         $asset->location = !empty(trim($input["location"])) ? trim($input["location"]) : null;
                         $asset->warranty_start = trim($input["warranty_start"]) != null ? trim($input["warranty_start"]) : null;
                         $asset->warranty_end = trim($input["warranty_end"]) ? trim($input["warranty_end"]) : null;
                         $asset->classification_id = $input["asset_class_id"];
                         $asset->status = $input["asset_status"];
                         $asset->save();
                         $changesMade = implode($changes, "");
                         $desc = "(" . Session::get('user_type') . ") " . "<strong>" . Session::get('username') . "</strong> has updated network asset <strong>" . $asset->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
                         $assetLog = new AssetLog();
                         $assetLog->asset_id = $asset->id;
                         $assetLog->user_id = Session::get("user_id");
                         $assetLog->description = $desc;
                         $assetLog->transaction = "Updates";
                         $assetLog->save();
                         return Redirect::to('assets/network/update/' . $input["asset_key"] . "/" . $asset->id)->with('success', "You have successfully updated the network asset information.");
                     }
                 }
             }
         }
     } else {
         return Redirect::to("/");
     }
 }