mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-31 22:14:36 -04:00
Balances with company ledger
This commit is contained in:
parent
37ec6afdad
commit
78ae24df46
@ -46,6 +46,8 @@ class UpdateCompanyLedgerWithInvoice
|
|||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
\Log::error('in update company ledger with invoice');
|
||||||
|
|
||||||
$balance = 0;
|
$balance = 0;
|
||||||
|
|
||||||
$ledger = CompanyLedger::whereClientId($this->invoice->client_id)
|
$ledger = CompanyLedger::whereClientId($this->invoice->client_id)
|
||||||
@ -56,7 +58,9 @@ class UpdateCompanyLedgerWithInvoice
|
|||||||
if($ledger)
|
if($ledger)
|
||||||
$balance = $ledger->balance;
|
$balance = $ledger->balance;
|
||||||
|
|
||||||
\Log::error("adjusting balance {$balance}");
|
$adjustment = $balance + $this->adjustment;
|
||||||
|
|
||||||
|
\Log::error("adjusting balance {$balance} to {$adjustment}");
|
||||||
|
|
||||||
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
$company_ledger = CompanyLedgerFactory::create($this->invoice->company_id, $this->invoice->user_id);
|
||||||
$company_ledger->client_id = $this->invoice->client_id;
|
$company_ledger->client_id = $this->invoice->client_id;
|
||||||
|
@ -52,8 +52,6 @@ class UpdateCompanyLedgerWithPayment
|
|||||||
{
|
{
|
||||||
$balance = 0;
|
$balance = 0;
|
||||||
|
|
||||||
$this->adjustment = $this->adjustment * -1;
|
|
||||||
|
|
||||||
/* Get the last record for the client and set the current balance*/
|
/* Get the last record for the client and set the current balance*/
|
||||||
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
|
$ledger = CompanyLedger::whereClientId($this->payment->client_id)
|
||||||
->whereCompanyId($this->payment->company_id)
|
->whereCompanyId($this->payment->company_id)
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace App\Listeners\Invoice;
|
namespace App\Listeners\Invoice;
|
||||||
|
|
||||||
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
||||||
|
use App\Jobs\Company\UpdateCompanyLedgerWithPayment;
|
||||||
use App\Models\SystemLog;
|
use App\Models\SystemLog;
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
@ -43,15 +44,17 @@ class UpdateInvoicePayment implements ShouldQueue
|
|||||||
/* Simplest scenario*/
|
/* Simplest scenario*/
|
||||||
if($invoices_total == $payment->amount)
|
if($invoices_total == $payment->amount)
|
||||||
{
|
{
|
||||||
|
\Log::error("invoice totals match payment amount");
|
||||||
$invoices->each(function ($invoice){
|
$invoices->each(function ($invoice) use($payment){
|
||||||
|
//$invoice->updateBalance($invoice->balance*-1);
|
||||||
|
//UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($invoice->balance*-1));
|
||||||
|
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($invoice->balance*-1));
|
||||||
$invoice->updateBalance($invoice->balance*-1);
|
$invoice->updateBalance($invoice->balance*-1);
|
||||||
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($invoice->balance*-1));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
\Log::error("invoice totals don't match, search for partials");
|
||||||
$total = 0;
|
$total = 0;
|
||||||
|
|
||||||
foreach($invoice as $invoice)
|
foreach($invoice as $invoice)
|
||||||
@ -62,6 +65,7 @@ class UpdateInvoicePayment implements ShouldQueue
|
|||||||
else
|
else
|
||||||
$total += $invoice->balance;
|
$total += $invoice->balance;
|
||||||
|
|
||||||
|
Log::error("total = {$total}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* test if there is a batch of partial invoices that have been paid */
|
/* test if there is a batch of partial invoices that have been paid */
|
||||||
@ -72,7 +76,7 @@ class UpdateInvoicePayment implements ShouldQueue
|
|||||||
//paid
|
//paid
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
\Log::error("no matches, fail");
|
||||||
$data = [
|
$data = [
|
||||||
'payment' => $payment,
|
'payment' => $payment,
|
||||||
'invoices' => $invoices,
|
'invoices' => $invoices,
|
||||||
|
@ -307,6 +307,7 @@ class Invoice extends BaseModel
|
|||||||
*/
|
*/
|
||||||
public function updateBalance($balance_adjustment)
|
public function updateBalance($balance_adjustment)
|
||||||
{
|
{
|
||||||
|
\Log::error('updating invoice balance');
|
||||||
|
|
||||||
if ($this->is_deleted)
|
if ($this->is_deleted)
|
||||||
return;
|
return;
|
||||||
@ -319,7 +320,6 @@ class Invoice extends BaseModel
|
|||||||
$this->status_id = self::STATUS_PAID;
|
$this->status_id = self::STATUS_PAID;
|
||||||
|
|
||||||
$this->save();
|
$this->save();
|
||||||
|
\Log::error('finished updatingoice balance');
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,12 +11,14 @@
|
|||||||
|
|
||||||
namespace App\PaymentDrivers;
|
namespace App\PaymentDrivers;
|
||||||
|
|
||||||
|
use App\Events\Payment\PaymentWasCreated;
|
||||||
use App\Factory\PaymentFactory;
|
use App\Factory\PaymentFactory;
|
||||||
use App\Models\ClientGatewayToken;
|
use App\Models\ClientGatewayToken;
|
||||||
use App\Models\GatewayType;
|
use App\Models\GatewayType;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use App\Models\PaymentType;
|
use App\Models\PaymentType;
|
||||||
|
use App\Models\SystemLog;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
@ -345,19 +347,20 @@ class StripePaymentDriver extends BasePaymentDriver
|
|||||||
if(!$payment_type)
|
if(!$payment_type)
|
||||||
$payment_type = PaymentType::CREDIT_CARD_OTHER;
|
$payment_type = PaymentType::CREDIT_CARD_OTHER;
|
||||||
|
|
||||||
$payment = PaymentFactory::create($this->client->company->id, $this->client->user->id);
|
|
||||||
$payment->client_id = $this->client->id;
|
|
||||||
$payment->client_contact_id = $client_contact->id;
|
|
||||||
$payment->company_gateway_id = $this->company_gateway->id;
|
|
||||||
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
||||||
$payment->amount = $this->convertFromStripeAmount($server_response->amount, $this->client->currency->precision);
|
|
||||||
$payment->payment_date = Carbon::now();
|
|
||||||
$payment->payment_type_id = $payment_type;
|
|
||||||
$payment->transaction_reference = $payment_method;
|
|
||||||
$payment->save();
|
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'payment_method' => $payment_method,
|
||||||
|
'payment_type' => $payment_type,
|
||||||
|
'amount' => $server_response->amount,
|
||||||
|
];
|
||||||
|
|
||||||
|
/* Create payment*/
|
||||||
|
$payment = $this->createPayment($data);
|
||||||
|
|
||||||
|
/* Link invoices to payment*/
|
||||||
$this->attachInvoices($payment, $hashed_ids);
|
$this->attachInvoices($payment, $hashed_ids);
|
||||||
|
|
||||||
|
event(new PaymentWasCreated($payment));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move this into an event
|
* Move this into an event
|
||||||
@ -371,9 +374,50 @@ class StripePaymentDriver extends BasePaymentDriver
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
return redirect()->route('client.payments.show', ['id' => $this->encodePrimaryKey($payment->id)]);
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/*Fail and log*/
|
||||||
|
|
||||||
|
$log = [
|
||||||
|
'server_response' => $server_response,
|
||||||
|
'invoices' => $invoices,
|
||||||
|
];
|
||||||
|
|
||||||
|
$sl = [
|
||||||
|
'company_id' => $this->client->company->id,
|
||||||
|
'client_id' => $this->client->id,
|
||||||
|
'user_id' => $this->client->user_id,
|
||||||
|
'log' => $log,
|
||||||
|
'category_id' => SystemLog::GATEWAY_RESPONSE,
|
||||||
|
'event_id' => SystemLog::GATEWAY_FAILURE,
|
||||||
|
];
|
||||||
|
|
||||||
|
SystemLog::create($sl);
|
||||||
|
|
||||||
|
throw new Exception("Failed to process payment", 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function createPayment($data)
|
||||||
|
{
|
||||||
|
|
||||||
|
$payment = parent::createPayment($data);
|
||||||
|
|
||||||
|
$client_contact = $this->getContact();
|
||||||
|
$client_contact_id = $client_contact ? $client_contact->id : null;
|
||||||
|
|
||||||
|
$payment->amount = $this->convertFromStripeAmount($data['amount'], $this->client->currency->precision);
|
||||||
|
$payment->payment_type_id = $data['payment_type'];
|
||||||
|
$payment->transaction_reference = $data['payment_method'];
|
||||||
|
$payment->client_contact_id = $client_contact_id;
|
||||||
|
$payment->save();
|
||||||
|
|
||||||
|
return $payment;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ use App\DataMapper\DefaultSettings;
|
|||||||
use App\Events\Invoice\InvoiceWasMarkedSent;
|
use App\Events\Invoice\InvoiceWasMarkedSent;
|
||||||
use App\Events\Invoice\InvoiceWasUpdated;
|
use App\Events\Invoice\InvoiceWasUpdated;
|
||||||
use App\Helpers\Invoice\InvoiceCalc;
|
use App\Helpers\Invoice\InvoiceCalc;
|
||||||
|
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
|
||||||
use App\Listeners\Invoice\CreateInvoiceInvitation;
|
use App\Listeners\Invoice\CreateInvoiceInvitation;
|
||||||
use App\Models\Account;
|
use App\Models\Account;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
@ -122,6 +123,8 @@ class RandomDataSeeder extends Seeder
|
|||||||
$invoice->save();
|
$invoice->save();
|
||||||
|
|
||||||
event(new CreateInvoiceInvitation($invoice));
|
event(new CreateInvoiceInvitation($invoice));
|
||||||
|
|
||||||
|
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);
|
||||||
|
|
||||||
$invoice_repo->markSent($invoice);
|
$invoice_repo->markSent($invoice);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user