I added this code to create custom credit note to make custom return but i need to link it with sale order
how can i do that?
class ReturnPickingReversal(models.TransientModel):
_name = 'return.picking.reversal'
_description = 'Return Picking Account Reversal'
def reverse_moves(self):
return_picking = None
if self.env.context.get('active_id') and self.env.context.get('active_model') == 'stock.return.picking':
return_picking = self.env['stock.return.picking'].browse(self.env.context.get('active_id'))
print("return_picking", return_picking)
# Create reverse stock moves (Return Delivery Order)
for wizard in return_picking:
new_picking_id, pick_type_id = wizard._create_returns()
# Create reverse account moves (Credit Note)
picking_id = new_picking_id and self.env['stock.picking'].browse(new_picking_id) or None
moves = picking_id and picking_id.sale_id and self.env['account.move'].browse(
picking_id.sale_id.invoice_ids.ids)
current_user = picking_id.env.uid
invoice_line_list = []
for move_ids_without_package in picking_id.move_ids_without_package:
vals = (0, 0, {
'name': move_ids_without_package.description_picking,
'product_id': move_ids_without_package.product_id.id,
'price_unit': move_ids_without_package.so_product_price,
'account_id': move_ids_without_package.product_id.property_account_income_id.id if move_ids_without_package.product_id.property_account_income_id
else move_ids_without_package.product_id.categ_id.property_account_income_categ_id.id,
'tax_ids': [(6, 0, [
picking_id.company_id.account_sale_tax_id.id])] if picking_id.company_id.account_sale_tax_id else [],
'quantity': move_ids_without_package.product_uom_qty,
})
invoice_line_list.append(vals)
self.env['account.move'].create({
'move_type': 'out_refund',
'invoice_origin': picking_id.name,
'invoice_user_id': current_user,
'narration': picking_id.name,
'partner_id': picking_id.partner_id.id,
'currency_id': picking_id.env.user.company_id.currency_id.id,
'ref': _('Reversal for: %s') % self.reason.name if self.reason else '',
'date': self.date or False,
'invoice_date': self.date or False,
'journal_id': self.journal_id and self.journal_id.id or False,
'invoice_line_ids': invoice_line_list
})
ctx = dict(self.env.context)
ctx.update({
'default_partner_id': self.picking_id.partner_id.id,
'search_default_picking_type_id': pick_type_id,
'search_default_draft': False,
'search_default_assigned': False,
'search_default_confirmed': False,
'search_default_ready': False,
'search_default_late': False,
'search_default_available': False,
})
return {
'name': _('Returned Picking'),
'view_mode': 'form,tree,calendar',
'res_model': 'stock.picking',
'res_id': new_picking_id,
'type': 'ir.actions.act_window',
'context': ctx,
}
class ReturnReason(models.Model):
_name = 'return.reason'
name = fields.Char("Name")