1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Fixes for missing company object in job classes. (#3179)

* Fixes for missing company object in events

* Cleanup client balancing code

* Fixes for client balance

* Fixes for missing company variable
This commit is contained in:
David Bomba 2019-12-29 17:28:57 +11:00 committed by GitHub
parent 06e182bf04
commit ff7b62de51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 111 additions and 65 deletions

View File

@ -316,7 +316,7 @@ class CreateTestData extends Command
event(new CreateInvoiceInvitation($invoice));
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
$this->invoice_repo->markSent($invoice);

View File

@ -11,6 +11,7 @@
namespace App\Events\Invoice;
use App\Models\Company;
use App\Models\Invoice;
use Illuminate\Queue\SerializesModels;
@ -26,13 +27,17 @@ class InvoiceWasMarkedSent
*/
public $invoice;
public $company;
/**
* Create a new event instance.
*
* @param Invoice $invoice
*/
public function __construct(Invoice $invoice)
public function __construct(Invoice $invoice, Company $company)
{
$this->invoice = $invoice;
$this->company = $company;
}
}

View File

@ -216,7 +216,7 @@ class InvoiceController extends BaseController
$invoice = StoreInvoice::dispatchNow($invoice, $request->all(), $invoice->company); //todo potentially this may return mixed ie PDF/$invoice... need to revisit when we implement UI
event(new InvoiceWasCreated($invoice));
event(new InvoiceWasCreated($invoice, $invoice->company));
return $this->itemResponse($invoice);

View File

@ -12,7 +12,9 @@
namespace App\Jobs\Client;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateClientBalance
@ -23,16 +25,19 @@ class UpdateClientBalance
protected $client;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Client $client, $amount)
public function __construct(Client $client, $amount, Company $company)
{
$this->amount = $amount;
$this->client = $client;
$this->company = $company;
}
/**
@ -42,6 +47,7 @@ class UpdateClientBalance
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$this->client->balance += $this->amount;
$this->client->save();

View File

@ -12,7 +12,9 @@
namespace App\Jobs\Client;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateClientPaidToDate
@ -23,16 +25,18 @@ class UpdateClientPaidToDate
protected $client;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Client $client, $amount)
public function __construct(Client $client, $amount, Company $company)
{
$this->amount = $amount;
$this->client = $client;
$this->company = $company;
}
/**
@ -42,6 +46,7 @@ class UpdateClientPaidToDate
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$this->client->paid_to_date += $this->amount;
$this->client->save();

View File

@ -12,6 +12,8 @@
namespace App\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use Illuminate\Foundation\Bus\Dispatchable;
@ -24,19 +26,22 @@ class UpdateCompanyLedgerWithInvoice
public $invoice;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Invoice $invoice, float $adjustment)
public function __construct(Invoice $invoice, float $adjustment, Company $company)
{
$this->invoice = $invoice;
$this->adjustment = $adjustment;
$this->company = $company;
}
/**
@ -46,7 +51,8 @@ class UpdateCompanyLedgerWithInvoice
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$balance = 0;
$ledger = CompanyLedger::whereClientId($this->invoice->client_id)

View File

@ -12,6 +12,8 @@
namespace App\Jobs\Company;
use App\Factory\CompanyLedgerFactory;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\CompanyLedger;
use App\Models\Invoice;
use App\Models\Payment;
@ -28,19 +30,21 @@ class UpdateCompanyLedgerWithPayment
public $payment;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, float $adjustment)
public function __construct(Payment $payment, float $adjustment, Company $company)
{
$this->payment = $payment;
$this->adjustment = $adjustment;
$this->company = $company;
}
/**
@ -50,6 +54,9 @@ class UpdateCompanyLedgerWithPayment
*/
public function handle()
{
MultiDB::setDB($this->company->db);
$balance = 0;
/* Get the last record for the client and set the current balance*/

View File

@ -66,9 +66,9 @@ class ApplyInvoicePayment implements ShouldQueue
MultiDB::setDB($this->company->db);
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->amount*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $this->amount*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $this->amount);
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->amount*-1), $this->company);
UpdateClientBalance::dispatchNow($this->payment->client, $this->amount*-1, $this->company);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $this->amount, $this->company);
/* Update Pivot Record amount */
$this->payment->invoices->each(function ($inv){
@ -86,16 +86,19 @@ class ApplyInvoicePayment implements ShouldQueue
//is partial and amount is exactly the partial amount
if($this->invoice->partial == $this->amount)
{
$this->invoice->clearPartial();
$this->invoice->setDueDate();
$this->invoice->setStatus(Invoice::STATUS_PARTIAL);
$this->invoice->updateBalance($this->amount*-1);
}
elseif($this->invoice->partial > 0 && $this->invoice->partial > $this->amount) //partial amount exists, but the amount is less than the partial amount
{
$this->invoice->partial -= $this->amount;
$this->invoice->updateBalance($this->amount*-1);
}
elseif($this->invoice->partial > 0 && $this->invoice->partial < $this->amount) //partial exists and the amount paid is GREATER than the partial amount
{
@ -104,6 +107,7 @@ class ApplyInvoicePayment implements ShouldQueue
$this->invoice->setDueDate();
$this->invoice->setStatus(Invoice::STATUS_PARTIAL);
$this->invoice->updateBalance($this->amount*-1);
}
}
@ -114,6 +118,7 @@ class ApplyInvoicePayment implements ShouldQueue
$this->invoice->setDueDate();
$this->invoice->setStatus(Invoice::STATUS_PAID);
$this->invoice->updateBalance($this->amount*-1);
}

View File

@ -78,9 +78,9 @@ class MarkInvoicePaid implements ShouldQueue
/* Update Invoice balance */
event(new PaymentWasCreated($payment, $payment->company));
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1));
UpdateClientBalance::dispatchNow($payment->client, $payment->amount*-1);
UpdateClientPaidToDate::dispatchNow($payment->client, $payment->amount);
UpdateCompanyLedgerWithPayment::dispatchNow($payment, ($payment->amount*-1), $this->company);
UpdateClientBalance::dispatchNow($payment->client, $payment->amount*-1, $this->company);
UpdateClientPaidToDate::dispatchNow($payment->client, $payment->amount, $this->company);
return $this->invoice;
}

View File

@ -71,11 +71,11 @@ class ReverseInvoicePayment implements ShouldQueue
});
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment->amount));
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($this->payment->amount), $this->company);
UpdateClientBalance::dispatchNow($client, $this->payment->amount);
UpdateClientBalance::dispatchNow($client, $this->payment->amount, $this->company);
UpdateClientPaidToDate::dispatchNow($client, $this->payment->amount*-1);
UpdateClientPaidToDate::dispatchNow($client, $this->payment->amount*-1, $this->company);
}
}

View File

@ -65,9 +65,9 @@ class UpdateInvoicePayment implements ShouldQueue
{
$invoices->each(function ($invoice){
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance);
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1), $this->company);
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1, $this->company);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance, $this->company);
$invoice->pivot->amount = $invoice->balance;
$invoice->pivot->save();
@ -102,9 +102,9 @@ class UpdateInvoicePayment implements ShouldQueue
if($invoice->hasPartial()) {
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->partial);
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->partial*-1), $this->company);
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->partial*-1, $this->company);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->partial, $this->company);
$invoice->pivot->amount = $invoice->partial;
$invoice->pivot->save();
@ -119,9 +119,9 @@ class UpdateInvoicePayment implements ShouldQueue
else
{
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1));
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance);
UpdateCompanyLedgerWithPayment::dispatchNow($this->payment, ($invoice->balance*-1), $this->company);
UpdateClientBalance::dispatchNow($this->payment->client, $invoice->balance*-1, $this->company);
UpdateClientPaidToDate::dispatchNow($this->payment->client, $invoice->balance, $this->company);
$invoice->pivot->amount = $invoice->balance;
$invoice->pivot->save();

View File

@ -11,6 +11,7 @@
namespace App\Jobs\Product;
use App\Libraries\MultiDB;
use App\Models\Company;
use App\Models\Payment;
use App\Models\Product;
@ -30,18 +31,21 @@ class UpdateOrCreateProduct implements ShouldQueue
private $invoice;
private $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($products, $invoice)
public function __construct($products, $invoice, $company)
{
$this->products = $products;
$this->invoice = $invoice;
$this->company = $company;
}
/**
@ -52,6 +56,7 @@ class UpdateOrCreateProduct implements ShouldQueue
*/
public function handle()
{
MultiDB::setDB($this->company->db);
foreach($this->products as $item)
{
@ -59,18 +64,18 @@ class UpdateOrCreateProduct implements ShouldQueue
$product = Product::firstOrNew(['product_key' => $item->product_key, 'company_id' => $this->invoice->company->id]);
$product->product_key = $item->product_key;
$product->notes = $item->notes;
$product->cost = $item->cost;
$product->price = $item->price;
$product->quantity = $item->quantity;
$product->tax_name1 = $item->tax_name1;
$product->tax_rate1 = $item->tax_rate1;
$product->tax_name2 = $item->tax_name2;
$product->tax_rate2 = $item->tax_rate2;
$product->custom_value1 = $item->custom_value1;
$product->custom_value2 = $item->custom_value2;
$product->custom_value3 = $item->custom_value3;
$product->custom_value4 = $item->custom_value4;
$product->notes = isset($item->notes) ? $item->notes : '';
$product->cost = isset($item->cost) ? $item->cost : 0;
$product->price = isset($item->price) ? $item->price : 0;
$product->quantity = isset($item->quantity) ? $item->quantity : 0;
$product->tax_name1 = isset($item->tax_name1) ? $item->tax_name1 : '';
$product->tax_rate1 = isset($item->tax_rate1) ? $item->tax_rate1 : 0 ;
$product->tax_name2 = isset($item->tax_name2) ? $item->tax_name2 : '';
$product->tax_rate2 = isset($item->tax_rate2) ? $item->tax_rate2 : 0;
$product->custom_value1 = isset($item->custom_value1) ? $item->custom_value1 : '';
$product->custom_value2 = isset($item->custom_value2) ? $item->custom_value2 : '';
$product->custom_value3 = isset($item->custom_value3) ? $item->custom_value3 : '';
$product->custom_value4 = isset($item->custom_value4) ? $item->custom_value4 : '';
$product->user_id = $this->invoice->user_id;
$product->company_id = $this->invoice->company_id;
$product->project_id = $this->invoice->project_id;

View File

@ -11,6 +11,7 @@
namespace App\Listeners\Invoice;
use App\Libraries\MultiDB;
use App\Models\Activity;
use App\Repositories\ActivityRepository;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -37,6 +38,7 @@ class CreateInvoiceHtmlBackup implements ShouldQueue
*/
public function handle($event)
{
MultiDB::setDB($event->company->db);
$fields = new \stdClass;

View File

@ -17,6 +17,8 @@ use App\Events\Invoice\InvoiceWasUpdated;
use App\Helpers\Invoice\InvoiceSum;
use App\Helpers\Invoice\InvoiceSumInclusive;
use App\Jobs\Client\UpdateClientBalance;
use App\Jobs\Company\UpdateCompanyLedgerWithInvoice;
use App\Jobs\Invoice\ApplyInvoiceNumber;
use App\Jobs\Invoice\CreateInvoicePdf;
use App\Models\Currency;
use App\Models\Filterable;
@ -330,6 +332,7 @@ class Invoice extends BaseModel
if(!Storage::exists($storage_path)) {
event(new InvoiceWasUpdated($this, $this->company));
CreateInvoicePdf::dispatch($this, $this->company);
}
return $public_path;
@ -443,11 +446,17 @@ class Invoice extends BaseModel
$this->setReminder();
event(new InvoiceWasMarkedSent($this));
event(new InvoiceWasMarkedSent($this, $this->company));
UpdateClientBalance::dispatchNow($this->client, $this->balance);
UpdateClientBalance::dispatchNow($this->client, $this->balance, $this->company);
ApplyInvoiceNumber::dispatchNow($this, $this->client->getMergedSettings(), $this->company);
UpdateCompanyLedgerWithInvoice::dispatchNow($this, $this->balance, $this->company);
$this->save();
return $this;
}
/**

View File

@ -122,13 +122,13 @@ class InvoiceRepository extends BaseRepository
$finished_amount = $invoice->amount;
/**/
if($finished_amount != $starting_amount)
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount));
if(($finished_amount != $starting_amount) && ($invoice->status_id != Invoice::STATUS_DRAFT))
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, ($finished_amount - $starting_amount), $invoice->company);
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company);
if($invoice->company->update_products !== false)
UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice);
UpdateOrCreateProduct::dispatch($invoice->line_items, $invoice, $invoice->company);
return $invoice->fresh();
@ -143,18 +143,8 @@ class InvoiceRepository extends BaseRepository
*/
public function markSent(Invoice $invoice) : ?Invoice
{
$invoice->markSent();
/*
* Why? because up until this point the invoice was a draft.
* When marked as sent it becomes a ledgerable item.
*
*/
$invoice = ApplyInvoiceNumber::dispatchNow($invoice, $invoice->client->getMergedSettings(), $invoice->company);
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);
return $invoice;
return $invoice->markSent();
}

View File

@ -31,9 +31,15 @@ class PaymentRepository extends BaseRepository
return Payment::class;
}
/**
* Saves
* @param Request $request [description]
* @param Payment $payment [description]
* @return [type] [description]
*/
public function save(Request $request, Payment $payment) : ?Payment
{
//todo this save() only works for new payments... will fail if a Payment is updated and saved through here.
$payment->fill($request->input());
$payment->save();

View File

@ -160,11 +160,11 @@ class RandomDataSeeder extends Seeder
event(new CreateInvoiceInvitation($invoice));
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance);
UpdateCompanyLedgerWithInvoice::dispatchNow($invoice, $invoice->balance, $invoice->company);
$invoice_repo->markSent($invoice);
event(new InvoiceWasMarkedSent($invoice));
event(new InvoiceWasMarkedSent($invoice, $company));
if(rand(0, 1)) {
$payment = App\Models\Payment::create([

View File

@ -45,7 +45,7 @@ class UpdateCompanyLedgerTest extends TestCase
->orderBy('id', 'DESC')
->first();
$payment = $ledger->adjustment * -1;
$payment = $ledger->adjustment * - 1;
$this->assertEquals($invoice->amount, $payment);
}
@ -63,7 +63,7 @@ class UpdateCompanyLedgerTest extends TestCase
->whereCompanyId($this->invoice->company_id)
->get();
$this->assertEquals(1, count($ledger));
$this->assertGreaterThan(1, count($ledger));
}

View File

@ -200,7 +200,7 @@ trait MockAccountData
});
UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount);
UpdateCompanyLedgerWithInvoice::dispatchNow($this->invoice, $this->invoice->amount, $this->invoice->company);
$recurring_invoice = InvoiceToRecurringInvoiceFactory::create($this->invoice);
$recurring_invoice->next_send_date = Carbon::now();

View File

@ -76,11 +76,11 @@ class GeneratesCounterTest extends TestCase
$invoice_number = $this->getNextInvoiceNumber($this->client);
$this->assertEquals($invoice_number, 0007);
$this->assertEquals($invoice_number, '0008');
$invoice_number = $this->getNextInvoiceNumber($this->client);
$this->assertEquals($invoice_number, '0008');
$this->assertEquals($invoice_number, '0009');
}
@ -242,11 +242,11 @@ class GeneratesCounterTest extends TestCase
$invoice_number = $this->getNextInvoiceNumber($cliz);
$this->assertEquals($invoice_number, '0007');
$this->assertEquals($invoice_number, '0008');
$invoice_number = $this->getNextInvoiceNumber($cliz);
$this->assertEquals($invoice_number, '0008');
$this->assertEquals($invoice_number, '0009');
}