From f3dad8d48b59fe838f73a7528b969843478b2306 Mon Sep 17 00:00:00 2001 From: Mohit Panjwani Date: Tue, 30 Nov 2021 21:55:25 +0530 Subject: [PATCH] fix issues --- app/Models/User.php | 106 ++++++++---------- ...3244_add_base_columns_to_expense_table.php | 3 +- 2 files changed, 48 insertions(+), 61 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index ddb767c3..ca7d379f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -96,12 +96,7 @@ class User extends Authenticatable implements HasMedia public function estimates() { - return $this->hasMany(Estimate::class, 'creator_id'); - } - - public function customers() - { - return $this->hasMany(Customer::class, 'creator_id'); + return $this->hasMany(Estimate::class); } public function currency() @@ -119,29 +114,34 @@ class User extends Authenticatable implements HasMedia return $this->belongsToMany(Company::class, 'user_company', 'user_id', 'company_id'); } - public function recurringInvoices() + public function addresses() { - return $this->hasMany(RecurringInvoice::class, 'creator_id'); + return $this->hasMany(Address::class); } public function expenses() { - return $this->hasMany(Expense::class, 'creator_id'); + 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); } public function payments() { - return $this->hasMany(Payment::class, 'creator_id'); + return $this->hasMany(Payment::class); } public function invoices() { - return $this->hasMany(Invoice::class, 'creator_id'); - } - - public function items() - { - return $this->hasMany(Item::class, 'creator_id'); + return $this->hasMany(Invoice::class); } public function settings() @@ -255,6 +255,37 @@ 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(); @@ -454,47 +485,4 @@ 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; - } } diff --git a/database/migrations/2021_08_19_063244_add_base_columns_to_expense_table.php b/database/migrations/2021_08_19_063244_add_base_columns_to_expense_table.php index db9f57c6..63c327db 100644 --- a/database/migrations/2021_08_19_063244_add_base_columns_to_expense_table.php +++ b/database/migrations/2021_08_19_063244_add_base_columns_to_expense_table.php @@ -16,8 +16,7 @@ class AddBaseColumnsToExpenseTable extends Migration Schema::table('expenses', function (Blueprint $table) { $table->decimal('exchange_rate', 19, 6)->nullable(); $table->unsignedBigInteger('base_amount')->nullable(); - $table->unsignedInteger('currency_id'); - $table->foreign('currency_id')->references('id')->on('currencies'); + $table->unsignedInteger('currency_id')->nullable(); }); }