diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 6033dabcdc32..fd934399fe43 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -28,10 +28,14 @@ use App\Models\Invoice; use App\Models\InvoiceInvitation; use App\Models\Payment; use App\Models\Paymentable; +use App\Models\PurchaseOrder; +use App\Models\PurchaseOrderInvitation; +use App\Models\Quote; use App\Models\QuoteInvitation; use App\Models\RecurringInvoiceInvitation; use App\Models\User; use App\Models\Vendor; +use App\Models\VendorContact; use App\Utils\Ninja; use Exception; use Illuminate\Console\Command; @@ -397,29 +401,49 @@ class CheckData extends Command QuoteInvitation::where('deleted_at',"0000-00-00 00:00:00.000000")->withTrashed()->update(['deleted_at' => null]); CreditInvitation::where('deleted_at',"0000-00-00 00:00:00.000000")->withTrashed()->update(['deleted_at' => null]); - $entities = ['invoice', 'quote', 'credit', 'recurring_invoice']; - foreach($entities as $entity) - { - $table = "{$entity}s"; - $invitation_table = "{$entity}_invitations"; + collect([Invoice::class, Quote::class, Credit::class, PurchaseOrder::class])->each(function ($entity){ - $entities = DB::table($table) - ->leftJoin($invitation_table, function ($join) use($invitation_table, $table, $entity){ - $join->on("{$invitation_table}.{$entity}_id", '=', "{$table}.id"); - // ->whereNull("{$invitation_table}.deleted_at"); - }) - ->groupBy("{$table}.id", "{$table}.user_id", "{$table}.company_id", "{$table}.client_id") - ->havingRaw("count({$invitation_table}.id) = 0") - ->get(["{$table}.id", "{$table}.user_id", "{$table}.company_id", "{$table}.client_id"]); + if($entity::doesntHave('invitations')->count() > 0) + { + $entity::doesntHave('invitations')->cursor()->each(function ($entity) { - $this->logMessage($entities->count()." {$table} without any invitations"); + $client_vendor_key = 'client_id'; + $contact_id = 'client_contact_id'; + $contact_class = ClientContact::class; - if ($this->option('fix') == 'true') - $this->fixInvitations($entities, $entity); + $entity_key = \Illuminate\Support\Str::of(class_basename($entity))->snake()->append('_id')->value; + $entity_obj = get_class($entity).'Invitation'; - } + if($entity instanceof PurchaseOrder){ + $client_vendor_key = 'vendor_id'; + $contact_id = 'vendor_contact_id'; + $contact_class = VendorContact::class; + } + + $invitation = new $entity_obj(); + $invitation->company_id = $entity->company_id; + $invitation->user_id = $entity->user_id; + $invitation->{$entity_key} = $entity->id; + $invitation->{$contact_id} = $contact_class::where('company_id', $entity->company_id)->where($client_vendor_key,$entity->{$client_vendor_key})->first()->id; + $invitation->key = Str::random(config('ninja.key_length')); + + $this->logMessage("Add invitation for {$entity_key} - {$entity->id}"); + + try{ + $invitation->save(); + } + catch(\Exception $e){ + $this->logMessage($e->getMessage()); + $invitation = null; + } + + }); + + } + + }); } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 28664dd3b9aa..14a1c388c3d8 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -42,6 +42,17 @@ class AppServiceProvider extends ServiceProvider public function boot() { + // DB::listen(function($query) { + // nlog( + // $query->sql, + // [ + // 'bindings' => $query->bindings, + // 'time' => $query->time + // ] + // ); + // }); + + Relation::morphMap([ 'invoices' => Invoice::class, 'proposals' => Proposal::class,