@ -22,76 +22,61 @@
from odoo . exceptions import UserError
from odoo import models , fields , api , _
import logging
_logger = logging . getLogger ( __name__ )
class InvoiceStockMove ( models . Model ) :
_inherit = ' account.move '
@api . onchange ( ' type ' , ' invoice_line_ids ' , ' id ' , ' state ' )
def onchange_invoice_type ( self ) :
picking_type_code = fields . Char ( compute = " _compute_picking_type_code_ANDtrasferANDpicking_type " , store = True , help = " field used as domain filter for picking_type_id and is based on type of invoice " )
transfer_state = fields . Selection ( [ ( ' not_initiated ' , ' not_initiated ' ) ,
( ' nothing_to_transfer ' , ' nothing_to_transfer ' ) ,
( ' transfer_created ' , ' transfer created ' ) ,
( ' waiting_transfer ' , ' waiting_transfer ' ) ,
( ' make_the_transfer_from_purchase ' , ' make_the_transfer_from_purchase ' ) ,
( ' make_the_transfer_from_sale ' , ' make_the_transfer_from_sale ' ) ,
# ('created_from_another_object_edit_lines_to_make_tranfer','created_from_another_object_edit_lines_to_make_tranfer'),
] , string = ' Stock Transfer State ' , help = ' If the transfer form invoice was done or not ' ,
compute = ' _compute_picking_type_code_ANDtrasferANDpicking_type ' , )
if self . env . context . get ( ' default_team_id ' , False ) or ( self . _fields . get ( ' team_id ' , False ) and self . invoice_origin ) : # is a invoice from sale order
picking_type_id = fields . Many2one ( ' stock.picking.type ' , ' Picking Type ' , required = False ,
help = " This will determine picking type of incoming shipment where will go the goods " ,
copy = False , )
invoice_picking_id = fields . Many2one ( ' stock.picking ' , string = " Picking Id " , copy = False )
picking_status = fields . Selection ( related = ' invoice_picking_id.state ' , string = ' Picking Status ' )
@api . depends ( ' type ' , ' state ' , ' invoice_line_ids ' , ' invoice_picking_id ' )
def _compute_picking_type_code_ANDtrasferANDpicking_type ( self ) :
for record in self :
code = ' '
if record . type in [ ' out_invoice ' , ' out_receipt ' ] :
code = ' outgoing '
elif record . type in [ ' in_invoice ' , ' in_receipt ' ] :
code = ' incoming '
record . picking_type_code = code
if ( record . _context . get ( ' active_model ' ) == ' sale.order ' ) or ( record . env . context . get ( ' default_team_id ' , False ) ) or ( record . _fields . get ( ' team_id ' , False ) and record . invoice_origin ) : # is a invoice from sale order
transfer_state = ' make_the_transfer_from_sale '
self . picking_type_id = False
self . transfer_state = transfer_state
# record.picking_type_id = False
record . transfer_state = transfer_state
return
if self . env . context . get ( ' default_purchase_id ' , False ) or ( self . _fields . get ( ' purchase_id ' , False ) and self . purchase_id ) : # is a invoice from purchase order
if ( record . _context . get ( ' active_model ' ) == ' purchase.order ' ) or ( record . env . context . get ( ' default_purchase_id ' , False ) ) or ( record . _fields . get ( ' purchase_id ' , False ) and record . purchase_id ) : # is a invoice from purchase order
transfer_state = ' make_the_transfer_from_purchase '
self . picking_type_id = False
self . transfer_state = transfer_state
# record.picking_type_id = False
record . transfer_state = transfer_state
return
if record . invoice_picking_id :
record . transfer_state = ' transfer_created '
return
transfer_state = ' nothing_to_transfer '
for line in self . invoice_line_ids :
for line in record . invoice_line_ids :
if line . product_id :
if line . product_id . type == ' product ' :
transfer_state = ' not_initiated '
break
self . transfer_state = transfer_state
domain = [ ( ' id ' , ' = ' , 0 ) ]
if transfer_state == ' not_initiated ' :
if self . type in [ ' out_invoice ' , ' out_receipt ' ] :
domain = [ ( ' code ' , ' = ' , ' outgoing ' ) ]
elif self . type in [ ' in_invoice ' , ' in_receipt ' ] :
domain = [ ( ' code ' , ' = ' , ' incoming ' ) ]
if not self . picking_type_id :
type_obj = self . env [ ' stock.picking.type ' ]
company_id = self . env . context . get ( ' company_id ' ) or self . env . user . company_id . id
full_domain = domain + [ ( ' warehouse_id.company_id ' , ' = ' , company_id ) ]
self . picking_type_id = type_obj . search ( full_domain , limit = 1 )
# types = type_obj.search([('code', '=', 'incoming'), ('warehouse_id', '=', False)])
return { ' domain ' : { ' picking_type_id ' : domain } }
@api . model
def _get_default_statep ( self ) :
if self . _context . get ( ' active_model ' ) == ' sale.order ' :
return ' make_the_transfer_from_sale '
if self . _context . get ( ' active_model ' ) == ' purchase.order ' :
return ' make_the_transfer_from_purchase '
return ' created_from_another_object_edit_lines_to_make_tranfer '
transfer_state = fields . Selection ( [ ( ' not_initiated ' , ' not_initiated ' ) ,
( ' nothing_to_transfer ' , ' nothing_to_transfer ' ) ,
( ' transfered ' , ' transfered ' ) ,
( ' waiting_transfer ' , ' waiting_transfer ' ) ,
( ' make_the_transfer_from_purchase ' , ' make_the_transfer_from_purchase ' ) ,
( ' make_the_transfer_from_sale ' , ' make_the_transfer_from_sale ' ) ,
( ' created_from_another_object_edit_lines_to_make_tranfer ' , ' created_from_another_object_edit_lines_to_make_tranfer ' ) ,
] , string = ' Stock Transfer State ' , help = ' If the transfer form invoice was done or not ' ,
default = _get_default_statep , copy = False )
picking_type_id = fields . Many2one ( ' stock.picking.type ' , ' Picking Type ' , required = False ,
help = " This will determine picking type of incoming shipment " ,
copy = False , )
invoice_picking_id = fields . Many2one ( ' stock.picking ' , string = " Picking Id " , copy = False )
picking_status = fields . Selection ( related = ' invoice_picking_id.state ' )
record . transfer_state = transfer_state
def action_stock_move ( self ) :
@ -118,7 +103,7 @@ class InvoiceStockMove(models.Model):
}
picking = self . env [ ' stock.picking ' ] . create ( pick )
self . invoice_picking_id = picking . id
self . transfer_state = ' transfered '
self . transfer_state = ' transfer_creat ed '
moves = order . invoice_line_ids . filtered ( lambda r : r . product_id . type in [ ' product ' , ' consu ' ] ) . _create_stock_moves ( picking )
move_ids = moves . _action_confirm ( )