[NEW] Addons creation - product_rental_bookings

This commit is contained in:
Stéphan Sainléger
2022-04-19 11:54:03 +02:00
parent 73749bcfb6
commit 8e2973526a
55 changed files with 10183 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import advance_payment_invoice

View File

@@ -0,0 +1,177 @@
# -*- coding: utf-8 -*-
import time
from odoo import api, fields, models, _
from odoo.exceptions import UserError
class ProductAdvancePaymentInvoice(models.TransientModel):
_name = "product.advance.payment.invoice"
_description = "product Advance Payment Invoice"
@api.model
def _count(self):
return len(self._context.get("active_ids", []))
@api.model
def _get_advance_payment_method(self):
if self._count() == 1:
rental_obj = self.env["rental.product.order"]
order = rental_obj.browse(self._context.get("active_ids"))[0]
if order.invoice_count:
return "all"
return "delivered"
@api.model
def _default_product_id(self):
product_id = (
self.env["ir.config_parameter"]
.sudo()
.get_param("sale.default_deposit_product_id")
)
return self.env["product.product"].browse(int(product_id))
@api.model
def _default_deposit_account_id(self):
return self._default_product_id().property_account_income_id
@api.model
def _default_deposit_taxes_id(self):
return self._default_product_id().taxes_id
advance_payment_method = fields.Selection(
[
("delivered", "Invoiceable lines"),
("all", "Invoiceable lines (deduct down payments)"),
("percentage", "Down payment (percentage)"),
("fixed", "Down payment (fixed amount)"),
],
string="What do you want to invoice?",
default=_get_advance_payment_method,
required=True,
)
product_id = fields.Many2one(
"product.product",
string="Down Payment Product",
domain=[("type", "=", "service")],
default=_default_product_id,
)
count = fields.Integer(default=_count, string="# of Orders")
amount = fields.Float(
"Down Payment Amount",
help="The amount to be invoiced in advance, taxes excluded.",
)
deposit_account_id = fields.Many2one(
"account.account",
string="Income Account",
domain=[("deprecated", "=", False)],
help="Account used for deposits",
default=_default_deposit_account_id,
)
deposit_taxes_id = fields.Many2many(
"account.tax",
string="Customer Taxes",
help="Taxes used for deposits",
default=_default_deposit_taxes_id,
)
def _create_invoice(self, order, ro_line, amount):
inv_obj = self.env["account.move"]
account_id = False
if self.product_id.id:
account_id = self.product_id.property_account_income_id.id
if self.amount <= 0.00:
raise UserError(_("The value of the down payment amount must be positive."))
if self.advance_payment_method == "percentage":
amount = order.untaxed_amount * self.amount / 100
name = _("Down payment of %s%%") % (self.amount,)
else:
amount = self.amount
name = _("Down Payment")
taxes = self.product_id.taxes_id
tax_ids = taxes.ids
invoice = inv_obj.create(
{
"name": order.res_number,
"origin": order.res_number,
"partner_id": order.customer_name.id,
"type": "out_invoice",
"reference": False,
"account_id": order.customer_name.property_account_receivable_id.id,
"invoice_line_ids": [
(
0,
0,
{
"name": name,
"account_id": account_id,
"price_unit": amount,
"product_id": self.product_id.id,
"invoice_line_tax_ids": [(6, 0, tax_ids)],
},
)
],
"currency_id": order.pricelist_id.currency_id.id,
"user_id": order.user_id.id,
}
)
invoice.compute_taxes()
invoice.message_post_with_view(
"mail.message_origin_link",
values={"self": invoice, "origin": order},
subtype_id=self.env.ref("mail.mt_note").id,
)
return invoice
def create_invoices(self):
rental_orders = self.env["rental.product.order"].browse(
self._context.get("active_ids", [])
)
if self.advance_payment_method == "delivered":
rental_orders.action_invoice_create()
elif self.advance_payment_method == "all":
rental_orders.action_invoice_create(final=True)
else:
# Create deposit product if necessary
if not self.product_id:
vals = self._prepare_deposit_product()
self.product_id = self.env["product.product"].create(vals)
self.env["ir.config_parameter"].sudo().set_param(
"sale.default_deposit_product_id", self.product_id.id
)
rental_line_obj = self.env["product.order.line"]
for order in rental_orders:
if self.advance_payment_method == "percentage":
amount = order.untaxed_amount * self.amount / 100
else:
amount = self.amount
taxes = self.product_id.taxes_id
tax_ids = taxes.ids
ro_line = rental_line_obj.create(
{
"name": _("Advance: %s") % (time.strftime("%m %Y"),),
"price": amount,
"product_order_id": order.id,
"tax_id": [(6, 0, tax_ids)],
}
)
self._create_invoice(order, ro_line, amount)
if self._context.get("open_invoices", False):
return rental_orders.action_view_invoice()
return {"type": "ir.actions.act_window_close"}
def _prepare_deposit_product(self):
return {
"name": "Down payment",
"type": "service",
"invoice_policy": "order",
"property_account_income_id": self.deposit_account_id.id,
"taxes_id": [(6, 0, self.deposit_taxes_id.ids)],
}

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="view_product_advance_payment_invoice" model="ir.ui.view">
<field name="name">Invoice Rental Orders</field>
<field name="model">product.advance.payment.invoice</field>
<field name="arch" type="xml">
<form string="Invoice Rentals Order">
<p class="oe_grey">
Invoices will be created in draft so that you can review
them before validation.
</p>
<group>
<field name="count" readonly="True"/>
<field name="advance_payment_method" class="oe_inline" widget="radio"
/>
<field name="product_id"
context="{'search_default_services': 1, 'default_type': 'service', 'default_invoice_policy': 'order'}"
class="oe_inline"
invisible="1"/>
<label for="amount"
attrs="{'invisible': [('advance_payment_method', 'not in', ('fixed','percentage'))]}"/>
<div attrs="{'invisible': [('advance_payment_method', 'not in', ('fixed','percentage'))]}">
<field name="amount"
attrs="{'required': [('advance_payment_method', 'in', ('fixed','percentage'))]}"
class="oe_inline" widget="monetary"/>
</div>
<field name="deposit_account_id" class="oe_inline"
attrs="{'invisible': ['|', ('advance_payment_method', 'not in', ('fixed', 'percentage')), ('product_id', '!=', False)]}"
groups="account.group_account_manager"/>
<field name="deposit_taxes_id" class="oe_inline" widget="many2many_tags"
domain="[('type_tax_use','=','sale')]"
attrs="{'invisible': ['|', ('advance_payment_method', 'not in', ('fixed', 'percentage')), ('product_id', '!=', False)]}"/>
</group>
<footer>
<button name="create_invoices" string="Create and View Invoices" type="object"
context="{'open_invoices': True}" class="btn-primary"/>
<button name="create_invoices" string="Create Invoices" type="object"
class="btn-primary"/>
<button string="Cancel" class="btn-default" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_view_product_advance_payment_invoice" model="ir.actions.act_window">
<field name="name">Invoice Rental Orders</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">product.advance.payment.invoice</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</odoo>