refactor: override _set_opening_control_data and remove redundant cash session logic

This commit is contained in:
Suherdy Yacob 2026-05-18 10:42:34 +07:00
parent 54b94387c5
commit 191dfc8b5b

View File

@ -47,70 +47,17 @@ class PosSession(models.Model):
})
return res
def set_cashbox_pos(self, cashbox_value: int, notes: str):
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
difference = cashbox_value - (expected or 0.0)
session.write({
"state": "opened",
"opening_notes": notes,
"cash_register_balance_start": cashbox_value,
"opening_cash_expected": expected,
"opening_cash_counted": cashbox_value if session.config_id.cash_control else 0.0,
})
session._post_cash_details_message("Opening", difference, notes)
return True
def _get_opening_float_amount(self):
self.ensure_one()
if not self.config_id.cash_control:
return 0.0
opening_amount = self.opening_cash_counted
if self.currency_id.is_zero(opening_amount or 0.0):
opening_amount = self.opening_cash_expected
return opening_amount or 0.0
def _post_statement_difference(self, amount, is_opening=False):
for session in self:
session_amount = amount
if not is_opening and session.config_id.cash_control:
opening_float = session._get_opening_float_amount()
if opening_float:
if session.currency_id.is_zero(session_amount + opening_float):
continue
if session.currency_id.compare_amounts(session.cash_register_balance_end_real or 0.0, opening_float) <= 0:
session_amount += opening_float
if session.currency_id.is_zero(session_amount):
continue
elif session.currency_id.is_zero(session_amount):
continue
super(PosSession, session)._post_statement_difference(session_amount)
@api.depends("payment_method_ids", "order_ids", "cash_register_balance_start", "cash_real_transaction", "statement_line_ids")
def _compute_cash_balance(self):
for session in self:
cash_payment_method = session.payment_method_ids.filtered("is_cash_count")[:1]
if cash_payment_method:
result = self.env["pos.payment"]._read_group(
[
("session_id", "=", session.id),
("payment_method_id", "=", cash_payment_method.id),
],
aggregates=["amount:sum"],
)
total_cash_payment = result[0][0] or 0.0
if session.state == "closed":
manual_cash_total = session.cash_real_transaction or 0.0
else:
manual_cash_total = sum(session.statement_line_ids.mapped("amount"))
total_cash = manual_cash_total + total_cash_payment
session.cash_register_balance_end = session.cash_register_balance_start + total_cash
session.cash_register_difference = session.cash_register_balance_end_real - session.cash_register_balance_end
else:
session.cash_register_balance_end = 0.0
session.cash_register_difference = 0.0
return res
def get_closing_control_data(self):
data = super().get_closing_control_data()