diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index b7b00830b6..e88d898ad5 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -321,10 +321,17 @@ class CheckData extends Command foreach ($client->invoices()->where('is_deleted', false)->where('status_id', '>', 1)->get() as $invoice) { - $total_amount = $invoice->payments()->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->get()->sum('pivot.amount'); - $total_refund = $invoice->payments()->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->get()->sum('pivot.refunded'); + // $total_amount = $invoice->payments()->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->get()->sum('pivot.amount'); + // $total_refund = $invoice->payments()->where('is_deleted', false)->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED, Payment::STATUS_REFUNDED])->get()->sum('pivot.refunded'); + + // $total_invoice_payments += ($total_amount - $total_refund); + + $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(); - $total_invoice_payments += ($total_amount - $total_refund); } //commented IN 27/06/2021 - sums ALL client payments AND the unapplied amounts to match the client paid to date @@ -339,7 +346,12 @@ class CheckData extends Command // 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')); + // $credit_total_applied += $payment->paymentables()->where('paymentable_type', App\Models\Credit::class)->get()->sum(DB::raw('amount')); + $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) { @@ -372,17 +384,25 @@ class CheckData extends Command Client::cursor()->where('is_deleted', 0)->each(function ($client) { $client->invoices->where('is_deleted', false)->whereIn('status_id', '!=', Invoice::STATUS_DRAFT)->each(function ($invoice) use ($client) { - $total_amount = $invoice->payments()->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->get()->sum('pivot.amount'); - $total_refund = $invoice->payments()->get()->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); + // $total_amount = $invoice->payments()->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->get()->sum('pivot.amount'); + // $total_refund = $invoice->payments()->get()->whereIn('status_id', [Payment::STATUS_COMPLETED, Payment:: STATUS_PENDING, Payment::STATUS_PARTIALLY_REFUNDED])->sum('pivot.refunded'); + + $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_paid = $total_amount - $total_refund; + $total_credit = $invoice->credits()->get()->sum('amount'); - $total_paid = $total_amount - $total_refund; $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 Amount = {$total_amount} != Calculated Total = {$calculated_paid_amount} - Total Refund = {$total_refund} Total credit = {$total_credit}"); + $this->logMessage($client->present()->name.' - '.$client->id." - Total Paid = {$total_paid} != Calculated Total = {$calculated_paid_amount}"); $this->isValid = false; } diff --git a/app/Jobs/Company/CompanyImport.php b/app/Jobs/Company/CompanyImport.php index 3e40f5f2f8..216ab6a122 100644 --- a/app/Jobs/Company/CompanyImport.php +++ b/app/Jobs/Company/CompanyImport.php @@ -220,6 +220,10 @@ class CompanyImport implements ShouldQueue private function unzipFile() { + + if(mime_content_type(Storage::path($this->file_location)) == 'text/plain') + return Storage::path($this->file_location); + $path = TempFile::filePath(Storage::get($this->file_location), basename($this->file_location)); $zip = new ZipArchive(); diff --git a/app/Jobs/Invoice/ZipInvoices.php b/app/Jobs/Invoice/ZipInvoices.php index 33cb460b1f..0dff22aa35 100644 --- a/app/Jobs/Invoice/ZipInvoices.php +++ b/app/Jobs/Invoice/ZipInvoices.php @@ -63,51 +63,12 @@ class ZipInvoices implements ShouldQueue /** * Execute the job. * - * * @return void * @throws \ZipStream\Exception\FileNotFoundException * @throws \ZipStream\Exception\FileNotReadableException * @throws \ZipStream\Exception\OverflowException */ - // public function handle() - // { - // $tempStream = fopen('php://memory', 'w+'); - - // $options = new Archive(); - // $options->setOutputStream($tempStream); - - // // create a new zipstream object - // $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip'; - - // $invoice = $this->invoices->first(); - // $invitation = $invoice->invitations->first(); - - // $path = $invoice->client->invoice_filepath($invitation); - - // $zip = new ZipStream($file_name, $options); - - // foreach ($this->invoices as $invoice) { - // //$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path())); - // $zip->addFileFromPath(basename($invoice->pdf_file_path($invitation)), $invoice->pdf_file_path()); - // } - - // $zip->finish(); - - // Storage::disk('public')->put($path.$file_name, $tempStream); - - // fclose($tempStream); - - // $nmo = new NinjaMailerObject; - // $nmo->mailable = new DownloadInvoices(Storage::disk('public')->url($path.$file_name), $this->company); - // $nmo->to_user = $this->user; - // $nmo->settings = $this->settings; - // $nmo->company = $this->company; - - // NinjaMailerJob::dispatch($nmo); - - // UnlinkFile::dispatch('public', $path.$file_name)->delay(now()->addHours(1)); - // } - + public function handle() { # create new zip object @@ -145,8 +106,6 @@ class ZipInvoices implements ShouldQueue NinjaMailerJob::dispatch($nmo); - nlog("sending email"); - UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); } } diff --git a/app/PaymentDrivers/Stripe/ACH.php b/app/PaymentDrivers/Stripe/ACH.php index 302e91284b..5b39fcfd46 100644 --- a/app/PaymentDrivers/Stripe/ACH.php +++ b/app/PaymentDrivers/Stripe/ACH.php @@ -128,7 +128,7 @@ class ACH $data['currency'] = $this->stripe->client->getCurrencyCode(); $data['payment_method_id'] = GatewayType::BANK_TRANSFER; $data['customer'] = $this->stripe->findOrCreateCustomer(); - $data['amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision); + $data['amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()); return render('gateways.stripe.ach.pay', $data); } @@ -151,7 +151,7 @@ class ACH $state = [ 'payment_method' => $request->payment_method_id, 'gateway_type_id' => $request->company_gateway_id, - 'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'currency' => $request->currency, 'customer' => $request->customer, ]; @@ -196,7 +196,7 @@ class ACH $data = [ 'payment_method' => $state['source'], 'payment_type' => PaymentType::ACH, - 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->amount, $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'transaction_reference' => $state['charge']->id, 'gateway_type_id' => GatewayType::BANK_TRANSFER, ]; diff --git a/app/PaymentDrivers/Stripe/Alipay.php b/app/PaymentDrivers/Stripe/Alipay.php index c79496eab2..20f2c0e1eb 100644 --- a/app/PaymentDrivers/Stripe/Alipay.php +++ b/app/PaymentDrivers/Stripe/Alipay.php @@ -37,7 +37,7 @@ class Alipay $data['gateway'] = $this->stripe; $data['return_url'] = $this->buildReturnUrl(); $data['currency'] = $this->stripe->client->getCurrencyCode(); - $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision); + $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()); $data['invoices'] = $this->stripe->payment_hash->invoices(); $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]); @@ -74,7 +74,7 @@ class Alipay $data = [ 'payment_method' => $this->stripe->payment_hash->data->source, 'payment_type' => PaymentType::ALIPAY, - 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'transaction_reference' => $source, 'gateway_type_id' => GatewayType::ALIPAY, @@ -104,7 +104,7 @@ class Alipay $this->stripe->client, $server_response, $this->stripe->client->company, - $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision) + $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()) ); $message = [ diff --git a/app/PaymentDrivers/Stripe/Charge.php b/app/PaymentDrivers/Stripe/Charge.php index 89baece962..4e68065446 100644 --- a/app/PaymentDrivers/Stripe/Charge.php +++ b/app/PaymentDrivers/Stripe/Charge.php @@ -68,7 +68,7 @@ class Charge try { $data = [ - 'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'currency' => $this->stripe->client->getCurrencyCode(), 'payment_method' => $cgt->token, 'customer' => $cgt->gateway_customer_reference, diff --git a/app/PaymentDrivers/Stripe/CreditCard.php b/app/PaymentDrivers/Stripe/CreditCard.php index 89fde9e312..72981fea30 100644 --- a/app/PaymentDrivers/Stripe/CreditCard.php +++ b/app/PaymentDrivers/Stripe/CreditCard.php @@ -60,7 +60,7 @@ class CreditCard public function paymentView(array $data) { $payment_intent_data = [ - 'amount' => $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'currency' => $this->stripe->client->getCurrencyCode(), 'customer' => $this->stripe->findOrCreateCustomer(), 'description' => ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number'), // TODO: More meaningful description. @@ -115,7 +115,7 @@ class CreditCard $data = [ 'payment_method' => $this->stripe->payment_hash->data->server_response->payment_method, 'payment_type' => PaymentType::parseCardType(strtolower($stripe_method->card->brand)), - 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'transaction_reference' => optional($this->stripe->payment_hash->data->payment_intent->charges->data[0])->id, 'gateway_type_id' => GatewayType::CREDIT_CARD, ]; diff --git a/app/PaymentDrivers/Stripe/SOFORT.php b/app/PaymentDrivers/Stripe/SOFORT.php index 4e55d2c428..16e046c50d 100644 --- a/app/PaymentDrivers/Stripe/SOFORT.php +++ b/app/PaymentDrivers/Stripe/SOFORT.php @@ -40,7 +40,7 @@ class SOFORT { $data['gateway'] = $this->stripe; $data['return_url'] = $this->buildReturnUrl(); - $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision); + $data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()); $data['client'] = $this->stripe->client; $data['country'] = $this->stripe->client->country->iso_3166_2; @@ -80,7 +80,7 @@ class SOFORT $data = [ 'payment_method' => $this->stripe->payment_hash->data->source, 'payment_type' => PaymentType::SOFORT, - 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision), + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), 'transaction_reference' => $source, 'gateway_type_id' => GatewayType::SOFORT, ]; @@ -107,7 +107,7 @@ class SOFORT $this->stripe->client, $server_response, $this->stripe->client->company, - $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision) + $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()) ); $message = [ diff --git a/app/PaymentDrivers/Stripe/Utilities.php b/app/PaymentDrivers/Stripe/Utilities.php index df915b83fe..586871d08a 100644 --- a/app/PaymentDrivers/Stripe/Utilities.php +++ b/app/PaymentDrivers/Stripe/Utilities.php @@ -14,13 +14,24 @@ namespace App\PaymentDrivers\Stripe; trait Utilities { - public function convertFromStripeAmount($amount, $precision) + /*Helpers for currency conversions, NOTE* for some currencies we need to change behaviour */ + public function convertFromStripeAmount($amount, $precision, $currency) { + + if($currency->code == "JPY") + return $amount; + return $amount / pow(10, $precision); + } - public function convertToStripeAmount($amount, $precision) + public function convertToStripeAmount($amount, $precision, $currency) { - return (int)($amount * pow(10, $precision)); + + if($currency->code == "JPY") + return $amount; + + return round(($amount * pow(10, $precision)),0); + } } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 700a4c6a97..c096729a13 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -341,7 +341,7 @@ class StripePaymentDriver extends BaseDriver try { $response = $this->stripe ->refunds - ->create(['charge' => $payment->transaction_reference, 'amount' => $this->convertToStripeAmount($amount, $this->client->currency()->precision)], $meta); + ->create(['charge' => $payment->transaction_reference, 'amount' => $this->convertToStripeAmount($amount, $this->client->currency()->precision, $this->client->currency())], $meta); if ($response->status == $response::STATUS_SUCCEEDED) { SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(),], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->client, $this->client->company);