1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-05 18:52:44 +01:00

Refactor invoice calculations class, use FormRequests on destroy()

This commit is contained in:
David Bomba 2019-04-16 13:28:05 +10:00
parent c5e0d2a8d9
commit cb7eea0e7b
9 changed files with 44 additions and 33 deletions

View File

@ -18,7 +18,7 @@ class InvoiceFactory
$invoice->due_date = null;
$invoice->is_deleted = false;
$invoice->line_items = json_encode([]);
$invoice->settings = json_encode([]);
$invoice->settings = json_encode([]); //todo need to embed the settings here
$invoice->backup = json_encode([]);
$invoice->tax_name1 = '';
$invoice->tax_rate1 = 0;

View File

@ -44,12 +44,11 @@ class InvoiceCalc
* Constructs the object with Invoice and Settings object
*
* @param \App\Models\Invoice $invoice The invoice
* @param \stdClass $settings The settings
*/
public function __construct($invoice, \stdClass $settings)
public function __construct($invoice)
{
$this->invoice = $invoice;
$this->settings = $settings;
$this->settings = $invoice->settings;
$this->tax_map = new Collection;
}

View File

@ -6,6 +6,7 @@ use App\DataMapper\ClientSettings;
use App\Factory\ClientFactory;
use App\Filters\ClientFilters;
use App\Http\Requests\Client\CreateClientRequest;
use App\Http\Requests\Client\DestroyClientRequest;
use App\Http\Requests\Client\EditClientRequest;
use App\Http\Requests\Client\ShowClientRequest;
use App\Http\Requests\Client\StoreClientRequest;
@ -19,10 +20,10 @@ use App\Models\Country;
use App\Models\Currency;
use App\Models\Size;
use App\Repositories\ClientRepository;
use App\Transformers\ClientTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Transformers\ClientTransformer;
/**
* Class ClientController
@ -142,7 +143,7 @@ class ClientController extends BaseController
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Client $client)
public function destroy(DestroyClientRequest $request, Client $client)
{
$client->delete();

View File

@ -3,7 +3,13 @@
namespace App\Http\Controllers;
use App\Factory\InvoiceFactory;
use App\Http\Requests\Invoice\CreateInvoiceRequest;
use App\Http\Requests\Invoice\EditInvoiceRequest;
use App\Http\Requests\Invoice\ShowInvoiceRequest;
use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Invoice\UpdateInvoiceRequest;
use App\Models\Invoice;
use App\Transformers\InvoiceTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Http\Request;
@ -56,7 +62,7 @@ class InvoiceController extends BaseController
*
* @return \Illuminate\Http\Response
*/
public function create()
public function create(CreateInvoiceRequest $request)
{
$invoice = InvoiceFactory::create(auth()->user()->company()->id, auth()->user()->id);
@ -69,9 +75,12 @@ class InvoiceController extends BaseController
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
public function store(StoreInvoiceRequest $request)
{
//
$invoice = $this->invoice_repo->save();
return $this->itemResponse($invoice);
}
/**
@ -80,7 +89,7 @@ class InvoiceController extends BaseController
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
public function show(ShowInvoiceRequest $request)
{
//
}
@ -91,7 +100,7 @@ class InvoiceController extends BaseController
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
public function edit(EditInvoiceRequest)
{
//
}
@ -103,7 +112,7 @@ class InvoiceController extends BaseController
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(UpdateInvoiceRequest $request)
{
//
}
@ -114,7 +123,7 @@ class InvoiceController extends BaseController
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function destroy(DestroyInvoiceRequest $request, Invoice $invoice)
{
//
}

View File

@ -13,7 +13,7 @@ class EditClientRequest extends Request
* @return bool
*/
public function authorize()
public function authorize() : bool
{
return auth()->user()->can('edit', $this->client);
}

View File

@ -2,15 +2,17 @@
namespace App\Repositories;
use App\Helpers\Invoice\InvoiceCalc;
use App\Models\Invoice;
use Illuminate\Http\Request;
/**
*
* InvoiceRepository
*/
class InvoiceRepository extends BaseRepository
{
public function getClassName()
{
return Invoice::class;
@ -21,6 +23,8 @@ class InvoiceRepository extends BaseRepository
$invoice->fill($request->input());
$invoice->save();
$invoice_calc = new InvoiceCalc($invoice, $invoice->settings);
return $invoice;
}

View File

@ -21,7 +21,6 @@ $factory->define(App\Models\Invoice::class, function (Faker $faker) {
'invoice_date' => $faker->date(),
'due_date' => $faker->date(),
'line_items' => false,
'options' => '',
'backup' => '',
];
});

View File

@ -111,7 +111,7 @@ class ClientTest extends TestCase
$this->assertNotNull($company);
$this->assertNotNull($user->tokens->first()->company);
factory(\App\Models\Client::class, 20)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
factory(\App\Models\Client::class, 5)->create(['user_id' => $user->id, 'company_id' => $company->id])->each(function ($c) use ($user, $company){
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
@ -120,7 +120,7 @@ class ClientTest extends TestCase
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,10)->create([
factory(\App\Models\ClientContact::class,2)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
@ -162,7 +162,7 @@ class ClientTest extends TestCase
])->delete('/api/v1/clients/'.$this->encodePrimaryKey($client->id));
$response->assertStatus(200);
/*
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
@ -173,7 +173,7 @@ class ClientTest extends TestCase
$response->assertStatus(200);
*/
}
}

View File

@ -25,12 +25,11 @@ class InvoiceTest extends TestCase
parent::setUp();
$this->invoice = InvoiceFactory::create();
$this->invoice = InvoiceFactory::create(1,1);//stub the company and user_id
$this->invoice->line_items = $this->buildLineItems();
$this->settings = $this->buildSettings();
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$this->invoice->settings = $this->buildSettings();
$this->invoice_calc = new InvoiceCalc($this->invoice);
}
@ -119,9 +118,9 @@ class InvoiceTest extends TestCase
$this->invoice->custom_value1 = 5;
$this->invoice->tax_name1 = 'GST';
$this->invoice->tax_rate1 = 10;
$this->settings->inclusive_taxes = false;
$this->invoice->settings->inclusive_taxes = false;
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$this->invoice_calc = new InvoiceCalc($this->invoice);
$this->invoice_calc->build();
@ -134,7 +133,7 @@ class InvoiceTest extends TestCase
public function testInvoiceTotalsWithDiscountWithSurchargeWithDoubleExclusiveTax()
{
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$this->invoice_calc = new InvoiceCalc($this->invoice);
$this->invoice->discount = 5;
$this->invoice->custom_value1 = 5;
@ -142,7 +141,7 @@ class InvoiceTest extends TestCase
$this->invoice->tax_rate1 = 10;
$this->invoice->tax_name2 = 'GST';
$this->invoice->tax_rate2 = 10;
$this->settings->inclusive_taxes = false;
$this->invoice->settings->inclusive_taxes = false;
$this->invoice_calc->build();
@ -174,11 +173,11 @@ class InvoiceTest extends TestCase
$line_items[] = $item;
$this->invoice->line_items = $line_items;
$this->settings->inclusive_taxes = true;
$this->invoice->settings->inclusive_taxes = true;
$this->invoice->discount = 0;
$this->invoice->custom_value1 = 0;
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$this->invoice_calc = new InvoiceCalc($this->invoice);
$this->invoice_calc->build();
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);
@ -215,9 +214,9 @@ class InvoiceTest extends TestCase
$this->invoice->tax_rate1 = 10;
$this->invoice->tax_name2 = 'GST';
$this->invoice->tax_rate2 = 10;
$this->settings->inclusive_taxes = false;
$this->invoice_calc = new InvoiceCalc($this->invoice, $this->settings);
$this->invoice->settings->inclusive_taxes = false;
$this->invoice_calc = new InvoiceCalc($this->invoice);
$this->invoice_calc->build();
$this->assertEquals($this->invoice_calc->getSubTotal(), 20);