summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMqdd <ahmadmiqdad27@gmail.com>2026-03-01 22:59:52 +0700
committerMqdd <ahmadmiqdad27@gmail.com>2026-03-01 22:59:52 +0700
commitd18a5e4e79bad1193198160abeff73302767f509 (patch)
treeae5ad4ea5d9d58ef19dcf6fbe9c7681467f4fe32
parentf62b42549df53a352b1da2df584e9a98b5d16975 (diff)
<MIqdad> fix reverse invoice (kelar)
-rwxr-xr-xfixco_custom/__manifest__.py1
-rw-r--r--fixco_custom/models/account_move_reversal.py2
-rwxr-xr-xfixco_custom/models/stock_picking.py96
-rw-r--r--fixco_custom/views/account_move_reversal_wiz.xml15
4 files changed, 83 insertions, 31 deletions
diff --git a/fixco_custom/__manifest__.py b/fixco_custom/__manifest__.py
index b72622a..5d9a65a 100755
--- a/fixco_custom/__manifest__.py
+++ b/fixco_custom/__manifest__.py
@@ -58,6 +58,7 @@
'views/stock_inventory.xml',
'views/kartu_stock.xml',
'views/account_lock_date.xml',
+ 'views/account_move_reversal_wiz.xml',
],
'demo': [],
'css': [],
diff --git a/fixco_custom/models/account_move_reversal.py b/fixco_custom/models/account_move_reversal.py
index c3cc5cf..128809f 100644
--- a/fixco_custom/models/account_move_reversal.py
+++ b/fixco_custom/models/account_move_reversal.py
@@ -6,6 +6,8 @@ from odoo.exceptions import UserError
class AccountMoveReversal(models.TransientModel):
_inherit = 'account.move.reversal'
+
+ picking_id = fields.Many2one('stock.picking', string='Picking')
def reverse_moves(self):
self.ensure_one()
diff --git a/fixco_custom/models/stock_picking.py b/fixco_custom/models/stock_picking.py
index 1c90d51..449959b 100755
--- a/fixco_custom/models/stock_picking.py
+++ b/fixco_custom/models/stock_picking.py
@@ -247,43 +247,77 @@ class StockPicking(models.Model):
return res
def automatic_reversed_invoice(self):
- origin = self.origin or ''
- clean_origin = origin.replace('Return of ', '')
+ origin = self.origin or ''
+ clean_origin = origin.replace('Return of ', '')
- return_picking = self.env['stock.picking'].search([
- ('name', '=', clean_origin),
- ('state', '=', 'done')
- ], limit=1)
+ return_picking = self.env['stock.picking'].search([
+ ('name', '=', clean_origin),
+ ('state', '=', 'done')
+ ], limit=1)
- if not return_picking:
- return False
+ if not return_picking:
+ return False
- account_move = self.env['account.move'].search([
- ('picking_id', '=', return_picking.id),
- ('state', '=', 'posted')
- ], limit=1)
+ account_move = self.env['account.move'].search([
+ ('picking_id', '=', return_picking.id),
+ ('state', '=', 'posted')
+ ], limit=1)
- if not account_move:
- return False
-
- reversal = self.env['account.move.reversal'].create({
- 'move_ids': [(6, 0, account_move.ids)],
- 'date_mode': 'custom',
- 'date': fields.Date.context_today(self),
- 'refund_method': 'refund',
- 'reason': _('Auto reverse from return picking %s') % self.name,
- 'company_id': account_move.company_id.id,
- })
-
- action = reversal.reverse_moves()
- # Force write invoice date using date done SP
- new_move = self.env['account.move'].browse(action.get('res_id'))
- if new_move:
- new_move.write({
- 'invoice_date': self.date_done,
+ if not account_move:
+ return False
+
+ reversal = self.env['account.move.reversal'].create({
+ 'move_ids': [(6, 0, account_move.ids)],
+ 'picking_id': self.id,
+ 'date_mode': 'custom',
+ 'date': fields.Date.context_today(self),
+ 'refund_method': 'refund',
+ 'reason': _('Auto reverse from return picking %s') % self.name,
+ 'company_id': account_move.company_id.id,
})
- return action
+ action = reversal.reverse_moves()
+ new_move = self.env['account.move'].browse(action.get('res_id'))
+
+ if new_move:
+ if new_move.state == 'posted':
+ new_move.button_draft()
+
+ # skip move validity
+ new_move = new_move.with_context(check_move_validity=False)
+
+ #Ambil data retur
+ retur_data = {}
+ for line in self.move_ids_without_package:
+ retur_data[line.product_id.id] = retur_data.get(line.product_id.id, 0) + line.quantity_done
+
+ #Update Qty atau Unlink
+ for inv_line in new_move.invoice_line_ids:
+ p_id = inv_line.product_id.id
+ if p_id in retur_data and retur_data[p_id] > 0:
+ inv_line.write({'quantity': retur_data[p_id]})
+ retur_data[p_id] = 0
+ else:
+ inv_line.unlink()
+
+ new_move._recompute_tax_lines()
+
+ # Recompute baris piutang/hutang
+ # bakal nyesuain angka debit/kredit biar balance sama total invoice
+ new_move._recompute_payment_terms_lines()
+
+ new_move._recompute_dynamic_lines(recompute_all_taxes=True)
+
+ # invoice date ikutin date done stock picking
+ new_move.write({
+ 'invoice_date': self.date_done.date(),
+ 'date': self.date_done.date()
+ })
+
+ # check validity diakhir
+ new_move.with_context(check_move_validity=True).action_post()
+
+ return action
diff --git a/fixco_custom/views/account_move_reversal_wiz.xml b/fixco_custom/views/account_move_reversal_wiz.xml
new file mode 100644
index 0000000..c9aaae9
--- /dev/null
+++ b/fixco_custom/views/account_move_reversal_wiz.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<odoo>
+ <data>
+ <record id="view_account_move_reversal" model="ir.ui.view">
+ <field name="name">account.move.reversal.form</field>
+ <field name="model">account.move.reversal</field>
+ <field name="inherit_id" ref="account.view_account_move_reversal"/>
+ <field name="arch" type="xml">
+ <field name="journal_id" position="after">
+ <field name="picking_id"/>
+ </field>
+ </field>
+ </record>
+ </data>
+</odoo>