Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3838][ADD] purchase_order_migration #135

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions purchase_order_migration/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
=============================
Purchase Order Data Migration
=============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:7840f2f9a0e7fa562cf685887cceb707340acc123a2d6adf5df63e0f9b6d5d0b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-qrtl%2Frmm--custom-lightgray.png?logo=github
:target: https://github.com/qrtl/rmm-custom/tree/15.0/purchase_order_migration
:alt: qrtl/rmm-custom

|badge1| |badge2| |badge3|

This module allows you to migrate old data from the old system to the
current environment.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/qrtl/rmm-custom/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/qrtl/rmm-custom/issues/new?body=module:%20purchase_order_migration%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Quartile Limited

Maintainers
-----------

This module is part of the `qrtl/rmm-custom <https://github.com/qrtl/rmm-custom/tree/15.0/purchase_order_migration>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions purchase_order_migration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
16 changes: 16 additions & 0 deletions purchase_order_migration/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2024 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Purchase Order Data Migration",
"version": "15.0.1.0.0",
"category": "Tools",
"author": "Quartile Limited",
"website": "https://www.quartile.co",
"license": "AGPL-3",
"depends": ["purchase_order_old_id", "data_migration"],
"data": [
"security/ir.model.access.csv",
"data/purchase_migration_scheduler.xml",
],
"installable": True,
}
16 changes: 16 additions & 0 deletions purchase_order_migration/data/purchase_migration_scheduler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding='UTF-8' ?>
<odoo noupdate="1">
<record model="ir.cron" id="ir_cron_purchase_order_data_migration">
<field
name="name"
>Purchase Order Data Migration from another odoo instance</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="state">code</field>
<field name="code">model._run_purchase_order_data_migration()</field>
<field name="doall" eval="False" />
<field name="active" eval="False" />
<field name="model_id" ref="model_purchase_order_migration" />
</record>
</odoo>
1 change: 1 addition & 0 deletions purchase_order_migration/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import purchase_order_migration
198 changes: 198 additions & 0 deletions purchase_order_migration/models/purchase_order_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# Copyright 2023 Quartile Limited
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import SUPERUSER_ID, api, models


class PurchaseOrderMigration(models.Model):
_name = "purchase.order.migration"
_inherit = ["data.migration", "data.migration.mapping"]
_description = "Purchase Order Data Migration"

def get_or_create_dummy_product(self):
env = api.Environment(self.env.cr, SUPERUSER_ID, {})
# Check if the dummy product already exists
dummy_product = env["product.product"].search(
[("default_code", "=", "DUMMY_PRODUCT")], limit=1
)
if dummy_product:
return dummy_product.id
else:
# If the dummy product does not exist, create it
dummy_product = env["product.product"].create(
{
"name": "Dummy Product",
"type": "product",
"default_code": "DUMMY_PRODUCT",
"taxes_id": False, # Assuming no taxes, adjust if necessary
}
)
return dummy_product.id

def _run_purchase_order_data_migration(self):
required_fields = [
"id",
"name",
"origin",
"partner_ref",
"date_order",
"date_approve",
"partner_id",
"dest_address_id",
"currency_id",
"state",
"notes",
"date_planned",
"amount_untaxed",
"amount_tax",
"amount_total",
"fiscal_position_id",
"payment_term_id",
"incoterm_id",
"company_id",
"address",
"remark",
"picking_type_id",
"sale_prediction_amount",
"request_channel_id",
"request_medium_id",
"call_back",
"state_id",
"city",
"street",
"street2",
"zip",
"phone_update",
"phone_search",
"supplier_phone",
"supplier_mobile",
"purchase_category_id",
"employee_id",
"shop_id",
"purchase_by_id",
"allow_convert",
"invoice_status",
# 'sale_order_id', # To check need to migrate or not
]
instance, uid_v11, models_v11 = self._data_migration_authentication()
offset = 0
limit = 1000
while True:
purchase_order_v11 = models_v11.execute_kw(
instance.instance_db,
uid_v11,
instance.password,
"purchase.order",
"search_read",
[],
{"fields": required_fields, "offset": offset, "limit": limit},
)
if not purchase_order_v11:
break
offset += limit

for purchase in purchase_order_v11:
self.with_delay()._process_purchase_order_migration(purchase)

def _process_purchase_order_migration(self, purchase):
env = api.Environment(self.env.cr, SUPERUSER_ID, {})
many2one_field_mapping = {
"incoterm_id": "account.incoterms",
"fiscal_position_id": "account.fiscal.position",
"company_id": "res.company",
"payment_term_id": "account.payment.term",
"currency_id": "res.currency",
"dest_address_id": "res.partner",
"partner_id": "res.partner",
"request_channel_id": "request.channel",
"request_medium_id": "request.medium",
"purchase_category_id": "purchase.category",
"employee_id": "hr.employee",
"shop_id": "stock.warehouse",
"purchase_by_id": "hr.employee",
"state_id": "res.country.state",
"sale_order_id": "sale.order",
"picking_type_id": "stock.picking.type",
}
description = purchase["picking_type_id"][1].split(": ")
purchase["picking_type_id"][1] = description[1]
for field, model in many2one_field_mapping.items():
if field in purchase:
purchase[field] = self.map_many2one_field_by_name(
env, model, purchase[field]
)
state = purchase["state"]
purchase["state"] = False
purchase["old_id"] = purchase["id"]
order = env["purchase.order"].create(purchase)
self.create_purchase_order_line(order, state)
order.write(
{
"amount_tax": purchase["amount_tax"],
"amount_untaxed": purchase["amount_untaxed"],
"amount_total": purchase["amount_total"],
"state": state,
}
)
order.invoice_status = purchase["invoice_status"]

def create_purchase_order_line(self, order, state):
required_fields = [
"product_id",
"account_analytic_id",
"analytic_tag_ids",
"company_id",
"date_planned",
"name",
"price_subtotal",
"price_tax",
"price_total",
"price_unit",
"product_qty",
"product_uom",
"qty_invoiced",
"qty_received",
"taxes_id",
]

instance, uid_v11, models_v11 = self._data_migration_authentication()
purchase_order_line_v11 = models_v11.execute_kw(
instance.instance_db,
uid_v11,
instance.password,
"purchase.order.line",
"search_read",
[],
{"fields": required_fields, "domain": [("order_id", "=", order.old_id)]},
)
for line in purchase_order_line_v11:
env = api.Environment(self.env.cr, SUPERUSER_ID, {})
many2one_field_mapping = {
"account_analytic_id": "account.analytic.account",
"analytic_tag_ids": "account.analytic.tag",
"company_id": "res.company",
"product_uom": "uom.uom",
}
dummy_product_id = self.get_or_create_dummy_product()
if state == "draft":
product_id = self.map_many2one_field_by_name(
env, "product.product", line["product_id"]
)
line["product_id"] = dummy_product_id
if product_id:
line["product_id"] = product_id
else:
line["product_id"] = dummy_product_id
for field, model in many2one_field_mapping.items():
if field in line and line[field]:
line[field] = self.map_many2one_field_by_name(
env, model, line[field]
)
for field, value in list(line.items()):
if isinstance(value, tuple) or isinstance(value, list):
line.pop(field, None)

line["order_id"] = order.id
order_line = env["purchase.order.line"].create(line)
order_line.taxes_id = False
order_line.write({"price_tax": line["price_tax"]})
2 changes: 2 additions & 0 deletions purchase_order_migration/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows you to migrate old data from the old system to the
current environment.
2 changes: 2 additions & 0 deletions purchase_order_migration/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_purchase_order_migration,purchase.order.migration,model_purchase_order_migration,base.group_system,1,1,1,1
Loading
Loading