Fixes for tets

This commit is contained in:
David Bomba 2019-10-07 20:39:22 +11:00
parent 264d6662e3
commit c46ebb2778
10 changed files with 45 additions and 27 deletions

View File

@ -201,7 +201,7 @@ class InvoiceCalc
foreach($this->invoice->line_items as $item) { 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(); $item_calc->process();

View File

@ -25,6 +25,8 @@ class InvoiceItemCalc
protected $settings; protected $settings;
protected $invoice;
private $total_taxes; private $total_taxes;
private $total_discounts; private $total_discounts;
@ -33,7 +35,7 @@ class InvoiceItemCalc
private $line_total; private $line_total;
public function __construct(\stdClass $item, $settings) public function __construct(\stdClass $item, $settings, $invoice)
{ {
$this->item = $item; $this->item = $item;
@ -42,11 +44,13 @@ class InvoiceItemCalc
$this->tax_collection = collect([]); $this->tax_collection = collect([]);
$this->invoice = $invoice;
} }
public function process() 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() $this->setDiscount()
->calcTaxes(); ->calcTaxes();
@ -61,7 +65,7 @@ class InvoiceItemCalc
if($this->item->is_amount_discount) 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; $this->line_total -= $discount;
@ -69,7 +73,7 @@ class InvoiceItemCalc
} }
else 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; $this->line_total -= $discount;
@ -87,12 +91,12 @@ class InvoiceItemCalc
if(isset($this->item->tax_rate1) && $this->item->tax_rate1 > 0) 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) 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 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; $item_tax += $item_tax_rate1_total;
@ -101,12 +105,12 @@ class InvoiceItemCalc
if(isset($this->item->tax_rate2) && $this->item->tax_rate2 > 0) 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) 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 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; $item_tax += $item_tax_rate2_total;

View File

@ -395,7 +395,7 @@ class InvoiceController extends BaseController
$invoice = $this->invoice_repo->save($request->all(), $invoice); $invoice = $this->invoice_repo->save($request->all(), $invoice);
event(new InvoiceWasUpdated($invoice)); event(new InvoiceWasUpdated($invoice->with('client,client.currency')));
return $this->itemResponse($invoice); return $this->itemResponse($invoice);

View File

@ -93,7 +93,7 @@ class CreateInvoicePdf implements ShouldQueue
$variables = array_merge($invoice->makeLabels(), $invoice->makeValues()); $variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
$design = str_replace(array_keys($variables), array_values($variables), $design); $design = str_replace(array_keys($variables), array_values($variables), $design);
//
$data['invoice'] = $invoice; $data['invoice'] = $invoice;
return $this->renderView($design, $data); return $this->renderView($design, $data);

View File

@ -61,7 +61,7 @@ class Number
//public static function formatMoney($value, $currency, $country, $settings) :string //public static function formatMoney($value, $currency, $country, $settings) :string
public static function formatMoney($value, $client) :string public static function formatMoney($value, $client) :string
{ {
//\Log::error(debug_backtrace()[1]['function']);
$thousand = $client->currency->thousand_separator; $thousand = $client->currency->thousand_separator;
$decimal = $client->currency->decimal_separator; $decimal = $client->currency->decimal_separator;
$precision = $client->currency->precision; $precision = $client->currency->precision;

View File

@ -154,6 +154,11 @@ trait MakesInvoiceValues
*/ */
public function makeValues() :array public function makeValues() :array
{ {
if(!$this->client->currency || !$this->client){
throw new Exception(debug_backtrace()[1]['function'], 1);
exit;
}
$data = []; $data = [];
$data['$invoice_date'] = $this->invoice_date; $data['$invoice_date'] = $this->invoice_date;

View File

@ -32,8 +32,6 @@ class ClientModelTest extends TestCase
public function testPaymentMethods() public function testPaymentMethods()
{ {
$amount = 40; $amount = 40;
$company_gateways = $this->client->getSetting('company_gateways'); $company_gateways = $this->client->getSetting('company_gateways');

View File

@ -139,7 +139,7 @@ class InvoiceTest extends TestCase
$this->assertNotNull($company); $this->assertNotNull($company);
//$this->assertNotNull($user->token->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([ factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id, 'user_id' => $user->id,
@ -179,7 +179,6 @@ class InvoiceTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$invoice_update = [ $invoice_update = [
'client_id' => $invoice->client_id,
'status_id' => Invoice::STATUS_PAID 'status_id' => Invoice::STATUS_PAID
]; ];
@ -187,7 +186,7 @@ class InvoiceTest extends TestCase
$this->assertNotNull($invoice->settings); $this->assertNotNull($invoice->settings);
$this->assertTrue(property_exists($invoice->settings, 'custom_invoice_taxes1')); $this->assertTrue(property_exists($invoice->settings, 'custom_invoice_taxes1'));
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'), 'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token, 'X-API-TOKEN' => $token,

View File

@ -5,6 +5,7 @@ namespace Tests\Unit;
use App\Factory\InvoiceItemFactory; use App\Factory\InvoiceItemFactory;
use App\Helpers\Invoice\InvoiceItemCalc; use App\Helpers\Invoice\InvoiceItemCalc;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
/** /**
@ -14,12 +15,16 @@ use Tests\TestCase;
class InvoiceItemTest extends TestCase class InvoiceItemTest extends TestCase
{ {
use MockAccountData;
use DatabaseTransactions;
public function setUp() :void public function setUp() :void
{ {
parent::setUp(); parent::setUp();
$this->makeTestData();
} }
public function testInvoiceItemTotalSimple() public function testInvoiceItemTotalSimple()
@ -33,7 +38,7 @@ class InvoiceItemTest extends TestCase
$settings->inclusive_taxes = true; $settings->inclusive_taxes = true;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 10); $this->assertEquals($item_calc->getLineTotal(), 10);
@ -50,7 +55,7 @@ class InvoiceItemTest extends TestCase
$settings = new \stdClass; $settings = new \stdClass;
$settings->inclusive_taxes = true; $settings->inclusive_taxes = true;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 8); $this->assertEquals($item_calc->getLineTotal(), 8);
@ -68,7 +73,7 @@ class InvoiceItemTest extends TestCase
$settings->inclusive_taxes = true; $settings->inclusive_taxes = true;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getLineTotal(), 7.48); $this->assertEquals($item_calc->getLineTotal(), 7.48);
@ -87,7 +92,7 @@ class InvoiceItemTest extends TestCase
$settings->inclusive_taxes = true; $settings->inclusive_taxes = true;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 0.68); $this->assertEquals($item_calc->getTotalTaxes(), 0.68);
@ -106,7 +111,7 @@ class InvoiceItemTest extends TestCase
$settings->inclusive_taxes = false; $settings->inclusive_taxes = false;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 0.75); $this->assertEquals($item_calc->getTotalTaxes(), 0.75);
@ -126,7 +131,7 @@ class InvoiceItemTest extends TestCase
$settings->inclusive_taxes = true; $settings->inclusive_taxes = true;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 1.79); $this->assertEquals($item_calc->getTotalTaxes(), 1.79);
@ -146,7 +151,7 @@ class InvoiceItemTest extends TestCase
$settings->inclusive_taxes = false; $settings->inclusive_taxes = false;
$settings->precision = 2; $settings->precision = 2;
$item_calc = new InvoiceItemCalc($item, $settings); $item_calc = new InvoiceItemCalc($item, $settings, $this->invoice);
$item_calc->process(); $item_calc->process();
$this->assertEquals($item_calc->getTotalTaxes(), 2.06); $this->assertEquals($item_calc->getTotalTaxes(), 2.06);

View File

@ -25,9 +25,16 @@ class InvoiceTest extends TestCase
{ {
parent::setUp(); parent::setUp();
//append a client here - otherwise tests are broken.
$this->invoice = InvoiceFactory::create(1,1);//stub the company and user_id $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->invoice->line_items = $this->buildLineItems();
$this->settings = $this->invoice->settings; $this->settings = $this->invoice->settings;