class product_product(Model): _inherit = 'product.product' def _get_supplier_goodies_ids(self, cr, uid, ids, name, arg, context=None): if context is None: context={} if context.get('date'): date = context['date'] else: date = datetime.today().strftime('%Y-%m-%d') res = {} link_obj = self.pool.get('product.link') for product_id in ids: res[product_id] = link_obj.search(cr, uid, [ ['product_id','=', ids[0]], ['type','=', 'goodies'], '|', ['start_date', '<=', date], ['start_date', '=', False], '|', ['end_date', '>=', date], ['end_date', '=', False], ['supplier_goodies', '=', True], ], context=context) return res _columns = { 'supplier_goodies_ids': fields.function(_get_supplier_goodies_ids, type='many2many', relation="product.link"), } def is_purchase_goodies(self, cr, uid, ids, context=None): return self.pool.get('product.link').search(cr, uid, [ ['linked_product_id','=', ids[0]], ['type', '=', 'goodies'], ['supplier_goodies', '=', True], ], context=context) and True or False
class ProductTemplate(models.Model): _inherit = "product.template" force_currency_id = fields.Many2one( 'res.currency', 'Force Currency', help='Use this currency instead of the product company currency') def _product_currency(self, cr, uid, ids, name, arg, context=None): res = super(ProductTemplate, self)._product_currency(cr, uid, ids, name, arg, context=context) for rec in self.browse(cr, uid, ids, context=context): if rec.force_currency_id: res[rec.id] = rec.force_currency_id.id return res _columns = { 'currency_id': old_fields.function(_product_currency, type='many2one', relation='res.currency', string='Currency'), }
class stock_quant(osv.osv): _inherit = "stock.quant" def _calc_unit_value(self, cr, uid, ids, name, attr, context=None): context = dict(context or {}) res = {} uid_company_id = self.pool.get('res.users').browse( cr, uid, uid, context=context).company_id.id for quant in self.browse(cr, uid, ids, context=context): context.pop('force_company', None) if quant.company_id.id != uid_company_id: # if the company of the quant is different than the current user company, force the company in the context # then re-do a browse to read the property fields for the good company. context['force_company'] = quant.company_id.id quant = self.browse(cr, uid, quant.id, context=context) res[quant.id] = self._get_inventory_value( cr, uid, quant, context=context) / quant.qty return res _columns = { 'unit_price': fields.function(_calc_unit_value, string="Pret unitar", type='float', readonly=True), }
class magento_backend(orm.Model): _inherit = 'magento.backend' def _get_environment_config_by_name(self, cr, uid, ids, field_names, arg, context=None): values = {} for backend in self.browse(cr, uid, ids, context=context): values[backend.id] = {} for field_name in field_names: section_name = '.'.join( (self._name.replace('.', '_'), backend.name)) try: value = serv_config.get(section_name, field_name) values[backend.id][field_name] = value except: _logger.exception( 'error trying to read field %s ' 'in section %s', field_name, section_name) values[backend.id][field_name] = False return values _columns = { 'location': fields.function(_get_environment_config_by_name, string='Location', type='char', multi='connection_config'), 'username': fields.function(_get_environment_config_by_name, string='Username', type='char', multi='connection_config'), 'password': fields.function(_get_environment_config_by_name, string='Password', type='char', multi='connection_config'), }
class account_analytic_account(orm.Model): def _invoiced_calc(self, cr, uid, ids, name, arg, context=None): obj_invoice = self.pool.get('account.invoice') res = {} cr.execute('SELECT account_id as account_id, l.invoice_id ' 'FROM hr_analytic_timesheet h LEFT JOIN account_analytic_line l ' 'ON (h.line_id=l.id) ' 'WHERE l.account_id = ANY(%s)', (ids,)) account_to_invoice_map = {} for rec in cr.dictfetchall(): account_to_invoice_map.setdefault(rec['account_id'], []).append(rec['invoice_id']) for account in self.browse(cr, uid, ids, context=context): invoice_ids = filter(None, list(set(account_to_invoice_map.get(account.id, [])))) for invoice in obj_invoice.browse(cr, uid, invoice_ids, context=context): res.setdefault(account.id, 0.0) res[account.id] += invoice.amount_untaxed for id in ids: res[id] = round(res.get(id, 0.0),2) return res _inherit = "account.analytic.account" _columns = { 'pricelist_id': fields.many2one('product.pricelist', 'Pricelist', help="The product to invoice is defined on the employee form, the price will be deducted by this pricelist on the product."), 'amount_max': fields.float('Max. Invoice Price', help="Keep empty if this contract is not limited to a total fixed price."), 'amount_invoiced': fields.function(_invoiced_calc, string='Invoiced Amount', help="Total invoiced"), 'to_invoice': fields.many2one('hr_timesheet_invoice.factor', 'Timesheet Invoicing Ratio', help="You usually invoice 100% of the timesheets. But if you mix fixed price and timesheet invoicing, you may use another ratio. For instance, if you do a 20% advance invoice (fixed price, based on a sales order), you should invoice the rest on timesheet with a 80% ratio."), } def on_change_partner_id(self, cr, uid, ids, partner_id, name, context=None): res = super(account_analytic_account, self).on_change_partner_id(cr, uid, ids, partner_id, name, context=context) if partner_id: part = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context) pricelist = part.property_product_pricelist and part.property_product_pricelist.id or False if pricelist: res['value']['pricelist_id'] = pricelist return res def set_close(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'state': 'close'}, context=context) def set_cancel(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'state': 'cancelled'}, context=context) def set_open(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'state': 'open'}, context=context) def set_pending(self, cr, uid, ids, context=None): return self.write(cr, uid, ids, {'state': 'pending'}, context=context)
class ProductProduct(models.Model): _inherit = 'product.product' # we do this so that we can show prices with or without taxe without # needing a pricelist def _product_lst_price(self, cr, uid, ids, name, arg, context=None): res = super(ProductProduct, self)._product_lst_price(cr, uid, ids, name, arg, context=context) if not context.get('taxes_included'): return res company_id = (context.get('company_id') or self.pool['res.users'].browse(cr, uid, uid, context).company_id.id) for product in self.browse(cr, uid, ids, context=context): res[product.id] = product.taxes_id.filtered( lambda x: x.company_id.id == company_id).compute_all( res[product.id], product=product.id)['total_included'] return res def _set_product_lst_price(self, cr, uid, id, name, value, args, context=None): if context.get('taxes_included'): raise UserError( _("You can not set list price if you are working with 'Taxes " "Included' in the context")) return super(ProductProduct, self)._set_product_lst_price(cr, uid, id, name, value, args, context=context) _columns = { 'lst_price': old_fields.function(_product_lst_price, fnct_inv=_set_product_lst_price, type='float', string='Public Price', digits_compute=dp.get_precision('Product Price')), }
class partner(osv.osv): """""" _inherit = 'res.partner' def _retrieve_user(self, cr, uid, ids, arg, karg, context=None): """ retrieve the (possibly inactive) user corresponding to wizard_user.partner_id @param wizard_user: browse record of model portal.wizard.user @return: browse record of model res.users """ context = dict(context or {}, active_test=False) res_users = self.pool.get('res.users') res = {} for i in ids: domain = [('partner_id', '=', i)] user_ids = res_users.search(cr, uid, domain, context=context) user_id = False if user_ids: user_id = user_ids[0] res[i] = user_id return res _columns = { 'login': fields.related('related_user_id', 'login', string='Login', type='char', size=64, readonly=True, help="Used to log into the system"), 'password': fields.related('related_user_id', 'password', string='Password', type='char', size=64, readonly=True, help="Keep empty if you don't want the user to be able to connect on the system."), 'related_user_id': fields.function(_retrieve_user, relation='res.users', string='User', type='many2one', ), 'template_user_id': fields.many2one('res.users', string="Template User", domain=[('active', '=', False)],), } def open_related_user(self, cr, uid, ids, context=None): user_id = self.browse( cr, uid, ids[0], context=context).related_user_id.id if not user_id: return False view_ref = self.pool.get('ir.model.data').get_object_reference( cr, uid, 'base', 'view_users_form') view_id = view_ref and view_ref[1] or False, return { 'type': 'ir.actions.act_window', 'view_id': view_id, 'res_model': 'res.users', 'view_mode': 'form', 'res_id': user_id, 'target': 'current', # 'flags': {'form': {'action_buttons': True, 'options': {'mode': 'edit'}}} } def delete_user(self, cr, uid, ids, context=None): user_id = self.browse( cr, uid, ids[0], context=context).related_user_id.id if not user_id: return False return self.pool.get('res.users').unlink(cr, uid, [user_id], context=context) def retrieve_user(self, cr, uid, partner, context=None): """ retrieve the (possibly inactive) user corresponding to partner @param partner: browse record of model portal.wizard.user @return: browse record of model res.users """ context = dict(context or {}, active_test=False) res_users = self.pool.get('res.users') domain = [('partner_id', '=', partner.id)] user_ids = res_users.search(cr, uid, domain, context=context) return user_ids def quickly_create_user(self, cr, uid, ids, context=None): res_users = self.pool.get('res.users') # Make this an option context = dict(context or {}, no_reset_password=True) # TODO Pasar argumentos para activar o desactivar create_user = True for partner in self.browse(cr, SUPERUSER_ID, ids, context): group_ids = [] if not partner.template_user_id: raise osv.except_osv(_('Non template user selected!'), _('Please define a template user for this partner: "%s" (id:%d).') % (partner.name, partner.id)) group_ids = [x.id for x in partner.template_user_id.groups_id] user_ids = self.retrieve_user(cr, SUPERUSER_ID, partner, context) if create_user: # create a user if necessary, and make sure it is in the portal # group if not user_ids: user_ids = [ self._create_user(cr, SUPERUSER_ID, partner, context)] res_users.write( cr, SUPERUSER_ID, user_ids, {'active': True, 'groups_id': [(6, 0, group_ids)]}) # prepare for the signup process # TODO make an option of this # partner.signup_prepare() # TODO option to send or not email # self._send_email(cr, uid, partner, context) elif user_ids: # deactivate user res_users.write(cr, SUPERUSER_ID, user_ids, {'active': False}) def _create_user(self, cr, uid, partner, context=None): """ create a new user for partner.partner_id @param partner: browse record of model partner.user @return: browse record of model res.users """ res_users = self.pool.get('res.users') # to prevent shortcut creation create_context = dict( context or {}, noshortcut=True, no_reset_password=True) if partner.email: login = extract_email(partner.email) else: login = self._clean_and_make_unique( cr, uid, partner.name, context=context) values = { # 'email': extract_email(partner.email), 'login': login, # 'login': extract_email(partner.email), 'partner_id': partner.id, 'company_id': partner.company_id.id, 'company_ids': [(4, partner.company_id.id)], 'password': ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6)), 'groups_id': [(6, 0, [])], } return res_users.create(cr, uid, values, context=create_context) def _clean_and_make_unique(self, cr, uid, name, context=None): # when an alias name appears to already be an email, we keep the local # part only name = remove_accents(name).lower().split('@')[0] name = re.sub(r'[^\w+.]+', '.', name) return self._find_unique(cr, uid, name, context=context) def _find_unique(self, cr, uid, name, context=None): """Find a unique alias name similar to ``name``. If ``name`` is already taken, make a variant by adding an integer suffix until an unused alias is found. """ sequence = None while True: new_name = "%s%s" % ( name, sequence) if sequence is not None else name if not self.pool.get('res.users').search(cr, uid, [('login', '=', new_name)]): break sequence = (sequence + 1) if sequence else 2 return new_name
class ProductProduct(models.Model): _inherit = 'product.product' pack_line_ids = fields.One2many( 'product.pack.line', 'parent_product_id', 'Pack Products', help='List of products that are part of this pack.') used_pack_line_ids = fields.One2many( 'product.pack.line', 'product_id', 'On Packs', help='List of packs where product is used.') def _product_available(self, cr, uid, ids, field_names=None, arg=False, context=None): """ For product packs we get availability in a different way """ pack_product_ids = self.search(cr, uid, [ ('pack', '=', True), ('id', 'in', ids), ]) res = super(ProductProduct, self)._product_available( cr, uid, list(set(ids) - set(pack_product_ids)), field_names, arg, context) for product in self.browse(cr, uid, pack_product_ids, context=context): pack_qty_available = [] pack_virtual_available = [] for subproduct in product.pack_line_ids: subproduct_stock = self._product_available( cr, uid, [subproduct.product_id.id], field_names, arg, context)[subproduct.product_id.id] sub_qty = subproduct.quantity if sub_qty: pack_qty_available.append( math.floor(subproduct_stock['qty_available'] / sub_qty)) pack_virtual_available.append( math.floor(subproduct_stock['virtual_available'] / sub_qty)) # TODO calcular correctamente pack virtual available para negativos res[product.id] = { 'qty_available': (pack_qty_available and min(pack_qty_available) or False), 'incoming_qty': 0, 'outgoing_qty': 0, 'virtual_available': (pack_virtual_available and max(min(pack_virtual_available), 0) or False), } return res def _search_product_quantity(self, cr, uid, obj, name, domain, context): """ We use original search function """ return super(ProductProduct, self)._search_product_quantity(cr, uid, obj, name, domain, context) # overwrite ot this fields so that we can modify _product_available # function to support packs _columns = { 'qty_available': old_fields.function(_product_available, multi='qty_available', fnct_search=_search_product_quantity), 'virtual_available': old_fields.function(_product_available, multi='qty_available', fnct_search=_search_product_quantity), 'incoming_qty': old_fields.function(_product_available, multi='qty_available', fnct_search=_search_product_quantity), 'outgoing_qty': old_fields.function(_product_available, multi='qty_available', fnct_search=_search_product_quantity), } @api.multi @api.constrains('pack_line_ids') def check_recursion(self): """ Check recursion on packs """ for rec in self: pack_lines = rec.pack_line_ids while pack_lines: if rec in pack_lines.mapped('product_id'): raise UserError( _('Error! You cannot create recursive packs.\n' 'Product id: %s') % rec.id) pack_lines = pack_lines.mapped('product_id.pack_line_ids')