1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

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;
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++;

View File

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