Fixed query to copy notes

This commit is contained in:
snipe 2020-10-23 00:25:20 -07:00
parent 66ac5d05ad
commit 8827d33a43

View file

@ -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');
});
}