diff --git a/addons/l10n_pe_website_sale/README.rst b/addons/l10n_pe_website_sale/README.rst new file mode 100644 index 0000000000000..a3af9b1d7f793 --- /dev/null +++ b/addons/l10n_pe_website_sale/README.rst @@ -0,0 +1,2 @@ +Be able to see Identification Type in ecommerce checkout form. +============================================================== diff --git a/addons/l10n_pe_website_sale/__init__.py b/addons/l10n_pe_website_sale/__init__.py new file mode 100644 index 0000000000000..4dcc9d3a4b7ae --- /dev/null +++ b/addons/l10n_pe_website_sale/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import controllers +from . import models diff --git a/addons/l10n_pe_website_sale/__manifest__.py b/addons/l10n_pe_website_sale/__manifest__.py new file mode 100644 index 0000000000000..2ee49824dad79 --- /dev/null +++ b/addons/l10n_pe_website_sale/__manifest__.py @@ -0,0 +1,27 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +{ + "name": "Peruvian eCommerce", + "version": "0.1", + "summary": "Be able to see Identification Type in ecommerce checkout form.", + "category": "Accounting/Localizations/Website", + "author": "Vauxoo, Odoo", + "license": "LGPL-3", + "depends": [ + "website_sale", + "l10n_pe", + ], + "data": [ + "data/ir_model_fields.xml", + "views/templates.xml", + ], + "demo": [ + "demo/website_demo.xml", + ], + "assets": { + "web.assets_frontend": [ + "l10n_pe_website_sale/static/src/js/website_sale.js", + ], + }, + "installable": True, + "auto_install": True, +} diff --git a/addons/l10n_pe_website_sale/controllers/__init__.py b/addons/l10n_pe_website_sale/controllers/__init__.py new file mode 100644 index 0000000000000..9a57ed53e4786 --- /dev/null +++ b/addons/l10n_pe_website_sale/controllers/__init__.py @@ -0,0 +1,2 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import main diff --git a/addons/l10n_pe_website_sale/controllers/main.py b/addons/l10n_pe_website_sale/controllers/main.py new file mode 100644 index 0000000000000..cbf99215aa3e0 --- /dev/null +++ b/addons/l10n_pe_website_sale/controllers/main.py @@ -0,0 +1,93 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import http +from odoo.http import request + +from odoo.addons.website_sale.controllers.main import WebsiteSale + + +class L10nPEWebsiteSale(WebsiteSale): + def _get_mandatory_fields_billing(self, country_id=False): + """Extend mandatory fields to add new identification, responsibility + city_id and distrcit fields when company is Peru""" + res = super()._get_mandatory_fields_billing(country_id) + if country_id: + country = request.env['res.country'].browse(country_id) + if country and country == request.env.ref("base.pe"): + res += ["city_id", "l10n_pe_district"] + res.remove("city") + if request.website.sudo().company_id.country_id.code == "PE": + res += ["l10n_latam_identification_type_id", "vat"] + return res + + def _get_mandatory_fields_shipping(self, country_id=False): + """Extend mandatory fields to add city_id and district fields when the selected country is Peru""" + res = super()._get_mandatory_fields_shipping(country_id) + if country_id: + country = request.env['res.country'].browse(country_id) + if country and country == request.env.ref("base.pe"): + res += ["city_id", "l10n_pe_district"] + res.remove("city") + return res + + def _get_country_related_render_values(self, kw, render_values): + res = super()._get_country_related_render_values(kw, render_values) + + if request.website.sudo().company_id.country_id.code == "PE": + values = render_values["checkout"] + state = ( + "state_id" in values + and values["state_id"] != "" + and request.env["res.country.state"].browse(int(values["state_id"])) + ) + city = ( + "city_id" in values + and values["city_id"] != "" + and request.env["res.city"].browse(int(values["city_id"])) + ) + to_include = { + "identification": kw.get("l10n_latam_identification_type_id"), + "identification_types": request.env["l10n_latam.identification.type"].sudo().search( + ["|", ("country_id", "=", False), ("country_id.code", "=", "PE")] + ), + } + if state: + to_include["state"] = state + to_include["state_cities"] = state.get_website_sale_cities() + if city: + to_include["city"] = city + to_include["city_districts"] = city.get_website_sale_districts() + res.update(to_include) + return res + + def _get_vat_validation_fields(self, data): + res = super()._get_vat_validation_fields(data) + if request.website.sudo().company_id.country_id.code == "PE": + res.update( + { + "l10n_latam_identification_type_id": int(data["l10n_latam_identification_type_id"]) + if data.get("l10n_latam_identification_type_id") + else False + } + ) + res.update({"name": data["name"] if data.get("name") else False}) + return res + + @http.route( + ['/shop/state_infos/'], + type="json", + auth="public", + methods=["POST"], + website=True, + ) + def state_infos(self, state, **kw): + return dict( + cities=[(c.id, c.name, c.l10n_pe_code) for c in state.get_website_sale_cities()], + ) + + @http.route( + ['/shop/city_infos/'], type="json", auth="public", methods=["POST"], website=True + ) + def city_infos(self, city, **kw): + return dict( + districts=[(d.id, d.name, d.code) for d in city.get_website_sale_districts()], + ) diff --git a/addons/l10n_pe_website_sale/data/ir_model_fields.xml b/addons/l10n_pe_website_sale/data/ir_model_fields.xml new file mode 100644 index 0000000000000..88d25759c6df0 --- /dev/null +++ b/addons/l10n_pe_website_sale/data/ir_model_fields.xml @@ -0,0 +1,13 @@ + + + + + res.partner + + + + diff --git a/addons/l10n_pe_website_sale/demo/website_demo.xml b/addons/l10n_pe_website_sale/demo/website_demo.xml new file mode 100644 index 0000000000000..959181ca8e8d4 --- /dev/null +++ b/addons/l10n_pe_website_sale/demo/website_demo.xml @@ -0,0 +1,33 @@ + + + + + (PE) Peruvian Website + + + + + + + + + + + + + + + + + + diff --git a/addons/l10n_pe_website_sale/i18n/es.po b/addons/l10n_pe_website_sale/i18n/es.po new file mode 100644 index 0000000000000..e4ab639ab5066 --- /dev/null +++ b/addons/l10n_pe_website_sale/i18n/es.po @@ -0,0 +1,76 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * l10n_pe_website_sale +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2023-04-04 22:20+0000\n" +"PO-Revision-Date: 2023-04-04 22:20+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_info +msgid "" +"Changing Identification type is not allowed once document(s) have been " +"issued for your account. Please contact us directly for this operation." +msgstr "" +"No se permite cambiar el tipo de identificación una vez que se hayan emitido los documentos " +"para su cuenta. Póngase en contacto con nosotros directamente para esta operación." + +#. module: l10n_pe_website_sale +#: model:ir.model,name:l10n_pe_website_sale.model_res_city +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_address_info +msgid "City" +msgstr "Ciudad" + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_address_info +msgid "City..." +msgstr "Ciudad..." + +#. module: l10n_pe_website_sale +#: model:ir.model,name:l10n_pe_website_sale.model_res_country_state +msgid "Country state" +msgstr "Estado" + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_address_info +msgid "District" +msgstr "Distrito" + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_address_info +msgid "District..." +msgstr "Distrito..." + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_info +msgid "Identification Type" +msgstr "Tipo de Identificación" + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.partner_info +msgid "Identification Type..." +msgstr "Tipo de Identificación..." + +#. module: l10n_pe_website_sale +#: model:website,prevent_zero_price_sale_text:l10n_pe_website_sale.default_website_pe +msgid "Not Available For Sale" +msgstr "No disponible para la venta" + +#. module: l10n_pe_website_sale +#: model_terms:ir.ui.view,arch_db:l10n_pe_website_sale.address +msgid "Number" +msgstr "Número" + +#. module: l10n_pe_website_sale +#: model:ir.model,name:l10n_pe_website_sale.model_website +msgid "Website" +msgstr "Sitio web" diff --git a/addons/l10n_pe_website_sale/models/__init__.py b/addons/l10n_pe_website_sale/models/__init__.py new file mode 100644 index 0000000000000..632d7f9e11361 --- /dev/null +++ b/addons/l10n_pe_website_sale/models/__init__.py @@ -0,0 +1,5 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import res_city +from . import res_country_state +from . import website diff --git a/addons/l10n_pe_website_sale/models/res_city.py b/addons/l10n_pe_website_sale/models/res_city.py new file mode 100644 index 0000000000000..a8276eb07d385 --- /dev/null +++ b/addons/l10n_pe_website_sale/models/res_city.py @@ -0,0 +1,10 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + + +class City(models.Model): + _inherit = "res.city" + + def get_website_sale_districts(self): + return self.env["l10n_pe.res.city.district"].sudo().search([("city_id", "=", self.id)]) diff --git a/addons/l10n_pe_website_sale/models/res_country_state.py b/addons/l10n_pe_website_sale/models/res_country_state.py new file mode 100644 index 0000000000000..6958b944480bb --- /dev/null +++ b/addons/l10n_pe_website_sale/models/res_country_state.py @@ -0,0 +1,10 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + + +class CountryState(models.Model): + _inherit = "res.country.state" + + def get_website_sale_cities(self): + return self.env["res.city"].sudo().search([("state_id", "=", self.id)]) diff --git a/addons/l10n_pe_website_sale/models/website.py b/addons/l10n_pe_website_sale/models/website.py new file mode 100644 index 0000000000000..1a28e3c8794d6 --- /dev/null +++ b/addons/l10n_pe_website_sale/models/website.py @@ -0,0 +1,12 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import models + + +class Website(models.Model): + _inherit = "website" + + def _display_partner_b2b_fields(self): + """Peruvian localization must always display b2b fields""" + self.ensure_one() + return self.company_id.country_id.code == "PE" or super()._display_partner_b2b_fields() diff --git a/addons/l10n_pe_website_sale/static/description/icon.png b/addons/l10n_pe_website_sale/static/description/icon.png new file mode 100644 index 0000000000000..e6080d3f776f5 Binary files /dev/null and b/addons/l10n_pe_website_sale/static/description/icon.png differ diff --git a/addons/l10n_pe_website_sale/static/src/js/website_sale.js b/addons/l10n_pe_website_sale/static/src/js/website_sale.js new file mode 100644 index 0000000000000..ff66eee3af0c7 --- /dev/null +++ b/addons/l10n_pe_website_sale/static/src/js/website_sale.js @@ -0,0 +1,109 @@ +/** @odoo-module **/ +import { WebsiteSale } from "@website_sale/js/website_sale"; + +WebsiteSale.include({ + events: Object.assign({}, WebsiteSale.prototype.events, { + "change select[name='state_id']": "_onChangeState", + "change select[name='city_id']": "_onChangeCity", + }), + start: function () { + this.selectCities = document.querySelector("select[name='city_id']"); + this.selectDistricts = document.querySelector("select[name='l10n_pe_district']"); + this.cityBlock = document.querySelector(".div_city"); + this.autoFormat = document.querySelector(".checkout_autoformat"); + this.selectedState = document.querySelector("select[name='state_id']"); + return this._super.apply(this, arguments); + }, + _changeState: function () { + if (!document.getElementById("state_id").value) { + return; + } + this._rpc({ + route: "/shop/state_infos/" + document.getElementById("state_id").value, + }).then(data => { + // populate cities and display + if (data.cities.length) { + this.selectCities.innerHTML = ""; + let option = document.createElement("option"); + option.textContent = "City..."; + this.selectCities.appendChild(option); + data.cities.forEach(city => { + let opt = document.createElement("option"); + opt.textContent = city[1]; + opt.value = city[0]; + opt.setAttribute("data-code", city[2]); + this.selectCities.appendChild(opt); + }); + this.selectCities.parentElement.style.display = "block"; + } else { + this.selectCities.value = ""; + this.selectCities.parentElement.style.display = "none"; + } + this.selectDistricts.value = ""; + this.selectDistricts.parentElement.style.display = "none"; + }); + }, + _changeCity: function () { + if (!document.getElementById("city_id").value) { + return; + } + this._rpc({ + route: "/shop/city_infos/" + document.getElementById("city_id").value, + }).then(data => { + // populate districts and display + if (data.districts.length) { + this.selectDistricts.innerHTML = ""; + data.districts.forEach(district => { + let opt = document.createElement("option"); + opt.textContent = district[1]; + opt.value = district[0]; + opt.setAttribute("data-code", district[2]); + this.selectDistricts.appendChild(opt); + }); + this.selectDistricts.parentElement.style.display = "block"; + } else { + this.selectDistricts.value = ""; + this.selectDistricts.parentElement.style.display = "none"; + } + }); + }, + _onChangeState: function (ev) { + if (!this.autoFormat.length) { + return; + } + this._changeState(); + }, + _onChangeCity: function (ev) { + if (!this.autoFormat.length) { + return; + } + this._changeCity(); + }, + _onChangeCountry: function (ev) { + this._super(...arguments); + + // si el div de state se muestra hacer trigger del change state + + // Create a new 'change' event en JS + // var event = new Event('change'); + // this.selectedState.dispatchEvent(event); + + // jquery + $(this.selectedState).change(); + + let selectedCountry = ev.currentTarget.options[ev.currentTarget.selectedIndex].getAttribute("code"); + + if (selectedCountry == "PE") { + this.cityBlock.classList.add("d-none"); + } else if (selectedCountry != "PE") { + this.cityBlock.querySelectorAll("input").forEach(input => { + input.value = ""; + }); + this.cityBlock.classList.remove("d-none"); + this.selectCities.value = ""; + this.selectCities.parentElement.style.display = "none"; + this.selectDistricts.value = ""; + this.selectDistricts.parentElement.style.display = "none"; + } + }, +}); diff --git a/addons/l10n_pe_website_sale/views/templates.xml b/addons/l10n_pe_website_sale/views/templates.xml new file mode 100644 index 0000000000000..a5d35f23c260f --- /dev/null +++ b/addons/l10n_pe_website_sale/views/templates.xml @@ -0,0 +1,127 @@ + + + + + + + + + +