Fetch reservation with unit

This commit is contained in:
Juan Pablo Vial
2025-09-23 15:37:45 -03:00
parent d171a61b8a
commit 496dca9133
3 changed files with 46 additions and 6 deletions

View File

@ -71,9 +71,9 @@ class Reservations
$output['errors'] []= $this->parseError($exception); $output['errors'] []= $this->parseError($exception);
}*/ }*/
if (count($input['reservations']) === count($output['reservations'])) { /*if (count($input['reservations']) === count($output['reservations'])) {
$output['success'] = true; $output['success'] = true;
} }*/
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }

View File

@ -92,18 +92,57 @@ class Reservation extends Common\Ideal\Repository
/** /**
* @param int $buyer_rut * @param int $buyer_rut
* @param int $unit_id
* @param DateTimeInterface $date * @param DateTimeInterface $date
* @return Model\Venta\Reservation * @return Model\Venta\Reservation
* @throws Common\Implement\Exception\EmptyResult * @throws EmptyResult
*/ */
public function fetchByBuyerAndDate(int $buyer_rut, DateTimeInterface $date): Model\Venta\Reservation public function fetchByBuyerAndUnitAndDate(int $buyer_rut, int $unit_id, DateTimeInterface $date): Model\Venta\Reservation
{
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from('reservations a')
->joined('INNER JOIN reservation_details rd ON a.id = rd.reservation_id')
->where('a.buyer_rut = :buyer_rut AND rd.unit_id = :unit_id AND a.date >= :date');
return $this->fetchOne($query, ['buyer_rut' => $buyer_rut, 'unit_id' => $unit_id, 'date' => $date->sub(new DateInterval('P10D'))->format('Y-m-d')]);
}
/**
* @param int $buyer_rut
* @param int $project_id
* @param DateTimeInterface $date
* @return array
* @throws EmptyResult
*/
public function fetchByBuyerAndProjectAndDate(int $buyer_rut, int $project_id, DateTimeInterface $date): array
{
$query = $this->connection->getQueryBuilder()
->select()
->from('reservations')
->where('buyer_rut = :buyer_rut AND project_id = :project_id AND date >= :date');
return $this->fetchMany($query, ['buyer_rut' => $buyer_rut, 'project_id' => $project_id, 'date' => $date->sub(new DateInterval('P10D'))->format('Y-m-d')]);
}
/**
* @param int $buyer_rut
* @param DateTimeInterface $date
* @return array
* @throws EmptyResult
*/
public function fetchByBuyerAndDate(int $buyer_rut, DateTimeInterface $date): array
{ {
$query = $this->connection->getQueryBuilder() $query = $this->connection->getQueryBuilder()
->select() ->select()
->from('reservations') ->from('reservations')
->where('buyer_rut = :buyer_rut AND date >= :date'); ->where('buyer_rut = :buyer_rut AND date >= :date');
return $this->fetchOne($query, ['buyer_rut' => $buyer_rut, 'date' => $date->sub(new DateInterval('P10D'))->format('Y-m-d')]); return $this->fetchMany($query, ['buyer_rut' => $buyer_rut, 'date' => $date->sub(new DateInterval('P10D'))->format('Y-m-d')]);
} }
/**
* @param int $project_id
* @return array
* @throws EmptyResult
*/
public function fetchByProject(int $project_id): array public function fetchByProject(int $project_id): array
{ {
$query = $this->connection->getQueryBuilder() $query = $this->connection->getQueryBuilder()
@ -145,6 +184,7 @@ class Reservation extends Common\Ideal\Repository
return $this->fetchMany($query, ['project_id' => $project_id, return $this->fetchMany($query, ['project_id' => $project_id,
'state' => $state]); 'state' => $state]);
} }
/** /**
* @param int $project_id * @param int $project_id
* @return array * @return array

View File

@ -123,7 +123,7 @@ class Reservation extends Ideal\Service\API
$date = new DateTimeImmutable($data['date']); $date = new DateTimeImmutable($data['date']);
} catch (DateMalformedStringException) {} } catch (DateMalformedStringException) {}
try { try {
$reservation = $this->reservationRepository->fetchByBuyerAndDate($data['buyer_rut'], $date); $reservation = $this->reservationRepository->fetchByBuyerAndUnitAndDate($data['buyer_rut'], (int) $data['units'][0], $date);
if (array_key_exists('broker_rut', $data) and $data['broker_rut'] !== '') { if (array_key_exists('broker_rut', $data) and $data['broker_rut'] !== '') {
try { try {