Merge branch 'user-delete-issue' into 'master'

solve delete users and create template issue

See merge request mohit.panjvani/crater-web!1310
This commit is contained in:
Mohit Panjwani
2021-12-02 10:08:24 +00:00
3 changed files with 108 additions and 50 deletions

View File

@ -52,6 +52,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");

View File

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

View File

@ -0,0 +1,45 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Str;
class MigrateTemplatesFromVersion4 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$templates = Storage::disk('views')->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()
{
//
}
}