|
| 1 | +from odoo.addons.base_rest import restapi |
| 2 | +from odoo.addons.base_rest_datamodel.restapi import Datamodel |
| 3 | +from odoo.addons.component.core import Component |
| 4 | + |
| 5 | + |
| 6 | +class PmsReservationService(Component): |
| 7 | + _inherit = "base.rest.service" |
| 8 | + _name = "pms.reservation.service" |
| 9 | + _usage = "reservations" |
| 10 | + _collection = "pms.reservation.services" |
| 11 | + _description = """ |
| 12 | + Reservation API Services |
| 13 | + Services developed with the new api provided by base_rest |
| 14 | + """ |
| 15 | + |
| 16 | + @restapi.method( |
| 17 | + [(["/"], "GET")], |
| 18 | + input_param=Datamodel("pms.reservation.search.param"), |
| 19 | + output_param=Datamodel("pms.reservation.short.info", is_list=True), |
| 20 | + auth="public", |
| 21 | + ) |
| 22 | + def search(self, reservation_search_param): |
| 23 | + """ |
| 24 | + Devuelve un listado de todas las reservas, admite parámetros de búsqueda 'id' y 'name' (ejs.: "/reservations", "/reservations?id=1&name=R/21003449") |
| 25 | + """ |
| 26 | + domain = [] |
| 27 | + if reservation_search_param.name: |
| 28 | + domain.append(("name", "like", reservation_search_param.name)) |
| 29 | + if reservation_search_param.id: |
| 30 | + domain.append(("id", "=", reservation_search_param.id)) |
| 31 | + res = [] |
| 32 | + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] |
| 33 | + for reservation in self.env["pms.reservation"].sudo().search(domain): |
| 34 | + res.append( |
| 35 | + PmsReservationShortInfo( |
| 36 | + id=reservation.id, |
| 37 | + partner=reservation.partner_id.name, |
| 38 | + checkin=str(reservation.checkin), |
| 39 | + checkout=str(reservation.checkout), |
| 40 | + preferred_room_id=reservation.preferred_room_id.name |
| 41 | + if reservation.preferred_room_id |
| 42 | + else "", |
| 43 | + room_type_id=reservation.room_type_id.name |
| 44 | + if reservation.room_type_id |
| 45 | + else "", |
| 46 | + name=reservation.name, |
| 47 | + price=reservation.price_room_services_set, |
| 48 | + partner_requests=reservation.partner_requests |
| 49 | + if reservation.partner_requests |
| 50 | + else "", |
| 51 | + ) |
| 52 | + ) |
| 53 | + return res |
| 54 | + |
| 55 | + @restapi.method( |
| 56 | + [(["/<int:id>"], "GET")], |
| 57 | + output_param=Datamodel("pms.reservation.short.info"), |
| 58 | + auth="public", |
| 59 | + ) |
| 60 | + def search_by_id(self, reservation_id): |
| 61 | + """ |
| 62 | + Devuelve la reserva que se corresponde con el 'id' indicado en el path (ej.: /reservations/34) |
| 63 | + """ |
| 64 | + reservation = self.env["pms.reservation"].sudo().browse(reservation_id) |
| 65 | + if reservation: |
| 66 | + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] |
| 67 | + return PmsReservationShortInfo( |
| 68 | + id=reservation.id, |
| 69 | + partner=reservation.partner_id.name, |
| 70 | + checkin=str(reservation.checkin), |
| 71 | + checkout=str(reservation.checkout), |
| 72 | + preferred_room_id=reservation.preferred_room_id.name |
| 73 | + if reservation.preferred_room_id |
| 74 | + else "", |
| 75 | + room_type_id=reservation.room_type_id.name |
| 76 | + if reservation.room_type_id |
| 77 | + else "", |
| 78 | + name=reservation.name, |
| 79 | + price=reservation.price_room_services_set, |
| 80 | + partner_requests=reservation.partner_requests |
| 81 | + if reservation.partner_requests |
| 82 | + else "", |
| 83 | + ) |
| 84 | + |
| 85 | + @restapi.method( |
| 86 | + [(["/<int:id>/partner-requests"], "PATCH")], |
| 87 | + input_param=Datamodel("pms.reservation.patch.param"), |
| 88 | + output_param=Datamodel("pms.reservation.short.info"), |
| 89 | + auth="public", |
| 90 | + ) |
| 91 | + def update_partner_requests(self, reservation_id, reservation_patch_params): |
| 92 | + """ |
| 93 | + Actualiza el campo de peticiones de huéspedes para la reserva indicada con el id (ej.: /reservations/34) |
| 94 | + """ |
| 95 | + reservation = self.env["pms.reservation"].sudo().browse(reservation_id) |
| 96 | + reservation.partner_requests = reservation_patch_params.partner_requests |
| 97 | + if reservation: |
| 98 | + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] |
| 99 | + return PmsReservationShortInfo( |
| 100 | + id=reservation.id, |
| 101 | + partner=reservation.partner_id.name, |
| 102 | + checkin=str(reservation.checkin), |
| 103 | + checkout=str(reservation.checkout), |
| 104 | + preferred_room_id=reservation.preferred_room_id.name |
| 105 | + if reservation.preferred_room_id |
| 106 | + else "", |
| 107 | + room_type_id=reservation.room_type_id.name |
| 108 | + if reservation.room_type_id |
| 109 | + else "", |
| 110 | + name=reservation.name, |
| 111 | + price=reservation.price_room_services_set, |
| 112 | + partner_requests=reservation.partner_requests |
| 113 | + if reservation.partner_requests |
| 114 | + else "", |
| 115 | + ) |
| 116 | + |
| 117 | + @restapi.method( |
| 118 | + [(["/<int:id>"], "DELETE")], |
| 119 | + output_param=Datamodel("pms.reservation.short.info"), |
| 120 | + auth="public", |
| 121 | + ) |
| 122 | + def delete_reservation(self, reservation_id): |
| 123 | + """ |
| 124 | + Borra la reserva indicada con el id (ej.: /reservations/34) |
| 125 | + """ |
| 126 | + reservation = self.env["pms.reservation"].sudo().browse(reservation_id) |
| 127 | + if reservation: |
| 128 | + PmsReservationShortInfo = self.env.datamodels["pms.reservation.short.info"] |
| 129 | + result = PmsReservationShortInfo(id=reservation.id, |
| 130 | + partner=reservation.partner_id.name, |
| 131 | + checkin=str(reservation.checkin), |
| 132 | + checkout=str(reservation.checkout), |
| 133 | + preferred_room_id=reservation.preferred_room_id.name |
| 134 | + if reservation.preferred_room_id |
| 135 | + else "", |
| 136 | + room_type_id=reservation.room_type_id.name |
| 137 | + if reservation.room_type_id |
| 138 | + else "", |
| 139 | + name=reservation.name, |
| 140 | + price=reservation.price_room_services_set, |
| 141 | + partner_requests=reservation.partner_requests |
| 142 | + if reservation.partner_requests |
| 143 | + else "", |
| 144 | + ) |
| 145 | + reservation.sudo().unlink() |
| 146 | + return result |
0 commit comments