Wie man mit onchange in owl odoo 18 umgehtJavaScript

Javascript-Forum
Anonymous
 Wie man mit onchange in owl odoo 18 umgeht

Post by Anonymous »

Dies ist meine benutzerdefinierte Vorlage, die die Website von weberiten.




Select Shop

Select a Shop







Please select the shop nearest to your location.













< /code>
Und dies ist mein JavaScript-Code, in dem ich den Onchange < /p>
definiert habeimport { _t } from "@web/core/l10n/translation";

export class ShopSelection extends Component {
setup() {
this.rpc = useService("rpc");
}

async onShopChange(ev) {
const shopId = ev.target.value;
if (!shopId) return;

try {
const result = await this.rpc("/select/shop", {
shop_id: parseInt(shopId)
});

if (result.success) {
console.log("Shop changed successfully");
window.location.reload(); // Refresh to apply company changes
} else {
this.showStockErrors(result);
}
} catch (error) {
console.error("Error:", error);
alert(_t("Operation failed"));
}
}

showStockErrors(result) {
// Clear previous errors
document.querySelectorAll("[class^='shop-selection-errors-']").forEach(el => el.innerHTML = "");

// Display new errors
result.products?.forEach(p => {
const errorDiv = document.querySelector(`.shop-selection-errors-${p.id}`);
if (errorDiv) {
errorDiv.innerHTML = `


${_t("Available")}: ${p.available} (${_t("Required")}: ${p.required})


`;
}
});
alert(result.message || _t("Insufficient stock"));
}
}
< /code>
Außerdem ist dies mein Controller, in dem ich die Route und die von mir implementierende Logik definiert habe. Dies soll überprüfen, ob die Bestellung (Produkt) im Karren im ausgewählten Shop (Unternehmen) verfügbar ist. Dieser Teil der Anpassung mache ich auf Odoo -E -Commerce -Modul. < /p>
@http.route(['/select/shop'], type='json', auth="public", website=True)
def stock_check(self, shop_id, **kwargs):
order = request.website.sale_get_order(force_create=True)
if not order or not shop_id:
return {'success': False}
try:
shop = request.env['res.company'].sudo().browse(int(shop_id))
if not shop.exists() or not shop.is_shop:
return {'success': False, 'message': _("Invalid shop")}

# Get ALL internal locations for the shop's company
locations = request.env['stock.location'].sudo().search([
('company_id', '=', shop.id),
('usage', '=', 'internal') # Stock locations only
])
if not locations:
return {'success': False, 'message': _("No stock locations found for this shop")}

out_of_stock = []
for line in order.order_line:
# Only check storable products
if line.product_id.detailed_type != 'product':
continue

# Sum on-hand quantity across all shop's locations
total_available = sum(
line.product_id.with_context(location=loc.id).qty_available
for loc in locations
)

if total_available < line.product_uom_qty:
out_of_stock.append({
'id': line.product_id.id,
'name': line.product_id.name,
'available': total_available,
'required': line.product_uom_qty
})

if out_of_stock:
return {
'success': False,
'message': _("Insufficient stock for %d products", len(out_of_stock)),
'products': out_of_stock
}

# Update order's company
order.sudo().write({
'company_id': shop.id,
'shop_company_id': shop.id
})
return {'success': True}

except Exception as e:
_logger.error("Shop Selection Error: %s", str(e))
return {'success': False, 'message': _("Technical error")}
< /code>
Aber das Problem, mit dem ich ausgesetzt bin, ist, dass die OnChange -Methode nicht ausgelöst wird, sodass die Aktienprüfung nicht ausgeführt wird. Ich bin mir nicht sicher, was das Problem ist. Dies ist mein erstes Mal, dass ich an Odoo 18 arbeite. Jede Hilfe wird aufrichtig geschätzt. Vielen Dank im Voraus.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post