SparePartTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. namespace Tests\Unit\Models;
  3. use App\Models\Reservation;
  4. use App\Models\Shortage;
  5. use App\Models\SparePart;
  6. use App\Models\SparePartOrder;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. use Tests\TestCase;
  9. class SparePartTest extends TestCase
  10. {
  11. use RefreshDatabase;
  12. protected $seed = true;
  13. public function test_price_accessors_convert_from_kopeks_to_rubles(): void
  14. {
  15. $sparePart = SparePart::factory()->create([
  16. 'purchase_price' => 150.50, // Will be stored as 15050 kopeks
  17. 'customer_price' => 200.00,
  18. 'expertise_price' => 180.25,
  19. ]);
  20. $sparePart->refresh();
  21. $this->assertEquals(150.50, $sparePart->purchase_price);
  22. $this->assertEquals(200.00, $sparePart->customer_price);
  23. $this->assertEquals(180.25, $sparePart->expertise_price);
  24. }
  25. public function test_price_txt_accessors_format_prices(): void
  26. {
  27. $sparePart = SparePart::factory()->create([
  28. 'purchase_price' => 1500.50,
  29. ]);
  30. $sparePart->refresh();
  31. $this->assertStringContainsString('₽', $sparePart->purchase_price_txt);
  32. $this->assertStringContainsString('1', $sparePart->purchase_price_txt);
  33. $this->assertStringContainsString('500', $sparePart->purchase_price_txt);
  34. }
  35. public function test_price_txt_returns_dash_for_null_prices(): void
  36. {
  37. $sparePart = SparePart::factory()->create([
  38. 'purchase_price' => null,
  39. ]);
  40. $sparePart->refresh();
  41. $this->assertEquals('-', $sparePart->purchase_price_txt);
  42. }
  43. public function test_physical_stock_without_docs_calculates_correctly(): void
  44. {
  45. $sparePart = SparePart::factory()->create();
  46. // Create orders WITHOUT documents
  47. SparePartOrder::factory()
  48. ->inStock()
  49. ->withDocuments(false)
  50. ->withQuantity(10)
  51. ->forSparePart($sparePart)
  52. ->create();
  53. SparePartOrder::factory()
  54. ->inStock()
  55. ->withDocuments(false)
  56. ->withQuantity(5)
  57. ->forSparePart($sparePart)
  58. ->create();
  59. // Create order WITH documents (should not count)
  60. SparePartOrder::factory()
  61. ->inStock()
  62. ->withDocuments(true)
  63. ->withQuantity(20)
  64. ->forSparePart($sparePart)
  65. ->create();
  66. // Create shipped order (should not count)
  67. SparePartOrder::factory()
  68. ->shipped()
  69. ->withDocuments(false)
  70. ->forSparePart($sparePart)
  71. ->create();
  72. $this->assertEquals(15, $sparePart->physical_stock_without_docs);
  73. }
  74. public function test_physical_stock_with_docs_calculates_correctly(): void
  75. {
  76. $sparePart = SparePart::factory()->create();
  77. SparePartOrder::factory()
  78. ->inStock()
  79. ->withDocuments(true)
  80. ->withQuantity(7)
  81. ->forSparePart($sparePart)
  82. ->create();
  83. SparePartOrder::factory()
  84. ->inStock()
  85. ->withDocuments(false)
  86. ->withQuantity(10)
  87. ->forSparePart($sparePart)
  88. ->create();
  89. $this->assertEquals(7, $sparePart->physical_stock_with_docs);
  90. }
  91. public function test_reserved_without_docs_calculates_correctly(): void
  92. {
  93. $sparePart = SparePart::factory()->create();
  94. $order = SparePartOrder::factory()
  95. ->inStock()
  96. ->withDocuments(false)
  97. ->forSparePart($sparePart)
  98. ->create();
  99. // Active reservations WITHOUT documents
  100. Reservation::factory()
  101. ->active()
  102. ->withQuantity(3)
  103. ->withDocuments(false)
  104. ->fromOrder($order)
  105. ->forSparePart($sparePart)
  106. ->create();
  107. Reservation::factory()
  108. ->active()
  109. ->withQuantity(2)
  110. ->withDocuments(false)
  111. ->fromOrder($order)
  112. ->forSparePart($sparePart)
  113. ->create();
  114. // Cancelled reservation (should not count)
  115. Reservation::factory()
  116. ->cancelled()
  117. ->withQuantity(10)
  118. ->withDocuments(false)
  119. ->fromOrder($order)
  120. ->forSparePart($sparePart)
  121. ->create();
  122. // Reservation WITH documents (should not count)
  123. // Need to create separate order with documents for this
  124. $orderWithDocs = SparePartOrder::factory()
  125. ->inStock()
  126. ->withDocuments(true)
  127. ->forSparePart($sparePart)
  128. ->create();
  129. Reservation::factory()
  130. ->active()
  131. ->withQuantity(5)
  132. ->fromOrder($orderWithDocs)
  133. ->forSparePart($sparePart)
  134. ->create();
  135. $this->assertEquals(5, $sparePart->reserved_without_docs);
  136. }
  137. public function test_free_stock_without_docs_calculates_correctly(): void
  138. {
  139. $sparePart = SparePart::factory()->create();
  140. $order = SparePartOrder::factory()
  141. ->inStock()
  142. ->withDocuments(false)
  143. ->withQuantity(10)
  144. ->forSparePart($sparePart)
  145. ->create();
  146. Reservation::factory()
  147. ->active()
  148. ->withQuantity(3)
  149. ->withDocuments(false)
  150. ->fromOrder($order)
  151. ->forSparePart($sparePart)
  152. ->create();
  153. // Physical: 10, Reserved: 3, Free: 7
  154. $this->assertEquals(7, $sparePart->free_stock_without_docs);
  155. }
  156. public function test_total_physical_stock_sums_both_types(): void
  157. {
  158. $sparePart = SparePart::factory()->create();
  159. SparePartOrder::factory()
  160. ->inStock()
  161. ->withDocuments(false)
  162. ->withQuantity(10)
  163. ->forSparePart($sparePart)
  164. ->create();
  165. SparePartOrder::factory()
  166. ->inStock()
  167. ->withDocuments(true)
  168. ->withQuantity(5)
  169. ->forSparePart($sparePart)
  170. ->create();
  171. $this->assertEquals(15, $sparePart->total_physical_stock);
  172. }
  173. public function test_total_free_stock_calculates_correctly(): void
  174. {
  175. $sparePart = SparePart::factory()->create();
  176. $orderNoDocs = SparePartOrder::factory()
  177. ->inStock()
  178. ->withDocuments(false)
  179. ->withQuantity(10)
  180. ->forSparePart($sparePart)
  181. ->create();
  182. $orderWithDocs = SparePartOrder::factory()
  183. ->inStock()
  184. ->withDocuments(true)
  185. ->withQuantity(8)
  186. ->forSparePart($sparePart)
  187. ->create();
  188. Reservation::factory()
  189. ->active()
  190. ->withQuantity(3)
  191. ->withDocuments(false)
  192. ->fromOrder($orderNoDocs)
  193. ->forSparePart($sparePart)
  194. ->create();
  195. Reservation::factory()
  196. ->active()
  197. ->withQuantity(2)
  198. ->withDocuments(true)
  199. ->fromOrder($orderWithDocs)
  200. ->forSparePart($sparePart)
  201. ->create();
  202. // Total physical: 18, Total reserved: 5, Total free: 13
  203. $this->assertEquals(13, $sparePart->total_free_stock);
  204. }
  205. public function test_quantity_backward_compatibility_attributes(): void
  206. {
  207. $sparePart = SparePart::factory()->create();
  208. $order = SparePartOrder::factory()
  209. ->inStock()
  210. ->withDocuments(false)
  211. ->withQuantity(10)
  212. ->forSparePart($sparePart)
  213. ->create();
  214. Reservation::factory()
  215. ->active()
  216. ->withQuantity(3)
  217. ->withDocuments(false)
  218. ->fromOrder($order)
  219. ->forSparePart($sparePart)
  220. ->create();
  221. // Old attributes should return free stock
  222. $this->assertEquals($sparePart->free_stock_without_docs, $sparePart->quantity_without_docs);
  223. $this->assertEquals($sparePart->free_stock_with_docs, $sparePart->quantity_with_docs);
  224. $this->assertEquals($sparePart->total_free_stock, $sparePart->total_quantity);
  225. }
  226. public function test_has_open_shortages_returns_true_when_shortages_exist(): void
  227. {
  228. $sparePart = SparePart::factory()->create();
  229. Shortage::factory()
  230. ->open()
  231. ->forSparePart($sparePart)
  232. ->create();
  233. $this->assertTrue($sparePart->hasOpenShortages());
  234. }
  235. public function test_has_open_shortages_returns_false_when_no_shortages(): void
  236. {
  237. $sparePart = SparePart::factory()->create();
  238. $this->assertFalse($sparePart->hasOpenShortages());
  239. }
  240. public function test_has_open_shortages_ignores_closed_shortages(): void
  241. {
  242. $sparePart = SparePart::factory()->create();
  243. Shortage::factory()
  244. ->closed()
  245. ->forSparePart($sparePart)
  246. ->create();
  247. $this->assertFalse($sparePart->hasOpenShortages());
  248. }
  249. public function test_open_shortages_qty_calculates_correctly(): void
  250. {
  251. $sparePart = SparePart::factory()->create();
  252. Shortage::factory()
  253. ->open()
  254. ->withQuantities(10, 3) // missing 7
  255. ->forSparePart($sparePart)
  256. ->create();
  257. Shortage::factory()
  258. ->open()
  259. ->withQuantities(5, 2) // missing 3
  260. ->forSparePart($sparePart)
  261. ->create();
  262. Shortage::factory()
  263. ->withQuantities(20, 20) // fully reserved = closed
  264. ->forSparePart($sparePart)
  265. ->create();
  266. $this->assertEquals(10, $sparePart->open_shortages_qty);
  267. }
  268. public function test_is_below_min_stock_returns_true_when_below(): void
  269. {
  270. $sparePart = SparePart::factory()
  271. ->withMinStock(10)
  272. ->create();
  273. SparePartOrder::factory()
  274. ->inStock()
  275. ->withDocuments(false)
  276. ->withQuantity(5)
  277. ->forSparePart($sparePart)
  278. ->create();
  279. $this->assertTrue($sparePart->isBelowMinStock());
  280. }
  281. public function test_is_below_min_stock_returns_false_when_at_or_above(): void
  282. {
  283. $sparePart = SparePart::factory()
  284. ->withMinStock(10)
  285. ->create();
  286. SparePartOrder::factory()
  287. ->inStock()
  288. ->withDocuments(false)
  289. ->withQuantity(15)
  290. ->forSparePart($sparePart)
  291. ->create();
  292. $this->assertFalse($sparePart->isBelowMinStock());
  293. }
  294. public function test_get_free_stock_helper_method(): void
  295. {
  296. $sparePart = SparePart::factory()->create();
  297. SparePartOrder::factory()
  298. ->inStock()
  299. ->withDocuments(false)
  300. ->withQuantity(10)
  301. ->forSparePart($sparePart)
  302. ->create();
  303. SparePartOrder::factory()
  304. ->inStock()
  305. ->withDocuments(true)
  306. ->withQuantity(5)
  307. ->forSparePart($sparePart)
  308. ->create();
  309. $this->assertEquals(10, $sparePart->getFreeStock(false));
  310. $this->assertEquals(5, $sparePart->getFreeStock(true));
  311. }
  312. public function test_pricing_codes_list_attribute(): void
  313. {
  314. $sparePart = SparePart::factory()->create();
  315. // Assuming pricing codes relationship works
  316. $this->assertIsString($sparePart->pricing_codes_list);
  317. }
  318. public function test_relations_exist(): void
  319. {
  320. $sparePart = SparePart::factory()->create();
  321. $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->orders());
  322. $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->reservations());
  323. $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->shortages());
  324. $this->assertInstanceOf(\Illuminate\Database\Eloquent\Relations\HasMany::class, $sparePart->movements());
  325. }
  326. }