diff --git a/hotel/data/hotel_demo.xml b/hotel/data/hotel_demo.xml index 60cbf27de..2f4cd2427 100644 --- a/hotel/data/hotel_demo.xml +++ b/hotel/data/hotel_demo.xml @@ -265,6 +265,7 @@ 'checkout': (DateTime.today() + timedelta(days=2)).strftime('%Y-%m-%d'), 'adults': 2, 'state': 'confirm', + 'board_service_id': ref('hotel_board_service_1'), })]"/> @@ -276,6 +277,7 @@ 'checkin': (DateTime.today() + timedelta(days=2)).strftime('%Y-%m-%d'), 'checkout': (DateTime.today() + timedelta(days=4)).strftime('%Y-%m-%d'), 'adults': 3, + 'board_service_id': ref('hotel_board_service_2'), })]"/> @@ -312,6 +314,7 @@ 'checkout': (DateTime.today() + timedelta(days=4)).strftime('%Y-%m-%d'), 'adults': 1, 'state': 'confirm', + 'board_service_id': ref('hotel_board_service_1'), })]"/> diff --git a/hotel/models/hotel_reservation.py b/hotel/models/hotel_reservation.py index e6178463a..8406eee33 100644 --- a/hotel/models/hotel_reservation.py +++ b/hotel/models/hotel_reservation.py @@ -290,6 +290,15 @@ class HotelReservation(models.Model): #~ 'reserve_color': colors[0], #~ 'reserve_color_text': colors[1], }) + if 'board_service_id' in vals: + board_services = [] + board = self.env['hotel.board.service'].browse(vals['board_service_id']) + for product in board.service_ids: + board_services.append((0, False, { + 'product_id': product.id, + 'is_board_service': True, + })) + vals.update({'service_ids': board_services}) if self.compute_price_out_vals(vals): days_diff = ( fields.Date.from_string(vals['checkout']) - fields.Date.from_string(vals['checkin']) @@ -317,6 +326,22 @@ class HotelReservation(models.Model): fields.Date.from_string(checkout) - \ fields.Date.from_string(checkin) ).days + if self.compute_board_services(vals): + record.service_ids.filtered(lambda r: r.is_board_service == True).unlink() + board_services = [] + board = self.env['hotel.board.service'].browse(vals['board_service_id']) + for product in board.service_ids: + board_services.append((0, False, { + 'product_id': product.id, + 'is_board_service': True, + })) + # NEED REVIEW: Why I need add manually the old IDs if board service is (0,0,(-)) ¿?¿?¿ + record.update({'service_ids': [(6, 0, record.service_ids.ids)] + board_services}) + update_services = record.service_ids.filtered( + lambda r: r.is_board_service == True + ) + for service in update_services: + service.onchange_product_calc_qty() if record.compute_price_out_vals(vals): record.update(record.prepare_reservation_lines( checkin, @@ -325,14 +350,12 @@ class HotelReservation(models.Model): if record.compute_qty_service_day(vals): for service in record.service_ids: if service.product_id.per_day: - params = { - 'per_person': service.product_id.per_person, - 'persons': vals['adults'] if 'adults' in vals else record.adults - } service.update(service.prepare_service_lines( - checkin, - days_diff, - params + dfrom=checkin, + days=days_diff, + per_person=product.per_person, + persons=reservation.adults, + old_line_days=service.service_line_ids )) if ('checkin' in vals and record.checkin != vals['checkin']) or \ ('checkout' in vals and record.checkout != vals['checkout']) or \ @@ -341,6 +364,21 @@ class HotelReservation(models.Model): res = super(HotelReservation, self).write(vals) return res + @api.multi + def compute_board_services(self, vals): + """ + We must compute service_ids when we hace a board_service_id without + service_ids associated to reservation + """ + if 'board_service_id' in vals: + if 'service_ids' in vals: + for service in vals['service_ids']: + if 'is_board_service' in service[2] and \ + service[2]['is_board_service'] == True: + return False + return True + return False + @api.multi def compute_qty_service_day(self, vals): """ @@ -361,12 +399,14 @@ class HotelReservation(models.Model): def _prepare_add_missing_fields(self, values): """ Deduce missing required fields from the onchange """ res = {} - onchange_fields = ['room_id', 'reservation_type', 'currency_id', 'name'] + onchange_fields = ['room_id', 'reservation_type', + 'currency_id', 'name', 'board_service_id'] if values.get('room_type_id'): line = self.new(values) if any(f not in values for f in onchange_fields): line.onchange_room_id() line.onchange_compute_reservation_description() + line.onchange_board_service() if 'pricelist_id' not in values: line.onchange_partner_id() for field in onchange_fields: @@ -567,21 +607,23 @@ class HotelReservation(models.Model): @api.onchange('board_service_id') def onchange_board_service(self): if self.board_service_id: - self.service_ids.filtered(lambda r: r.is_board_service == True).unlink() board_services = [] for product in self.board_service_id.service_ids: - board_services.append((0, False, { - 'product_id': product.id, - 'is_board_service': True, - })) - # NEED REVIEW: Why I need add manually the old IDs if board service is (0,0,(-)) ¿?¿?¿ - self.update({'service_ids': [(6, 0, self.service_ids.ids)] + board_services}) - update_services = self.service_ids.filtered( - lambda r: r.is_board_service == True - ) - for service in update_services: - service.onchange_product_calc_qty() - + if product.per_day: + vals = { + 'product_id': product.id, + 'is_board_service': True, + } + vals.update(self.env['hotel.service'].prepare_service_lines( + dfrom=self.checkin, + days=self.nights, + per_person=product.per_person, + persons=self.adults, + old_line_days=False)) + board_services.append((0, False, vals)) + other_services = self.service_ids.filtered(lambda r: r.is_board_service == False) + + self.update({'service_ids': [(6, 0, other_services.ids)] + board_services}) """ COMPUTE RESERVE COLOR ---------------------------------------------- diff --git a/hotel/models/hotel_service.py b/hotel/models/hotel_service.py index 52dcc4f7c..26c52c7b6 100644 --- a/hotel/models/hotel_service.py +++ b/hotel/models/hotel_service.py @@ -78,14 +78,13 @@ class HotelService(models.Model): if self.compute_lines_out_vals(vals): reservation = self.env['hotel.reservation'].browse(vals['ser_room_line']) product = self.env['product.product'].browse(vals['product_id']) - params = { - 'per_person': product.per_person, - 'persons': reservation.adults - } + vals.update(self.prepare_service_lines( - reservation.checkin, - reservation.nights, - params + dfrom=reservation.checkin, + days=reservation.nights, + per_person=product.per_person, + persons=reservation.adults, + old_day_lines=False, )) record = super(HotelService, self).create(vals) return record @@ -105,14 +104,12 @@ class HotelService(models.Model): reservations = self.env['hotel.reservation'] reservation = reservations.browse(vals['ser_room_line']) \ if 'ser_room_line' in vals else record.ser_room_line - params = { - 'per_person': product.per_person, - 'persons': reservation.adults - } record.update(record.prepare_service_lines( - reservation.checkin, - reservation.nights, - params + dfrom=reservation.checkin, + days=reservation.nights, + per_person=product.per_person, + persons=reservation.adults, + old_line_days=self.service_line_ids )) res = super(HotelService, self).write(vals) return res @@ -142,37 +139,36 @@ class HotelService(models.Model): if record.per_day and record.ser_room_line: product = record.product_id reservation = record.ser_room_line - params = { - 'per_person': product.per_person, - 'persons': reservation.adults - } record.update(self.prepare_service_lines( - reservation.checkin, - reservation.nights, - params)) - @api.multi - def prepare_service_lines(self, dfrom, days, params=False): + dfrom=reservation.checkin, + days=reservation.nights, + per_person=product.per_person, + persons=reservation.adults, + old_line_days=self.service_line_ids)) + + @api.model + def prepare_service_lines(self, **kwargs): """ - Respect the old manual changes on lines + Prepare line and respect the old manual changes on lines """ - self.ensure_one() cmds = [(5, 0, 0)] - old_lines_days = self.mapped('service_line_ids.date') + old_lines_days = kwargs.get('old_lines_days') total_qty = 0 day_qty = 1 - if params.get('per_person'): #WARNING: Change adults in reservation NOT update qty service!! - day_qty = params.get('persons') - for i in range(0, days): - idate = (fields.Date.from_string(dfrom) + timedelta(days=i)).strftime( + if kwargs.get('per_person'): #WARNING: Change adults in reservation NOT update qty service!! + day_qty = kwargs.get('persons') + old_line_days = self.env['hotel.service.line'].browse(kwargs.get('old_line_days')) + for i in range(0, kwargs.get('days')): + idate = (fields.Date.from_string(kwargs.get('dfrom')) + timedelta(days=i)).strftime( DEFAULT_SERVER_DATE_FORMAT) - old_line = self.service_line_ids.filtered(lambda r: r.date == idate) - if idate not in old_lines_days: + if not old_lines_days or idate not in old_lines_days.mapped('date'): cmds.append((0, False, { 'date': idate, 'day_qty': day_qty })) total_qty = total_qty + day_qty else: + old_line = old_line_days.filtered(lambda r: r.date == idate) cmds.append((4, old_line.id)) total_qty = total_qty + old_line.day_qty return {'service_line_ids': cmds, 'product_qty': total_qty} diff --git a/hotel/models/inherited_account_invoice.py b/hotel/models/inherited_account_invoice.py index e0e951f07..be8626210 100644 --- a/hotel/models/inherited_account_invoice.py +++ b/hotel/models/inherited_account_invoice.py @@ -58,6 +58,8 @@ class AccountInvoice(models.Model): @api.multi def action_invoice_open(self): + #TODO: VAT Control + """ to_open_invoices_without_vat = self.filtered( lambda inv: inv.state != 'open' and inv.partner_id.vat == False) if to_open_invoices_without_vat: @@ -65,5 +67,6 @@ class AccountInvoice(models.Model): for invoice in to_open_invoices_without_vat: vat_error += ", " + invoice.partner_id.name raise ValidationError(vat_error) + """ return super(AccountInvoice, self).action_invoice_open()