mirror of
https://github.com/OCA/pms.git
synced 2025-01-29 00:17:45 +02:00
[ADD]pms_api_rest: extra beds service
This commit is contained in:
@@ -29,3 +29,4 @@ from . import res_city_zip
|
||||
|
||||
from . import pms_search_param
|
||||
from . import pms_ubication
|
||||
from . import pms_extra_bed
|
||||
|
||||
20
pms_api_rest/datamodels/pms_extra_bed.py
Normal file
20
pms_api_rest/datamodels/pms_extra_bed.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from marshmallow import fields
|
||||
|
||||
from odoo.addons.datamodel.core import Datamodel
|
||||
|
||||
|
||||
class PmsExtraBedSearchParam(Datamodel):
|
||||
_name = "pms.extra.beds.search.param"
|
||||
id = fields.Integer(required=False, allow_none=True)
|
||||
name = fields.String(required=False, allow_none=True)
|
||||
pmsPropertyId = fields.Integer(required=True, allow_none=False)
|
||||
dateFrom = fields.String(required=False, allow_none=True)
|
||||
dateTo = fields.String(required=False, allow_none=True)
|
||||
|
||||
|
||||
class PmsExtraBedInfo(Datamodel):
|
||||
_name = "pms.extra.bed.info"
|
||||
id = fields.Integer(required=False, allow_none=True)
|
||||
name = fields.String(required=False, allow_none=True)
|
||||
dailyLimitConfig = fields.Integer(required=False, allow_none=True)
|
||||
dailyLimitAvail = fields.Integer(required=False, allow_none=True)
|
||||
@@ -15,3 +15,4 @@ from . import res_partner_category_services
|
||||
from . import res_city_zip_service
|
||||
from . import pms_room_type_class_service
|
||||
from . import pms_ubication_service
|
||||
from . import pms_extra_beds_service
|
||||
|
||||
72
pms_api_rest/services/pms_extra_beds_service.py
Normal file
72
pms_api_rest/services/pms_extra_beds_service.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from odoo.addons.base_rest import restapi
|
||||
from odoo.addons.base_rest_datamodel.restapi import Datamodel
|
||||
from odoo.addons.component.core import Component
|
||||
|
||||
|
||||
class PmsExtraBedsService(Component):
|
||||
_inherit = "base.rest.service"
|
||||
_name = "pms.extra.beds.service"
|
||||
_usage = "extra-beds"
|
||||
_collection = "pms.services"
|
||||
|
||||
@restapi.method(
|
||||
[
|
||||
(
|
||||
[
|
||||
"/",
|
||||
],
|
||||
"GET",
|
||||
)
|
||||
],
|
||||
input_param=Datamodel("pms.extra.beds.search.param"),
|
||||
output_param=Datamodel("pms.extra.bed.info", is_list=True),
|
||||
auth="jwt_api_pms",
|
||||
)
|
||||
def get_extra_beds(self, extra_beds_search_param):
|
||||
domain = [("is_extra_bed", "=", True)]
|
||||
if extra_beds_search_param.name:
|
||||
domain.append(("name", "like", extra_beds_search_param.name))
|
||||
if extra_beds_search_param.id:
|
||||
domain.append(("id", "=", extra_beds_search_param.id))
|
||||
if extra_beds_search_param.pmsPropertyId:
|
||||
domain.extend(
|
||||
[
|
||||
"|",
|
||||
("pms_property_ids", "in", extra_beds_search_param.pmsPropertyId),
|
||||
("pms_property_ids", "=", False),
|
||||
]
|
||||
)
|
||||
|
||||
result_extra_beds = []
|
||||
PmsExtraBed = self.env.datamodels["pms.extra.bed.info"]
|
||||
|
||||
for bed in self.env["product.product"].search(
|
||||
domain,
|
||||
):
|
||||
avail = -1
|
||||
if extra_beds_search_param.dateFrom and extra_beds_search_param.dateTo:
|
||||
qty_for_day = self.env["pms.service.line"].read_group(
|
||||
[
|
||||
("product_id", "=", bed.id),
|
||||
("date", ">=", extra_beds_search_param.dateFrom),
|
||||
("date", "<=", extra_beds_search_param.dateTo),
|
||||
("cancel_discount", "=", 0),
|
||||
],
|
||||
["day_qty:sum"],
|
||||
["date:day"],
|
||||
)
|
||||
max_daily_used = max(date["day_qty"] for date in qty_for_day)
|
||||
avail = bed.daily_limit - max_daily_used
|
||||
# Avoid send negative values in avail
|
||||
avail = avail if avail >= 0 else 0
|
||||
|
||||
result_extra_beds.append(
|
||||
PmsExtraBed(
|
||||
id=bed.id,
|
||||
name=bed.name,
|
||||
dailyLimitConfig=bed.daily_limit,
|
||||
dailyLimitAvail=avail,
|
||||
)
|
||||
)
|
||||
|
||||
return result_extra_beds
|
||||
Reference in New Issue
Block a user