fix issues

This commit is contained in:
Mohit Panjwani
2021-11-30 21:55:25 +05:30
parent 9629771d22
commit f3dad8d48b
2 changed files with 48 additions and 61 deletions

View File

@ -96,12 +96,7 @@ class User extends Authenticatable implements HasMedia
public function estimates() public function estimates()
{ {
return $this->hasMany(Estimate::class, 'creator_id'); return $this->hasMany(Estimate::class);
}
public function customers()
{
return $this->hasMany(Customer::class, 'creator_id');
} }
public function currency() public function currency()
@ -119,29 +114,34 @@ class User extends Authenticatable implements HasMedia
return $this->belongsToMany(Company::class, 'user_company', 'user_id', 'company_id'); 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() 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() public function payments()
{ {
return $this->hasMany(Payment::class, 'creator_id'); return $this->hasMany(Payment::class);
} }
public function invoices() public function invoices()
{ {
return $this->hasMany(Invoice::class, 'creator_id'); return $this->hasMany(Invoice::class);
}
public function items()
{
return $this->hasMany(Item::class, 'creator_id');
} }
public function settings() 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() public function getAvatarAttribute()
{ {
$avatar = $this->getMedia('admin_avatar')->first(); $avatar = $this->getMedia('admin_avatar')->first();
@ -454,47 +485,4 @@ class User extends Authenticatable implements HasMedia
return false; 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;
}
} }

View File

@ -16,8 +16,7 @@ class AddBaseColumnsToExpenseTable extends Migration
Schema::table('expenses', function (Blueprint $table) { Schema::table('expenses', function (Blueprint $table) {
$table->decimal('exchange_rate', 19, 6)->nullable(); $table->decimal('exchange_rate', 19, 6)->nullable();
$table->unsignedBigInteger('base_amount')->nullable(); $table->unsignedBigInteger('base_amount')->nullable();
$table->unsignedInteger('currency_id'); $table->unsignedInteger('currency_id')->nullable();
$table->foreign('currency_id')->references('id')->on('currencies');
}); });
} }