Kenjx

Account Invoice Merge Wizard

Jun 10th, 2024
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1.  
  2. class InvoiceMerge(models.TransientModel):
  3.     _name = "invoice.merge"
  4.     _description = "Merge Partner Invoice"
  5.  
  6.     keep_references = fields.Boolean(
  7.         "Keep references from original invoices", default=True
  8.     )
  9.     date_invoice = fields.Date("Invoice Date")
  10.  
  11.     @api.model
  12.     def _get_not_mergeable_invoices_message(self, invoices):
  13.         """Overridable function to custom error message"""
  14.         key_fields = invoices._get_invoice_key_cols()
  15.         error_msg = {}
  16.         if len(invoices) != len(invoices._get_draft_invoices()):
  17.             error_msg["state"] = _("Merge-able State (ex : %s)") % (
  18.                 invoices and invoices[0].state or _("Draft")
  19.             )
  20.         for field in key_fields:
  21.             if len(set(invoices.mapped(field))) > 1:
  22.                 error_msg[field] = invoices._fields[field].string
  23.         return error_msg
  24.  
  25.     @api.model
  26.     def _dirty_check(self):
  27.         if self.env.context.get("active_model", "") == "account.move":
  28.             ids = self.env.context["active_ids"]
  29.             if len(ids) < 2:
  30.                 raise UserError(
  31.                     _("Please select multiple invoices to merge in the list " "view.")
  32.                 )
  33.  
  34.             invs = self.env["account.move"].browse(ids)
  35.             error_msg = self._get_not_mergeable_invoices_message(invs)
  36.             if error_msg:
  37.                 all_msg = _("All invoices must have the same: \n")
  38.                 all_msg += "\n".join([value for value in error_msg.values()])
  39.                 raise UserError(all_msg)
  40.         return {}
  41.  
  42.     @api.model
  43.     def fields_view_get(
  44.         self, view_id=None, view_type="form", toolbar=False, submenu=False
  45.     ):
  46.         """Changes the view dynamically
  47.        @param self: The object pointer.
  48.        @return: New arch of view.
  49.        """
  50.         res = super(InvoiceMerge, self).fields_view_get(
  51.             view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=False
  52.         )
  53.         self._dirty_check()
  54.         return res
  55.  
  56.     def merge_invoices(self):
  57.         """To merge similar type of account invoices.
  58.        @param self: The object pointer.
  59.        @return: account invoice action
  60.        """
  61.         ids = self.env.context.get("active_ids", [])
  62.         invoices = self.env["account.move"].browse(
  63.             self.env.context.get("active_ids", [])
  64.         )
  65.         allinvoices = invoices.do_merge(
  66.             keep_references=self.keep_references, date_invoice=self.date_invoice
  67.         )
  68.         xid = {
  69.             "out_invoice": "action_move_out_invoice_type",
  70.             "out_refund": "action_move_out_refund_type",
  71.             "in_invoice": "action_move_in_invoice_type",
  72.             "in_refund": "action_move_in_refund_type",
  73.         }[invoices[0].move_type]
  74.         action = self.env["ir.actions.act_window"]._for_xml_id("account.{}".format(xid))
  75.         action.update(
  76.             {
  77.                 "domain": [("id", "in", ids + list(allinvoices.keys()))],
  78.             }
  79.         )
  80.         return action
  81.  
Advertisement
Add Comment
Please, Sign In to add comment