select([ 'MAT-A' => [ $this->row(1, 10000, '2026-10-01 08:00:00', 2), $this->row(2, 10000, '2026-10-02 08:00:00', 3), ], 'MAT-B' => [ $this->row(3, 20000, '2026-10-01 09:00:00', 4), $this->row(4, 10000, '2026-10-03 09:00:00', 5), ], ], 30000); $this->assertTrue($result['exact']); $this->assertSame(30000, $result['selected_cents']); $this->assertSame(0, $result['difference_cents']); $this->assertSame([1, 3], $result['selected_ids']); $this->assertSame(2, $result['selected_materials']); } public function test_same_material_rows_are_selected_from_earliest_shipping_date(): void { $result = (new KukaPostingAmountSelection())->select([ 'MAT-A' => [ $this->row(1, 10000, '2026-10-02 08:00:00', 2), $this->row(2, 20000, '2026-10-01 08:00:00', 3), ], 'MAT-B' => [ $this->row(3, 10000, '2026-10-01 09:00:00', 4), ], ], 30000); $this->assertTrue($result['exact']); $this->assertSame([2, 3], $result['selected_ids']); } public function test_auto_calibration_adds_a_later_row_to_reach_the_exact_month_total(): void { $result = (new KukaPostingAmountSelection())->select([ 'MAT-A' => [ $this->row(1, 10000, '2026-10-01 08:00:00', 2), $this->row(2, 10000, '2026-10-02 08:00:00', 3), ], 'MAT-B' => [ $this->row(3, 10000, '2026-10-01 09:00:00', 4), $this->row(4, 10000, '2026-10-02 09:00:00', 5), ], ], 30000); $this->assertTrue($result['exact']); $this->assertSame(3, $result['selected_rows']); $this->assertSame(30000, $result['selected_cents']); } public function test_inexact_plan_is_returned_with_a_visible_difference(): void { $result = (new KukaPostingAmountSelection())->select([ 'MAT-A' => [ $this->row(1, 10000, '2026-10-01 08:00:00', 2), $this->row(2, 10000, '2026-10-02 08:00:00', 3), ], 'MAT-B' => [ $this->row(3, 10000, '2026-10-01 09:00:00', 4), $this->row(4, 10000, '2026-10-02 09:00:00', 5), ], ], 35000); $this->assertFalse($result['exact']); $this->assertSame(40000, $result['selected_cents']); $this->assertSame(-5000, $result['difference_cents']); } public function test_month_total_below_one_row_per_material_is_rejected(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('每个料号至少选择一行'); (new KukaPostingAmountSelection())->select([ 'MAT-A' => [$this->row(1, 10000, '2026-10-01 08:00:00', 2)], 'MAT-B' => [$this->row(2, 20000, '2026-10-01 09:00:00', 3)], ], 25000); } /** * @return array{id:int,amount_cents:int,date:string,source_row_number:int} */ private function row(int $id, int $amountCents, string $date, int $sourceRowNumber): array { return [ 'id' => $id, 'amount_cents' => $amountCents, 'date' => $date, 'source_row_number' => $sourceRowNumber, ]; } }