Skip to content

Commit

Permalink
[ADD] sustainability_point_of_sale: add carbon on receipt (#216)
Browse files Browse the repository at this point in the history
## Related Issues

- closes #197
  • Loading branch information
jacopobacci authored Dec 11, 2024
1 parent 192c853 commit 2d403e1
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ addon | version | maintainers | summary
[sustainability_employee_commuting](sustainability_employee_commuting/) | 17.0.1.0.0 | [![bonnetadam](https://github.com/bonnetadam.png?size=30px)](https://github.com/bonnetadam) [![jacopobacci](https://github.com/jacopobacci.png?size=30px)](https://github.com/jacopobacci) | Module for employee commuting co2
[sustainability_hr_expense_report](sustainability_hr_expense_report/) | 17.0.1.0.0 | [![bonnetadam](https://github.com/bonnetadam.png?size=30px)](https://github.com/bonnetadam) | Provide CO2 accounting data for expense reports
[sustainability_mis_builder](sustainability_mis_builder/) | 17.0.1.0.0 | [![jguenat](https://github.com/jguenat.png?size=30px)](https://github.com/jguenat) | Provide CO2e accounting lines data for MIS builder reports
[sustainability_point_of_sale](sustainability_point_of_sale/) | 17.0.1.0.0 | [![jacopobacci](https://github.com/jacopobacci.png?size=30px)](https://github.com/jacopobacci) | Sustainability Point of Sale
[sustainability_purchase](sustainability_purchase/) | 17.0.1.0.0 | [![jguenat](https://github.com/jguenat.png?size=30px)](https://github.com/jguenat) [![bonnetadam](https://github.com/bonnetadam.png?size=30px)](https://github.com/bonnetadam) [![jacopobacci](https://github.com/jacopobacci.png?size=30px)](https://github.com/jacopobacci) | Glue module for sustainability & purchase modules
[sustainability_website_sale](sustainability_website_sale/) | 17.0.1.0.0 | [![jacopobacci](https://github.com/jacopobacci.png?size=30px)](https://github.com/jacopobacci) | Sustainability eCommerce

Expand Down
2 changes: 2 additions & 0 deletions sustainability/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ResConfigSettings(models.TransientModel):
module_sustainability_employee_commuting = fields.Boolean()
module_sustainability_hr_expense_report = fields.Boolean()
module_sustainability_website_sale = fields.Boolean()
module_sustainability_point_of_sale = fields.Boolean()

@api.depends("company_id")
def _compute_available_modules(self):
Expand All @@ -56,6 +57,7 @@ def _compute_available_modules(self):
"purchase",
"hr",
"website_sale",
"point_of_sale",
# Enterprise
"account_asset",
# OCA
Expand Down
7 changes: 7 additions & 0 deletions sustainability/views/res_config_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
>
<field name="module_sustainability_website_sale" />
</setting>
<setting
string="Show CO2e in Point of Sale"
invisible="(not ('point_of_sale' in available_module_names))"
help="Display CO2e values in Point of Sale receipts"
>
<field name="module_sustainability_point_of_sale" />
</setting>
</block>
<div class="col-6 p-4 center-content center co2_extra_modules" invisible="extra_module_names == False" style="margin-left: 3rem">
<h4>Use Sutainability module with more applications !</h4>
Expand Down
68 changes: 68 additions & 0 deletions sustainability_point_of_sale/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
============================
Sustainability Point of Sale
============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:eb6ae11c771dfd9d98633ef21792f52007ff53521ce72e0e540ed006c2f135cf
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-sustainability--suite%2Fsustainability--odoo-lightgray.png?logo=github
:target: https://github.com/sustainability-suite/sustainability-odoo/tree/17.0/sustainability_point_of_sale
:alt: sustainability-suite/sustainability-odoo

|badge1| |badge2| |badge3|

This module can be activated from Sustainability settings to add CO2e infos on Point of Sale receipts.

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/sustainability-suite/sustainability-odoo/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/sustainability-suite/sustainability-odoo/issues/new?body=module:%20sustainability_point_of_sale%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Open Net Sàrl

Contributors
~~~~~~~~~~~~

* Jacopo Bacci <https://github.com/jacopobacci>

Maintainers
~~~~~~~~~~~

.. |maintainer-jacopobacci| image:: https://github.com/jacopobacci.png?size=40px
:target: https://github.com/jacopobacci
:alt: jacopobacci

Current maintainer:

|maintainer-jacopobacci|

This module is part of the `sustainability-suite/sustainability-odoo <https://github.com/sustainability-suite/sustainability-odoo/tree/17.0/sustainability_point_of_sale>`_ project on GitHub.

You are welcome to contribute.
20 changes: 20 additions & 0 deletions sustainability_point_of_sale/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Sustainability Point of Sale",
"category": "Sales/Point of Sale",
"version": "17.0.1.0.0",
"installable": True,
"application": False,
"auto_install": False,
"license": "LGPL-3",
"author": "Open Net Sàrl",
"maintainers": ["jacopobacci"],
"development_status": "Production/Stable",
"website": "https://www.open-net.ch",
"depends": ["sustainability", "point_of_sale"],
"assets": {
"point_of_sale._assets_pos": [
"sustainability_point_of_sale/static/src/**/*",
],
},
"sequence": 1,
}
1 change: 1 addition & 0 deletions sustainability_point_of_sale/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Jacopo Bacci <https://github.com/jacopobacci>
1 change: 1 addition & 0 deletions sustainability_point_of_sale/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module can be activated from Sustainability settings to add CO2e infos on Point of Sale receipts.
75 changes: 75 additions & 0 deletions sustainability_point_of_sale/static/src/js/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/** @odoo-module */

import {patch} from "@web/core/utils/patch";
import {Order, Orderline} from "@point_of_sale/app/store/models";

let productQuantities = {};

patch(Order.prototype, {
export_for_printing() {
const result = super.export_for_printing(...arguments);
result.total_carbon_value = this.get_total_carbon_value();

productQuantities = {};

return result;
},
get_total_carbon_value() {
const total = this.orderlines.reduce((sum, line) => sum + line.carbon_value * line.quantity, 0);
if (total) return total.toFixed(2);
return 0;
},
});

patch(Orderline.prototype, {
async setup() {
await super.setup(...arguments);
await this._setCarbonValue();
},
/**
* Retrieves the product's carbon output factor from the product or its template and sets the `carbon_value`.
* The standard retrieval hierarchy (product -> template -> category -> company) is bypassed to ensure greater precision for the POS.
* If the carbon factor is not found in the product or template, the value (0) won't be shown.
*/
async _setCarbonValue() {
this.carbon_value = 0;

const productId = this.product.id;
productQuantities[productId] = (productQuantities[productId] || 0) + 1;

const productRes = await this.env.services.orm.searchRead("product.product", [["id", "=", productId]], ["carbon_out_factor_id", "product_tmpl_id"]);
if (productRes.length === 0) return;

let carbonOutFactor = productRes[0].carbon_out_factor_id;

if (!carbonOutFactor) {
const productTemplate = productRes[0].product_tmpl_id;
const productTemplateRes = await this.env.services.orm.searchRead("product.template", [["id", "=", productTemplate[0]]], ["carbon_out_factor_id"]);

if (productTemplateRes.length === 0) return;

carbonOutFactor = productTemplateRes[0].carbon_out_factor_id;
}

if (!carbonOutFactor) return;

const carbonFactor = await this.env.services.orm.searchRead("carbon.factor", [["id", "=", carbonOutFactor[0]]], ["carbon_value"]);
if (carbonFactor.length === 0) return;

this.carbon_value = parseFloat(carbonFactor[0].carbon_value || 0).toFixed(2);
},
getDisplayData() {
const qty = productQuantities[this.product.id];
const data = super.getDisplayData();

if (this.carbon_value) data.carbon_value = (this.carbon_value * qty).toFixed(2);

return data;
},
export_as_JSON() {
const json = super.export_as_JSON(...arguments);
const productId = json.product_id;
productQuantities[productId] = json.qty;
return json;
},
});
32 changes: 32 additions & 0 deletions sustainability_point_of_sale/static/src/xml/order_receipt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates id="template" xml:space="preserve">
<t t-name="sustainability_point_of_sale.OrderReceipt" t-inherit="point_of_sale.OrderReceipt" t-inherit-mode="extension">
<xpath expr="//Orderline" position="inside">
<div t-if="line.carbon_value" class="text-end" style="font-size: 90%;">
Ͼ
<span>
<t t-out="line.carbon_value" class="ms-1" />
</span>
</div>
</xpath>
</t>
<t t-name="sustainability_point_of_sale.OrderReceipt" t-inherit="point_of_sale.OrderReceipt" t-inherit-mode="extension">
<xpath expr="//div[hasclass('pos-receipt-amount')]" position="inside">
<t t-if="props.data.total_carbon_value">
<div class="text-end" style="font-size: 90%;">
Ͼ
<span t-out="props.data.total_carbon_value" class="ms-1" />
</div>
</t>
</xpath>
</t>
<t t-name="sustainability_point_of_sale.OrderReceipt" t-inherit="point_of_sale.OrderReceipt" t-inherit-mode="extension">
<xpath expr="//div[hasclass('after-footer')]" position="after">
<t t-if="props.data.total_carbon_value">
<br />
<br />
<div class="pos-receipt-order-data mt-0">1 Ͼ = 1 kgCO2e</div>
</t>
</xpath>
</t>
</templates>

0 comments on commit 2d403e1

Please sign in to comment.