mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-31 17:27:33 -04:00 
			
		
		
		
	Fixes for DB:raw()
This commit is contained in:
		
							parent
							
								
									62853c1dcb
								
							
						
					
					
						commit
						f7f4804e34
					
				| @ -549,7 +549,7 @@ class CheckData extends Command | ||||
| 
 | ||||
|     private function clientPaidToDateQuery() | ||||
|     { | ||||
|         $results = \DB::select(\DB::raw(" | ||||
|         $results = \DB::select(" | ||||
|          SELECT  | ||||
|          clients.id as client_id,  | ||||
|          clients.paid_to_date as client_paid_to_date, | ||||
| @ -564,14 +564,14 @@ class CheckData extends Command | ||||
|          GROUP BY clients.id | ||||
|          HAVING payments_applied != client_paid_to_date | ||||
|          ORDER BY clients.id; | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()));
 | ||||
|         ");
 | ||||
|      | ||||
|         return $results; | ||||
|     } | ||||
| 
 | ||||
|     private function clientCreditPaymentables($client) | ||||
|     { | ||||
|         $results = \DB::select(\DB::raw(" | ||||
|         $results = \DB::select(" | ||||
|         SELECT  | ||||
|         SUM(paymentables.amount - paymentables.refunded) as credit_payment | ||||
|         FROM payments | ||||
| @ -583,7 +583,7 @@ class CheckData extends Command | ||||
|         AND paymentables.amount > 0 | ||||
|         AND payments.is_deleted = 0 | ||||
|         AND payments.client_id = ?; | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [App\Models\Credit::class, $client->id]);
 | ||||
|         ", [App\Models\Credit::class, $client->id]);
 | ||||
|      | ||||
|         return $results; | ||||
|     } | ||||
| @ -621,108 +621,9 @@ class CheckData extends Command | ||||
|         $this->logMessage("{$this->wrong_paid_to_dates} clients with incorrect paid to dates"); | ||||
|    } | ||||
| 
 | ||||
|     private function checkPaidToDates() | ||||
|     { | ||||
|         $this->wrong_paid_to_dates = 0; | ||||
|         $credit_total_applied = 0; | ||||
| 
 | ||||
|         $clients = DB::table('clients') | ||||
|                     ->leftJoin('payments', function ($join) { | ||||
|                         $join->on('payments.client_id', '=', 'clients.id') | ||||
|                             ->where('payments.is_deleted', 0) | ||||
|                             ->whereIn('payments.status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]); | ||||
|                     }) | ||||
|                     ->where('clients.is_deleted', 0) | ||||
|                     ->where('clients.updated_at', '>', now()->subDays(2)) | ||||
|                     ->groupBy('clients.id') | ||||
|                     ->havingRaw('clients.paid_to_date != sum(coalesce(payments.amount - payments.refunded, 0))') | ||||
|                     ->get(['clients.id', 'clients.paid_to_date', DB::raw('sum(coalesce(payments.amount - payments.refunded, 0)) as amount')->getValue(DB::connection()->getQueryGrammar())]); | ||||
| 
 | ||||
|         /* Due to accounting differences we need to perform a second loop here to ensure there actually is an issue */ | ||||
|         $clients->each(function ($client_record) use ($credit_total_applied) { | ||||
|             $client = Client::withTrashed()->find($client_record->id); | ||||
| 
 | ||||
|             $total_invoice_payments = 0; | ||||
| 
 | ||||
|             foreach ($client->invoices()->where('is_deleted', false)->where('status_id', '>', 1)->get() as $invoice) { | ||||
|                 $total_invoice_payments += $invoice->payments() | ||||
|                                                     ->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) | ||||
|                                                     ->selectRaw('sum(paymentables.amount - paymentables.refunded) as p') | ||||
|                                                     ->pluck('p') | ||||
|                                                     ->first(); | ||||
|             } | ||||
| 
 | ||||
|             //commented IN 27/06/2021 - sums ALL client payments AND the unapplied amounts to match the client paid to date
 | ||||
|             $p = Payment::where('client_id', $client->id) | ||||
|             ->where('is_deleted', 0) | ||||
|             ->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) | ||||
|             // ->sum(DB::Raw('amount - applied')->getValue(DB::connection()->getQueryGrammar()));
 | ||||
|             ->selectRaw('SUM(payments.amount - payments.applied) as amount')->first()->amount ?? 0; | ||||
|             $total_invoice_payments += $p; | ||||
| 
 | ||||
|             // 10/02/21
 | ||||
|             foreach ($client->payments as $payment) { | ||||
|                 $credit_total_applied += $payment->paymentables() | ||||
|                                                 ->where('paymentable_type', App\Models\Credit::class) | ||||
|                                                 ->selectRaw('sum(paymentables.amount - paymentables.refunded) as p') | ||||
|                                                 ->pluck('p') | ||||
|                                                 ->first(); | ||||
|             } | ||||
| 
 | ||||
|             if ($credit_total_applied < 0) { | ||||
|                 $total_invoice_payments += $credit_total_applied; | ||||
|             } | ||||
| 
 | ||||
|             if (round($total_invoice_payments, 2) != round($client->paid_to_date, 2)) { | ||||
|                 $this->wrong_paid_to_dates++; | ||||
| 
 | ||||
|                 $this->logMessage($client->present()->name().' id = # '.$client->id." - Paid to date does not match Client Paid To Date = {$client->paid_to_date} - Invoice Payments = {$total_invoice_payments}"); | ||||
| 
 | ||||
|                 $this->isValid = false; | ||||
| 
 | ||||
|                 if ($this->option('paid_to_date')) { | ||||
|                     $this->logMessage("# {$client->id} " . $client->present()->name().' - '.$client->number." Fixing {$client->paid_to_date} to {$total_invoice_payments}"); | ||||
|                     $client->paid_to_date = $total_invoice_payments; | ||||
|                     $client->save(); | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
| 
 | ||||
|         $this->logMessage("{$this->wrong_paid_to_dates} clients with incorrect paid to dates"); | ||||
|     } | ||||
| 
 | ||||
|     private function checkInvoicePayments() | ||||
|     { | ||||
|         $this->wrong_balances = 0; | ||||
| 
 | ||||
|         Client::query()->cursor()->where('is_deleted', 0)->where('clients.updated_at', '>', now()->subDays(2))->each(function ($client) { | ||||
|             $client->invoices->where('is_deleted', false)->whereIn('status_id', '!=', Invoice::STATUS_DRAFT)->each(function ($invoice) use ($client) { | ||||
|                 $total_paid = $invoice->payments() | ||||
|                                     ->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment::STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED]) | ||||
|                                     ->selectRaw('sum(paymentables.amount - paymentables.refunded) as p') | ||||
|                                     ->pluck('p') | ||||
|                                     ->first(); | ||||
| 
 | ||||
|                 $total_credit = $invoice->credits()->get()->sum('amount'); | ||||
| 
 | ||||
|                 $calculated_paid_amount = $invoice->amount - $invoice->balance - $total_credit; | ||||
| 
 | ||||
|                 if ((string)$total_paid != (string)($invoice->amount - $invoice->balance - $total_credit)) { | ||||
|                     $this->wrong_balances++; | ||||
| 
 | ||||
|                     $this->logMessage($client->present()->name().' - '.$client->id." - Total Paid = {$total_paid} != Calculated Total = {$calculated_paid_amount}"); | ||||
| 
 | ||||
|                     $this->isValid = false; | ||||
|                 } | ||||
|             }); | ||||
|         }); | ||||
| 
 | ||||
|         $this->logMessage("{$this->wrong_balances} clients with incorrect invoice balances"); | ||||
|     } | ||||
| 
 | ||||
|     private function clientBalanceQuery() | ||||
|     { | ||||
|         $results = \DB::select(\DB::raw(" | ||||
|         $results = \DB::select(" | ||||
|         SELECT          | ||||
|         SUM(invoices.balance) as invoice_balance,  | ||||
|         clients.id as client_id,  | ||||
| @ -736,7 +637,7 @@ class CheckData extends Command | ||||
|         GROUP BY clients.id | ||||
|         HAVING invoice_balance != clients.balance | ||||
|         ORDER BY clients.id; | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()));
 | ||||
|         ");
 | ||||
|      | ||||
|         return $results; | ||||
|     } | ||||
| @ -813,7 +714,7 @@ class CheckData extends Command | ||||
| 
 | ||||
|     private function invoiceBalanceQuery() | ||||
|     { | ||||
|         $results = \DB::select(\DB::raw(" | ||||
|         $results = \DB::select(" | ||||
|         SELECT  | ||||
|         clients.id, | ||||
|         clients.balance, | ||||
| @ -827,7 +728,7 @@ class CheckData extends Command | ||||
|         GROUP BY clients.id | ||||
|         HAVING(invoices_balance != clients.balance) | ||||
|         ORDER BY clients.id; | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()));
 | ||||
|         ");
 | ||||
|      | ||||
|         return $results; | ||||
|     } | ||||
| @ -961,7 +862,7 @@ class CheckData extends Command | ||||
|                 } | ||||
|                 $records = DB::table($table) | ||||
|                                 ->join($tableName, "{$tableName}.id", '=', "{$table}.{$field}_id") | ||||
|                                 ->where("{$table}.{$company_id}", '!=', DB::raw("{$tableName}.company_id")) | ||||
|                                 ->where("{$table}.{$company_id}", '!=', "{$tableName}.company_id") | ||||
|                                 ->get(["{$table}.id"]); | ||||
| 
 | ||||
|                 if ($records->count()) { | ||||
| @ -971,11 +872,6 @@ class CheckData extends Command | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // foreach(User::cursor() as $user) {
 | ||||
| 
 | ||||
|         //     $records = Company::where('account_id',)
 | ||||
| 
 | ||||
|         // }
 | ||||
|     } | ||||
| 
 | ||||
|     public function pluralizeEntityType($type) | ||||
|  | ||||
| @ -746,12 +746,7 @@ class BaseImport | ||||
|         if($user) | ||||
|             return $user->id; | ||||
| 
 | ||||
|         $user = User::where('account_id', $this->company->account->id) | ||||
|             ->where( | ||||
|                 \DB::raw('CONCAT_WS(" ", first_name, last_name)')->getValue(\DB::connection()->getQueryGrammar()), | ||||
|                 'like', | ||||
|                 '%'.$user_hash.'%' | ||||
|             ) | ||||
|         $user = User::whereRaw("account_id = ? AND CONCAT_WS(' ', first_name, last_name) like ?", [$this->company->account_id, '%'.$user_hash.'%']) | ||||
|             ->first(); | ||||
| 
 | ||||
|         if ($user) { | ||||
|  | ||||
| @ -248,19 +248,6 @@ class CheckCompanyData implements ShouldQueue | ||||
|             $this->is_valid = false; | ||||
|         } | ||||
| 
 | ||||
|         // if ($this->option('fix') == 'true') {
 | ||||
|         //     foreach ($clients as $client) {
 | ||||
|         //         $contact = new ClientContact();
 | ||||
|         //         $contact->company_id = $client->company_id;
 | ||||
|         //         $contact->user_id = $client->user_id;
 | ||||
|         //         $contact->client_id = $client->id;
 | ||||
|         //         $contact->is_primary = true;
 | ||||
|         //         $contact->send_invoice = true;
 | ||||
|         //         $contact->contact_key = str_random(config('ninja.key_length'));
 | ||||
|         //         $contact->save();
 | ||||
|         //     }
 | ||||
|         // }
 | ||||
| 
 | ||||
|         // check for more than one primary contact
 | ||||
|         $clients = DB::table('clients') | ||||
|                     ->where('clients.company_id', $this->company->id) | ||||
| @ -272,11 +259,7 @@ class CheckCompanyData implements ShouldQueue | ||||
|                     ->groupBy('clients.id') | ||||
|                     ->havingRaw('count(client_contacts.id) != 1'); | ||||
| 
 | ||||
|         // if ($this->option('client_id')) {
 | ||||
|         //     $clients->where('clients.id', '=', $this->option('client_id'));
 | ||||
|         // }
 | ||||
| 
 | ||||
|         $clients = $clients->get(['clients.id', DB::raw('count(client_contacts.id)')->getValue(DB::connection()->getQueryGrammar())]); | ||||
|         $clients = $clients->get(['clients.id', DB::raw('count(client_contacts.id)')]); | ||||
|         $this->company_data[] = $clients->count().' clients without a single primary contact'; | ||||
| 
 | ||||
|         if ($clients->count() > 0) { | ||||
| @ -315,7 +298,7 @@ class CheckCompanyData implements ShouldQueue | ||||
|                 } | ||||
|                 $records = DB::table($table) | ||||
|                                 ->join($tableName, "{$tableName}.id", '=', "{$table}.{$field}_id") | ||||
|                                 ->where("{$table}.{$company_id}", '!=', DB::raw("{$tableName}.company_id")->getValue(DB::connection()->getQueryGrammar())) | ||||
|                                 ->where("{$table}.{$company_id}", '!=', "{$tableName}.company_id") | ||||
|                                 ->get(["{$table}.id"]); | ||||
| 
 | ||||
|                 if ($records->count()) { | ||||
|  | ||||
| @ -302,7 +302,7 @@ class Import implements ShouldQueue | ||||
| 
 | ||||
|             // 10/02/21
 | ||||
|             foreach ($client->payments as $payment) { | ||||
|                 $credit_total_applied += $payment->paymentables()->where('paymentable_type', \App\Models\Credit::class)->get()->sum(\DB::raw('amount')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                 $credit_total_applied += $payment->paymentables()->where('paymentable_type', \App\Models\Credit::class)->get()->sum('amount'); | ||||
|             } | ||||
| 
 | ||||
|             if ($credit_total_applied < 0) { | ||||
|  | ||||
| @ -25,7 +25,7 @@ trait ChartQueries | ||||
|     { | ||||
|         $user_filter = $this->is_admin ? '' : 'AND expenses.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT sum(expenses.amount) as amount, | ||||
|             IFNULL(expenses.currency_id, :company_currency) as currency_id | ||||
|             FROM expenses | ||||
| @ -34,7 +34,7 @@ trait ChartQueries | ||||
|             AND (expenses.date BETWEEN :start_date AND :end_date) | ||||
|             {$user_filter} | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|         ", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     public function getExpenseChartQuery($start_date, $end_date, $currency_id) | ||||
| @ -42,7 +42,7 @@ trait ChartQueries | ||||
| 
 | ||||
|         $user_filter = $this->is_admin ? '' : 'AND expenses.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(expenses.amount) as total, | ||||
|             expenses.date, | ||||
| @ -54,7 +54,7 @@ trait ChartQueries | ||||
|             {$user_filter} | ||||
|             GROUP BY expenses.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [
 | ||||
|         ", [
 | ||||
|             'company_currency' => $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
| @ -71,7 +71,7 @@ trait ChartQueries | ||||
|          | ||||
|         $user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT sum(payments.amount) as amount, | ||||
|             IFNULL(payments.currency_id, :company_currency) as currency_id | ||||
|             FROM payments | ||||
| @ -80,7 +80,7 @@ trait ChartQueries | ||||
|             AND payments.company_id = :company_id | ||||
|             AND (payments.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [
 | ||||
|         ", [
 | ||||
|             'company_currency' => $this->company->settings->currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
|             'start_date' => $start_date, | ||||
| @ -93,7 +93,7 @@ trait ChartQueries | ||||
| 
 | ||||
|         $user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(payments.amount - payments.refunded) as total, | ||||
|             payments.date, | ||||
| @ -106,7 +106,7 @@ trait ChartQueries | ||||
|             AND (payments.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY payments.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [
 | ||||
|         ", [
 | ||||
|             'company_currency' => $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
| @ -123,7 +123,7 @@ trait ChartQueries | ||||
|          | ||||
|         $user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.balance) as amount, | ||||
|             COUNT(*) as outstanding_count,  | ||||
| @ -139,14 +139,14 @@ trait ChartQueries | ||||
|             AND invoices.balance > 0 | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|         ", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     public function getRevenueQueryX($start_date, $end_date) | ||||
|     { | ||||
|         $user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.paid_to_date) as paid_to_date, | ||||
|             IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id | ||||
| @ -161,14 +161,14 @@ trait ChartQueries | ||||
|             AND invoices.status_id IN (3,4) | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|         ", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     public function getRevenueQuery($start_date, $end_date) | ||||
|     { | ||||
|         $user_filter = $this->is_admin ? '' : 'AND payments.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select((" | ||||
|             SELECT | ||||
|             sum(payments.amount - payments.refunded) as paid_to_date, | ||||
|             payments.currency_id AS currency_id | ||||
| @ -179,14 +179,14 @@ trait ChartQueries | ||||
|             AND payments.status_id IN (1,4,5,6) | ||||
|             AND (payments.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY payments.currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|         ", ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     public function getInvoicesQuery($start_date, $end_date) | ||||
|     { | ||||
|         $user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.amount) as invoiced_amount, | ||||
|             IFNULL(CAST(JSON_UNQUOTE(JSON_EXTRACT( clients.settings, '$.currency_id' )) AS SIGNED), :company_currency) AS currency_id | ||||
| @ -201,14 +201,14 @@ trait ChartQueries | ||||
|             AND invoices.is_deleted = 0 | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|         ", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     public function getOutstandingChartQuery($start_date, $end_date, $currency_id) | ||||
|     { | ||||
|         $user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.balance) as total, | ||||
|             invoices.date, | ||||
| @ -224,7 +224,7 @@ trait ChartQueries | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY invoices.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [
 | ||||
|         ", [
 | ||||
|             'company_currency' => (int) $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
| @ -238,7 +238,7 @@ trait ChartQueries | ||||
|     { | ||||
|         $user_filter = $this->is_admin ? '' : 'AND clients.user_id = '.$this->user->id; | ||||
| 
 | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.amount) as total, | ||||
|             invoices.date, | ||||
| @ -254,7 +254,7 @@ trait ChartQueries | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY invoices.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [
 | ||||
|         ", [
 | ||||
|             'company_currency' => (int) $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
|  | ||||
| @ -24,7 +24,7 @@ trait ChartQueriesLegacy | ||||
|     public function getExpenseQuery($start_date, $end_date) | ||||
|     { | ||||
|          | ||||
|         return DB::select(DB::raw(' | ||||
|         return DB::select(' | ||||
|             SELECT sum(expenses.amount) as amount, | ||||
|             IFNULL(expenses.currency_id, :company_currency) as currency_id | ||||
|             FROM expenses | ||||
| @ -32,12 +32,12 @@ trait ChartQueriesLegacy | ||||
|             AND expenses.company_id = :company_id | ||||
|             AND (expenses.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ')->getValue(DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]); | ||||
|         ', ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]); | ||||
|     } | ||||
| 
 | ||||
|     public function getExpenseChartQuery($start_date, $end_date, $currency_id) | ||||
|     { | ||||
|         return DB::select(DB::raw(' | ||||
|         return DB::select(' | ||||
|             SELECT | ||||
|             sum(expenses.amount) as total, | ||||
|             expenses.date, | ||||
| @ -48,7 +48,7 @@ trait ChartQueriesLegacy | ||||
|             AND expenses.is_deleted = 0 | ||||
|             GROUP BY expenses.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ')->getValue(DB::connection()->getQueryGrammar()), [ | ||||
|         ', [ | ||||
|             'company_currency' => $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
| @ -62,7 +62,7 @@ trait ChartQueriesLegacy | ||||
|      */ | ||||
|     public function getPaymentQuery($start_date, $end_date) | ||||
|     { | ||||
|         return DB::select(DB::raw(' | ||||
|         return DB::select(' | ||||
|             SELECT sum(payments.amount) as amount, | ||||
|             IFNULL(payments.currency_id, :company_currency) as currency_id | ||||
|             FROM payments | ||||
| @ -70,7 +70,7 @@ trait ChartQueriesLegacy | ||||
|             AND payments.company_id = :company_id | ||||
|             AND (payments.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ')->getValue(DB::connection()->getQueryGrammar()), [ | ||||
|         ', [ | ||||
|             'company_currency' => $this->company->settings->currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
|             'start_date' => $start_date, | ||||
| @ -80,7 +80,7 @@ trait ChartQueriesLegacy | ||||
| 
 | ||||
|     public function getPaymentChartQuery($start_date, $end_date, $currency_id) | ||||
|     { | ||||
|         return DB::select(DB::raw(' | ||||
|         return DB::select(' | ||||
|             SELECT | ||||
|             sum(payments.amount - payments.refunded) as total, | ||||
|             payments.date, | ||||
| @ -92,7 +92,7 @@ trait ChartQueriesLegacy | ||||
|             AND payments.is_deleted = 0 | ||||
|             GROUP BY payments.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ')->getValue(DB::connection()->getQueryGrammar()), [ | ||||
|         ', [ | ||||
|             'company_currency' => $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
| @ -106,7 +106,7 @@ trait ChartQueriesLegacy | ||||
|      */ | ||||
|     public function getOutstandingQuery($start_date, $end_date) | ||||
|     { | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.balance) as amount, | ||||
|             IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id | ||||
| @ -120,12 +120,12 @@ trait ChartQueriesLegacy | ||||
|             AND invoices.is_deleted = 0 | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|         ", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     public function getRevenueQuery($start_date, $end_date) | ||||
|     { | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.paid_to_date) as paid_to_date, | ||||
|             IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id | ||||
| @ -144,7 +144,7 @@ trait ChartQueriesLegacy | ||||
| 
 | ||||
|     public function getInvoiceChartQuery($start_date, $end_date, $currency_id) | ||||
|     { | ||||
|         return DB::select(DB::raw(" | ||||
|         return DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.amount) as total, | ||||
|             invoices.date, | ||||
| @ -159,7 +159,7 @@ trait ChartQueriesLegacy | ||||
|             AND invoices.is_deleted = 0 | ||||
|             GROUP BY invoices.date | ||||
|             HAVING currency_id = :currency_id | ||||
|         ")->getValue(DB::connection()->getQueryGrammar()), [
 | ||||
|         ", [
 | ||||
|             'company_currency' => (int) $this->company->settings->currency_id, | ||||
|             'currency_id' => $currency_id, | ||||
|             'company_id' => $this->company->id, | ||||
|  | ||||
| @ -96,22 +96,17 @@ class HandleRestore extends AbstractService | ||||
|             $this->adjustment_amount += $payment->paymentables | ||||
|                                                 ->where('paymentable_type', '=', 'invoices') | ||||
|                                                 ->where('paymentable_id', $this->invoice->id) | ||||
| 
 | ||||
|                                                 ->sum('amount'); | ||||
|                                                 // ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar()));
 | ||||
| nlog($this->adjustment_amount); | ||||
|             $this->adjustment_amount += $payment->paymentables | ||||
|                                                 ->where('paymentable_type', '=', 'invoices') | ||||
|                                                 ->where('paymentable_id', $this->invoice->id) | ||||
|                                                 ->sum('amount'); | ||||
|                                                 // ->sum(DB::raw('refunded')->getValue(DB::connection()->getQueryGrammar()));
 | ||||
| nlog($this->adjustment_amount); | ||||
| 
 | ||||
|             //14/07/2023 - do not include credits in the payment amount
 | ||||
|             $this->adjustment_amount -= $payment->paymentables | ||||
|                                             ->where('paymentable_type', '=', 'App\Models\Credit') | ||||
|                                             ->sum('amount'); | ||||
|                                             // ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar()));
 | ||||
| nlog($this->adjustment_amount); | ||||
| 
 | ||||
|         } | ||||
| 
 | ||||
| @ -134,16 +129,16 @@ nlog($this->adjustment_amount); | ||||
|             $payment_adjustment = $payment->paymentables | ||||
|                                             ->where('paymentable_type', '=', 'invoices') | ||||
|                                             ->where('paymentable_id', $this->invoice->id) | ||||
|                                             ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                             ->sum('amount'); | ||||
| 
 | ||||
|             $payment_adjustment -= $payment->paymentables | ||||
|                                             ->where('paymentable_type', '=', 'invoices') | ||||
|                                             ->where('paymentable_id', $this->invoice->id) | ||||
|                                             ->sum(DB::raw('refunded')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                             ->sum('refunded'); | ||||
| 
 | ||||
|             $payment_adjustment -= $payment->paymentables | ||||
|                         ->where('paymentable_type', '=', 'App\Models\Credit') | ||||
|                         ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                         ->sum('amount'); | ||||
|   | ||||
|             $payment->amount += $payment_adjustment; | ||||
|             $payment->applied += $payment_adjustment; | ||||
|  | ||||
| @ -88,17 +88,17 @@ class MarkInvoiceDeleted extends AbstractService | ||||
|             $payment_adjustment = $payment->paymentables | ||||
|                                             ->where('paymentable_type', '=', 'invoices') | ||||
|                                             ->where('paymentable_id', $this->invoice->id) | ||||
|                                             ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                             ->sum('amount'); | ||||
| 
 | ||||
|             $payment_adjustment -= $payment->paymentables | ||||
|                                             ->where('paymentable_type', '=', 'invoices') | ||||
|                                             ->where('paymentable_id', $this->invoice->id) | ||||
|                                             ->sum(DB::raw('refunded')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                             ->sum('refunded'); | ||||
| 
 | ||||
|             //14-07-2023 - Do not include credits in the payment adjustment.
 | ||||
|             $payment_adjustment -= $payment->paymentables | ||||
|                                             ->where('paymentable_type', '=', 'App\Models\Credit') | ||||
|                                             ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                             ->sum('amount'); | ||||
| 
 | ||||
|             $payment->amount -= $payment_adjustment; | ||||
|             $payment->applied -= $payment_adjustment; | ||||
| @ -121,12 +121,12 @@ class MarkInvoiceDeleted extends AbstractService | ||||
|             $this->adjustment_amount += $payment->paymentables | ||||
|                                                 ->where('paymentable_type', '=', 'invoices') | ||||
|                                                 ->where('paymentable_id', $this->invoice->id) | ||||
|                                                 ->sum(DB::raw('amount')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                                 ->sum('amount'); | ||||
| 
 | ||||
|             $this->adjustment_amount -= $payment->paymentables | ||||
|                                                 ->where('paymentable_type', '=', 'invoices') | ||||
|                                                 ->where('paymentable_id', $this->invoice->id) | ||||
|                                                 ->sum(DB::raw('refunded')->getValue(DB::connection()->getQueryGrammar())); | ||||
|                                                 ->sum('refunded'); | ||||
|         } | ||||
| 
 | ||||
|         $this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded'); | ||||
|  | ||||
| @ -178,17 +178,6 @@ class ProfitLoss | ||||
|             $this->credit_taxes += $map->tax_amount_credit_converted; | ||||
|         } | ||||
| 
 | ||||
|         // $invoices = $this->invoicePaymentIncome();
 | ||||
| 
 | ||||
|         // $this->income = 0;
 | ||||
|         // $this->income_taxes = 0;
 | ||||
|         // $this->income_map = $invoices;
 | ||||
| 
 | ||||
|         // foreach($invoices as $invoice){
 | ||||
|         //     $this->income += $invoice->net_converted_amount;
 | ||||
|         //     $this->income_taxes += $invoice->net_converted_taxes;
 | ||||
|         // }
 | ||||
| 
 | ||||
|         return $this; | ||||
|     } | ||||
| 
 | ||||
| @ -232,7 +221,7 @@ class ProfitLoss | ||||
|    */ | ||||
|     private function invoiceIncome() | ||||
|     { | ||||
|         return \DB::select(\DB::raw(" | ||||
|         return \DB::select(" | ||||
|             SELECT | ||||
|             sum(invoices.amount) as amount, | ||||
|             sum(invoices.total_taxes) as total_taxes, | ||||
| @ -250,7 +239,7 @@ class ProfitLoss | ||||
|             AND invoices.is_deleted = 0 | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(\DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]);
 | ||||
|         ", ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -402,28 +391,6 @@ class ProfitLoss | ||||
|         return  $csv->toString(); | ||||
|     } | ||||
| 
 | ||||
|     private function invoicePaymentIncome() | ||||
|     { | ||||
|         return \DB::select(\DB::raw(" | ||||
|             SELECT | ||||
|             sum(invoices.amount - invoices.balance) as amount, | ||||
|             sum(invoices.total_taxes) * ((sum(invoices.amount - invoices.balance)/invoices.amount)) as total_taxes, | ||||
|             (sum(invoices.amount - invoices.balance) / IFNULL(invoices.exchange_rate, 1)) AS net_converted_amount, | ||||
|             (sum(invoices.total_taxes) * ((sum(invoices.amount - invoices.balance)/invoices.amount)) / IFNULL(invoices.exchange_rate, 1)) AS net_converted_taxes, | ||||
|             IFNULL(JSON_EXTRACT( settings, '$.currency_id' ), :company_currency) AS currency_id | ||||
|             FROM clients | ||||
|             JOIN invoices | ||||
|             on invoices.client_id = clients.id | ||||
|             WHERE invoices.status_id IN (3,4) | ||||
|             AND invoices.company_id = :company_id | ||||
|             AND invoices.amount > 0 | ||||
|             AND clients.is_deleted = 0 | ||||
|             AND invoices.is_deleted = 0 | ||||
|             AND (invoices.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ")->getValue(\DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]);
 | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|        +"payments": "12260.870000", | ||||
|        +"payments_converted": "12260.870000000000", | ||||
| @ -431,7 +398,7 @@ class ProfitLoss | ||||
|      */ | ||||
|     private function paymentIncome() | ||||
|     { | ||||
|         return \DB::select(\DB::raw(' | ||||
|         return \DB::select(' | ||||
|              SELECT  | ||||
|              SUM(coalesce(payments.amount - payments.refunded,0)) as payments, | ||||
|              SUM(coalesce(payments.amount - payments.refunded,0)) * IFNULL(payments.exchange_rate ,1) as payments_converted, | ||||
| @ -447,7 +414,7 @@ class ProfitLoss | ||||
|              AND (payments.date BETWEEN :start_date AND :end_date) | ||||
|              GROUP BY currency_id | ||||
|              ORDER BY currency_id; | ||||
|         ')->getValue(\DB::connection()->getQueryGrammar()), ['company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]); | ||||
|         ', ['company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]); | ||||
|     } | ||||
| 
 | ||||
|     private function expenseData() | ||||
| @ -545,7 +512,7 @@ class ProfitLoss | ||||
| 
 | ||||
|     private function expenseCalcWithTax() | ||||
|     { | ||||
|         return \DB::select(\DB::raw(' | ||||
|         return \DB::select(' | ||||
|             SELECT sum(expenses.amount) as amount, | ||||
|             IFNULL(expenses.currency_id, :company_currency) as currency_id | ||||
|             FROM expenses | ||||
| @ -553,7 +520,7 @@ class ProfitLoss | ||||
|             AND expenses.company_id = :company_id | ||||
|             AND (expenses.date BETWEEN :start_date AND :end_date) | ||||
|             GROUP BY currency_id | ||||
|         ')->getValue(\DB::connection()->getQueryGrammar()), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]); | ||||
|         ', ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $this->start_date, 'end_date' => $this->end_date]); | ||||
|     } | ||||
| 
 | ||||
|     private function setBillingReportType() | ||||
|  | ||||
| @ -43,7 +43,7 @@ class Ninja | ||||
| 
 | ||||
|     public static function getDebugInfo() | ||||
|     { | ||||
|         $mysql_version = DB::select(DB::raw('select version() as version')->getValue(DB::connection()->getQueryGrammar()))[0]->version; | ||||
|         $mysql_version = DB::select('select version() as version')[0]->version; | ||||
| 
 | ||||
|         $version = request()->input('version', 'No Version Supplied.'); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user