-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] sustainability_point_of_sale: add carbon on receipt (#216)
## Related Issues - closes #197
- Loading branch information
1 parent
192c853
commit 2d403e1
Showing
9 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Jacopo Bacci <https://github.com/jacopobacci> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
sustainability_point_of_sale/static/src/xml/order_receipt.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |