diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 446c5c12ac19..0a828d0ceeaa 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -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); diff --git a/app/Events/Invoice/InvoiceWasMarkedSent.php b/app/Events/Invoice/InvoiceWasMarkedSent.php index f09f886d365d..5042c8a600bf 100644 --- a/app/Events/Invoice/InvoiceWasMarkedSent.php +++ b/app/Events/Invoice/InvoiceWasMarkedSent.php @@ -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; } } diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index f661d2f69318..69df1c6e4a2b 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -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); diff --git a/app/Jobs/Client/UpdateClientBalance.php b/app/Jobs/Client/UpdateClientBalance.php index 9ee0cd0ae6b3..247ab6f89c4b 100644 --- a/app/Jobs/Client/UpdateClientBalance.php +++ b/app/Jobs/Client/UpdateClientBalance.php @@ -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(); diff --git a/app/Jobs/Client/UpdateClientPaidToDate.php b/app/Jobs/Client/UpdateClientPaidToDate.php index d833d293ce9f..35fc1262a935 100644 --- a/app/Jobs/Client/UpdateClientPaidToDate.php +++ b/app/Jobs/Client/UpdateClientPaidToDate.php @@ -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(); diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php index e24b736ca8f7..6c18e670fc24 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithInvoice.php @@ -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) diff --git a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php index fcea4f42d8af..fc367adefb2a 100644 --- a/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php +++ b/app/Jobs/Company/UpdateCompanyLedgerWithPayment.php @@ -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*/ diff --git a/app/Jobs/Invoice/ApplyInvoicePayment.php b/app/Jobs/Invoice/ApplyInvoicePayment.php index 8e63a140c8f7..4e378cd9c043 100644 --- a/app/Jobs/Invoice/ApplyInvoicePayment.php +++ b/app/Jobs/Invoice/ApplyInvoicePayment.php @@ -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); + } diff --git a/app/Jobs/Invoice/MarkInvoicePaid.php b/app/Jobs/Invoice/MarkInvoicePaid.php index f9f7a8858b2d..230a72d33e31 100644 --- a/app/Jobs/Invoice/MarkInvoicePaid.php +++ b/app/Jobs/Invoice/MarkInvoicePaid.php @@ -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; } diff --git a/app/Jobs/Invoice/ReverseInvoicePayment.php b/app/Jobs/Invoice/ReverseInvoicePayment.php index 3a67569517d6..1ef1338207b1 100644 --- a/app/Jobs/Invoice/ReverseInvoicePayment.php +++ b/app/Jobs/Invoice/ReverseInvoicePayment.php @@ -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); } } \ No newline at end of file diff --git a/app/Jobs/Invoice/UpdateInvoicePayment.php b/app/Jobs/Invoice/UpdateInvoicePayment.php index 8b062dd54093..473df12a3e3e 100644 --- a/app/Jobs/Invoice/UpdateInvoicePayment.php +++ b/app/Jobs/Invoice/UpdateInvoicePayment.php @@ -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(); diff --git a/app/Jobs/Product/UpdateOrCreateProduct.php b/app/Jobs/Product/UpdateOrCreateProduct.php index 1dd085ee3f72..089793b648c8 100644 --- a/app/Jobs/Product/UpdateOrCreateProduct.php +++ b/app/Jobs/Product/UpdateOrCreateProduct.php @@ -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; diff --git a/app/Listeners/Invoice/CreateInvoiceHtmlBackup.php b/app/Listeners/Invoice/CreateInvoiceHtmlBackup.php index 4e374bdaef76..42efcc1deaee 100644 --- a/app/Listeners/Invoice/CreateInvoiceHtmlBackup.php +++ b/app/Listeners/Invoice/CreateInvoiceHtmlBackup.php @@ -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; diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 4aca341c1c37..2ed3d2b11092 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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; } /** diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index da3a61a95aec..a2888baa0b10 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -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(); } diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 1db89490bbd9..93743344316e 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -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(); diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index f5167ffb3967..4fd1bb0d0932 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -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([ diff --git a/tests/Integration/UpdateCompanyLedgerTest.php b/tests/Integration/UpdateCompanyLedgerTest.php index 98f114e21c47..0627238866bc 100644 --- a/tests/Integration/UpdateCompanyLedgerTest.php +++ b/tests/Integration/UpdateCompanyLedgerTest.php @@ -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)); } diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index e8b6d1c16d3c..114bfedaabe7 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -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(); diff --git a/tests/Unit/GeneratesCounterTest.php b/tests/Unit/GeneratesCounterTest.php index f68b16dc9b15..79264007134d 100644 --- a/tests/Unit/GeneratesCounterTest.php +++ b/tests/Unit/GeneratesCounterTest.php @@ -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'); }