|
| 1 | +# Copyright 2020 ForgeFlow <http://www.forgeflow.com> |
| 2 | +# Copyright 2021 Tecnativa - Pedro M. Baeza |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 4 | +from openupgradelib import openupgrade |
| 5 | +from openupgradelib import openupgrade_merge_records |
| 6 | + |
| 7 | + |
| 8 | +def run_dropshipping_functions(env): |
| 9 | + companies = env["res.company"].with_context(active_test=False).search([], order="id") |
| 10 | + mapping = {company.id: {} for company in companies} |
| 11 | + # Duplicate/create/adjust sequences |
| 12 | + sequence = env.ref('stock_dropshipping.seq_picking_type_dropship', False) |
| 13 | + if sequence and sequence.exists(): |
| 14 | + for i, company in enumerate(companies): |
| 15 | + vals = { |
| 16 | + "code": "stock.dropshipping", |
| 17 | + "name": "Dropship (%s)" % company.name, |
| 18 | + "company_id": company.id, |
| 19 | + } |
| 20 | + if i == 0: |
| 21 | + sequence.write(vals) |
| 22 | + new_sequence = sequence |
| 23 | + else: |
| 24 | + new_sequence = sequence.copy(vals) |
| 25 | + mapping[company.id]["sequence"] = new_sequence |
| 26 | + env['res.company'].create_missing_dropship_sequence() |
| 27 | + for company in companies: |
| 28 | + # Look for missing mapped sequences |
| 29 | + if not mapping[company.id].get("sequence"): |
| 30 | + sequence = env['ir.sequence'].search([ |
| 31 | + ('code', '=', 'stock.dropshipping'), |
| 32 | + ('company_id', '=', company.id), |
| 33 | + ]) |
| 34 | + mapping[company.id]["sequence"] = sequence |
| 35 | + # Duplicate/create/adjust picking types |
| 36 | + picking_type = env.ref('stock_dropshipping.picking_type_dropship', False) |
| 37 | + if picking_type and picking_type.exists(): |
| 38 | + for i, company in enumerate(companies): |
| 39 | + vals = { |
| 40 | + "sequence_id": mapping[company.id]["sequence"].id, |
| 41 | + "company_id": company.id, |
| 42 | + } |
| 43 | + if picking_type.default_location_dest_id.usage != "customer": |
| 44 | + vals["default_location_dest_id"] = env.ref('stock.stock_location_customers').id |
| 45 | + if picking_type.default_location_src_id.usage != "supplier": |
| 46 | + vals["default_location_src_id"] = env.ref('stock.stock_location_suppliers').id |
| 47 | + if i == 0: |
| 48 | + picking_type.write(vals) |
| 49 | + new_picking_type = picking_type |
| 50 | + else: |
| 51 | + new_picking_type = picking_type.copy(vals) |
| 52 | + mapping[company.id]["picking_type"] = new_picking_type |
| 53 | + for company in companies: |
| 54 | + # Look for missing mapped picking types |
| 55 | + if not mapping[company.id].get("picking_type"): |
| 56 | + dropship_picking_type = env['stock.picking.type'].search([ |
| 57 | + ('company_id', '=', company.id), |
| 58 | + ('default_location_src_id.usage', '=', 'supplier'), |
| 59 | + ('default_location_dest_id.usage', '=', 'customer'), |
| 60 | + ], limit=1, order='sequence') |
| 61 | + mapping[company.id]["picking_type"] = dropship_picking_type |
| 62 | + env['res.company'].create_missing_dropship_picking_type() |
| 63 | + # Duplicate/create/adjust stock rules |
| 64 | + stock_rule = env.ref('stock_dropshipping.stock_rule_drop_shipping', False) |
| 65 | + dropship_route = env.ref('stock_dropshipping.route_drop_shipping') |
| 66 | + if stock_rule and stock_rule.exists(): |
| 67 | + for i, company in enumerate(companies): |
| 68 | + supplier_location = stock_rule.location_src_id |
| 69 | + customer_location = stock_rule.location_id |
| 70 | + if supplier_location.usage != "supplier": |
| 71 | + supplier_location = env.ref('stock.stock_location_suppliers').id |
| 72 | + if customer_location.usage != "customer": |
| 73 | + customer_location = env.ref('stock.stock_location_customers').id |
| 74 | + dropship_picking_type = mapping[company.id]["picking_type"] |
| 75 | + vals = ({ |
| 76 | + "route_id": dropship_route.id, |
| 77 | + "picking_type_id": dropship_picking_type.id, |
| 78 | + "company_id": company.id, |
| 79 | + "location_src_id": supplier_location.id, |
| 80 | + "location_id": customer_location.id, |
| 81 | + "name": "%s → %s" % (supplier_location.name, customer_location.name), |
| 82 | + }) |
| 83 | + if i == 0: |
| 84 | + stock_rule.write(vals) |
| 85 | + new_stock_rule = stock_rule |
| 86 | + else: |
| 87 | + new_stock_rule = stock_rule.copy(vals) |
| 88 | + mapping[company.id]["stock_rule"] = new_stock_rule |
| 89 | + env['res.company'].create_missing_dropship_rule() |
| 90 | + for company in companies: |
| 91 | + # Look for missing mapped stock rules |
| 92 | + if not mapping[company.id].get("stock_rule"): |
| 93 | + stock_rule = env["stock.rule"].search([ |
| 94 | + ("route_id", "=", dropship_route.id), |
| 95 | + ("company_id", "=", company.id), |
| 96 | + ]) |
| 97 | + mapping[company.id]["stock_rule"] = stock_rule |
| 98 | + # Remove old XML-IDs |
| 99 | + env["ir.model.data"].search([ |
| 100 | + ("module", "=", "stock_dropshipping"), |
| 101 | + ("name", "in", ["seq_picking_type_dropship", |
| 102 | + "picking_type_dropship", "stock_rule_drop_shipping"]), |
| 103 | + ]).unlink() |
| 104 | + # now, we redirect referenced records to new records if needed company-wise |
| 105 | + for company in companies[1:]: |
| 106 | + openupgrade_merge_records._change_foreign_key_refs( |
| 107 | + env, |
| 108 | + "ir.sequence", |
| 109 | + sequence.ids, |
| 110 | + mapping[company.id]["sequence"].id, |
| 111 | + [], |
| 112 | + "ir_sequence", |
| 113 | + extra_where=" AND company_id = %s" % company.id, |
| 114 | + ) |
| 115 | + openupgrade_merge_records._change_foreign_key_refs( |
| 116 | + env, |
| 117 | + "stock.picking.type", |
| 118 | + picking_type.ids, |
| 119 | + mapping[company.id]["picking_type"].id, |
| 120 | + [], |
| 121 | + "stock_picking_type", |
| 122 | + extra_where=" AND company_id = %s" % company.id, |
| 123 | + ) |
| 124 | + openupgrade_merge_records._change_foreign_key_refs( |
| 125 | + env, |
| 126 | + "stock.rule", |
| 127 | + stock_rule.ids, |
| 128 | + mapping[company.id]["stock_rule"].id, |
| 129 | + [], |
| 130 | + "stock_rule", |
| 131 | + extra_where=" AND company_id = %s" % company.id, |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +@openupgrade.migrate() |
| 136 | +def migrate(env, version): |
| 137 | + run_dropshipping_functions(env) |
0 commit comments