2020-01-23 21:35:00 +01:00
< ? php
2020-04-30 13:45:47 +02:00
/**
2020-09-06 11:38:10 +02:00
* Invoice Ninja ( https :// invoiceninja . com ) .
2020-04-30 13:45:47 +02:00
*
* @ link https :// github . com / invoiceninja / invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @ copyright Copyright ( c ) 2023. Invoice Ninja LLC ( https :// invoiceninja . com )
2020-04-30 13:45:47 +02:00
*
2021-06-16 08:58:16 +02:00
* @ license https :// www . elastic . co / licensing / elastic - license
2020-04-30 13:45:47 +02:00
*/
2020-01-23 21:35:00 +01:00
namespace App\Jobs\Util ;
2023-10-26 04:57:44 +02:00
use App\DataMapper\Analytics\MigrationFailure ;
use App\DataMapper\CompanySettings ;
use App\Exceptions\ClientHostedMigrationException ;
use App\Exceptions\MigrationValidatorFailed ;
use App\Exceptions\ResourceDependencyMissing ;
2023-06-29 08:38:33 +02:00
use App\Factory\ClientFactory ;
2023-10-26 04:57:44 +02:00
use App\Factory\CompanyLedgerFactory ;
2023-06-29 08:38:33 +02:00
use App\Factory\CreditFactory ;
use App\Factory\InvoiceFactory ;
use App\Factory\PaymentFactory ;
use App\Factory\ProductFactory ;
2023-10-26 04:57:44 +02:00
use App\Factory\QuoteFactory ;
use App\Factory\RecurringInvoiceFactory ;
2023-06-29 08:38:33 +02:00
use App\Factory\TaxRateFactory ;
2023-10-26 04:57:44 +02:00
use App\Factory\UserFactory ;
use App\Factory\VendorFactory ;
use App\Http\Requests\Company\UpdateCompanyRequest ;
use App\Http\ValidationRules\ValidCompanyGatewayFeesAndLimitsRule ;
use App\Http\ValidationRules\ValidUserForCompany ;
use App\Jobs\Company\CreateCompanyToken ;
2023-06-29 08:38:33 +02:00
use App\Jobs\Mail\NinjaMailerJob ;
use App\Jobs\Mail\NinjaMailerObject ;
use App\Jobs\Ninja\CheckCompanyData ;
2023-10-26 04:57:44 +02:00
use App\Libraries\MultiDB ;
use App\Mail\Migration\StripeConnectMigration ;
use App\Mail\MigrationCompleted ;
use App\Models\Activity ;
use App\Models\Client ;
use App\Models\ClientContact ;
use App\Models\ClientGatewayToken ;
use App\Models\Company ;
use App\Models\CompanyGateway ;
use App\Models\Credit ;
use App\Models\Document ;
use App\Models\Expense ;
use App\Models\ExpenseCategory ;
use App\Models\Invoice ;
use App\Models\Payment ;
use App\Models\PaymentTerm ;
use App\Models\Product ;
use App\Models\Project ;
use App\Models\Quote ;
use App\Models\RecurringExpense ;
use App\Models\RecurringInvoice ;
use App\Models\Task ;
use App\Models\TaskStatus ;
use App\Models\TaxRate ;
use App\Models\User ;
use App\Models\Vendor ;
use App\Repositories\ClientContactRepository ;
2023-06-29 08:38:33 +02:00
use App\Repositories\ClientRepository ;
use App\Repositories\CompanyRepository ;
2023-10-26 04:57:44 +02:00
use App\Repositories\CreditRepository ;
use App\Repositories\Migration\InvoiceMigrationRepository ;
use App\Repositories\Migration\PaymentMigrationRepository ;
2023-06-29 08:38:33 +02:00
use App\Repositories\ProductRepository ;
2023-10-26 04:57:44 +02:00
use App\Repositories\UserRepository ;
use App\Repositories\VendorContactRepository ;
use App\Repositories\VendorRepository ;
use App\Utils\Ninja ;
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 ;
2020-01-23 21:35:00 +01:00
use Illuminate\Contracts\Queue\ShouldQueue ;
use Illuminate\Foundation\Bus\Dispatchable ;
2023-10-26 04:57:44 +02:00
use Illuminate\Http\UploadedFile ;
use Illuminate\Queue\InteractsWithQueue ;
2022-06-08 06:25:44 +02:00
use Illuminate\Queue\Middleware\WithoutOverlapping ;
2023-10-26 04:57:44 +02:00
use Illuminate\Queue\SerializesModels ;
use Illuminate\Support\Carbon ;
use Illuminate\Support\Facades\App ;
use Illuminate\Support\Facades\Mail ;
use Illuminate\Support\Facades\Validator ;
use Illuminate\Support\Str ;
use Turbo124\Beacon\Facades\LightLogs ;
2020-01-23 21:35:00 +01:00
class Import implements ShouldQueue
{
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels ;
2020-02-24 11:15:30 +01:00
use CompanyGatewayFeesAndLimitsSaver ;
2020-03-31 13:52:21 +02:00
use MakesHash ;
2020-04-01 23:18:17 +02:00
use CleanLineItems ;
2020-11-04 01:32:18 +01:00
use Uploadable ;
2020-11-24 11:12:05 +01:00
use SavesDocuments ;
2023-02-09 02:10:08 +01:00
2023-06-08 09:16:24 +02:00
private string $file_path ; //the file path - using a different JSON parser here.
2020-01-23 21:35:00 +01:00
/**
* @ var Company
*/
private $company ;
2021-01-10 11:17:18 +01:00
private $token ;
2020-01-23 21:35:00 +01:00
/**
* @ var array
*/
private $available_imports = [
2020-12-14 22:52:14 +01:00
'account' ,
2020-03-03 23:44:42 +01:00
'company' ,
'users' ,
2020-05-27 01:49:06 +02:00
'payment_terms' ,
2020-03-03 23:44:42 +01:00
'tax_rates' ,
'clients' ,
2020-12-28 07:04:24 +01:00
'company_gateways' ,
'client_gateway_tokens' ,
2020-10-30 22:13:02 +01:00
'vendors' ,
2020-10-30 13:01:30 +01:00
'projects' ,
2020-10-06 02:52:16 +02:00
'products' ,
2020-11-23 13:55:04 +01:00
'credits' ,
2020-10-02 00:19:42 +02:00
'recurring_invoices' ,
2021-08-27 12:45:09 +02:00
'invoices' ,
2020-10-06 02:52:16 +02:00
'quotes' ,
2020-10-18 09:46:10 +02:00
'payments' ,
2020-10-30 10:17:29 +01:00
'expense_categories' ,
'task_statuses' ,
'expenses' ,
2021-08-29 14:18:15 +02:00
'recurring_expenses' ,
2020-10-30 10:17:29 +01:00
'tasks' ,
2021-01-09 12:10:04 +01:00
'documents' ,
2023-06-25 11:04:06 +02:00
'activities' ,
2020-01-23 21:35:00 +01:00
];
/**
* @ var User
*/
private $user ;
/**
* Custom list of resources to be imported .
*
* @ var array
*/
private $resources ;
/**
* Local state manager for ids .
*
* @ var array
*/
private $ids = [];
2020-11-18 11:46:36 +01:00
public $tries = 1 ;
2020-03-04 05:06:27 +01:00
2022-12-08 00:38:52 +01:00
public $timeout = 10000000 ;
2020-04-01 10:54:22 +02:00
2023-07-10 04:55:21 +02:00
public $silent_migration ;
2020-11-23 04:51:49 +01:00
// public $backoff = 86430;
2020-04-01 10:54:22 +02:00
2020-11-25 15:19:52 +01:00
// public $maxExceptions = 2;
2020-01-23 21:35:00 +01:00
/**
* Create a new job instance .
*
2023-08-07 07:30:34 +02:00
* @ param string $file_path
2020-01-23 21:35:00 +01:00
* @ param Company $company
* @ param User $user
* @ param array $resources
2023-08-07 07:30:34 +02:00
* @ param bool $silent_migration
2020-01-23 21:35:00 +01:00
*/
2023-07-10 04:55:21 +02:00
public function __construct ( string $file_path , Company $company , User $user , array $resources = [], $silent_migration = false )
2020-01-23 21:35:00 +01:00
{
2020-11-24 06:11:20 +01:00
$this -> file_path = $file_path ;
2020-01-23 21:35:00 +01:00
$this -> company = $company ;
$this -> user = $user ;
$this -> resources = $resources ;
2023-07-10 04:55:21 +02:00
$this -> silent_migration = $silent_migration ;
2020-01-23 21:35:00 +01:00
}
2023-02-09 02:10:08 +01:00
public function middleware ()
2023-02-16 02:36:09 +01:00
{
2023-06-26 13:05:15 +02:00
return [( new WithoutOverlapping ( $this -> company -> company_key ))];
2023-02-09 02:10:08 +01:00
}
2022-06-08 06:25:44 +02:00
2020-01-23 21:35:00 +01:00
/**
* Execute the job .
*
*/
2020-11-25 15:19:52 +01:00
public function handle ()
2020-01-23 21:35:00 +01:00
{
2020-05-06 13:49:42 +02:00
set_time_limit ( 0 );
2021-07-08 03:48:11 +02:00
nlog ( " Starting Migration " );
nlog ( $this -> user -> email );
2021-07-31 12:43:56 +02:00
nlog ( " Company ID = " );
nlog ( $this -> company -> id );
2021-07-08 03:48:11 +02:00
2020-11-30 08:43:33 +01:00
auth () -> login ( $this -> user , false );
2023-06-08 09:16:24 +02:00
/** @var \App\Models\User $user */
$user = auth () -> user ();
$user -> setCompany ( $this -> company );
2020-11-30 08:43:33 +01:00
2020-11-24 06:11:20 +01:00
$array = json_decode ( file_get_contents ( $this -> file_path ), 1 );
$data = $array [ 'data' ];
2020-11-23 13:55:04 +01:00
2020-11-24 06:11:20 +01:00
foreach ( $this -> available_imports as $import ) {
if ( ! array_key_exists ( $import , $data )) {
info ( " Resource { $import } is not available for migration. " );
2020-04-01 10:54:22 +02:00
continue ;
2020-03-03 23:44:42 +01:00
}
2020-03-05 21:30:32 +01:00
2020-11-24 06:11:20 +01:00
$method = sprintf ( 'process%s' , Str :: ucfirst ( Str :: camel ( $import )));
2020-01-23 21:35:00 +01:00
2020-11-24 06:11:20 +01:00
info ( " Importing { $import } " );
2020-04-01 10:54:22 +02:00
2020-11-24 06:11:20 +01:00
$this -> { $method }( $data [ $import ]);
2020-01-23 21:35:00 +01:00
}
2020-03-05 21:30:32 +01:00
2021-08-01 03:11:57 +02:00
$task_statuses = [
[ 'name' => ctrans ( 'texts.backlog' ), 'company_id' => $this -> company -> id , 'user_id' => $this -> user -> id , 'created_at' => now (), 'updated_at' => now (), 'status_order' => 1 ],
[ 'name' => ctrans ( 'texts.ready_to_do' ), 'company_id' => $this -> company -> id , 'user_id' => $this -> user -> id , 'created_at' => now (), 'updated_at' => now (), 'status_order' => 2 ],
[ 'name' => ctrans ( 'texts.in_progress' ), 'company_id' => $this -> company -> id , 'user_id' => $this -> user -> id , 'created_at' => now (), 'updated_at' => now (), 'status_order' => 3 ],
[ 'name' => ctrans ( 'texts.done' ), 'company_id' => $this -> company -> id , 'user_id' => $this -> user -> id , 'created_at' => now (), 'updated_at' => now (), 'status_order' => 4 ],
2021-05-12 12:46:59 +02:00
2021-08-01 03:11:57 +02:00
];
TaskStatus :: insert ( $task_statuses );
$account = $this -> company -> account ;
$account -> default_company_id = $this -> company -> id ;
2021-11-10 19:54:59 +01:00
$account -> is_migrated = true ;
2021-08-01 03:11:57 +02:00
$account -> save ();
//company size check
2021-09-11 13:16:43 +02:00
if ( $this -> company -> invoices () -> count () > 500 || $this -> company -> products () -> count () > 500 || $this -> company -> clients () -> count () > 500 ) {
2022-03-06 22:58:18 +01:00
$this -> company -> account -> companies () -> update ([ 'is_large' => true ]);
2021-08-01 03:11:57 +02:00
}
2021-06-21 04:58:52 +02:00
2021-10-08 11:51:55 +02:00
$this -> company -> client_registration_fields = \App\DataMapper\ClientRegistrationFields :: generate ();
$this -> company -> save ();
2020-07-01 07:51:19 +02:00
$this -> setInitialCompanyLedgerBalances ();
2020-12-02 04:08:35 +01:00
2021-02-04 21:47:16 +01:00
// $this->fixClientBalances();
2022-09-07 06:15:27 +02:00
$check_data = ( new CheckCompanyData ( $this -> company , md5 ( time ()))) -> handle ();
2021-04-27 12:38:34 +02:00
2021-08-01 03:11:57 +02:00
// if(Ninja::isHosted() && array_key_exists('ninja_tokens', $data))
$this -> processNinjaTokens ( $data [ 'ninja_tokens' ]);
// $this->fixData();
2023-02-16 02:36:09 +01:00
try {
2021-08-02 01:20:38 +02:00
App :: forgetInstance ( 'translator' );
$t = app ( 'translator' );
$t -> replace ( Ninja :: transformTranslations ( $this -> company -> settings ));
2023-10-26 04:57:44 +02:00
if ( ! $this -> silent_migration ) {
2023-07-10 04:55:21 +02:00
Mail :: to ( $this -> user -> email , $this -> user -> name ()) -> send ( new MigrationCompleted ( $this -> company -> id , $this -> company -> db , implode ( " <br> " , $check_data )));
2023-10-26 04:57:44 +02:00
}
2023-07-10 04:55:21 +02:00
2023-02-16 02:36:09 +01:00
} catch ( \Exception $e ) {
2021-03-16 12:29:16 +01:00
nlog ( $e -> getMessage ());
}
2020-07-19 09:17:19 +02:00
/*After a migration first some basic jobs to ensure the system is up to date*/
2023-02-16 02:36:09 +01:00
if ( Ninja :: isSelfHost ()) {
2022-11-08 22:09:42 +01:00
VersionCheck :: dispatch ();
2023-02-16 02:36:09 +01:00
}
2021-07-31 12:43:56 +02:00
2020-04-01 14:39:59 +02:00
info ( 'Completed🚀🚀🚀🚀🚀 at ' . now ());
2021-04-06 00:19:27 +02:00
2023-02-16 02:36:09 +01:00
try {
2022-07-05 00:26:41 +02:00
unlink ( $this -> file_path );
2023-02-16 02:36:09 +01:00
} catch ( \Exception $e ) {
2022-07-05 00:26:41 +02:00
nlog ( " problem unsetting file " );
}
2020-01-23 21:35:00 +01:00
}
2021-06-21 04:58:52 +02:00
private function fixData ()
{
$this -> company -> clients () -> withTrashed () -> where ( 'is_deleted' , 0 ) -> cursor () -> each ( function ( $client ) {
$total_invoice_payments = 0 ;
$credit_total_applied = 0 ;
foreach ( $client -> invoices () -> where ( 'is_deleted' , false ) -> where ( 'status_id' , '>' , 1 ) -> get () as $invoice ) {
2023-02-16 02:36:09 +01:00
$total_amount = $invoice -> payments () -> where ( 'is_deleted' , false ) -> whereIn ( 'status_id' , [ Payment :: STATUS_COMPLETED , Payment :: STATUS_PENDING , Payment :: STATUS_PARTIALLY_REFUNDED , Payment :: STATUS_REFUNDED ]) -> get () -> sum ( 'pivot.amount' );
$total_refund = $invoice -> payments () -> where ( 'is_deleted' , false ) -> whereIn ( 'status_id' , [ Payment :: STATUS_COMPLETED , Payment :: STATUS_PENDING , Payment :: STATUS_PARTIALLY_REFUNDED , Payment :: STATUS_REFUNDED ]) -> get () -> sum ( 'pivot.refunded' );
2021-06-21 04:58:52 +02:00
$total_invoice_payments += ( $total_amount - $total_refund );
}
// 10/02/21
foreach ( $client -> payments as $payment ) {
2023-09-04 02:27:28 +02:00
$credit_total_applied += $payment -> paymentables () -> where ( 'paymentable_type' , \App\Models\Credit :: class ) -> get () -> sum ( 'amount' );
2021-06-21 04:58:52 +02:00
}
if ( $credit_total_applied < 0 ) {
$total_invoice_payments += $credit_total_applied ;
2023-02-16 02:36:09 +01:00
}
2021-06-21 04:58:52 +02:00
if ( round ( $total_invoice_payments , 2 ) != round ( $client -> paid_to_date , 2 )) {
$client -> paid_to_date = $total_invoice_payments ;
2023-06-25 12:09:45 +02:00
$client -> saveQuietly ();
2021-06-21 04:58:52 +02:00
}
});
}
2020-07-01 07:51:19 +02:00
private function setInitialCompanyLedgerBalances ()
{
2023-08-07 07:30:34 +02:00
Client :: query () -> where ( 'company_id' , $this -> company -> id ) -> cursor () -> each ( function ( $client ) {
2021-02-14 10:55:04 +01:00
$invoice_balances = $client -> invoices -> where ( 'is_deleted' , false ) -> where ( 'status_id' , '>' , 1 ) -> sum ( 'balance' );
2020-07-01 07:51:19 +02:00
$company_ledger = CompanyLedgerFactory :: create ( $client -> company_id , $client -> user_id );
$company_ledger -> client_id = $client -> id ;
2021-02-14 10:55:04 +01:00
$company_ledger -> adjustment = $invoice_balances ;
2020-07-01 07:51:19 +02:00
$company_ledger -> notes = 'Migrated Client Balance' ;
2021-02-14 10:55:04 +01:00
$company_ledger -> balance = $invoice_balances ;
2020-07-01 08:03:46 +02:00
$company_ledger -> activity_id = Activity :: CREATE_CLIENT ;
2023-06-25 12:09:45 +02:00
$company_ledger -> saveQuietly ();
2020-07-01 07:51:19 +02:00
$client -> company_ledger () -> save ( $company_ledger );
2021-02-14 10:55:04 +01:00
$client -> balance = $invoice_balances ;
2023-06-25 12:09:45 +02:00
$client -> saveQuietly ();
2020-07-01 07:51:19 +02:00
});
}
2020-12-14 22:52:14 +01:00
private function processAccount ( array $data ) : void
{
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'token' , $data )) {
2021-01-10 11:17:18 +01:00
$this -> token = $data [ 'token' ];
unset ( $data [ 'token' ]);
}
2020-12-14 22:52:14 +01:00
$account = $this -> company -> account ;
2023-01-06 16:48:25 +01:00
/* If the user has upgraded their account, do not wipe their payment plan*/
2023-02-16 02:36:09 +01:00
if ( $account -> isPaid () || ( isset ( $data [ 'plan' ]) && $data [ 'plan' ] == 'white_label' )) {
if ( isset ( $data [ 'plan' ])) {
2023-01-06 16:48:25 +01:00
unset ( $data [ 'plan' ]);
2023-02-16 02:36:09 +01:00
}
2023-01-06 16:48:25 +01:00
2023-02-16 02:36:09 +01:00
if ( isset ( $data [ 'plan_term' ])) {
2023-01-06 16:48:25 +01:00
unset ( $data [ 'plan_term' ]);
2023-02-16 02:36:09 +01:00
}
2023-01-06 16:48:25 +01:00
2023-02-16 02:36:09 +01:00
if ( isset ( $data [ 'plan_paid' ])) {
2023-01-06 16:48:25 +01:00
unset ( $data [ 'plan_paid' ]);
2023-02-16 02:36:09 +01:00
}
2023-01-06 16:48:25 +01:00
2023-02-16 02:36:09 +01:00
if ( isset ( $data [ 'plan_started' ])) {
2023-01-06 16:48:25 +01:00
unset ( $data [ 'plan_started' ]);
2023-02-16 02:36:09 +01:00
}
2023-01-06 16:48:25 +01:00
2023-02-16 02:36:09 +01:00
if ( isset ( $data [ 'plan_expires' ])) {
2023-01-06 16:48:25 +01:00
unset ( $data [ 'plan_expires' ]);
2023-02-16 02:36:09 +01:00
}
2023-01-06 16:48:25 +01:00
}
2020-12-14 22:52:14 +01:00
$account -> fill ( $data );
$account -> save ();
2021-07-17 07:58:37 +02:00
//Prevent hosted users being pushed into a trial
2023-02-16 02:36:09 +01:00
if ( Ninja :: isHosted () && $account -> plan != '' ) {
2021-07-17 07:58:37 +02:00
$account -> trial_plan = '' ;
$account -> save ();
}
2020-12-14 22:52:14 +01:00
}
2020-01-23 21:35:00 +01:00
/**
* @ param array $data
2020-10-28 11:10:49 +01:00
* @ throws Exception
2020-01-23 21:35:00 +01:00
*/
private function processCompany ( array $data ) : void
{
Company :: unguard ();
2020-10-28 11:10:49 +01:00
2020-09-10 13:01:10 +02:00
if (
$data [ 'settings' ][ 'invoice_design_id' ] > 9 ||
$data [ 'settings' ][ 'invoice_design_id' ] > " 9 "
) {
$data [ 'settings' ][ 'invoice_design_id' ] = 1 ;
}
2023-02-16 02:36:09 +01:00
$data [ 'settings' ][ 'email_sending_method' ] = 'default' ;
2020-01-23 21:35:00 +01:00
2020-03-31 13:52:21 +02:00
$data = $this -> transformCompanyData ( $data );
2023-02-16 02:36:09 +01:00
if ( Ninja :: isHosted ()) {
2023-07-16 07:12:55 +02:00
2023-10-26 04:57:44 +02:00
$data [ 'subdomain' ] = str_replace ( " _ " , " " , $data [ 'subdomain' ]);
2023-07-16 07:12:55 +02:00
2023-02-16 02:36:09 +01:00
if ( ! MultiDB :: checkDomainAvailable ( $data [ 'subdomain' ])) {
2021-05-26 10:47:10 +02:00
$data [ 'subdomain' ] = MultiDB :: randomSubdomainGenerator ();
2023-02-16 02:36:09 +01:00
}
2021-06-25 11:58:35 +02:00
2023-02-16 02:36:09 +01:00
if ( strlen ( $data [ 'subdomain' ]) == 0 ) {
2021-06-25 11:58:35 +02:00
$data [ 'subdomain' ] = MultiDB :: randomSubdomainGenerator ();
2023-02-16 02:36:09 +01:00
}
2021-05-26 10:47:10 +02:00
}
2021-05-22 04:36:35 +02:00
2020-01-23 21:35:00 +01:00
$rules = ( new UpdateCompanyRequest ()) -> rules ();
$validator = Validator :: make ( $data , $rules );
2023-02-16 02:36:09 +01:00
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2023-02-16 02:36:09 +01:00
}
2021-02-04 01:07:21 +01:00
2023-02-16 02:36:09 +01:00
if ( isset ( $data [ 'account_id' ])) {
2020-01-23 21:35:00 +01:00
unset ( $data [ 'account_id' ]);
2023-02-16 02:36:09 +01:00
}
2021-02-04 01:07:21 +01:00
2023-02-16 02:36:09 +01:00
if ( isset ( $data [ 'version' ])) {
2021-02-04 01:07:21 +01:00
unset ( $data [ 'version' ]);
2023-02-16 02:36:09 +01:00
}
2020-01-23 21:35:00 +01:00
2020-09-06 11:38:10 +02:00
if ( isset ( $data [ 'referral_code' ])) {
2020-08-28 03:06:46 +02:00
$account = $this -> company -> account ;
$account -> referral_code = $data [ 'referral_code' ];
$account -> save ();
unset ( $data [ 'referral_code' ]);
}
2021-07-08 12:22:06 +02:00
if ( isset ( $data [ 'custom_fields' ]) && is_array ( $data [ 'custom_fields' ])) {
$data [ 'custom_fields' ] = $this -> parseCustomFields ( $data [ 'custom_fields' ]);
}
2020-01-23 21:35:00 +01:00
$company_repository = new CompanyRepository ();
$company_repository -> save ( $data , $this -> company );
2020-02-26 04:26:07 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $data [ 'settings' ] -> company_logo ) && strlen ( $data [ 'settings' ] -> company_logo ) > 0 ) {
2020-11-19 12:33:14 +01:00
try {
$tempImage = tempnam ( sys_get_temp_dir (), basename ( $data [ 'settings' ] -> company_logo ));
copy ( $data [ 'settings' ] -> company_logo , $tempImage );
$this -> uploadLogo ( $tempImage , $this -> company , $this -> company );
2020-11-25 15:19:52 +01:00
} catch ( \Exception $e ) {
2021-08-21 09:25:37 +02:00
$settings = $this -> company -> settings ;
$settings -> company_logo = '' ;
$this -> company -> settings = $settings ;
$this -> company -> save ();
2020-11-19 12:33:14 +01:00
}
2020-11-04 01:32:18 +01:00
}
2020-02-26 04:26:07 +01:00
Company :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$rules = null ;
$validator = null ;
$company_repository = null ;
2020-01-23 21:35:00 +01:00
}
2021-07-08 12:22:06 +02:00
private function parseCustomFields ( $fields ) : array
{
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'account1' , $fields )) {
2021-07-08 12:22:06 +02:00
$fields [ 'company1' ] = $fields [ 'account1' ];
2023-02-16 02:36:09 +01:00
}
2021-07-08 12:22:06 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'account2' , $fields )) {
2021-07-08 12:22:06 +02:00
$fields [ 'company2' ] = $fields [ 'account2' ];
2023-02-16 02:36:09 +01:00
}
2021-07-08 12:22:06 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'invoice1' , $fields )) {
2021-07-08 12:22:06 +02:00
$fields [ 'surcharge1' ] = $fields [ 'invoice1' ];
2023-02-16 02:36:09 +01:00
}
2021-07-08 12:22:06 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'invoice2' , $fields )) {
2021-07-08 12:22:06 +02:00
$fields [ 'surcharge2' ] = $fields [ 'invoice2' ];
2023-02-16 02:36:09 +01:00
}
2021-07-08 12:22:06 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'invoice_text1' , $fields )) {
2021-07-08 12:22:06 +02:00
$fields [ 'invoice1' ] = $fields [ 'invoice_text1' ];
2023-02-16 02:36:09 +01:00
}
2021-07-08 12:22:06 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'invoice_text2' , $fields )) {
2021-07-08 12:22:06 +02:00
$fields [ 'invoice2' ] = $fields [ 'invoice_text2' ];
2023-02-16 02:36:09 +01:00
}
2021-07-08 12:22:06 +02:00
foreach ( $fields as & $value ) {
$value = ( string ) $value ;
}
return $fields ;
}
2020-03-31 13:52:21 +02:00
private function transformCompanyData ( array $data ) : array
{
$company_settings = CompanySettings :: defaults ();
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'settings' , $data )) {
2020-03-31 13:52:21 +02:00
foreach ( $data [ 'settings' ] as $key => $value ) {
2020-04-01 14:39:59 +02:00
if ( $key == 'invoice_design_id' || $key == 'quote_design_id' || $key == 'credit_design_id' ) {
$value = $this -> encodePrimaryKey ( $value );
2021-07-01 05:34:57 +02:00
2023-02-16 02:36:09 +01:00
if ( ! $value ) {
2021-07-01 05:34:57 +02:00
$value = $this -> encodePrimaryKey ( 1 );
2023-02-16 02:36:09 +01:00
}
2020-04-01 10:54:22 +02:00
}
2022-01-06 03:08:19 +01:00
/* changes $key = '' to $value == '' and changed the return value from -1 to "0" 06/01/2022 */
if ( $key == 'payment_terms' && $value == '' ) {
$value = " 0 " ;
2020-04-01 10:54:22 +02:00
}
2020-03-31 13:52:21 +02:00
$company_settings -> { $key } = $value ;
2022-01-13 00:54:34 +01:00
2023-02-16 02:36:09 +01:00
if ( $key == 'payment_terms' ) {
2022-01-13 00:54:34 +01:00
settype ( $company_settings -> payment_terms , 'string' );
}
2020-03-31 13:52:21 +02:00
}
2023-02-16 02:36:09 +01:00
if ( Ninja :: isHosted ()) {
$data [ 'portal_mode' ] = 'subdomain' ;
$data [ 'portal_domain' ] = '' ;
}
2023-01-20 00:58:24 +01:00
2023-02-23 00:20:44 +01:00
$company_settings -> font_size = 16 ;
2020-03-31 13:52:21 +02:00
$data [ 'settings' ] = $company_settings ;
}
2021-07-17 14:01:00 +02:00
2020-03-31 13:52:21 +02:00
2020-04-01 14:39:59 +02:00
return $data ;
2020-03-31 13:52:21 +02:00
}
2020-01-23 21:35:00 +01:00
/**
* @ param array $data
2020-10-28 11:10:49 +01:00
* @ throws Exception
2020-01-23 21:35:00 +01:00
*/
private function processTaxRates ( array $data ) : void
{
TaxRate :: unguard ();
$rules = [
'*.name' => 'required' ,
//'*.name' => 'required|distinct|unique:tax_rates,name,null,null,company_id,' . $this->company->id,
'*.rate' => 'required|numeric' ,
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
foreach ( $data as $resource ) {
$modified = $resource ;
$company_id = $this -> company -> id ;
$user_id = $this -> processUserId ( $resource );
2020-03-21 06:37:30 +01:00
if ( isset ( $resource [ 'user_id' ])) {
2020-01-27 21:56:48 +01:00
unset ( $resource [ 'user_id' ]);
2020-03-21 06:37:30 +01:00
}
2020-01-23 21:35:00 +01:00
2020-03-21 06:37:30 +01:00
if ( isset ( $resource [ 'company_id' ])) {
2020-01-27 21:56:48 +01:00
unset ( $resource [ 'company_id' ]);
2020-03-21 06:37:30 +01:00
}
2020-01-23 21:35:00 +01:00
$tax_rate = TaxRateFactory :: create ( $this -> company -> id , $user_id );
$tax_rate -> fill ( $resource );
$tax_rate -> save ();
}
2020-02-26 04:26:07 +01:00
TaxRate :: reguard ();
2020-05-06 13:49:42 +02:00
2023-02-16 02:36:09 +01:00
if ( TaxRate :: count () > 0 ) {
2023-01-20 00:58:24 +01:00
$this -> company -> enabled_tax_rates = 2 ;
$this -> company -> save ();
}
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$rules = null ;
$validator = null ;
2020-01-23 21:35:00 +01:00
}
2023-02-09 03:51:03 +01:00
private function testUserDbLocationSanity ( array $data ) : bool
{
2023-02-16 02:36:09 +01:00
if ( Ninja :: isSelfHost ()) {
2023-02-09 03:51:03 +01:00
return true ;
2023-02-16 02:36:09 +01:00
}
2023-02-09 03:51:03 +01:00
$current_db = config ( 'database.default' );
2023-02-09 04:06:41 +01:00
$db1_count = User :: on ( 'db-ninja-01' ) -> withTrashed () -> whereIn ( 'email' , array_column ( $data , 'email' )) -> count ();
$db2_count = User :: on ( 'db-ninja-02' ) -> withTrashed () -> whereIn ( 'email' , array_column ( $data , 'email' )) -> count ();
2023-02-09 03:51:03 +01:00
2023-02-09 04:06:41 +01:00
MultiDB :: setDb ( $current_db );
2023-02-09 03:51:03 +01:00
2023-02-16 02:36:09 +01:00
if ( $db2_count == 0 && $db1_count == 0 ) {
2023-02-09 04:06:41 +01:00
return true ;
2023-02-16 02:36:09 +01:00
}
2023-02-09 03:51:03 +01:00
2023-02-16 02:36:09 +01:00
if ( $db1_count >= 1 && $db2_count >= 1 ) {
2023-02-09 04:06:41 +01:00
return false ;
2023-02-16 02:36:09 +01:00
}
2023-02-09 03:51:03 +01:00
return true ;
}
2020-01-23 21:35:00 +01:00
/**
* @ param array $data
2020-10-28 11:10:49 +01:00
* @ throws Exception
2020-01-23 21:35:00 +01:00
*/
private function processUsers ( array $data ) : void
{
2023-02-16 02:36:09 +01:00
if ( ! $this -> testUserDbLocationSanity ( $data )) {
2023-02-09 04:06:41 +01:00
throw new ClientHostedMigrationException ( 'You have users that belong to different accounts registered in the system, please contact us to resolve.' , 400 );
2023-02-16 02:36:09 +01:00
}
2023-02-09 03:51:03 +01:00
2020-01-23 21:35:00 +01:00
User :: unguard ();
$rules = [
'*.first_name' => [ 'string' ],
'*.last_name' => [ 'string' ],
2021-05-26 05:17:22 +02:00
'*.email' => [ 'distinct' , 'email' , new ValidUserForCompany ()],
2020-01-23 21:35:00 +01:00
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
$user_repository = new UserRepository ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
2022-11-04 06:24:57 +01:00
// unset($modified['password']); //cant import passwords.
2021-07-27 00:41:53 +02:00
unset ( $modified [ 'confirmation_code' ]); //cant import passwords.
2022-04-07 12:08:30 +02:00
unset ( $modified [ 'oauth_user_id' ]);
unset ( $modified [ 'oauth_provider_id' ]);
2020-04-15 00:37:27 +02:00
$user = $user_repository -> save ( $modified , $this -> fetchUser ( $resource [ 'email' ]), true , true );
2021-03-23 04:53:10 +01:00
$user -> email_verified_at = now ();
2021-05-11 13:26:55 +02:00
2023-02-16 02:36:09 +01:00
if ( $modified [ 'deleted_at' ]) {
2021-05-11 13:26:55 +02:00
$user -> deleted_at = now ();
2023-02-16 02:36:09 +01:00
}
2021-05-11 13:26:55 +02:00
2022-11-04 06:24:57 +01:00
$user -> password = $modified [ 'password' ];
2021-03-23 04:53:10 +01:00
$user -> save ();
2020-01-23 21:35:00 +01:00
$user_agent = array_key_exists ( 'token_name' , $resource ) ? : request () -> server ( 'HTTP_USER_AGENT' );
2022-09-07 06:15:27 +02:00
( new CreateCompanyToken ( $this -> company , $user , $user_agent )) -> handle ();
2020-01-23 21:35:00 +01:00
$key = " users_ { $resource [ 'id' ] } " ;
$this -> ids [ 'users' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $user -> id ,
];
}
2020-02-26 04:26:07 +01:00
User :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$rules = null ;
$validator = null ;
$user_repository = null ;
2020-01-23 21:35:00 +01:00
}
2021-05-06 05:22:55 +02:00
private function checkUniqueConstraint ( $model , $column , $value )
{
2021-05-10 01:52:58 +02:00
$value = trim ( $value );
2021-07-16 14:00:21 +02:00
$model_query = $model :: where ( $column , $value )
-> where ( 'company_id' , $this -> company -> id )
2021-07-17 12:36:51 +02:00
-> withTrashed ()
2021-07-16 14:00:21 +02:00
-> exists ();
2021-05-06 05:22:55 +02:00
2023-02-16 02:36:09 +01:00
if ( $model_query ) {
2021-07-16 14:00:21 +02:00
return $value . '_' . Str :: random ( 5 );
2023-02-16 02:36:09 +01:00
}
2021-05-06 05:22:55 +02:00
return $value ;
}
2020-01-23 21:35:00 +01:00
/**
* @ param array $data
2020-10-28 11:10:49 +01:00
* @ throws Exception
2020-01-23 21:35:00 +01:00
*/
private function processClients ( array $data ) : void
{
Client :: unguard ();
2020-04-01 14:39:59 +02:00
$contact_repository = new ClientContactRepository ();
2020-01-29 03:43:38 +01:00
$client_repository = new ClientRepository ( $contact_repository );
2020-01-23 21:35:00 +01:00
foreach ( $data as $key => $resource ) {
$modified = $resource ;
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-06-30 02:09:18 +02:00
$modified [ 'balance' ] = $modified [ 'balance' ] ? : 0 ;
2020-07-17 11:47:17 +02:00
$modified [ 'paid_to_date' ] = $modified [ 'paid_to_date' ] ? : 0 ;
2021-05-06 05:22:55 +02:00
$modified [ 'number' ] = $this -> checkUniqueConstraint ( Client :: class , 'number' , $modified [ 'number' ]);
2020-09-06 11:38:10 +02:00
2020-01-23 21:35:00 +01:00
unset ( $modified [ 'id' ]);
2020-01-29 03:43:38 +01:00
unset ( $modified [ 'contacts' ]);
2020-01-23 21:35:00 +01:00
2020-03-21 06:37:30 +01:00
$client = $client_repository -> save (
$modified ,
ClientFactory :: create (
2020-03-26 04:23:57 +01:00
$this -> company -> id ,
$modified [ 'user_id' ]
)
2020-01-23 21:35:00 +01:00
);
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$client -> created_at = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$client -> updated_at = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
2022-03-12 09:51:11 +01:00
$client -> country_id = array_key_exists ( 'country_id' , $modified ) ? $modified [ 'country_id' ] : $this -> company -> settings -> country_id ;
2021-05-05 11:46:51 +02:00
$client -> save ([ 'timestamps' => false ]);
2021-09-09 13:18:04 +02:00
$client -> fresh ();
2020-08-05 02:12:47 +02:00
$client -> contacts () -> forceDelete ();
2020-03-21 06:37:30 +01:00
if ( array_key_exists ( 'contacts' , $resource )) { // need to remove after importing new migration.json
2020-01-29 03:43:38 +01:00
$modified_contacts = $resource [ 'contacts' ];
2020-03-21 06:37:30 +01:00
foreach ( $modified_contacts as $key => $client_contacts ) {
2020-01-29 03:43:38 +01:00
$modified_contacts [ $key ][ 'company_id' ] = $this -> company -> id ;
$modified_contacts [ $key ][ 'user_id' ] = $this -> processUserId ( $resource );
$modified_contacts [ $key ][ 'client_id' ] = $client -> id ;
2023-02-16 02:36:09 +01:00
$modified_contacts [ $key ][ 'password' ] = Str :: random ( 8 );
2020-01-29 03:43:38 +01:00
unset ( $modified_contacts [ $key ][ 'id' ]);
}
2020-05-27 06:46:19 +02:00
$saveable_contacts [ 'contacts' ] = $modified_contacts ;
$contact_repository -> save ( $saveable_contacts , $client );
2020-12-29 00:44:24 +01:00
//link contact ids
2021-01-04 13:38:00 +01:00
foreach ( $resource [ 'contacts' ] as $key => $old_contact ) {
2021-01-19 23:35:52 +01:00
$contact_match = ClientContact :: where ( 'contact_key' , $old_contact [ 'contact_key' ])
-> where ( 'company_id' , $this -> company -> id )
-> where ( 'client_id' , $client -> id )
-> withTrashed ()
-> first ();
2020-12-29 00:44:24 +01:00
2021-01-04 13:38:00 +01:00
if ( $contact_match ) {
2020-12-29 00:44:24 +01:00
$this -> ids [ 'client_contacts' ][ 'client_contacts_' . $old_contact [ 'id' ]] = [
'old' => $old_contact [ 'id' ],
'new' => $contact_match -> id ,
];
}
}
2020-01-29 03:43:38 +01:00
}
2020-01-23 21:35:00 +01:00
$key = " clients_ { $resource [ 'id' ] } " ;
$this -> ids [ 'clients' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $client -> id ,
];
2021-09-09 13:18:04 +02:00
$client = null ;
2020-01-23 21:35:00 +01:00
}
2020-02-26 04:26:07 +01:00
Client :: reguard ();
2020-05-06 13:49:42 +02:00
2023-02-16 02:36:09 +01:00
Client :: withTrashed () -> with ( 'contacts' ) -> where ( 'company_id' , $this -> company -> id ) -> cursor () -> each ( function ( $client ) {
$contact = $client -> contacts -> sortByDesc ( 'is_primary' ) -> first ();
$contact -> is_primary = true ;
$contact -> save ();
2022-09-21 14:02:29 +02:00
});
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$contact_repository = null ;
$client_repository = null ;
2020-01-23 21:35:00 +01:00
}
2020-10-30 22:13:02 +01:00
/**
* @ param array $data
* @ throws Exception
*/
private function processVendors ( array $data ) : void
{
Vendor :: unguard ();
$contact_repository = new VendorContactRepository ();
$vendor_repository = new VendorRepository ( $contact_repository );
foreach ( $data as $key => $resource ) {
$modified = $resource ;
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2021-08-21 11:53:20 +02:00
$modified [ 'number' ] = $this -> checkUniqueConstraint ( Vendor :: class , 'number' , $modified [ 'number' ]);
2020-10-30 22:13:02 +01:00
unset ( $modified [ 'id' ]);
unset ( $modified [ 'contacts' ]);
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'created_at' ] = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'updated_at' ] = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-03-18 08:24:56 +01:00
if ( ! array_key_exists ( 'currency_id' , $modified ) || ! $modified [ 'currency_id' ]) {
2023-03-16 01:51:07 +01:00
$modified [ 'currency_id' ] = $this -> company -> settings -> currency_id ;
}
2020-10-31 01:46:00 +01:00
$vendor = $vendor_repository -> save (
2020-10-30 22:13:02 +01:00
$modified ,
VendorFactory :: create (
$this -> company -> id ,
$modified [ 'user_id' ]
)
);
2020-10-31 01:46:00 +01:00
$vendor -> contacts () -> forceDelete ();
2020-10-30 22:13:02 +01:00
if ( array_key_exists ( 'contacts' , $resource )) { // need to remove after importing new migration.json
$modified_contacts = $resource [ 'contacts' ];
2020-10-31 01:46:00 +01:00
foreach ( $modified_contacts as $key => $vendor_contacts ) {
2020-10-30 22:13:02 +01:00
$modified_contacts [ $key ][ 'company_id' ] = $this -> company -> id ;
$modified_contacts [ $key ][ 'user_id' ] = $this -> processUserId ( $resource );
2020-10-31 06:35:05 +01:00
$modified_contacts [ $key ][ 'vendor_id' ] = $vendor -> id ;
2020-10-30 22:13:02 +01:00
$modified_contacts [ $key ][ 'password' ] = 'mysuperpassword' ; // @todo, and clean up the code..
unset ( $modified_contacts [ $key ][ 'id' ]);
}
$saveable_contacts [ 'contacts' ] = $modified_contacts ;
2020-10-31 01:46:00 +01:00
$contact_repository -> save ( $saveable_contacts , $vendor );
2020-10-30 22:13:02 +01:00
}
$key = " vendors_ { $resource [ 'id' ] } " ;
$this -> ids [ 'vendors' ][ $key ] = [
'old' => $resource [ 'id' ],
2020-10-31 01:46:00 +01:00
'new' => $vendor -> id ,
2020-10-30 22:13:02 +01:00
];
}
Vendor :: reguard ();
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$contact_repository = null ;
$client_repository = null ;
}
2020-01-23 21:35:00 +01:00
private function processProducts ( array $data ) : void
{
Product :: unguard ();
$rules = [
//'*.product_key' => 'required|distinct|unique:products,product_key,null,null,company_id,' . $this->company->id,
'*.cost' => 'numeric' ,
'*.price' => 'numeric' ,
'*.quantity' => 'numeric' ,
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
$product_repository = new ProductRepository ();
foreach ( $data as $resource ) {
$modified = $resource ;
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'created_at' ] = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'updated_at' ] = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2020-01-23 21:35:00 +01:00
unset ( $modified [ 'id' ]);
2020-03-21 06:37:30 +01:00
$product_repository -> save (
$modified ,
ProductFactory :: create (
2020-03-26 04:23:57 +01:00
$this -> company -> id ,
$modified [ 'user_id' ]
)
2020-01-23 21:35:00 +01:00
);
}
2020-02-26 04:26:07 +01:00
Product :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$product_repository = null ;
2020-01-23 21:35:00 +01:00
}
2021-08-29 14:18:15 +02:00
private function processRecurringExpenses ( array $data ) : void
{
RecurringExpense :: unguard ();
$rules = [
'*.amount' => [ 'numeric' ],
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
}
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
if ( isset ( $resource [ 'client_id' ])) {
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
}
if ( isset ( $resource [ 'category_id' ])) {
$modified [ 'category_id' ] = $this -> transformId ( 'expense_categories' , $resource [ 'category_id' ]);
}
if ( isset ( $resource [ 'vendor_id' ])) {
$modified [ 'vendor_id' ] = $this -> transformId ( 'vendors' , $resource [ 'vendor_id' ]);
}
2023-08-07 07:30:34 +02:00
/** @var \App\Models\Expense $expense */
2021-08-29 14:18:15 +02:00
$expense = RecurringExpense :: create ( $modified );
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-08-29 14:18:15 +02:00
$expense -> created_at = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-08-29 14:18:15 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-08-29 14:18:15 +02:00
$expense -> updated_at = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-08-29 14:18:15 +02:00
$expense -> save ([ 'timestamps' => false ]);
$old_user_key = array_key_exists ( 'user_id' , $resource ) ? ? $this -> user -> id ;
$key = " recurring_expenses_ { $resource [ 'id' ] } " ;
$this -> ids [ 'recurring_expenses' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $expense -> id ,
];
}
RecurringExpense :: reguard ();
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
}
2020-10-02 00:19:42 +02:00
private function processRecurringInvoices ( array $data ) : void
{
RecurringInvoice :: unguard ();
$rules = [
'*.client_id' => [ 'required' ],
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
}
$invoice_repository = new InvoiceMigrationRepository ();
foreach ( $data as $key => $resource ) {
$modified = $resource ;
if ( array_key_exists ( 'client_id' , $resource ) && ! array_key_exists ( 'clients' , $this -> ids )) {
throw new ResourceDependencyMissing ( 'Processing invoices failed, because of missing dependency - clients.' );
}
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'line_items' ] = $this -> cleanItems ( $modified [ 'line_items' ]);
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'next_send_date' , $resource )) {
2022-07-02 03:40:51 +02:00
$modified [ 'next_send_date_client' ] = $resource [ 'next_send_date' ];
2023-02-16 02:36:09 +01:00
}
2022-07-02 03:40:51 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'created_at' ] = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'updated_at' ] = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2020-10-02 00:19:42 +02:00
unset ( $modified [ 'id' ]);
2021-01-04 13:38:00 +01:00
if ( array_key_exists ( 'invitations' , $resource )) {
foreach ( $resource [ 'invitations' ] as $key => $invite ) {
2020-12-29 00:44:24 +01:00
$resource [ 'invitations' ][ $key ][ 'client_contact_id' ] = $this -> transformId ( 'client_contacts' , $invite [ 'client_contact_id' ]);
$resource [ 'invitations' ][ $key ][ 'user_id' ] = $modified [ 'user_id' ];
$resource [ 'invitations' ][ $key ][ 'company_id' ] = $this -> company -> id ;
2021-05-03 06:02:55 +02:00
$resource [ 'invitations' ][ $key ][ 'email_status' ] = '' ;
2020-12-29 00:44:24 +01:00
unset ( $resource [ 'invitations' ][ $key ][ 'recurring_invoice_id' ]);
2021-04-16 05:58:14 +02:00
unset ( $resource [ 'invitations' ][ $key ][ 'id' ]);
2020-12-29 00:44:24 +01:00
}
2020-12-29 23:24:33 +01:00
2021-02-03 13:29:44 +01:00
$modified [ 'invitations' ] = $this -> deDuplicateInvitations ( $resource [ 'invitations' ]);
2020-12-29 22:58:48 +01:00
}
2020-10-02 00:19:42 +02:00
$invoice = $invoice_repository -> save (
$modified ,
RecurringInvoiceFactory :: create ( $this -> company -> id , $modified [ 'user_id' ])
);
2023-02-16 02:36:09 +01:00
if ( $invoice -> status_id == 4 && $invoice -> remaining_cycles == - 1 ) {
2022-04-26 05:21:59 +02:00
$invoice -> status_id = 2 ;
$invoice -> save ();
}
2020-10-02 00:19:42 +02:00
$key = " recurring_invoices_ { $resource [ 'id' ] } " ;
$this -> ids [ 'recurring_invoices' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $invoice -> id ,
];
}
RecurringInvoice :: reguard ();
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$invoice_repository = null ;
}
2020-01-23 21:35:00 +01:00
private function processInvoices ( array $data ) : void
{
Invoice :: unguard ();
$rules = [
'*.client_id' => [ 'required' ],
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
2020-07-01 06:37:05 +02:00
$invoice_repository = new InvoiceMigrationRepository ();
2020-01-23 21:35:00 +01:00
2020-05-06 13:49:42 +02:00
foreach ( $data as $key => $resource ) {
2020-01-23 21:35:00 +01:00
$modified = $resource ;
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'client_id' , $resource ) && ! array_key_exists ( 'clients' , $this -> ids )) {
2020-03-05 21:30:32 +01:00
throw new ResourceDependencyMissing ( 'Processing invoices failed, because of missing dependency - clients.' );
2020-01-23 21:35:00 +01:00
}
2020-09-06 11:38:10 +02:00
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
2021-08-29 14:39:50 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'recurring_id' , $resource ) && ! is_null ( $resource [ 'recurring_id' ])) {
2021-08-29 14:39:50 +02:00
$modified [ 'recurring_id' ] = $this -> transformId ( 'recurring_invoices' , ( string ) $resource [ 'recurring_id' ]);
2023-02-16 02:36:09 +01:00
}
2021-08-29 14:39:50 +02:00
2020-09-06 11:38:10 +02:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-01-23 21:35:00 +01:00
$modified [ 'company_id' ] = $this -> company -> id ;
2020-04-01 23:18:17 +02:00
$modified [ 'line_items' ] = $this -> cleanItems ( $modified [ 'line_items' ]);
2023-08-31 00:33:20 +02:00
//31/08-2023 set correct paid to date here:
$modified [ 'paid_to_date' ] = $modified [ 'amount' ] - $modified [ 'balance' ] ? ? 0 ;
2020-01-23 21:35:00 +01:00
unset ( $modified [ 'id' ]);
2020-12-29 00:44:24 +01:00
2021-01-04 13:38:00 +01:00
if ( array_key_exists ( 'invitations' , $resource )) {
foreach ( $resource [ 'invitations' ] as $key => $invite ) {
2020-12-29 00:44:24 +01:00
$resource [ 'invitations' ][ $key ][ 'client_contact_id' ] = $this -> transformId ( 'client_contacts' , $invite [ 'client_contact_id' ]);
$resource [ 'invitations' ][ $key ][ 'user_id' ] = $modified [ 'user_id' ];
$resource [ 'invitations' ][ $key ][ 'company_id' ] = $this -> company -> id ;
2021-05-03 06:02:55 +02:00
$resource [ 'invitations' ][ $key ][ 'email_status' ] = '' ;
2020-12-29 00:44:24 +01:00
unset ( $resource [ 'invitations' ][ $key ][ 'invoice_id' ]);
2021-04-16 05:58:14 +02:00
unset ( $resource [ 'invitations' ][ $key ][ 'id' ]);
2020-12-29 00:44:24 +01:00
}
2021-02-03 13:29:44 +01:00
$modified [ 'invitations' ] = $this -> deDuplicateInvitations ( $resource [ 'invitations' ]);
2020-12-29 21:37:48 +01:00
}
2021-02-03 13:29:44 +01:00
2020-01-23 21:35:00 +01:00
$invoice = $invoice_repository -> save (
2020-03-21 06:37:30 +01:00
$modified ,
InvoiceFactory :: create ( $this -> company -> id , $modified [ 'user_id' ])
2020-01-23 21:35:00 +01:00
);
$key = " invoices_ { $resource [ 'id' ] } " ;
$this -> ids [ 'invoices' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $invoice -> id ,
];
}
2020-02-26 04:26:07 +01:00
Invoice :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$invoice_repository = null ;
2020-01-23 21:35:00 +01:00
}
2021-02-03 13:29:44 +01:00
/* Prevent edge case where V4 has inserted multiple invitations for a resource for a client contact */
private function deDuplicateInvitations ( $invitations )
2023-02-16 02:36:09 +01:00
{
2021-02-03 13:29:44 +01:00
return array_intersect_key ( $invitations , array_unique ( array_column ( $invitations , 'client_contact_id' )));
}
2020-01-23 21:35:00 +01:00
private function processCredits ( array $data ) : void
{
Credit :: unguard ();
$rules = [
'*.client_id' => [ 'required' ],
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
$credit_repository = new CreditRepository ();
foreach ( $data as $resource ) {
$modified = $resource ;
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'client_id' , $resource ) && ! array_key_exists ( 'clients' , $this -> ids )) {
2020-03-05 21:30:32 +01:00
throw new ResourceDependencyMissing ( 'Processing credits failed, because of missing dependency - clients.' );
2020-01-23 21:35:00 +01:00
}
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
$modified [ 'company_id' ] = $this -> company -> id ;
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'created_at' ] = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'updated_at' ] = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2020-01-23 21:35:00 +01:00
unset ( $modified [ 'id' ]);
2023-07-12 13:19:27 +02:00
2020-01-27 21:56:48 +01:00
$credit = $credit_repository -> save (
2020-03-21 06:37:30 +01:00
$modified ,
CreditFactory :: create ( $this -> company -> id , $modified [ 'user_id' ])
2020-01-23 21:35:00 +01:00
);
2023-10-26 04:57:44 +02:00
if ( $credit -> status_id == 4 ) {
2023-07-12 13:19:27 +02:00
$client = $credit -> client ;
$client -> balance -= $credit -> balance ;
$client -> credit_balance -= $credit -> amount ;
$client -> saveQuietly ();
$credit -> paid_to_date = $credit -> amount ;
$credit -> balance = 0 ;
$credit -> saveQuietly ();
}
2021-02-04 21:47:16 +01:00
//remove credit balance from ledger
2023-02-16 02:36:09 +01:00
if ( $credit -> balance > 0 && $credit -> client -> balance > 0 ) {
2021-02-04 21:47:16 +01:00
$client = $credit -> client ;
$client -> balance -= $credit -> balance ;
$client -> save ();
}
2020-01-23 21:35:00 +01:00
$key = " credits_ { $resource [ 'id' ] } " ;
$this -> ids [ 'credits' ][ $key ] = [
'old' => $resource [ 'id' ],
2020-01-27 21:56:48 +01:00
'new' => $credit -> id ,
2020-01-23 21:35:00 +01:00
];
}
2020-02-26 04:26:07 +01:00
Credit :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$credit_repository = null ;
2020-01-23 21:35:00 +01:00
}
private function processQuotes ( array $data ) : void
{
Quote :: unguard ();
$rules = [
'*.client_id' => [ 'required' ],
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
2021-12-08 02:54:19 +01:00
$quote_repository = new InvoiceMigrationRepository ();
2020-01-23 21:35:00 +01:00
foreach ( $data as $resource ) {
$modified = $resource ;
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'client_id' , $resource ) && ! array_key_exists ( 'clients' , $this -> ids )) {
2020-03-05 21:30:32 +01:00
throw new ResourceDependencyMissing ( 'Processing quotes failed, because of missing dependency - clients.' );
2020-01-23 21:35:00 +01:00
}
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
2021-06-05 07:03:47 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'invoice_id' , $resource ) && isset ( $resource [ 'invoice_id' ]) && $this -> tryTransformingId ( 'invoices' , $resource [ 'invoice_id' ])) {
2021-06-05 07:03:47 +02:00
$modified [ 'invoice_id' ] = $this -> transformId ( 'invoices' , $resource [ 'invoice_id' ]);
2023-02-16 02:36:09 +01:00
}
2021-06-05 07:03:47 +02:00
2020-01-23 21:35:00 +01:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
$modified [ 'company_id' ] = $this -> company -> id ;
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'created_at' ] = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$modified [ 'updated_at' ] = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'tax_rate1' , $modified ) && is_null ( $modified [ 'tax_rate1' ])) {
2021-05-10 01:52:58 +02:00
$modified [ 'tax_rate1' ] = 0 ;
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'tax_rate2' , $modified ) && is_null ( $modified [ 'tax_rate2' ])) {
2021-05-10 01:52:58 +02:00
$modified [ 'tax_rate2' ] = 0 ;
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2020-01-23 21:35:00 +01:00
unset ( $modified [ 'id' ]);
2021-02-03 13:29:44 +01:00
if ( array_key_exists ( 'invitations' , $resource )) {
foreach ( $resource [ 'invitations' ] as $key => $invite ) {
$resource [ 'invitations' ][ $key ][ 'client_contact_id' ] = $this -> transformId ( 'client_contacts' , $invite [ 'client_contact_id' ]);
$resource [ 'invitations' ][ $key ][ 'user_id' ] = $modified [ 'user_id' ];
$resource [ 'invitations' ][ $key ][ 'company_id' ] = $this -> company -> id ;
2021-05-03 06:02:55 +02:00
$resource [ 'invitations' ][ $key ][ 'email_status' ] = '' ;
2021-12-08 02:54:19 +01:00
unset ( $resource [ 'invitations' ][ $key ][ 'quote_id' ]);
2021-04-16 05:58:14 +02:00
unset ( $resource [ 'invitations' ][ $key ][ 'id' ]);
2021-02-03 13:29:44 +01:00
}
$modified [ 'invitations' ] = $this -> deDuplicateInvitations ( $resource [ 'invitations' ]);
}
2020-11-23 22:00:59 +01:00
$quote = $quote_repository -> save (
2020-03-21 06:37:30 +01:00
$modified ,
QuoteFactory :: create ( $this -> company -> id , $modified [ 'user_id' ])
2020-01-23 21:35:00 +01:00
);
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-05 11:46:51 +02:00
$quote -> created_at = $modified [ 'created_at' ];
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-05 11:46:51 +02:00
$quote -> updated_at = $modified [ 'updated_at' ];
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
$quote -> save ([ 'timestamps' => false ]);
2020-01-23 21:35:00 +01:00
$old_user_key = array_key_exists ( 'user_id' , $resource ) ? ? $this -> user -> id ;
2020-03-03 23:44:42 +01:00
2020-11-23 22:00:59 +01:00
$key = " quotes_ { $resource [ 'id' ] } " ;
2020-01-23 21:35:00 +01:00
2020-02-22 03:25:49 +01:00
$this -> ids [ 'quotes' ][ $key ] = [
'old' => $resource [ 'id' ],
2020-11-23 22:00:59 +01:00
'new' => $quote -> id ,
2020-01-23 21:35:00 +01:00
];
}
2020-02-26 04:26:07 +01:00
Quote :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$quote_repository = null ;
2020-01-23 21:35:00 +01:00
}
private function processPayments ( array $data ) : void
2020-11-23 22:33:37 +01:00
{
2020-01-23 21:35:00 +01:00
Payment :: reguard ();
$rules = [
'*.amount' => [ 'required' ],
'*.client_id' => [ 'required' ],
];
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-01-23 21:35:00 +01:00
}
2020-07-01 06:37:05 +02:00
$payment_repository = new PaymentMigrationRepository ( new CreditRepository ());
2020-01-23 21:35:00 +01:00
foreach ( $data as $resource ) {
$modified = $resource ;
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'client_id' , $resource ) && ! array_key_exists ( 'clients' , $this -> ids )) {
2020-03-05 21:30:32 +01:00
throw new ResourceDependencyMissing ( 'Processing payments failed, because of missing dependency - clients.' );
2020-01-23 21:35:00 +01:00
}
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
$modified [ 'company_id' ] = $this -> company -> id ;
unset ( $modified [ 'invoice_id' ]);
2020-01-27 21:56:48 +01:00
if ( isset ( $modified [ 'invoices' ])) {
2020-07-01 06:37:05 +02:00
foreach ( $modified [ 'invoices' ] as $key => $invoice ) {
2020-11-25 15:19:52 +01:00
if ( $this -> tryTransformingId ( 'invoices' , $invoice [ 'invoice_id' ])) {
2020-10-18 09:46:10 +02:00
$modified [ 'invoices' ][ $key ][ 'invoice_id' ] = $this -> transformId ( 'invoices' , $invoice [ 'invoice_id' ]);
2020-11-25 15:19:52 +01:00
} else {
2023-02-16 02:36:09 +01:00
nlog ( $modified [ 'invoices' ]);
unset ( $modified [ 'invoices' ]);
//if the transformation didn't work - you _must_ unset this data as it will be incorrect!
2020-10-18 11:25:32 +02:00
}
2020-03-21 06:37:30 +01:00
}
2020-01-23 21:35:00 +01:00
}
$payment = $payment_repository -> save (
2020-03-21 06:37:30 +01:00
$modified ,
PaymentFactory :: create ( $this -> company -> id , $modified [ 'user_id' ])
2020-01-23 21:35:00 +01:00
);
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$payment -> created_at = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$payment -> updated_at = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
$payment -> save ([ 'timestamps' => false ]);
2021-01-19 23:35:52 +01:00
if ( array_key_exists ( 'company_gateway_id' , $resource ) && isset ( $resource [ 'company_gateway_id' ]) && $resource [ 'company_gateway_id' ] != 'NULL' ) {
2023-02-16 02:36:09 +01:00
if ( $this -> tryTransformingId ( 'company_gateways' , $resource [ 'company_gateway_id' ])) {
2021-07-20 09:23:31 +02:00
$payment -> company_gateway_id = $this -> transformId ( 'company_gateways' , $resource [ 'company_gateway_id' ]);
2023-02-16 02:36:09 +01:00
}
2021-07-20 09:23:31 +02:00
2020-12-28 07:04:24 +01:00
$payment -> save ();
}
2020-01-23 21:35:00 +01:00
$old_user_key = array_key_exists ( 'user_id' , $resource ) ? ? $this -> user -> id ;
$this -> ids [ 'payments' ] = [
" payments_ { $old_user_key } " => [
'old' => $old_user_key ,
'new' => $payment -> id ,
2020-04-01 14:39:59 +02:00
],
2020-01-23 21:35:00 +01:00
];
2020-11-22 22:25:29 +01:00
2023-02-16 02:36:09 +01:00
if ( in_array ( $payment -> status_id , [ Payment :: STATUS_REFUNDED , Payment :: STATUS_PARTIALLY_REFUNDED ])) {
2021-02-07 13:35:16 +01:00
$this -> processPaymentRefund ( $payment );
}
2020-01-23 21:35:00 +01:00
}
2020-02-26 04:26:07 +01:00
Payment :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
$payment_repository = null ;
2020-01-23 21:35:00 +01:00
}
2021-02-07 13:35:16 +01:00
private function processPaymentRefund ( $payment )
{
$invoices = $payment -> invoices () -> get ();
2023-02-16 02:36:09 +01:00
$invoices -> each ( function ( $invoice ) use ( $payment ) {
2021-02-07 13:35:16 +01:00
if ( $payment -> refunded > 0 && in_array ( $invoice -> status_id , [ Invoice :: STATUS_SENT ])) {
$invoice -> service ()
-> updateBalance ( $payment -> refunded )
-> updatePaidToDate ( $payment -> refunded *- 1 )
-> updateStatus ()
-> save ();
}
});
}
2020-11-22 22:25:29 +01:00
private function updatePaymentForStatus ( $payment , $status_id ) : Payment
{
// define('PAYMENT_STATUS_PENDING', 1);
// define('PAYMENT_STATUS_VOIDED', 2);
// define('PAYMENT_STATUS_FAILED', 3);
// define('PAYMENT_STATUS_COMPLETED', 4);
// define('PAYMENT_STATUS_PARTIALLY_REFUNDED', 5);
// define('PAYMENT_STATUS_REFUNDED', 6);
switch ( $status_id ) {
case 1 :
return $payment ;
break ;
case 2 :
return $payment -> service () -> deletePayment ();
break ;
case 3 :
return $payment -> service () -> deletePayment ();
break ;
case 4 :
return $payment ;
break ;
case 5 :
$payment -> status_id = Payment :: STATUS_PARTIALLY_REFUNDED ;
$payment -> save ();
return $payment ;
2020-11-25 15:19:52 +01:00
break ;
2020-11-22 22:25:29 +01:00
case 6 :
$payment -> status_id = Payment :: STATUS_REFUNDED ;
$payment -> save ();
return $payment ;
2020-11-25 15:19:52 +01:00
break ;
2020-11-22 22:25:29 +01:00
default :
return $payment ;
break ;
}
}
2020-02-18 21:53:12 +01:00
private function processDocuments ( array $data ) : void
{
2020-11-25 15:19:52 +01:00
// Document::unguard();
2020-04-01 14:39:59 +02:00
/* No validators since data provided by database is already valid. */
2020-02-18 21:53:12 +01:00
2020-11-25 15:19:52 +01:00
foreach ( $data as $resource ) {
2020-02-18 21:53:12 +01:00
$modified = $resource ;
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'invoice_id' , $resource ) && $resource [ 'invoice_id' ] && ! array_key_exists ( 'invoices' , $this -> ids )) {
2021-02-09 15:15:26 +01:00
return ;
//throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - invoices.');
2020-02-18 21:53:12 +01:00
}
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'expense_id' , $resource ) && $resource [ 'expense_id' ] && ! array_key_exists ( 'expenses' , $this -> ids )) {
2021-02-09 15:15:26 +01:00
return ;
//throw new ResourceDependencyMissing('Processing documents failed, because of missing dependency - expenses.');
2020-02-18 21:53:12 +01:00
}
2020-03-03 23:44:42 +01:00
2020-03-21 06:37:30 +01:00
if ( array_key_exists ( 'invoice_id' , $resource ) && $resource [ 'invoice_id' ] && array_key_exists ( 'invoices' , $this -> ids )) {
2021-02-14 10:25:19 +01:00
$try_quote = false ;
2021-02-14 10:55:04 +01:00
$exception = false ;
2021-07-13 00:07:09 +02:00
$entity = false ;
2023-02-16 02:36:09 +01:00
try {
2021-02-14 10:25:19 +01:00
$invoice_id = $this -> transformId ( 'invoices' , $resource [ 'invoice_id' ]);
2023-08-06 09:35:19 +02:00
$entity = Invoice :: query () -> where ( 'id' , $invoice_id ) -> withTrashed () -> first ();
2023-02-16 02:36:09 +01:00
} catch ( \Exception $e ) {
2021-02-14 10:55:04 +01:00
nlog ( " i couldn't find the invoice document { $resource [ 'invoice_id' ] } , perhaps it is a quote? " );
nlog ( $e -> getMessage ());
2021-02-14 10:25:19 +01:00
$try_quote = true ;
}
2023-02-16 02:36:09 +01:00
if ( $try_quote && array_key_exists ( 'quotes' , $this -> ids )) {
try {
2021-06-21 00:18:30 +02:00
$quote_id = $this -> transformId ( 'quotes' , $resource [ 'invoice_id' ]);
2023-08-06 09:35:19 +02:00
$entity = Quote :: query () -> where ( 'id' , $quote_id ) -> withTrashed () -> first ();
2023-02-16 02:36:09 +01:00
} catch ( \Exception $e ) {
2021-06-21 00:18:30 +02:00
nlog ( " i couldn't find the quote document { $resource [ 'invoice_id' ] } , perhaps it is a quote? " );
nlog ( $e -> getMessage ());
}
2021-02-14 10:25:19 +01:00
}
2023-02-16 02:36:09 +01:00
if ( ! $entity ) {
2021-08-05 23:21:40 +02:00
continue ;
2023-02-16 02:36:09 +01:00
}
2021-08-05 23:21:40 +02:00
// throw new Exception("Resource invoice/quote document not available.");
2020-02-18 21:53:12 +01:00
}
2021-02-14 10:25:19 +01:00
2020-03-21 06:37:30 +01:00
if ( array_key_exists ( 'expense_id' , $resource ) && $resource [ 'expense_id' ] && array_key_exists ( 'expenses' , $this -> ids )) {
2020-11-24 11:12:05 +01:00
$expense_id = $this -> transformId ( 'expenses' , $resource [ 'expense_id' ]);
2023-08-06 09:35:19 +02:00
$entity = Expense :: query () -> where ( 'id' , $expense_id ) -> withTrashed () -> first ();
2020-02-18 21:53:12 +01:00
}
2021-01-10 11:17:18 +01:00
$file_url = $resource [ 'url' ];
$file_name = $resource [ 'name' ];
$file_path = sys_get_temp_dir () . '/' . $file_name ;
2021-02-03 13:29:44 +01:00
try {
file_put_contents ( $file_path , $this -> curlGet ( $file_url ));
$finfo = new \finfo ( FILEINFO_MIME_TYPE );
$file_info = $finfo -> file ( $file_path );
$uploaded_file = new UploadedFile (
2023-02-16 02:36:09 +01:00
$file_path ,
$file_name ,
$file_info ,
filesize ( $file_path ),
0 ,
false
);
2021-02-03 13:29:44 +01:00
2023-06-29 08:38:33 +02:00
// $this->saveDocument($uploaded_file, $entity, $is_public = true);
$document = ( new \App\Jobs\Util\UploadFile (
$uploaded_file ,
\App\Jobs\Util\UploadFile :: DOCUMENT ,
$this -> user ,
$this -> company ,
$entity ,
null ,
true
)) -> handle ();
2023-02-16 02:36:09 +01:00
} catch ( \Exception $e ) {
2021-02-03 13:29:44 +01:00
//do nothing, gracefully :)
}
2021-01-09 12:10:04 +01:00
}
2020-02-24 11:15:30 +01:00
}
2020-05-27 01:49:06 +02:00
private function processPaymentTerms ( array $data ) : void
{
PaymentTerm :: unguard ();
2020-09-06 11:38:10 +02:00
$modified = collect ( $data ) -> map ( function ( $item ) {
2020-05-27 01:49:06 +02:00
$item [ 'user_id' ] = $this -> user -> id ;
$item [ 'company_id' ] = $this -> company -> id ;
2020-12-22 22:38:13 +01:00
$item [ 'is_deleted' ] = isset ( $item [ 'is_deleted' ]) ? $item [ 'is_deleted' ] : 0 ;
2020-05-27 01:49:06 +02:00
return $item ;
}) -> toArray ();
PaymentTerm :: insert ( $modified );
PaymentTerm :: reguard ();
2020-09-06 11:38:10 +02:00
2020-05-27 01:49:06 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
}
2020-02-24 11:15:30 +01:00
private function processCompanyGateways ( array $data ) : void
{
CompanyGateway :: unguard ();
$rules = [
'*.gateway_key' => 'required' ,
'*.fees_and_limits' => new ValidCompanyGatewayFeesAndLimitsRule (),
];
2020-03-03 23:44:42 +01:00
2020-02-24 11:15:30 +01:00
$validator = Validator :: make ( $data , $rules );
if ( $validator -> fails ()) {
2020-03-05 21:30:32 +01:00
throw new MigrationValidatorFailed ( json_encode ( $validator -> errors ()));
2020-02-24 11:15:30 +01:00
}
foreach ( $data as $resource ) {
$modified = $resource ;
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
$modified [ 'company_id' ] = $this -> company -> id ;
unset ( $modified [ 'id' ]);
if ( isset ( $modified [ 'config' ])) {
$modified [ 'config' ] = encrypt ( $modified [ 'config' ]);
}
if ( isset ( $modified [ 'fees_and_limits' ])) {
$modified [ 'fees_and_limits' ] = $this -> cleanFeesAndLimits ( $modified [ 'fees_and_limits' ]);
}
2023-02-16 02:36:09 +01:00
if ( ! array_key_exists ( 'accepted_credit_cards' , $modified ) || ( array_key_exists ( 'accepted_credit_cards' , $modified ) && empty ( $modified [ 'accepted_credit_cards' ]))) {
2022-04-01 00:43:25 +02:00
$modified [ 'accepted_credit_cards' ] = 0 ;
2023-02-16 02:36:09 +01:00
}
2022-04-01 00:43:25 +02:00
2021-09-05 06:03:21 +02:00
// /* On Hosted platform we need to advise Stripe users to connect with Stripe Connect */
2023-02-16 02:36:09 +01:00
if ( Ninja :: isHosted () && $modified [ 'gateway_key' ] == 'd14dd26a37cecc30fdd65700bfb55b23' ) {
2021-08-02 05:12:33 +02:00
$nmo = new NinjaMailerObject ;
$nmo -> mailable = new StripeConnectMigration ( $this -> company );
$nmo -> company = $this -> company ;
$nmo -> settings = $this -> company -> settings ;
$nmo -> to_user = $this -> user ;
2023-07-10 04:55:21 +02:00
2023-10-26 04:57:44 +02:00
if ( ! $this -> silent_migration ) {
2023-07-10 04:55:21 +02:00
NinjaMailerJob :: dispatch ( $nmo , true );
2023-10-26 04:57:44 +02:00
}
2021-08-02 05:12:33 +02:00
2021-05-09 09:33:23 +02:00
$modified [ 'gateway_key' ] = 'd14dd26a47cecc30fdd65700bfb67b34' ;
}
2021-09-05 06:03:21 +02:00
2023-02-16 02:36:09 +01:00
if ( Ninja :: isSelfHost () && $modified [ 'gateway_key' ] == 'd14dd26a47cecc30fdd65700bfb67b34' ) {
2021-09-05 06:03:21 +02:00
$modified [ 'gateway_key' ] = 'd14dd26a37cecc30fdd65700bfb55b23' ;
}
2023-08-07 07:30:34 +02:00
/** @var \App\Models\CompanyGateway $company_gateway */
2020-02-24 11:15:30 +01:00
$company_gateway = CompanyGateway :: create ( $modified );
2020-12-28 07:04:24 +01:00
$key = " company_gateways_ { $resource [ 'id' ] } " ;
2020-02-24 11:15:30 +01:00
2020-12-28 07:04:24 +01:00
$this -> ids [ 'company_gateways' ][ $key ] = [
2020-02-24 11:15:30 +01:00
'old' => $resource [ 'id' ],
'new' => $company_gateway -> id ,
];
}
2020-02-26 04:26:07 +01:00
CompanyGateway :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
2020-02-24 11:15:30 +01:00
}
private function processClientGatewayTokens ( array $data ) : void
{
ClientGatewayToken :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
2021-05-17 00:32:45 +02:00
$modified [ 'company_gateway_id' ] = $this -> transformId ( 'company_gateways' , $resource [ 'company_gateway_id' ]);
2020-11-19 12:33:14 +01:00
//$modified['user_id'] = $this->processUserId($resource);
2023-08-08 10:56:31 +02:00
/** @var \App\Models\ClientGatewayToken $cgt **/
$cgt = ClientGatewayToken :: create ( $modified );
2020-02-24 11:15:30 +01:00
2020-12-13 11:33:30 +01:00
$key = " client_gateway_tokens_ { $resource [ 'id' ] } " ;
2020-02-24 11:15:30 +01:00
2020-12-13 11:33:30 +01:00
$this -> ids [ 'client_gateway_tokens' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $cgt -> id ,
2020-02-24 11:15:30 +01:00
];
}
2020-02-26 04:26:07 +01:00
2020-03-21 06:37:30 +01:00
ClientGatewayToken :: reguard ();
2020-05-06 13:49:42 +02:00
/*Improve memory handling by setting everything to null when we have finished*/
$data = null ;
2020-02-18 21:53:12 +01:00
}
2020-10-30 10:17:29 +01:00
private function processTaskStatuses ( array $data ) : void
2020-11-25 15:19:52 +01:00
{
info ( 'in task statuses' );
2020-10-30 10:17:29 +01:00
TaskStatus :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
2020-11-07 10:08:21 +01:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-10-30 10:17:29 +01:00
2023-08-08 10:56:31 +02:00
/** @var \App\Models\TaskStatus $task_status **/
$task_status = TaskStatus :: create ( $modified );
2020-10-30 10:17:29 +01:00
2020-12-13 11:33:30 +01:00
$key = " task_statuses_ { $resource [ 'id' ] } " ;
2020-10-30 10:17:29 +01:00
2020-12-13 11:33:30 +01:00
$this -> ids [ 'task_statuses' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $task_status -> id ,
2020-10-30 10:17:29 +01:00
];
}
TaskStatus :: reguard ();
$data = null ;
2020-10-30 13:16:19 +01:00
info ( 'finished task statuses' );
2020-10-30 10:17:29 +01:00
}
private function processExpenseCategories ( array $data ) : void
{
ExpenseCategory :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
2020-11-07 10:08:21 +01:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-10-30 10:17:29 +01:00
2023-08-08 10:56:31 +02:00
/** @var \App\Models\ExpenseCategory $expense_category **/
$expense_category = ExpenseCategory :: create ( $modified );
2020-10-30 10:17:29 +01:00
$old_user_key = array_key_exists ( 'user_id' , $resource ) ? ? $this -> user -> id ;
2020-11-28 07:22:43 +01:00
$key = " expense_categories_ { $resource [ 'id' ] } " ;
$this -> ids [ 'expense_categories' ][ $key ] = [
'old' => $resource [ 'id' ],
2021-01-09 12:10:04 +01:00
'new' => $expense_category -> id ,
2020-10-30 10:17:29 +01:00
];
}
ExpenseCategory :: reguard ();
$data = null ;
}
private function processTasks ( array $data ) : void
{
Task :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
2020-11-07 10:08:21 +01:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-10-31 01:46:00 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $modified [ 'client_id' ])) {
2020-10-31 01:46:00 +01:00
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-31 01:46:00 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $modified [ 'invoice_id' ])) {
2020-10-31 01:46:00 +01:00
$modified [ 'invoice_id' ] = $this -> transformId ( 'invoices' , $resource [ 'invoice_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-31 01:46:00 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $modified [ 'project_id' ])) {
2020-10-31 01:46:00 +01:00
$modified [ 'project_id' ] = $this -> transformId ( 'projects' , $resource [ 'project_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-31 01:46:00 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $modified [ 'status_id' ])) {
2020-10-31 01:46:00 +01:00
$modified [ 'status_id' ] = $this -> transformId ( 'task_statuses' , $resource [ 'status_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-30 10:17:29 +01:00
2023-08-08 10:56:31 +02:00
/** @var \App\Models\Task $task **/
$task = Task :: create ( $modified );
2020-10-30 10:17:29 +01:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$task -> created_at = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$task -> updated_at = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2021-05-05 11:46:51 +02:00
$task -> save ([ 'timestamps' => false ]);
2020-10-30 10:17:29 +01:00
$old_user_key = array_key_exists ( 'user_id' , $resource ) ? ? $this -> user -> id ;
$this -> ids [ 'tasks' ] = [
" tasks_ { $old_user_key } " => [
'old' => $resource [ 'id' ],
'new' => $task -> id ,
],
];
}
Task :: reguard ();
$data = null ;
}
2020-10-30 13:01:30 +01:00
private function processProjects ( array $data ) : void
{
Project :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
2020-11-07 10:08:21 +01:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-10-31 01:46:00 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $modified [ 'client_id' ])) {
2020-10-31 01:46:00 +01:00
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-30 13:01:30 +01:00
2023-08-08 12:39:46 +02:00
/** @var \App\Models\Project $project **/
$project = Project :: create ( $modified );
2020-10-30 13:01:30 +01:00
2020-11-23 22:00:59 +01:00
$key = " projects_ { $resource [ 'id' ] } " ;
2020-10-30 13:01:30 +01:00
2020-11-23 22:00:59 +01:00
$this -> ids [ 'projects' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $project -> id ,
2020-10-30 13:01:30 +01:00
];
}
Project :: reguard ();
$data = null ;
}
2023-06-25 11:04:06 +02:00
private function processActivities ( array $data ) : void
{
2023-10-26 04:57:44 +02:00
Activity :: query () -> where ( 'company_id' , $this -> company -> id ) -> cursor () -> each ( function ( $a ) {
2023-06-25 11:04:06 +02:00
$a -> forceDelete ();
2023-06-25 12:09:45 +02:00
nlog ( " deleting { $a -> id } " );
2023-06-25 11:04:06 +02:00
});
Activity :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
$modified [ 'company_id' ] = $this -> company -> id ;
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2023-10-26 04:57:44 +02:00
try {
if ( isset ( $modified [ 'client_id' ])) {
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'invoice_id' ])) {
$modified [ 'invoice_id' ] = $this -> transformId ( 'invoices' , $resource [ 'invoice_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'quote_id' ])) {
$modified [ 'quote_id' ] = $this -> transformId ( 'quotes' , $resource [ 'quote_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'recurring_invoice_id' ])) {
$modified [ 'recurring_invoice_id' ] = $this -> transformId ( 'recurring_invoices' , $resource [ 'recurring_invoice_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'payment_id' ])) {
$modified [ 'payment_id' ] = $this -> transformId ( 'payments' , $resource [ 'payment_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'credit_id' ])) {
$modified [ 'credit_id' ] = $this -> transformId ( 'credits' , $resource [ 'credit_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'expense_id' ])) {
$modified [ 'expense_id' ] = $this -> transformId ( 'expenses' , $resource [ 'expense_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'task_id' ])) {
$modified [ 'task_id' ] = $this -> transformId ( 'tasks' , $resource [ 'task_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
if ( isset ( $modified [ 'client_contact_id' ])) {
$modified [ 'client_contact_id' ] = $this -> transformId ( 'client_contacts' , $resource [ 'client_contact_id' ]);
}
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
$modified [ 'updated_at' ] = $modified [ 'created_at' ];
2023-06-25 12:09:45 +02:00
2023-10-26 04:57:44 +02:00
/** @var \App\Models\Activity $act **/
$act = Activity :: make ( $modified );
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
$act -> save ([ 'timestamps' => false ]);
} catch ( \Exception $e ) {
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
nlog ( " could not import activity: { $e -> getMessage () } " );
2023-06-25 11:04:06 +02:00
2023-10-26 04:57:44 +02:00
}
2023-06-25 11:04:06 +02:00
}
Activity :: reguard ();
}
2020-10-30 13:22:32 +01:00
private function processExpenses ( array $data ) : void
{
Expense :: unguard ();
foreach ( $data as $resource ) {
$modified = $resource ;
unset ( $modified [ 'id' ]);
$modified [ 'company_id' ] = $this -> company -> id ;
2020-11-07 10:08:21 +01:00
$modified [ 'user_id' ] = $this -> processUserId ( $resource );
2020-10-30 22:13:02 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $resource [ 'client_id' ])) {
2020-10-30 22:13:02 +01:00
$modified [ 'client_id' ] = $this -> transformId ( 'clients' , $resource [ 'client_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-30 22:13:02 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $resource [ 'category_id' ])) {
2020-10-30 22:13:02 +01:00
$modified [ 'category_id' ] = $this -> transformId ( 'expense_categories' , $resource [ 'category_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-30 22:13:02 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $resource [ 'invoice_id' ])) {
2020-10-30 22:13:02 +01:00
$modified [ 'invoice_id' ] = $this -> transformId ( 'invoices' , $resource [ 'invoice_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-30 22:13:02 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $resource [ 'project_id' ])) {
2020-10-30 22:13:02 +01:00
$modified [ 'project_id' ] = $this -> transformId ( 'projects' , $resource [ 'project_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-31 06:35:05 +01:00
2020-11-25 15:19:52 +01:00
if ( isset ( $resource [ 'vendor_id' ])) {
2020-10-31 06:35:05 +01:00
$modified [ 'vendor_id' ] = $this -> transformId ( 'vendors' , $resource [ 'vendor_id' ]);
2020-11-25 15:19:52 +01:00
}
2020-10-30 13:22:32 +01:00
2023-08-08 10:56:31 +02:00
/** @var \App\Models\Expense $expense **/
$expense = Expense :: create ( $modified );
2020-10-30 13:22:32 +01:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'created_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$expense -> created_at = Carbon :: parse ( $modified [ 'created_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-05 11:46:51 +02:00
2023-02-16 02:36:09 +01:00
if ( array_key_exists ( 'updated_at' , $modified )) {
2021-05-10 01:52:58 +02:00
$expense -> updated_at = Carbon :: parse ( $modified [ 'updated_at' ]);
2023-02-16 02:36:09 +01:00
}
2021-05-10 01:52:58 +02:00
2021-05-05 11:46:51 +02:00
$expense -> save ([ 'timestamps' => false ]);
2020-10-30 13:22:32 +01:00
$old_user_key = array_key_exists ( 'user_id' , $resource ) ? ? $this -> user -> id ;
2021-01-09 12:10:04 +01:00
$key = " expenses_ { $resource [ 'id' ] } " ;
$this -> ids [ 'expenses' ][ $key ] = [
'old' => $resource [ 'id' ],
'new' => $expense -> id ,
2020-10-30 13:22:32 +01:00
];
}
Expense :: reguard ();
$data = null ;
}
2020-01-23 21:35:00 +01:00
/**
* |--------------------------------------------------------------------------
* | Additional migration methods .
* |--------------------------------------------------------------------------
* |
* | These methods aren 't initialized automatically, so they don' t depend on
* | the migration data .
*/
/**
* Cloned from App\Http\Requests\User\StoreUserRequest .
*
* @ param string $data
* @ return User
*/
public function fetchUser ( string $data ) : User
{
$user = MultiDB :: hasUser ([ 'email' => $data ]);
2020-04-01 14:39:59 +02:00
if ( ! $user ) {
2020-03-24 14:25:20 +01:00
$user = UserFactory :: create ( $this -> company -> account -> id );
2020-01-23 21:35:00 +01:00
}
2020-11-17 10:53:32 +01:00
2020-01-23 21:35:00 +01:00
return $user ;
}
/**
* @ param string $resource
* @ param string $old
* @ return int
2020-10-28 11:10:49 +01:00
* @ throws Exception
2020-01-23 21:35:00 +01:00
*/
2020-10-30 22:13:02 +01:00
public function transformId ( $resource , string $old ) : int
2020-01-23 21:35:00 +01:00
{
2020-04-01 14:39:59 +02:00
if ( ! array_key_exists ( $resource , $this -> ids )) {
2020-11-25 15:19:52 +01:00
info ( print_r ( $resource , 1 ));
2020-10-28 11:10:49 +01:00
throw new Exception ( " Resource { $resource } not available. " );
2020-01-23 21:35:00 +01:00
}
2020-04-01 14:39:59 +02:00
if ( ! array_key_exists ( " { $resource } _ { $old } " , $this -> ids [ $resource ])) {
2020-10-28 11:10:49 +01:00
throw new Exception ( " Missing resource key: { $resource } _ { $old } " );
2020-01-23 21:35:00 +01:00
}
return $this -> ids [ $resource ][ " { $resource } _ { $old } " ][ 'new' ];
}
2020-11-25 03:59:23 +01:00
private function tryTransformingId ( $resource , string $old ) : ? int
{
if ( ! array_key_exists ( $resource , $this -> ids )) {
return false ;
}
if ( ! array_key_exists ( " { $resource } _ { $old } " , $this -> ids [ $resource ])) {
return false ;
}
return $this -> ids [ $resource ][ " { $resource } _ { $old } " ][ 'new' ];
}
2020-01-23 21:35:00 +01:00
/**
2020-04-01 14:39:59 +02:00
* Process & handle user_id .
2020-01-23 21:35:00 +01:00
*
* @ param array $resource
* @ return int | mixed
2020-10-28 11:10:49 +01:00
* @ throws Exception
2020-01-23 21:35:00 +01:00
*/
public function processUserId ( array $resource )
{
2020-04-01 14:39:59 +02:00
if ( ! array_key_exists ( 'user_id' , $resource )) {
2020-01-23 21:35:00 +01:00
return $this -> user -> id ;
}
2020-04-01 14:39:59 +02:00
if ( array_key_exists ( 'user_id' , $resource ) && ! array_key_exists ( 'users' , $this -> ids )) {
2020-01-23 21:35:00 +01:00
return $this -> user -> id ;
}
return $this -> transformId ( 'users' , $resource [ 'user_id' ]);
}
2020-04-01 10:54:22 +02:00
public function failed ( $exception = null )
{
2020-04-01 14:39:59 +02:00
info ( 'the job failed' );
2020-10-07 05:00:32 +02:00
2023-02-09 02:10:08 +01:00
config ([ 'queue.failed.driver' => null ]);
2020-10-07 05:00:32 +02:00
$job_failure = new MigrationFailure ();
$job_failure -> string_metric5 = get_class ( $this );
$job_failure -> string_metric6 = $exception -> getMessage ();
LightLogs :: create ( $job_failure )
2021-12-11 10:49:29 +01:00
-> queue ();
2020-10-07 05:00:32 +02:00
2020-04-01 14:39:59 +02:00
info ( print_r ( $exception -> getMessage (), 1 ));
2021-05-20 23:58:46 +02:00
2023-02-16 02:36:09 +01:00
if ( Ninja :: isHosted ()) {
2021-05-20 23:58:46 +02:00
app ( 'sentry' ) -> captureException ( $exception );
2023-02-16 02:36:09 +01:00
}
2020-04-01 10:54:22 +02:00
}
2021-01-10 11:17:18 +01:00
public function curlGet ( $url , $headers = false )
{
return $this -> exec ( 'GET' , $url , null );
}
public function exec ( $method , $url , $data )
{
2023-02-16 02:36:09 +01:00
$client = new \GuzzleHttp\Client ([ 'headers' =>
[
'X-Ninja-Token' => $this -> token ,
2021-01-10 11:17:18 +01:00
]
]);
$response = $client -> request ( 'GET' , $url );
return $response -> getBody ();
}
2021-05-20 06:13:54 +02:00
2021-05-12 12:46:59 +02:00
private function processNinjaTokens ( array $data )
{
2021-06-09 05:17:55 +02:00
nlog ( " attempting to process Ninja Tokens " );
2023-02-16 02:36:09 +01:00
if ( Ninja :: isHosted ()) {
try {
2022-09-07 06:15:27 +02:00
\Modules\Admin\Jobs\Account\NinjaUser :: dispatch ( $data , $this -> company );
2023-02-16 02:36:09 +01:00
} catch ( \Exception $e ) {
2022-01-12 10:29:10 +01:00
nlog ( $e -> getMessage ());
}
}
2021-05-12 12:46:59 +02:00
}
2021-02-03 21:52:37 +01:00
/* In V4 we use negative invoices ( credits ) and add then into the client balance . In V5 , these sit off ledger and are applied later .
This next section will check for credit balances and reduce the client balance so that the V5 balances are correct
*/
2021-02-04 21:47:16 +01:00
// private function fixClientBalances()
// {
2021-02-03 21:52:37 +01:00
2021-02-04 21:47:16 +01:00
// Client::cursor()->each(function ($client) {
2021-02-03 21:52:37 +01:00
2021-02-04 21:47:16 +01:00
// $credit_balance = $client->credits->where('is_deleted', false)->sum('balance');
2021-02-03 21:52:37 +01:00
2021-02-04 21:47:16 +01:00
// if($credit_balance > 0){
// $client->balance += $credit_balance;
// $client->save();
// }
2021-02-03 21:52:37 +01:00
2021-02-04 21:47:16 +01:00
// });
2021-02-03 21:52:37 +01:00
2021-02-04 21:47:16 +01:00
// }
2020-01-23 21:35:00 +01:00
}