namespace DataSource;
use DateTime;
use Driver\MySQLi;
use Domain\RoomCustomPriceDomain;
class RoomCustomPriceDataSource extends RootDataSource
{
public function instantiateRoomCustomPriceDomain(array $row)
{
$row['created_date'] = $this->getDateObject($row['created_date']);
$row['certain_date'] = $this->getDateObject($row['certain_date']);
$roomCustomPriceDomain = new RoomCustomPriceDomain(
$row['id'],
$row['room_id'],
$row['created_date'],
$row['certain_date'],
$row['price'],
$row['rebate']
);
return $roomCustomPriceDomain;
}
public function insert(array $roomCustomPriceDomainArray)
{
$statement = $this->mysqli->buildStatement('
DELETE FROM
room_custom_price
WHERE
certain_date = :certainDate AND
room_id = :roomId
');
foreach ($roomCustomPriceDomainArray as $roomCustomPriceDomain) {
$statement->execute(array(
':roomId' => $roomCustomPriceDomain->getRoomId(),
':certainDate' => $roomCustomPriceDomain->getCertainDate()->format('Y-m-d')
));
}
$statement->close();
$statement = $this->mysqli->buildStatement('
INSERT INTO
room_custom_price
VALUES(
:id,
:roomId,
:createdDate,
:certainDate,
:price,
:rebate
)
');
foreach ($roomCustomPriceDomainArray as $roomCustomPriceDomain) {
$statement->execute(array(
':id' => $roomCustomPriceDomain->getId(),
':roomId' => $roomCustomPriceDomain->getRoomId(),
':createdDate' => $roomCustomPriceDomain->getCreatedDate()->format('Y-m-d H:i:s'),
':certainDate' => $roomCustomPriceDomain->getCertainDate()->format('Y-m-d'),
':price' => $roomCustomPriceDomain->getPrice(),
':rebate' => $roomCustomPriceDomain->getRebate()
));
}
$statement->close();
return true;
}
public function delete(array $dataArray)
{
$statement = $this->mysqli->buildStatement('
DELETE FROM
room_custom_price
WHERE
room_id = :roomId AND
certain_date = :certainDate
');
foreach ($dataArray as $data) {
$statement->execute(array(
':roomId' => $data['roomId'],
':certainDate' => $data['certainDate']
));
}
$statement->close();
}
public function getAllByRoomId($roomId, $now)
{
$certainDate = new DateTime($now->format('Y-m-d'));
$startDate = $certainDate->modify('-1 month')->format('Y-m-17');
$endDate = $certainDate->modify('+2 months')->format('Y-m-13');
$statement = $this->mysqli->buildStatement('
SELECT
DATE_FORMAT(certain_date, \'%m-%d-%Y\') AS certain_date,
price
FROM
room_custom_price
WHERE
room_id = :roomId AND
(certain_date BETWEEN :startDate AND :endDate)
');
$statement->execute(array(
':roomId' => $roomId,
':startDate' => $startDate,
':endDate' => $endDate
));
$rows = $statement->fetchAllAssociative();
$statement->close();
return $rows;
}
}