From 66ac5d05ad843405eee09a5ef7c2c9e85984c68f Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 22 Oct 2020 23:18:14 -0700 Subject: [PATCH 1/3] Started migration to normalize note --- .../Controllers/Api/AccessoriesController.php | 1 - ..._accessory_checkout_note_to_join_table.php | 84 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index d921065a5f..6a01b52c09 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -154,7 +154,6 @@ class AccessoriesController extends Controller $offset = request('offset', 0); $limit = request('limit', 50); - $accessory->lastCheckoutArray = $accessory->lastCheckout->toArray(); $accessory_users = $accessory->users; $total = $accessory_users->count(); diff --git a/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php b/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php new file mode 100644 index 0000000000..39920c2f67 --- /dev/null +++ b/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php @@ -0,0 +1,84 @@ +string('note')->nullable(true)->default(null); +// }); + + // Loop through the checked out accessories, find their related action_log entry, and copy over the note + // to the newly created note field + + + $accessories = Accessory::get(); + $count = 0; + + foreach ($accessories as $accessory) { + $count++; + + foreach ($accessory->users as $join_log) { + + \Log::debug('Looking for accessories_users that match '. $join_log->created_at); + + $log_entries = $accessory + ->assetlog() + ->whereNotNull('note') + ->where('created_at', '=',$join_log->created_at) + ->where('action_type', '=', 'checkout') + ->orderBy('created_at', 'DESC') + ->take('1'); + + + \Log::debug($accessory->toSql()); + + $log_entries->get(); + + \Log::debug($count. '. Looking for action_logs that match '. $join_log->created_at); + + foreach ($log_entries as $log_entry) { + \Log::debug($log_entries->count().' action_logs that match'); + + \Log::debug($count. '. Checkout date in asset log: '.$log_entry->created_at.' against accessories_users: '.$join_log->created_at); + + if ($log_entry->created_at == $join_log->created_at) { + \Log::debug($count. '. Note for '.$accessory->name.' checked out on '.$log_entry->created_at.' to '.$log_entry->target_id.' is '.$log_entry->note); + } else { + \Log::debug('No match'); + + } + } + + } + + + + } + + die(); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('accessory_checkout', function (Blueprint $table) { + $table->dropColumn('note'); + }); + } +} From 8827d33a434eb017ba12c8454904d5bdf6f3a9f4 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 23 Oct 2020 00:25:20 -0700 Subject: [PATCH 2/3] Fixed query to copy notes --- ..._accessory_checkout_note_to_join_table.php | 55 ++++++++++--------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php b/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php index 39920c2f67..bfa9a7e362 100644 --- a/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php +++ b/database/migrations/2020_10_22_233743_move_accessory_checkout_note_to_join_table.php @@ -4,6 +4,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; use App\Models\Accessory; +use App\Models\Actionlog; class MoveAccessoryCheckoutNoteToJoinTable extends Migration { @@ -14,49 +15,54 @@ class MoveAccessoryCheckoutNoteToJoinTable extends Migration */ public function up() { -// Schema::table('accessory_checkout', function (Blueprint $table) { -// $table->string('note')->nullable(true)->default(null); -// }); + Schema::table('accessories_users', function (Blueprint $table) { + $table->string('note')->nullable(true)->default(null); + }); // Loop through the checked out accessories, find their related action_log entry, and copy over the note // to the newly created note field - $accessories = Accessory::get(); $count = 0; + \Log::debug('Accessory Count: '. $accessories->count()); + + // Loop through all of the accessories foreach ($accessories as $accessory) { $count++; - foreach ($accessory->users as $join_log) { + \Log::debug('Querying join logs'); + $join_logs = DB::table('accessories_users')->get(); + // Loop through the accessories_users records + foreach ($join_logs as $join_log) { + \Log::debug($join_logs->count().' join log records'); \Log::debug('Looking for accessories_users that match '. $join_log->created_at); - $log_entries = $accessory - ->assetlog() - ->whereNotNull('note') - ->where('created_at', '=',$join_log->created_at) + // Get the records from action_logs so we can copy the notes over to the new notes field + // on the accessories_users table + $action_log_entries = Actionlog::where('created_at', '=',$join_log->created_at) + ->where('target_id', '=',$join_log->assigned_to) + ->where('item_id', '=',$accessory->id) + ->where('target_type', '=','App\\Models\\User') ->where('action_type', '=', 'checkout') - ->orderBy('created_at', 'DESC') - ->take('1'); + ->orderBy('created_at', 'DESC')->get(); + \Log::debug($action_log_entries->count().' matching entries in the action_logs table'); + \Log::debug('Looking for action_logs that match '. $join_log->created_at); - \Log::debug($accessory->toSql()); + foreach ($action_log_entries as $action_log_entry) { - $log_entries->get(); + \Log::debug('Checkout date in asset log: '.$action_log_entry->created_at.' against accessories_users: '.$join_log->created_at); + \Log::debug('Action log: '.$action_log_entry->created_at); + \Log::debug('Join log: '.$join_log->created_at); - \Log::debug($count. '. Looking for action_logs that match '. $join_log->created_at); - - foreach ($log_entries as $log_entry) { - \Log::debug($log_entries->count().' action_logs that match'); - - \Log::debug($count. '. Checkout date in asset log: '.$log_entry->created_at.' against accessories_users: '.$join_log->created_at); - - if ($log_entry->created_at == $join_log->created_at) { - \Log::debug($count. '. Note for '.$accessory->name.' checked out on '.$log_entry->created_at.' to '.$log_entry->target_id.' is '.$log_entry->note); + if ($action_log_entry->created_at == $join_log->created_at) { + DB::table('accessories_users') + ->where('id', $join_log->id) + ->update(['note' => $action_log_entry->note]); } else { \Log::debug('No match'); - } } @@ -66,7 +72,6 @@ class MoveAccessoryCheckoutNoteToJoinTable extends Migration } - die(); } @@ -77,7 +82,7 @@ class MoveAccessoryCheckoutNoteToJoinTable extends Migration */ public function down() { - Schema::table('accessory_checkout', function (Blueprint $table) { + Schema::table('accessories_users', function (Blueprint $table) { $table->dropColumn('note'); }); } From 88f03e6b55d5d344b69f60b0fe5971d6ace1284f Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 23 Oct 2020 00:44:26 -0700 Subject: [PATCH 3/3] Added last_checkout and notes from pivot for accessories --- .../Accessories/AccessoryCheckoutController.php | 3 ++- app/Http/Transformers/AccessoriesTransformer.php | 8 +++++++- app/Models/Accessory.php | 2 +- app/Models/User.php | 3 ++- resources/views/accessories/view.blade.php | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php index a6cb745923..656a040a55 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php @@ -75,7 +75,8 @@ class AccessoryCheckoutController extends Controller 'accessory_id' => $accessory->id, 'created_at' => Carbon::now(), 'user_id' => Auth::id(), - 'assigned_to' => $request->get('assigned_to') + 'assigned_to' => $request->get('assigned_to'), + 'note' => $request->input('note') ]); DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first(); diff --git a/app/Http/Transformers/AccessoriesTransformer.php b/app/Http/Transformers/AccessoriesTransformer.php index 8b56ae05f6..d40642d875 100644 --- a/app/Http/Transformers/AccessoriesTransformer.php +++ b/app/Http/Transformers/AccessoriesTransformer.php @@ -68,8 +68,13 @@ class AccessoriesTransformer $array = array(); + + foreach ($accessory_users as $user) { + \Log::debug(print_r($user->pivot, true)); + \Log::debug(print_r($user->pivot, true)); $array[] = [ + 'assigned_pivot_id' => $user->pivot->id, 'id' => (int) $user->id, 'username' => e($user->username), @@ -77,7 +82,8 @@ class AccessoriesTransformer 'first_name'=> e($user->first_name), 'last_name'=> e($user->last_name), 'employee_number' => e($user->employee_num), - 'checkout_notes' => $accessory->lastCheckoutArray[0]['note'], + 'checkout_notes' => $user->pivot->note, + 'last_checkout' => Helper::getFormattedDateObject($user->pivot->created_at, 'datetime'), 'type' => 'user', 'available_actions' => ['checkin' => true] ]; diff --git a/app/Models/Accessory.php b/app/Models/Accessory.php index 25c835d382..fbd611939d 100755 --- a/app/Models/Accessory.php +++ b/app/Models/Accessory.php @@ -234,7 +234,7 @@ class Accessory extends SnipeModel public function users() { - return $this->belongsToMany('\App\Models\User', 'accessories_users', 'accessory_id', 'assigned_to')->withPivot('id')->withTrashed(); + return $this->belongsToMany('\App\Models\User', 'accessories_users', 'accessory_id', 'assigned_to')->withPivot('id', 'created_at', 'note')->withTrashed(); } /** diff --git a/app/Models/User.php b/app/Models/User.php index ff886905fc..a5d2f36c92 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -295,7 +295,8 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo */ public function accessories() { - return $this->belongsToMany('\App\Models\Accessory', 'accessories_users', 'assigned_to', 'accessory_id')->withPivot('id')->withTrashed(); + return $this->belongsToMany('\App\Models\Accessory', 'accessories_users', 'assigned_to', 'accessory_id') + ->withPivot('id', 'created_at', 'note')->withTrashed(); } /** diff --git a/resources/views/accessories/view.blade.php b/resources/views/accessories/view.blade.php index 5ca50ea4f4..7241807cf3 100644 --- a/resources/views/accessories/view.blade.php +++ b/resources/views/accessories/view.blade.php @@ -76,6 +76,7 @@ {{ trans('general.user') }} {{ trans('general.notes') }} + {{ trans('admin/hardware/table.checkout_date') }} {{ trans('table.actions') }}