def pre_init_hook(cr): table = "hr_employee" column = "partner_id" field_type = "int4" if not sql.column_exists(cr, table, column): sql.create_column(cr, table, columnname=column, columntype=field_type) cr.execute("SELECT id FROM %s WHERE %s is null", (AsIs(table), AsIs(column))) employee_ids = [] for row in cr.fetchall(): employee_ids.append(row[0]) env = api.Environment(cr, SUPERUSER_ID, {}) for employee in env["hr.employee"].browse(employee_ids): partner = env["res.partner"].create({ "name": employee.name, "is_practitioner": True, "active": employee.active, }) partner.flush() cr.execute( "UPDATE %s SET %s = %s WHERE id = %s", (AsIs(table), AsIs(column), partner.id, employee.id), ) cr.execute("SELECT id FROM %s WHERE %s is null", (AsIs(table), AsIs(column)))
def _auto_init(self): # Skip the computation of the field `l10n_latam_document_type_id` at the module installation # Without this, at the module installation, # it would call `_compute_l10n_latam_document_type` on all existing records # which can take quite a while if you already have a lot of moves. It can even fail with a MemoryError. # In addition, it sets `_compute_l10n_latam_document_type = False` on all records # because this field depends on the many2many `l10n_latam_available_document_type_ids`, # which relies on having records for the model `l10n_latam.document.type` # which only happens once the according localization module is loaded. # The localization module is loaded afterwards, because the localization module depends on this module, # (e.g. `l10n_cl` depends on `l10n_latam_invoice_document`, and therefore `l10n_cl` is loaded after) # and therefore there are no records for the model `l10n_latam.document.type` at the time this fields # gets computed on installation. Hence, all records' `_compute_l10n_latam_document_type` are set to `False`. # In addition, multiple localization module depends on this module (e.g. `l10n_cl`, `l10n_ar`) # So, imagine `l10n_cl` gets installed first, and then `l10n_ar` is installed next, # if `l10n_latam_document_type_id` needed to be computed on install, # the install of `l10n_cl` would call the compute method, # because `l10n_latam_invoice_document` would be installed at the same time, # but then `l10n_ar` would miss it, because `l10n_latam_invoice_document` would already be installed. # Besides, this field is computed only for drafts invoices, as stated in the compute method: # `for rec in self.filtered(lambda x: x.state == 'draft'):` # So, if we want this field to be computed on install, it must be done only on draft invoices, and only once # the localization modules are loaded. # It should be done in a dedicated post init hook, # filtering correctly the invoices for which it must be computed. # Though I don't think this is needed. # In practical, it's very rare to already have invoices (draft, in addition) # for a Chilian or Argentian company (`res.company`) before installing `l10n_cl` or `l10n_ar`. if not column_exists(self.env.cr, "account_move", "l10n_latam_document_type_id"): create_column(self.env.cr, "account_move", "l10n_latam_document_type_id", "int4") return super()._auto_init()
def pre_init_hook(cr): """ account.invoice and account.invoice.line inherits from l10n_br_account.fiscal_document and l10n_br_account.fiscal_document.line respectively. But the problem is that you may have existing invoice and lines (like demo data or because you were using Odoo before installing this module or because you use your Odoo instance for other countries than Brazil) so we should make the Odoo ORM happy for these records and we do that with dummy records that we use to fill these new foreign keys. """ env = api.Environment(cr, SUPERUSER_ID, {}) if not column_exists(cr, "account_invoice", "fiscal_document_id"): create_column(cr, "account_invoice", "fiscal_document_id", "INTEGER") fiscal_doc_id = env.ref("l10n_br_fiscal.fiscal_document_dummy").id cr.execute( """update account_invoice set fiscal_document_id=%s where fiscal_document_id IS NULL;""", (fiscal_doc_id, ), ) fiscal_doc_line_id = env.ref( "l10n_br_fiscal.fiscal_document_line_dummy").id if not column_exists(cr, "account_invoice_line", "fiscal_document_line_id"): create_column(cr, "account_invoice_line", "fiscal_document_line_id", "INTEGER") cr.execute( """update account_invoice_line set fiscal_document_line_id=%s where fiscal_document_line_id IS NULL;""", (fiscal_doc_line_id, ), )
def update_db_column(self, model, column): """ Create/update the column corresponding to ``self``. For creation of geo column :param model: an instance of the field's model :param column: the column's configuration (dict) if it exists, or ``None`` """ # the column does not exist, create it if not column: create_geo_column(model._cr, model._table, self.name, self.geo_type, self.srid, self.dim, self.string) return if column['udt_name'] == self.column_type[0]: return self.update_geo_db_column(model) if column['udt_name'] in self.column_cast_from: sql.convert_column(model._cr, model._table, self.name, self.column_type[1]) else: newname = (self.name + '_moved{}').format i = 0 while sql.column_exists(model._cr, model._table, newname(i)): i += 1 if column['is_nullable'] == 'NO': sql.drop_not_null(model._cr, model._table, self.name) sql.rename_column(model._cr, model._table, self.name, newname(i)) sql.create_column(model._cr, model._table, self.name, self.column_type[1], self.string)
def _auto_init(self): # Skip the computation of the field `l10n_latam_document_type_id` at the module installation # See `_auto_init` in `l10n_latam_invoice_document/models/account_move.py` for more information if not column_exists(self.env.cr, "account_move_line", "l10n_latam_document_type_id"): create_column(self.env.cr, "account_move_line", "l10n_latam_document_type_id", "int4") return super()._auto_init()
def _auto_init(self): """ Create column for `preferred_payment_method_id` to avoid having it computed by the ORM on installation. Since `property_payment_method_id` is introduced in this module, there is no need for UPDATE """ if not column_exists(self.env.cr, "account_move", "preferred_payment_method_id"): create_column(self.env.cr, "account_move", "preferred_payment_method_id", "int4") return super()._auto_init()
def _auto_init(self): """ Create related field here, too slow when computing it afterwards through _compute_related. Since group_id.sale_id is created in this module, no need for an UPDATE statement. """ if not column_exists(self.env.cr, 'stock_picking', 'sale_id'): create_column(self.env.cr, 'stock_picking', 'sale_id', 'int4') return super()._auto_init()
def create_risk_partner_id_column(cr): if not sql.column_exists(cr, "sale_order_line", "risk_partner_id"): sql.create_column(cr, "sale_order_line", "risk_partner_id", "int4") logger.info("Computing field risk_partner_id on sale.order.line") cr.execute(""" UPDATE sale_order_line sol SET risk_partner_id = p.commercial_partner_id FROM sale_order so LEFT JOIN res_partner p ON p.id = so.partner_invoice_id WHERE so.id = sol.order_id and sol.risk_partner_id IS DISTINCT FROM p.commercial_partner_id; """)
def create_commercial_partner_id_column(cr): if not sql.column_exists(cr, 'sale_order_line', 'commercial_partner_id'): sql.create_column(cr, 'sale_order_line', 'commercial_partner_id', 'int4') logger.info('Computing field commercial_partner_id on sale.order.line') cr.execute(""" UPDATE sale_order_line sol SET commercial_partner_id = p.commercial_partner_id FROM res_partner p WHERE p.id = sol.order_partner_id and sol.commercial_partner_id IS DISTINCT FROM p.commercial_partner_id; """)
def _auto_init(self): """ Create column to stop ORM from computing it himself (too slow) """ if not column_exists(self.env.cr, 'sale_order_line', 'is_service'): create_column(self.env.cr, 'sale_order_line', 'is_service', 'bool') self.env.cr.execute(""" UPDATE sale_order_line line SET is_service = (pt.type = 'service') FROM product_product pp LEFT JOIN product_template pt ON pt.id = pp.product_tmpl_id WHERE pp.id = line.product_id """) return super()._auto_init()
def pre_init_hook(cr): """ account.invoice and account.invoice.line inherits from l10n_br_account.fiscal_document and l10n_br_account.fiscal_document.line respectively. But the problem is that you may have existing invoice and lines (like demo data or because you were using Odoo before installing this module or because you use your Odoo instance for other countries than Brazil) so we should make the Odoo ORM happy for these records and we do that with dummy records that we use to fill these new foreign keys. """ env = api.Environment(cr, SUPERUSER_ID, {}) # Create fiscal_document_id fields if not column_exists(cr, "account_invoice", "fiscal_document_id"): create_column(cr, "account_invoice", "fiscal_document_id", "INTEGER") # Create fiscal_document_line_id fields if not column_exists(cr, "account_invoice_line", "fiscal_document_line_id"): create_column(cr, "account_invoice_line", "fiscal_document_line_id", "INTEGER") companies = env["res.company"].search([]) for company in companies: cr.execute( """ UPDATE account_invoice SET fiscal_document_id=%s WHERE fiscal_document_id IS NULL;""", (company.fiscal_dummy_id.id, ), ) cr.execute( """ UPDATE account_invoice_line SET fiscal_document_line_id=%s WHERE fiscal_document_line_id IS NULL;""", (company.fiscal_dummy_id.line_ids[0].id, ), )
def migrate(cr, version): """Migrate only_quotation field - Copy product.template values to product.product - Rename to shop_only_quotation """ if ( not version or column_exists(cr, "product_product", "shop_only_quotation") or not column_exists(cr, "product_template", "only_quotation") ): return create_column(cr, "product_product", "shop_only_quotation", "BOOLEAN") cr.execute( """ UPDATE product_product pp SET shop_only_quotation = pt.only_quotation FROM product_template pt WHERE pp.product_tmpl_id = pt.id """ ) cr.execute("ALTER TABLE product_template DROP COLUMN only_quotation")
def pre_init_hook(cr): for table, column in COLUMNS: if not column_exists(cr, table, column): _logger.info("Create discount column %s in database", column) create_column(cr, table, column, "numeric")