| 1 |
from odoo.addons.point_of_sale.tests.common import TestPoSCommon |
| 2 |
from odoo.tests import tagged |
| 3 |
from ..models.pos import ORDER_FIELDS_BEFORE_17_4, ORDER_FIELDS_FROM_17_4, LINE_FIELDS |
| 4 |
from json import dumps |
| 5 |
|
| 6 |
|
| 7 |
@tagged('post_install_l10n', 'post_install', '-at_install') |
| 8 |
class TestStringToHash(TestPoSCommon): |
| 9 |
|
| 10 |
@classmethod |
| 11 |
def setUpClass(cls): |
| 12 |
super().setUpClass() |
| 13 |
|
| 14 |
cls.pricelist = cls.env['product.pricelist'].create({ |
| 15 |
'name': 'Test Pricelist', |
| 16 |
'currency_id': cls.company_data['company'].currency_id.id, |
| 17 |
}) |
| 18 |
cls.company.country_id = cls.env.company.account_fiscal_country_id.id |
| 19 |
|
| 20 |
def _compute_string_to_hash_original(self, orders): |
| 21 |
def _getattrstring(obj, field_str): |
| 22 |
field_value = obj[field_str] |
| 23 |
if obj._fields[field_str].type == 'many2one': |
| 24 |
field_value = field_value.id |
| 25 |
if obj._fields[field_str].type in ['many2many', 'one2many']: |
| 26 |
field_value = field_value.sorted().ids |
| 27 |
return str(field_value) |
| 28 |
|
| 29 |
for order in orders: |
| 30 |
values = {} |
| 31 |
if order.pos_version: |
| 32 |
order_fields = ORDER_FIELDS_FROM_17_4 |
| 33 |
else: |
| 34 |
order_fields = ORDER_FIELDS_BEFORE_17_4 |
| 35 |
for field in order_fields: |
| 36 |
values[field] = _getattrstring(order, field) |
| 37 |
|
| 38 |
for line in order.lines: |
| 39 |
for field in LINE_FIELDS: |
| 40 |
k = 'line_%d_%s' % (line.id, field) |
| 41 |
values[k] = _getattrstring(line, field) |
| 42 |
# make the json serialization canonical |
| 43 |
# (https://tools.ietf.org/html/draft-staykov-hu-json-canonical-form-00) |
| 44 |
return dumps(values, sort_keys=True, |
| 45 |
ensure_ascii=True, indent=None, |
| 46 |
separators=(',', ':')) |
| 47 |
|
| 48 |
def _create_and_pay_pos_order(self, line_data_list, payments): |
| 49 |
currency = self.company_data['company'].currency_id |
| 50 |
lines = [] |
| 51 |
total_tax = 0.0 |
| 52 |
total_amount = 0.0 |
| 53 |
|
| 54 |
for idx, line_data in enumerate(line_data_list): |
| 55 |
product = line_data.get('product', self.product_a) |
| 56 |
qty = line_data['qty'] |
| 57 |
price_unit = line_data['price_unit'] |
| 58 |
taxes = line_data.get('tax_ids', self.tax_sale_a) |
| 59 |
|
| 60 |
line_tax = sum((tax.amount / 100) * qty * price_unit for tax in taxes) |
| 61 |
line_total = qty * price_unit + line_tax |
| 62 |
|
| 63 |
total_tax += line_tax |
| 64 |
total_amount += qty * price_unit |
| 65 |
|
| 66 |
rounded_total = currency.round(line_total) |
| 67 |
|
| 68 |
lines.append((0, 0, { |
| 69 |
'name': f"OL/000{idx + 1}", |
| 70 |
'product_id': product.id, |
| 71 |
'price_unit': price_unit, |
| 72 |
'qty': qty, |
| 73 |
'tax_ids': [(6, 0, taxes.ids)], |
| 74 |
'price_subtotal': qty * price_unit, |
| 75 |
'price_subtotal_incl': rounded_total, |
| 76 |
})) |
| 77 |
|
| 78 |
order = self.env['pos.order'].create({ |
| 79 |
'company_id': self.company_data['company'].id, |
| 80 |
'partner_id': self.partner_a.id, |
| 81 |
'session_id': self.basic_config.current_session_id.id, |
| 82 |
'lines': lines, |
| 83 |
'amount_total': currency.round(total_amount + total_tax), |
| 84 |
'amount_tax': currency.round(total_tax), |
| 85 |
'amount_paid': 0, |
| 86 |
'amount_return': 0, |
| 87 |
'pricelist_id': self.pricelist.id |
| 88 |
}) |
| 89 |
|
| 90 |
for payment in payments: |
| 91 |
context_payment = { |
| 92 |
"active_ids": [order.id], |
| 93 |
"active_id": order.id |
| 94 |
} |
| 95 |
pos_make_payment = self.env['pos.make.payment'].with_context(context_payment).create({ |
| 96 |
'amount': payment['amount'], |
| 97 |
'payment_method_id': payment['payment_method'].id, |
| 98 |
}) |
| 99 |
pos_make_payment.with_context(context_payment).check() |
| 100 |
return order |
| 101 |
|
| 102 |
def test_string_to_hash(self): |
| 103 |
self.basic_config.open_ui() |
| 104 |
order = self._create_and_pay_pos_order([ |
| 105 |
{'qty': 1, 'price_unit': 10000, 'product': self.product_a, 'tax_ids': self.tax_sale_a}, |
| 106 |
{'qty': 2, 'price_unit': 5000, 'product': self.product_a, 'tax_ids': self.tax_sale_b}, |
| 107 |
{'qty': 3, 'price_unit': 2000, 'tax_ids': self.tax_sale_b | self.tax_sale_b} |
| 108 |
], [ |
| 109 |
{'amount': 10000, 'payment_method': self.bank_pm1}, |
| 110 |
{'amount': 8900, 'payment_method': self.cash_pm1}, |
| 111 |
{'amount': 11000, 'payment_method': self.pay_later_pm} |
| 112 |
]) |
| 113 |
self.basic_config.current_session_id.action_pos_session_closing_control() |
| 114 |
self.assertEqual(order.l10n_fr_string_to_hash, self._compute_string_to_hash_original(order)) |