1

Following my previous question

I have managed to add the remaining fields to the wizard.

However, I'm still facing some issue with it, this is my current code:

class generate_stock_picking(models.TransientModel):
    _name = 'generate.stock.picking'

    isbns = fields.One2many('order.picking', 'order_id', 'ISBN')
    print_order = fields.Many2one('bsi.print.order', 'Print Order')
    company_id = fields.Many2one('res.company', 'Company')
    location_id = fields.Many2one('stock.location', string="Source Location")
    location_dest_id = fields.Many2one('stock.location', string="Destination Location")

@api.model
def default_get(self, fields):
    res = super(generate_stock_picking, self).default_get(fields)
    isbns = []
    if self.env.context.has_key('active_id'):
        print_order = self.env.context['active_id']
        order = self.env['bsi.print.order'].browse(print_order)
        sp_types = self.env['stock.picking.type'].search([
        ('code', '=', 'incoming')
        ])
        if len(sp_types) > 0:
            for line in order.order_picking:
                if line.remaining_qty > 0:
                    val = {
                        #'origin': line.name,
                        'name': 'name',
                        'isbn': line.isbn.id,
                        'qty': line.remaining_qty,
                        'picking_type_id': sp_types[0].id,
                        'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                        }
                    isbns.append([0,0,val])
            res.update({'isbns':isbns,'print_order':print_order})
        return res

@api.multi    
def generate(self):
    print_order = self.env.context['active_id']
    order = self.env['bsi.print.order'].browse(print_order)
    for record in order:
        if self.isbns:
            order_picking = []
            sp_types = self.env['stock.picking.type'].search([
            ('code', '=', 'incoming')
            ])
            print_order = self.env['stock.picking'].create({
                'origin': record.name,
                'state': 'draft',
                'stock_picking_lines': self.print_order.id,
                'picking_type_id': sp_types[0].id,
                })
            for line in self.isbns:
                order_picking.append(self.env['bsi.print.order.lines'].create({
                    'print_order':print_order.id,
                    'isbn':line.isbn.id,
                    'qty':line.qty}))
                prod_isbn = self.env['bsi.print.order.lines'].search([('print_order','=',self.print_order.id),
                                                           ('isbn','=',line.isbn.id)])
                prod_isbn.consumed_qty = line.qty
            print_order.write({'order_picking':[(6,0,map(lambda x:x.id,order_picking))]})
            tree_view_id = self.env.ref('stock.vpicktree').id
            form_view_id = self.env.ref('stock.view_picking_form').id
            self.print_order.state = 'delivered'
            return {
                'name': _('Stock Picking'),
                'type': 'ir.actions.act_window',
                'res_model': 'stock.picking',
                'view_mode': 'tree,form',
                'view_type': 'form',
                'views': [(tree_view_id, 'tree'),(form_view_id, 'form')],
                'view_id':tree_view_id,
                'res_id': [print_order.id],
                'domain': [('id', 'in', [print_order.id])]
                 }


class order_picking(models.TransientModel):
    _name = 'order.picking'

    order_id = fields.Many2one('generate.stock.picking', 'Order')
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")

When I click on the wizard, which I call from my model like this:

@api.multi
def open_wizard(self):
    return {
        'context': self.env.context,        
        'name': 'Move Product',
        'view_type': 'form',
        'view_mode': 'form',
        'res_model': 'generate.stock.picking',
        'type': 'ir.actions.act_window',
    }

It throws me this:

       Integrity Error

The operation cannot be completed, probably due to the following:
- deletion: you may be trying to delete a record while other records still reference it
- creation/update: a mandatory field is not correctly set

[object with reference: bsi.print.order - bsi.print.order] 

The object bsi.print.order is the class from which I'm trying to create the stock.picking.

Any ideas?

If You need further explanation, please let me know.

NeoVe
  • 3,857
  • 8
  • 54
  • 134

0 Answers0