如何计算Odoo 8酒店管理模块的夜数

时间:2015-07-10 05:32:34

标签: odoo-8

我正在使用odoo版本8中的酒店管理模块。

我希望计算入住日期和退房日期之间的住宿天数。谁能帮我这个?

我为预订定义了一个类,其中包含用于签入和签出的更改方法。我尝试了计算total_nights的函数。这是我的代码,

class hotel_reservation(osv.Model):
_name = "hotel.reservation"
_rec_name = "reservation_no"
_description = "Reservation"
_order = 'reservation_no desc'
_columns = {
    'reservation_no': fields.char('Reservation No', size=64, required=True, readonly=True),
    'date_order':fields.datetime('Date Ordered', required=True, readonly=True, states={'draft':[('readonly', False)]}),
    'warehouse_id':fields.many2one('stock.warehouse', 'Hotel', readonly=True, required=True, states={'draft':[('readonly', False)]}),
    'partner_id':fields.many2one('res.partner', 'Guest Name', readonly=True, required=True, states={'draft':[('readonly', False)]}),
    'pricelist_id':fields.many2one('product.pricelist', 'Price List', required=True, readonly=True, states={'draft':[('readonly', False)]}, help="Pricelist for current reservation. "),
    'partner_invoice_id':fields.many2one('res.partner', 'Invoice Address', readonly=True, states={'draft':[('readonly', False)]}, help="Invoice address for current reservation. "),
    'partner_order_id':fields.many2one('res.partner', 'Ordering Contact', readonly=True, states={'draft':[('readonly', False)]}, help="The name and address of the contact that requested the order or quotation."),
    'partner_shipping_id':fields.many2one('res.partner', 'Delivery Address', readonly=True, states={'draft':[('readonly', False)]}, help="Delivery address for current reservation. "),
    'checkin': fields.datetime('Expected-Date-Arrival', required=True, readonly=True, states={'draft':[('readonly', False)]}),
    'checkout': fields.datetime('Expected-Date-Departure', required=True, readonly=True, states={'draft':[('readonly', False)]}),
    'adults':fields.integer('Adults', size=64, readonly=True, states={'draft':[('readonly', False)]}, help='List of adults there in guest list. '),
    'children':fields.integer('Children', size=64, readonly=True, states={'draft':[('readonly', False)]}, help='Number of children there in guest list. '),
    'reservation_line':fields.one2many('hotel_reservation.line', 'line_id', 'Reservation Line', help='Hotel room reservation details. '),
    'state': fields.selection([('draft', 'Draft'), ('confirm', 'Confirm'), ('cancel', 'Cancel'), ('done', 'Done')], 'State', readonly=True),
    'folio_id': fields.many2many('hotel.folio', 'hotel_folio_reservation_rel', 'order_id', 'invoice_id', 'Folio'),
    'dummy': fields.datetime('Dummy'),
    #'No_of_nights': fields.integer('Number of Nights', size=64, readonly=True, states={'draft':[('readonly', False)]}, help='Number of Nights.'),
    'no_of_nights':fields.function(_total_nights, string='Amount to be returned',
                                   store={
                                          'value': (lambda self, cr, uid, ids, c={}: ids, ['amount', 'roi'], 10),
                                          },), 
}
_defaults = {
    'reservation_no': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'hotel.reservation'),
    'state': lambda *a: 'draft',
    'date_order': lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
}

 def on_change_checkin(self, cr, uid, ids, date_order, checkin_date=time.strftime('%Y-%m-%d %H:%M:%S'), context=None):
    if date_order and checkin_date:
        if checkin_date < date_order:
            raise osv.except_osv(_('Warning'), _('Checkin date should be greater than the current date.'))
    return {'value':{}}

def on_change_checkout(self, cr, uid, ids, checkin_date=time.strftime('%Y-%m-%d %H:%M:%S'), checkout_date=time.strftime('%Y-%m-%d %H:%M:%S'), context=None):
    if not (checkout_date and checkin_date):
        return {'value':{}}
    if checkout_date < checkin_date:
        raise osv.except_osv(_('Warning'), _('Checkout date should be greater than the Checkin date.'))
    delta = datetime.timedelta(days=1)
    addDays = datetime.datetime(*time.strptime(checkout_date, '%Y-%m-%d %H:%M:%S')[:5]) + delta
    val = {'value':{'dummy':addDays.strftime('%Y-%m-%d %H:%M:%S')}}
    return val     

def onchange_partner_id(self, cr, uid, ids, partner_id):
    if not partner_id:
        return {'value':{'partner_invoice_id': False, 'partner_shipping_id':False, 'partner_order_id':False}}
    partner_obj = self.pool.get('res.partner')
    addr = partner_obj.address_get(cr, uid, [partner_id], ['delivery', 'invoice', 'contact'])
    pricelist = partner_obj.browse(cr, uid, partner_id).property_product_pricelist.id
    return {'value':{'partner_invoice_id': addr['invoice'], 'partner_order_id':addr['contact'], 'partner_shipping_id':addr['delivery'], 'pricelist_id': pricelist}}

def total_nights(self, cr, uid, ids, context=None):
    print("Function for Calculating total Nights !")

    context = context or {}

    res = {}
    ''''''          
    for value in self.browse(cr, uid, ids, checkin_date=time.strftime('%Y-%m-%d %H:%M:%S'), checkout_date=time.strftime('%Y-%m-%d %H:%M:%S'), context=context):

        check_in = datetime.strptime(check_in, "%Y-%m-%d")
        check_out = datetime.strptime(check_out, "%Y-%m-%d")
        delta = check_out - check_in

        print delta.days 

    return res

class hotel_room_reservation_line(osv.Model):
_name = 'hotel.room.reservation.line'
_description = 'Hotel Room Reservation'
_rec_name = 'room_id'
_columns = {
    'room_id': fields.many2one('hotel.room', 'Room id'),
    'check_in':fields.datetime('Check In Date', required=True),
    'check_out': fields.datetime('Check Out Date', required=True),
    'state': fields.selection([('assigned', 'Assigned'), ('unassigned', 'Unassigned')], 'Room Status'),
    'reservation_id': fields.many2one('hotel.reservation', 'Reservation'),
}

hotel_room_reservation_line()

1 个答案:

答案 0 :(得分:0)

此代码可以帮助您:

# Making difference in dates to get no. of days .....

@api.one
@api.onchange('check_in')   
def onchange_date_time(self):
    # print 'working till now ----------------------------'
    if self.check_in:
        check_in = datetime.datetime.strptime(self.check_in, "%Y-%m-%d").date()
        if check_in<datetime.date.today():
            #raise except_orm('Error','Please enter the correct date')
            self.check_in = None
        else:
            pass


@api.one
@api.onchange('check_out')  
def onchange_check_out(self):
    if self.check_out:
        check_out = datetime.datetime.strptime(self.check_out, "%Y-%m-%d").date()
        check_in = datetime.datetime.strptime(self.check_in, "%Y-%m-%d").date()
        if check_out<datetime.date.today():
            self.check_out = False
            # if not self.check_out:
                # raise osv.except_orm('Error','Please enter the correct date')

        else:
            pass


@api.onchange('check_out')
def onchange_num_day(self):
    if self.check_out and self.check_in:
        check_in = datetime.datetime.strptime(self.check_in, "%Y-%m-%d").date()
        check_out = datetime.datetime.strptime(self.check_out, "%Y-%m-%d").date()
        self.days = (check_out-check_in).days+1
        if self.days == 0:
            self.days = 1

  #Hence we will get No.of days .. Ends 

字段:checked_in,checked_out,days

由于