From 5471eb2571e67bdc81fe96ee12cd3ee3c7a1d272 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 21 Jan 2022 14:35:16 +1100 Subject: [PATCH] Fixes for Stripe FPX --- app/PaymentDrivers/Stripe/FPX.php | 2 +- app/PaymentDrivers/StripePaymentDriver.php | 1 + app/Services/Chart/ChartQueries.php | 113 +++++++++++++++++++++ app/Services/Chart/ChartService.php | 41 +++----- 4 files changed, 129 insertions(+), 28 deletions(-) diff --git a/app/PaymentDrivers/Stripe/FPX.php b/app/PaymentDrivers/Stripe/FPX.php index c1817cadd4b1..c06c6ca58ea3 100644 --- a/app/PaymentDrivers/Stripe/FPX.php +++ b/app/PaymentDrivers/Stripe/FPX.php @@ -47,7 +47,7 @@ class FPX $intent = \Stripe\PaymentIntent::create([ 'amount' => $data['stripe_amount'], - 'currency' => 'eur', + 'currency' => $this->stripe->client->getCurrencyCode(), 'payment_method_types' => ['fpx'], 'customer' => $this->stripe->findOrCreateCustomer(), 'description' => $this->stripe->decodeUnicodeString(ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number')), diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index a1a055821d48..39f896be0583 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -93,6 +93,7 @@ class StripePaymentDriver extends BaseDriver GatewayType::BANCONTACT => Bancontact::class, GatewayType::BECS => BECS::class, GatewayType::ACSS => ACSS::class, + GatewayType::FPX => FPX::class, ]; const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE; diff --git a/app/Services/Chart/ChartQueries.php b/app/Services/Chart/ChartQueries.php index bb438a25ab04..b3d3f53de10b 100644 --- a/app/Services/Chart/ChartQueries.php +++ b/app/Services/Chart/ChartQueries.php @@ -11,6 +11,9 @@ namespace App\Services\Chart; +use App\Models\Expense; +use App\Models\Invoice; +use App\Models\Payment; use Illuminate\Support\Facades\DB; /** @@ -82,5 +85,115 @@ trait ChartQueries } + 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 getChartData($start_date, $end_date, $group_by = 'DAYOFYEAR', $entity_type = 'invoice', $currency_id) + { + + if(!$currency_id) + $currency_id = $this->company->settings->currency_id; + + return DB::select( DB::raw(" + SELECT + sum(invoices.balance) as balance, + sum(invoices.amount) 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,4) + AND invoices.company_id = :company_id + AND clients.is_deleted = 0 + AND invoices.is_deleted = 0 + AND (invoices.date BETWEEN :start_date AND :end_date) + HAVING currency_id = :currency_id + GROUP BY :group_by + "), [ + 'currency_id' => $currency_id, + 'group_by' => $group_by, + 'company_currency' => $this->company->settings->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 diff --git a/app/Services/Chart/ChartService.php b/app/Services/Chart/ChartService.php index 9a65f7ad17ba..db932763986f 100644 --- a/app/Services/Chart/ChartService.php +++ b/app/Services/Chart/ChartService.php @@ -70,6 +70,16 @@ class ChartService } +/* Payments */ + public function payments($start_date, $end_date) + { + $payments = $this->getPaymentQuery(); + } + +/* Payments */ + +/* Totals */ + public function totals($start_date, $end_date) :array { $data = []; @@ -82,33 +92,6 @@ class ChartService return $data; } - 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 - - } - private function getRevenue($start_date, $end_date) :array { $revenue = $this->getRevenueQuery($start_date, $end_date); @@ -133,6 +116,10 @@ class ChartService return $expenses; } +/* Totals */ + +/* Helpers */ + private function addCountryCodes($data_set) :array {