__author__ = 'Khudrath'
from openerp.osv import fields, osv


class hotel_bed_type(osv.osv):
    _name = "hotel.bed.type"
    _description = "hotel bed"

    def _used_stock(self, cr, uid, ids, fieldnames, args, context=None):
        res = {}
        for obj in self.browse(cr, uid, ids, context=context):
            bed_qty = 0
            list_ids = self.pool.get("hotel.room.bed").search(cr, uid, [('name', '=', obj.id)], context=None)
            for list_basic_qty in self.pool.get("hotel.room.bed").read(cr, uid, list_ids, ['bed_qty'], context=context):
                bed_qty += list_basic_qty['bed_qty']
            res[obj.id] = bed_qty
        return res

    def _available_stock(self, cr, uid, ids, fieldnames, args, context=None):
        res = {}
        for obj in self.browse(cr, uid, ids, context=context):
            bed_qty = 0
            list_ids = self.pool.get("hotel.room.bed").search(cr, uid, [('name', '=', obj.id)], context=None)
            for list_basic_qty in self.pool.get("hotel.room.bed").read(cr, uid, list_ids, ['bed_qty'], context=context):
               bed_qty += list_basic_qty['bed_qty']
            res[obj.id] = (obj.total_stock - bed_qty)
        return res

    def _check_avi_total_stock(self, cr, uid, ids, context=None):
        for avi_bed in self.browse(cr, uid, ids):
            total_avi_bed = 0
            list_bed_types = self.pool.get("hotel.room.bed").search(cr, uid, [('name', '=', avi_bed.id)], context=None)
            for list_bed_type_value in self.pool.get("hotel.room.bed").\
                    read(cr, uid, list_bed_types, ['bed_qty'], context=context):
                total_avi_bed += list_bed_type_value['bed_qty']
        if avi_bed.total_stock >= total_avi_bed :
            return True
        else:
            return False

    _columns = {
        'name': fields.char('Type', size=128, required=True, select=True),
        'value': fields.integer
        ('Capacity', help="1 for Single, 2 for Double. This will be used for counting available Spaces in a room"),
        'total_stock': fields.integer('Total Stock', help="Total Stock"),
        'available_stock': fields.function(_available_stock, type='integer', string='Available Stock', store=False),
        'used_stock': fields.function(_used_stock, type='integer', string='Used Stock', store=False),
    }
    _sql_constraints = [
        ('name_bed_type_uniq', 'unique(name)', 'The name of the Bed Type must be unique...!'),
    ]
    _constraints = [
        (_check_avi_total_stock, 'Error! Requested Number of Bed Are Not Decreaseble.', ['total_stock']),
    ]

hotel_bed_type()

@@ -1799,16 +1799,257 @@
  False%0A%0A
+    def _constraints_capacity_nonzero(self, cr, uid, ids, context=None):%0A        result =True%0A        for current_rec in self.browse(cr, uid, ids):%0A            if current_rec.value %3C= 0:%0A                result = False%0A        return result%0A%0A
     _col
@@ -2163,25 +2163,16 @@
 .integer
-%0A        
 ('Capaci
@@ -2175,16 +2175,48 @@
 pacity',
+%0A                               
  help=%221
@@ -2242,16 +2242,58 @@
 Double. 
+%22 +%0A                                     %22
 This wil
@@ -2897,16 +2897,110 @@
 ock'%5D),%0A
+        (_constraints_capacity_nonzero, 'Capacity must be greater than zero.', %5B'Capacity'%5D),%0A
     %5D%0A%0Ah

capacity must be non zero