diff --git a/app/Http/Controllers/ImportQuickbooksController.php b/app/Http/Controllers/ImportQuickbooksController.php index 88e6a180b8..e1d75589e3 100644 --- a/app/Http/Controllers/ImportQuickbooksController.php +++ b/app/Http/Controllers/ImportQuickbooksController.php @@ -60,75 +60,4 @@ class ImportQuickbooksController extends BaseController return redirect()->to($authorizationUrl); } - public function preimport(string $type, string $hash) - { - // // Check for authorization otherwise - // // Create a reference - // $data = [ - // 'hash' => $hash, - // 'type' => $type - // ]; - // $this->getData($data); - } - - protected function getData($data) - { - - // $entity = $this->import_entities[$data['type']]; - // $cache_name = "{$data['hash']}-{$data['type']}"; - // // TODO: Get or put cache or DB? - // if(! Cache::has($cache_name)) { - // $contents = call_user_func([$this->service, "fetch{$entity}s"]); - // if($contents->isEmpty()) { - // return; - // } - - // Cache::put($cache_name, base64_encode($contents->toJson()), 600); - // } - } - - /** - * @OA\Post( - * path="/api/v1/import_json", - * operationId="getImportJson", - * tags={"import"}, - * summary="Import data from the system", - * description="Import data from the system", - * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), - * @OA\Response( - * response=200, - * description="success", - * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), - * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), - * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * ), - * @OA\Response( - * response=422, - * description="Validation error", - * @OA\JsonContent(ref="#/components/schemas/ValidationError"), - * ), - * @OA\Response( - * response="default", - * description="Unexpected Error", - * @OA\JsonContent(ref="#/components/schemas/Error"), - * ), - * ) - */ - public function import(Request $request) - { - // $hash = Str::random(32); - // foreach($request->input('import_types') as $type) { - // $this->preimport($type, $hash); - // } - // /** @var \App\Models\User $user */ - // // $user = auth()->user() ?? Auth::loginUsingId(60); - // $data = ['import_types' => $request->input('import_types') ] + compact('hash'); - // if (Ninja::isHosted()) { - // QuickbooksIngest::dispatch($data, $user->company()); - // } else { - // QuickbooksIngest::dispatch($data, $user->company()); - // } - - // return response()->json(['message' => 'Processing'], 200); - } } diff --git a/app/Import/Providers/Quickbooks.php b/app/Import/Providers/Quickbooks.php deleted file mode 100644 index 6dd7d5784b..0000000000 --- a/app/Import/Providers/Quickbooks.php +++ /dev/null @@ -1,253 +0,0 @@ -{$entity}(); - } - - //collate any errors - - // $this->finalizeImport(); - } - - public function client() - { - $entity_type = 'client'; - $data = $this->getData($entity_type); - if (empty($data)) { - $this->entity_count['clients'] = 0; - - return; - } - - $this->request_name = StoreClientRequest::class; - $this->repository_name = ClientRepository::class; - $this->factory_name = ClientFactory::class; - $this->repository = app()->make($this->repository_name); - $this->repository->import_mode = true; - $this->transformer = new ClientTransformer($this->company); - $client_count = $this->ingest($data, $entity_type); - $this->entity_count['clients'] = $client_count; - } - - public function product() - { - $entity_type = 'product'; - $data = $this->getData($entity_type); - if (empty($data)) { - $this->entity_count['products'] = 0; - - return; - } - - $this->request_name = StoreProductRequest::class; - $this->repository_name = ProductRepository::class; - $this->factory_name = ProductFactory::class; - $this->repository = app()->make($this->repository_name); - $this->repository->import_mode = true; - $this->transformer = new ProductTransformer($this->company); - $count = $this->ingest($data, $entity_type); - $this->entity_count['products'] = $count; - } - - public function getData($type) - { - - // get the data from cache? file? or api ? - return json_decode(base64_decode(Cache::get("{$this->hash}-{$type}")), true); - } - - public function payment() - { - $entity_type = 'payment'; - $data = $this->getData($entity_type); - if (empty($data)) { - $this->entity_count['payments'] = 0; - - return; - } - - $this->request_name = StorePaymentRequest::class; - $this->repository_name = PaymentRepository::class; - $this->factory_name = PaymentFactory::class; - $this->repository = app()->make($this->repository_name); - $this->repository->import_mode = true; - $this->transformer = new PaymentTransformer($this->company); - $count = $this->ingest($data, $entity_type); - $this->entity_count['payments'] = $count; - } - - public function invoice() - { - //make sure we update and create products - $initial_update_products_value = $this->company->update_products; - $this->company->update_products = true; - - $this->company->save(); - - $entity_type = 'invoice'; - $data = $this->getData($entity_type); - - if (empty($data)) { - $this->entity_count['invoices'] = 0; - - return; - } - - $this->request_name = StoreInvoiceRequest::class; - $this->repository_name = InvoiceRepository::class; - $this->factory_name = InvoiceFactory::class; - $this->repository = app()->make($this->repository_name); - $this->repository->import_mode = true; - $this->transformer = new InvoiceTransformer($this->company); - $invoice_count = $this->ingestInvoices($data, ''); - $this->entity_count['invoices'] = $invoice_count; - $this->company->update_products = $initial_update_products_value; - $this->company->save(); - } - - public function ingestInvoices($invoices, $invoice_number_key) - { - $count = 0; - $invoice_transformer = $this->transformer; - /** @var ClientRepository $client_repository */ - $client_repository = app()->make(ClientRepository::class); - $client_repository->import_mode = true; - $invoice_repository = new InvoiceRepository(); - $invoice_repository->import_mode = true; - - foreach ($invoices as $raw_invoice) { - if(!is_array($raw_invoice)) { - continue; - } - - try { - $invoice_data = $invoice_transformer->transform($raw_invoice); - $invoice_data['user_id'] = $this->company->owner()->id; - $invoice_data['line_items'] = (array) $invoice_data['line_items']; - $invoice_data['line_items'] = $this->cleanItems( - $invoice_data['line_items'] ?? [] - ); - - if ( - empty($invoice_data['client_id']) && - ! empty($invoice_data['client']) - ) { - $client_data = $invoice_data['client']; - $client_data['user_id'] = $this->getUserIDForRecord( - $invoice_data - ); - $client_repository->save( - $client_data, - $client = ClientFactory::create( - $this->company->id, - $client_data['user_id'] - ) - ); - $invoice_data['client_id'] = $client->id; - unset($invoice_data['client']); - } - - $validator = $this->request_name::runFormRequest($invoice_data); - if ($validator->fails()) { - $this->error_array['invoice'][] = [ - 'invoice' => $invoice_data, - 'error' => $validator->errors()->all(), - ]; - } else { - if(!Invoice::where('number', $invoice_data['number'])->first()) { - $invoice = InvoiceFactory::create( - $this->company->id, - $this->company->owner()->id - ); - $invoice->mergeFillable(['partial','amount','balance','line_items']); - if (! empty($invoice_data['status_id'])) { - $invoice->status_id = $invoice_data['status_id']; - } - - $saveable_invoice_data = $invoice_data; - if(array_key_exists('payments', $saveable_invoice_data)) { - unset($saveable_invoice_data['payments']); - } - - $invoice->fill($saveable_invoice_data); - $invoice->save(); - $count++; - - } - // $this->actionInvoiceStatus( - // $invoice, - // $invoice_data, - // $invoice_repository - // ); - } - } catch (\Exception $ex) { - if (\DB::connection(config('database.default'))->transactionLevel() > 0) { - \DB::connection(config('database.default'))->rollBack(); - } - - if ($ex instanceof ImportException) { - $message = $ex->getMessage(); - } else { - report($ex); - $message = 'Unknown error '; - nlog($ex->getMessage()); - nlog($raw_invoice); - } - - $this->error_array['invoice'][] = [ - 'invoice' => $raw_invoice, - 'error' => $message, - ]; - } - } - - return $count; - } - -} diff --git a/app/Jobs/Import/QuickbooksIngest.php b/app/Jobs/Import/QuickbooksIngest.php deleted file mode 100644 index b32db16f0e..0000000000 --- a/app/Jobs/Import/QuickbooksIngest.php +++ /dev/null @@ -1,48 +0,0 @@ -company = $company; - $this->request = $request; - } - - /** - * Execute the job. - */ - public function handle(): void - { - MultiDB::setDb($this->company->db); - set_time_limit(0); - - $engine = new Quickbooks(['import_type' => 'client', 'hash' => $this->request['hash'] ], $this->company); - foreach ($this->request['import_types'] as $entity) { - $engine->import($entity); - } - - $engine->finalizeImport(); - } -} diff --git a/app/Livewire/Flow2/RequiredFields.php b/app/Livewire/Flow2/RequiredFields.php index 5cce417df6..e3a3183075 100644 --- a/app/Livewire/Flow2/RequiredFields.php +++ b/app/Livewire/Flow2/RequiredFields.php @@ -88,7 +88,7 @@ class RequiredFields extends Component $rff = new RFFService( fields: $this->getContext()['fields'], database: $this->getContext()['db'], - company_gateway_id: $this->company_gateway->id, + company_gateway_id: (string)$this->company_gateway->id, ); /** @var \App\Models\ClientContact $contact */ @@ -111,7 +111,7 @@ class RequiredFields extends Component $rff = new RFFService( fields: $this->fields, database: $this->getContext()['db'], - company_gateway_id: $this->company_gateway->id, + company_gateway_id: (string) $this->company_gateway->id, ); $contact = auth()->user(); diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index e7ea1efd83..4ee0e8dae9 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -232,7 +232,6 @@ class BaseDriver extends AbstractPaymentDriver * * @param ClientGatewayToken $cgt The client gateway token object * @param PaymentHash $payment_hash The Payment hash containing the payment meta data - * @return ?Payment|bool The payment response */ public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) { diff --git a/app/PaymentDrivers/Forte/CreditCard.php b/app/PaymentDrivers/Forte/CreditCard.php index 16ceb030fd..04942e046b 100644 --- a/app/PaymentDrivers/Forte/CreditCard.php +++ b/app/PaymentDrivers/Forte/CreditCard.php @@ -109,8 +109,8 @@ class CreditCard SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_FORTE, - $this->client, - $this->client->company, + $this->forte->client, + $this->forte->client->company, ); throw new \App\Exceptions\PaymentFailed("Unable to store payment method: {$error->response->response_desc}", 400); diff --git a/app/PaymentDrivers/PayPalExpressPaymentDriver.php b/app/PaymentDrivers/PayPalExpressPaymentDriver.php deleted file mode 100644 index fa96aa67e2..0000000000 --- a/app/PaymentDrivers/PayPalExpressPaymentDriver.php +++ /dev/null @@ -1,261 +0,0 @@ -omnipay_gateway = Omnipay::create( - $this->company_gateway->gateway->provider - ); - - $this->omnipay_gateway->initialize((array) $this->company_gateway->getConfig()); - } - - public function setPaymentMethod($payment_method_id) - { - // PayPal doesn't have multiple ways of paying. - // There's just one, off-site redirect. - - return $this; - } - - public function authorizeView($payment_method) - { - // PayPal doesn't support direct authorization. - - return $this; - } - - public function authorizeResponse($request) - { - // PayPal doesn't support direct authorization. - - return $this; - } - - public function processPaymentView($data) - { - $this->initializeOmnipayGateway(); - - $this->payment_hash->data = array_merge((array) $this->payment_hash->data, ['amount' => $data['total']['amount_with_fee']]); - $this->payment_hash->save(); - - $response = $this->omnipay_gateway - ->purchase($this->generatePaymentDetails($data)) - ->setItems($this->generatePaymentItems($data)) - ->send(); - - if ($response->isRedirect()) { - return redirect($response->getRedirectUrl()); - } - - // $this->sendFailureMail($response->getMessage() ?: ''); - - $message = [ - 'server_response' => $response->getMessage(), - 'data' => $this->payment_hash->data, - ]; - - SystemLogger::dispatch( - $message, - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_FAILURE, - SystemLog::TYPE_PAYPAL, - $this->client, - $this->client->company, - ); - - throw new PaymentFailed($response->getMessage(), $response->getCode()); - } - - public function processPaymentResponse($request) - { - $this->initializeOmnipayGateway(); - - $response = $this->omnipay_gateway - ->completePurchase(['amount' => $this->payment_hash->data->amount, 'currency' => $this->client->getCurrencyCode()]) - ->send(); - - if ($response->isCancelled() && $this->client->getSetting('enable_client_portal')) { - return redirect()->route('client.invoices.index')->with('warning', ctrans('texts.status_cancelled')); - } elseif($response->isCancelled() && !$this->client->getSetting('enable_client_portal')) { - redirect()->route('client.invoices.show', ['invoice' => $this->payment_hash->fee_invoice])->with('warning', ctrans('texts.status_cancelled')); - } - - if ($response->isSuccessful()) { - $data = [ - 'payment_method' => $response->getData()['TOKEN'], - 'payment_type' => PaymentType::PAYPAL, - 'amount' => $this->payment_hash->data->amount, - 'transaction_reference' => $response->getTransactionReference(), - 'gateway_type_id' => GatewayType::PAYPAL, - ]; - - $payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED); - - SystemLogger::dispatch( - ['response' => (array) $response->getData(), 'data' => $data], - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_SUCCESS, - SystemLog::TYPE_PAYPAL, - $this->client, - $this->client->company, - ); - - return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); - } - - if (! $response->isSuccessful()) { - $data = $response->getData(); - - $this->sendFailureMail($response->getMessage() ?: ''); - - $message = [ - 'server_response' => $data['L_LONGMESSAGE0'], - 'data' => $this->payment_hash->data, - ]; - - SystemLogger::dispatch( - $message, - SystemLog::CATEGORY_GATEWAY_RESPONSE, - SystemLog::EVENT_GATEWAY_FAILURE, - SystemLog::TYPE_PAYPAL, - $this->client, - $this->client->company, - ); - - throw new PaymentFailed($response->getMessage(), $response->getCode()); - } - } - - public function generatePaymentDetails(array $data) - { - $_invoice = collect($this->payment_hash->data->invoices)->first(); - $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id)); - - // $this->fee = $this->feeCalc($invoice, $data['total']['amount_with_fee']); - - return [ - 'currency' => $this->client->getCurrencyCode(), - 'transactionType' => 'Purchase', - 'clientIp' => request()->getClientIp(), - // 'amount' => round(($data['total']['amount_with_fee'] + $this->fee),2), - 'amount' => round($data['total']['amount_with_fee'], 2), - 'returnUrl' => route('client.payments.response', [ - 'company_gateway_id' => $this->company_gateway->id, - 'payment_hash' => $this->payment_hash->hash, - 'payment_method_id' => GatewayType::PAYPAL, - ]), - 'cancelUrl' => $this->client->company->domain()."/client/invoices/{$invoice->hashed_id}", - 'description' => implode(',', collect($this->payment_hash->data->invoices) - ->map(function ($invoice) { - return sprintf('%s: %s', ctrans('texts.invoice_number'), $invoice->invoice_number); - })->toArray()), - 'transactionId' => $this->payment_hash->hash.'-'.time(), - 'ButtonSource' => 'InvoiceNinja_SP', - 'solutionType' => 'Sole', - 'no_shipping' => $this->company_gateway->require_shipping_address ? 0 : 1, - ]; - } - - public function generatePaymentItems(array $data) - { - $_invoice = collect($this->payment_hash->data->invoices)->first(); - $invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id)); - - $items = []; - - $items[] = new Item([ - 'name' => ' ', - 'description' => ctrans('texts.invoice_number').'# '.$invoice->number, - 'price' => $data['total']['amount_with_fee'], - 'quantity' => 1, - ]); - - return $items; - } - - private function feeCalc($invoice, $invoice_total) - { - $invoice->service()->removeUnpaidGatewayFees(); - $invoice = $invoice->fresh(); - - $balance = floatval($invoice->balance); - - $_updated_invoice = $invoice->service()->addGatewayFee($this->company_gateway, GatewayType::PAYPAL, $invoice_total)->save(); - - if (floatval($_updated_invoice->balance) > $balance) { - $fee = floatval($_updated_invoice->balance) - $balance; - - $this->payment_hash->fee_total = $fee; - $this->payment_hash->save(); - - return $fee; - } - - return 0; - } - - public function livewirePaymentView(array $data): string - { - $this->processPaymentView($data); - - return ''; // Gateway is offsite. - } - - public function processPaymentViewData(array $data): array - { - return $data; - } -} diff --git a/app/Services/Bank/ProcessBankRules.php b/app/Services/Bank/ProcessBankRules.php index 17a6c8bdd8..61bef394c3 100644 --- a/app/Services/Bank/ProcessBankRules.php +++ b/app/Services/Bank/ProcessBankRules.php @@ -163,7 +163,7 @@ class ProcessBankRules extends AbstractService } if ($bank_transaction_rule['auto_convert']) { - (new MatchBankTransactions($this->company->id, $this->company->db, [ + (new MatchBankTransactions($this->bank_transaction->company->id, $this->bank_transaction->company->db, [ 'transactions' => [ [ 'id' => $this->bank_transaction->id, diff --git a/app/Services/Quickbooks/Jobs/QuickbooksSync.php b/app/Services/Quickbooks/Jobs/QuickbooksSync.php index 9a7d0dbac7..86057a06bd 100644 --- a/app/Services/Quickbooks/Jobs/QuickbooksSync.php +++ b/app/Services/Quickbooks/Jobs/QuickbooksSync.php @@ -66,7 +66,7 @@ class QuickbooksSync implements ShouldQueue { MultiDB::setDb($this->db); - $this->company = Company::find($this->company_id); + $this->company = Company::query()->find($this->company_id); $this->qbs = new QuickbooksService($this->company); $this->settings = $this->company->quickbooks->settings; diff --git a/app/Services/Quickbooks/QuickbooksService.php b/app/Services/Quickbooks/QuickbooksService.php index d61e1065bb..38d5bceae9 100644 --- a/app/Services/Quickbooks/QuickbooksService.php +++ b/app/Services/Quickbooks/QuickbooksService.php @@ -88,7 +88,7 @@ class QuickbooksService */ public function syncFromQb() { - QuickbooksSync::dispatch($this->company); + QuickbooksSync::dispatch($this->company->id, $this->company->db); } } diff --git a/routes/api.php b/routes/api.php index bbd67c59de..c0c02ceef0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -428,8 +428,6 @@ Route::group(['middleware' => ['throttle:api', 'api_db', 'token_auth', 'locale'] Route::get('nordigen/institutions', [NordigenController::class, 'institutions'])->name('nordigen.institutions'); - Route::post('import/quickbooks', [ImportQuickbooksController::class, 'import'])->name('import.quickbooks'); - }); Route::post('api/v1/sms_reset', [TwilioController::class, 'generate2faResetCode'])->name('sms_reset.generate')->middleware('throttle:3,1');