Skip to content

Commit

Permalink
[ENH] hr_expense_petty_cash: multi-company
Browse files Browse the repository at this point in the history
  • Loading branch information
Saran440 committed Jul 23, 2024
1 parent 408d487 commit d69345a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions hr_expense_petty_cash/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"depends": ["hr_expense"],
"data": [
"security/ir.model.access.csv",
"security/petty_cash_security.xml",
"views/account_move_views.xml",
"views/hr_expense_sheet_views.xml",
"views/hr_expense_views.xml",
Expand Down
16 changes: 12 additions & 4 deletions hr_expense_petty_cash/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def _check_petty_cash_amount(self):
petty_cash_env = self.env["petty.cash"].sudo()
for rec in self:
petty_cash = petty_cash_env.search(
[("partner_id", "=", rec.partner_id.id)], limit=1
[
("partner_id", "=", rec.partner_id.id),
("company_id", "=", rec.company_id.id),
],
limit=1,
)
if petty_cash and rec.invoice_line_ids:
account = petty_cash.account_id
Expand Down Expand Up @@ -89,7 +93,7 @@ def _add_petty_cash_invoice_line(self, petty_cash):
self.ensure_one()
# Get suggested currency amount
amount = petty_cash.petty_cash_limit - petty_cash.petty_cash_balance
company_currency = self.env.user.company_id.currency_id
company_currency = self.company_id.currency_id
amount_doc_currency = company_currency._convert(
amount,
self.currency_id,
Expand All @@ -115,9 +119,13 @@ def _onchange_is_petty_cash(self):
if self.is_petty_cash:
if not self.partner_id:
raise ValidationError(_("Please select petty cash holder"))
# Selected parenter must be petty cash holder
# Selected partner must be petty cash holder
petty_cash = self.env["petty.cash"].search(
[("partner_id", "=", self.partner_id.id)], limit=1
[
("partner_id", "=", self.partner_id.id),
("company_id", "=", self.company_id.id),
],
limit=1,
)
if not petty_cash:
raise ValidationError(
Expand Down
1 change: 1 addition & 0 deletions hr_expense_petty_cash/models/hr_expense.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class HrExpense(models.Model):
string="Petty cash holder",
comodel_name="petty.cash",
ondelete="restrict",
check_company=True,
readonly=True,
states={"draft": [("readonly", False)]},
)
Expand Down
16 changes: 15 additions & 1 deletion hr_expense_petty_cash/models/petty_cash.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ class PettyCash(models.Model):
_name = "petty.cash"
_description = "Petty Cash"
_rec_name = "partner_id"
_check_company_auto = True

active = fields.Boolean(default=True)
partner_id = fields.Many2one(
comodel_name="res.partner",
string="Petty Cash Holder",
required=True,
check_company=True,
)
account_id = fields.Many2one(
comodel_name="account.account",
string="Petty Cash Account",
required=True,
check_company=True,
)
petty_cash_limit = fields.Float(
string="Max Limit",
Expand All @@ -30,9 +33,20 @@ class PettyCash(models.Model):
)
journal_id = fields.Many2one(
comodel_name="account.journal",
check_company=True,
)
company_id = fields.Many2one(
comodel_name="res.company",
required=True,
default=lambda self: self.env.company,
)

_sql_constraints = [
("partner_uniq", "unique(partner_id)", "Petty Cash Holder must be unique!"),
(
"partner_uniq",
"unique(partner_id, company_id)",
"Petty Cash Holder must be unique!",
),
]

@api.depends("partner_id", "account_id")
Expand Down
10 changes: 10 additions & 0 deletions hr_expense_petty_cash/security/petty_cash_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<odoo noupdate="1">
<record id="petty_cash_rule" model="ir.rule">
<field name="name">Petty Cash multi-company</field>
<field name="model_id" ref="model_petty_cash" />
<field name="global" eval="True" />
<field name="domain_force">
['|',('company_id','=',False),('company_id','in',company_ids)]
</field>
</record>
</odoo>
1 change: 0 additions & 1 deletion hr_expense_petty_cash/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down
11 changes: 11 additions & 0 deletions hr_expense_petty_cash/views/petty_cash_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<field name="journal_id" />
</group>
<group name="petty_cash">
<field
name="company_id"
groups="base.group_multi_company"
/>
<field name="petty_cash_limit" />
<field name="petty_cash_balance" />
</group>
Expand All @@ -41,6 +45,7 @@
<field name="journal_id" />
<field name="petty_cash_limit" sum="Limit" />
<field name="petty_cash_balance" sum="Balance" />
<field name="company_id" groups="base.group_multi_company" />
</tree>
</field>
</record>
Expand Down Expand Up @@ -87,6 +92,12 @@
domain="[('journal_id','!=',False)]"
/>
<group expand="0" string="Group By">
<filter
name="group_by_company"
string="Company"
context="{'group_by': 'company_id'}"
groups="base.group_multi_company"
/>
<filter
name="partner"
string="Petty Cash Holder"
Expand Down

0 comments on commit d69345a

Please sign in to comment.