Fixes for tax rates / archive delete

This commit is contained in:
David Bomba 2023-09-28 15:22:17 +10:00
parent 0a317b1e6b
commit 5f064a7d49
2 changed files with 72 additions and 8 deletions

View File

@ -125,7 +125,10 @@ class TaxRateController extends BaseController
*/
public function create(CreateTaxRateRequest $request)
{
$tax_rate = TaxRateFactory::create(auth()->user()->company()->id, auth()->user()->id);
/** @var \App\Models\User $user */
$user = auth()->user();
$tax_rate = TaxRateFactory::create($user->company()->id, auth()->user()->id);
return $this->itemResponse($tax_rate);
}
@ -138,7 +141,10 @@ class TaxRateController extends BaseController
*/
public function store(StoreTaxRateRequest $request)
{
$tax_rate = TaxRateFactory::create(auth()->user()->company()->id, auth()->user()->id);
/** @var \App\Models\User $user */
$user = auth()->user();
$tax_rate = TaxRateFactory::create($user->company()->id, $user->id);
$tax_rate->fill($request->all());
$tax_rate->save();
@ -417,15 +423,33 @@ class TaxRateController extends BaseController
*/
public function bulk()
{
$action = request()->input('action');
/** @var \App\Models\User $user */
$user = auth()->user();
$action = request()->input('action');
$ids = request()->input('ids');
$tax_rates = TaxRate::withTrashed()->find($this->transformKeys($ids));
$tax_rates->each(function ($tax_rate, $key) use ($action) {
if (auth()->user()->can('edit', $tax_rate)) {
$tax_rates->each(function ($tax_rate, $key) use ($action, $user) {
if ($user->can('edit', $tax_rate)) {
if(in_array($action, ['archive','delete'])) {
$settings = $user->company()->settings;
foreach(['tax_name1','tax_name2','tax_name3'] as $tax_name) {
if($settings->{$tax_name} == $tax_rate->name) {
$settings->{$tax_name} = '';
$settings->{str_replace("name", "rate", $tax_name)} = '';
}
}
$user->company()->saveSettings($settings, $user->company());
}
$this->base_repo->{$action}($tax_rate);
}
});

View File

@ -11,13 +11,15 @@
namespace Tests\Feature;
use Tests\TestCase;
use App\Models\Company;
use App\Models\TaxRate;
use Tests\MockAccountData;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -44,6 +46,44 @@ class TaxRateApiTest extends TestCase
Model::reguard();
}
public function testRemovingDefaultTaxes()
{
$t = TaxRate::factory()->create([
'company_id' => $this->company->id,
'user_id' => $this->user->id,
'name' => 'nastytax1',
'rate' => 10,
]);
$settings = $this->company->settings;
$settings->tax_rate1 = $t->rate;
$settings->tax_name1 = $t->name;
$this->company->saveSettings($settings, $this->company);
$this->company->fresh();
$this->assertEquals('nastytax1', $this->company->settings->tax_name1);
$this->assertEquals(10, $this->company->settings->tax_rate1);
$data = [
'ids' => [$this->encodePrimaryKey($t->id)],
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tax_rates/bulk?action=archive', $data);
$response->assertStatus(200);
$this->company = $this->company->fresh();
$this->assertEquals('', $this->company->getSetting('tax_name1'));
$this->assertEquals(0, $this->company->getSetting('tax_rate1'));
}
public function testTaxRatesGetFilter()
{
$response = $this->withHeaders([