OCA / product-pack

Odoo modules related to product packs
GNU Affero General Public License v3.0
45 stars 126 forks source link

[14.0] sale_product_pack : Duplicating order with pack lines, new lines of components are added #107

Closed Olimalt85 closed 1 year ago

Olimalt85 commented 1 year ago

Module : sale_product_pack Version : 14.0

When i duplicated my order with a product composed of other packs, some new lines with components are added

Steps to reproduce the behavior:

  1. Create your packs of products image image image

  2. Create a sale order with this pack product (pack 1) (Pack column is the field 'pack_parent_line_id') image

  3. Duplicate the order : image

Result : image

Expected : There should't be new lines added.

I first met the problem in my local odoo but then i tried in the runbot and it has the same behaviour.

pedrobaeza commented 1 year ago

The module is not prepared for nesting product packs. If you want, you can contribute with:

Olimalt85 commented 1 year ago

Here is what i did to be able to duplicate them without creating new lines and keeping the link between the componants and pack_parent_line_id.

I suppose that we can do it better

` from odoo import api, models, fields

class SaleOrder(models.Model): _inherit = "sale.order"

#I need this field in the sale order to avoid them create lines of composants in the expand_pack_line method when i duplicate one order
is_copy = fields.Boolean("Is a copy")

def _reset_sequence_before_copy(self):
    '''I need this method to deduplicate lines that have the same sequence in my order before i duplicate it (from module sale_order_line_sequence)'''
    for rec in self:
        current_sequence = 1
        for line in sorted(rec.order_line, key=lambda x: (x.sequence, x.id)):
            if line.sequence != current_sequence:
                line.sequence = current_sequence
            current_sequence += 1

def copy(self, default=None):

    #I deduplicate the sequences of the order lines because i will use it to identify and link the new lines to the original lines
    self._reset_sequence_before_copy()

    #I am using the field is_copy to avoid creating new line of pack when a pack is a copy from another pack
    if default:
        default['is_copy']=True
    else:
        default={'is_copy':True}
    sale_copy = super(SaleOrder, self).copy(default)

    #Delete the componants products because we are going to recopy them
    sale_copy.order_line.filtered(lambda l: l.pack_parent_line_id).unlink()

    #I copy the sequence number of the original line to keep the same sequence order
    for lines in sale_copy.order_line:
        lines.copied_sale_line_id = self.order_line.filtered(lambda r:r.sequence == lines.sequence and r.product_id.id == lines.product_id.id).id

    #search all the lines that have been deleted by the sale_product_pack module
    lines_to_copy_from_old = self.order_line.filtered(
                lambda l: l.pack_parent_line_id
            )

    #Copy them into the new order with the link to the original line and with its sequence
    for line in lines_to_copy_from_old:
        line.copy(default={'order_id': sale_copy.id,
                           'copied_sale_line_id': line.id,
                           'sequence': line.sequence})

    #Select all the lines that are liked to componants lines in the the order
    new_pack_lines = sale_copy.order_line.filtered(lambda r: r.copied_sale_line_id.pack_child_line_ids)

    #Modify field pack_parent_line_id of the componants that have been duplicated
    for new_pack_line in new_pack_lines:
        lines_to_link_to_current_pack = sale_copy.order_line.filtered(lambda r: r.id in new_pack_line.copied_sale_line_id.pack_child_line_ids.ids)
        lines_to_link_to_current_pack.write({'pack_parent_line_id': new_pack_line.id})

    #Disable the field is_copy so that we can add the pack products and allow it to add the componants lines
    sale_copy.is_copy=False
    return sale_copy

class SaleOrderLine(models.Model): _inherit = "sale.order.line"

def expand_pack_line(self, write=False):
    if not self.order_id.is_copy:
        super(SaleOrderLine, self).expand_pack_line(write)

copied_sale_line_id = fields.Many2one(
    "sale.order.line",
    "Copied sale_line_id",
    help="The copied sale_line_id.",
)

`

github-actions[bot] commented 1 year ago

There hasn't been any activity on this issue in the past 6 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 30 days. If you want this issue to never become stale, please ask a PSC member to apply the "no stale" label.