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("/");
     }
 }
 public function softwareAssetsReports()
 {
     if (Session::has('username') && (Session::get('user_type') == "Root" || Session::get('user_type') == "Admin" || Session::get("user_type") == "User")) {
         $view = View::make("Reports.software_assets_reports");
         $view->nav = "system";
         $view->tab = "software";
         $totalSoftwareAssets = Software::all()->count();
         $softwareTypes = SoftwareType::orderBy("software_type")->get();
         $available = Software::where("status", "=", "Available")->count();
         $pwu = Software::where("status", "=", "PWU")->count();
         $retired = Software::where("status", "=", "Retired")->count();
         $test_case = Software::where("status", "=", "Test Case")->count();
         $lost = Software::where("status", "=", "Lost")->count();
         $view->softwareTypes = $softwareTypes;
         $view->available = $available;
         $view->pwu = $pwu;
         $view->retired = $retired;
         $view->test_case = $test_case;
         $view->lost = $lost;
         $view->totalSoftwareAssets = $totalSoftwareAssets;
         return $view;
     } else {
         return Redirect::to("/");
     }
 }
 public function exportSoftwareBegin()
 {
     if (Session::has('username') && (Session::get('user_type') == "Root" || Session::get('user_type') == "Admin")) {
         if (isset($_GET["search"])) {
             $view = View::make("Export.export_software");
             $view->nav = "system";
             $view->tab = "software";
             $input = Input::all();
             $asset_tag = trim($input["asset_tag"]) != null ? str_replace(' ', '%', trim($input["asset_tag"])) : "";
             $employee = trim($input["employee"]) != null ? trim($input["employee"]) : "";
             $serial_number = trim($input["serial_number"]) != null ? str_replace(' ', '%', trim($input["serial_number"])) : "";
             $software_type = trim($input["software_type"]) != null ? str_replace(' ', '%', trim($input["software_type"])) : "";
             $asset_status = !empty($input["asset_status"]) ? $input["asset_status"] : "";
             $warranty_start = trim($input["warranty_start"]) != null ? str_replace(' ', '%', trim($input["warranty_start"])) : "";
             $warranty_end = trim($input["warranty_end"]) != null ? str_replace(' ', '%', trim($input["warranty_end"])) : "";
             $software = Software::where("asset_tag", "LIKE", "%{$asset_tag}%")->where(function ($query) use($employee) {
                 $emp = "%" . str_replace(' ', '%', $employee) . "%";
                 $emp = DB::connection()->getPdo()->quote($emp);
                 if (!empty($employee)) {
                     $query->where("employee_number", "LIKE", $employee)->orWhereHas("employee", function ($q) use($employee, $emp) {
                         $q->whereRaw("concat(first_name,' ',last_name) LIKE {$emp}");
                     })->orWhereHas("employee", function ($q) use($employee, $emp) {
                         $q->whereRaw("concat(last_name,' ',first_name) LIKE {$emp}");
                     });
                 }
             })->where(function ($query) use($serial_number) {
                 if (!empty($serial_number)) {
                     $query->where("assigned_to_serial_number", "=", $serial_number);
                 }
             })->where(function ($query) use($software_type) {
                 if (!empty(${$software_type})) {
                     $query->where("software_type_id", "=", $software_type);
                 }
             })->where(function ($query) use($asset_status) {
                 if (!empty($asset_status)) {
                     $query->where("status", "=", $asset_status);
                 }
             })->where(function ($query) use($warranty_start) {
                 if (!empty($warranty_start)) {
                     $query->where("warranty_start", "LIKE", "{$warranty_start}%");
                 }
             })->where(function ($query) use($warranty_end) {
                 if (!empty($warranty_end)) {
                     $query->where("warranty_end", "LIKE", "{$warranty_end}%");
                 }
             })->orderBy("asset_tag")->paginate(25);
             $result = Software::where("asset_tag", "LIKE", "%{$asset_tag}%")->where(function ($query) use($employee) {
                 $emp = "%" . str_replace(' ', '%', $employee) . "%";
                 $emp = DB::connection()->getPdo()->quote($emp);
                 if (!empty($employee)) {
                     $query->where("employee_number", "LIKE", $employee)->orWhereHas("employee", function ($q) use($employee, $emp) {
                         $q->whereRaw("concat(first_name,' ',last_name) LIKE {$emp}");
                     })->orWhereHas("employee", function ($q) use($employee, $emp) {
                         $q->whereRaw("concat(last_name,' ',first_name) LIKE {$emp}");
                     });
                 }
             })->where(function ($query) use($serial_number) {
                 if (!empty($serial_number)) {
                     $query->where("assigned_to_serial_number", "=", $serial_number);
                 }
             })->where(function ($query) use($software_type) {
                 if (!empty(${$software_type})) {
                     $query->where("software_type_id", "=", $software_type);
                 }
             })->where(function ($query) use($asset_status) {
                 if (!empty($asset_status)) {
                     $query->where("status", "=", $asset_status);
                 }
             })->where(function ($query) use($warranty_start) {
                 if (!empty($warranty_start)) {
                     $query->where("warranty_start", "LIKE", "{$warranty_start}%");
                 }
             })->where(function ($query) use($warranty_end) {
                 if (!empty($warranty_end)) {
                     $query->where("warranty_end", "LIKE", "{$warranty_end}%");
                 }
             })->count();
             $getSoftwareTypes = SoftwareType::all();
             $softwareTypes = array("" => "--Select One--");
             foreach ($getSoftwareTypes as $gst) {
                 $softwareTypes[$gst->id] = $gst->software_type;
             }
             $view->softwareTypes = $softwareTypes;
             $view->software = $software;
             $view->results = $result;
             Input::flash();
             return $view;
         } else {
             if (isset($_GET["export"])) {
                 $input = Input::all();
                 $asset_tag = trim($input["asset_tag"]) != null ? str_replace(' ', '%', trim($input["asset_tag"])) : "";
                 $employee = trim($input["employee"]) != null ? trim($input["employee"]) : "";
                 $serial_number = trim($input["serial_number"]) != null ? str_replace(' ', '%', trim($input["serial_number"])) : "";
                 $software_type = trim($input["software_type"]) != null ? str_replace(' ', '%', trim($input["software_type"])) : "";
                 $asset_status = !empty($input["asset_status"]) ? $input["asset_status"] : "";
                 $warranty_start = trim($input["warranty_start"]) != null ? str_replace(' ', '%', trim($input["warranty_start"])) : "";
                 $warranty_end = trim($input["warranty_end"]) != null ? str_replace(' ', '%', trim($input["warranty_end"])) : "";
                 if (Session::has("secure") && Session::get("user_type") == "Root" && isset($_GET["pk"])) {
                     $software = Software::where("asset_tag", "LIKE", "%{$asset_tag}%")->where(function ($query) use($employee) {
                         $emp = "%" . str_replace(' ', '%', $employee) . "%";
                         $emp = DB::connection()->getPdo()->quote($emp);
                         if (!empty($employee)) {
                             $query->where("employee_number", "LIKE", $employee)->orWhereHas("employee", function ($q) use($employee, $emp) {
                                 $q->whereRaw("concat(first_name,' ',last_name) LIKE {$emp}");
                             })->orWhereHas("employee", function ($q) use($employee, $emp) {
                                 $q->whereRaw("concat(last_name,' ',first_name) LIKE {$emp}");
                             });
                         }
                     })->where(function ($query) use($serial_number) {
                         if (!empty($serial_number)) {
                             $query->where("assigned_to_serial_number", "=", $serial_number);
                         }
                     })->where(function ($query) use($software_type) {
                         if (!empty(${$software_type})) {
                             $query->where("software_type_id", "=", $software_type);
                         }
                     })->where(function ($query) use($asset_status) {
                         if (!empty($asset_status)) {
                             $query->where("status", "=", $asset_status);
                         }
                     })->where(function ($query) use($warranty_start) {
                         if (!empty($warranty_start)) {
                             $query->where("warranty_start", "LIKE", "{$warranty_start}%");
                         }
                     })->where(function ($query) use($warranty_end) {
                         if (!empty($warranty_end)) {
                             $query->where("warranty_end", "LIKE", "{$warranty_end}%");
                         }
                     })->orderBy("asset_tag")->get();
                 } else {
                     $software = Software::where("asset_tag", "LIKE", "%{$asset_tag}%")->where(function ($query) use($employee) {
                         $emp = "%" . str_replace(' ', '%', $employee) . "%";
                         $emp = DB::connection()->getPdo()->quote($emp);
                         if (!empty($employee)) {
                             $query->where("employee_number", "LIKE", $employee)->orWhereHas("employee", function ($q) use($employee, $emp) {
                                 $q->whereRaw("concat(first_name,' ',last_name) LIKE {$emp}");
                             })->orWhereHas("employee", function ($q) use($employee, $emp) {
                                 $q->whereRaw("concat(last_name,' ',first_name) LIKE {$emp}");
                             });
                         }
                     })->where(function ($query) use($serial_number) {
                         if (!empty($serial_number)) {
                             $query->where("assigned_to_serial_number", "=", $serial_number);
                         }
                     })->where(function ($query) use($software_type) {
                         if (!empty(${$software_type})) {
                             $query->where("software_type_id", "=", $software_type);
                         }
                     })->where(function ($query) use($asset_status) {
                         if (!empty($asset_status)) {
                             $query->where("status", "=", $asset_status);
                         }
                     })->where(function ($query) use($warranty_start) {
                         if (!empty($warranty_start)) {
                             $query->where("warranty_start", "LIKE", "{$warranty_start}%");
                         }
                     })->where(function ($query) use($warranty_end) {
                         if (!empty($warranty_end)) {
                             $query->where("warranty_end", "LIKE", "{$warranty_end}%");
                         }
                     })->orderBy("asset_tag")->get(array("id", "asset_tag", "location", "warranty_start", "warranty_end", "notes", "status", "employee_number", "assigned_to_serial_number", "software_type_id", "date_added"));
                 }
                 if ($software->count() > 0) {
                     if (isset($_GET["format"])) {
                         $softwareArray = array();
                         if (Session::has("secure") && Session::get("user_type") == "Root" && isset($_GET["pk"])) {
                             foreach ($software as $s) {
                                 $softwareArray[$s->id] = array("Asset Tag" => $s->asset_tag, "Product Key" => $s->product_key, "Employee" => !empty($s->employee->first_name) ? $s->employee->first_name . " " . $s->employee->last_name : "Unassigned.", "Employee Number" => !empty($s->employee->employee_number) ? $s->employee->employee_number : null, "Location" => !empty($s->location) ? $s->location : "No Information.", "Assigned to Laptop" => !empty($s->assigned_to_serial_number) ? $s->assigned_to_serial_number : "No Information.", "Notes" => !empty($s->notes) ? $s->notes : null, "Warranty Start Date" => !empty($s->warranty_start) ? DateTime::createFromFormat("Y-m-d", $s->warranty_start)->format("F d, Y") : "No Information", "Warranty End Date" => !empty($s->warranty_end) ? DateTime::createFromFormat("Y-m-d", $s->warranty_end)->format("F d, Y") : "No Information", "Date Added to System" => DateTime::createFromFormat("Y-m-d H:i:s", $s->date_added)->format("F d, Y g:iA"), "Software Type" => $s->type->software_type, "Status" => $s->status);
                             }
                         } else {
                             foreach ($software as $s) {
                                 $softwareArray[$s->id] = array("Asset Tag" => $s->asset_tag, "Employee" => !empty($s->employee->first_name) ? $s->employee->first_name . " " . $s->employee->last_name : "Unassigned.", "Employee Number" => !empty($s->employee->employee_number) ? $s->employee->employee_number : null, "Location" => !empty($s->location) ? $s->location : "No Information.", "Assigned to Laptop" => !empty($s->assigned_to_serial_number) ? $s->assigned_to_serial_number : "No Information.", "Notes" => !empty($s->notes) ? $s->notes : null, "Warranty Start Date" => !empty($s->warranty_start) ? DateTime::createFromFormat("Y-m-d", $s->warranty_start)->format("F d, Y") : "No Information", "Warranty End Date" => !empty($s->warranty_end) ? DateTime::createFromFormat("Y-m-d", $s->warranty_end)->format("F d, Y") : "No Information", "Date Added to System" => DateTime::createFromFormat("Y-m-d H:i:s", $s->date_added)->format("F d, Y g:iA"), "Software Type" => $s->type->software_type, "Status" => $s->status);
                             }
                         }
                     } else {
                         $softwareArray = $software->toArray();
                     }
                     Excel::create('software_assets_export_' . str_random(6), function ($excel) use($softwareArray) {
                         // Set the title
                         $excel->setTitle('Software Assets Export');
                         // Chain the setters
                         $excel->setCreator('Vault')->setCompany('Vault');
                         // Call them separately
                         $excel->setDescription('Software Assets Data Export from IT Vault.');
                         // Our first sheet
                         $excel->sheet('Software Assets', function ($sheet) use($softwareArray) {
                             $sheet->fromArray($softwareArray);
                         });
                     })->download('xlsx');
                 } else {
                     Input::flash();
                     return Redirect::to('export/software')->withInput()->with('info', "No records have been retrieved. Data export cancelled.");
                 }
             }
         }
     } else {
         return Redirect::to("/");
     }
 }