Sunday 20 December 2015

How to defaultly create invoice at the time of transferring





class stock_picking(models.Model):
    _inherit = "stock.picking"




    @api.cr_uid_ids_context
    def do_transfer(self, cr, uid, picking_ids, context=None):
        res = super(stock_picking,self).do_transfer(cr, uid, picking_ids, context)
        inv_type = ''
        picking_pool = self.pool.get('stock.picking')
        pickings = picking_pool.browse(cr, uid, picking_ids[0], context=context)
        pick = pickings and pickings[0]
        if not pick or not pick.move_lines:
            return 'sale'
        type = pick.picking_type_id.code
        usage = pick.move_lines[0].location_id.usage if type == 'incoming' else pick.move_lines[0].location_dest_id.usage

        journal_type = JOURNAL_TYPE_MAP.get((type, usage), ['sale'])[0] or 'out_invoice'
        if journal_type == 'sale':
            inv_type = 'out_invoice'
        elif journal_type == 'purchase':
            inv_type = 'in_invoice'

        elif journal_type == 'sale_refund':
            inv_type = 'out_refund'
        else:
            inv_type = 'in_refund'
                               
        journal_obj = self.pool.get('account.journal')
        journals = journal_obj.search(cr, uid, [('type', '=', journal_type)])
        journal_id = journals and journals[0]
        picking_pool.action_invoice_create(cr, uid, picking_ids,
              journal_id = journal_id,
              group = False,
              type = inv_type,
              context=context)
   
        return res

No comments:

Post a Comment