Improve query efficiency

This commit is contained in:
David Bomba 2022-09-01 17:37:55 +10:00
parent db30327391
commit 59f5eca578
3 changed files with 31 additions and 42 deletions

View File

@ -966,7 +966,7 @@ class CheckData extends Command
{ {
$this->wrong_paid_status = 0; $this->wrong_paid_status = 0;
foreach(Invoice::with(['payments'])->whereHas('payments')->where('status_id', 4)->where('balance', '>', 0)->where('is_deleted',0)->cursor() as $invoice) foreach(Invoice::with(['payments'])->where('is_deleted',0)->where('balance', '>', 0)->whereHas('payments')->where('status_id', 4)->cursor() as $invoice)
{ {
$this->wrong_paid_status++; $this->wrong_paid_status++;

View File

@ -19,6 +19,14 @@ use App\Models\Country;
use App\Models\ExpenseCategory; use App\Models\ExpenseCategory;
use App\Models\PaymentType; use App\Models\PaymentType;
use App\Models\User; use App\Models\User;
use App\Models\Expense;
use App\Models\Project;
use App\Models\Invoice;
use App\Models\Quote;
use App\Models\Client;
use App\Models\TaxRate;
use App\Models\Product;
use App\Models\Vendor;
use App\Utils\Number; use App\Utils\Number;
use Exception; use Exception;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@ -67,8 +75,7 @@ class BaseTransformer
{ {
if (! empty($client_name)) { if (! empty($client_name)) {
$client_id_search = $this->company $client_id_search = Client::where('company_id', $this->company->id)
->clients()
->where('is_deleted', false) ->where('is_deleted', false)
->where('id_number', $client_name); ->where('id_number', $client_name);
@ -76,8 +83,7 @@ class BaseTransformer
return $client_id_search->first()->id; return $client_id_search->first()->id;
} }
$client_name_search = $this->company $client_name_search = Client::where('company_id', $this->company->id)
->clients()
->where('is_deleted', false) ->where('is_deleted', false)
->where('name', $client_name); ->where('name', $client_name);
@ -108,8 +114,7 @@ class BaseTransformer
*/ */
public function hasClient($name) public function hasClient($name)
{ {
return $this->company return Client::where('company_id', $this->company->id)
->clients()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -124,8 +129,7 @@ class BaseTransformer
*/ */
public function hasVendor($name) public function hasVendor($name)
{ {
return $this->company return Vendor::where('company_id', $this->company)
->vendors()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -140,8 +144,7 @@ class BaseTransformer
*/ */
public function hasProject($name) public function hasProject($name)
{ {
return $this->company return Project::where('company_id', $this->company->id)
->projects()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -156,8 +159,7 @@ class BaseTransformer
*/ */
public function hasProduct($key) public function hasProduct($key)
{ {
return $this->company return Product::where('company_id', $this->company->id)
->products()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $key)), strtolower(str_replace(' ', '', $key)),
@ -189,8 +191,7 @@ class BaseTransformer
*/ */
public function getClientId($name) public function getClientId($name)
{ {
$client = $this->company $client = Client::where('company_id', $this->company->id)
->clients()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -207,8 +208,7 @@ class BaseTransformer
*/ */
public function getProduct($key) public function getProduct($key)
{ {
$product = $this->company $product = Product::where('company_id', $this->company->id)
->products()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`product_key`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $key)), strtolower(str_replace(' ', '', $key)),
@ -225,8 +225,7 @@ class BaseTransformer
*/ */
public function getContact($email) public function getContact($email)
{ {
$contact = $this->company $contact = ClientContact::where('company_id', $this->company->id)
->client_contacts()
->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`email`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $email)), strtolower(str_replace(' ', '', $email)),
]) ])
@ -278,8 +277,7 @@ class BaseTransformer
{ {
$name = strtolower(trim($name)); $name = strtolower(trim($name));
$tax_rate = $this->company $tax_rate = TaxRate::where('company_id', $this->company->id)
->tax_rates()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -298,8 +296,7 @@ class BaseTransformer
{ {
$name = strtolower(trim($name)); $name = strtolower(trim($name));
$tax_rate = $this->company $tax_rate = TaxRate::where('company_id', $this->company->id)
->tax_rates()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -348,8 +345,7 @@ class BaseTransformer
*/ */
public function getInvoiceId($invoice_number) public function getInvoiceId($invoice_number)
{ {
$invoice = $this->company $invoice = Invoice::where('company_id', $this->company->id)
->invoices()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $invoice_number)), strtolower(str_replace(' ', '', $invoice_number)),
@ -366,8 +362,7 @@ class BaseTransformer
*/ */
public function hasInvoice($invoice_number) public function hasInvoice($invoice_number)
{ {
return $this->company return Invoice::where('company_id', $this->company->id)
->invoices()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $invoice_number)), strtolower(str_replace(' ', '', $invoice_number)),
@ -380,8 +375,7 @@ class BaseTransformer
*/ */
public function hasExpense($expense_number) public function hasExpense($expense_number)
{ {
return $this->company return Expense::where('company_id', $this->company->id)
->expenses()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $expense_number)), strtolower(str_replace(' ', '', $expense_number)),
@ -396,8 +390,7 @@ class BaseTransformer
*/ */
public function hasQuote($quote_number) public function hasQuote($quote_number)
{ {
return $this->company return Quote::where('company_id', $this->company->id)
->quotes()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $quote_number)), strtolower(str_replace(' ', '', $quote_number)),
@ -412,8 +405,7 @@ class BaseTransformer
*/ */
public function getInvoiceClientId($invoice_number) public function getInvoiceClientId($invoice_number)
{ {
$invoice = $this->company $invoice = Invoice::where('company_id', $this->company->id)
->invoices()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`number`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $invoice_number)), strtolower(str_replace(' ', '', $invoice_number)),
@ -430,8 +422,7 @@ class BaseTransformer
*/ */
public function getVendorId($name) public function getVendorId($name)
{ {
$vendor = $this->company $vendor = Vendor::where('company_id', $this->company->id)
->vendors()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -467,8 +458,7 @@ class BaseTransformer
*/ */
public function getExpenseCategoryId($name) public function getExpenseCategoryId($name)
{ {
$ec = $this->company $ec = ExpenseCategory::where('company_id', $this->company->id)
->expense_categories()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),
@ -504,8 +494,7 @@ class BaseTransformer
*/ */
public function getProjectId($name, $clientId = null) public function getProjectId($name, $clientId = null)
{ {
$project = $this->company $project = Project::where('company_id', $this->company->id)
->projects()
->where('is_deleted', false) ->where('is_deleted', false)
->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [ ->whereRaw("LOWER(REPLACE(`name`, ' ' ,'')) = ?", [
strtolower(str_replace(' ', '', $name)), strtolower(str_replace(' ', '', $name)),

View File

@ -60,11 +60,11 @@ class ReminderJob implements ShouldQueue
{ {
nlog('Sending invoice reminders '.now()->format('Y-m-d h:i:s')); nlog('Sending invoice reminders '.now()->format('Y-m-d h:i:s'));
Invoice::where('next_send_date', '<=', now()->toDateTimeString()) Invoice::where('is_deleted', 0)
->whereNull('deleted_at')
->where('is_deleted', 0)
->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL])
->whereNull('deleted_at')
->where('balance', '>', 0) ->where('balance', '>', 0)
->where('next_send_date', '<=', now()->toDateTimeString())
->whereHas('client', function ($query) { ->whereHas('client', function ($query) {
$query->where('is_deleted', 0) $query->where('is_deleted', 0)
->where('deleted_at', null); ->where('deleted_at', null);