/** * Returns a new form for work orders. * * @param WorkOrder $workOrder * * @return \Orchestra\Contracts\Html\Builder */ public function form(WorkOrder $workOrder) { return $this->form->of('work-orders', function (FormGrid $form) use($workOrder) { if ($workOrder->exists) { $method = 'PATCH'; $url = route('maintenance.work-orders.update', [$workOrder->getKey()]); $form->submit = 'Save'; } else { $method = 'POST'; $url = route('maintenance.work-orders.store'); $form->submit = 'Create'; } $form->with($workOrder); $form->attributes(compact('method', 'url')); $form->fieldset(function (Fieldset $fieldset) { $fieldset->control('select', 'category')->value(function (WorkOrder $workOrder) { return $workOrder->category_id; })->options(function () { return Category::getSelectHierarchy('work-orders'); }); $fieldset->control('select', 'location')->value(function (WorkOrder $workOrder) { return $workOrder->location_id; })->options(function () { return Location::getSelectHierarchy(); }); $fieldset->control('select', 'status')->options(function () { $statuses = Status::all()->pluck('name', 'id'); $statuses[0] = 'None'; return $statuses; }); $fieldset->control('select', 'priority')->value(function (WorkOrder $workOrder) { return $workOrder->priority_id; })->options(function () { $priorities = Priority::all()->pluck('name', 'id'); $priorities[0] = 'None'; return $priorities; }); $fieldset->control('select', 'assets[]')->label('Assets')->options(function () { return Asset::all()->pluck('name', 'id'); })->attributes(['class' => 'select2', 'multiple' => true]); $fieldset->control('input:text', 'subject')->attributes(['placeholder' => 'ex. Worked on HVAC']); $fieldset->control('input:textarea', 'description'); }); }); }
/** * Import history * * This needs a LOT of love. It's done very inelegantly right now, and there are * a ton of optimizations that could (and should) be done. * * @author [A. Gianotto] [<*****@*****.**>] * @since [v3.3] * @return View */ public function postImportHistory(Request $request) { if (!ini_get("auto_detect_line_endings")) { ini_set("auto_detect_line_endings", '1'); } $assets = Asset::all(['asset_tag']); $csv = Reader::createFromPath(Input::file('user_import_csv')); $csv->setNewline("\r\n"); //get the first row, usually the CSV header //$headers = $csv->fetchOne(); $results = $csv->fetchAssoc(); $item = array(); $status = array(); foreach ($results as $row) { if (is_array($row)) { $row = array_change_key_case($row, CASE_LOWER); $asset_tag = Helper::array_smart_fetch($row, "asset tag"); if (!array_key_exists($asset_tag, $item)) { $item[$asset_tag] = array(); } $batch_counter = count($item[$asset_tag]); $item[$asset_tag][$batch_counter]['checkout_date'] = Carbon::parse(Helper::array_smart_fetch($row, "date"))->format('Y-m-d H:i:s'); $item[$asset_tag][$batch_counter]['asset_tag'] = Helper::array_smart_fetch($row, "asset tag"); $item[$asset_tag][$batch_counter]['name'] = Helper::array_smart_fetch($row, "name"); $item[$asset_tag][$batch_counter]['email'] = Helper::array_smart_fetch($row, "email"); $asset = Asset::where('asset_tag', '=', $asset_tag)->first(); $item[$asset_tag][$batch_counter]['asset_id'] = $asset->id; $base_username = User::generateFormattedNameFromFullName(Setting::getSettings()->username_format, $item[$asset_tag][$batch_counter]['name']); $user = User::where('username', '=', $base_username['username']); $user_query = ' on username ' . $base_username['username']; if ($request->input('match_firstnamelastname') == '1') { $firstnamedotlastname = User::generateFormattedNameFromFullName('firstname.lastname', $item[$asset_tag][$batch_counter]['name']); $item[$asset_tag][$batch_counter]['username'][] = $firstnamedotlastname['username']; $user->orWhere('username', '=', $firstnamedotlastname['username']); $user_query .= ', or on username ' . $firstnamedotlastname['username']; } if ($request->input('match_flastname') == '1') { $flastname = User::generateFormattedNameFromFullName('filastname', $item[$asset_tag][$batch_counter]['name']); $item[$asset_tag][$batch_counter]['username'][] = $flastname['username']; $user->orWhere('username', '=', $flastname['username']); $user_query .= ', or on username ' . $flastname['username']; } if ($request->input('match_firstname') == '1') { $firstname = User::generateFormattedNameFromFullName('firstname', $item[$asset_tag][$batch_counter]['name']); $item[$asset_tag][$batch_counter]['username'][] = $firstname['username']; $user->orWhere('username', '=', $firstname['username']); $user_query .= ', or on username ' . $firstname['username']; } if ($request->input('match_email') == '1') { if ($item[$asset_tag][$batch_counter]['email'] == '') { $item[$asset_tag][$batch_counter]['username'][] = $user_email = User::generateEmailFromFullName($item[$asset_tag][$batch_counter]['name']); $user->orWhere('username', '=', $user_email); $user_query .= ', or on username ' . $user_email; } } // A matching user was found if ($user = $user->first()) { $item[$asset_tag][$batch_counter]['checkedout_to'] = $user->id; $status['success'][] = 'Found user ' . Helper::array_smart_fetch($row, "name") . $user_query; if ($asset) { $item[$asset_tag][$batch_counter]['user_id'] = $user->id; Actionlog::firstOrCreate(array('asset_id' => $asset->id, 'asset_type' => 'hardware', 'user_id' => Auth::user()->id, 'note' => 'Checkout imported by ' . Auth::user()->fullName() . ' from history importer', 'checkedout_to' => $item[$asset_tag][$batch_counter]['user_id'], 'created_at' => $item[$asset_tag][$batch_counter]['checkout_date'], 'action_type' => 'checkout')); $asset->assigned_to = $user->id; $asset->save(); } else { $status['error'][] = 'Asset does not exist so no checkin log was created.'; } } else { $item[$asset_tag][$batch_counter]['checkedout_to'] = null; $status['error'][] = 'No matching user for ' . Helper::array_smart_fetch($row, "name"); } } } // Loop through and backfill the checkins foreach ($item as $key => $asset_batch) { $total_in_batch = count($asset_batch); for ($x = 0; $x < $total_in_batch; $x++) { $next = $x + 1; // Only do this if a matching user was found if ($asset_batch[$x]['checkedout_to'] != '') { if ($total_in_batch > 1 && $x < $total_in_batch && array_key_exists($next, $asset_batch)) { $checkin_date = Carbon::parse($asset_batch[$next]['checkout_date'])->subDay(1)->format('Y-m-d H:i:s'); $asset_batch[$x]['real_checkin'] = $checkin_date; Actionlog::firstOrCreate(array('asset_id' => $asset_batch[$x]['asset_id'], 'asset_type' => 'hardware', 'user_id' => Auth::user()->id, 'note' => 'Checkin imported by ' . Auth::user()->fullName() . ' from history importer', 'checkedout_to' => null, 'created_at' => $checkin_date, 'action_type' => 'checkin')); } } } } return View::make('hardware/history')->with('status', $status); }
public static function detailedAssetList() { $assets = array('' => trans('general.select_asset')) + Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id')->toArray(); return $assets; }
/** * Execute the console command. * * @return mixed */ public function fire() { $filename = $this->argument('filename'); if (!$this->option('web-importer')) { $logFile = $this->option('logfile'); \Log::useFiles($logFile); if ($this->option('testrun')) { $this->comment('====== TEST ONLY Asset Import for ' . $filename . ' ===='); $this->comment('============== NO DATA WILL BE WRITTEN =============='); } else { $this->comment('======= Importing Assets from ' . $filename . ' ========='); } } if (!ini_get("auto_detect_line_endings")) { ini_set("auto_detect_line_endings", '1'); } $csv = Reader::createFromPath($this->argument('filename')); $csv->setNewline("\r\n"); $results = $csv->fetchAssoc(); $newarray = null; foreach ($results as $index => $arraytoNormalize) { $internalnewarray = array_change_key_case($arraytoNormalize); $newarray[$index] = $internalnewarray; } $this->locations = Location::All(['name', 'id']); $this->categories = Category::All(['name', 'category_type', 'id']); $this->manufacturers = Manufacturer::All(['name', 'id']); $this->asset_models = AssetModel::All(['name', 'modelno', 'category_id', 'manufacturer_id', 'id']); $this->companies = Company::All(['name', 'id']); $this->status_labels = Statuslabel::All(['name', 'id']); $this->suppliers = Supplier::All(['name', 'id']); $this->assets = Asset::all(['asset_tag']); $this->accessories = Accessory::All(['name']); $this->consumables = Consumable::All(['name']); $this->customfields = CustomField::All(['name']); $bar = null; if (!$this->option('web-importer')) { $bar = $this->output->createProgressBar(count($newarray)); } // Loop through the records DB::transaction(function () use(&$newarray, $bar) { Model::unguard(); $item_type = strtolower($this->option('item-type')); foreach ($newarray as $row) { // Let's just map some of these entries to more user friendly words // Fetch general items here, fetch item type specific items in respective methods /** @var Asset, License, Accessory, or Consumable $item_type */ $item_category = $this->array_smart_fetch($row, "category"); $item_company_name = $this->array_smart_fetch($row, "company"); $item_location = $this->array_smart_fetch($row, "location"); $item_status_name = $this->array_smart_fetch($row, "status"); $item["item_name"] = $this->array_smart_fetch($row, "item name"); if ($this->array_smart_fetch($row, "purchase date") != '') { $item["purchase_date"] = date("Y-m-d 00:00:01", strtotime($this->array_smart_fetch($row, "purchase date"))); } else { $item["purchase_date"] = null; } $item["purchase_cost"] = $this->array_smart_fetch($row, "purchase cost"); $item["order_number"] = $this->array_smart_fetch($row, "order number"); $item["notes"] = $this->array_smart_fetch($row, "notes"); $item["quantity"] = $this->array_smart_fetch($row, "quantity"); $item["requestable"] = $this->array_smart_fetch($row, "requestable"); $item["asset_tag"] = $this->array_smart_fetch($row, "asset tag"); $this->current_assetId = $item["item_name"]; if ($item["asset_tag"] != '') { $this->current_assetId = $item["asset_tag"]; } $this->log('Category: ' . $item_category); $this->log('Location: ' . $item_location); $this->log('Purchase Date: ' . $item["purchase_date"]); $this->log('Purchase Cost: ' . $item["purchase_cost"]); $this->log('Company Name: ' . $item_company_name); $this->log('Status: ' . $item_status_name); $item["user"] = $this->createOrFetchUser($row); $item["location"] = $this->createOrFetchLocation($item_location); $item["category"] = $this->createOrFetchCategory($item_category, $item_type); $item["manufacturer"] = $this->createOrFetchManufacturer($row); $item["company"] = $this->createOrFetchCompany($item_company_name); $item["status_label"] = $this->createOrFetchStatusLabel($item_status_name); switch ($item_type) { case "asset": // ----------------------------- // CUSTOM FIELDS // ----------------------------- // Loop through custom fields in the database and see if we have any matches in the CSV foreach ($this->customfields as $customfield) { if ($item['custom_fields'][$customfield->db_column_name()] = $this->array_smart_custom_field_fetch($row, $customfield)) { $this->log('Custom Field ' . $customfield->name . ': ' . $this->array_smart_custom_field_fetch($row, $customfield)); } } $this->createAssetIfNotExists($row, $item); break; case "accessory": $this->createAccessoryIfNotExists($item); break; case 'consumable': $this->createConsumableIfNotExists($item); break; } if (!$this->option('web-importer')) { $bar->advance(); } $this->log('------------- Action Summary ----------------'); } }); if (!$this->option('web-importer')) { $bar->finish(); } $this->log('====================================='); if (!$this->option('web-importer')) { if (!empty($this->errors)) { $this->comment("The following Errors were encountered."); foreach ($this->errors as $asset => $error) { $this->comment('Error: Item: ' . $asset . 'failed validation: ' . json_encode($error)); } } else { $this->comment("All Items imported successfully!"); } } else { if (empty($this->errors)) { return 0; } else { $this->comment(json_encode($this->errors)); //Send a big string to the return 1; } } $this->comment(""); return 2; }
/** * Returns a form view to edit a selected asset maintenance. * * @see AssetMaintenancesController::postEdit() method that stores the data * @author Vincent Sposato <*****@*****.**> * @param int $assetMaintenanceId * @version v1.0 * @since [v1.8] * @return mixed */ public function getEdit($assetMaintenanceId = null) { // Check if the asset maintenance exists if (is_null($assetMaintenance = AssetMaintenance::find($assetMaintenanceId))) { // Redirect to the improvement management page return redirect()->to('admin/asset_maintenances')->with('error', trans('admin/asset_maintenances/message.not_found')); } elseif (!Company::isCurrentUserHasAccess($assetMaintenance->asset)) { return static::getInsufficientPermissionsRedirect(); } if ($assetMaintenance->completion_date == '0000-00-00') { $assetMaintenance->completion_date = null; } if ($assetMaintenance->start_date == '0000-00-00') { $assetMaintenance->start_date = null; } if ($assetMaintenance->cost == '0.00') { $assetMaintenance->cost = null; } // Prepare Improvement Type List $assetMaintenanceType = ['' => 'Select an improvement type'] + AssetMaintenance::getImprovementOptions(); $assets = Company::scopeCompanyables(Asset::all(), 'assets.company_id')->lists('detailed_name', 'id'); // Get Supplier List $supplier_list = Helper::suppliersList(); // Render the view return View::make('asset_maintenances/edit')->with('asset_list', $assets)->with('selectedAsset', null)->with('supplier_list', $supplier_list)->with('assetMaintenanceType', $assetMaintenanceType)->with('assetMaintenance', $assetMaintenance); }