Reservation fixes
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
Reference in New Issue
Block a user