Wizard to create stock.picking from another model - Odoo v8

空扰寡人 提交于 2019-12-12 04:43:43

问题


I have this code for the wizard:

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

isbns = fields.One2many('order.lines', 'order_id', 'ISBN')
production_order = fields.Many2one('bsi.print.order', 'Print Order')
company_id = fields.Many2one('res.company', 'Company',default='_default_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'):
        production_order = self.env.context['active_id']
        order = self.env['bsi.print.order'].browse(production_order)
        sp_types = self.env['stock.picking.type'].search([
        ('code', '=', 'incoming')
        ])
        if len(sp_types) > 0:
            for line in order.order_lines:
                if line.remaining_qty > 0:
                    val = {
                        #'origin': line.name,
                        'isbn':line.isbn.id,
                        'qty': line.remaining_qty,
                        'picking_type_id': sp_types[0].id,
                        }
                    isbns.append([0,0,val])
            res.update({'isbns':isbns,'production_order':production_order})
        return res

@api.multi    
def generate(self):
    if self.isbns:
        order_lines = []
        sp_types = self.env['stock.picking.type'].search([
        ('code', '=', 'incoming')
        ])
        print_order = self.env['stock.picking'].create({
            'origin': self.name.id,
            'state': 'draft',
            'stock_picking_lines': self.production_order.id,
            'picking_type_id': sp_types[0].id,
            })
        for line in self.isbns:
            order_lines.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([('production_order','=',self.production_order.id),
                                                           ('isbn','=',line.isbn.id)])
            prod_isbn.consumed_qty = line.qty
        print_order.write({'order_lines':[(6,0,map(lambda x:x.id,order_lines))]})
        tree_view_id = self.env.ref('stock.vpicktree').id
        form_view_id = self.env.ref('stock.view_picking_form').id
        self.production_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])]
             }

This should create a stock.picking from bsi.print.order

But it throws me this:

Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 546, in _handle_exception
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 583, in dispatch
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 319, in _call_function
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\service\model.py", line 118, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 316, in checked_call
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 812, in __call__
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 412, in response_wrap
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\web\controllers\main.py", line 948, in call_button
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\web\controllers\main.py", line 936, in _call_kw
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 268, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 399, in old_api
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\bsi\wizard\generate_stock_picking.py", line 45, in generate
AttributeError: 'generate.stock.picking' object has no attribute 'name'

I need to pass the name and few other fields, since stock.picking has One2many to stock.move.

This wizard was originally conceived to just pass lines from one model to another.

On this case it should pass lines as stock.move and few other fields outside lines to stock.picking.

I have this method which does something similar (and it actually works) but it's not a wizard:

@api.multi
def create_printy(self):
    copy_record = self.env['stock.picking'] 
    for record in self:
        order_lines = []
        for rec in record.order_lines:
            order_lines.append(
            (0,0,
            {
                'name': 'name',
                'product_id': rec.isbn.id,
                'product_uom': rec.isbn.uom_id.id,
                'date_expected': time.strftime(DEFAULT_SERVER_DATETIME_FORMAT),
                'product_uom_qty': rec.qty,
                'location_id': record.location_id.id,
                'location_dest_id': record.location_dest_id.id,
                }
            ))
        sp_types = self.env['stock.picking.type'].search([
        ('code', '=', 'incoming')
        ])
        if len(sp_types) > 0:
            copy_record.create({
                'origin': record.name,
                'picking_type_id': sp_types[0].id,
                'move_lines': order_lines, 
                'move_type': 'direct',
                'priority': '1',
                'company_id': record.company_id.id,
            })
    self.write({'state': 'delivered',},)

The lines I know already how to pass them, but how do I adapt the stock.picking fields? (ie: copy_record on create_printy method)

来源:https://stackoverflow.com/questions/47381869/wizard-to-create-stock-picking-from-another-model-odoo-v8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!