diff --git a/app/src/Model/Venta/Reservation.php b/app/src/Model/Venta/Reservation.php index 3b1578b..cc777eb 100644 --- a/app/src/Model/Venta/Reservation.php +++ b/app/src/Model/Venta/Reservation.php @@ -4,6 +4,7 @@ namespace Incoviba\Model\Venta; use DateTimeInterface; use Incoviba\Common; use Incoviba\Model; +use InvalidArgumentException; class Reservation extends Common\Ideal\Model { @@ -49,7 +50,7 @@ class Reservation extends Common\Ideal\Model public function states(): array { - if (!isset($this->states)) { + if (count($this->states) === 0) { $this->states = $this->runFactory('states'); } return $this->states; @@ -60,7 +61,11 @@ class Reservation extends Common\Ideal\Model public function currentState(): Model\Venta\Reservation\State { if (!isset($this->currentState)) { - $this->currentState = last($this->states()); + $states = $this->states(); + if (count($states) === 0) { + throw new InvalidArgumentException('States must not be empty'); + } + $this->currentState = last($states); } return $this->currentState; } diff --git a/app/src/Repository/Venta/Reservation.php b/app/src/Repository/Venta/Reservation.php index 5aad21f..bb6cdd9 100644 --- a/app/src/Repository/Venta/Reservation.php +++ b/app/src/Repository/Venta/Reservation.php @@ -234,7 +234,7 @@ class Reservation extends Common\Ideal\Repository } try { $queryCheck = $this->connection->getQueryBuilder() - ->select('id') + ->select('reference_id') ->from('reservation_details') ->where('reservation_id = :id AND type = :type'); $statementCheck = $this->connection->execute($queryCheck, [ @@ -242,9 +242,12 @@ class Reservation extends Common\Ideal\Repository 'type' => Model\Venta\Reservation\Detail\Type::Broker->value ]); $result = $statementCheck->fetch(PDO::FETCH_ASSOC); - $new_id = $reservation->broker->id; - $reservation->broker = $this->brokerRepository->fetchById($result['id']); - $this->editBroker($reservation, ['broker_id' => $new_id]); + if ($result === false) { + throw new PDOException(); + } + $new_id = $reservation->broker->rut; + $reservation->broker = $this->brokerRepository->fetchById($result['reference_id']); + $this->editBroker($reservation, ['broker_rut' => $new_id]); } catch (PDOException) { $queryInsert = $this->connection->getQueryBuilder() ->insert() @@ -254,7 +257,7 @@ class Reservation extends Common\Ideal\Repository $this->connection->execute($queryInsert, [ 'reservation_id' => $reservation->id, 'type' => Model\Venta\Reservation\Detail\Type::Broker->value, - 'reference_id' => $reservation->broker->id + 'reference_id' => $reservation->broker->rut ]); } } @@ -386,20 +389,20 @@ class Reservation extends Common\Ideal\Repository } protected function editBroker(Model\Venta\Reservation &$reservation, array $new_data): void { - if (!array_key_exists('broker_id', $new_data) or $new_data['broker_id'] === $reservation->broker->id) { + if (!array_key_exists('broker_rut', $new_data) or $new_data['broker_rut'] === $reservation->broker->rut) { return; } try { $query = $this->connection->getQueryBuilder() ->update('reservation_details') - ->set('reference_id = :broker_id') + ->set('reference_id = :broker_rut') ->where('reservation_id = :id AND type = :type'); $this->connection->execute($query, [ 'id' => $reservation->id, 'type' => Model\Venta\Reservation\Detail\Type::Broker->value, - 'broker_id' => $new_data['broker_id'] + 'broker_rut' => $new_data['broker_rut'] ]); - $reservation->broker = $this->brokerRepository->fetchById($new_data['broker_id']); + $reservation->broker = $this->brokerRepository->fetchById($new_data['broker_rut']); } catch (PDOException) {} } protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void diff --git a/app/src/Service/Venta/Reservation.php b/app/src/Service/Venta/Reservation.php index bfcf618..968f76d 100644 --- a/app/src/Service/Venta/Reservation.php +++ b/app/src/Service/Venta/Reservation.php @@ -140,17 +140,7 @@ class Reservation extends Ideal\Service\API $buyerData[substr($key, strlen('buyer_'))] = $value; } $this->personaService->add($buyerData); - if (array_key_exists('broker_rut', $data)) { - if (empty($data['broker_rut'])) { - unset($data['broker_rut']); - } else { - try { - $this->brokerService->get($data['broker_rut']); - } catch (ServiceAction\Read) { - unset($data['broker_rut']); - } - } - } + $data['date'] = $date->format('Y-m-d'); try { $reservationData = $this->reservationRepository->filterData($data); @@ -177,7 +167,11 @@ class Reservation extends Ideal\Service\API $units = array_combine($data['units'], $data['units_value']); $this->addUnits($reservation, $units); - if (isset($data['promotions'])) { + if (array_key_exists('broker_rut', $data) and !empty($data['broker_rut'])) { + $this->addBroker($reservation, $data['broker_rut']); + } + + if (array_key_exists('promotions', $data)) { $this->addPromotions($reservation, $data['promotions']); } @@ -224,6 +218,14 @@ class Reservation extends Ideal\Service\API } $this->reservationRepository->save($reservation); } + protected function addBroker(Model\Venta\Reservation $reservation, int $broker_rut): void + { + try { + $broker = $this->brokerService->get($broker_rut); + $reservation->broker = $broker; + $this->reservationRepository->save($reservation); + } catch (ServiceAction\Read) {} + } protected function addPromotions(Model\Venta\Reservation $reservation, array $promotions): void { foreach ($promotions as $promotion_id) { diff --git a/app/tests/integration/API/Ventas/ReservationTest.php b/app/tests/integration/API/Ventas/ReservationTest.php index 13da859..0ec2474 100644 --- a/app/tests/integration/API/Ventas/ReservationTest.php +++ b/app/tests/integration/API/Ventas/ReservationTest.php @@ -37,7 +37,7 @@ class ReservationTest extends TestCase $unitsValue[] = $faker->randomFloat(2, 1000, 10000); } - $broker = $faker->optional(.1)->randomElement($brokers); + $broker = $faker->randomElement($brokers); $activePromotions = $this->container->get(Repository\Venta\Promotion::class)->fetchActiveByProject($project->id); @@ -87,5 +87,6 @@ class ReservationTest extends TestCase if ($broker !== null) { $this->assertEquals($data['broker_rut'], $reservation->broker->rut); } + $this->assertEquals(1, $reservation->currentState()->type->value); } } diff --git a/app/tests/unit/src/Service/Venta/ReservationTest.php b/app/tests/unit/src/Service/Venta/ReservationTest.php index 3232ba1..ca31e38 100644 --- a/app/tests/unit/src/Service/Venta/ReservationTest.php +++ b/app/tests/unit/src/Service/Venta/ReservationTest.php @@ -35,7 +35,7 @@ class ReservationTest extends TestCase $reservation->project = $this->getMockBuilder(Model\Proyecto::class)->disableOriginalConstructor()->getMock(); $reservation->project->id = $data['project_id']; $reservation->date = new DateTimeImmutable($data['date']); - if (array_key_exists('broker_rut', $data)) { + if (array_key_exists('broker_rut', $data) and !empty($data['broker_rut'])) { $reservation->broker = $this->getMockBuilder(Model\Proyecto\Broker::class)->disableOriginalConstructor()->getMock(); $reservation->broker->rut = $data['broker_rut']; } @@ -47,6 +47,12 @@ class ReservationTest extends TestCase }); $this->stateRepository = $this->getMockBuilder(Repository\Venta\Reservation\State::class) ->disableOriginalConstructor()->getMock(); + $this->stateRepository->method('fetchByReservation')->willReturnCallback(function($reservation_id) { + $state = new Model\Venta\Reservation\State(); + $state->reservation = new Model\Venta\Reservation(); + $state->reservation->id = $reservation_id; + return $state; + }); $this->personaService = $this->getMockBuilder(Service\Persona::class) ->disableOriginalConstructor()->getMock(); $this->personaService->method('add')->willReturnCallback(function($data) { @@ -61,6 +67,11 @@ class ReservationTest extends TestCase }); $this->brokerService = $this->getMockBuilder(Service\Proyecto\Broker::class) ->disableOriginalConstructor()->getMock(); + $this->brokerService->method('get')->willReturnCallback(function($id) { + $broker = new Model\Proyecto\Broker(); + $broker->rut = $id; + return $broker; + }); $this->promotionService = $this->getMockBuilder(Service\Venta\Promotion::class) ->disableOriginalConstructor()->getMock(); $this->promotionService->method('getById')->willReturnCallback(function($id) {