diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/account_debit_note/wizard | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/account_debit_note/wizard')
| -rw-r--r-- | addons/account_debit_note/wizard/__init__.py | 4 | ||||
| -rw-r--r-- | addons/account_debit_note/wizard/account_debit_note.py | 90 | ||||
| -rw-r--r-- | addons/account_debit_note/wizard/account_debit_note_view.xml | 40 |
3 files changed, 134 insertions, 0 deletions
diff --git a/addons/account_debit_note/wizard/__init__.py b/addons/account_debit_note/wizard/__init__.py new file mode 100644 index 00000000..2048c309 --- /dev/null +++ b/addons/account_debit_note/wizard/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import account_debit_note
\ No newline at end of file diff --git a/addons/account_debit_note/wizard/account_debit_note.py b/addons/account_debit_note/wizard/account_debit_note.py new file mode 100644 index 00000000..f3f9a42e --- /dev/null +++ b/addons/account_debit_note/wizard/account_debit_note.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- +from odoo import models, fields, api +from odoo.tools.translate import _ +from odoo.exceptions import UserError + + +class AccountDebitNote(models.TransientModel): + """ + Add Debit Note wizard: when you want to correct an invoice with a positive amount. + Opposite of a Credit Note, but different from a regular invoice as you need the link to the original invoice. + In some cases, also used to cancel Credit Notes + """ + _name = 'account.debit.note' + _description = 'Add Debit Note wizard' + + move_ids = fields.Many2many('account.move', 'account_move_debit_move', 'debit_id', 'move_id', + domain=[('state', '=', 'posted')]) + date = fields.Date(string='Debit Note Date', default=fields.Date.context_today, required=True) + reason = fields.Char(string='Reason') + journal_id = fields.Many2one('account.journal', string='Use Specific Journal', + help='If empty, uses the journal of the journal entry to be debited.') + copy_lines = fields.Boolean("Copy Lines", + help="In case you need to do corrections for every line, it can be in handy to copy them. " + "We won't copy them for debit notes from credit notes. ") + # computed fields + move_type = fields.Char(compute="_compute_from_moves") + journal_type = fields.Char(compute="_compute_from_moves") + + @api.model + def default_get(self, fields): + res = super(AccountDebitNote, self).default_get(fields) + move_ids = self.env['account.move'].browse(self.env.context['active_ids']) if self.env.context.get('active_model') == 'account.move' else self.env['account.move'] + if any(move.state != "posted" for move in move_ids): + raise UserError(_('You can only debit posted moves.')) + res['move_ids'] = [(6, 0, move_ids.ids)] + return res + + @api.depends('move_ids') + def _compute_from_moves(self): + for record in self: + move_ids = record.move_ids + record.move_type = move_ids[0].move_type if len(move_ids) == 1 or not any(m.move_type != move_ids[0].move_type for m in move_ids) else False + record.journal_type = record.move_type in ['in_refund', 'in_invoice'] and 'purchase' or 'sale' + + def _prepare_default_values(self, move): + if move.move_type in ('in_refund', 'out_refund'): + type = 'in_invoice' if move.move_type == 'in_refund' else 'out_invoice' + else: + type = move.move_type + default_values = { + 'ref': '%s, %s' % (move.name, self.reason) if self.reason else move.name, + 'date': self.date or move.date, + 'invoice_date': move.is_invoice(include_receipts=True) and (self.date or move.date) or False, + 'journal_id': self.journal_id and self.journal_id.id or move.journal_id.id, + 'invoice_payment_term_id': None, + 'debit_origin_id': move.id, + 'move_type': type, + } + if not self.copy_lines or move.move_type in [('in_refund', 'out_refund')]: + default_values['line_ids'] = [(5, 0, 0)] + return default_values + + def create_debit(self): + self.ensure_one() + new_moves = self.env['account.move'] + for move in self.move_ids.with_context(include_business_fields=True): #copy sale/purchase links + default_values = self._prepare_default_values(move) + new_move = move.copy(default=default_values) + move_msg = _( + "This debit note was created from:") + " <a href=# data-oe-model=account.move data-oe-id=%d>%s</a>" % ( + move.id, move.name) + new_move.message_post(body=move_msg) + new_moves |= new_move + + action = { + 'name': _('Debit Notes'), + 'type': 'ir.actions.act_window', + 'res_model': 'account.move', + } + if len(new_moves) == 1: + action.update({ + 'view_mode': 'form', + 'res_id': new_moves.id, + }) + else: + action.update({ + 'view_mode': 'tree,form', + 'domain': [('id', 'in', new_moves.ids)], + }) + return action diff --git a/addons/account_debit_note/wizard/account_debit_note_view.xml b/addons/account_debit_note/wizard/account_debit_note_view.xml new file mode 100644 index 00000000..be21ca6a --- /dev/null +++ b/addons/account_debit_note/wizard/account_debit_note_view.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + <data> + <record id="view_account_debit_note" model="ir.ui.view"> + <field name="name">account.debit.note.form</field> + <field name="model">account.debit.note</field> + <field name="arch" type="xml"> + <form string="Create Debit Note"> + <field name="move_type" invisible="1"/> + <field name="journal_type" invisible="1"/> + <field name="move_ids" invisible="1"/> + <group> + <group> + <field name="reason"/> + <field name="date" string="Debit Note Date"/> + <field name="copy_lines" attrs="{'invisible': [('move_type', 'in', ['in_refund', 'out_refund'])]}"/> + </group> + <group> + <field name="journal_id" domain="[('type', '=', journal_type)]"/> + </group> + </group> + <footer> + <button string='Create Debit Note' name="create_debit" type="object" class="btn-primary"/> + <button string="Cancel" class="btn-secondary" special="cancel"/> + </footer> + </form> + </field> + </record> + + <record id="action_view_account_move_debit" model="ir.actions.act_window"> + <field name="name">Create Debit Note</field> + <field name="res_model">account.debit.note</field> + <field name="view_mode">tree,form</field> + <field name="view_id" ref="view_account_debit_note"/> + <field name="target">new</field> + <field name="binding_model_id" ref="account.model_account_move" /> + <field name="binding_view_types">list</field> + </record> + </data> +</odoo> |
