Skip to content

Commit

Permalink
[MIG] pos_tare: Migration to 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
baimont committed May 4, 2023
1 parent 096f471 commit 325ae22
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 375 deletions.
7 changes: 5 additions & 2 deletions pos_tare/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Point Of Sale - Tare",
"summary": "Manage Tare in Point Of Sale module",
"version": "12.0.1.0.3",
"version": "14.0.1.0.0",
"category": "Point of Sale",
"author": "GRAP, Le Nid, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/pos",
Expand All @@ -20,7 +20,10 @@
"data/barcode_rule.xml",
],
"qweb": [
"static/src/xml/pos_tare.xml",
"static/src/xml/Screens/ProductScreen/Orderline.xml",
"static/src/xml/Screens/ProductScreen/NumpadWidget.xml",
"static/src/xml/Screens/ReceiptScreen/OrderReceipt.xml",
"static/src/xml/Screens/ScaleScreen/ScaleScreen.xml",
],
"demo": [
"demo/product_product.xml",
Expand Down
4 changes: 3 additions & 1 deletion pos_tare/models/barcode_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
class BarcodeRule(models.Model):
_inherit = "barcode.rule"

type = fields.Selection(selection_add=[("tare", "Tare")])
type = fields.Selection(
selection_add=[("tare", "Tare")], ondelete={"tare": "set default"}
)
5 changes: 2 additions & 3 deletions pos_tare/models/pos_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

from odoo import fields, models

from odoo.addons import decimal_precision as dp


class PosOrderLine(models.Model):
_inherit = "pos.order.line"

tare = fields.Float(
string="Tare", digits=dp.get_precision("Product Unit of Measure")
string="Tare",
digits="Product Unit of Measure",
)
4 changes: 1 addition & 3 deletions pos_tare/models/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

from odoo import fields, models

import odoo.addons.decimal_precision as dp


class ProductTemplate(models.Model):
_inherit = "product.template"

tare_weight = fields.Float(
digits=dp.get_precision("Product Unit of Measure"),
digits="Product Unit of Measure",
string="Tare Weight",
help="Set here Constant tare weight"
" for the given product. This tare will be substracted when"
Expand Down
167 changes: 167 additions & 0 deletions pos_tare/static/src/js/Screens/ProductScreen/ProductScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
odoo.define("pos_tare.screens", function (require) {
"use strict";
const ProductScreen = require("point_of_sale.ProductScreen");
const Registries = require("point_of_sale.Registries");
const {useBarcodeReader} = require("point_of_sale.custom_hooks");

const TareProductScreen = (ProductScreen) =>
class extends ProductScreen {
constructor() {
super(...arguments);
useBarcodeReader({
// We add the tare action
tare: this._barcodeTareAction,
});
}

async _barcodeTareAction(code) {
var last_orderline = this.currentOrder.get_last_orderline();
if (last_orderline) {
last_orderline.set_tare(code.value, true);
}
}

async _getAddProductOptions(product) {
let price_extra = 0.0;
let draftPackLotLines, weight, description, packLotLinesToEdit, tare; // eslint-disable-line

if (
this.env.pos.config.product_configurator &&
_.some(
product.attribute_line_ids,
(id) => id in this.env.pos.attributes_by_ptal_id
)
) {
const attributes = _.map(
product.attribute_line_ids,
(id) => this.env.pos.attributes_by_ptal_id[id]
).filter((attr) => attr !== undefined);
const {confirmed, payload} = await this.showPopup(
"ProductConfiguratorPopup",
{
product: product,
attributes: attributes,
}
);

if (confirmed) {
description = payload.selected_attributes.join(", ");
price_extra += payload.price_extra;
} else {
return;
}
}

// Gather lot information if required.
if (
["serial", "lot"].includes(product.tracking) &&
(this.env.pos.picking_type.use_create_lots ||
this.env.pos.picking_type.use_existing_lots)
) {
const isAllowOnlyOneLot = product.isAllowOnlyOneLot();
if (isAllowOnlyOneLot) {
packLotLinesToEdit = [];
} else {
const orderline = this.currentOrder
.get_orderlines()
.filter((line) => !line.get_discount())
.find((line) => line.product.id === product.id);
if (orderline) {
packLotLinesToEdit = orderline.getPackLotLinesToEdit();
} else {
packLotLinesToEdit = [];
}
}
const {confirmed, payload} = await this.showPopup("EditListPopup", {
title: this.env._t("Lot/Serial Number(s) Required"),
isSingleItem: isAllowOnlyOneLot,
array: packLotLinesToEdit,
});
if (confirmed) {
// Segregate the old and new packlot lines
const modifiedPackLotLines = Object.fromEntries(
payload.newArray
.filter((item) => item.id)
.map((item) => [item.id, item.text])
);
const newPackLotLines = payload.newArray
.filter((item) => !item.id)
.map((item) => ({lot_name: item.text}));

draftPackLotLines = {modifiedPackLotLines, newPackLotLines};
} else {
// We don't proceed on adding product.
return;
}
}

// Take the weight if necessary.
if (product.to_weight && this.env.pos.config.iface_electronic_scale) {
// Show the ScaleScreen to weigh the product.
if (this.isScaleAvailable) {
const {confirmed, payload} = await this.showTempScreen(
"ScaleScreen",
{
product,
}
);
if (confirmed) {
// /////////////////////////////
// Overload Section
// We add the tare to the payload
// /////////////////////////////
weight = payload.weight;
tare = payload.tare;
} else {
// Do not add the product;
return;
}
} else {
await this._onScaleNotAvailable();
}
}

return {
draftPackLotLines,
quantity: weight,
description,
price_extra,
tare: tare,
};
}

_setValue(val) {
super._setValue(val);
if (this.currentOrder.get_selected_orderline()) {
if (this.state.numpadMode === "tare") {
if (this.env.pos.config.iface_tare_method === "barcode") {
this.showPopup("ErrorPopup", {
title: this.env._t("Feature Disabled"),
body: this.env._t(
"You can not set the tare." +
" To be able to set the tare manually" +
" you have to change the tare input method" +
" in the POS configuration"
),
});
} else {
try {
this.currentOrder
.get_selected_orderline()
.set_tare(val, true);
} catch (error) {
this.showPopup("ErrorPopup", {
title: this.env._t("We can not apply this tare"),
body: error.message,
});
}
}
}
}
}
};

Registries.Component.extend(ProductScreen, TareProductScreen);

return ProductScreen;
});
52 changes: 52 additions & 0 deletions pos_tare/static/src/js/Screens/ScaleScreen/ScaleScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
odoo.define("pos_tare.ScaleScreen", function (require) {
"use strict";

const Registries = require("point_of_sale.Registries");
const ScaleScreen = require("point_of_sale.ScaleScreen");
const {useState} = owl.hooks;
const {useAutofocus} = require("web.custom_hooks");

const ColaborScaleScreen = (ScaleScreen) =>
class extends ScaleScreen {
constructor() {
super(...arguments);
this.state = useState({
tare: this.props.product.tare_weight,
weight: 0,
gross_weight: 0,
});
useAutofocus({selector: "#input_weight_tare"});
}

_readScale() {
if (this.env.pos.config.iface_gross_weight_method === "scale") {
super._readScale();
}
}

async _setWeight() {
await super._setWeight();
this.state.gross_weight = this.state.weight;
this.state.weight -= this.state.tare;
}

updateWeight() {
this.state.weight = this.state.gross_weight - this.state.tare;
}

confirm() {
this.props.resolve({
confirmed: true,
payload: {
weight: this.state.weight,
tare: this.state.tare,
},
});
this.trigger("close-temp-screen");
}
};

Registries.Component.extend(ScaleScreen, ColaborScaleScreen);

return ScaleScreen;
});
13 changes: 0 additions & 13 deletions pos_tare/static/src/js/db.js

This file was deleted.

23 changes: 15 additions & 8 deletions pos_tare/static/src/js/models.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
odoo.define("pos_tare.models", function (require) {
"use strict";
var core = require("web.core");
var models = require("point_of_sale.models");
models.load_fields("product.product", ["tare_weight"]);
var pos_tare_tools = require("pos_tare.tools");
var _t = core._t;

var _super_ = models.Orderline.prototype;
var _superOrder_ = models.Order.prototype;

var OrderWithTare = models.Order.extend({
add_product: function (product, options) {
var res = _superOrder_.add_product.call(this, product, options);
if (options.tare !== undefined) {
this.get_last_orderline().set_tare(options.tare);
}
return res;
},
});

var OrderLineWithTare = models.Orderline.extend({
// /////////////////////////////
// Overload Section
// /////////////////////////////

initialize: function (session, attributes) {
this.tare = 0;
return _super_.initialize.call(this, session, attributes);
Expand Down Expand Up @@ -65,11 +76,6 @@ odoo.define("pos_tare.models", function (require) {
tare_unit,
line_unit
);
var tare_in_product_uom_string = pos_tare_tools.format_tare(
this.pos,
tare_in_product_uom,
line_unit
);
if (update_net_weight) {
var net_quantity = this.get_quantity() - tare_in_product_uom;
// Update the quantity with the new weight net of tare quantity.
Expand Down Expand Up @@ -114,4 +120,5 @@ odoo.define("pos_tare.models", function (require) {
});

models.Orderline = OrderLineWithTare;
models.Order = OrderWithTare;
});
Loading

0 comments on commit 325ae22

Please sign in to comment.