From 7a45ed09826a083fb91718da622753dc7a7ecb34 Mon Sep 17 00:00:00 2001 From: jayvirsinh_gohil Date: Thu, 2 Dec 2021 12:38:56 +0530 Subject: [PATCH 1/2] solve delete users and create template issue --- .../Commands/CreateTemplateCommand.php | 2 +- app/Models/User.php | 112 ++++++++++-------- 2 files changed, 63 insertions(+), 51 deletions(-) diff --git a/app/Console/Commands/CreateTemplateCommand.php b/app/Console/Commands/CreateTemplateCommand.php index 704f2a55..6b14fb61 100644 --- a/app/Console/Commands/CreateTemplateCommand.php +++ b/app/Console/Commands/CreateTemplateCommand.php @@ -52,7 +52,7 @@ class CreateTemplateCommand extends Command } Storage::disk('views')->copy("/app/pdf/{$type}/{$type}1.blade.php", "/app/pdf/{$type}/{$templateName}.blade.php"); - copy(resource_path("/static/img/PDF/{$type}1.png"), resource_path("/static/img/PDF/{$templateName}.png")); + copy(public_path("/build/img/PDF/{$type}1.png"), public_path("/build/img/PDF/{$templateName}.png")); $path = resource_path("app/pdf/{$type}/{$templateName}.blade.php"); $type = ucfirst($type); diff --git a/app/Models/User.php b/app/Models/User.php index f956aad4..a019d523 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -96,7 +96,17 @@ class User extends Authenticatable implements HasMedia public function estimates() { - return $this->hasMany(Estimate::class); + return $this->hasMany(Estimate::class, 'creator_id'); + } + + public function customers() + { + return $this->hasMany(Customer::class, 'creator_id'); + } + + public function recurringInvoices() + { + return $this->hasMany(RecurringInvoice::class, 'creator_id'); } public function currency() @@ -114,34 +124,24 @@ class User extends Authenticatable implements HasMedia return $this->belongsToMany(Company::class, 'user_company', 'user_id', 'company_id'); } - public function addresses() - { - return $this->hasMany(Address::class); - } - public function expenses() { - return $this->hasMany(Expense::class); - } - - public function billingAddress() - { - return $this->hasOne(Address::class)->where('type', Address::BILLING_TYPE); - } - - public function shippingAddress() - { - return $this->hasOne(Address::class)->where('type', Address::SHIPPING_TYPE); + return $this->hasMany(Expense::class, 'creator_id'); } public function payments() { - return $this->hasMany(Payment::class); + return $this->hasMany(Payment::class, 'creator_id'); } public function invoices() { - return $this->hasMany(Invoice::class); + return $this->hasMany(Invoice::class, 'creator_id'); + } + + public function items() + { + return $this->hasMany(Item::class, 'creator_id'); } public function settings() @@ -255,37 +255,6 @@ class User extends Authenticatable implements HasMedia }); } - public static function deleteCustomers($ids) - { - foreach ($ids as $id) { - $customer = self::find($id); - - if ($customer->estimates()->exists()) { - $customer->estimates()->delete(); - } - - if ($customer->invoices()->exists()) { - $customer->invoices()->delete(); - } - - if ($customer->payments()->exists()) { - $customer->payments()->delete(); - } - - if ($customer->addresses()->exists()) { - $customer->addresses()->delete(); - } - - if ($customer->fields()->exists()) { - $customer->fields()->delete(); - } - - $customer->delete(); - } - - return true; - } - public function getAvatarAttribute() { $avatar = $this->getMedia('admin_avatar')->first(); @@ -485,4 +454,47 @@ class User extends Authenticatable implements HasMedia return false; } + + public static function deleteUsers($ids) + { + foreach ($ids as $id) { + $user = self::find($id); + + if ($user->invoices()->exists()) { + $user->invoices()->update(['creator_id' => null]); + } + + if ($user->estimates()->exists()) { + $user->estimates()->update(['creator_id' => null]); + } + + if ($user->customers()->exists()) { + $user->customers()->update(['creator_id' => null]); + } + + if ($user->recurringInvoices()->exists()) { + $user->recurringInvoices()->update(['creator_id' => null]); + } + + if ($user->expenses()->exists()) { + $user->expenses()->update(['creator_id' => null]); + } + + if ($user->payments()->exists()) { + $user->payments()->update(['creator_id' => null]); + } + + if ($user->items()->exists()) { + $user->items()->update(['creator_id' => null]); + } + + if ($user->settings()->exists()) { + $user->settings()->delete(); + } + + $user->delete(); + } + + return true; + } } From 887d5d0685d6063ed9b3a918ed832e921a5194dc Mon Sep 17 00:00:00 2001 From: jayvirsinh_gohil Date: Thu, 2 Dec 2021 14:46:53 +0530 Subject: [PATCH 2/2] solve template issue --- .../Commands/CreateTemplateCommand.php | 1 + ...74516_migrate_templates_from_version_4.php | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 database/migrations/2021_12_02_074516_migrate_templates_from_version_4.php diff --git a/app/Console/Commands/CreateTemplateCommand.php b/app/Console/Commands/CreateTemplateCommand.php index 6b14fb61..0eef7f21 100644 --- a/app/Console/Commands/CreateTemplateCommand.php +++ b/app/Console/Commands/CreateTemplateCommand.php @@ -53,6 +53,7 @@ class CreateTemplateCommand extends Command Storage::disk('views')->copy("/app/pdf/{$type}/{$type}1.blade.php", "/app/pdf/{$type}/{$templateName}.blade.php"); copy(public_path("/build/img/PDF/{$type}1.png"), public_path("/build/img/PDF/{$templateName}.png")); + copy(resource_path("/static/img/PDF/{$type}1.png"), resource_path("/static/img/PDF/{$templateName}.png")); $path = resource_path("app/pdf/{$type}/{$templateName}.blade.php"); $type = ucfirst($type); diff --git a/database/migrations/2021_12_02_074516_migrate_templates_from_version_4.php b/database/migrations/2021_12_02_074516_migrate_templates_from_version_4.php new file mode 100644 index 00000000..2e1aa0f9 --- /dev/null +++ b/database/migrations/2021_12_02_074516_migrate_templates_from_version_4.php @@ -0,0 +1,45 @@ +files('/app/pdf/invoice'); + + foreach ($templates as $key => $template) { + $templateName = Str::before(basename($template), '.blade.php'); + if (! file_exists(resource_path("/static/img/PDF/{$templateName}.png"))) { + copy(public_path("/assets/img/PDF/{$templateName}.png"), public_path("/build/img/PDF/{$templateName}.png")); + copy(public_path("/assets/img/PDF/{$templateName}.png"), resource_path("/static/img/PDF/{$templateName}.png")); + } + } + + $templates = Storage::disk('views')->files('/app/pdf/estimate'); + + foreach ($templates as $key => $template) { + $templateName = Str::before(basename($template), '.blade.php'); + if (! file_exists(resource_path("/static/img/PDF/{$templateName}.png"))) { + copy(public_path("/assets/img/PDF/{$templateName}.png"), public_path("/build/img/PDF/{$templateName}.png")); + copy(public_path("/assets/img/PDF/{$templateName}.png"), resource_path("/static/img/PDF/{$templateName}.png")); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}