diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index 8753e475762a..c52d6267aa39 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -432,10 +432,11 @@ class ClientController extends BaseController */ public function destroy(DestroyClientRequest $request, Client $client) { - //may not need these destroy routes as we are using actions to 'archive/delete' - $client->delete(); - return response()->json([], 200); + $this->client_repo->delete($client); + + return $this->itemResponse($client->fresh()); + } /** diff --git a/app/Http/Controllers/CompanyGatewayController.php b/app/Http/Controllers/CompanyGatewayController.php index c5572323bc12..3674f1aa4ed2 100644 --- a/app/Http/Controllers/CompanyGatewayController.php +++ b/app/Http/Controllers/CompanyGatewayController.php @@ -421,7 +421,7 @@ class CompanyGatewayController extends BaseController { $company_gateway->delete(); - return response()->json([], 200); + return $this->itemResponse($company_gateway->fresh()); } /** diff --git a/app/Http/Controllers/CreditController.php b/app/Http/Controllers/CreditController.php index 3cec7da3fb94..30019c4ed99c 100644 --- a/app/Http/Controllers/CreditController.php +++ b/app/Http/Controllers/CreditController.php @@ -420,9 +420,9 @@ class CreditController extends BaseController */ public function destroy(DestroyCreditRequest $request, Credit $credit) { - $credit->delete(); + $this->credit_repository->delete($credit); - return response()->json([], 200); + return $this->itemResponse($credit->fresh()); } /** diff --git a/app/Http/Controllers/DesignController.php b/app/Http/Controllers/DesignController.php index d306303f3f64..520783a9ad1e 100644 --- a/app/Http/Controllers/DesignController.php +++ b/app/Http/Controllers/DesignController.php @@ -415,7 +415,7 @@ class DesignController extends BaseController $design->delete(); $design->save(); - return response()->json([], 200); + return $this->itemResponse($design->fresh()); } /** diff --git a/app/Http/Controllers/GroupSettingController.php b/app/Http/Controllers/GroupSettingController.php index c56f9c0b584d..1f79f7838dc8 100644 --- a/app/Http/Controllers/GroupSettingController.php +++ b/app/Http/Controllers/GroupSettingController.php @@ -414,7 +414,7 @@ class GroupSettingController extends BaseController { $group_setting->delete(); - return response()->json([], 200); + return $this->itemResponse($group_setting->fresh()); } /** diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index a986afc1a28a..ba5f07d7cab3 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -451,9 +451,9 @@ class InvoiceController extends BaseController */ public function destroy(DestroyInvoiceRequest $request, Invoice $invoice) { - $invoice->delete(); + $this->invoice_repo->delete($invoice); - return response()->json([], 200); + return $this->itemResponse($invoice->fresh()); } /** diff --git a/app/Http/Controllers/PaymentTermController.php b/app/Http/Controllers/PaymentTermController.php index 8a0c114b9cbf..1f345cf6154c 100644 --- a/app/Http/Controllers/PaymentTermController.php +++ b/app/Http/Controllers/PaymentTermController.php @@ -397,7 +397,7 @@ class PaymentTermController extends BaseController { $payment_term->delete(); - return response()->json([], 200); + return $this->itemResponse($payment_term->fresh()); } /** diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 6de6394b1fc1..97086618f1ed 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -429,7 +429,7 @@ class ProjectController extends BaseController $project->delete(); $project->save(); - return response()->json([], 200); + return $this->itemResponse($project->fresh()); } /** diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 23de4f7aeb06..0aca87c000d2 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -441,9 +441,9 @@ class QuoteController extends BaseController */ public function destroy(DestroyQuoteRequest $request, Quote $quote) { - $quote->delete(); + $this->quote_repo->delete($quote); - return response()->json([], 200); + return $this->itemResponse($quote->fresh()); } /** diff --git a/app/Http/Controllers/RecurringInvoiceController.php b/app/Http/Controllers/RecurringInvoiceController.php index 96d32cc3a698..aae11052205a 100644 --- a/app/Http/Controllers/RecurringInvoiceController.php +++ b/app/Http/Controllers/RecurringInvoiceController.php @@ -426,9 +426,9 @@ class RecurringInvoiceController extends BaseController */ public function destroy(DestroyRecurringInvoiceRequest $request, RecurringInvoice $recurring_invoice) { - $recurring_invoice->delete(); + $this->recurring_invoice_repo->delete($recurring_invoice); - return response()->json([], 200); + return $this->itemResponse($recurring_invoice->fresh()); } diff --git a/app/Http/Controllers/RecurringQuoteController.php b/app/Http/Controllers/RecurringQuoteController.php index 8370b2e26488..cd02e058f044 100644 --- a/app/Http/Controllers/RecurringQuoteController.php +++ b/app/Http/Controllers/RecurringQuoteController.php @@ -428,9 +428,9 @@ class RecurringQuoteController extends BaseController */ public function destroy(DestroyRecurringQuoteRequest $request, RecurringQuote $recurring_quote) { - $recurring_quote->delete(); + $this->recurring_quote_repo->delete($recurring_quote); - return response()->json([], 200); + return $this->itemResponse($recurring_quote->fresh()); } /** diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index fcdba38be96c..0b64615be59f 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -425,9 +425,9 @@ class TaskController extends BaseController public function destroy(DestroyTaskRequest $request, Task $task) { //may not need these destroy routes as we are using actions to 'archive/delete' - $task->delete(); + $this->task_repo->delete($task); - return response()->json([], 200); + return $this->itemResponse($task->fresh()); } /** diff --git a/app/Http/Controllers/TaskStatusController.php b/app/Http/Controllers/TaskStatusController.php index 7bae5b0f4b18..4934e0b5fa90 100644 --- a/app/Http/Controllers/TaskStatusController.php +++ b/app/Http/Controllers/TaskStatusController.php @@ -400,7 +400,7 @@ class TaskStatusController extends BaseController { $task_status->delete(); - return response()->json([], 200); + return $this->itemResponse($task_status->fresh()); } /** diff --git a/app/Http/Controllers/VendorController.php b/app/Http/Controllers/VendorController.php index a92986594abe..f9ce40c6cb92 100644 --- a/app/Http/Controllers/VendorController.php +++ b/app/Http/Controllers/VendorController.php @@ -432,7 +432,7 @@ class VendorController extends BaseController //may not need these destroy routes as we are using actions to 'archive/delete' $vendor->delete(); - return response()->json([], 200); + return $this->itemResponse($vendor->fresh()); } /** diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index aab84fb8e935..1429b3e02444 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -60,7 +60,7 @@ class BaseTransformer if ($code) { $currency = $this->maps['currencies']->where('code', $code)->first(); - if ($currency_id) { + if ($currency) { return $currency->id; } } diff --git a/app/Jobs/RecurringInvoice/SendRecurring.php b/app/Jobs/RecurringInvoice/SendRecurring.php index b66f1a164ae1..0b3f1b52412d 100644 --- a/app/Jobs/RecurringInvoice/SendRecurring.php +++ b/app/Jobs/RecurringInvoice/SendRecurring.php @@ -74,6 +74,11 @@ class SendRecurring implements ShouldQueue } }); + //Admin notification for recurring invoice sent. + if ($invoice->invitations->count() >= 1) { + $invoice->entityEmailEvent($invoice->invitations->first(), 'invoice', 'email_template_invoice'); + } + if ($invoice->client->getSetting('auto_bill_date') == 'on_send_date' && $this->recurring_invoice->auto_bill_enabled) { $invoice->service()->autoBill()->save(); } diff --git a/app/Jobs/Util/WebhookHandler.php b/app/Jobs/Util/WebhookHandler.php index 60a5261b8a64..df86c38ed918 100644 --- a/app/Jobs/Util/WebhookHandler.php +++ b/app/Jobs/Util/WebhookHandler.php @@ -108,15 +108,29 @@ class WebhookHandler implements ShouldQueue $client = new Client(['headers' => array_merge($base_headers, $headers)]); + try { + $response = $client->post($subscription->target_url, [ RequestOptions::JSON => $data, // or 'json' => [...] ]); - if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) { - $subscription->delete(); - } + if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) + $subscription->delete(); - SystemLogger::dispatch( + SystemLogger::dispatch( + $response, + SystemLog::CATEGORY_WEBHOOK, + SystemLog::EVENT_WEBHOOK_RESPONSE, + SystemLog::TYPE_WEBHOOK_RESPONSE, + $this->company->clients->first(), + ); + + } + catch(\Exception $e){ + + // nlog($e->getMessage()); + + SystemLogger::dispatch( $e->getMessage(), SystemLog::CATEGORY_WEBHOOK, SystemLog::EVENT_WEBHOOK_RESPONSE, @@ -124,6 +138,10 @@ class WebhookHandler implements ShouldQueue $this->company->clients->first(), ); + } + + + } public function failed($exception) diff --git a/app/Services/Invoice/AddGatewayFee.php b/app/Services/Invoice/AddGatewayFee.php index 834dc4fc2b5b..4cadb94bd498 100644 --- a/app/Services/Invoice/AddGatewayFee.php +++ b/app/Services/Invoice/AddGatewayFee.php @@ -43,16 +43,17 @@ class AddGatewayFee extends AbstractService { $gateway_fee = round($this->company_gateway->calcGatewayFee($this->amount, $this->gateway_type_id, $this->invoice->uses_inclusive_taxes), $this->invoice->client->currency()->precision); - if ((int)$gateway_fee == 0) { + if ((int)$gateway_fee == 0) return $this->invoice; - } + // Removes existing stale gateway fees $this->cleanPendingGatewayFees(); - if ($gateway_fee > 0) { + // If a gateway fee is > 0 insert the line item + if ($gateway_fee > 0) return $this->processGatewayFee($gateway_fee); - } - + + // If we have reached this far, then we are apply a gateway discount return $this->processGatewayDiscount($gateway_fee); } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 86743bea4970..e2b61215eca7 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -88,7 +88,7 @@ class AutoBillInvoice extends AbstractService /* Build payment hash */ $payment_hash = PaymentHash::create([ 'hash' => Str::random(128), - 'data' => [['invoice_id' => $this->invoice->hashed_id, 'amount' => $amount]], + 'data' => ['invoices' => [['invoice_id' => $this->invoice->hashed_id, 'amount' => $amount]]], 'fee_total' => $fee, 'fee_invoice_id' => $this->invoice->id, ]); @@ -252,17 +252,51 @@ class AutoBillInvoice extends AbstractService * @param float $amount The amount to charge * @return ClientGatewayToken The client gateway token */ - private function getGateway($amount) - { - $gateway_tokens = $this->client->gateway_tokens()->orderBy('is_default', 'DESC')->get(); + // private function + // { + // $gateway_tokens = $this->client->gateway_tokens()->orderBy('is_default', 'DESC')->get(); - foreach ($gateway_tokens as $gateway_token) { - if ($this->validGatewayLimits($gateway_token, $amount)) { - return $gateway_token; + // foreach ($gateway_tokens as $gateway_token) { + // if ($this->validGatewayLimits($gateway_token, $amount)) { + // return $gateway_token; + // } + // } + // } + + public function getGateway($amount) + { + + //get all client gateway tokens and set the is_default one to the first record + //$gateway_tokens = $this->client->gateway_tokens()->orderBy('is_default', 'DESC'); + $gateway_tokens = $this->client->gateway_tokens; + + $filtered_gateways = $gateway_tokens->filter(function ($gateway_token) use($amount) { + + $company_gateway = $gateway_token->gateway; + + //check if fees and limits are set + if (isset($company_gateway->fees_and_limits) && property_exists($company_gateway->fees_and_limits, $gateway_token->gateway_type_id)) + { + //if valid we keep this gateway_token + if ($this->invoice->client->validGatewayForAmount($company_gateway->fees_and_limits->{$gateway_token->gateway_type_id}, $amount)) + return true; + else + return false; + } - } + return true; //if no fees_and_limits set then we automatically must add this gateway + + }); + + if($filtered_gateways->count() >= 1) + return $filtered_gateways->first(); + + return false; } + + + /** * Adds a gateway fee to the invoice. * @@ -332,33 +366,33 @@ class AutoBillInvoice extends AbstractService // return $this; // } - /** - * Checks whether a given gateway token is able - * to process the payment after passing through the - * fees and limits check. - * - * @param CompanyGateway $cg The CompanyGateway instance - * @param float $amount The amount to be paid - * @return bool - */ - public function validGatewayLimits($cg, $amount) : bool - { - if (isset($cg->fees_and_limits)) { - $fees_and_limits = $cg->fees_and_limits->{'1'}; - } else { - return true; - } + // /** + // * Checks whether a given gateway token is able + // * to process the payment after passing through the + // * fees and limits check. + // * + // * @param CompanyGateway $cg The CompanyGateway instance + // * @param float $amount The amount to be paid + // * @return bool + // */ + // public function validGatewayLimits($cg, $amount) : bool + // { + // if (isset($cg->fees_and_limits)) { + // $fees_and_limits = $cg->fees_and_limits->{'1'}; + // } else { + // return true; + // } - if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) { - info("amount {$amount} less than ".$fees_and_limits->min_limit); - $passes = false; - } elseif ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->max_limit) { - info("amount {$amount} greater than ".$fees_and_limits->max_limit); - $passes = false; - } else { - $passes = true; - } + // if ((property_exists($fees_and_limits, 'min_limit')) && $fees_and_limits->min_limit !== null && $amount < $fees_and_limits->min_limit) { + // info("amount {$amount} less than ".$fees_and_limits->min_limit); + // $passes = false; + // } elseif ((property_exists($fees_and_limits, 'max_limit')) && $fees_and_limits->max_limit !== null && $amount > $fees_and_limits->max_limit) { + // info("amount {$amount} greater than ".$fees_and_limits->max_limit); + // $passes = false; + // } else { + // $passes = true; + // } - return $passes; - } + // return $passes; + // } } diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index b0111f634ac1..603bb30dc269 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -376,6 +376,10 @@ class InvoiceService $this->invoice->terms = $settings->invoice_terms; } + if(!isset($this->invoice->public_notes)) { + $this->invoice->public_notes = $settings->public_notes; + } + return $this; } diff --git a/database/migrations/2021_01_17_040331_change_custom_surcharge_column_type.php b/database/migrations/2021_01_17_040331_change_custom_surcharge_column_type.php new file mode 100644 index 000000000000..13ab32f111a8 --- /dev/null +++ b/database/migrations/2021_01_17_040331_change_custom_surcharge_column_type.php @@ -0,0 +1,57 @@ +decimal('custom_surcharge1', 20,6)->change(); + $table->decimal('custom_surcharge2', 20,6)->change(); + $table->decimal('custom_surcharge3', 20,6)->change(); + $table->decimal('custom_surcharge4', 20,6)->change(); + }); + + Schema::table('recurring_invoices', function (Blueprint $table) { + $table->decimal('custom_surcharge1', 20,6)->change(); + $table->decimal('custom_surcharge2', 20,6)->change(); + $table->decimal('custom_surcharge3', 20,6)->change(); + $table->decimal('custom_surcharge4', 20,6)->change(); + }); + + + Schema::table('quotes', function (Blueprint $table) { + $table->decimal('custom_surcharge1', 20,6)->change(); + $table->decimal('custom_surcharge2', 20,6)->change(); + $table->decimal('custom_surcharge3', 20,6)->change(); + $table->decimal('custom_surcharge4', 20,6)->change(); + }); + + + Schema::table('credits', function (Blueprint $table) { + $table->decimal('custom_surcharge1', 20,6)->change(); + $table->decimal('custom_surcharge2', 20,6)->change(); + $table->decimal('custom_surcharge3', 20,6)->change(); + $table->decimal('custom_surcharge4', 20,6)->change(); + }); + + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + // + } +}