Skip to content

A free open source IT asset/license management system

Revert import history to master version to fix errors

snipec5e6f06e9b10
diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php
index 8f67158acf..cadf74bacc 100755
--- a/app/Http/Controllers/Assets/AssetsController.php
+++ b/app/Http/Controllers/Assets/AssetsController.php
@@ -522,45 +522,25 @@ class AssetsController extends Controller
* 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 [herroworrd]
- * @since [v5.0]
+ * @author [A. Gianotto] [<[email protected]>]
+ * @since [v3.3]
* @return View
*/
public function postImportHistory(Request $request)
{
-
if (!$request->hasFile('user_import_csv')) {
return back()->with('error', 'No file provided. Please select a file for import and try again. ');
}
-
if (!ini_get("auto_detect_line_endings")) {
ini_set("auto_detect_line_endings", '1');
}
-
- $requiredcolumns = ['Asset Tag', 'Checkout Date', 'Checkin Date', 'Full Name'];
-
$csv = Reader::createFromPath(Input::file('user_import_csv'));
-
$csv->setHeaderOffset(0);
-
- //Stop here if we don't have the columns we need
- if(count(array_intersect($requiredcolumns, $csv->getHeader())) != count($requiredcolumns)) {
- $status['error'][]['csv'][]['msg'] = 'Headers do not match';
- return view('hardware/history')->with('status', $status);
- }
- $statement = (new Statement())
- ->orderBy(\Closure::fromCallable([$this, 'sortByName']));
-
- $results = $statement->process($csv);
-
+ $results = $csv->getRecords();
+ $item = array();
$status = array();
$status['error'] = array();
$status['success'] = array();
- $base_username = null;
- $cachetime = Carbon::now()->addSeconds(120);
-
- foreach ($results as $record) {
-
foreach ($results as $row) {
if (is_array($row)) {
$row = array_change_key_case($row, CASE_LOWER);
@@ -569,29 +549,23 @@ class AssetsController extends Controller
$item[$asset_tag] = array();
}
$batch_counter = count($item[$asset_tag]);
-
$item[$asset_tag][$batch_counter]['checkout_date'] = Carbon::parse(Helper::array_smart_fetch($row, "checkout date"))->format('Y-m-d H:i:s');
$item[$asset_tag][$batch_counter]['checkin_date'] = Carbon::parse(Helper::array_smart_fetch($row, "checkin date"))->format('Y-m-d H:i:s');
\Log::debug($item[$asset_tag][$batch_counter]['checkin_date']);
-
$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");
-
if ($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'];
@@ -611,12 +585,10 @@ class AssetsController extends Controller
$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;
$item[$asset_tag][$batch_counter]['user_id'] = $user->id;
-
Actionlog::firstOrCreate(array(
'item_id' => $asset->id,
'item_type' => Asset::class,
@@ -627,9 +599,7 @@ class AssetsController extends Controller
'created_at' => $item[$asset_tag][$batch_counter]['checkout_date'],
'action_type' => 'checkout',
));
-
$asset->assigned_to = $user->id;
-
if ($asset->save()) {
$status['success'][]['asset'][$asset_tag]['msg'] = 'Asset successfully matched for '.Helper::array_smart_fetch($row, "name").$user_query.' on '.$item[$asset_tag][$batch_counter]['checkout_date'];
} else {
@@ -645,47 +615,35 @@ class AssetsController extends Controller
}
}
}
-
// 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 ((array_key_exists('checkedout_to', $asset_batch[$x])) && ($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]['checkin_date'])->format('Y-m-d H:i:s');
$asset_batch[$x]['real_checkin'] = $checkin_date;
-
\Log::debug($asset_batch[$next]['checkin_date']);
\Log::debug($checkin_date);
Actionlog::firstOrCreate(array(
- 'item_id' => $asset,
+ 'item_id' => $asset_batch[$x]['asset_id'],
'item_type' => Asset::class,
'user_id' => Auth::user()->id,
- 'note' => 'Historical record added by ' . Auth::user()->present()->fullName(),
- 'target_id' => $user,
- 'target_type' => User::class,
- 'created_at' => $checkindate,
+ 'note' => 'Checkin imported by ' . Auth::user()->present()->fullName() . ' from history importer',
+ 'target_id' => null,
+ 'created_at' => $checkin_date,
'action_type' => 'checkin'
));
-
- $status['success'][]['asset'][$asset_tag]['msg'] = 'Asset successfully matched for ' . $record['Full Name'] . ' on ' . $checkoutdate;
- }
- else {
- $status['error'][]['asset'][$asset_tag]['msg'] = 'Checkin date needs to be after checkout date.';
+ }
}
}
- else {
- $status['error'][]['asset'][$asset_tag]['msg'] = 'Asset not found in Snipe';
- }
}
-
return view('hardware/history')->with('status', $status);
}
- protected function sortByName(array $recordA, array $recordB): int
+ public function sortByName(array $recordA, array $recordB): int
{
return strcmp($recordB['Full Name'], $recordA['Full Name']);
}