Skip to content

Commit

Permalink
fix cancellazione quota utente. closes #262
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Feb 25, 2024
1 parent 7efcffc commit c2f85a7
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 27 deletions.
12 changes: 11 additions & 1 deletion code/app/Console/Commands/FixDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FixDatabase extends Command
protected $signature = 'fix:database';
protected $description = 'Sistema le informazioni sul DB per completare il deploy';

public function handle()
private function doAlways()
{
/*
I seeder dei tipi di movimento contabile e dei tipi di modificatore
Expand All @@ -35,6 +35,11 @@ public function handle()
*/
Artisan::call('db:seed', ['--force' => true, '--class' => 'MovementTypesSeeder']);
Artisan::call('db:seed', ['--force' => true, '--class' => 'ModifierTypesSeeder']);
}

public function handle()
{
$this->doAlways();

/*
Per revisionare le configurazioni relative ai limiti di credito per
Expand Down Expand Up @@ -73,6 +78,11 @@ public function handle()
$table->integer('payment_id')->nullable()->change();
});

Schema::table('users', function (Blueprint $table) {
$table->integer('fee_id')->nullable()->default(null)->change();
$table->integer('deposit_id')->nullable()->default(null)->change();
});

/*
Per aggiornare il formato delle date per gli ordini automatici
*/
Expand Down
3 changes: 2 additions & 1 deletion code/app/Helpers/Dates.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ function decodePeriodic($value)
return '';

$values = explode(' - ', $value);
if (count($values) < 4)
if (count($values) < 4) {
return '';
}

$ret = (object) [
'day' => '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function up()
$table->string('payment_method_id')->default('none');
$table->text('rid')->nullable();

$table->integer('fee_id')->default(0);
$table->integer('deposit_id')->default(0);
$table->integer('fee_id')->nullable()->default(null);
$table->integer('deposit_id')->nullable()->default(null);

$table->rememberToken();

Expand Down
36 changes: 25 additions & 11 deletions code/tests/Services/DatesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,25 +163,36 @@ public function testRecurrences()
$this->actingAs($this->userAdmin);
$recurrence = 'Lunedì - Secondo del Mese - ' . printableDate(date('Y-m-d')) . ' - ' . printableDate(date('Y-m-d', strtotime('+6 months')));

/*
Reminder: per le ricorrenze di chiusura e consegna si raccomanda si
usare offset inferiori ai 7 giorni, in modo da non tornare indietro
oltre una settimana.
Così si minimizza - ma non si elimina del tutto - il rischio di far
coincidere la data di apertura con quella odierna, che farebbe
scattare l'apertura di un ordine automatico, che modificherebbe le
prossime date di riferimento, che renderebbe invalidi i test
successivi
*/
app()->make('DatesService')->updateOrders([
'id' => ['', '', ''],
'target_id' => [$this->supplier1->id, $this->supplier1->id, $this->supplier1->id],
'recurring' => [$recurrence, $recurrence, $recurrence],
'action' => ['open', 'close', 'ship'],
'first_offset' => [10, 20, 15],
'second_offset' => [11, 5, 5],
'first_offset' => [10, 6, 5],
'second_offset' => [11, 3, 2],
'comment' => ['', 'pippo', ''],
'suspend' => [],
]);

$this->nextRound();

$dates = Date::all();
$this->assertEquals($dates->count(), 3);
$reference_monday = Carbon::parse('second monday of next month');
$reference_monday_formatted = $reference_monday->format('Y-m-d');

foreach($dates as $index => $date) {
$this->assertEquals($this->supplier1->id, $date->target->id);

$orders = $date->order_dates;
$this->assertTrue(count($orders) > 0);
$found = false;
Expand All @@ -202,40 +213,43 @@ public function testRecurrences()
}
}

$this->assertTrue($found);
break;

case 1:
$this->assertEquals('close', $date->action);
$this->assertEquals(20, $date->first_offset);
$this->assertEquals(5, $date->second_offset);
$this->assertEquals(6, $date->first_offset);
$this->assertEquals(3, $date->second_offset);
$this->assertEquals('pippo', $date->comment);

foreach($orders as $order) {
if ($order->end == $reference_monday_formatted) {
$this->assertEquals($reference_monday->copy()->subDays(20)->format('Y-m-d'), $order->start);
$this->assertEquals($reference_monday->copy()->addDays(5)->format('Y-m-d'), $order->shipping);
$this->assertEquals($reference_monday->copy()->subDays(6)->format('Y-m-d'), $order->start);
$this->assertEquals($reference_monday->copy()->addDays(3)->format('Y-m-d'), $order->shipping);
$found = true;
break;
}
}

$this->assertTrue($found);
break;

case 2:
$this->assertEquals('ship', $date->action);
$this->assertEquals(15, $date->first_offset);
$this->assertEquals(5, $date->second_offset);
$this->assertEquals(5, $date->first_offset);
$this->assertEquals(2, $date->second_offset);
$this->assertEquals('', $date->comment);

foreach($orders as $order) {
if ($order->shipping == $reference_monday_formatted) {
$this->assertEquals($reference_monday->copy()->subDays(15)->format('Y-m-d'), $order->start);
$this->assertEquals($reference_monday->copy()->subDays(5)->format('Y-m-d'), $order->end);
$this->assertEquals($reference_monday->copy()->subDays(5)->format('Y-m-d'), $order->start);
$this->assertEquals($reference_monday->copy()->subDays(2)->format('Y-m-d'), $order->end);
$found = true;
break;
}
}

$this->assertTrue($found);
break;
}

Expand Down
61 changes: 49 additions & 12 deletions code/tests/Services/MovementsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use Artisan;

use Illuminate\Support\Facades\Artisan;
use Illuminate\Database\Eloquent\ModelNotFoundException;

use App\Exceptions\AuthException;
use App\Exceptions\IllegalArgumentException;
use App\Movement;
use App\User;
use App\Balance;
use App\Currency;

class MovementsServiceTest extends TestCase
{
Expand All @@ -21,9 +24,9 @@ public function setUp(): void

$this->userWithAdminPerm = $this->createRoleAndUser($this->gas, 'movements.admin');
$this->userWithReferrerPerms = $this->createRoleAndUser($this->gas, 'movements.view');
$this->userWithNoPerms = \App\User::factory()->create(['gas_id' => $this->gas->id]);
$this->userWithNoPerms = User::factory()->create(['gas_id' => $this->gas->id]);

$this->sample_movement = \App\Movement::factory()->create([
$this->sample_movement = Movement::factory()->create([
'type' => 'donation-from-gas',
'method' => 'bank',
'sender_id' => $this->gas->id,
Expand Down Expand Up @@ -97,16 +100,16 @@ public function testCloseBalance()
{
$this->testStore();

$previous_balances = \App\Balance::all()->count();
$previous_balances = Balance::all()->count();

app()->make('MovementsService')->closeBalance([
'date' => printableDate(date('Y-m-d')),
]);

$now_balances = \App\Balance::all()->count();
$now_balances = Balance::all()->count();
$this->assertEquals($previous_balances * 2, $now_balances);

$now_balances = \App\Balance::where('current', true)->count();
$now_balances = Balance::where('current', true)->count();
$this->assertEquals($previous_balances, $now_balances);
}

Expand Down Expand Up @@ -236,9 +239,9 @@ public function testUserFees()
'amount' => $this->userWithNoPerms->gas->getConfig('annual_fee_amount'),
));

$reloaded = \App\User::find($this->userWithNoPerms->id);
$reloaded = User::find($this->userWithNoPerms->id);
$this->assertNotEquals($reloaded->fee_id, 0);
$fee = \App\Movement::find($reloaded->fee_id);
$fee = Movement::find($reloaded->fee_id);
$this->assertEquals($fee->amount, 5);

$expiration = date('Y-m-d', strtotime('-5 days'));
Expand All @@ -253,6 +256,40 @@ public function testUserFees()
$this->assertEquals($new_date, $expiration);
}

/*
Cancellazione manuale quota
*/
public function testRemoveUserFees()
{
$this->actingAs($this->userWithAdminPerm);

$this->userWithNoPerms->gas->setConfig('annual_fee_amount', 5);

app()->make('MovementsService')->store(array(
'type' => 'annual-fee',
'method' => 'bank',
'target_id' => $this->userWithNoPerms->gas->id,
'target_type' => 'App\Gas',
'sender_id' => $this->userWithNoPerms->id,
'sender_type' => 'App\User',
'amount' => $this->userWithNoPerms->gas->getConfig('annual_fee_amount'),
));

$this->nextRound();

$reloaded = User::find($this->userWithNoPerms->id);
$this->assertNotEquals($reloaded->fee_id, 0);
$fee = Movement::find($reloaded->fee_id);
$this->assertEquals($fee->amount, 5);

app()->make('MovementsService')->destroy($reloaded->fee_id);

$this->nextRound();

$reloaded = User::find($this->userWithNoPerms->id);
$this->assertNull($reloaded->fee_id);
}

/*
Salvataggio e modifica movimento con IntegralCES attivo
*/
Expand All @@ -267,7 +304,7 @@ public function testWithIntegralces()
]);

$currency_default = defaultCurrency();
$currency_integralces = \App\Currency::where('context', 'integralces')->first();
$currency_integralces = Currency::where('context', 'integralces')->first();

app()->make('MovementsService')->store(array(
'type' => 'donation-from-gas',
Expand All @@ -281,7 +318,7 @@ public function testWithIntegralces()
$this->assertEquals($this->sample_movement->amount * -1, $this->gas->currentBalanceAmount($currency_default));
$this->assertEquals(-100, $this->gas->currentBalanceAmount($currency_integralces));

$integralces_movement = \App\Movement::where('currency_id', $currency_integralces->id)->first();
$integralces_movement = Movement::where('currency_id', $currency_integralces->id)->first();
app()->make('MovementsService')->update($integralces_movement->id, array(
'currency_id' => $currency_default->id,
));
Expand Down

0 comments on commit c2f85a7

Please sign in to comment.