Reservation fixes
This commit is contained in:
@ -4,6 +4,7 @@ namespace Incoviba\Model\Venta;
|
|||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use Incoviba\Common;
|
use Incoviba\Common;
|
||||||
use Incoviba\Model;
|
use Incoviba\Model;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
class Reservation extends Common\Ideal\Model
|
class Reservation extends Common\Ideal\Model
|
||||||
{
|
{
|
||||||
@ -49,7 +50,7 @@ class Reservation extends Common\Ideal\Model
|
|||||||
|
|
||||||
public function states(): array
|
public function states(): array
|
||||||
{
|
{
|
||||||
if (!isset($this->states)) {
|
if (count($this->states) === 0) {
|
||||||
$this->states = $this->runFactory('states');
|
$this->states = $this->runFactory('states');
|
||||||
}
|
}
|
||||||
return $this->states;
|
return $this->states;
|
||||||
@ -60,7 +61,11 @@ class Reservation extends Common\Ideal\Model
|
|||||||
public function currentState(): Model\Venta\Reservation\State
|
public function currentState(): Model\Venta\Reservation\State
|
||||||
{
|
{
|
||||||
if (!isset($this->currentState)) {
|
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;
|
return $this->currentState;
|
||||||
}
|
}
|
||||||
|
@ -234,7 +234,7 @@ class Reservation extends Common\Ideal\Repository
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$queryCheck = $this->connection->getQueryBuilder()
|
$queryCheck = $this->connection->getQueryBuilder()
|
||||||
->select('id')
|
->select('reference_id')
|
||||||
->from('reservation_details')
|
->from('reservation_details')
|
||||||
->where('reservation_id = :id AND type = :type');
|
->where('reservation_id = :id AND type = :type');
|
||||||
$statementCheck = $this->connection->execute($queryCheck, [
|
$statementCheck = $this->connection->execute($queryCheck, [
|
||||||
@ -242,9 +242,12 @@ class Reservation extends Common\Ideal\Repository
|
|||||||
'type' => Model\Venta\Reservation\Detail\Type::Broker->value
|
'type' => Model\Venta\Reservation\Detail\Type::Broker->value
|
||||||
]);
|
]);
|
||||||
$result = $statementCheck->fetch(PDO::FETCH_ASSOC);
|
$result = $statementCheck->fetch(PDO::FETCH_ASSOC);
|
||||||
$new_id = $reservation->broker->id;
|
if ($result === false) {
|
||||||
$reservation->broker = $this->brokerRepository->fetchById($result['id']);
|
throw new PDOException();
|
||||||
$this->editBroker($reservation, ['broker_id' => $new_id]);
|
}
|
||||||
|
$new_id = $reservation->broker->rut;
|
||||||
|
$reservation->broker = $this->brokerRepository->fetchById($result['reference_id']);
|
||||||
|
$this->editBroker($reservation, ['broker_rut' => $new_id]);
|
||||||
} catch (PDOException) {
|
} catch (PDOException) {
|
||||||
$queryInsert = $this->connection->getQueryBuilder()
|
$queryInsert = $this->connection->getQueryBuilder()
|
||||||
->insert()
|
->insert()
|
||||||
@ -254,7 +257,7 @@ class Reservation extends Common\Ideal\Repository
|
|||||||
$this->connection->execute($queryInsert, [
|
$this->connection->execute($queryInsert, [
|
||||||
'reservation_id' => $reservation->id,
|
'reservation_id' => $reservation->id,
|
||||||
'type' => Model\Venta\Reservation\Detail\Type::Broker->value,
|
'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
|
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;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$query = $this->connection->getQueryBuilder()
|
$query = $this->connection->getQueryBuilder()
|
||||||
->update('reservation_details')
|
->update('reservation_details')
|
||||||
->set('reference_id = :broker_id')
|
->set('reference_id = :broker_rut')
|
||||||
->where('reservation_id = :id AND type = :type');
|
->where('reservation_id = :id AND type = :type');
|
||||||
$this->connection->execute($query, [
|
$this->connection->execute($query, [
|
||||||
'id' => $reservation->id,
|
'id' => $reservation->id,
|
||||||
'type' => Model\Venta\Reservation\Detail\Type::Broker->value,
|
'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) {}
|
} catch (PDOException) {}
|
||||||
}
|
}
|
||||||
protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void
|
protected function editPromotions(Model\Venta\Reservation &$reservation, array $new_data): void
|
||||||
|
@ -140,17 +140,7 @@ class Reservation extends Ideal\Service\API
|
|||||||
$buyerData[substr($key, strlen('buyer_'))] = $value;
|
$buyerData[substr($key, strlen('buyer_'))] = $value;
|
||||||
}
|
}
|
||||||
$this->personaService->add($buyerData);
|
$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');
|
$data['date'] = $date->format('Y-m-d');
|
||||||
try {
|
try {
|
||||||
$reservationData = $this->reservationRepository->filterData($data);
|
$reservationData = $this->reservationRepository->filterData($data);
|
||||||
@ -177,7 +167,11 @@ class Reservation extends Ideal\Service\API
|
|||||||
$units = array_combine($data['units'], $data['units_value']);
|
$units = array_combine($data['units'], $data['units_value']);
|
||||||
$this->addUnits($reservation, $units);
|
$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']);
|
$this->addPromotions($reservation, $data['promotions']);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,6 +218,14 @@ class Reservation extends Ideal\Service\API
|
|||||||
}
|
}
|
||||||
$this->reservationRepository->save($reservation);
|
$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
|
protected function addPromotions(Model\Venta\Reservation $reservation, array $promotions): void
|
||||||
{
|
{
|
||||||
foreach ($promotions as $promotion_id) {
|
foreach ($promotions as $promotion_id) {
|
||||||
|
@ -37,7 +37,7 @@ class ReservationTest extends TestCase
|
|||||||
$unitsValue[] = $faker->randomFloat(2, 1000, 10000);
|
$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);
|
$activePromotions = $this->container->get(Repository\Venta\Promotion::class)->fetchActiveByProject($project->id);
|
||||||
|
|
||||||
@ -87,5 +87,6 @@ class ReservationTest extends TestCase
|
|||||||
if ($broker !== null) {
|
if ($broker !== null) {
|
||||||
$this->assertEquals($data['broker_rut'], $reservation->broker->rut);
|
$this->assertEquals($data['broker_rut'], $reservation->broker->rut);
|
||||||
}
|
}
|
||||||
|
$this->assertEquals(1, $reservation->currentState()->type->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ class ReservationTest extends TestCase
|
|||||||
$reservation->project = $this->getMockBuilder(Model\Proyecto::class)->disableOriginalConstructor()->getMock();
|
$reservation->project = $this->getMockBuilder(Model\Proyecto::class)->disableOriginalConstructor()->getMock();
|
||||||
$reservation->project->id = $data['project_id'];
|
$reservation->project->id = $data['project_id'];
|
||||||
$reservation->date = new DateTimeImmutable($data['date']);
|
$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 = $this->getMockBuilder(Model\Proyecto\Broker::class)->disableOriginalConstructor()->getMock();
|
||||||
$reservation->broker->rut = $data['broker_rut'];
|
$reservation->broker->rut = $data['broker_rut'];
|
||||||
}
|
}
|
||||||
@ -47,6 +47,12 @@ class ReservationTest extends TestCase
|
|||||||
});
|
});
|
||||||
$this->stateRepository = $this->getMockBuilder(Repository\Venta\Reservation\State::class)
|
$this->stateRepository = $this->getMockBuilder(Repository\Venta\Reservation\State::class)
|
||||||
->disableOriginalConstructor()->getMock();
|
->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)
|
$this->personaService = $this->getMockBuilder(Service\Persona::class)
|
||||||
->disableOriginalConstructor()->getMock();
|
->disableOriginalConstructor()->getMock();
|
||||||
$this->personaService->method('add')->willReturnCallback(function($data) {
|
$this->personaService->method('add')->willReturnCallback(function($data) {
|
||||||
@ -61,6 +67,11 @@ class ReservationTest extends TestCase
|
|||||||
});
|
});
|
||||||
$this->brokerService = $this->getMockBuilder(Service\Proyecto\Broker::class)
|
$this->brokerService = $this->getMockBuilder(Service\Proyecto\Broker::class)
|
||||||
->disableOriginalConstructor()->getMock();
|
->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)
|
$this->promotionService = $this->getMockBuilder(Service\Venta\Promotion::class)
|
||||||
->disableOriginalConstructor()->getMock();
|
->disableOriginalConstructor()->getMock();
|
||||||
$this->promotionService->method('getById')->willReturnCallback(function($id) {
|
$this->promotionService->method('getById')->willReturnCallback(function($id) {
|
||||||
|
Reference in New Issue
Block a user