def create_return(self, scheduled_date, qty=None, uom=None): """Intended to be invoked by the delivery wizard""" self._ensure_can_be_returned() self._ensure_qty_to_return(qty, uom) group_dict = {} for record in self.filtered('can_be_returned'): key = (record.partner_shipping_id.id, record.company_id.id, record.warehouse_id) group_dict.setdefault(key, self.env['rma']) group_dict[key] |= record for rmas in group_dict.values(): origin = ', '.join(rmas.mapped('name')) rma_out_type = rmas[0].warehouse_id.rma_out_type_id picking_form = Form( recordp=self.env['stock.picking'].with_context( default_picking_type_id=rma_out_type.id), view="stock.view_picking_form", ) rmas[0]._prepare_returning_picking(picking_form, origin) picking = picking_form.save() for rma in rmas: with picking_form.move_ids_without_package.new() as move_form: rma._prepare_returning_move(move_form, scheduled_date, qty, uom) # rma_id is not present in the form view, so we need to get # the 'values to save' to add the rma id and use the # create method intead of save the form. picking_vals = picking_form._values_to_save(all_fields=True) move_vals = picking_vals['move_ids_without_package'][-1][2] move_vals.update( picking_id=picking.id, rma_id=rma.id, move_orig_ids=[(4, rma.reception_move_id.id)], company_id=picking.company_id.id, ) self.env['stock.move'].sudo().create(move_vals) rma.message_post( body=_('Return: <a href="#" data-oe-model="stock.picking" ' 'data-oe-id="%d">%s</a> has been created.') % (picking.id, picking.name)) picking.action_confirm() picking.action_assign() picking.message_post_with_view( 'mail.message_origin_link', values={ 'self': picking, 'origin': rmas }, subtype_id=self.env.ref('mail.mt_note').id, ) self.write({'state': 'waiting_return'})
def _prepare_invoice(self, date_invoice, journal=None): """Prepare in a Form the values for the generated invoice record. :return: A tuple with the vals dictionary and the Form with the preloaded values for being used in lines. """ self.ensure_one() if not journal: journal = ( self.journal_id if self.journal_id.type == self.contract_type else self.env["account.journal"].search( [ ("type", "=", self.contract_type), ("company_id", "=", self.company_id.id), ], limit=1, ) ) if not journal: raise ValidationError( _("Please define a %s journal for the company '%s'.") % (self.contract_type, self.company_id.name or "") ) invoice_type = "out_invoice" if self.contract_type == "purchase": invoice_type = "in_invoice" move_form = Form( self.env["account.move"].with_context( force_company=self.company_id.id, default_type=invoice_type ) ) move_form.partner_id = self.invoice_partner_id if self.payment_term_id: move_form.invoice_payment_term_id = self.payment_term_id if self.fiscal_position_id: move_form.fiscal_position_id = self.fiscal_position_id invoice_vals = move_form._values_to_save(all_fields=True) invoice_vals.update( { "ref": self.code, "company_id": self.company_id.id, "currency_id": self.currency_id.id, "invoice_date": date_invoice, "journal_id": journal.id, "invoice_origin": self.name, "user_id": self.user_id.id, } ) return invoice_vals, move_form
def action_refund(self): """Invoked when 'Refund' button in rma form view is clicked and 'rma_refund_action_server' server action is run. """ group_dict = {} for record in self.filtered("can_be_refunded"): key = (record.partner_invoice_id.id, record.company_id.id) group_dict.setdefault(key, self.env['rma']) group_dict[key] |= record for rmas in group_dict.values(): origin = ', '.join(rmas.mapped('name')) invoice_form = Form( self.env['account.invoice'].with_context( default_type='out_refund', company_id=rmas[0].company_id.id, ), "account.invoice_form") rmas[0]._prepare_refund(invoice_form, origin) refund = invoice_form.save() for rma in rmas: with invoice_form.invoice_line_ids.new() as line_form: rma._prepare_refund_line(line_form) # rma_id is not present in the form view, so we need to get # the 'values to save' to add the rma id and use the # create method instead of save the form. We also need # the new refund line id to be linked to the rma. refund_vals = invoice_form._values_to_save(all_fields=True) line_vals = refund_vals['invoice_line_ids'][-1][2] line_vals.update(invoice_id=refund.id, rma_id=rma.id) line_vals.update(rma._get_extra_refund_line_vals()) line = self.env['account.invoice.line'].create(line_vals) rma.write({ 'refund_line_id': line.id, 'refund_id': refund.id, 'state': 'refunded', }) refund.compute_taxes() refund.message_post_with_view( 'mail.message_origin_link', values={ 'self': refund, 'origin': rmas }, subtype_id=self.env.ref('mail.mt_note').id, )