0
votes

I created a module to add sale order lines as per customer to PO Order lines.

Selecting customer

When i add Sale Order it will be added in PO order.

SO added

I was trying to unlink a specific ids from one2many field. in picture Add SO fields

    class PurchaseOrder(models.Model):
    _inherit= "purchase.order"
    _name = "purchase.order"


    order_ids = fields.Many2many('sale.order', String="Add Order",domain="[('partner_id', 'child_of', partner_id),('state', 'in', ('quotation','socreated','done'))]")




    @api.onchange('order_ids')
    def orders_change(self):
        if not self.order_ids:
            return {}
        if not self.partner_customer_id:
            warning = {
                'title': _('Warning!'),
                'message': _('You must first select a partner!'),
            }
            # self.order_ids =False
            return {'warning': warning}

        line_ids = [] 

        u_ids=[]

        new_lines = self.env['purchase.order.line']


        for qt in self.order_ids:
            for i in self.order_line.mapped('sale_order_id'):

                line_ids.append(i)

            for u in self.order_ids:
                if u.id in line_id:
                    u_ids.append(u)
                    line_ids.remove(u)


            if line_ids and u_ids:
                lp = self.order_line.filtered(lambda r: r.sale_order_id <= line_ids[0])
                lp2 = self.order_line.filtered(lambda r: r.sale_order_id <= u_ids[0])


                for line in self.order_line:
                    if line in lp:
                        # self.order_line = [(6, 0, lp2.ids)]

                        line.unlink()

                continue



            for line in qt.order_line:
                # Load a PO line only once
                if line in self.order_line.mapped('sale_order_line_id'):
                    continue


                #seller section
                seller = line.product_id._select_seller(
                    line.product_id,
                    partner_id=self.partner_id,
                    quantity=line.product_uom_qty,
                    date=self.date_order and self.date_order[:10],
                    uom_id=line.product_uom)

                price_unit = self.env['account.tax']._fix_tax_included_price(seller.price,
                                                                             line.product_id.supplier_taxes_id,
                                                                             line.tax_id) if seller else 0.0
                if price_unit and seller and self.currency_id and seller.currency_id != self.currency_id:
                    price_unit = seller.currency_id.compute(price_unit, self.currency_id)

                if seller and line.product_uom and seller.product_uom != line.product_uom:
                    price_unit = self.env['product.uom']._compute_price(seller.product_uom.id, price_unit,
                                                                        to_uom_id=line.product_uom.id)
                unit = price_unit

                qty = line.product_uom_qty
                if float_compare(qty, 0.0, precision_rounding=line.product_uom.rounding) <= 0:
                    qty = 0.0
                tax_id = line.tax_id or line.product_id.taxes_id

                data = {
                    'sale_order_line_id': line.id,
                    'name': line.name,
                    'sequence_number':line.sequence_number,
                    'product_id': line.product_id.id,
                    'product_qty': qty,
                    'product_uom': line.product_id.uom_po_id or line.product_id.uom_id,
                    'price_unit': unit,
                    'cpo_no' : line.order_id.cpo_number,
                    'cpo_product_qty': qty,
                    'cpo_product_uom': line.product_id.uom_id,
                    'cpo_price_unit': line.price_unit,
                    'discount': 0.0,
                    'date_planned':(datetime.today() + relativedelta(weeks=4)).strftime(DEFAULT_SERVER_DATETIME_FORMAT),

                }
                new_line = new_lines.new(data)
                new_line._set_additional_fields(self)
                new_lines += new_line
        if new_lines :
            self.order_line += new_lines



class PurchaseOrderLine(models.Model):
    _inherit= "purchase.order.line"
    _name = "purchase.order.line"

    sale_order_line_id = fields.Many2one('sale.order.line', 'Order Line', ondelete='set null', select=True
                                        )

    sale_order_id = fields.Many2one('sale.order', related='sale_order_line_id.order_id', string='Order',
                                   store=False)

When i remove a order_ids , i want to unlink related lines from order_line (po)

link_ids will hold order_ids when it selected, when a id removed from order_ids it will be removed from link_ids. u_ids will hold the rest of order_id when it deleted.

when i remove a id from order_ids iwant to unlink the related line from order_line

but i cant delete it.

i have user [6,0,ids] method to replace values , it wont work in create state. Please help me.

4
when you change the order_ids the lines of selected SO addes normaly but when you remove it the lines will not ?Charif DZ
yes . if i delete any added id from order_ids i want to remove its line from order_lineVishnu VaNnErI
try to remove every record by using [(5,0,0)] then add the ids. this worked for me but the only problem is that it work if an other field trigger the onchange method not the same many2many or one2many fieldCharif DZ
but [(5,0,0)] won't work in create() right?Vishnu VaNnErI
if you didn't find any solution why don't you use update button when he change the selected order ids he needs to validate before showing the one2many field and this way you will work on write always because create will happen when he validate the the choices. what i'm trying to say is find what it work and work arround it to find a way to use itCharif DZ

4 Answers

2
votes
new_lines = self.env['purchase.order.line']
        for qt in self.order_ids:
            for i in self.order_line.mapped('sale_order_id'):
                line_id.append(i.id)
                line_ids.append(i)
            for u in self.order_ids:
                if u.id in line_id:
                    u_id.append(u.id)
                    line_id.remove(u.id)
                    u_ids.append(u)
                    line_ids.remove(u)
            k = self.order_line.mapped('sale_order_id')
            if line_id and line_ids and u_ids:
                lp = self.order_line.filtered(lambda r: r.sale_order_id <= line_ids[0])
                lp6 = self.order_line.filtered(lambda r: r.sale_order_id not in line_ids)
                lp2 = self.order_line.filtered(lambda r: r.sale_order_id in u_ids)

                for line in self.order_line:
                    if line in lp:
                        self.update({
                            'order_line': [(5, _, _)],
                        })
                for o in lp6:
                    data = {
                        'sale_order_line_id': o.sale_order_line_id.id,
                        'name': o.name,
                        'sequence_number': o.sequence_number,
                        'product_id': o.product_id.id,
                        'product_qty': o.product_qty,
                        'product_uom': o.product_uom,
                        'price_unit': o.price_unit,
                        'cpo_no': o.cpo_no,
                        'cpo_product_qty': o.cpo_product_qty,
                        'cpo_product_uom': o.cpo_product_uom,
                        'cpo_price_unit': o.cpo_price_unit,
                        # 'quote_ref':line.order_id.origin,
                        'discount': 0.0,
                        'date_planned': (datetime.today() + relativedelta(weeks=4)).strftime(
                            DEFAULT_SERVER_DATETIME_FORMAT),
                    }
                    for line in qt.order_line:
                                # Load a PO line only once
                        if line in self.order_line.mapped('sale_order_line_id'):
                            continue
                    new_line = new_lines.new(data)
                    new_line._set_additional_fields(self)
                    new_lines += new_line



                    new_line = False

                continue
1
votes

Following is a common logic for deleting a record in one2many

@api.onchage('field_name')
def onchange_field_name(self):
  for line in self.one2many_field_records:
    if line.order_id == 'your_satisfying_condition':
        line.unlink()

This is the usual way to delete the order line records

1
votes

Sorry i'm late it's been a log time:

try to remove every record by using [(5,0,0)] then add the ids. this worked for me but the only problem is that it work if an other field trigger the onchange method not the same many2many or one2many field

but in create mode if you didn't find any solution why don't you use update button when he change the selected order ids he needs to validate before showing the one2many field and this way you will work on write always because create will happen when he validate the the choices. what i'm trying to say is find what it work and work arround it to find a way to use it

0
votes

For the field you used to make relation:-

sale_order_id = fields.Many2one('sale.order', related='sale_order_line_id.order_id', string='Order',
                               store=False)

please try to give attribute ondelete = 'cascade' and change the line as :-

sale_order_id = fields.Many2one('sale.order', related='sale_order_line_id.order_id', string='Order', ondelete ='cascade'
                               store=False)

If you put like this , when the related sale order is deleted the one2many entry with relation record will be also removed. This is just an example and you could try this way. Thanks