pos_cash_opening_adjustment/models/pos_session.py

139 lines
6.0 KiB
Python
Executable File

# -*- coding: utf-8 -*-
from odoo import api, fields, models
class PosSession(models.Model):
_inherit = "pos.session"
opening_cash_expected = fields.Monetary(
string="Expected Opening Cash",
currency_field="currency_id",
readonly=True,
help="Forecasted cash amount in the drawer when the session is opened.",
)
opening_cash_counted = fields.Monetary(
string="Counted Opening Cash",
currency_field="currency_id",
readonly=True,
help="Actual cash amount counted by the cashier when the session is opened.",
)
opening_cash_difference = fields.Monetary(
string="Opening Cash Difference",
currency_field="currency_id",
compute="_compute_opening_cash_difference",
store=True,
help="Difference between the expected and the counted cash at session opening.",
)
closing_cash_expected = fields.Monetary(
string="Expected Closing Cash",
currency_field="currency_id",
readonly=True,
help="Expected cash amount in the drawer when the session is closed.",
)
closing_cash_counted = fields.Monetary(
string="Counted Closing Cash",
currency_field="currency_id",
readonly=True,
help="Actual cash amount counted by the cashier when the session is closed.",
)
closing_cash_difference = fields.Monetary(
string="Closing Cash Difference",
currency_field="currency_id",
readonly=True,
help="Difference between expected and counted cash at session closing.",
)
cash_difference = fields.Monetary(
string="Cash Different",
currency_field="currency_id",
compute="_compute_cash_difference",
help="The difference between the expected cash and the counted cash.",
)
@api.depends("opening_cash_expected", "opening_cash_counted")
def _compute_opening_cash_difference(self):
for session in self:
expected = session.opening_cash_expected or 0.0
counted = session.opening_cash_counted or 0.0
session.opening_cash_difference = counted - expected
@api.depends("state", "closing_cash_difference", "cash_register_difference")
def _compute_cash_difference(self):
for session in self:
if session.state == "closed":
session.cash_difference = session.closing_cash_difference or 0.0
else:
session.cash_difference = session.cash_register_difference or 0.0
def action_pos_session_open(self):
res = super().action_pos_session_open()
for session in self:
if session.config_id.cash_control:
session.write({
"opening_cash_expected": session.cash_register_balance_start or 0.0,
"opening_cash_counted": 0.0,
})
else:
session.write({
"opening_cash_expected": 0.0,
"opening_cash_counted": 0.0,
})
return res
def _set_opening_control_data(self, cashbox_value: int, notes: str):
res = super()._set_opening_control_data(cashbox_value, notes)
for session in self:
expected = session.opening_cash_expected if session.config_id.cash_control else 0.0
if not session.config_id.cash_control:
expected = session.cash_register_balance_start or 0.0
session.write({
"opening_cash_expected": expected,
"opening_cash_counted": cashbox_value if session.config_id.cash_control else 0.0,
})
return res
def get_closing_control_data(self):
data = super().get_closing_control_data()
default_cash_details = data.get("default_cash_details")
if default_cash_details:
manual_moves_total = sum(move.get("amount", 0.0) for move in default_cash_details.get("moves", []))
payments_amount = default_cash_details.get("payment_amount") or 0.0
opening_counted = self.opening_cash_counted or 0.0
expected_total = opening_counted + payments_amount + manual_moves_total
session_cash_amount = payments_amount + manual_moves_total
default_cash_details.update({
"opening": opening_counted,
"opening_cash_expected": self.opening_cash_expected or 0.0,
"opening_cash_counted": opening_counted,
"opening_cash_difference": self.opening_cash_difference or 0.0,
"session_cash_amount": session_cash_amount,
"expected_amount": expected_total,
"amount": expected_total,
})
return data
def _validate_session(self, balancing_account=False, amount_to_balance=0, bank_payment_method_diffs=None):
for session in self:
if session.config_id.cash_control:
expected = session.cash_register_balance_end
counted = session.cash_register_balance_end_real
difference = counted - expected
session.write({
'closing_cash_expected': expected,
'closing_cash_counted': counted,
'closing_cash_difference': difference,
})
if session.currency_id.compare_amounts(difference, 0.0) < 0:
session.message_post(body="Auto-adjustment: Cashier input %s overridden to expected %s to suppress shortage difference of %s." % (
session.currency_id.format(counted),
session.currency_id.format(expected),
session.currency_id.format(difference)
))
session.write({'cash_register_balance_end_real': expected})
for order in session.order_ids:
for payment in order.payment_ids:
if session.currency_id.is_zero(payment.amount):
payment.unlink()
return super(PosSession, self)._validate_session(balancing_account, amount_to_balance, bank_payment_method_diffs)