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

[15.0][MIG] payment: migration script #3513

Merged
merged 2 commits into from
Jan 13, 2023
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
2 changes: 1 addition & 1 deletion docsource/modules140-150.rst
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ Module coverage 14.0 -> 15.0
+-------------------------------------------------+----------------------+-------------------------------------------------+
| partner_autocomplete |Nothing to do | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| payment | | |
| payment | Done | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
| payment_adyen | | |
+-------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
39 changes: 37 additions & 2 deletions openupgrade_scripts/scripts/payment/15.0.2.0/noupdate_changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,34 @@
</record>
<record id="payment_acquirer_authorize" model="payment.acquirer">
<field name="payment_icon_ids" eval="[(6, 0, [ ref('payment.payment_icon_cc_maestro'), ref('payment.payment_icon_cc_mastercard'), ref('payment.payment_icon_cc_discover'), ref('payment.payment_icon_cc_diners_club_intl'), ref('payment.payment_icon_cc_jcb'), ref('payment.payment_icon_cc_visa'), ])]"/>
<field name="sequence"/>
<!-- <field name="sequence"/>-->
</record>
<record id="payment_acquirer_buckaroo" model="payment.acquirer">
<field name="payment_icon_ids" eval="[(6, 0, [ ref('payment.payment_icon_cc_bancontact'), ref('payment.payment_icon_cc_maestro'), ref('payment.payment_icon_cc_mastercard'), ref('payment.payment_icon_cc_visa'), ref('payment.payment_icon_cc_american_express'), ])]"/>
</record>
<record id="payment_acquirer_ogone" model="payment.acquirer">
<field name="description" type="html">
<p>
Ogone supports a wide range of payment methods: credit cards, debit cards, bank
transfers, Bancontact, iDeal, Giropay.
</p>
<ul class="list-inline">
<li class="list-inline-item"><i class="fa fa-check"/>Online Payment</li>
<li class="list-inline-item"><i class="fa fa-check"/>Payment Status Tracking</li>
<li class="list-inline-item"><i class="fa fa-check"/>Subscriptions</li>
<li class="list-inline-item"><i class="fa fa-check"/>Save Cards</li>
<li class="list-inline-item"><i class="fa fa-check"/>Embedded Credit Card Form</li>
</ul>
</field>
<field name="display_as">Credit Card (powered by Ogone)</field>
<field name="module_id" ref="base.module_payment_ogone"/>
<field name="name">Ogone</field>
<field name="payment_icon_ids" eval="[(6, 0, [ ref('payment.payment_icon_cc_ideal'), ref('payment.payment_icon_cc_bancontact'), ref('payment.payment_icon_cc_maestro'), ref('payment.payment_icon_cc_mastercard'), ref('payment.payment_icon_cc_visa'), ])]"/>
<!-- <field name="sequence"/>-->
</record>
<record id="payment_acquirer_paypal" model="payment.acquirer">
<field name="payment_icon_ids" eval="[(6, 0, [ ref('payment.payment_icon_cc_maestro'), ref('payment.payment_icon_cc_mastercard'), ref('payment.payment_icon_cc_discover'), ref('payment.payment_icon_cc_diners_club_intl'), ref('payment.payment_icon_cc_jcb'), ref('payment.payment_icon_cc_american_express'), ref('payment.payment_icon_cc_unionpay'), ref('payment.payment_icon_cc_visa'), ])]"/>
<field name="sequence"/>
<!-- <field name="sequence"/>-->
</record>
<record id="payment_acquirer_payulatam" model="payment.acquirer">
<field name="description" type="html">
Expand All @@ -51,6 +71,18 @@
</field>
<field name="payment_icon_ids" eval="[(6, 0, [ ref('payment.payment_icon_cc_diners_club_intl'), ref('payment.payment_icon_cc_mastercard'), ref('payment.payment_icon_cc_american_express'), ref('payment.payment_icon_cc_visa'), ref('payment.payment_icon_cc_codensa_easy_credit'), ])]"/>
</record>
<record id="payment_acquirer_payumoney" model="payment.acquirer">
<field name="description" type="html">
<p>
PayUmoney is an online payments solutions company serving the Indian market.
</p>
<ul class="list-inline">
<li class="list-inline-item"><i class="fa fa-check"/>Online Payment</li>
<li class="list-inline-item"><i class="fa fa-check"/>Payment Status Tracking</li>
</ul>
</field>
<field name="payment_icon_ids" eval="[(6, 0, [ ref('payment.payment_icon_cc_maestro'), ref('payment.payment_icon_cc_mastercard'), ref('payment.payment_icon_cc_american_express'), ref('payment.payment_icon_cc_visa'), ])]"/>
</record>
<record id="payment_acquirer_sepa_direct_debit" model="payment.acquirer">
<field name="description" type="html">
<p>
Expand Down Expand Up @@ -83,6 +115,9 @@
<field name="pending_msg"/>
<field name="sequence">30</field>
</record>
<record id="payment_token_billing_rule" model="ir.rule">
<field name="name">Access every token</field>
</record>
<record id="payment_token_user_rule" model="ir.rule">
<field name="name">Access only tokens belonging to commercial partner</field>
</record>
Expand Down
104 changes: 104 additions & 0 deletions openupgrade_scripts/scripts/payment/15.0.2.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from openupgradelib import openupgrade


def fill_payment_adquirer_allow_tokenization(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_acquirer pa
SET allow_tokenization = TRUE
WHERE save_token IN ('always', 'ask')""",
)


def fill_payment_transaction_tokenize(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_transaction pt
SET tokenize = TRUE
WHERE type = 'form_save'""",
)


def convert_payment_acquirer_provider(env):
openupgrade.logged_query(
env.cr,
f"""
UPDATE payment_acquirer
SET provider = 'none'
WHERE {openupgrade.get_legacy_name('provider')} = 'manual'""",
)


def fill_payment_transaction_last_state_change(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_transaction
SET last_state_change = write_date
WHERE last_state_change IS NULL""",
)


def fill_payment_transaction_partner_state_id(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_transaction pt
SET partner_state_id = rp.state_id
FROM res_partner rp
WHERE rp.id = pt.partner_id
AND rp.state_id IS NOT NULL
AND partner_state_id IS NULL""",
)


def create_account_payment_method_line(env):
# Create account payment method lines from account payment methods
openupgrade.logged_query(
env.cr,
"""
INSERT INTO account_payment_method_line (name, sequence,
payment_method_id, journal_id, create_uid, write_uid,
create_date, write_date)
SELECT DISTINCT ON (apm.id, aj.id) apm.name, 10, apm.id, aj.id,
apm.create_uid, apm.write_uid, apm.create_date, apm.write_date
FROM account_payment_method apm
JOIN payment_acquirer pa ON pa.provider = apm.code
JOIN account_journal aj ON aj.type = 'bank' AND aj.id = pa.journal_id
WHERE apm.code NOT IN ('manual', 'check_printing')
""",
)


@openupgrade.migrate()
def migrate(env, version):
fill_payment_adquirer_allow_tokenization(env)
fill_payment_transaction_tokenize(env)
convert_payment_acquirer_provider(env)
fill_payment_transaction_partner_state_id(env)
fill_payment_transaction_last_state_change(env)
create_account_payment_method_line(env)
openupgrade.load_data(env.cr, "payment", "15.0.2.0/noupdate_changes.xml")
openupgrade.delete_record_translations(
env.cr,
"payment",
[
"payment_acquirer_adyen",
"payment_acquirer_alipay",
"payment_acquirer_ogone",
"payment_acquirer_payulatam",
"payment_acquirer_payumoney",
"payment_acquirer_sepa_direct_debit",
"payment_acquirer_stripe",
"payment_token_billing_rule",
"payment_token_user_rule",
"payment_transaction_billing_rule",
"payment_transaction_user_rule",
],
)
openupgrade.delete_records_safely_by_xml_id(
env,
["payment.payment_acquirer_odoo_by_adyen", "payment.default_acquirer_button"],
)
100 changes: 100 additions & 0 deletions openupgrade_scripts/scripts/payment/15.0.2.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from openupgradelib import openupgrade

_copied_columns = {
"payment_acquirer": [
("provider", None, None),
],
}

_renamed_fields = [
(
"payment.token",
"payment_token",
"payment_ids",
"transaction_ids",
),
(
"payment.transaction",
"payment_transaction",
"is_processed",
"is_post_processed",
),
(
"payment.transaction",
"payment_transaction",
"payment_token_id",
"token_id",
),
(
"payment.acquirer",
"payment_acquirer",
"registration_view_template_id",
"inline_form_view_id",
),
]

_renamed_xmlids = [
("payment.payment_token_salesman_rule", "payment.payment_token_billing_rule"),
("payment.payment_acquirer_ingenico", "payment.payment_acquirer_ogone"),
("payment.payment_acquirer_payu", "payment.payment_acquirer_payumoney"),
]


def fill_payment_token_name(env):
# field it's required now
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_token
SET name = 'XXXXXXXXXXXX????'
WHERE name IS NULL""",
)


def fill_payment_transaction_partner_id(env):
# field it's required now
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_transaction pt
SET partner_id = COALESCE(ap.partner_id, rc.partner_id)
FROM account_payment ap
JOIN account_move am ON ap.move_id = am.id
JOIN account_journal aj ON am.journal_id = aj.id
JOIN res_company rc ON aj.company_id = rc.id
WHERE pt.partner_id IS NULL
AND ap.payment_token_id = pt.token_id
AND (ap.payment_transaction_id = pt.id OR pt.payment_id = ap.id)""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_transaction pt
SET partner_id = COALESCE(ap.partner_id, rc.partner_id)
FROM account_payment ap
JOIN account_move am ON ap.move_id = am.id
JOIN account_journal aj ON am.journal_id = aj.id
JOIN res_company rc ON aj.company_id = rc.id
WHERE pt.partner_id IS NULL
AND (ap.payment_transaction_id = pt.id OR pt.payment_id = ap.id)""",
)


def delete_payment_adquirer_inline_form_view_id(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE payment_acquirer pa
SET inline_form_view_id = NULL
WHERE payment_flow = 'form'""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.copy_columns(env.cr, _copied_columns)
openupgrade.rename_fields(env, _renamed_fields)
openupgrade.rename_xmlids(env.cr, _renamed_xmlids)
fill_payment_token_name(env)
fill_payment_transaction_partner_id(env)
delete_payment_adquirer_inline_form_view_id(env)
Loading