Skip to content

[ADD] pos_second_uom: create custom module for adding second UOM in product #834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 18.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pos_second_uom/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
15 changes: 15 additions & 0 deletions pos_second_uom/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "POS second UOM",
"version": "1.0",
"depends": ["base", "point_of_sale", "web"],
"category": "Sales/Point of Sale",
"data": ["views/product_view.xml"],
"assets": {
"point_of_sale._assets_pos": [
"pos_second_uom/static/src/**/",
],
},
"sequence": 1,
"application": True,
"license": "OEEL-1",
}
1 change: 1 addition & 0 deletions pos_second_uom/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import product_template
16 changes: 16 additions & 0 deletions pos_second_uom/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from odoo import fields, models


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

uom_category_id = fields.Many2one(
related="uom_id.category_id", string="UOM Category", readonly=True, store=True
)

pos_second_uom_id = fields.Many2one(
comodel_name="uom.uom",
string="POS second UOM",
domain="[('category_id', '=', uom_category_id)]",
store=True,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Component } from "@odoo/owl"
import { useService } from "@web/core/utils/hooks";
import { AddQuantityDialog } from "../add_quantity_dialog/add_quantity_dialog";


export class AddQuantityButton extends Component {
static template = "pos_second_uom.AddQuantityButton"

setup() {
super.setup();
this.dialogService = useService("dialog");
}

onClick() {
this.dialogService.add(AddQuantityDialog)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="pos_add_quantity_button" xml:space="preserve">
<t t-name="pos_second_uom.AddQuantityButton">
<button class="set-partner btn btn-light btn-lg lh-lg text-truncate w-auto" t-on-click="onClick">
Add Quantity
</button>
</t>
</templates>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/** @odoo-module **/

import { Component, useRef } from "@odoo/owl";
import { Dialog } from "@web/core/dialog/dialog";
import { usePos } from "@point_of_sale/app/store/pos_hook";

export class AddQuantityDialog extends Component {
static template = "pos_second_uom.AddQuantityDialog";

static components = { Dialog };

static props = {
close: { type: Function }
};

setup() {
this.pos = usePos()
this.quantityInputRef = useRef("quantityInput");
}

confirm() {
const quantity = this.quantityInputRef.el.value;
const order = this.pos.get_order();
const selectedOrderline = order.get_selected_orderline();
const product = selectedOrderline.get_product();
const originalUOM = product.uom_id.category_id.uom_ids[0];
const secondUOM = product.uom_id.category_id.uom_ids[1];
const convertedQuantity = quantity * (secondUOM.factor_inv / originalUOM.factor);
selectedOrderline.set_quantity(convertedQuantity);
this.props.close();
}

cancel() {
this.props.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="pos_add_quantity_dialog" xml:space="preserve">
<t t-name="pos_second_uom.AddQuantityDialog">
<Dialog title="'Add Quantity'" size="'md'">
<main class="modal-body p-4">
<div>
<label for="quantityInput" class="form-label mb-2">Quantity:</label>
<input type="number" id="quantityInput" class="form-control" value="1" min="1" t-ref="quantityInput"/>
</div>
</main>
<t t-set-slot="footer">
<button type="button" class="btn btn-secondary me-2" t-on-click="cancel">Cancel</button>
<button type="button" class="btn btn-primary" t-on-click="confirm">Confirm</button>
</t>
</Dialog>
</t>
</templates>
13 changes: 13 additions & 0 deletions pos_second_uom/static/src/control_buttons/control_buttons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { patch } from "@web/core/utils/patch";
import { ControlButtons } from "@point_of_sale/app/screens/product_screen/control_buttons/control_buttons";
import { AddQuantityButton } from "../add_quantity_button/add_quantity_button";

patch(ControlButtons, {
setup() {
super.setup();
},
components: {
...ControlButtons.components,
AddQuantityButton,
},
});
8 changes: 8 additions & 0 deletions pos_second_uom/static/src/control_buttons/control_buttons.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates id="pos_add_quantity_button" xml:space="preserve">
<t t-name="pos_second_uom.AddQuantityButton" t-inherit="point_of_sale.ControlButtons" t-inherit-mode="extension">
<xpath expr="//button[hasclass('flex-shrink-0')]" position="before">
<AddQuantityButton/>
</xpath>
</t>
</templates>
14 changes: 14 additions & 0 deletions pos_second_uom/views/product_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="product_template_form_view_inherit" model="ir.ui.view">
<field name="name">product.template.form.inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<xpath expr="//field[@name='pos_categ_ids']" position="after">
<field name="uom_category_id" invisible="1" />
<field name="pos_second_uom_id" />
</xpath>
</field>
</record>
</odoo>