0

I am trying to read the total amount of cash payments made in a POS session in Odoo 14 :

Could someone point me into the right direction: i have following code:

get totalCash() {
        // Get order list
            const orders = this.env.pos.get_order_list();
            let totalCash = 0;

        console.log("Order list: ", orders);

        // Iterate over each order
        for (const order of orders) {
            console.log("Order: ", order);

            // Iterate over each payment line of the order
            for (const paymentLine of order.paymentlines.models) {
                console.log("Payment line: ", paymentLine);

                // Check if the payment method is cash
                if (paymentLine.cashregister.journal.type === 'cash') {
                    console.log("Cash register type: ", paymentLine.cashregister.journal.type);
                    console.log("Amount before: ", totalCash);
                    totalCash += paymentLine.amount;
                    console.log("Amount after: ", totalCash);
                }
            }
        }

        return totalCash.toFixed(2);

        }

in the console my output is following: Object { cid: "c49", attributes: {…}, _changing: false, _previousAttributes: {…}, changed: {}, _pending: false, locked: false, pos: {…}, selected_orderline: undefined, selected_paymentline: undefined, … }

So it seems i am finding the objects, but i am not using the right names for the payment lines. Anybody knows how to fix this?

Thanks a lot.

Stephane
  • 29
  • 1
  • 6

1 Answers1

0

You can add additional console.log to investigate your objects Content/Structure (orders, paymentLine...) to find the correct property names. Even better: Use the JavaScript console of your web-browser, by manually adding a break-point in your code (using the command: debugger)... And in your Browser: Open the developer tools by right-clicking on the webpage and selecting "Inspect" or by pressing Ctrl+Shift+I (or Cmd+Option+I on a Mac).

get totalCash() 
    {
    debugger; // Set a breakpoint, to investigate in your Browser
    const orders = this.env.pos.get_order_list();
    let totalCash = 0;

    console.log("Order list: ", orders);

    for (const order of orders) {
        console.log("Order: ", order);

        for (const paymentLine of order.paymentlines.models) {
            console.log("Payment line: ", paymentLine);

            // Check if the payment method is cash
            if (paymentLine.cashregister.journal.type === 'cash') {
                console.log("Cash register type: ", paymentLine.cashregister.journal.type);
                console.log("Amount before: ", totalCash);
                totalCash += paymentLine.amount;
                console.log("Amount after: ", totalCash);
            }
        }
    }

    return totalCash.toFixed(2);
    }
sylvain
  • 853
  • 1
  • 7
  • 20