Skip to content

Commit

Permalink
Merge branch 'fix/HES-1305' into 'develop'
Browse files Browse the repository at this point in the history
fix unlimited_stock

See merge request heseya/store-api!672
  • Loading branch information
bvlinsky committed May 24, 2022
2 parents 0f4843f + baf66a2 commit 5f6ce90
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public function deposits(): HasMany
public function groupedDeposits(): HasMany
{
return $this->hasMany(Deposit::class)
->selectRaw('SUM(quantity) as quantity, shipping_time, shipping_date')
->groupBy(['shipping_time', 'shipping_date'])
->selectRaw('item_id, SUM(quantity) as quantity, shipping_time, shipping_date')
->groupBy(['shipping_time', 'shipping_date', 'item_id'])
->having('quantity', '>', '0');
}

Expand Down
5 changes: 4 additions & 1 deletion app/Rules/UnlimitedShippingDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public function __construct(private Item $item)
*/
public function passes($attribute, $value): bool
{
if (is_null($value)) {
return true;
}
$depositService = App::make(DepositServiceContract::class);
$depositsDate = $depositService->getDepositsGroupByDateForItem($this->item, 'DESC');

return !isset($depositsDate[0]) ||
$this->item->unlimited_stock_shipping_date >= $depositsDate[0]['shipping_date'];
$value >= $depositsDate[0]['shipping_date'];
}

public function message(): string
Expand Down
5 changes: 4 additions & 1 deletion app/Rules/UnlimitedShippingTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ public function __construct(private Item $item)
*/
public function passes($attribute, $value): bool
{
if (is_null($value)) {
return true;
}
$depositService = App::make(DepositServiceContract::class);
$depositsTime = $depositService->getDepositsGroupByTimeForItem($this->item, 'DESC');

return !isset($depositsTime[0]) ||
$this->item->unlimited_stock_shipping_time >= $depositsTime[0]['shipping_time'];
$value >= $depositsTime[0]['shipping_time'];
}

public function message(): string
Expand Down
3 changes: 3 additions & 0 deletions app/Services/DepositService.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public function getDepositsGroupByDateForItem(Item $item, string $order = 'ASC')
return Deposit::query()->selectRaw('SUM(quantity) as quantity, shipping_date')
->whereNotNull('shipping_date')
->where('item_id', '=', $item->getKey())
->having('quantity', '>', '0')
->groupBy('shipping_date')
->orderBy('shipping_date', $order)
->get()->toArray();
Expand All @@ -179,6 +180,7 @@ public function getDepositsGroupByTimeForItem(Item $item, string $order = 'ASC')
return Deposit::query()->selectRaw('SUM(quantity) as quantity, shipping_time')
->whereNotNull('shipping_time')
->where('item_id', '=', $item->getKey())
->having('quantity', '>', '0')
->groupBy('shipping_time')
->orderBy('shipping_time', $order)
->get()->toArray();
Expand All @@ -189,6 +191,7 @@ public function getDepositWithoutShipping(Item $item): array
return Deposit::query()->selectRaw('SUM(quantity) as quantity')
->whereNull('shipping_time')
->whereNull('shipping_date')
->having('quantity', '>', '0')
->where('item_id', '=', $item->getKey())
->groupBy('shipping_time')
->get()->toArray();
Expand Down
84 changes: 79 additions & 5 deletions tests/Feature/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -769,16 +769,16 @@ public function testUpdateValidationUnlimitedShippingDateLesserThenShippingDate(
$this->$user->givePermissionTo('items.edit');

Event::fake(ItemUpdated::class);
$date = Carbon::now();

Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => 2.0,
'shipping_date' => $date->addDays(4)->toDateTimeString(),
'shipping_date' => Carbon::now()->addDays(4)->toDateTimeString(),
]);

$item = [
'sku' => 'TES/T3',
'unlimited_stock_shipping_date' => $date->addDays(1)->toDateTimeString(),
'unlimited_stock_shipping_date' => Carbon::now()->addDays(1)->toDateTimeString(),
];

$this->actingAs($this->$user)->patchJson(
Expand Down Expand Up @@ -824,9 +824,26 @@ public function testUpdateUnlimitedShippingTime($user): void
{
$this->$user->givePermissionTo('items.edit');

$time = 4;
Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => 2.0,
'shipping_time' => $time,
]);
Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => -2.0,
'shipping_time' => $time,
]);
Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => 2.0,
'shipping_time' => $time - 2,
]);

$item = [
'sku' => 'TES/T3',
'unlimited_stock_shipping_time' => 5,
'unlimited_stock_shipping_time' => $time - 1,
];

$this->actingAs($this->$user)->patchJson(
Expand Down Expand Up @@ -862,9 +879,24 @@ public function testUpdateUnlimitedShippingDate($user): void
{
$this->$user->givePermissionTo('items.edit');

Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => 2.0,
'shipping_date' => Carbon::now()->addDays(4)->toDateTimeString(),
]);
Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => -2.0,
'shipping_date' => Carbon::now()->addDays(4)->toDateTimeString(),
]);
Deposit::factory()->create([
'item_id' => $this->item->getKey(),
'quantity' => 2.0,
'shipping_date' => Carbon::now()->addDays(2)->toDateTimeString(),
]);
$item = [
'sku' => 'TES/T3',
'unlimited_stock_shipping_date' => Carbon::now()->toDateTimeString(),
'unlimited_stock_shipping_date' => Carbon::now()->addDays(3)->toDateTimeString(),
];

$this->actingAs($this->$user)->patchJson(
Expand Down Expand Up @@ -992,4 +1024,46 @@ public function testShowWhitAvailability($user): void
],
]);
}

/**
* @dataProvider authProvider
*/
public function testIndexWhitAvailability($user): void
{
$this->$user->givePermissionTo('items.show');
$this->$user->givePermissionTo('items.show_details');
$this->$user->givePermissionTo('deposits.add');

$item = Item::factory()->create();

$deposit = [
'quantity' => 10,
'shipping_time' => 10,
];

$this->actingAs($this->$user)->postJson(
"/items/id:{$item->getKey()}/deposits",
$deposit,
)->assertCreated();

$this
->actingAs($this->$user)
->getJson('/items/id:' . $item->getKey())
->assertOk()
->assertJsonFragment([
'availability' => [
['quantity' => '10.0000', 'shipping_time' => 10, 'shipping_date' => null],
],
]);

$this
->actingAs($this->$user)
->getJson('/items')
->assertOk()
->assertJsonFragment([
'availability' => [
['quantity' => '10.0000', 'shipping_time' => 10, 'shipping_date' => null],
],
]);
}
}

0 comments on commit 5f6ce90

Please sign in to comment.