ProductionOrderPlanningControllerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. declare(strict_types=1);
  3. namespace Tests\Feature;
  4. use App\Enums\ProductionOrderActivityType;
  5. use App\Enums\ProductionOrderStatus;
  6. use App\Models\ProductionOrder;
  7. use App\Models\ProductionOrderDelivery;
  8. use App\Models\ProductionOrderInstallation;
  9. use App\Models\Role;
  10. use App\Models\User;
  11. use Illuminate\Foundation\Testing\RefreshDatabase;
  12. use Tests\TestCase;
  13. class ProductionOrderPlanningControllerTest extends TestCase
  14. {
  15. use RefreshDatabase;
  16. protected bool $seed = true;
  17. private User $admin;
  18. private User $assistant;
  19. private User $manager;
  20. private User $driver;
  21. private User $brigadier;
  22. private ProductionOrder $order;
  23. protected function setUp(): void
  24. {
  25. parent::setUp();
  26. $this->admin = User::factory()->admin()->create();
  27. $this->assistant = User::factory()->assistantHead()->create();
  28. $this->manager = User::factory()->manager()->create();
  29. $this->driver = User::factory()->driver()->create();
  30. $this->brigadier = User::factory()->brigadier()->create();
  31. $this->order = ProductionOrder::factory()->create(['manager_id' => $this->manager->id]);
  32. }
  33. public function test_admin_can_add_multiple_deliveries_and_installations_with_audit(): void
  34. {
  35. $this->actingAs($this->admin)
  36. ->post(route('schedule.orders.deliveries.store', $this->order), $this->deliveryData([
  37. 'delivery_date' => '2026-09-10',
  38. ]))
  39. ->assertRedirect();
  40. $this->actingAs($this->admin)
  41. ->post(route('schedule.orders.deliveries.store', $this->order), $this->deliveryData([
  42. 'delivery_date' => '2026-09-12',
  43. 'driver_id' => $this->brigadier->id,
  44. ]))
  45. ->assertRedirect();
  46. $this->actingAs($this->admin)
  47. ->post(route('schedule.orders.installations.store', $this->order), $this->installationData())
  48. ->assertRedirect();
  49. $this->assertSame(2, $this->order->deliveries()->count());
  50. $this->assertSame(1, $this->order->installations()->count());
  51. $installation = $this->order->installations()->firstOrFail();
  52. $this->assertSame(3, $installation->schedules()->count());
  53. $this->assertSame(
  54. ['2026-09-20', '2026-09-21', '2026-09-22'],
  55. $installation->schedules()->orderBy('installation_date')->pluck('installation_date')->all(),
  56. );
  57. $this->assertDatabaseHas('production_order_activities', [
  58. 'production_order_id' => $this->order->id,
  59. 'type' => ProductionOrderActivityType::DeliveryScheduled->value,
  60. 'user_id' => $this->admin->id,
  61. 'message' => 'Назначена доставка на 10.09.2026, водитель '.$this->driver->name.'.',
  62. ]);
  63. $this->assertDatabaseHas('production_order_activities', [
  64. 'production_order_id' => $this->order->id,
  65. 'type' => ProductionOrderActivityType::InstallationScheduled->value,
  66. 'user_id' => $this->admin->id,
  67. 'message' => 'Назначен монтаж на 20.09.2026, бригадир '.$this->brigadier->name.'.',
  68. ]);
  69. $this->assertSame(3, $this->order->activities()->count());
  70. }
  71. public function test_assistant_can_update_and_delete_planning_records_without_extra_audit(): void
  72. {
  73. $delivery = ProductionOrderDelivery::factory()->create([
  74. 'production_order_id' => $this->order->id,
  75. 'driver_id' => $this->driver->id,
  76. 'delivery_date' => '2026-09-10',
  77. ]);
  78. $installation = ProductionOrderInstallation::factory()->create([
  79. 'production_order_id' => $this->order->id,
  80. 'brigadier_id' => $this->brigadier->id,
  81. ]);
  82. $this->actingAs($this->assistant)
  83. ->put(
  84. route('schedule.orders.deliveries.update', [$this->order, $delivery]),
  85. $this->deliveryData(['delivery_date' => '2026-09-15']),
  86. )
  87. ->assertRedirect();
  88. $this->actingAs($this->assistant)
  89. ->put(
  90. route('schedule.orders.installations.update', [$this->order, $installation]),
  91. $this->installationData(['installation_days' => 5]),
  92. )
  93. ->assertRedirect();
  94. $this->assertDatabaseHas('production_order_deliveries', [
  95. 'id' => $delivery->id,
  96. 'delivery_date' => '2026-09-15',
  97. 'updated_by' => $this->assistant->id,
  98. ]);
  99. $this->assertDatabaseHas('production_order_installations', [
  100. 'id' => $installation->id,
  101. 'installation_days' => 5,
  102. 'updated_by' => $this->assistant->id,
  103. ]);
  104. $this->assertSame(5, $installation->schedules()->count());
  105. $this->assertDatabaseHas('schedules', [
  106. 'production_order_installation_id' => $installation->id,
  107. 'installation_date' => '2026-09-24',
  108. 'source' => \App\Models\Schedule::SOURCE_PRODUCTION_ORDER,
  109. 'brigadier_id' => $this->brigadier->id,
  110. ]);
  111. $this->assertSame(0, $this->order->activities()->count());
  112. $this->actingAs($this->assistant)
  113. ->delete(route('schedule.orders.deliveries.destroy', [$this->order, $delivery]))
  114. ->assertRedirect();
  115. $this->actingAs($this->assistant)
  116. ->delete(route('schedule.orders.installations.destroy', [$this->order, $installation]))
  117. ->assertRedirect();
  118. $this->assertSoftDeleted('production_order_deliveries', ['id' => $delivery->id]);
  119. $this->assertSoftDeleted('production_order_installations', ['id' => $installation->id]);
  120. $this->assertDatabaseMissing('schedules', ['production_order_installation_id' => $installation->id]);
  121. }
  122. public function test_manager_cannot_manage_delivery_or_installation(): void
  123. {
  124. $this->actingAs($this->manager)
  125. ->post(route('schedule.orders.deliveries.store', $this->order), $this->deliveryData())
  126. ->assertForbidden();
  127. $this->actingAs($this->manager)
  128. ->post(route('schedule.orders.installations.store', $this->order), $this->installationData())
  129. ->assertForbidden();
  130. }
  131. public function test_delivery_and_installation_require_users_with_allowed_roles(): void
  132. {
  133. $wrongRole = User::factory()->create(['role' => Role::MANAGER]);
  134. $this->actingAs($this->admin)
  135. ->post(route('schedule.orders.deliveries.store', $this->order), $this->deliveryData([
  136. 'driver_id' => $wrongRole->id,
  137. ]))
  138. ->assertSessionHasErrors('driver_id');
  139. $this->actingAs($this->admin)
  140. ->post(route('schedule.orders.installations.store', $this->order), $this->installationData([
  141. 'brigadier_id' => $this->driver->id,
  142. ]))
  143. ->assertSessionHasErrors('brigadier_id');
  144. }
  145. public function test_planning_record_from_another_order_returns_not_found(): void
  146. {
  147. $otherOrder = ProductionOrder::factory()->create(['manager_id' => $this->manager->id]);
  148. $delivery = ProductionOrderDelivery::factory()->create([
  149. 'production_order_id' => $otherOrder->id,
  150. 'driver_id' => $this->driver->id,
  151. ]);
  152. $this->actingAs($this->admin)
  153. ->put(
  154. route('schedule.orders.deliveries.update', [$this->order, $delivery]),
  155. $this->deliveryData(),
  156. )
  157. ->assertNotFound();
  158. }
  159. public function test_order_card_shows_planning_and_audit_to_read_only_role(): void
  160. {
  161. $delivery = ProductionOrderDelivery::factory()->create([
  162. 'production_order_id' => $this->order->id,
  163. 'driver_id' => $this->driver->id,
  164. 'delivery_date' => '2026-09-18',
  165. ]);
  166. $this->order->activities()->create([
  167. 'type' => ProductionOrderActivityType::DeliveryScheduled,
  168. 'user_id' => $this->admin->id,
  169. 'delivery_id' => $delivery->id,
  170. 'message' => 'Заказ добавлен в график доставок.',
  171. ]);
  172. $this->actingAs($this->manager)
  173. ->get(route('schedule.orders.show', $this->order))
  174. ->assertOk()
  175. ->assertSee('18.09.2026')
  176. ->assertSee($this->driver->name)
  177. ->assertSee('Заказ добавлен в график доставок.')
  178. ->assertDontSee('Добавить доставку');
  179. }
  180. public function test_status_change_is_the_only_order_field_update_written_to_audit(): void
  181. {
  182. $this->order->update(['note' => 'Обычное изменение']);
  183. $this->assertSame(0, $this->order->activities()->count());
  184. $service = app(\App\Services\ProductionOrderService::class);
  185. $service->update(
  186. $this->order,
  187. ['status' => ProductionOrderStatus::InTransit->value],
  188. $this->admin,
  189. false,
  190. );
  191. $this->assertDatabaseHas('production_order_activities', [
  192. 'production_order_id' => $this->order->id,
  193. 'type' => ProductionOrderActivityType::StatusChanged->value,
  194. 'message' => 'Статус изменён на «В пути».',
  195. ]);
  196. $this->assertSame(1, $this->order->activities()->count());
  197. }
  198. public function test_weekly_delivery_schedule_contains_only_selected_week(): void
  199. {
  200. ProductionOrderDelivery::factory()->create([
  201. 'production_order_id' => $this->order->id,
  202. 'driver_id' => $this->driver->id,
  203. 'delivery_date' => '2026-09-10',
  204. 'delivery_time' => '10:30',
  205. 'carrier' => 'Нужный перевозчик',
  206. ]);
  207. ProductionOrderDelivery::factory()->create([
  208. 'production_order_id' => $this->order->id,
  209. 'driver_id' => $this->driver->id,
  210. 'delivery_date' => '2026-09-20',
  211. 'carrier' => 'Перевозчик другой недели',
  212. ]);
  213. $this->actingAs($this->manager)
  214. ->withSession(['year' => 2020])
  215. ->get(route('schedule.deliveries', ['week' => '2026-09-07']))
  216. ->assertOk()
  217. ->assertViewIs('production_order_deliveries.index')
  218. ->assertSee('Нужный перевозчик')
  219. ->assertSee($this->order->order_number)
  220. ->assertSee($this->driver->name)
  221. ->assertDontSee('Перевозчик другой недели');
  222. $this->actingAs($this->driver)
  223. ->get(route('schedule.deliveries', ['week' => '2026-09-07']))
  224. ->assertOk();
  225. }
  226. public function test_installation_is_visible_in_existing_schedule_and_managed_from_order_card(): void
  227. {
  228. $this->actingAs($this->admin)
  229. ->post(route('schedule.orders.installations.store', $this->order), $this->installationData([
  230. 'installation_date' => '2026-09-15',
  231. 'installation_days' => 2,
  232. ]))
  233. ->assertRedirect();
  234. $installation = $this->order->installations()->firstOrFail();
  235. $schedule = $installation->schedules()->firstOrFail();
  236. $this->actingAs($this->admin)
  237. ->get(route('schedule.index', ['tab' => 'week', 'year' => 2026, 'week' => 38]))
  238. ->assertOk()
  239. ->assertSee($this->order->order_number)
  240. ->assertSee($this->order->object_address)
  241. ->assertSee('Размещен')
  242. ->assertSee(route('schedule.orders.show', $this->order), false);
  243. $this->actingAs($this->admin)
  244. ->delete(route('schedule.delete', $schedule))
  245. ->assertForbidden();
  246. $this->assertDatabaseHas('schedules', ['id' => $schedule->id]);
  247. }
  248. /** @return array<string, mixed> */
  249. private function deliveryData(array $overrides = []): array
  250. {
  251. return array_replace([
  252. 'delivery_date' => '2026-09-10',
  253. 'delivery_time' => '10:30',
  254. 'driver_id' => $this->driver->id,
  255. 'carrier' => 'ООО Перевозчик',
  256. 'request_number' => 'ЗД-100',
  257. 'request_date' => '2026-09-08',
  258. 'carrier_note' => 'Позвонить заранее',
  259. 'contact_position' => 'Прораб',
  260. 'contact_name' => 'Иван Иванов',
  261. 'contact_phone' => '+7 900 000-00-00',
  262. 'desired_time' => '11:00',
  263. 'access_system' => 'По пропуску',
  264. 'unloading_place' => 'Вторые ворота',
  265. 'advance_notice' => 'За один час',
  266. 'document_signing_method' => 'Оригинал на объекте',
  267. 'has_passports_and_certificates' => true,
  268. 'note' => 'Без опозданий',
  269. ], $overrides);
  270. }
  271. /** @return array<string, mixed> */
  272. private function installationData(array $overrides = []): array
  273. {
  274. return array_replace([
  275. 'installation_date' => '2026-09-20',
  276. 'installation_days' => 3,
  277. 'brigadier_id' => $this->brigadier->id,
  278. 'note' => 'Первый этап',
  279. ], $overrides);
  280. }
  281. }