Skip to content

Commit

Permalink
fixup! fixup! [MIG] pos_deposit to v16
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Dec 26, 2024
1 parent 0c53399 commit b040a3f
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 80 deletions.
5 changes: 4 additions & 1 deletion pos_container_deposit/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"demo/product_product.xml",
],
"assets": {
"web.assets_tests": [
"pos_container_deposit/static/tests/tours/*.js",
],
"point_of_sale.assets": [
"pos_container_deposit/static/src/js/models.js",
"pos_container_deposit/static/src/js/*.js",
],
},
"installable": True,
Expand Down
22 changes: 22 additions & 0 deletions pos_container_deposit/static/src/js/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright 2024 Hunki Enterprises BV
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
*/
odoo.define("pos_container_deposit.components", function (require) {
"use strict";

const Orderline = require("point_of_sale.Orderline");
const Registries = require("point_of_sale.Registries");
const PosDepositOrderlineExtension = (Orderline) =>
class PosDepositOrderline extends Orderline {
selectLine() {
/*
* Don't allow selecting deposit products
*/
if (!this.props.line.is_container_deposit) {
super.selectLine(...arguments);
}
}
};

Registries.Component.extend(Orderline, PosDepositOrderlineExtension);
});
159 changes: 80 additions & 79 deletions pos_container_deposit/static/src/js/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@ odoo.define("pos_container_deposit.models", function (require) {
"use strict";

const {Order, Orderline} = require("point_of_sale.models");
const OrderlineComponent = require("point_of_sale.Orderline");
const Registries = require("point_of_sale.Registries");
const {Gui} = require("point_of_sale.Gui");
const core = require("web.core");
const _t = core._t;

const PosDepositOrderExtension = (Order) =>
// TODO: add deposit products qith negative quantity, never merge them, find deposit product line with positive quantity
class PosDepositOrder extends Order {
add_product(product, options) {
add_product(product) {
/*
* Whenn adding a product with deposit, also add its deposit product
* Show an error message when adding a product with a container deposit product that is not loaded
*/
super.add_product(...arguments);
var deposit = null;
if (product.deposit_product_id) {
const deposit =
this.pos.db.product_by_id[product.deposit_product_id[0]];
deposit = this.pos.db.product_by_id[product.deposit_product_id[0]];
if (!deposit) {
Gui.showPopup("ErrorPopup", {
title: _t("Deposit not available"),
Expand All @@ -35,21 +32,28 @@ odoo.define("pos_container_deposit.models", function (require) {
});
return false;
}
const selected_orderline = this.selected_orderline;
super.add_product(deposit, options);
if (selected_orderline) {
this.select_orderline(selected_orderline);
}
}
super.add_product(...arguments);
}
add_orderline(line) {
/*
* When adding a product with container deposit, add its container deposit product
*/
super.add_orderline(...arguments);
if (line.container_deposit_product && !line.container_deposit_line) {
this.add_product(line.container_deposit_product, {
quantity: line.get_quantity(),
});
line.container_deposit_line = this.get_last_orderline();
line.container_deposit_line.is_container_deposit = true;
this.select_orderline(line);
}
}
select_orderline(line) {
/*
* Never select an orderline with deposit, delect one next to it instead
* Never select an orderline with deposit, select one next to it instead
*/
const product = line
? this.pos.db.product_by_id[line.product.id]
: null;
if (product && product.is_deposit) {
if (line && line.is_container_deposit) {
const line_index = this.orderlines.indexOf(line);
if (line_index >= 0 && this.orderlines.length > 1) {
super.select_orderline(
Expand All @@ -60,92 +64,89 @@ odoo.define("pos_container_deposit.models", function (require) {
super.select_orderline(...arguments);
}
}
can_be_merged_with(orderline) {
};

const PosDepositOrderlineExtension = (Orderline) =>
class PosDepositOrderLine extends Orderline {
constructor() {
/*
* Never merge deposit orderlines
* Set container deposit specific properties
*/
super(...arguments);
const deposit_product = this.product
? this.pos.db.get_product_by_id(this.product.id).deposit_product_id
: null;
if (deposit_product) {
this.container_deposit_product = this.pos.db.get_product_by_id(
deposit_product[0]
);
}
}
get_orderline_for_product_id(product_id) {
init_from_JSON(json) {
/*
* Return the first orderline object for a product
* Restore container deposit specific properties and link between line with deposit and deposit line
*/
for (var i = 0; i < this.orderlines.length; i++) {
if (this.orderlines[i].product.id === product_id) {
return this.orderlines[i];
super.init_from_JSON(json);
if (json.container_deposit_line_id) {
this.container_deposit_line =
this.order.get_orderline(json.container_deposit_line_id) ||
json.container_deposit_line_id;
}
if (json.is_container_deposit) {
for (var i = 0; i < this.order.orderlines.length; i++) {
if (
this.order.orderlines[i].container_deposit_line === this.id
) {
this.order.orderlines[i].container_deposit_line = this;
break;
}
}
this.is_container_deposit = true;
}
return null;
}
};

const PosDepositOrderlineExtension = (Orderline) =>
class PosDepositOrderLine extends Orderline {
set_quantity(quantity, keep_price) {
export_as_JSON() {
/*
* When setting quantity of a product with deposit, also add its deposit product
* Export deposit line as id
*/
const difference =
quantity === "remove" ? -this.quantity : quantity - this.quantity;
const result = super.set_quantity(...arguments);
if (
(difference || quantity === "remove") &&
this.product.deposit_product_id
) {
const deposit_line = this.order.get_orderline_for_product_id(
this.product.deposit_product_id[0]
);
if (deposit_line) {
var deposit_quantity = deposit_line.quantity + difference;
deposit_line.set_quantity(
deposit_quantity
? deposit_quantity
: quantity === "remove"
? quantity
: deposit_quantity,
keep_price
);
}
const result = super.export_as_JSON();
result.is_container_deposit = this.is_container_deposit;
if (this.container_deposit_line) {
result.container_deposit_line_id = this.container_deposit_line.id;
}
return result;
}
merge(orderline) {
set_quantity(quantity, keep_price) {
/*
* Merging orderlines with products having a deposit increases the amount
* of deposit products - reset that
* When setting quantity of a product with deposit, also add to its deposit line
*/
var deposit_line = null,
deposit_quantity = 0;
if (this.product.deposit_product_id) {
deposit_line = this.order.get_orderline_for_product_id(
this.product.deposit_product_id[0]
const difference =
quantity === "remove" ? -this.quantity : quantity - this.quantity;
const deposit_line = this.container_deposit_line;
const result = super.set_quantity(...arguments);
if ((difference || quantity === "remove") && deposit_line) {
var deposit_quantity = deposit_line.quantity + difference;
deposit_line.set_quantity(
deposit_quantity
? deposit_quantity
: quantity === "remove"
? quantity
: deposit_quantity,
keep_price
);
deposit_quantity = deposit_line ? deposit_line.get_quantity() : 0;
}
super.merge(...arguments);
if (deposit_line) {
deposit_line.set_quantity(deposit_quantity);
}
return result;
}
};

const PosDepositOrderlineComponentExtension = (OrderlineComponent) =>
class PosDepositOrderlineComponent extends OrderlineComponent {
selectLine() {
can_be_merged_with(orderline) {
/*
* Don't allow selecting deposit products
* Never merge deposit orderlines
*/
const line = this.props.line;
const product = line.pos.db.product_by_id[line.product.id];
if (!product || !product.is_deposit) {
super.selectLine(...arguments);
if (this.is_container_deposit || orderline.is_container_deposit) {
return false;
}
return super.can_be_merged_with(...arguments);
}
};

Registries.Model.extend(Order, PosDepositOrderExtension);
Registries.Model.extend(Orderline, PosDepositOrderlineExtension);
Registries.Component.extend(
OrderlineComponent,
PosDepositOrderlineComponentExtension
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
odoo.define("pos_container_deposit.test_tour", function (require) {
"use strict";

const {ProductScreen} = require("point_of_sale.tour.ProductScreenTourMethods");
const {getSteps, startSteps} = require("point_of_sale.tour.utils");
const Tour = require("web_tour.tour");
const product_name = "Generic sugar liquid";
const deposit_product_name = "Bottle deposit .25";

startSteps();

ProductScreen.do.clickHomeCategory();
ProductScreen.do.clickDisplayedProduct(product_name);
ProductScreen.do.clickDisplayedProduct(product_name);
ProductScreen.check.checkOrderlinesNumber(2);
ProductScreen.check.selectedOrderlineHas(product_name);
getSteps().push({
content: `selecting orderline with product '${deposit_product_name}' and quantity '2.0'`,
trigger: `.order .orderline:not(:has(.selected)) .product-name:contains("${deposit_product_name}") ~ .info-list em:contains("2.0")`,
});
ProductScreen.check.selectedOrderlineHas(product_name);
ProductScreen.do.pressNumpad("Price");
ProductScreen.check.modeIsActive("Price");
ProductScreen.do.pressNumpad("5");
ProductScreen.check.selectedOrderlineHas(product_name, "2", "10");
ProductScreen.do.clickDisplayedProduct(product_name);
ProductScreen.do.clickDisplayedProduct(product_name);
ProductScreen.check.checkOrderlinesNumber(4);
ProductScreen.do.pressNumpad("Qty");
ProductScreen.do.pressNumpad("Backspace");
ProductScreen.do.pressNumpad("Backspace");
ProductScreen.check.modeIsActive("Qty");
ProductScreen.check.selectedOrderlineHas(product_name, "2", "10");
ProductScreen.check.checkOrderlinesNumber(2);
ProductScreen.do.pressNumpad("Qty");
ProductScreen.do.pressNumpad("Backspace");
ProductScreen.do.pressNumpad("Backspace");
ProductScreen.check.modeIsActive("Qty");
ProductScreen.check.orderIsEmpty();

Tour.register(
"pos_container_deposit.test_tour",
{test: true, url: "/pos/ui"},
getSteps()
);
});
1 change: 1 addition & 0 deletions pos_container_deposit/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_pos_container_deposit
from . import test_pos_container_deposit_frontend
14 changes: 14 additions & 0 deletions pos_container_deposit/tests/test_pos_container_deposit_frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo.tests.common import tagged

from odoo.addons.point_of_sale.tests.test_frontend import TestPointOfSaleHttpCommon


@tagged("post_install", "-at_install")
class TestPosContainerDeposit(TestPointOfSaleHttpCommon):
def run_tour(self):
self.main_pos_config.open_ui()
self.start_tour(

Check warning on line 10 in pos_container_deposit/tests/test_pos_container_deposit_frontend.py

View check run for this annotation

Codecov / codecov/patch

pos_container_deposit/tests/test_pos_container_deposit_frontend.py#L9-L10

Added lines #L9 - L10 were not covered by tests
"/pos/ui?config_id=%d" % self.main_pos_config.id,
"pos_container_deposit.test_tour",
login="accountman",
)
6 changes: 6 additions & 0 deletions setup/pos_container_deposit/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit b040a3f

Please sign in to comment.