Files
odoo_source/addons/l10n_sa_pos/static/src/js/models.js
T
Rahul PrajapatiandSamuel Degueldre b119cca37b [REF] point_of_sale, *: remove class and component registries
*: l10n_co_pos, l10n_fr_pos_cert, l10n_gcc_pos, l10n_in_pos,
l10n_sa_pos, pos_adyen, pos_discount, pos_epson_printer,
pos_epson_printer_restaurant, pos_hr, pos_hr_restaurant, pos_loyalty,
pos_mercury, pos_restaurant, pos_sale, pos_sale, pos_sale, pos_sale,
pos_sale, pos_sale, pos_sale_loyalty, pos_sale_product_configurator,
pos_six, pos_six, pos_six, pos_six, pos_stripe, pos_stripe

Previously, the point of sale module used a custom inheritance system
for models and components. This system being non-standard, it makes it
hard for newcomers to understand how to use it or for people with
experience in the pos code base to understand how things are done
elsewhere. This commit removes the "registries" system from the pos
modules and adapts existing code to use standard inheritance mechanisms,
ie extending classes directly, and using patch to modify behaviour in
place.

task-3119628

closes odoo/odoo#109928

Related: odoo/enterprise#35795
Signed-off-by: Samuel Degueldre <sad@odoo.com>
Co-authored-by: Samuel Degueldre <sad@odoo.com>
2023-01-23 16:01:27 +01:00

76 lines
3.1 KiB
JavaScript

/** @odoo-module */
import { Order } from "@point_of_sale/js/models";
import { patch } from "@web/core/utils/patch";
patch(Order.prototype, "l10n_sa_pos.Order", {
export_for_printing() {
const result = this._super(...arguments);
if (this.pos.company.country.code === "SA") {
result.is_settlement = this.is_settlement();
if (!result.is_settlement) {
const codeWriter = new window.ZXing.BrowserQRCodeSvgWriter();
const qr_values = this.compute_sa_qr_code(
result.company.name,
result.company.vat,
result.date.isostring,
result.total_with_tax,
result.total_tax
);
const qr_code_svg = new XMLSerializer().serializeToString(
codeWriter.write(qr_values, 150, 150)
);
result.qr_code = "data:image/svg+xml;base64," + window.btoa(qr_code_svg);
}
}
return result;
},
/**
* If the order is empty (there are no products)
* and all "pay_later" payments are negative,
* we are settling a customer's account.
* If the module pos_settle_due is not installed,
* the function always returns false (since "pay_later" doesn't exist)
* @returns {boolean} true if the current order is a settlement, else false
*/
is_settlement() {
return (
this.is_empty() &&
!!this.paymentlines.filter(
(paymentline) =>
paymentline.payment_method.type === "pay_later" && paymentline.amount < 0
).length
);
},
compute_sa_qr_code(name, vat, date_isostring, amount_total, amount_tax) {
/* Generate the qr code for Saudi e-invoicing. Specs are available at the following link at page 23
https://zatca.gov.sa/ar/E-Invoicing/SystemsDevelopers/Documents/20210528_ZATCA_Electronic_Invoice_Security_Features_Implementation_Standards_vShared.pdf
*/
const seller_name_enc = this._compute_qr_code_field(1, name);
const company_vat_enc = this._compute_qr_code_field(2, vat);
const timestamp_enc = this._compute_qr_code_field(3, date_isostring);
const invoice_total_enc = this._compute_qr_code_field(4, amount_total.toString());
const total_vat_enc = this._compute_qr_code_field(5, amount_tax.toString());
const str_to_encode = seller_name_enc.concat(
company_vat_enc,
timestamp_enc,
invoice_total_enc,
total_vat_enc
);
let binary = "";
for (let i = 0; i < str_to_encode.length; i++) {
binary += String.fromCharCode(str_to_encode[i]);
}
return btoa(binary);
},
_compute_qr_code_field(tag, field) {
const textEncoder = new TextEncoder();
const name_byte_array = Array.from(textEncoder.encode(field));
const name_tag_encoding = [tag];
const name_length_encoding = [name_byte_array.length];
return name_tag_encoding.concat(name_length_encoding, name_byte_array);
},
});