diff --git a/app/Helpers/Invoice/InvoiceCalc.php b/app/Helpers/Invoice/InvoiceCalc.php index 8e8032ba14a4..92a1344e5b01 100644 --- a/app/Helpers/Invoice/InvoiceCalc.php +++ b/app/Helpers/Invoice/InvoiceCalc.php @@ -201,7 +201,7 @@ class InvoiceCalc foreach($this->invoice->line_items as $item) { - $item_calc = new InvoiceItemCalc($item, $this->settings); + $item_calc = new InvoiceItemCalc($item, $this->settings, $this->invoice); $item_calc->process(); diff --git a/app/Helpers/Invoice/InvoiceItemCalc.php b/app/Helpers/Invoice/InvoiceItemCalc.php index fa5a3339ba4d..d88356bb088a 100644 --- a/app/Helpers/Invoice/InvoiceItemCalc.php +++ b/app/Helpers/Invoice/InvoiceItemCalc.php @@ -25,6 +25,8 @@ class InvoiceItemCalc protected $settings; + protected $invoice; + private $total_taxes; private $total_discounts; @@ -33,7 +35,7 @@ class InvoiceItemCalc private $line_total; - public function __construct(\stdClass $item, $settings) + public function __construct(\stdClass $item, $settings, $invoice) { $this->item = $item; @@ -42,11 +44,13 @@ class InvoiceItemCalc $this->tax_collection = collect([]); + $this->invoice = $invoice; } public function process() { - $this->line_total = $this->formatValue($this->item->cost, $this->settings->precision) * $this->formatValue($this->item->quantity, $this->settings->precision); + + $this->line_total = $this->formatValue($this->item->cost, $this->invoice->client->currency->precision) * $this->formatValue($this->item->quantity, $this->invoice->client->currency->precision); $this->setDiscount() ->calcTaxes(); @@ -61,7 +65,7 @@ class InvoiceItemCalc if($this->item->is_amount_discount) { - $discount = $this->formatValue($this->item->discount, $this->settings->precision); + $discount = $this->formatValue($this->item->discount, $this->invoice->client->currency->precision); $this->line_total -= $discount; @@ -69,7 +73,7 @@ class InvoiceItemCalc } else { - $discount = $this->formatValue(($this->line_total * $this->item->discount / 100), $this->settings->precision); + $discount = $this->formatValue(($this->line_total * $this->item->discount / 100), $this->invoice->client->currency->precision); $this->line_total -= $discount; @@ -87,12 +91,12 @@ class InvoiceItemCalc if(isset($this->item->tax_rate1) && $this->item->tax_rate1 > 0) { - $tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->settings->precision); + $tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->invoice->client->currency->precision); if($this->settings->inclusive_taxes) - $item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->settings->precision); + $item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->invoice->client->currency->precision); else - $item_tax_rate1_total = $this->formatValue(($this->line_total * $tax_rate1/100), $this->settings->precision); + $item_tax_rate1_total = $this->formatValue(($this->line_total * $tax_rate1/100), $this->invoice->client->currency->precision); $item_tax += $item_tax_rate1_total; @@ -101,12 +105,12 @@ class InvoiceItemCalc if(isset($this->item->tax_rate2) && $this->item->tax_rate2 > 0) { - $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->settings->precision); + $tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->invoice->client->currency->precision); if($this->settings->inclusive_taxes) - $item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->settings->precision); + $item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->invoice->client->currency->precision); else - $item_tax_rate2_total = $this->formatValue(($this->line_total * $tax_rate2/100), $this->settings->precision); + $item_tax_rate2_total = $this->formatValue(($this->line_total * $tax_rate2/100), $this->invoice->client->currency->precision); $item_tax += $item_tax_rate2_total; diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index 3f2132130017..d3910881158a 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -395,7 +395,7 @@ class InvoiceController extends BaseController $invoice = $this->invoice_repo->save($request->all(), $invoice); - event(new InvoiceWasUpdated($invoice)); + event(new InvoiceWasUpdated($invoice->with('client,client.currency'))); return $this->itemResponse($invoice); diff --git a/app/Listeners/Invoice/CreateInvoicePdf.php b/app/Listeners/Invoice/CreateInvoicePdf.php index cbcb7825ea2b..6459b8789ed2 100644 --- a/app/Listeners/Invoice/CreateInvoicePdf.php +++ b/app/Listeners/Invoice/CreateInvoicePdf.php @@ -93,7 +93,7 @@ class CreateInvoicePdf implements ShouldQueue $variables = array_merge($invoice->makeLabels(), $invoice->makeValues()); $design = str_replace(array_keys($variables), array_values($variables), $design); -// + $data['invoice'] = $invoice; return $this->renderView($design, $data); diff --git a/app/Utils/Number.php b/app/Utils/Number.php index 1d6ff50cd6a0..2a8d7dc73d8a 100644 --- a/app/Utils/Number.php +++ b/app/Utils/Number.php @@ -61,7 +61,7 @@ class Number //public static function formatMoney($value, $currency, $country, $settings) :string public static function formatMoney($value, $client) :string { - +//\Log::error(debug_backtrace()[1]['function']); $thousand = $client->currency->thousand_separator; $decimal = $client->currency->decimal_separator; $precision = $client->currency->precision; diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index 3642d78dd18d..0ee6e9a65b96 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -154,6 +154,11 @@ trait MakesInvoiceValues */ public function makeValues() :array { + if(!$this->client->currency || !$this->client){ + throw new Exception(debug_backtrace()[1]['function'], 1); + exit; + } + $data = []; $data['$invoice_date'] = $this->invoice_date; diff --git a/tests/Feature/ClientModelTest.php b/tests/Feature/ClientModelTest.php index fdcb5b659367..baebb16cbd26 100644 --- a/tests/Feature/ClientModelTest.php +++ b/tests/Feature/ClientModelTest.php @@ -32,8 +32,6 @@ class ClientModelTest extends TestCase public function testPaymentMethods() { - - $amount = 40; $company_gateways = $this->client->getSetting('company_gateways'); diff --git a/tests/Feature/InvoiceTest.php b/tests/Feature/InvoiceTest.php index 70b0a3aeb9f8..e63a73cdb0d9 100644 --- a/tests/Feature/InvoiceTest.php +++ b/tests/Feature/InvoiceTest.php @@ -139,7 +139,7 @@ class InvoiceTest extends TestCase $this->assertNotNull($company); //$this->assertNotNull($user->token->company); - factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){ + factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'currency_id' => 1])->each(function ($c) use ($user, $company){ factory(\App\Models\ClientContact::class,1)->create([ 'user_id' => $user->id, @@ -179,7 +179,6 @@ class InvoiceTest extends TestCase $response->assertStatus(200); $invoice_update = [ - 'client_id' => $invoice->client_id, 'status_id' => Invoice::STATUS_PAID ]; @@ -187,7 +186,7 @@ class InvoiceTest extends TestCase $this->assertNotNull($invoice->settings); $this->assertTrue(property_exists($invoice->settings, 'custom_invoice_taxes1')); - + $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $token, diff --git a/tests/Unit/InvoiceItemTest.php b/tests/Unit/InvoiceItemTest.php index edfb27d7fa27..32104a4cd8bf 100644 --- a/tests/Unit/InvoiceItemTest.php +++ b/tests/Unit/InvoiceItemTest.php @@ -5,6 +5,7 @@ namespace Tests\Unit; use App\Factory\InvoiceItemFactory; use App\Helpers\Invoice\InvoiceItemCalc; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Tests\MockAccountData; use Tests\TestCase; /** @@ -14,12 +15,16 @@ use Tests\TestCase; class InvoiceItemTest extends TestCase { + use MockAccountData; + use DatabaseTransactions; public function setUp() :void { parent::setUp(); + $this->makeTestData(); + } public function testInvoiceItemTotalSimple() @@ -33,7 +38,7 @@ class InvoiceItemTest extends TestCase $settings->inclusive_taxes = true; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getLineTotal(), 10); @@ -50,7 +55,7 @@ class InvoiceItemTest extends TestCase $settings = new \stdClass; $settings->inclusive_taxes = true; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getLineTotal(), 8); @@ -68,7 +73,7 @@ class InvoiceItemTest extends TestCase $settings->inclusive_taxes = true; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getLineTotal(), 7.48); @@ -87,7 +92,7 @@ class InvoiceItemTest extends TestCase $settings->inclusive_taxes = true; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getTotalTaxes(), 0.68); @@ -106,7 +111,7 @@ class InvoiceItemTest extends TestCase $settings->inclusive_taxes = false; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getTotalTaxes(), 0.75); @@ -126,7 +131,7 @@ class InvoiceItemTest extends TestCase $settings->inclusive_taxes = true; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getTotalTaxes(), 1.79); @@ -146,7 +151,7 @@ class InvoiceItemTest extends TestCase $settings->inclusive_taxes = false; $settings->precision = 2; - $item_calc = new InvoiceItemCalc($item, $settings); + $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice); $item_calc->process(); $this->assertEquals($item_calc->getTotalTaxes(), 2.06); diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index f9b24a593e38..6530476b7e25 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -25,9 +25,16 @@ class InvoiceTest extends TestCase { parent::setUp(); - + //append a client here - otherwise tests are broken. $this->invoice = InvoiceFactory::create(1,1);//stub the company and user_id + $client = factory(\App\Models\Client::class)->create([ + 'user_id' => 1, + 'company_id' => 1, + ]); + + $this->invoice->client_id = $client->id; + $this->invoice->line_items = $this->buildLineItems(); $this->settings = $this->invoice->settings;