Coder Social home page Coder Social logo

Comments (3)

pedrobaeza avatar pedrobaeza commented on August 14, 2024

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

  • Adding such comment on the ROADMAP of the module.
  • Also adding a domain to prevent to select product packs on the pack lines.
  • Or making this to work.

from product-pack.

Olimalt85 avatar Olimalt85 commented on August 14, 2024

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.",
)

`

from product-pack.

github-actions avatar github-actions commented on August 14, 2024

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.

from product-pack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.