diff --git a/app/Http/Requests/Report/GenericReportRequest.php b/app/Http/Requests/Report/GenericReportRequest.php index 95d5a04ac0d8..c197b21d7670 100644 --- a/app/Http/Requests/Report/GenericReportRequest.php +++ b/app/Http/Requests/Report/GenericReportRequest.php @@ -31,9 +31,9 @@ class GenericReportRequest extends Request 'start_date' => 'string|date', 'end_date' => 'string|date', 'date_key' => 'string', - 'date_range' => 'string', + 'date_range' => 'required|string', 'report_keys' => 'present|array', - 'send_email' => 'bool', + 'send_email' => 'required|bool', ]; } } diff --git a/app/Services/Chart/ChartQueries.php b/app/Services/Chart/ChartQueries.php index 6224f98d3c53..b05f3d3f1042 100644 --- a/app/Services/Chart/ChartQueries.php +++ b/app/Services/Chart/ChartQueries.php @@ -22,11 +22,119 @@ use Illuminate\Support\Facades\DB; trait ChartQueries { - // $currencies = Payment::withTrashed() - // ->where('company_id', $this->company->id) - // ->where('is_deleted', 0) - // ->distinct() - // ->get(['currency_id']); + /** + * Expenses + */ + public function getExpenseQuery($start_date, $end_date) + { + + return DB::select( DB::raw(" + SELECT sum(expenses.amount) as amount, + IFNULL(expenses.currency_id, :company_currency) as currency_id + FROM expenses + WHERE expenses.is_deleted = 0 + AND expenses.company_id = :company_id + AND (expenses.date BETWEEN :start_date AND :end_date) + GROUP BY currency_id + "), ['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(" + SELECT + sum(expenses.amount) as total, + expenses.date, + IFNULL(expenses.currency_id, :company_currency) AS currency_id + FROM expenses + WHERE (expenses.date BETWEEN :start_date AND :end_date) + AND expenses.company_id = :company_id + AND expenses.is_deleted = 0 + GROUP BY expenses.date + HAVING currency_id = :currency_id + "), [ + 'company_currency' => $this->company->settings->currency_id, + 'currency_id' => $currency_id, + 'company_id' => $this->company->id, + 'start_date' => $start_date, + 'end_date' => $end_date + ]); + + } + + /** + * Payments + */ + public function getPaymentQuery($start_date, $end_date) + { + + return DB::select( DB::raw(" + SELECT sum(payments.amount) as amount, + IFNULL(payments.currency_id, :company_currency) as currency_id + FROM payments + WHERE payments.is_deleted = 0 + AND payments.company_id = :company_id + AND (payments.date BETWEEN :start_date AND :end_date) + GROUP BY currency_id + "), [ + 'company_currency' => $this->company->settings->currency_id, + 'company_id' => $this->company->id, + 'start_date' => $start_date, + 'end_date' => $end_date + ]); + + } + + public function getPaymentChartQuery($start_date, $end_date, $currency_id) + { + + return DB::select( DB::raw(" + SELECT + sum(payments.amount - payments.refunded) as total, + payments.date, + IFNULL(payments.currency_id, :company_currency) AS currency_id + FROM payments + WHERE payments.status_id IN (4,5,6) + AND (payments.date BETWEEN :start_date AND :end_date) + AND payments.company_id = :company_id + AND payments.is_deleted = 0 + GROUP BY payments.date + HAVING currency_id = :currency_id + "), [ + 'company_currency' => $this->company->settings->currency_id, + 'currency_id' => $currency_id, + 'company_id' => $this->company->id, + 'start_date' => $start_date, + 'end_date' => $end_date + ]); + + } + + /** + * Invoices + */ + public function getOutstandingQuery($start_date, $end_date) + { + + return DB::select( DB::raw(" + SELECT + sum(invoices.balance) as amount, + 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 (2,3) + AND invoices.company_id = :company_id + AND invoices.balance > 0 + AND clients.is_deleted = 0 + AND invoices.is_deleted = 0 + AND (invoices.date BETWEEN :start_date AND :end_date) + GROUP BY currency_id + "), ['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) { @@ -49,55 +157,6 @@ trait ChartQueries } - public function getOutstandingQuery($start_date, $end_date) - { - - return DB::select( DB::raw(" - SELECT - sum(invoices.balance) as balance, - 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 (2,3) - AND invoices.company_id = :company_id - AND invoices.balance > 0 - AND clients.is_deleted = 0 - AND invoices.is_deleted = 0 - AND (invoices.due_date BETWEEN :start_date AND :end_date) - GROUP BY currency_id - "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); - - } - - public function getExpenseQuery($start_date, $end_date) - { - - return DB::select( DB::raw(" - SELECT sum(expenses.amount) as amount, - IFNULL(expenses.currency_id, :company_currency) as currency_id - FROM expenses - WHERE expenses.is_deleted = 0 - AND expenses.company_id = :company_id - AND (expenses.date BETWEEN :start_date AND :end_date) - GROUP BY currency_id - "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); - - } - - public function getPaymentQuery($start_date, $end_date) - { - return DB::select( DB::raw(" - SELECT sum(expenses.amount) as amount, - IFNULL(expenses.currency_id, :company_currency) as currency_id - FROM expenses - WHERE expenses.is_deleted = 0 - AND expenses.company_id = :company_id - AND (expenses.date BETWEEN :start_date AND :end_date) - GROUP BY currency_id - "), ['company_currency' => $this->company->settings->currency_id, 'company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); - } - public function getInvoiceChartQuery($start_date, $end_date, $currency_id) { @@ -123,118 +182,7 @@ trait ChartQueries 'start_date' => $start_date, 'end_date' => $end_date ]); + } - public function getPaymentChartQuery($start_date, $end_date, $currency_id) - { - - return DB::select( DB::raw(" - SELECT - sum(payments.amount - payments.refunded) as total, - payments.date, - IFNULL(payments.currency_id, :company_currency) AS currency_id - FROM payments - WHERE payments.status_id IN (4,5,6) - AND (payments.date BETWEEN :start_date AND :end_date) - AND payments.company_id = :company_id - AND payments.is_deleted = 0 - GROUP BY payments.date - HAVING currency_id = :currency_id - "), [ - 'company_currency' => $this->company->settings->currency_id, - 'currency_id' => $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(" - SELECT - sum(expenses.amount) as total, - expenses.date, - IFNULL(expenses.currency_id, :company_currency) AS currency_id - FROM expenses - WHERE (expenses.date BETWEEN :start_date AND :end_date) - AND expenses.company_id = :company_id - AND expenses.is_deleted = 0 - GROUP BY expenses.date - HAVING currency_id = :currency_id - "), [ - 'company_currency' => $this->company->settings->currency_id, - 'currency_id' => $currency_id, - 'company_id' => $this->company->id, - 'start_date' => $start_date, - 'end_date' => $end_date - ]); - } - - } - - - - -/* - public function payments($accountId, $userId, $viewAll) - { - $payments = DB::table('payments') - ->leftJoin('clients', 'clients.id', '=', 'payments.client_id') - ->leftJoin('contacts', 'contacts.client_id', '=', 'clients.id') - ->leftJoin('invoices', 'invoices.id', '=', 'payments.invoice_id') - ->where('payments.account_id', '=', $accountId) - ->where('payments.is_deleted', '=', false) - ->where('invoices.is_deleted', '=', false) - ->where('clients.is_deleted', '=', false) - ->where('contacts.deleted_at', '=', null) - ->where('contacts.is_primary', '=', true) - ->whereNotIn('payments.payment_status_id', [PAYMENT_STATUS_VOIDED, PAYMENT_STATUS_FAILED]); - - if (! $viewAll) { - $payments = $payments->where('payments.user_id', '=', $userId); - } - - return $payments->select(['payments.payment_date', DB::raw('(payments.amount - payments.refunded) as amount'), 'invoices.public_id', 'invoices.invoice_number', 'clients.name as client_name', 'contacts.email', 'contacts.first_name', 'contacts.last_name', 'clients.currency_id', 'clients.public_id as client_public_id', 'clients.user_id as client_user_id']) - ->orderBy('payments.payment_date', 'desc') - ->take(100) - ->get(); - } - - public function oustanding($start_date, $end_date) - { - - $company_currency = (int) $this->company->settings->currency_id; - - $results = \DB::select( \DB::raw(" - SELECT - sum(invoices.balance) as balance, - JSON_EXTRACT( settings, '$.currency_id' ) AS currency_id - FROM clients - JOIN invoices - on invoices.client_id = clients.id - WHERE invoices.status_id IN (2,3) - AND invoices.company_id = :company_id - AND invoices.balance > 0 - AND clients.is_deleted = 0 - AND invoices.is_deleted = 0 - AND (invoices.due_date BETWEEN :start_date AND :end_date) - GROUP BY currency_id - "), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] ); - - //return $results; - - //the output here will most likely contain a currency_id = null value - we need to merge this value with the company currency - - } - - - - - - - - - */ \ No newline at end of file