diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 6f74fb8a250d..50ec11688f70 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -299,6 +299,9 @@ class CheckData extends Command foreach (Client::cursor() as $client) { $invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); + $credit_balance = $client->credits->where('is_deleted', false)->sum('balance'); + + $invoice_balance += $credit_balance; $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); @@ -385,6 +388,9 @@ class CheckData extends Command foreach (Client::cursor() as $client) { //$invoice_balance = $client->invoices->where('is_deleted', false)->where('status_id', '>', 1)->sum('balance'); $invoice_balance = Invoice::where('client_id', $client->id)->where('is_deleted', false)->where('status_id', '>', 1)->withTrashed()->sum('balance'); + $client_balance = Credit::where('client_id', $client->id)->where('is_deleted', false)->withTrashed()->sum('balance'); + + $invoice_balance += $client_balance; $ledger = CompanyLedger::where('client_id', $client->id)->orderBy('id', 'DESC')->first(); diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index dd5bccf38bbf..5c1e6f1b51ac 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -74,6 +74,7 @@ use App\Repositories\VendorRepository; use App\Utils\Traits\CleanLineItems; use App\Utils\Traits\CompanyGatewayFeesAndLimitsSaver; use App\Utils\Traits\MakesHash; +use App\Utils\Traits\SavesDocuments; use App\Utils\Traits\Uploadable; use Exception; use Illuminate\Bus\Queueable; @@ -93,6 +94,7 @@ class Import implements ShouldQueue use MakesHash; use CleanLineItems; use Uploadable; + use SavesDocuments; /** * @var array */ @@ -126,7 +128,7 @@ class Import implements ShouldQueue 'task_statuses', 'expenses', 'tasks', - // //'documents', + 'documents', ]; /** @@ -150,7 +152,7 @@ class Import implements ShouldQueue public $tries = 1; - public $timeout = 864000; + public $timeout = 0; // public $backoff = 86430; @@ -184,8 +186,6 @@ class Import implements ShouldQueue $array = json_decode(file_get_contents($this->file_path), 1); $data = $array['data']; -info(array_keys($data)); - foreach ($this->available_imports as $import) { info("the key = {$import}"); @@ -903,10 +903,11 @@ info(array_keys($data)); private function processDocuments(array $data): void { - Document::unguard(); + // Document::unguard(); /* No validators since data provided by database is already valid. */ - foreach ($data as $resource) { + foreach($data as $resource) + { $modified = $resource; if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && ! array_key_exists('invoices', $this->ids)) { @@ -917,42 +918,67 @@ info(array_keys($data)); throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - expenses.'); } - /* Remove because of polymorphic joins. */ - unset($modified['invoice_id']); - unset($modified['expense_id']); - if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && array_key_exists('invoices', $this->ids)) { - $modified['documentable_id'] = $this->transformId('invoices', $resource['invoice_id']); - $modified['documentable_type'] = Invoice::class; + $invoice_id = $this->transformId('invoices', $resource['invoice_id']); + $entity = Invoice::where('id', $invoice_id)->withTrashed()->first(); } if (array_key_exists('expense_id', $resource) && $resource['expense_id'] && array_key_exists('expenses', $this->ids)) { - $modified['documentable_id'] = $this->transformId('expenses', $resource['expense_id']); - $modified['documentable_type'] = Expense::class; + $expense_id = $this->transformId('expenses', $resource['expense_id']); + $entity = Expense::where('id', $expense_id)->withTrashed()->first(); } - $modified['user_id'] = $this->processUserId($resource); - $modified['company_id'] = $this->company->id; + $this->saveDocument(file_get_contents($resource['url']), $entity, $is_public = true); - $document = Document::create($modified); - - // $entity = $modified['documentable_type']::find($modified['documentable_id']); - // $entity->documents()->save($modified); - - $old_user_key = array_key_exists('user_id', $resource) ?? $this->user->id; - - $this->ids['documents'] = [ - "documents_{$old_user_key}" => [ - 'old' => $resource['id'], - 'new' => $document->id, - ], - ]; } - Document::reguard(); + // foreach ($data as $resource) { + // $modified = $resource; - /*Improve memory handling by setting everything to null when we have finished*/ - $data = null; + // if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && ! array_key_exists('invoices', $this->ids)) { + // throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - invoices.'); + // } + + // if (array_key_exists('expense_id', $resource) && $resource['expense_id'] && ! array_key_exists('expenses', $this->ids)) { + // throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - expenses.'); + // } + + // /* Remove because of polymorphic joins. */ + // unset($modified['invoice_id']); + // unset($modified['expense_id']); + + // if (array_key_exists('invoice_id', $resource) && $resource['invoice_id'] && array_key_exists('invoices', $this->ids)) { + // $modified['documentable_id'] = $this->transformId('invoices', $resource['invoice_id']); + // $modified['documentable_type'] = Invoice::class; + // } + + // if (array_key_exists('expense_id', $resource) && $resource['expense_id'] && array_key_exists('expenses', $this->ids)) { + // $modified['documentable_id'] = $this->transformId('expenses', $resource['expense_id']); + // $modified['documentable_type'] = Expense::class; + // } + + // $modified['user_id'] = $this->processUserId($resource); + // $modified['company_id'] = $this->company->id; + + // $document = Document::create($modified); + + // // $entity = $modified['documentable_type']::find($modified['documentable_id']); + // // $entity->documents()->save($modified); + + // $old_user_key = array_key_exists('user_id', $resource) ?? $this->user->id; + + // $this->ids['documents'] = [ + // "documents_{$old_user_key}" => [ + // 'old' => $resource['id'], + // 'new' => $document->id, + // ], + // ]; + // } + + // Document::reguard(); + + // /*Improve memory handling by setting everything to null when we have finished*/ + // $data = null; } private function processPaymentTerms(array $data) :void diff --git a/app/Jobs/Util/StartMigration.php b/app/Jobs/Util/StartMigration.php index f80e963c6adb..f5500149c5f4 100644 --- a/app/Jobs/Util/StartMigration.php +++ b/app/Jobs/Util/StartMigration.php @@ -53,7 +53,7 @@ class StartMigration implements ShouldQueue */ public $tries = 1; - public $timeout = 864000; + public $timeout = 0; // public $maxExceptions = 2; diff --git a/app/Models/Company.php b/app/Models/Company.php index df64f0716182..e7e333282d61 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -46,8 +46,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Notifications\Notification; use Illuminate\Support\Facades\Log; use Laracasts\Presenter\PresentableTrait; -use Staudenmeir\EloquentHasManyDeep\HasRelationships; -use Staudenmeir\EloquentHasManyDeep\HasTableAlias; class Company extends BaseModel { @@ -55,8 +53,6 @@ class Company extends BaseModel use MakesHash; use CompanySettingsSaver; use ThrottlesEmail; - use HasRelationships; - use HasTableAlias; const ENTITY_RECURRING_INVOICE = 'recurring_invoice'; const ENTITY_CREDIT = 'credit'; diff --git a/app/Models/CompanyUser.php b/app/Models/CompanyUser.php index 8655e398be78..90386e136258 100644 --- a/app/Models/CompanyUser.php +++ b/app/Models/CompanyUser.php @@ -13,11 +13,10 @@ namespace App\Models; use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\SoftDeletes; -use Staudenmeir\EloquentHasManyDeep\HasRelationships; class CompanyUser extends Pivot { - use HasRelationships; + use SoftDeletes; // protected $guarded = ['id']; @@ -95,7 +94,6 @@ class CompanyUser extends Pivot //return $this->hasMany(CompanyToken::class); //return $this->hasOne(CompanyToken::class, 'user_id', 'user_id','company_id', 'company_id'); - //return $this->hasOneDeep(CompanyToken::class, [CompanyUser::class], ['user_id','company_id'], ['company_id','company_id']); //return $this->belongsTo(CompanyToken::class, 'user_id', 'user_id'); diff --git a/app/Models/User.php b/app/Models/User.php index 34505645c4a6..00653a53b18f 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -33,7 +33,6 @@ use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Laracasts\Presenter\PresentableTrait; -use Staudenmeir\EloquentHasManyDeep\HasRelationships; class User extends Authenticatable implements MustVerifyEmail { @@ -44,7 +43,6 @@ class User extends Authenticatable implements MustVerifyEmail use UserSessionAttributes; use UserSettings; use Filterable; - use HasRelationships; use HasFactory; protected $guard = 'user'; diff --git a/app/Utils/PhantomJS/Phantom.php b/app/Utils/PhantomJS/Phantom.php index 9c67bac1a6a4..fb95c27556ae 100644 --- a/app/Utils/PhantomJS/Phantom.php +++ b/app/Utils/PhantomJS/Phantom.php @@ -68,6 +68,7 @@ class Phantom $file_path = $path.$entity_obj->number.'.pdf'; $url = config('ninja.app_url').'phantom/'.$entity.'/'.$invitation->key.'?phantomjs_secret='.config('ninja.phantomjs_secret'); +info($url); $key = config('ninja.phantomjs_key'); $secret = config('ninja.phantomjs_key'); diff --git a/app/Utils/Traits/SavesDocuments.php b/app/Utils/Traits/SavesDocuments.php index 6afb48c5313f..2ce496f9b378 100644 --- a/app/Utils/Traits/SavesDocuments.php +++ b/app/Utils/Traits/SavesDocuments.php @@ -43,4 +43,30 @@ trait SavesDocuments ); } } + + public function saveDocument($document, $entity, $is_public = true) + { + if ($entity instanceof Company) { + $account = $entity->account; + $company = $entity; + } else { + $account = $entity->company->account; + $company = $entity->company; + } + + if (! $account->hasFeature(Account::FEATURE_DOCUMENTS)) { + return false; + } + + $document = UploadFile::dispatchNow( + $document, + UploadFile::DOCUMENT, + $entity->user, + $entity->company, + $entity, + null, + $is_public + ); + + } } diff --git a/composer.json b/composer.json index d64163f2f0b6..21f49728cad5 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,6 @@ "fzaninotto/faker": "^1.4", "google/apiclient": "^2.7", "guzzlehttp/guzzle": "^7.0.1", - "halaxa/json-machine": "^0.4.0", "hashids/hashids": "^3.0", "intervention/image": "^2.5", "laracasts/presenter": "^0.2.1", @@ -59,7 +58,6 @@ "predis/predis": "^1.1", "sentry/sentry-laravel": "^2", "spatie/browsershot": "^3.37", - "staudenmeir/eloquent-has-many-deep": "^1.11", "stripe/stripe-php": "^7.50", "turbo124/beacon": "^1", "turbo124/laravel-gmail": "^5.0", diff --git a/config/queue.php b/config/queue.php index b706b148f30c..c7961a8a9b46 100644 --- a/config/queue.php +++ b/config/queue.php @@ -38,14 +38,14 @@ return [ 'driver' => 'database', 'table' => 'jobs', 'queue' => 'default', - 'retry_after' => 90, + 'retry_after' => 900000, ], 'beanstalkd' => [ 'driver' => 'beanstalkd', 'host' => 'localhost', 'queue' => 'default', - 'retry_after' => 90, + 'retry_after' => 900000, 'block_for' => 0, ], @@ -63,7 +63,7 @@ return [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('REDIS_QUEUE', 'default'), - 'retry_after' => 90, + 'retry_after' => 900000, 'block_for' => null, ],