Spaces:
Running
Running
File size: 12,094 Bytes
0c117c4 992488d 0c117c4 992488d 0c117c4 992488d 0c117c4 992488d 0c117c4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | <?php
namespace App\Services;
use App\Enums\BookingStatus;
use App\Mails\BookingMail;
use App\Models\Booking as BookingModel;
use Carbon\Carbon;
use Dacastro4\LaravelGmail\Services\Message\Mail;
use Illuminate\Support\Facades\Storage;
use phpseclib3\File\ASN1\Maps\Extension;
use stdClass;
class BookingServices extends BaseServices
{
private $roomTypePacketServices;
private $roomServices;
private $roomBookingServices;
private $paymentServices;
private $packetServices;
public function __construct(BookingModel $model,
RoomTypePacketServices $roomTypePacketServices,
RoomServices $roomServices,
RoomBookingServices $roomBookingServices,
PaymentServices $paymentServices,
PacketServices $packetServices)
{
parent::__construct($model);
$this->roomTypePacketServices = $roomTypePacketServices;
$this->roomServices = $roomServices;
$this->roomBookingServices = $roomBookingServices;
$this->paymentServices = $paymentServices;
$this->packetServices = $packetServices;
}
public function index($request, $customerId = null)
{
$limit = $request->get("limit");
$query_array = $request->query();
$query = $this->model;
$status = $query_array['status'] ?? "";
if (!empty($status)) {
$query = $query->where('status', $status);
}
if ($customerId && !empty($customerId)) {
$query = $query->where("customer_id", $customerId);
// $query->with('customer', function ($query) use ($customerId) {
// $query->where('id', $customerId);
// });
}
$query->with("payments", function ($query) {
$query->select("payments.id", "payments.payment_date",
"payments.email", "payments.address",
"payments.payment_method", "payments.description",
"payments.payment_amount", "payments.booking_id");
});
$query->with("rooms", function ($query) {
$query->select("rooms.id", "rooms.room_view",
"rooms.room_number", "rooms.room_type_packet_id");
});
$query->select("id", "checkout_at", "checkin_at", "total_price"
, "number_guests", "status", "cancel_reason");
$query = $query->orderBy('updated_at', 'desc');
$rs = empty($limit) ? ($query->get()) : ($query->paginate($limit));
$this->prepareRoom($rs);
return $rs;
}
public function prepareRoom(&$booking)
{
$booking->each(function ($item) {
$this->roomServices->getRoomByIdsAndPacket($item);
});
}
public function getBookingByNotAvailble($param)
{
$from_time = $param['checkin_at'];
$to_time = $param['checkout_at'];
$query = $this->model;
$query = $query->whereHas('bookingStatus', function ($query) {
$query->where('name', '!=', BookingStatus::getKey(BookingStatus::CANCEL));
});
$query->where(function ($query) use ($from_time, $to_time) {
$query->orwhere(function ($query) use ($from_time, $to_time) {
$query
->whereRaw("bookings.checkin_at <= STR_TO_DATE(?, '%Y-%m-%d %H:%i:%s')", $from_time)
->whereRaw("bookings.checkout_at >= STR_TO_DATE(?, '%Y-%m-%d %H:%i:%s')", $from_time);
});
$query->orwhere(function ($query) use ($from_time, $to_time) {
$query
->whereRaw("bookings.checkin_at >= STR_TO_DATE(?, '%Y-%m-%d %H:%i:%s')", $from_time)
->whereRaw("bookings.checkin_at <= STR_TO_DATE(?, '%Y-%m-%d %H:%i:%s')", $to_time);
});
});
return $query->get();
}
public function makeCheckout($request)
{
// todo: tính lại total price dựa vào giá phòng, và giá packet
// todo: nhận về id room type, list id packet, tìm ra các room ứng với dữ liệu trên
// todo: thuế đc lấy từ db ?
// nếu số lượng room search nhỏ hơn số lượng room cần đăt , thì báo lỗi
// 1. tạo 1 record booking với thông tin booking
// nếu dữ liệu của paymentamount bằng với total price, thì set status là complete
// nhỏ hơn thì set partialy paid , nếu paymentamount = 0 thì set pending
// 2. tạo reocrd bookin_room với id booking và id room
// 3. tạo record payment với id booking
$gstRate = 0.12;
$totalPrice = 0;
$roomTypeId = $request["room"]["id"];
$numberRoom = count($request["packets"]);
$packets = [];
$paymentAmount = $request["payment"]["payment_amount"] ?? 0;
foreach ($request["packets"] as $item) {
$packets[] = $item["id"];
}
$isStopBooking = false;
$booking = [];
$hashCheck = [];
// tạo dữ liệu số lượng packet ứng với packet
foreach ($packets as $packet) {
if (isset($hashCheck[$packet])) {
$hashCheck[$packet] += 1;
} else {
$hashCheck[$packet] = 1;
}
}
// tinh tong tien
$checkout_at = Carbon::createFromFormat('Y-m-d', $request["checkout_at"]);
$checkin_at = Carbon::createFromFormat('Y-m-d', $request["checkin_at"]);
$bookingPeriodDays = $checkout_at->diff($checkin_at)->days;
$bookingPeriodDays = $bookingPeriodDays > 0 ? $bookingPeriodDays : 1;
$roomTypeService = app()->make(RoomTypeServices::class);
$tmp_packets = $this->packetServices->getPacketByIds($packets);
$roomType = $roomTypeService->getRoomTypeById($roomTypeId);
if (!$roomType || $isStopBooking) {
throw new \Exception("room not available");
}
$roomTypePrice = $roomType->base_price ?? 0;
$tmp_packets->each(function ($packet) use (&$totalPrice, $roomTypePrice, $bookingPeriodDays, $hashCheck) {
$totalPrice += (($packet->base_price) + $roomTypePrice) * $hashCheck[$packet->id] * $bookingPeriodDays;
});
$totalPrice = ($totalPrice * $gstRate) + $totalPrice;
$roomAvailable = collect();
foreach ($hashCheck as $key => $value) {
// lấy ra roomtypepacket ứng với packet roomtype
$roomTypePackets = $this->roomTypePacketServices->getRoomTypePacketByRoomTypeAndPacket($roomTypeId, $key)->pluck("id");
if (count($roomTypePackets) < 1) {
$isStopBooking = true;
break;
}
// điều kiện booking về ngày check in phải thõa
$bookings = $this->getBookingByNotAvailble($request)->pluck("id");
// lấy ra room thõa đk booking, và thõa đk room type
$rooms = $this->roomServices->getRoomByRoomTypePacketsAndBooking($roomTypePackets, $bookings);
// nếu số room tìm đc nhỏ hơn số room muốn đặt thì lỗi
if ($rooms->count() < $value) {
$isStopBooking = true;
break;
} else {
$rooms = $rooms->take($value);
$roomAvailable = $roomAvailable->merge($rooms);
}
}
if ($isStopBooking) {
throw new \Exception("room not available");
}
if ($paymentAmount == 0) {
$booking["status"] = BookingStatus::PENDING;
} else if ($totalPrice > $paymentAmount) {
$booking["status"] = BookingStatus::PENDING;
} else if ($paymentAmount >= $totalPrice) {
$booking["status"] = BookingStatus::PENDING;
}
$booking["customer_id"] = $this->getCurrentUser()->id;
$booking["checkin_at"] = $request["checkin_at"];
$booking["checkout_at"] = $request["checkout_at"];
$booking["total_price"] = $totalPrice;
$booking["number_guests"] = $request["guests"];
$booking = $this->save($booking);
if (!$booking || $isStopBooking) {
throw new \Exception("booking not available");
}
$roomBookings = collect();
// các room thõa đk có thể đặt phòng
$roomAvailable->each(function ($room) use ($booking, &$roomBookings) {
$roomBooking = [
"booking_id" => $booking->id,
"room_id" => $room->id,
"room_type_packet_id" => $room->room_type_packet_id
];
$roomBooking = $this->roomBookingServices->save($roomBooking);
if (!$roomBooking) {
$isStopBooking = true;
return false;
}
$roomBookings->push($roomBooking);
});
if ($isStopBooking) {
throw new \Exception("booking not available");
}
$payment = [
"booking_id" => $booking->id,
"payment_date" => $request["payment"]["payment_date"] ?? "",
"payment_method" => $request["payment"]["payment_method"] ?? "",
"description" => $request["payment"]["description"] ?? "",
"payment_amount" => $paymentAmount,
"address" => $request["payment"]["address"] ?? "",
"email" => $request["payment"]["email"] ?? "",
"city" => $request["payment"]["city"] ?? "",
"state" => $request["payment"]["state"] ?? "",
"post_code" => $request["payment"]["post_code"] ?? ""
];
$payment = $this->paymentServices->save($payment);
if (!$payment || $isStopBooking) {
throw new \Exception("payment not available");
}
$booking->rooms = $roomAvailable;
$this->roomServices->getRoomByIdsAndPacket($booking);
$gmailProvider = new Mail;
$gmailProvider->from('lisatthu35@gmail.com');
$gmailProvider->subject( "noreply-mail" );
$gmailProvider->to($payment->email);
$mailableInstance = new BookingMail($booking,$payment, "Thông tin đơn đặt phòng");
$gmailProvider->message($mailableInstance->render());
$gmailProvider->send();
//Mail::to($payment->email)->send(new BookingMail($booking,$payment, "Thông tin đơn đặt phòng"));
return $booking;
}
public function show($id)
{
$data = $this->model->where('id', $id)->first();
return $data;
}
public function save(array $attributes)
{
$mailSubject = false;
$entity = null;
if (!empty($attributes['id'])) {
$entity = $this->model->where('id', $attributes['id'])->first();
if ($entity) {
$entity->fill($attributes)->save();
if ($entity->status === BookingStatus::CANCEL) {
$mailSubject = "Đơn đặt phòng đã bị từ chối";
}
if ($entity->status === BookingStatus::COMPLETED) {
$mailSubject = "Đơn đặt phòng đã thành công";
}
}
} else {
$entity = $this->model->create($attributes);
}
if ($mailSubject) {
$gmailProvider = new Mail;
$gmailProvider->from('lisatthu35@gmail.com');
$gmailProvider->subject( "noreply-mail" );
$gmailProvider->to($entity->payments[0]->email);
$mailableInstance = new BookingMail($entity, $entity->payments[0], $mailSubject);
$gmailProvider->message($mailableInstance->render());
$gmailProvider->send();
//Mail::to($entity->payments[0]->email)->send(new BookingMail($entity, $entity->payments[0], $mailSubject));
}
return $entity;
}
public function delete($id)
{
$entity = $this->model
->where('id', $id)->first();
return !empty($entity) ? $entity->delete() : null;
}
}
|