Files
odoo_source/addons/point_of_sale/models/pos_payment.py
T
Joseph Caburnay 6420b6d778 [IMP] point_of_sale,account: revamp of accounting in pos
This commit makes some major changes in the Point of Sale module
when posting journal entries which allowed the following new features:

1. Show that the invoice generated is paid unlike before where invoice
doesn't appear to be paid (both in the pdf and in the backend).
2. User can create a 'Pay Later' payment method (created by default as
'Customer Account') to allow the customers to make credit in their
purchases.
3. An extension possibility where POS can settle customers dues.

The above is accomplished by doing the following:

1. Invoices are now reconciled to created account moves by POS using
the pos payments registered to the pos order. And this is done
right after the order is synced to the backend.
2. A new type of pos payment called 'pay_later' is introduced.
  - When an order is invoiced and is paid with 'pay_later' payment,
    the customer in the invoice will have `due` in his account which is
    immediately reflected when the order is synced.
  - When an order is not invoiced and is paid with 'pay_later' payment,
    the customer's 'due' reflects when the session is closed.
3. When the session is closed, POS payments from cash payment methods
are moved to the default account of the cash journal specified in the
payment method via creation of `account.bank.statement.line`s. (This is
already the case but it is mentioned this is for the purpose of
transparency.)
4. As for the bank pos payments, `account.payment` records are created
to move the payments to the specified `outstanding_account_id` in the
pos payment method. The `account.payment` records are written the bank
journal specified in the payment method.
5. As for the `pay_later` payments, we keep the behavior when no
journal is specified in the payment method where payments become
receivables to the customer who made the payments.

Other notable changes:
- The default POSS journal is changed to have 'general' type in order
to able to view the journal entries written in it.
- `pos.payment.method` can now be categorized as 'cash', 'bank' or 'pay_later'.
- `account_default_pos_receivable_account_id` can now be configured from
the Point of Sale general settings.
- `pos_invoice_report` is removed in favor of `account.account_invoices`
- disallow creating new payment method from `pos.config` form
- 'Journal Items' button in the `pos.session` form are now always visible.
  - This allows seeing the list of invoices (and their payment moves)
    to be accessible thru this button.

See test cases from the following spreadsheet:
https://docs.google.com/spreadsheets/d/1mt2jRSDU7OONPBFjwyTcnhRjITQI8rGMLLQA5K3fAjo/edit?usp=sharing

Part-of: odoo/odoo#74870
2021-08-27 17:43:08 +00:00

98 lines
5.1 KiB
Python

from odoo import api, fields, models, _
from odoo.tools import formatLang, float_is_zero
from odoo.exceptions import ValidationError
class PosPayment(models.Model):
""" Used to register payments made in a pos.order.
See `payment_ids` field of pos.order model.
The main characteristics of pos.payment can be read from
`payment_method_id`.
"""
_name = "pos.payment"
_description = "Point of Sale Payments"
_order = "id desc"
name = fields.Char(string='Label', readonly=True)
pos_order_id = fields.Many2one('pos.order', string='Order', required=True)
amount = fields.Monetary(string='Amount', required=True, currency_field='currency_id', readonly=True, help="Total amount of the payment.")
payment_method_id = fields.Many2one('pos.payment.method', string='Payment Method', required=True)
payment_date = fields.Datetime(string='Date', required=True, readonly=True, default=lambda self: fields.Datetime.now())
currency_id = fields.Many2one('res.currency', string='Currency', related='pos_order_id.currency_id')
currency_rate = fields.Float(string='Conversion Rate', related='pos_order_id.currency_rate', help='Conversion rate from company currency to order currency.')
partner_id = fields.Many2one('res.partner', string='Customer', related='pos_order_id.partner_id')
session_id = fields.Many2one('pos.session', string='Session', related='pos_order_id.session_id', store=True, index=True)
company_id = fields.Many2one('res.company', string='Company', related='pos_order_id.company_id', store=True)
card_type = fields.Char('Type of card used')
cardholder_name = fields.Char('Cardholder Name')
transaction_id = fields.Char('Payment Transaction ID')
payment_status = fields.Char('Payment Status')
ticket = fields.Char('Payment Receipt Info')
is_change = fields.Boolean(string='Is this payment change?', default=False)
account_move_id = fields.Many2one('account.move')
@api.model
def name_get(self):
res = []
for payment in self:
if payment.name:
res.append((payment.id, '%s %s' % (payment.name, formatLang(self.env, payment.amount, currency_obj=payment.currency_id))))
else:
res.append((payment.id, formatLang(self.env, payment.amount, currency_obj=payment.currency_id)))
return res
@api.constrains('payment_method_id')
def _check_payment_method_id(self):
for payment in self:
if payment.payment_method_id not in payment.session_id.config_id.payment_method_ids:
raise ValidationError(_('The payment method selected is not allowed in the config of the POS session.'))
def _export_for_ui(self, payment):
return {
'payment_method_id': payment.payment_method_id.id,
'amount': payment.amount,
'payment_status': payment.payment_status,
'card_type': payment.card_type,
'cardholder_name': payment.cardholder_name,
'transaction_id': payment.transaction_id,
'ticket': payment.ticket,
'is_change': payment.is_change,
}
def export_for_ui(self):
return self.mapped(self._export_for_ui) if self else []
def _create_payment_moves(self):
result = self.env['account.move']
for payment in self:
order = payment.pos_order_id
payment_method = payment.payment_method_id
if payment_method.type == 'pay_later' or float_is_zero(payment.amount, precision_rounding=order.currency_id.rounding):
continue
accounting_partner = self.env["res.partner"]._find_accounting_partner(payment.partner_id)
pos_session = order.session_id
journal = pos_session.config_id.journal_id
payment_move = self.env['account.move'].with_context(default_journal_id=journal.id).create({
'journal_id': journal.id,
'date': fields.Date.context_today(payment),
'ref': _('Invoice payment for %s (%s) using %s') % (order.name, order.account_move.name, payment_method.name),
'pos_payment_ids': payment.ids,
})
result |= payment_move
payment.write({'account_move_id': payment_move.id})
amounts = pos_session._update_amounts({'amount': 0, 'amount_converted': 0}, {'amount': payment.amount}, payment.payment_date)
credit_line_vals = pos_session._credit_amounts({
'account_id': accounting_partner.property_account_receivable_id.id,
'partner_id': accounting_partner.id,
'move_id': payment_move.id,
}, amounts['amount'], amounts['amount_converted'])
debit_line_vals = pos_session._debit_amounts({
'account_id': pos_session.company_id.account_default_pos_receivable_account_id.id,
'move_id': payment_move.id,
}, amounts['amount'], amounts['amount_converted'])
self.env['account.move.line'].with_context(check_move_validity=False).create([credit_line_vals, debit_line_vals])
payment_move._post()
return result