class ImageMixin(models.AbstractModel): _name = 'image.mixin' _description = "Image Mixin" # all image fields are base64 encoded and PIL-supported image_1920 = fields.Image("Image", max_width=1920, max_height=1920) # resized fields stored (as attachment) for performance image_1024 = fields.Image("Image 1024", related="image_1920", max_width=1024, max_height=1024, store=True) image_512 = fields.Image("Image 512", related="image_1920", max_width=512, max_height=512, store=True) image_256 = fields.Image("Image 256", related="image_1920", max_width=256, max_height=256, store=True) image_128 = fields.Image("Image 128", related="image_1920", max_width=128, max_height=128, store=True)
class FleetVehicleModel(models.Model): _name = 'fleet.vehicle.model' _description = 'Model of a vehicle' _order = 'name asc' name = fields.Char('Model name', required=True) brand_id = fields.Many2one('fleet.vehicle.model.brand', 'Manufacturer', required=True, help='Manufacturer of the vehicle') vendors = fields.Many2many('res.partner', 'fleet_vehicle_model_vendors', 'model_id', 'partner_id', string='Vendors') manager_id = fields.Many2one( 'res.users', 'Fleet Manager', default=lambda self: self.env.uid, domain=lambda self: [('groups_id', 'in', self.env.ref('fleet.fleet_group_manager').id)]) image_128 = fields.Image(related='brand_id.image_128', readonly=False) @api.depends('name', 'brand_id') def name_get(self): res = [] for record in self: name = record.name if record.brand_id.name: name = record.brand_id.name + '/' + name res.append((record.id, name)) return res
class FleetVehicleModelBrand(models.Model): _name = 'fleet.vehicle.model.brand' _description = 'Brand of the vehicle' _order = 'name asc' name = fields.Char('Make', required=True) image_128 = fields.Image("Logo", max_width=128, max_height=128)
class ModelImage(models.Model): _name = 'test_new_api.model_image' _description = 'Test Image field' name = fields.Char(required=True) image = fields.Image() image_512 = fields.Image("Image 512", related='image', max_width=512, max_height=512, store=True, readonly=False) image_256 = fields.Image("Image 256", related='image', max_width=256, max_height=256, store=False, readonly=False) image_128 = fields.Image("Image 128", max_width=128, max_height=128)
class Sponsor(models.Model): _name = "event.sponsor" _description = 'Event Sponsor' _order = "sequence" event_id = fields.Many2one('event.event', 'Event', required=True) sponsor_type_id = fields.Many2one('event.sponsor.type', 'Sponsoring Type', required=True) partner_id = fields.Many2one('res.partner', 'Sponsor/Customer', required=True) url = fields.Char('Sponsor Website') sequence = fields.Integer('Sequence', store=True, related='sponsor_type_id.sequence', readonly=False) image_128 = fields.Image(string="Logo", related='partner_id.image_128', store=True, readonly=False)
class ProductProduct(models.Model): _name = "product.product" _description = "Product" _inherits = {'product.template': 'product_tmpl_id'} _inherit = ['mail.thread', 'mail.activity.mixin'] _order = 'default_code, name, id' # price: total price, context dependent (partner, pricelist, quantity) price = fields.Float('Price', compute='_compute_product_price', digits='Product Price', inverse='_set_product_price') # price_extra: catalog extra value only, sum of variant extra attributes price_extra = fields.Float( 'Variant Price Extra', compute='_compute_product_price_extra', digits='Product Price', help="This is the sum of the extra price of all attributes") # lst_price: catalog value + extra, context dependent (uom) lst_price = fields.Float( 'Public Price', compute='_compute_product_lst_price', digits='Product Price', inverse='_set_product_lst_price', help= "The sale price is managed from the product template. Click on the 'Configure Variants' button to set the extra attribute prices." ) default_code = fields.Char('Internal Reference', index=True) code = fields.Char('Reference', compute='_compute_product_code') partner_ref = fields.Char('Customer Ref', compute='_compute_partner_ref') active = fields.Boolean( 'Active', default=True, help= "If unchecked, it will allow you to hide the product without removing it." ) product_tmpl_id = fields.Many2one('product.template', 'Product Template', auto_join=True, index=True, ondelete="cascade", required=True) barcode = fields.Char( 'Barcode', copy=False, help="International Article Number used for product identification.") product_template_attribute_value_ids = fields.Many2many( 'product.template.attribute.value', relation='product_variant_combination', string="Attribute Values", ondelete='restrict') combination_indices = fields.Char(compute='_compute_combination_indices', store=True, index=True) is_product_variant = fields.Boolean(compute='_compute_is_product_variant') standard_price = fields.Float( 'Cost', company_dependent=True, digits='Product Price', groups="base.group_user", help= """In Standard Price & AVCO: value of the product (automatically computed in AVCO). In FIFO: value of the last unit that left the stock (automatically computed). Used to value the product when the purchase cost is not known (e.g. inventory adjustment). Used to compute margins on sale orders.""") volume = fields.Float('Volume') weight = fields.Float('Weight', digits='Stock Weight') pricelist_item_count = fields.Integer( "Number of price rules", compute="_compute_variant_item_count") packaging_ids = fields.One2many( 'product.packaging', 'product_id', 'Product Packages', help="Gives the different ways to package the same product.") # all image fields are base64 encoded and PIL-supported # all image_variant fields are technical and should not be displayed to the user image_variant_1920 = fields.Image("Variant Image", max_width=1920, max_height=1920) # resized fields stored (as attachment) for performance image_variant_1024 = fields.Image("Variant Image 1024", related="image_variant_1920", max_width=1024, max_height=1024, store=True) image_variant_512 = fields.Image("Variant Image 512", related="image_variant_1920", max_width=512, max_height=512, store=True) image_variant_256 = fields.Image("Variant Image 256", related="image_variant_1920", max_width=256, max_height=256, store=True) image_variant_128 = fields.Image("Variant Image 128", related="image_variant_1920", max_width=128, max_height=128, store=True) can_image_variant_1024_be_zoomed = fields.Boolean( "Can Variant Image 1024 be zoomed", compute='_compute_can_image_variant_1024_be_zoomed', store=True) # Computed fields that are used to create a fallback to the template if # necessary, it's recommended to display those fields to the user. image_1920 = fields.Image("Image", compute='_compute_image_1920', inverse='_set_image_1920') image_1024 = fields.Image("Image 1024", compute='_compute_image_1024') image_512 = fields.Image("Image 512", compute='_compute_image_512') image_256 = fields.Image("Image 256", compute='_compute_image_256') image_128 = fields.Image("Image 128", compute='_compute_image_128') can_image_1024_be_zoomed = fields.Boolean( "Can Image 1024 be zoomed", compute='_compute_can_image_1024_be_zoomed') @api.depends('image_variant_1920', 'image_variant_1024') def _compute_can_image_variant_1024_be_zoomed(self): for record in self: record.can_image_variant_1024_be_zoomed = record.image_variant_1920 and tools.is_image_size_above( record.image_variant_1920, record.image_variant_1024) def _compute_image_1920(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.image_1920 = record.image_variant_1920 or record.product_tmpl_id.image_1920 def _set_image_1920(self): for record in self: if ( # We are trying to remove an image even though it is already # not set, remove it from the template instead. not record.image_1920 and not record.image_variant_1920 or # We are trying to add an image, but the template image is # not set, write on the template instead. record.image_1920 and not record.product_tmpl_id.image_1920 or # There is only one variant, always write on the template. self.search_count([ ('product_tmpl_id', '=', record.product_tmpl_id.id), ('active', '=', True), ]) <= 1): record.image_variant_1920 = False record.product_tmpl_id.image_1920 = record.image_1920 else: record.image_variant_1920 = record.image_1920 def _compute_image_1024(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.image_1024 = record.image_variant_1024 or record.product_tmpl_id.image_1024 def _compute_image_512(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.image_512 = record.image_variant_512 or record.product_tmpl_id.image_512 def _compute_image_256(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.image_256 = record.image_variant_256 or record.product_tmpl_id.image_256 def _compute_image_128(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.image_128 = record.image_variant_128 or record.product_tmpl_id.image_128 def _compute_can_image_1024_be_zoomed(self): """Get the image from the template if no image is set on the variant.""" for record in self: record.can_image_1024_be_zoomed = record.can_image_variant_1024_be_zoomed if record.image_variant_1920 else record.product_tmpl_id.can_image_1024_be_zoomed def init(self): """Ensure there is at most one active variant for each combination. There could be no variant for a combination if using dynamic attributes. """ self.env.cr.execute( "CREATE UNIQUE INDEX IF NOT EXISTS product_product_combination_unique ON %s (product_tmpl_id, combination_indices) WHERE active is true" % self._table) _sql_constraints = [ ('barcode_uniq', 'unique(barcode)', "A barcode can only be assigned to one product !"), ] def _get_invoice_policy(self): return False @api.depends('product_template_attribute_value_ids') def _compute_combination_indices(self): for product in self: product.combination_indices = product.product_template_attribute_value_ids._ids2str( ) def _compute_is_product_variant(self): for product in self: product.is_product_variant = True @api.depends_context('pricelist', 'partner', 'quantity', 'uom', 'date', 'no_variant_attributes_price_extra') def _compute_product_price(self): prices = {} pricelist_id_or_name = self._context.get('pricelist') if pricelist_id_or_name: pricelist = None partner = self.env.context.get('partner', False) quantity = self.env.context.get('quantity', 1.0) # Support context pricelists specified as display_name or ID for compatibility if isinstance(pricelist_id_or_name, str): pricelist_name_search = self.env[ 'product.pricelist'].name_search(pricelist_id_or_name, operator='=', limit=1) if pricelist_name_search: pricelist = self.env['product.pricelist'].browse( [pricelist_name_search[0][0]]) elif isinstance(pricelist_id_or_name, int): pricelist = self.env['product.pricelist'].browse( pricelist_id_or_name) if pricelist: quantities = [quantity] * len(self) partners = [partner] * len(self) prices = pricelist.get_products_price(self, quantities, partners) for product in self: product.price = prices.get(product.id, 0.0) def _set_product_price(self): for product in self: if self._context.get('uom'): value = self.env['uom.uom'].browse( self._context['uom'])._compute_price( product.price, product.uom_id) else: value = product.price value -= product.price_extra product.write({'list_price': value}) def _set_product_lst_price(self): for product in self: if self._context.get('uom'): value = self.env['uom.uom'].browse( self._context['uom'])._compute_price( product.lst_price, product.uom_id) else: value = product.lst_price value -= product.price_extra product.write({'list_price': value}) def _compute_product_price_extra(self): for product in self: product.price_extra = sum( product.product_template_attribute_value_ids.mapped( 'price_extra')) @api.depends('list_price', 'price_extra') @api.depends_context('uom') def _compute_product_lst_price(self): to_uom = None if 'uom' in self._context: to_uom = self.env['uom.uom'].browse(self._context['uom']) for product in self: if to_uom: list_price = product.uom_id._compute_price( product.list_price, to_uom) else: list_price = product.list_price product.lst_price = list_price + product.price_extra @api.depends_context('partner_id') def _compute_product_code(self): for product in self: for supplier_info in product.seller_ids: if supplier_info.name.id == product._context.get('partner_id'): product.code = supplier_info.product_code or product.default_code break else: product.code = product.default_code @api.depends_context('partner_id') def _compute_partner_ref(self): for product in self: for supplier_info in product.seller_ids: if supplier_info.name.id == product._context.get('partner_id'): product_name = supplier_info.product_name or product.default_code or product.name product.partner_ref = '%s%s' % (product.code and '[%s] ' % product.code or '', product_name) break else: product.partner_ref = product.display_name def _compute_variant_item_count(self): for product in self: domain = [ '|', '&', ('product_tmpl_id', '=', product.product_tmpl_id.id), ('applied_on', '=', '1_product'), '&', ('product_id', '=', product.id), ('applied_on', '=', '0_product_variant') ] product.pricelist_item_count = self.env[ 'product.pricelist.item'].search_count(domain) @api.onchange('uom_id', 'uom_po_id') def _onchange_uom(self): if self.uom_id and self.uom_po_id and self.uom_id.category_id != self.uom_po_id.category_id: self.uom_po_id = self.uom_id @api.model_create_multi def create(self, vals_list): products = super( ProductProduct, self.with_context(create_product_product=True)).create(vals_list) # `_get_variant_id_for_combination` depends on existing variants self.clear_caches() return products def write(self, values): res = super(ProductProduct, self).write(values) if 'product_template_attribute_value_ids' in values: # `_get_variant_id_for_combination` depends on `product_template_attribute_value_ids` self.clear_caches() if 'active' in values: # prefetched o2m have to be reloaded (because of active_test) # (eg. product.template: product_variant_ids) self.invalidate_cache() # `_get_first_possible_variant_id` depends on variants active state self.clear_caches() return res def unlink(self): unlink_products = self.env['product.product'] unlink_templates = self.env['product.template'] for product in self: # If there is an image set on the variant and no image set on the # template, move the image to the template. if product.image_variant_1920 and not product.product_tmpl_id.image_1920: product.product_tmpl_id.image_1920 = product.image_variant_1920 # Check if product still exists, in case it has been unlinked by unlinking its template if not product.exists(): continue # Check if the product is last product of this template... other_products = self.search([('product_tmpl_id', '=', product.product_tmpl_id.id), ('id', '!=', product.id)]) # ... and do not delete product template if it's configured to be created "on demand" if not other_products and not product.product_tmpl_id.has_dynamic_attributes( ): unlink_templates |= product.product_tmpl_id unlink_products |= product res = super(ProductProduct, unlink_products).unlink() # delete templates after calling super, as deleting template could lead to deleting # products due to ondelete='cascade' unlink_templates.unlink() # `_get_variant_id_for_combination` depends on existing variants self.clear_caches() return res def _unlink_or_archive(self, check_access=True): """Unlink or archive products. Try in batch as much as possible because it is much faster. Use dichotomy when an exception occurs. """ # Avoid access errors in case the products is shared amongst companies # but the underlying objects are not. If unlink fails because of an # AccessError (e.g. while recomputing fields), the 'write' call will # fail as well for the same reason since the field has been set to # recompute. if check_access: self.check_access_rights('unlink') self.check_access_rule('unlink') self.check_access_rights('write') self.check_access_rule('write') self = self.sudo() try: with self.env.cr.savepoint(), tools.mute_logger('coffice.sql_db'): self.unlink() except Exception: # We catch all kind of exceptions to be sure that the operation # doesn't fail. if len(self) > 1: self[:len(self) // 2]._unlink_or_archive(check_access=False) self[len(self) // 2:]._unlink_or_archive(check_access=False) else: if self.active: # Note: this can still fail if something is preventing # from archiving. # This is the case from existing stock reordering rules. self.write({'active': False}) @api.returns('self', lambda value: value.id) def copy(self, default=None): """Variants are generated depending on the configuration of attributes and values on the template, so copying them does not make sense. For convenience the template is copied instead and its first variant is returned. """ return self.product_tmpl_id.copy(default=default).product_variant_id @api.model def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None): # TDE FIXME: strange if self._context.get('search_default_categ_id'): args.append((('categ_id', 'child_of', self._context['search_default_categ_id']))) return super(ProductProduct, self)._search(args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid) def name_get(self): # TDE: this could be cleaned a bit I think def _name_get(d): name = d.get('name', '') code = self._context.get('display_default_code', True) and d.get( 'default_code', False) or False if code: name = '[%s] %s' % (code, name) return (d['id'], name) partner_id = self._context.get('partner_id') if partner_id: partner_ids = [ partner_id, self.env['res.partner'].browse( partner_id).commercial_partner_id.id ] else: partner_ids = [] company_id = self.env.context.get('company_id') # all user don't have access to seller and partner # check access and use superuser self.check_access_rights("read") self.check_access_rule("read") result = [] # Prefetch the fields used by the `name_get`, so `browse` doesn't fetch other fields # Use `load=False` to not call `name_get` for the `product_tmpl_id` self.sudo().read(['name', 'default_code', 'product_tmpl_id'], load=False) product_template_ids = self.sudo().mapped('product_tmpl_id').ids if partner_ids: supplier_info = self.env['product.supplierinfo'].sudo().search([ ('product_tmpl_id', 'in', product_template_ids), ('name', 'in', partner_ids), ]) # Prefetch the fields used by the `name_get`, so `browse` doesn't fetch other fields # Use `load=False` to not call `name_get` for the `product_tmpl_id` and `product_id` supplier_info.sudo().read([ 'product_tmpl_id', 'product_id', 'product_name', 'product_code' ], load=False) supplier_info_by_template = {} for r in supplier_info: supplier_info_by_template.setdefault(r.product_tmpl_id, []).append(r) for product in self.sudo(): variant = product.product_template_attribute_value_ids._get_combination_name( ) name = variant and "%s (%s)" % (product.name, variant) or product.name sellers = [] if partner_ids: product_supplier_info = supplier_info_by_template.get( product.product_tmpl_id, []) sellers = [ x for x in product_supplier_info if x.product_id and x.product_id == product ] if not sellers: sellers = [ x for x in product_supplier_info if not x.product_id ] # Filter out sellers based on the company. This is done afterwards for a better # code readability. At this point, only a few sellers should remain, so it should # not be a performance issue. if company_id: sellers = [ x for x in sellers if x.company_id.id in [company_id, False] ] if sellers: for s in sellers: seller_variant = s.product_name and ( variant and "%s (%s)" % (s.product_name, variant) or s.product_name) or False mydict = { 'id': product.id, 'name': seller_variant or name, 'default_code': s.product_code or product.default_code, } temp = _name_get(mydict) if temp not in result: result.append(temp) else: mydict = { 'id': product.id, 'name': name, 'default_code': product.default_code, } result.append(_name_get(mydict)) return result @api.model def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): if not args: args = [] if name: positive_operators = ['=', 'ilike', '=ilike', 'like', '=like'] product_ids = [] if operator in positive_operators: product_ids = self._search([('default_code', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid) if not product_ids: product_ids = self._search([('barcode', '=', name)] + args, limit=limit, access_rights_uid=name_get_uid) if not product_ids and operator not in expression.NEGATIVE_TERM_OPERATORS: # Do not merge the 2 next lines into one single search, SQL search performance would be abysmal # on a database with thousands of matching products, due to the huge merge+unique needed for the # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table # Performing a quick memory merge of ids in Python will give much better performance product_ids = self._search(args + [('default_code', operator, name)], limit=limit) if not limit or len(product_ids) < limit: # we may underrun the limit because of dupes in the results, that's fine limit2 = (limit - len(product_ids)) if limit else False product2_ids = self._search( args + [('name', operator, name), ('id', 'not in', product_ids)], limit=limit2, access_rights_uid=name_get_uid) product_ids.extend(product2_ids) elif not product_ids and operator in expression.NEGATIVE_TERM_OPERATORS: domain = expression.OR([ [ '&', ('default_code', operator, name), ('name', operator, name) ], [ '&', ('default_code', '=', False), ('name', operator, name) ], ]) domain = expression.AND([args, domain]) product_ids = self._search(domain, limit=limit, access_rights_uid=name_get_uid) if not product_ids and operator in positive_operators: ptrn = re.compile('(\[(.*?)\])') res = ptrn.search(name) if res: product_ids = self._search( [('default_code', '=', res.group(2))] + args, limit=limit, access_rights_uid=name_get_uid) # still no results, partner in context: search on supplier info as last hope to find something if not product_ids and self._context.get('partner_id'): suppliers_ids = self.env['product.supplierinfo']._search( [('name', '=', self._context.get('partner_id')), '|', ('product_code', operator, name), ('product_name', operator, name)], access_rights_uid=name_get_uid) if suppliers_ids: product_ids = self._search( [('product_tmpl_id.seller_ids', 'in', suppliers_ids)], limit=limit, access_rights_uid=name_get_uid) else: product_ids = self._search(args, limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get( self.browse(product_ids).with_user(name_get_uid)) @api.model def view_header_get(self, view_id, view_type): res = super(ProductProduct, self).view_header_get(view_id, view_type) if self._context.get('categ_id'): return _('Products: ') + self.env['product.category'].browse( self._context['categ_id']).name return res def open_pricelist_rules(self): self.ensure_one() domain = [ '|', '&', ('product_tmpl_id', '=', self.product_tmpl_id.id), ('applied_on', '=', '1_product'), '&', ('product_id', '=', self.id), ('applied_on', '=', '0_product_variant') ] return { 'name': _('Price Rules'), 'view_mode': 'tree,form', 'views': [(self.env.ref( 'product.product_pricelist_item_tree_view_from_product').id, 'tree'), (False, 'form')], 'res_model': 'product.pricelist.item', 'type': 'ir.actions.act_window', 'target': 'current', 'domain': domain, 'context': { 'default_product_id': self.id, 'default_applied_on': '0_product_variant', } } def open_product_template(self): """ Utility method used to add an "Open Template" button in product views """ self.ensure_one() return { 'type': 'ir.actions.act_window', 'res_model': 'product.template', 'view_mode': 'form', 'res_id': self.product_tmpl_id.id, 'target': 'new' } def _prepare_sellers(self, params): return self.seller_ids.filtered(lambda s: s.name.active).sorted( lambda s: (s.sequence, -s.min_qty, s.price)) def _select_seller(self, partner_id=False, quantity=0.0, date=None, uom_id=False, params=False): self.ensure_one() if date is None: date = fields.Date.context_today(self) precision = self.env['decimal.precision'].precision_get( 'Product Unit of Measure') res = self.env['product.supplierinfo'] sellers = self._prepare_sellers(params) if self.env.context.get('force_company'): sellers = sellers.filtered( lambda s: not s.company_id or s.company_id.id == self.env. context['force_company']) for seller in sellers: # Set quantity in UoM of seller quantity_uom_seller = quantity if quantity_uom_seller and uom_id and uom_id != seller.product_uom: quantity_uom_seller = uom_id._compute_quantity( quantity_uom_seller, seller.product_uom) if seller.date_start and seller.date_start > date: continue if seller.date_end and seller.date_end < date: continue if partner_id and seller.name not in [ partner_id, partner_id.parent_id ]: continue if float_compare(quantity_uom_seller, seller.min_qty, precision_digits=precision) == -1: continue if seller.product_id and seller.product_id != self: continue if not res or res.name == seller.name: res |= seller return res.sorted('price')[:1] def price_compute(self, price_type, uom=False, currency=False, company=False): # TDE FIXME: delegate to template or not ? fields are reencoded here ... # compatibility about context keys used a bit everywhere in the code if not uom and self._context.get('uom'): uom = self.env['uom.uom'].browse(self._context['uom']) if not currency and self._context.get('currency'): currency = self.env['res.currency'].browse( self._context['currency']) products = self if price_type == 'standard_price': # standard_price field can only be seen by users in base.group_user # Thus, in order to compute the sale price from the cost for users not in this group # We fetch the standard price as the superuser products = self.with_context( force_company=company and company.id or self._context.get( 'force_company', self.env.company.id)).sudo() prices = dict.fromkeys(self.ids, 0.0) for product in products: prices[product.id] = product[price_type] or 0.0 if price_type == 'list_price': prices[product.id] += product.price_extra # we need to add the price from the attributes that do not generate variants # (see field product.attribute create_variant) if self._context.get('no_variant_attributes_price_extra'): # we have a list of price_extra that comes from the attribute values, we need to sum all that prices[product.id] += sum( self._context.get('no_variant_attributes_price_extra')) if uom: prices[product.id] = product.uom_id._compute_price( prices[product.id], uom) # Convert from current user company currency to asked one # This is right cause a field cannot be in more than one currency if currency: prices[product.id] = product.currency_id._convert( prices[product.id], currency, product.company_id, fields.Date.today()) return prices @api.model def get_empty_list_help(self, help): self = self.with_context(empty_list_help_document_name=_("product"), ) return super(ProductProduct, self).get_empty_list_help(help) def get_product_multiline_description_sale(self): """ Compute a multiline description of this product, in the context of sales (do not use for purchases or other display reasons that don't intend to use "description_sale"). It will often be used as the default description of a sale order line referencing this product. """ name = self.display_name if self.description_sale: name += '\n' + self.description_sale return name def _is_variant_possible(self, parent_combination=None): """Return whether the variant is possible based on its own combination, and optionally a parent combination. See `_is_combination_possible` for more information. This will always exclude variants for templates that have `no_variant` attributes because the variant itself will not be the full combination. :param parent_combination: combination from which `self` is an optional or accessory product. :type parent_combination: recordset `product.template.attribute.value` :return: ẁhether the variant is possible based on its own combination :rtype: bool """ self.ensure_one() return self.product_tmpl_id._is_combination_possible( self.product_template_attribute_value_ids, parent_combination=parent_combination) def toggle_active(self): """ Archiving related product.template if there is only one active product.product """ with_one_active = self.filtered(lambda product: len( product.product_tmpl_id.product_variant_ids) == 1) for product in with_one_active: product.product_tmpl_id.toggle_active() return super(ProductProduct, self - with_one_active).toggle_active()
class FleetVehicle(models.Model): _inherit = ['mail.thread', 'mail.activity.mixin'] _name = 'fleet.vehicle' _description = 'Vehicle' _order = 'license_plate asc, acquisition_date asc' def _get_default_state(self): state = self.env.ref('fleet.fleet_vehicle_state_registered', raise_if_not_found=False) return state if state and state.id else False name = fields.Char(compute="_compute_vehicle_name", store=True) active = fields.Boolean('Active', default=True, tracking=True) company_id = fields.Many2one('res.company', 'Company', default=lambda self: self.env.company) currency_id = fields.Many2one('res.currency', related='company_id.currency_id') license_plate = fields.Char( tracking=True, help='License plate number of the vehicle (i = plate number for a car)' ) vin_sn = fields.Char( 'Chassis Number', help='Unique number written on the vehicle motor (VIN/SN number)', copy=False) driver_id = fields.Many2one('res.partner', 'Driver', tracking=True, help='Driver of the vehicle', copy=False) future_driver_id = fields.Many2one( 'res.partner', 'Future Driver', tracking=True, help='Next Driver of the vehicle', copy=False, domain= "['|', ('company_id', '=', False), ('company_id', '=', company_id)]") model_id = fields.Many2one('fleet.vehicle.model', 'Model', tracking=True, required=True, help='Model of the vehicle') manager_id = fields.Many2one('res.users', related='model_id.manager_id') brand_id = fields.Many2one('fleet.vehicle.model.brand', 'Brand', related="model_id.brand_id", store=True, readonly=False) log_drivers = fields.One2many('fleet.vehicle.assignation.log', 'vehicle_id', string='Assignation Logs') log_fuel = fields.One2many('fleet.vehicle.log.fuel', 'vehicle_id', 'Fuel Logs') log_services = fields.One2many('fleet.vehicle.log.services', 'vehicle_id', 'Services Logs') log_contracts = fields.One2many('fleet.vehicle.log.contract', 'vehicle_id', 'Contracts') cost_count = fields.Integer(compute="_compute_count_all", string="Costs") contract_count = fields.Integer(compute="_compute_count_all", string='Contract Count') service_count = fields.Integer(compute="_compute_count_all", string='Services') fuel_logs_count = fields.Integer(compute="_compute_count_all", string='Fuel Log Count') odometer_count = fields.Integer(compute="_compute_count_all", string='Odometer') history_count = fields.Integer(compute="_compute_count_all", string="Drivers History Count") next_assignation_date = fields.Date( 'Assignation Date', help= 'This is the date at which the car will be available, if not set it means available instantly' ) acquisition_date = fields.Date( 'Immatriculation Date', required=False, default=fields.Date.today, help='Date when the vehicle has been immatriculated') first_contract_date = fields.Date(string="First Contract Date", default=fields.Date.today) color = fields.Char(help='Color of the vehicle') state_id = fields.Many2one('fleet.vehicle.state', 'State', default=_get_default_state, group_expand='_read_group_stage_ids', tracking=True, help='Current state of the vehicle', ondelete="set null") location = fields.Char(help='Location of the vehicle (garage, ...)') seats = fields.Integer('Seats Number', help='Number of seats of the vehicle') model_year = fields.Char('Model Year', help='Year of the model') doors = fields.Integer('Doors Number', help='Number of doors of the vehicle', default=5) tag_ids = fields.Many2many('fleet.vehicle.tag', 'fleet_vehicle_vehicle_tag_rel', 'vehicle_tag_id', 'tag_id', 'Tags', copy=False) odometer = fields.Float( compute='_get_odometer', inverse='_set_odometer', string='Last Odometer', help='Odometer measure of the vehicle at the moment of this log') odometer_unit = fields.Selection([('kilometers', 'Kilometers'), ('miles', 'Miles')], 'Odometer Unit', default='kilometers', help='Unit of the odometer ', required=True) transmission = fields.Selection([('manual', 'Manual'), ('automatic', 'Automatic')], 'Transmission', help='Transmission Used by the vehicle') fuel_type = fields.Selection([('gasoline', 'Gasoline'), ('diesel', 'Diesel'), ('lpg', 'LPG'), ('electric', 'Electric'), ('hybrid', 'Hybrid')], 'Fuel Type', help='Fuel Used by the vehicle') horsepower = fields.Integer() horsepower_tax = fields.Float('Horsepower Taxation') power = fields.Integer('Power', help='Power in kW of the vehicle') co2 = fields.Float('CO2 Emissions', help='CO2 emissions of the vehicle') image_128 = fields.Image(related='model_id.image_128', readonly=False) contract_renewal_due_soon = fields.Boolean( compute='_compute_contract_reminder', search='_search_contract_renewal_due_soon', string='Has Contracts to renew', multi='contract_info') contract_renewal_overdue = fields.Boolean( compute='_compute_contract_reminder', search='_search_get_overdue_contract_reminder', string='Has Contracts Overdue', multi='contract_info') contract_renewal_name = fields.Text( compute='_compute_contract_reminder', string='Name of contract to renew soon', multi='contract_info') contract_renewal_total = fields.Text( compute='_compute_contract_reminder', string='Total of contracts due or overdue minus one', multi='contract_info') car_value = fields.Float(string="Catalog Value (VAT Incl.)", help='Value of the bought vehicle') net_car_value = fields.Float(string="Purchase Value", help="Purchase Value of the car") residual_value = fields.Float() plan_to_change_car = fields.Boolean(related='driver_id.plan_to_change_car', store=True, readonly=False) @api.depends('model_id.brand_id.name', 'model_id.name', 'license_plate') def _compute_vehicle_name(self): for record in self: record.name = (record.model_id.brand_id.name or '') + '/' + ( record.model_id.name or '') + '/' + (record.license_plate or _('No Plate')) def _get_odometer(self): FleetVehicalOdometer = self.env['fleet.vehicle.odometer'] for record in self: vehicle_odometer = FleetVehicalOdometer.search( [('vehicle_id', '=', record.id)], limit=1, order='value desc') if vehicle_odometer: record.odometer = vehicle_odometer.value else: record.odometer = 0 def _set_odometer(self): for record in self: if record.odometer: date = fields.Date.context_today(record) data = { 'value': record.odometer, 'date': date, 'vehicle_id': record.id } self.env['fleet.vehicle.odometer'].create(data) def _compute_count_all(self): Odometer = self.env['fleet.vehicle.odometer'] LogFuel = self.env['fleet.vehicle.log.fuel'] LogService = self.env['fleet.vehicle.log.services'] LogContract = self.env['fleet.vehicle.log.contract'] Cost = self.env['fleet.vehicle.cost'] for record in self: record.odometer_count = Odometer.search_count([('vehicle_id', '=', record.id)]) record.fuel_logs_count = LogFuel.search_count([('vehicle_id', '=', record.id)]) record.service_count = LogService.search_count([('vehicle_id', '=', record.id)]) record.contract_count = LogContract.search_count([ ('vehicle_id', '=', record.id), ('state', '!=', 'closed') ]) record.cost_count = Cost.search_count([('vehicle_id', '=', record.id), ('parent_id', '=', False)]) record.history_count = self.env[ 'fleet.vehicle.assignation.log'].search_count([ ('vehicle_id', '=', record.id) ]) @api.depends('log_contracts') def _compute_contract_reminder(self): params = self.env['ir.config_parameter'].sudo() delay_alert_contract = int( params.get_param('hr_fleet.delay_alert_contract', default=30)) for record in self: overdue = False due_soon = False total = 0 name = '' for element in record.log_contracts: if element.state in ('open', 'diesoon', 'expired') and element.expiration_date: current_date_str = fields.Date.context_today(record) due_time_str = element.expiration_date current_date = fields.Date.from_string(current_date_str) due_time = fields.Date.from_string(due_time_str) diff_time = (due_time - current_date).days if diff_time < 0: overdue = True total += 1 if diff_time < delay_alert_contract: due_soon = True total += 1 if overdue or due_soon: log_contract = self.env[ 'fleet.vehicle.log.contract'].search( [('vehicle_id', '=', record.id), ('state', 'in', ('open', 'diesoon', 'expired'))], limit=1, order='expiration_date asc') if log_contract: # we display only the name of the oldest overdue/due soon contract name = log_contract.cost_subtype_id.name record.contract_renewal_overdue = overdue record.contract_renewal_due_soon = due_soon record.contract_renewal_total = total - 1 # we remove 1 from the real total for display purposes record.contract_renewal_name = name def _search_contract_renewal_due_soon(self, operator, value): params = self.env['ir.config_parameter'].sudo() delay_alert_contract = int( params.get_param('hr_fleet.delay_alert_contract', default=30)) res = [] assert operator in ('=', '!=', '<>') and value in ( True, False), 'Operation not supported' if (operator == '=' and value is True) or (operator in ('<>', '!=') and value is False): search_operator = 'in' else: search_operator = 'not in' today = fields.Date.context_today(self) datetime_today = fields.Datetime.from_string(today) limit_date = fields.Datetime.to_string(datetime_today + relativedelta( days=+delay_alert_contract)) self.env.cr.execute( """SELECT cost.vehicle_id, count(contract.id) AS contract_number FROM fleet_vehicle_cost cost LEFT JOIN fleet_vehicle_log_contract contract ON contract.cost_id = cost.id WHERE contract.expiration_date IS NOT NULL AND contract.expiration_date > %s AND contract.expiration_date < %s AND contract.state IN ('open', 'diesoon', 'expired') GROUP BY cost.vehicle_id""", (today, limit_date)) res_ids = [x[0] for x in self.env.cr.fetchall()] res.append(('id', search_operator, res_ids)) return res def _search_get_overdue_contract_reminder(self, operator, value): res = [] assert operator in ('=', '!=', '<>') and value in ( True, False), 'Operation not supported' if (operator == '=' and value is True) or (operator in ('<>', '!=') and value is False): search_operator = 'in' else: search_operator = 'not in' today = fields.Date.context_today(self) self.env.cr.execute( '''SELECT cost.vehicle_id, count(contract.id) AS contract_number FROM fleet_vehicle_cost cost LEFT JOIN fleet_vehicle_log_contract contract ON contract.cost_id = cost.id WHERE contract.expiration_date IS NOT NULL AND contract.expiration_date < %s AND contract.state IN ('open', 'diesoon', 'expired') GROUP BY cost.vehicle_id ''', (today, )) res_ids = [x[0] for x in self.env.cr.fetchall()] res.append(('id', search_operator, res_ids)) return res @api.model def create(self, vals): res = super(FleetVehicle, self).create(vals) if 'driver_id' in vals and vals['driver_id']: res.create_driver_history(vals['driver_id']) if 'future_driver_id' in vals and vals['future_driver_id']: future_driver = self.env['res.partner'].browse( vals['future_driver_id']) future_driver.write({'plan_to_change_car': True}) return res def write(self, vals): if 'driver_id' in vals and vals['driver_id']: driver_id = vals['driver_id'] self.filtered(lambda v: v.driver_id.id != driver_id ).create_driver_history(driver_id) if 'future_driver_id' in vals and vals['future_driver_id']: future_driver = self.env['res.partner'].browse( vals['future_driver_id']) future_driver.write({'plan_to_change_car': True}) res = super(FleetVehicle, self).write(vals) if 'active' in vals and not vals['active']: self.mapped('log_contracts').write({'active': False}) return res def _close_driver_history(self): self.env['fleet.vehicle.assignation.log'].search([ ('vehicle_id', 'in', self.ids), ('driver_id', 'in', self.mapped('driver_id').ids), ('date_end', '=', False) ]).write({'date_end': fields.Date.today()}) def create_driver_history(self, driver_id): for vehicle in self: self.env['fleet.vehicle.assignation.log'].create({ 'vehicle_id': vehicle.id, 'driver_id': driver_id, 'date_start': fields.Date.today(), }) def action_accept_driver_change(self): # Find all the vehicles for which the driver is the future_driver_id # remove their driver_id and close their history using current date vehicles = self.search([('driver_id', 'in', self.mapped('future_driver_id').ids)]) vehicles.write({'driver_id': False}) vehicles._close_driver_history() for vehicle in self: vehicle.future_driver_id.write({'plan_to_change_car': False}) vehicle.driver_id = vehicle.future_driver_id vehicle.future_driver_id = False @api.model def _read_group_stage_ids(self, stages, domain, order): return self.env['fleet.vehicle.state'].search([], order=order) @api.model def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): args = args or [] if operator == 'ilike' and not (name or '').strip(): domain = [] else: domain = [ '|', ('name', operator, name), ('driver_id.name', operator, name) ] rec = self._search(expression.AND([domain, args]), limit=limit, access_rights_uid=name_get_uid) return models.lazy_name_get(self.browse(rec).with_user(name_get_uid)) def return_action_to_open(self): """ This opens the xml view specified in xml_id for the current vehicle """ self.ensure_one() xml_id = self.env.context.get('xml_id') if xml_id: res = self.env['ir.actions.act_window'].for_xml_id('fleet', xml_id) res.update(context=dict(self.env.context, default_vehicle_id=self.id, group_by=False), domain=[('vehicle_id', '=', self.id)]) return res return False def act_show_log_cost(self): """ This opens log view to view and add new log for this vehicle, groupby default to only show effective costs @return: the costs log view """ self.ensure_one() copy_context = dict(self.env.context) copy_context.pop('group_by', None) res = self.env['ir.actions.act_window'].for_xml_id( 'fleet', 'fleet_vehicle_costs_action') res.update(context=dict(copy_context, default_vehicle_id=self.id, search_default_parent_false=True), domain=[('vehicle_id', '=', self.id)]) return res def _track_subtype(self, init_values): self.ensure_one() if 'driver_id' in init_values: return self.env.ref('fleet.mt_fleet_driver_updated') return super(FleetVehicle, self)._track_subtype(init_values) def open_assignation_logs(self): self.ensure_one() return { 'type': 'ir.actions.act_window', 'name': 'Assignation Logs', 'view_mode': 'tree', 'res_model': 'fleet.vehicle.assignation.log', 'domain': [('vehicle_id', '=', self.id)], 'context': { 'default_driver_id': self.driver_id.id, 'default_vehicle_id': self.id } }
class Track(models.Model): _name = "event.track" _description = 'Event Track' _order = 'priority, date' _inherit = [ 'mail.thread', 'mail.activity.mixin', 'website.seo.metadata', 'website.published.mixin' ] @api.model def _get_default_stage_id(self): return self.env['event.track.stage'].search([], limit=1).id name = fields.Char('Title', required=True, translate=True) active = fields.Boolean(default=True) user_id = fields.Many2one('res.users', 'Responsible', tracking=True, default=lambda self: self.env.user) company_id = fields.Many2one('res.company', related='event_id.company_id') partner_id = fields.Many2one('res.partner', 'Speaker') partner_name = fields.Char('Name') partner_email = fields.Char('Email') partner_phone = fields.Char('Phone') partner_biography = fields.Html('Biography') tag_ids = fields.Many2many('event.track.tag', string='Tags') stage_id = fields.Many2one('event.track.stage', string='Stage', ondelete='restrict', index=True, copy=False, default=_get_default_stage_id, group_expand='_read_group_stage_ids', required=True, tracking=True) kanban_state = fields.Selection( [('normal', 'Grey'), ('done', 'Green'), ('blocked', 'Red')], string='Kanban State', copy=False, default='normal', required=True, tracking=True, help= "A track's kanban state indicates special situations affecting it:\n" " * Grey is the default situation\n" " * Red indicates something is preventing the progress of this track\n" " * Green indicates the track is ready to be pulled to the next stage") description = fields.Html(translate=html_translate, sanitize_attributes=False) date = fields.Datetime('Track Date') date_end = fields.Datetime('Track End Date', compute='_compute_end_date', store=True) duration = fields.Float('Duration', default=1.5, help="Track duration in hours.") location_id = fields.Many2one('event.track.location', 'Room') event_id = fields.Many2one('event.event', 'Event', required=True) color = fields.Integer('Color Index') priority = fields.Selection([('0', 'Low'), ('1', 'Medium'), ('2', 'High'), ('3', 'Highest')], 'Priority', required=True, default='1') image = fields.Image("Image", related='partner_id.image_128', store=True, readonly=False) @api.depends('name') def _compute_website_url(self): super(Track, self)._compute_website_url() for track in self: if track.id: track.website_url = '/event/%s/track/%s' % (slug( track.event_id), slug(track)) @api.onchange('partner_id') def _onchange_partner_id(self): if self.partner_id: self.partner_name = self.partner_id.name self.partner_email = self.partner_id.email self.partner_phone = self.partner_id.phone self.partner_biography = self.partner_id.website_description @api.depends('date', 'duration') def _compute_end_date(self): for track in self: if track.date: delta = timedelta(minutes=60 * track.duration) track.date_end = track.date + delta else: track.date_end = False @api.model def create(self, vals): track = super(Track, self).create(vals) track.event_id.message_post_with_view( 'website_event_track.event_track_template_new', values={'track': track}, subject=track.name, subtype_id=self.env.ref('website_event_track.mt_event_track').id, ) return track def write(self, vals): if 'stage_id' in vals and 'kanban_state' not in vals: vals['kanban_state'] = 'normal' res = super(Track, self).write(vals) if vals.get('partner_id'): self.message_subscribe([vals['partner_id']]) return res @api.model def _read_group_stage_ids(self, stages, domain, order): """ Always display all stages """ return stages.search([], order=order) def _track_template(self, changes): res = super(Track, self)._track_template(changes) track = self[0] if 'stage_id' in changes and track.stage_id.mail_template_id: res['stage_id'] = (track.stage_id.mail_template_id, { 'composition_mode': 'comment', 'auto_delete_message': True, 'subtype_id': self.env['ir.model.data'].xmlid_to_res_id('mail.mt_note'), 'email_layout_xmlid': 'mail.mail_notification_light' }) return res def _track_subtype(self, init_values): self.ensure_one() if 'kanban_state' in init_values and self.kanban_state == 'blocked': return self.env.ref('website_event_track.mt_track_blocked') elif 'kanban_state' in init_values and self.kanban_state == 'done': return self.env.ref('website_event_track.mt_track_ready') return super(Track, self)._track_subtype(init_values) def _message_get_suggested_recipients(self): recipients = super(Track, self)._message_get_suggested_recipients() for track in self: if track.partner_email and track.partner_email != track.partner_id.email: track._message_add_suggested_recipient( recipients, email=track.partner_email, reason=_('Speaker Email')) return recipients def _message_post_after_hook(self, message, msg_vals): if self.partner_email and not self.partner_id: # we consider that posting a message with a specified recipient (not a follower, a specific one) # on a document without customer means that it was created through the chatter using # suggested recipients. This heuristic allows to avoid ugly hacks in JS. new_partner = message.partner_ids.filtered( lambda partner: partner.email == self.partner_email) if new_partner: self.search([ ('partner_id', '=', False), ('partner_email', '=', new_partner.email), ('stage_id.is_cancel', '=', False), ]).write({'partner_id': new_partner.id}) return super(Track, self)._message_post_after_hook(message, msg_vals) def open_track_speakers_list(self): return { 'name': _('Speakers'), 'domain': [('id', 'in', self.mapped('partner_id').ids)], 'view_mode': 'kanban,form', 'res_model': 'res.partner', 'view_id': False, 'type': 'ir.actions.act_window', }
class HrEmployeePublic(models.Model): _name = "hr.employee.public" _inherit = ["hr.employee.base"] _description = 'Public Employee' _order = 'name' _auto = False _log_access = True # Include magic fields # Fields coming from hr.employee.base create_date = fields.Datetime(readonly=True) name = fields.Char(readonly=True) active = fields.Boolean(readonly=True) department_id = fields.Many2one(readonly=True) job_id = fields.Many2one(readonly=True) job_title = fields.Char(readonly=True) company_id = fields.Many2one(readonly=True) address_id = fields.Many2one(readonly=True) mobile_phone = fields.Char(readonly=True) work_phone = fields.Char(readonly=True) work_email = fields.Char(readonly=True) work_location = fields.Char(readonly=True) user_id = fields.Many2one(readonly=True) resource_id = fields.Many2one(readonly=True) resource_calendar_id = fields.Many2one(readonly=True) tz = fields.Selection(readonly=True) color = fields.Integer(readonly=True) # hr.employee.public specific fields child_ids = fields.One2many('hr.employee.public', 'parent_id', string='Direct subordinates', readonly=True) image_1920 = fields.Image("Original Image", compute='_compute_image', compute_sudo=True) image_1024 = fields.Image("Image 1024", compute='_compute_image', compute_sudo=True) image_512 = fields.Image("Image 512", compute='_compute_image', compute_sudo=True) image_256 = fields.Image("Image 256", compute='_compute_image', compute_sudo=True) image_128 = fields.Image("Image 128", compute='_compute_image', compute_sudo=True) parent_id = fields.Many2one('hr.employee.public', 'Manager', readonly=True) coach_id = fields.Many2one('hr.employee.public', 'Coach', readonly=True) def _compute_image(self): for employee in self: # We have to be in sudo to have access to the images employee_id = self.sudo().env['hr.employee'].browse(employee.id) employee.image_1920 = employee_id.image_1920 employee.image_1024 = employee_id.image_1024 employee.image_512 = employee_id.image_512 employee.image_256 = employee_id.image_256 employee.image_128 = employee_id.image_128 @api.model def _get_fields(self): return ','.join( 'emp.%s' % name for name, field in self._fields.items() if field.store and field.type not in ['many2many', 'one2many']) def init(self): tools.drop_view_if_exists(self.env.cr, self._table) self.env.cr.execute("""CREATE or REPLACE VIEW %s as ( SELECT %s FROM hr_employee emp )""" % (self._table, self._get_fields()))
class LunchProductReport(models.Model): _name = "lunch.product.report" _description = 'Product report' _auto = False _order = 'is_favorite desc, is_new desc, last_order_date asc, product_id asc' id = fields.Integer('ID') product_id = fields.Many2one('lunch.product', 'Product') name = fields.Char('Product Name') category_id = fields.Many2one('lunch.product.category', 'Product Category') description = fields.Text('Description') price = fields.Float('Price') supplier_id = fields.Many2one('lunch.supplier', 'Vendor') company_id = fields.Many2one('res.company') currency_id = fields.Many2one('res.currency', related='company_id.currency_id') is_favorite = fields.Boolean('Favorite') user_id = fields.Many2one('res.users') is_new = fields.Boolean('New') active = fields.Boolean('Active') last_order_date = fields.Date('Last Order Date') image_128 = fields.Image(compute="_compute_image_128") # This field is used only for searching is_available_at = fields.Many2one('lunch.location', 'Product Availability', compute='_compute_is_available_at', search='_search_is_available_at') def _compute_image_128(self): for product_r in self: product = product_r.product_id category = product_r.category_id if product.image_128: product_r.image_128 = product.image_128 elif category.image_128: product_r.image_128 = category.image_128 else: product_r.image_128 = False def _compute_is_available_at(self): """ Is available_at is always false when browsing it this field is there only to search (see _search_is_available_at) """ for product in self: product.is_available_at = False def _search_is_available_at(self, operator, value): supported_operators = ['in', 'not in', '=', '!='] if not operator in supported_operators: return expression.TRUE_DOMAIN if isinstance(value, int): value = [value] if operator in expression.NEGATIVE_TERM_OPERATORS: return expression.AND([[('supplier_id.available_location_ids', 'not in', value)], [('supplier_id.available_location_ids', '!=', False)]]) return expression.OR([[('supplier_id.available_location_ids', 'in', value)], [('supplier_id.available_location_ids', '=', False)]]) def write(self, values): if 'is_favorite' in values: if values['is_favorite']: commands = [(4, product_id) for product_id in self.mapped('product_id').ids] else: commands = [(3, product_id) for product_id in self.mapped('product_id').ids] self.env.user.write({ 'favorite_lunch_product_ids': commands, }) def init(self): tools.drop_view_if_exists(self._cr, self._table) self._cr.execute(""" CREATE or REPLACE view %s AS ( SELECT row_number() over (ORDER BY users.id,product.id) AS id, product.id AS product_id, product.name, product.category_id, product.description, product.price, product.supplier_id, product.company_id, product.active, users.id AS user_id, fav.user_id IS NOT NULL AS is_favorite, product.new_until >= current_date AS is_new, orders.last_order_date FROM lunch_product product INNER JOIN res_users users ON product.company_id IS NULL OR users.company_id = product.company_id -- multi company INNER JOIN res_groups_users_rel groups ON groups.uid = users.id -- only generate for internal users LEFT JOIN LATERAL (select max(date) AS last_order_date FROM lunch_order where user_id=users.id and product_id=product.id) AS orders ON TRUE LEFT JOIN LATERAL (select user_id FROM lunch_product_favorite_user_rel where user_id=users.id and product_id=product.id) AS fav ON TRUE WHERE users.active AND product.active AND groups.gid = %%s --only take into account active products and users ); """ % self._table, (self.env.ref('base.group_user').id,))
class LunchOrderWizard(models.TransientModel): _name = 'lunch.order.temp' _description = 'Lunch Order Temp' def _default_order_line(self): line_id = self.env.context.get('line_id') if line_id: last_time_ordered = self.env['lunch.order'].browse(line_id) else: last_time_ordered = self.env['lunch.order'].search( [('product_id', '=', self.env.context.get('default_product_id', 0)), ('user_id', '=', self.env.context.get('default_user_id', self.env.user.id))], order="date desc, id desc", limit=1) return last_time_ordered currency_id = fields.Many2one( 'res.currency', default=lambda self: self.env.company.currency_id) product_id = fields.Many2one('lunch.product', string='Product') product_description = fields.Text('Description', related='product_id.description') product_name = fields.Char('Product Name', related='product_id.name') product_category = fields.Many2one('lunch.product.category', related='product_id.category_id') topping_label_1 = fields.Char( related='product_id.category_id.topping_label_1') topping_label_2 = fields.Char( related='product_id.category_id.topping_label_2') topping_label_3 = fields.Char( related='product_id.category_id.topping_label_3') topping_quantity_1 = fields.Selection( related='product_id.category_id.topping_quantity_1') topping_quantity_2 = fields.Selection( related='product_id.category_id.topping_quantity_2') topping_quantity_3 = fields.Selection( related='product_id.category_id.topping_quantity_3') topping_ids_1 = fields.Many2many( 'lunch.topping', 'lunch_order_temp_topping', 'order_id', 'topping_id', string="Extra Garniture", domain= "[('category_id', '=', product_category), ('topping_category', '=', 1)]", default=lambda self: self._default_order_line().topping_ids_1) topping_ids_2 = fields.Many2many( 'lunch.topping', 'lunch_order_temp_topping', 'order_id', 'topping_id', string="Extra Garniture 2", domain= "[('category_id', '=', product_category), ('topping_category', '=', 2)]", default=lambda self: self._default_order_line().topping_ids_2) topping_ids_3 = fields.Many2many( 'lunch.topping', 'lunch_order_temp_topping', 'order_id', 'topping_id', string="Extra Garniture 3", domain= "[('category_id', '=', product_category), ('topping_category', '=', 3)]", default=lambda self: self._default_order_line().topping_ids_3) available_toppings_1 = fields.Boolean( help='Are extras available for this product', compute='_compute_available_toppings') available_toppings_2 = fields.Boolean( help='Are extras available for this product', compute='_compute_available_toppings') available_toppings_3 = fields.Boolean( help='Are extras available for this product', compute='_compute_available_toppings') image_1920 = fields.Image(related='product_id.image_1920') image_128 = fields.Image(related='product_id.image_128') quantity = fields.Float('Quantity', default=1) price_total = fields.Float('Total Price', compute='_compute_price_total') note = fields.Text('Special Instructions', default=lambda self: self._default_order_line().note) user_id = fields.Many2one('res.users', default=lambda self: self.env.user.id) edit = fields.Boolean( 'Edit Mode', default=lambda self: bool(self.env.context.get('line_id'))) @api.depends('product_id') def _compute_available_toppings(self): for wizard in self: wizard.available_toppings_1 = bool( wizard.env['lunch.topping'].search_count([ ('category_id', '=', wizard.product_category.id), ('topping_category', '=', 1) ])) wizard.available_toppings_2 = bool( wizard.env['lunch.topping'].search_count([ ('category_id', '=', wizard.product_category.id), ('topping_category', '=', 2) ])) wizard.available_toppings_3 = bool( wizard.env['lunch.topping'].search_count([ ('category_id', '=', wizard.product_category.id), ('topping_category', '=', 3) ])) @api.constrains('topping_ids_1', 'topping_ids_2', 'topping_ids_3') def _check_topping_quantity(self): errors = { '1_more': _('You should order at least one %s'), '1': _('You have to order one and only one %s'), } for wizard in self: for index in range(1, 4): availability = wizard['available_toppings_%s' % index] quantity = wizard['topping_quantity_%s' % index] toppings = wizard['topping_ids_%s' % index].filtered( lambda x: x.topping_category == index) label = wizard['topping_label_%s' % index] if availability and quantity != '0_more': check = bool( len(toppings) == 1 if quantity == '1' else toppings) if not check: raise ValidationError(errors[quantity] % label) @api.depends('product_id', 'topping_ids_1', 'topping_ids_2', 'topping_ids_3', 'quantity') def _compute_price_total(self): for wizard in self: wizard.price_total = wizard.quantity * ( wizard.product_id.price + sum( (wizard.topping_ids_1 | wizard.topping_ids_2 | wizard.topping_ids_3).mapped('price'))) def _get_matching_lines(self): domain = [('user_id', '=', self.user_id.id), ('product_id', '=', self.product_id.id), ('date', '=', fields.Date.today()), ('note', '=', self._get_note())] lines = self.env['lunch.order'].search(domain) return lines.filtered(lambda line: (line.topping_ids_1 | line.topping_ids_2 | line. topping_ids_3) == self.topping_ids_1) def _get_note(self): """ returns self.note, but make sure that if it is an empty string it becomes False """ return self.note if self.note else False def add_to_cart(self): self.ensure_one() line_id = self.env.context.get('line_id') matching_line = False matching_lines = self._get_matching_lines() if matching_lines: matching_line = matching_lines[0] quantity = 1 if matching_line.id != line_id: if self.edit: line = self.env['lunch.order'].browse(line_id) quantity = line.quantity line.sudo().unlink() else: quantity = 0 matching_line.quantity += quantity else: if self.edit: line = self.env['lunch.order'].browse(line_id) line.topping_ids_1 = self.topping_ids_1 line.topping_ids_2 = self.topping_ids_2 line.topping_ids_3 = self.topping_ids_3 line.note = self._get_note() else: self.env['lunch.order'].create({ 'product_id': self.product_id.id, 'topping_ids_1': [(6, 0, self.topping_ids_1.ids)], 'topping_ids_2': [(6, 0, self.topping_ids_2.ids)], 'topping_ids_3': [(6, 0, self.topping_ids_3.ids)], 'quantity': self.quantity, 'note': self._get_note() })
class HrEmployeePrivate(models.Model): """ NB: Any field only available on the model hr.employee (i.e. not on the hr.employee.public model) should have `groups="hr.group_hr_user"` on its definition to avoid being prefetched when the user hasn't access to the hr.employee model. Indeed, the prefetch loads the data for all the fields that are available according to the group defined on them. """ _name = "hr.employee" _description = "Employee" _order = 'name' _inherit = [ 'hr.employee.base', 'mail.thread', 'mail.activity.mixin', 'resource.mixin', 'image.mixin' ] _mail_post_access = 'read' @api.model def _default_image(self): image_path = get_module_resource('hr', 'static/src/img', 'default_image.png') return base64.b64encode(open(image_path, 'rb').read()) # resource and user # required on the resource, make sure required="True" set in the view name = fields.Char(string="Employee Name", related='resource_id.name', store=True, readonly=False, tracking=True) user_id = fields.Many2one('res.users', 'User', related='resource_id.user_id', store=True, readonly=False) user_partner_id = fields.Many2one(related='user_id.partner_id', related_sudo=False, string="User's partner") active = fields.Boolean('Active', related='resource_id.active', default=True, store=True, readonly=False) # private partner address_home_id = fields.Many2one( 'res.partner', 'Address', help= 'Enter here the private address of the employee, not the one linked to your company.', groups="hr.group_hr_user", tracking=True, domain= "['|', ('company_id', '=', False), ('company_id', '=', company_id)]") is_address_home_a_company = fields.Boolean( 'The employee address has a company linked', compute='_compute_is_address_home_a_company', ) private_email = fields.Char(related='address_home_id.email', string="Private Email", groups="hr.group_hr_user") country_id = fields.Many2one('res.country', 'Nationality (Country)', groups="hr.group_hr_user", tracking=True) gender = fields.Selection([('male', 'Male'), ('female', 'Female'), ('other', 'Other')], groups="hr.group_hr_user", default="male", tracking=True) marital = fields.Selection([('single', 'Single'), ('married', 'Married'), ('cohabitant', 'Legal Cohabitant'), ('widower', 'Widower'), ('divorced', 'Divorced')], string='Marital Status', groups="hr.group_hr_user", default='single', tracking=True) spouse_complete_name = fields.Char(string="Spouse Complete Name", groups="hr.group_hr_user", tracking=True) spouse_birthdate = fields.Date(string="Spouse Birthdate", groups="hr.group_hr_user", tracking=True) children = fields.Integer(string='Number of Children', groups="hr.group_hr_user", tracking=True) place_of_birth = fields.Char('Place of Birth', groups="hr.group_hr_user", tracking=True) country_of_birth = fields.Many2one('res.country', string="Country of Birth", groups="hr.group_hr_user", tracking=True) birthday = fields.Date('Date of Birth', groups="hr.group_hr_user", tracking=True) ssnid = fields.Char('SSN No', help='Social Security Number', groups="hr.group_hr_user", tracking=True) sinid = fields.Char('SIN No', help='Social Insurance Number', groups="hr.group_hr_user", tracking=True) identification_id = fields.Char(string='Identification No', groups="hr.group_hr_user", tracking=True) passport_id = fields.Char('Passport No', groups="hr.group_hr_user", tracking=True) bank_account_id = fields.Many2one( 'res.partner.bank', 'Bank Account Number', domain= "[('partner_id', '=', address_home_id), '|', ('company_id', '=', False), ('company_id', '=', company_id)]", groups="hr.group_hr_user", tracking=True, help='Employee bank salary account') permit_no = fields.Char('Work Permit No', groups="hr.group_hr_user", tracking=True) visa_no = fields.Char('Visa No', groups="hr.group_hr_user", tracking=True) visa_expire = fields.Date('Visa Expire Date', groups="hr.group_hr_user", tracking=True) additional_note = fields.Text(string='Additional Note', groups="hr.group_hr_user", tracking=True) certificate = fields.Selection([ ('bachelor', 'Bachelor'), ('master', 'Master'), ('other', 'Other'), ], 'Certificate Level', default='other', groups="hr.group_hr_user", tracking=True) study_field = fields.Char("Field of Study", groups="hr.group_hr_user", tracking=True) study_school = fields.Char("School", groups="hr.group_hr_user", tracking=True) emergency_contact = fields.Char("Emergency Contact", groups="hr.group_hr_user", tracking=True) emergency_phone = fields.Char("Emergency Phone", groups="hr.group_hr_user", tracking=True) km_home_work = fields.Integer(string="Km Home-Work", groups="hr.group_hr_user", tracking=True) image_1920 = fields.Image(default=_default_image) phone = fields.Char(related='address_home_id.phone', related_sudo=False, string="Private Phone", groups="hr.group_hr_user") # employee in company child_ids = fields.One2many('hr.employee', 'parent_id', string='Direct subordinates') category_ids = fields.Many2many('hr.employee.category', 'employee_category_rel', 'emp_id', 'category_id', groups="hr.group_hr_manager", string='Tags') # misc notes = fields.Text('Notes', groups="hr.group_hr_user") color = fields.Integer('Color Index', default=0, groups="hr.group_hr_user") barcode = fields.Char(string="Badge ID", help="ID used for employee identification.", groups="hr.group_hr_user", copy=False) pin = fields.Char( string="PIN", groups="hr.group_hr_user", copy=False, help= "PIN used to Check In/Out in Kiosk Mode (if enabled in Configuration)." ) departure_reason = fields.Selection([('fired', 'Fired'), ('resigned', 'Resigned'), ('retired', 'Retired')], string="Departure Reason", groups="hr.group_hr_user", copy=False, tracking=True) departure_description = fields.Text(string="Additional Information", groups="hr.group_hr_user", copy=False, tracking=True) message_main_attachment_id = fields.Many2one(groups="hr.group_hr_user") _sql_constraints = [ ('barcode_uniq', 'unique (barcode)', "The Badge ID must be unique, this one is already assigned to another employee." ), ('user_uniq', 'unique (user_id, company_id)', "A user cannot be linked to multiple employees in the same company.") ] def name_get(self): if self.check_access_rights('read', raise_exception=False): return super(HrEmployeePrivate, self).name_get() return self.env['hr.employee.public'].browse(self.ids).name_get() def _read(self, fields): if self.check_access_rights('read', raise_exception=False): return super(HrEmployeePrivate, self)._read(fields) res = self.env['hr.employee.public'].browse(self.ids).read(fields) for r in res: record = self.browse(r['id']) record._update_cache({k: v for k, v in r.items() if k in fields}, validate=False) def read(self, fields, load='_classic_read'): if self.check_access_rights('read', raise_exception=False): return super(HrEmployeePrivate, self).read(fields, load=load) private_fields = set(fields).difference( self.env['hr.employee.public']._fields.keys()) if private_fields: raise AccessError( _('The fields "%s" you try to read is not available on the public employee profile.' ) % (','.join(private_fields))) return self.env['hr.employee.public'].browse(self.ids).read(fields, load=load) @api.model def load_views(self, views, options=None): if self.check_access_rights('read', raise_exception=False): return super(HrEmployeePrivate, self).load_views(views, options=options) return self.env['hr.employee.public'].load_views(views, options=options) @api.model def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None): """ We override the _search because it is the method that checks the access rights This is correct to override the _search. That way we enforce the fact that calling search on an hr.employee returns a hr.employee recordset, even if you don't have access to this model, as the result of _search (the ids of the public employees) is to be browsed on the hr.employee model. This can be trusted as the ids of the public employees exactly match the ids of the related hr.employee. """ if self.check_access_rights('read', raise_exception=False): return super(HrEmployeePrivate, self)._search(args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid) return self.env['hr.employee.public']._search( args, offset=offset, limit=limit, order=order, count=count, access_rights_uid=access_rights_uid) def get_formview_id(self, access_uid=None): """ Override this method in order to redirect many2one towards the right model depending on access_uid """ if access_uid: self_sudo = self.with_user(access_uid) else: self_sudo = self if self_sudo.check_access_rights('read', raise_exception=False): return super(HrEmployeePrivate, self).get_formview_id(access_uid=access_uid) # Hardcode the form view for public employee return self.env.ref('hr.hr_employee_public_view_form').id def get_formview_action(self, access_uid=None): """ Override this method in order to redirect many2one towards the right model depending on access_uid """ res = super(HrEmployeePrivate, self).get_formview_action(access_uid=access_uid) if access_uid: self_sudo = self.with_user(access_uid) else: self_sudo = self if not self_sudo.check_access_rights('read', raise_exception=False): res['res_model'] = 'hr.employee.public' return res @api.constrains('pin') def _verify_pin(self): for employee in self: if employee.pin and not employee.pin.isdigit(): raise ValidationError( _("The PIN must be a sequence of digits.")) @api.onchange('job_id') def _onchange_job_id(self): if self.job_id: self.job_title = self.job_id.name @api.onchange('address_id') def _onchange_address(self): self.work_phone = self.address_id.phone self.mobile_phone = self.address_id.mobile @api.onchange('company_id') def _onchange_company(self): address = self.company_id.partner_id.address_get(['default']) self.address_id = address['default'] if address else False @api.onchange('department_id') def _onchange_department(self): if self.department_id.manager_id: self.parent_id = self.department_id.manager_id @api.onchange('user_id') def _onchange_user(self): if self.user_id: self.update(self._sync_user(self.user_id)) if not self.name: self.name = self.user_id.name @api.onchange('resource_calendar_id') def _onchange_timezone(self): if self.resource_calendar_id and not self.tz: self.tz = self.resource_calendar_id.tz def _sync_user(self, user): vals = dict( image_1920=user.image_1920, work_email=user.email, ) if user.tz: vals['tz'] = user.tz return vals @api.model def create(self, vals): if vals.get('user_id'): user = self.env['res.users'].browse(vals['user_id']) vals.update(self._sync_user(user)) vals['name'] = vals.get('name', user.name) employee = super(HrEmployeePrivate, self).create(vals) url = '/web#%s' % url_encode({ 'action': 'hr.plan_wizard_action', 'active_id': employee.id, 'active_model': 'hr.employee' }) employee._message_log(body=_( '<b>Congratulations!</b> May I recommend you to setup an <a href="%s">onboarding plan?</a>' ) % (url)) if employee.department_id: self.env['mail.channel'].sudo().search([ ('subscription_department_ids', 'in', employee.department_id.id) ])._subscribe_users() return employee def write(self, vals): if 'address_home_id' in vals: account_id = vals.get('bank_account_id') or self.bank_account_id.id if account_id: self.env['res.partner.bank'].browse( account_id).partner_id = vals['address_home_id'] if vals.get('user_id'): vals.update( self._sync_user(self.env['res.users'].browse(vals['user_id']))) res = super(HrEmployeePrivate, self).write(vals) if vals.get('department_id') or vals.get('user_id'): department_id = vals['department_id'] if vals.get( 'department_id') else self[:1].department_id.id # When added to a department or changing user, subscribe to the channels auto-subscribed by department self.env['mail.channel'].sudo().search([ ('subscription_department_ids', 'in', department_id) ])._subscribe_users() return res def unlink(self): resources = self.mapped('resource_id') super(HrEmployeePrivate, self).unlink() return resources.unlink() def toggle_active(self): res = super(HrEmployeePrivate, self).toggle_active() self.filtered(lambda employee: employee.active).write({ 'departure_reason': False, 'departure_description': False, }) if len(self) == 1 and not self.active: return { 'type': 'ir.actions.act_window', 'name': _('Register Departure'), 'res_model': 'hr.departure.wizard', 'view_mode': 'form', 'target': 'new', 'context': { 'active_id': self.id }, 'views': [[False, 'form']] } return res def generate_random_barcode(self): for employee in self: employee.barcode = "".join(choice(digits) for i in range(8)) @api.depends('address_home_id.parent_id') def _compute_is_address_home_a_company(self): """Checks that chosen address (res.partner) is not linked to a company. """ for employee in self: try: employee.is_address_home_a_company = employee.address_home_id.parent_id.id is not False except AccessError: employee.is_address_home_a_company = False # --------------------------------------------------------- # Business Methods # --------------------------------------------------------- @api.model def get_import_templates(self): return [{ 'label': _('Import Template for Employees'), 'template': '/hr/static/xls/hr_employee.xls' }] def _post_author(self): """ When a user updates his own employee's data, all operations are performed by super user. However, tracking messages should not be posted as CofficeBot but as the actual user. This method is used in the overrides of `_message_log` and `message_post` to post messages as the correct user. """ real_user = self.env.context.get('binary_field_real_user') if self.env.is_superuser() and real_user: self = self.with_user(real_user) return self # --------------------------------------------------------- # Messaging # --------------------------------------------------------- def _message_log(self, **kwargs): return super(HrEmployeePrivate, self._post_author())._message_log(**kwargs) @api.returns('mail.message', lambda value: value.id) def message_post(self, **kwargs): return super(HrEmployeePrivate, self._post_author()).message_post(**kwargs) def _sms_get_partner_fields(self): return ['user_partner_id'] def _sms_get_number_fields(self): return ['mobile_phone']