| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- <?php
- namespace Tests\Unit\Models;
- use App\Models\Reservation;
- use App\Models\Shortage;
- use App\Models\SparePart;
- use App\Models\SparePartOrder;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class SparePartTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- public function test_price_accessors_convert_from_kopeks_to_rubles(): void
- {
- $sparePart = SparePart::factory()->create([
- 'purchase_price' => 150.50, // Will be stored as 15050 kopeks
- 'customer_price' => 200.00,
- 'expertise_price' => 180.25,
- ]);
- $sparePart->refresh();
- $this->assertEquals(150.50, $sparePart->purchase_price);
- $this->assertEquals(200.00, $sparePart->customer_price);
- $this->assertEquals(180.25, $sparePart->expertise_price);
- }
- public function test_price_txt_accessors_format_prices(): void
- {
- $sparePart = SparePart::factory()->create([
- 'purchase_price' => 1500.50,
- ]);
- $sparePart->refresh();
- $this->assertStringContainsString('₽', $sparePart->purchase_price_txt);
- $this->assertStringContainsString('1', $sparePart->purchase_price_txt);
- $this->assertStringContainsString('500', $sparePart->purchase_price_txt);
- }
- public function test_price_txt_returns_dash_for_null_prices(): void
- {
- $sparePart = SparePart::factory()->create([
- 'purchase_price' => null,
- ]);
- $sparePart->refresh();
- $this->assertEquals('-', $sparePart->purchase_price_txt);
- }
- public function test_physical_stock_without_docs_calculates_correctly(): void
- {
- $sparePart = SparePart::factory()->create();
- // Create orders WITHOUT documents
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(5)
- ->forSparePart($sparePart)
- ->create();
- // Create order WITH documents (should not count)
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(true)
- ->withQuantity(20)
- ->forSparePart($sparePart)
- ->create();
- // Create shipped order (should not count)
- SparePartOrder::factory()
- ->shipped()
- ->withDocuments(false)
- ->forSparePart($sparePart)
- ->create();
- $this->assertEquals(15, $sparePart->physical_stock_without_docs);
- }
- public function test_physical_stock_with_docs_calculates_correctly(): void
- {
- $sparePart = SparePart::factory()->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(true)
- ->withQuantity(7)
- ->forSparePart($sparePart)
- ->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- $this->assertEquals(7, $sparePart->physical_stock_with_docs);
- }
- public function test_reserved_without_docs_calculates_correctly(): void
- {
- $sparePart = SparePart::factory()->create();
- $order = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->forSparePart($sparePart)
- ->create();
- // Active reservations WITHOUT documents
- Reservation::factory()
- ->active()
- ->withQuantity(3)
- ->withDocuments(false)
- ->fromOrder($order)
- ->forSparePart($sparePart)
- ->create();
- Reservation::factory()
- ->active()
- ->withQuantity(2)
- ->withDocuments(false)
- ->fromOrder($order)
- ->forSparePart($sparePart)
- ->create();
- // Cancelled reservation (should not count)
- Reservation::factory()
- ->cancelled()
- ->withQuantity(10)
- ->withDocuments(false)
- ->fromOrder($order)
- ->forSparePart($sparePart)
- ->create();
- // Reservation WITH documents (should not count)
- // Need to create separate order with documents for this
- $orderWithDocs = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(true)
- ->forSparePart($sparePart)
- ->create();
- Reservation::factory()
- ->active()
- ->withQuantity(5)
- ->fromOrder($orderWithDocs)
- ->forSparePart($sparePart)
- ->create();
- $this->assertEquals(5, $sparePart->reserved_without_docs);
- }
- public function test_free_stock_without_docs_calculates_correctly(): void
- {
- $sparePart = SparePart::factory()->create();
- $order = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- Reservation::factory()
- ->active()
- ->withQuantity(3)
- ->withDocuments(false)
- ->fromOrder($order)
- ->forSparePart($sparePart)
- ->create();
- // Physical: 10, Reserved: 3, Free: 7
- $this->assertEquals(7, $sparePart->free_stock_without_docs);
- }
- public function test_total_physical_stock_sums_both_types(): void
- {
- $sparePart = SparePart::factory()->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(true)
- ->withQuantity(5)
- ->forSparePart($sparePart)
- ->create();
- $this->assertEquals(15, $sparePart->total_physical_stock);
- }
- public function test_total_free_stock_calculates_correctly(): void
- {
- $sparePart = SparePart::factory()->create();
- $orderNoDocs = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- $orderWithDocs = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(true)
- ->withQuantity(8)
- ->forSparePart($sparePart)
- ->create();
- Reservation::factory()
- ->active()
- ->withQuantity(3)
- ->withDocuments(false)
- ->fromOrder($orderNoDocs)
- ->forSparePart($sparePart)
- ->create();
- Reservation::factory()
- ->active()
- ->withQuantity(2)
- ->withDocuments(true)
- ->fromOrder($orderWithDocs)
- ->forSparePart($sparePart)
- ->create();
- // Total physical: 18, Total reserved: 5, Total free: 13
- $this->assertEquals(13, $sparePart->total_free_stock);
- }
- public function test_quantity_backward_compatibility_attributes(): void
- {
- $sparePart = SparePart::factory()->create();
- $order = SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- Reservation::factory()
- ->active()
- ->withQuantity(3)
- ->withDocuments(false)
- ->fromOrder($order)
- ->forSparePart($sparePart)
- ->create();
- // Old attributes should return free stock
- $this->assertEquals($sparePart->free_stock_without_docs, $sparePart->quantity_without_docs);
- $this->assertEquals($sparePart->free_stock_with_docs, $sparePart->quantity_with_docs);
- $this->assertEquals($sparePart->total_free_stock, $sparePart->total_quantity);
- }
- public function test_has_open_shortages_returns_true_when_shortages_exist(): void
- {
- $sparePart = SparePart::factory()->create();
- Shortage::factory()
- ->open()
- ->forSparePart($sparePart)
- ->create();
- $this->assertTrue($sparePart->hasOpenShortages());
- }
- public function test_has_open_shortages_returns_false_when_no_shortages(): void
- {
- $sparePart = SparePart::factory()->create();
- $this->assertFalse($sparePart->hasOpenShortages());
- }
- public function test_has_open_shortages_ignores_closed_shortages(): void
- {
- $sparePart = SparePart::factory()->create();
- Shortage::factory()
- ->closed()
- ->forSparePart($sparePart)
- ->create();
- $this->assertFalse($sparePart->hasOpenShortages());
- }
- public function test_open_shortages_qty_calculates_correctly(): void
- {
- $sparePart = SparePart::factory()->create();
- Shortage::factory()
- ->open()
- ->withQuantities(10, 3) // missing 7
- ->forSparePart($sparePart)
- ->create();
- Shortage::factory()
- ->open()
- ->withQuantities(5, 2) // missing 3
- ->forSparePart($sparePart)
- ->create();
- Shortage::factory()
- ->withQuantities(20, 20) // fully reserved = closed
- ->forSparePart($sparePart)
- ->create();
- $this->assertEquals(10, $sparePart->open_shortages_qty);
- }
- public function test_is_below_min_stock_returns_true_when_below(): void
- {
- $sparePart = SparePart::factory()
- ->withMinStock(10)
- ->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(5)
- ->forSparePart($sparePart)
- ->create();
- $this->assertTrue($sparePart->isBelowMinStock());
- }
- public function test_is_below_min_stock_returns_false_when_at_or_above(): void
- {
- $sparePart = SparePart::factory()
- ->withMinStock(10)
- ->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(15)
- ->forSparePart($sparePart)
- ->create();
- $this->assertFalse($sparePart->isBelowMinStock());
- }
- public function test_get_free_stock_helper_method(): void
- {
- $sparePart = SparePart::factory()->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(false)
- ->withQuantity(10)
- ->forSparePart($sparePart)
- ->create();
- SparePartOrder::factory()
- ->inStock()
- ->withDocuments(true)
- ->withQuantity(5)
- ->forSparePart($sparePart)
- ->create();
- $this->assertEquals(10, $sparePart->getFreeStock(false));
- $this->assertEquals(5, $sparePart->getFreeStock(true));
- }
- public function test_pricing_codes_list_attribute(): void
- {
- $sparePart = SparePart::factory()->create();
- // Assuming pricing codes relationship works
- $this->assertIsString($sparePart->pricing_codes_list);
- }
- public function test_relations_exist(): void
- {
- $sparePart = SparePart::factory()->create();
- $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->orders());
- $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->reservations());
- $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->shortages());
- $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->movements());
- }
- }
|