diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index b8d425ce8413..5e7a40ee3d6f 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -34,6 +34,7 @@ use App\Models\Project; use App\Models\Quote; use App\Models\RecurringInvoice; use App\Models\Task; +use App\Models\TaxRate; use App\Models\User; use App\Models\Vendor; use App\Models\VendorContact; @@ -154,6 +155,29 @@ class CreateSingleAccount extends Command 'company_id' => $company->id, ]); + + TaxRate::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'name' => 'GST', + 'rate' => 10 + ]); + + TaxRate::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'name' => 'VAT', + 'rate' => 17.5 + ]); + + TaxRate::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'name' => 'CA Sales Tax', + 'rate' => 5 + ]); + + $this->info('Creating '.$this->count.' clients'); for ($x = 0; $x < $this->count; $x++) { diff --git a/app/Jobs/Cron/AutoBillCron.php b/app/Jobs/Cron/AutoBillCron.php new file mode 100644 index 000000000000..6f9bf09c303e --- /dev/null +++ b/app/Jobs/Cron/AutoBillCron.php @@ -0,0 +1,108 @@ +format('Y-m-d h:i:s')); + + if (! config('ninja.db.multi_db_enabled')) { + + $auto_bill_partial_invoices = Invoice::whereDate('partial_due_date', '<=', now()) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('auto_bill_enabled', true) + ->where('balance', '>', 0) + ->with('company') + ->cursor(); + + $auto_bill_partial_invoices->each(function ($invoice){ + $this->runAutoBiller($invoice); + }); + + $auto_bill_invoices = Invoice::whereDate('due_date', '<=', now()) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('auto_bill_enabled', true) + ->where('balance', '>', 0) + ->with('company') + ->cursor(); + + $auto_bill_invoices->each(function ($invoice){ + $this->runAutoBiller($invoice); + }); + + + } else { + //multiDB environment, need to + foreach (MultiDB::$dbs as $db) { + MultiDB::setDB($db); + + $auto_bill_partial_invoices = Invoice::whereDate('partial_due_date', '<=', now()) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('auto_bill_enabled', true) + ->where('balance', '>', 0) + ->with('company') + ->cursor(); + + $auto_bill_partial_invoices->each(function ($invoice){ + $this->runAutoBiller($invoice); + }); + + $auto_bill_invoices = Invoice::whereDate('due_date', '<=', now()) + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]) + ->where('auto_bill_enabled', true) + ->where('balance', '>', 0) + ->with('company') + ->cursor(); + + $auto_bill_invoices->each(function ($invoice){ + $this->runAutoBiller($invoice); + }); + + } + } + } + + private function runAutoBiller(Invoice $invoice) + { + $invoice->service()->autoBill()->save(); + } +} diff --git a/app/PaymentDrivers/Stripe/Utilities.php b/app/PaymentDrivers/Stripe/Utilities.php index a61ca2505909..9f9e34746d73 100644 --- a/app/PaymentDrivers/Stripe/Utilities.php +++ b/app/PaymentDrivers/Stripe/Utilities.php @@ -21,6 +21,6 @@ trait Utilities public function convertToStripeAmount($amount, $precision) { - return $amount * pow(10, $precision); + return (int)($amount * pow(10, $precision)); } } diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index 3be132848d3b..4b9ff0a41a02 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -40,6 +40,8 @@ class AutoBillInvoice extends AbstractService public function run() { + $is_partial = false; + /* Is the invoice payable? */ if (! $this->invoice->isPayable()) return $this->invoice; @@ -57,6 +59,8 @@ class AutoBillInvoice extends AbstractService /* Determine $amount */ if ($this->invoice->partial > 0) { + $is_partial = true; + $invoice_total = $this->invoice->amount; $amount = $this->invoice->partial; } elseif ($this->invoice->balance > 0) { $amount = $this->invoice->balance; @@ -77,7 +81,10 @@ class AutoBillInvoice extends AbstractService //$fee = $gateway_token->gateway->calcGatewayFee($amount, $gateway_token->gateway_type_id, $this->invoice->uses_inclusive_taxes); $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $gateway_token->gateway_type_id, $amount)->save(); - $fee = $this->invoice->amount - $amount; + if($is_partial) + $fee = $this->invoice->amount - $invoice_total; + else + $fee = $this->invoice->amount - $amount; /* Build payment hash */ $payment_hash = PaymentHash::create([ diff --git a/database/factories/TaxRateFactory.php b/database/factories/TaxRateFactory.php new file mode 100644 index 000000000000..a4230bce1246 --- /dev/null +++ b/database/factories/TaxRateFactory.php @@ -0,0 +1,38 @@ + $this->faker->word(3), + 'rate' => rand(1,20) + ]; + } +}