|
| 1 | +# Copyright 2024 Ecosoft Co., Ltd. (http://ecosoft.co.th) |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). |
| 3 | + |
| 4 | +from odoo import _, api, fields, models |
| 5 | +from odoo.exceptions import ValidationError |
| 6 | + |
| 7 | + |
| 8 | +class SaleInvoicePlanBatch(models.Model): |
| 9 | + _name = "sale.invoice.plan.batch" |
| 10 | + _inherit = ["mail.thread", "mail.activity.mixin"] |
| 11 | + _description = "Create invoices from invoice plan in batch" |
| 12 | + _check_company_auto = True |
| 13 | + _order = "id desc" |
| 14 | + |
| 15 | + name = fields.Char( |
| 16 | + required=True, |
| 17 | + readonly=True, |
| 18 | + default=lambda self: _("New"), |
| 19 | + copy=False, |
| 20 | + ) |
| 21 | + description = fields.Char( |
| 22 | + readonly=True, |
| 23 | + states={"draft": [("readonly", False)]}, |
| 24 | + ) |
| 25 | + company_id = fields.Many2one( |
| 26 | + comodel_name="res.company", |
| 27 | + string="Company", |
| 28 | + readonly=True, |
| 29 | + default=lambda self: self.env.company, |
| 30 | + ) |
| 31 | + batch_line_ids = fields.One2many( |
| 32 | + comodel_name="sale.invoice.plan.batch.line", |
| 33 | + inverse_name="batch_id", |
| 34 | + readonly=True, |
| 35 | + states={ |
| 36 | + "draft": [("readonly", False)], |
| 37 | + "ready": [("readonly", False)], |
| 38 | + }, |
| 39 | + help="Selected invoice plan to create invoice", |
| 40 | + ) |
| 41 | + state = fields.Selection( |
| 42 | + selection=[ |
| 43 | + ("draft", "Draft"), |
| 44 | + ("ready", "Ready"), |
| 45 | + ("done", "Done"), |
| 46 | + ("done_exception", "Done with Exception"), |
| 47 | + ], |
| 48 | + default="draft", |
| 49 | + tracking=True, |
| 50 | + index=True, |
| 51 | + required=True, |
| 52 | + readonly=True, |
| 53 | + ) |
| 54 | + # Filters |
| 55 | + plan_date_from = fields.Date( |
| 56 | + string="Plan Date From", |
| 57 | + required=False, |
| 58 | + readonly=True, |
| 59 | + states={"draft": [("readonly", False)]}, |
| 60 | + help="All un-invoiced invoice plan with plan date prior to this date will be included", |
| 61 | + ) |
| 62 | + plan_date_to = fields.Date( |
| 63 | + string="Plan Date To", |
| 64 | + default=fields.Date.today, |
| 65 | + required=True, |
| 66 | + readonly=True, |
| 67 | + states={"draft": [("readonly", False)]}, |
| 68 | + help="All un-invoiced invoice plan with plan date prior to this date will be included", |
| 69 | + ) |
| 70 | + sale_ids = fields.Many2many( |
| 71 | + comodel_name="sale.order", |
| 72 | + readonly=True, |
| 73 | + states={"draft": [("readonly", False)]}, |
| 74 | + ) |
| 75 | + partner_ids = fields.Many2many( |
| 76 | + comodel_name="res.partner", |
| 77 | + readonly=True, |
| 78 | + states={"draft": [("readonly", False)]}, |
| 79 | + ) |
| 80 | + |
| 81 | + _sql_constraints = [ |
| 82 | + ("name_uniq", "UNIQUE(name)", "Batch name must be unique!"), |
| 83 | + ] |
| 84 | + |
| 85 | + @api.model_create_multi |
| 86 | + def create(self, vals_list): |
| 87 | + for vals in vals_list: |
| 88 | + if not vals.get("name") or vals["name"] == _("New"): |
| 89 | + vals["name"] = self.env["ir.sequence"].next_by_code( |
| 90 | + "sale.invoice.plan.batch" |
| 91 | + ) or _("New") |
| 92 | + return super().create(vals_list) |
| 93 | + |
| 94 | + def reset_to_draft(self): |
| 95 | + self.write({"state": "draft"}) |
| 96 | + |
| 97 | + def _get_filter(self): |
| 98 | + self.ensure_one() |
| 99 | + domain = [ |
| 100 | + ("invoice_type", "=", "installment"), |
| 101 | + ("state", "in", ("sale", "done")), |
| 102 | + ("invoiced", "=", False), |
| 103 | + ("plan_date", "<=", self.plan_date_to), |
| 104 | + ("sale_id.invoice_status", "=", "to invoice"), |
| 105 | + ] |
| 106 | + if self.plan_date_from: |
| 107 | + domain.append(("plan_date", ">=", self.plan_date_from)) |
| 108 | + if self.partner_ids: |
| 109 | + domain.append(("partner_id", "in", self.partner_ids.ids)) |
| 110 | + if self.sale_ids: |
| 111 | + domain.append(("sale_id", "in", self.sale_ids.ids)) |
| 112 | + return domain |
| 113 | + |
| 114 | + def get_planned_installments(self): |
| 115 | + for batch in self: |
| 116 | + domain = batch._get_filter() |
| 117 | + installments = self.env["sale.invoice.plan"].search( |
| 118 | + domain, order="sale_id desc, installment" |
| 119 | + ) |
| 120 | + batch.batch_line_ids.unlink() |
| 121 | + lines = [(0, 0, {"invoice_plan_id": x.id}) for x in installments] |
| 122 | + batch.write({"batch_line_ids": lines}) |
| 123 | + self.write({"state": "ready"}) |
| 124 | + |
| 125 | + def create_invoices(self): |
| 126 | + MakeInvoice = self.env["sale.advance.payment.inv"] |
| 127 | + batch_lines = self.mapped("batch_line_ids") |
| 128 | + sales = batch_lines.mapped("invoice_plan_id.sale_id") |
| 129 | + for sale in sales: # Process by sale order |
| 130 | + sale_batch_lines = batch_lines.filtered(lambda l: l.sale_id == sale) |
| 131 | + MakeInvoice = MakeInvoice.with_context(active_ids=[sale.id]) |
| 132 | + for bl in sale_batch_lines.sorted("installment"): |
| 133 | + try: |
| 134 | + makeinv_wizard = {"advance_payment_method": "delivered"} |
| 135 | + makeinvoice = MakeInvoice.create(makeinv_wizard) |
| 136 | + makeinvoice.sudo().with_context( |
| 137 | + invoice_plan_id=bl.invoice_plan_id.id |
| 138 | + ).create_invoices() |
| 139 | + self.env.cr.commit() |
| 140 | + except Exception as e: |
| 141 | + bl.error = True |
| 142 | + bl.error_message = e |
| 143 | + self.write({"state": "done"}) |
| 144 | + |
| 145 | + def open_sales(self): |
| 146 | + self.ensure_one() |
| 147 | + action = { |
| 148 | + "name": _("Sales Order"), |
| 149 | + "type": "ir.actions.act_window", |
| 150 | + "res_model": "sale.order", |
| 151 | + "view_mode": "list,form", |
| 152 | + "domain": [("id", "in", self.batch_line_ids.sale_id.ids)], |
| 153 | + "context": {"create": False}, |
| 154 | + } |
| 155 | + return action |
| 156 | + |
| 157 | + def open_invoices(self): |
| 158 | + self.ensure_one() |
| 159 | + action = { |
| 160 | + "name": _("Customer Invoices"), |
| 161 | + "type": "ir.actions.act_window", |
| 162 | + "res_model": "account.move", |
| 163 | + "view_mode": "list,form", |
| 164 | + "domain": [("id", "in", self.batch_line_ids.invoice_move_ids.ids)], |
| 165 | + "context": {"create": False}, |
| 166 | + } |
| 167 | + return action |
| 168 | + |
| 169 | + def unlink(self): |
| 170 | + recs = self.filtered(lambda l: l.state in ["done", "done_exception"]) |
| 171 | + if recs: |
| 172 | + raise ValidationError( |
| 173 | + _("The batch %s is not in draft state, so you cannot delete it.") |
| 174 | + % ", ".join(recs.mapped("name")) |
| 175 | + ) |
| 176 | + return super().unlink() |
| 177 | + |
| 178 | + |
| 179 | +class SaleInvoicePlanBatchLine(models.Model): |
| 180 | + _name = "sale.invoice.plan.batch.line" |
| 181 | + _description = "Sale Invoice Plan Batch Line" |
| 182 | + _order = "id" |
| 183 | + |
| 184 | + batch_id = fields.Many2one(comodel_name="sale.invoice.plan.batch", index=True) |
| 185 | + invoice_plan_id = fields.Many2one( |
| 186 | + comodel_name="sale.invoice.plan", |
| 187 | + readonly=True, |
| 188 | + ) |
| 189 | + sale_id = fields.Many2one( |
| 190 | + related="invoice_plan_id.sale_id", |
| 191 | + ) |
| 192 | + partner_id = fields.Many2one( |
| 193 | + related="sale_id.partner_id", |
| 194 | + ) |
| 195 | + installment = fields.Integer( |
| 196 | + related="invoice_plan_id.installment", |
| 197 | + ) |
| 198 | + plan_date = fields.Date( |
| 199 | + related="invoice_plan_id.plan_date", |
| 200 | + ) |
| 201 | + invoice_type = fields.Selection( |
| 202 | + related="invoice_plan_id.invoice_type", |
| 203 | + ) |
| 204 | + percent = fields.Float( |
| 205 | + related="invoice_plan_id.percent", |
| 206 | + ) |
| 207 | + amount = fields.Float( |
| 208 | + related="invoice_plan_id.amount", |
| 209 | + ) |
| 210 | + invoiced = fields.Boolean( |
| 211 | + related="invoice_plan_id.invoiced", |
| 212 | + ) |
| 213 | + invoice_move_ids = fields.Many2many( |
| 214 | + comodel_name="account.move", |
| 215 | + related="invoice_plan_id.invoice_move_ids", |
| 216 | + ) |
| 217 | + error = fields.Boolean() |
| 218 | + error_message = fields.Text() |
0 commit comments