0

Issue with adding products to the cart and creating invoices

Merry Christmas )

am using Ninox to manage sales and carts, and I am facing an issue when adding products to the cart. I want to implement the functionality where:

  1. If the client already has an open cart (invoice with CartClosed = "No"), the new product should be added to that cart.
  2. If the client doesn’t have an open cart, a new invoice should be created, and the product should be added to that new invoice.

However, despite the correct condition checks, the system always creates a new invoice even if an open cart already exists. I am using the following code:

let selectedClient := this.Clients;
 let xNewProduct := this.Products;
 let xNewQuantity := this.Quantity;
let openInvoice := first(select Sales where Clients = selectedClient and CartClosed = "No");
 if openInvoice != null then

let existingProduct := first(select Sail_tab where Sales = openInvoice and Products = xNewProduct);
if existingProduct != null then
existingProduct.('Quantity1' := existingProduct.'Quantity1' + xNewQuantity);
else
let newSailTab := create Sail_tab;
newSailTab.(Sales := openInvoice);
newSailTab.(Products := xNewProduct);
newSailTab.('Quantity1' := xNewQuantity);
end
  else
 let newInvoice := create Sales;
newInvoice.(Date := today());
newInvoice.(Clients := selectedClient);
newInvoice.(CartClosed := "No");

let newSailTab := create Sail_tab;
 newSailTab.(Sales := newInvoice);
 newSailTab.(Products := xNewProduct);
 newSailTab.('Quantity1' := xNewQuantity);
end;

this.Products := 0;
this.Quantity := null;

What’s not working:

  1. When adding a product to the cart, it always creates a new invoice even if an open cart already exists.
  2. It’s not correctly finding and using the open invoice to add products.

What I’ve tried:

  1. Using a query to find the open invoice with CartClosed = "No".
  2. If an open cart is found, I add the product to it.
  3. If no cart is found, I create a new invoice.

Question: Why is it not finding the open invoice, and why does it always create a new one? How can I fix this code to correctly work with already existing open carts?

10 replies

null
    • Fred
    • 3 wk ago
    • Reported - view

    I would test out line 4 by itself to see that you are actually getting the records you want. So in a new formula field just copy the first 4 lines and then put this at the end:

    debugValueInfo(openInvoice)
    

    Do you see a nId returned when you select a client?

    After you solve the above, it looks like Sales and Clients are linked so you can replace the select in openInvoice with:

    let openInvoice := first(selectedClient.Sales[CartClosed = "No"]);
    
    • Ninox partner
    • RoSoft_Steven.1
    • 2 wk ago
    • Reported - view

    I suppose your CartClosed is a Yes/no field? Then you should know that such fields have 3 states: true, false and null. 
    So if you want to check if the CartClosed is "No" it will probably would be null since you never touched it. A solution would be is to check if it is not "Yes". Changing line 4 in your code to: let openInvoice := first(select Sales where Clients = selectedClient and CartClosed != true); can probably help...

      • Fred
      • 2 wk ago
      • Reported - view

      That is a gotcha that is not talked about a lot. So make sure when you start your tables to always default your yes/no to a value so all of your fields have some value.

      • Ninox partner
      • RoSoft_Steven.1
      • 2 wk ago
      • Reported - view

       True

      • iliper LTD
      • iliper_LTD
      • 2 wk ago
      • Reported - view

       

      Changes Made:

      1. Created a new field for the cart status:
        We updated the CartClosed field to allow three possible values:

        • 1 — cart is open,
        • 2 — cart is closed,
        • 3 — cart is open or undefined (used if the cart has not yet been finalized).
      2. Following your advice and Fred’s suggestions:

        • Checking the cart status field: We implemented the advice from the forum and Fred’s suggestion to consider three values for the CartClosed field (true, false, and null), and adjusted the check to account for all three possible states.
        • Checking for an existing invoice: We added a check to search for open invoices with CartClosed set to 1 or 3, which helps identify existing open carts and avoid creating new ones if one is already open.

      Issue:

      Despite the changes we made, the code still creates a new invoice each time the "Add to Cart" button is clicked, even if an open cart already exists.

       

      let selectedClient := this.Clients;  // Get the selected client from the current form
      let xNewProduct := this.Products;  // Get the selected product from the current form
      let xNewQuantity := this.Quantity;  // Get the selected quantity from the current form
      
      // Check if there is an open invoice (CartClosed is either 1 or 3)
      let openInvoice := first(selectedClient.Sales[CartClosed = 1 or CartClosed = 3]);
      
      // If an open invoice exists, check if the product is already in the invoice
      if openInvoice != null then
          let existingProduct := first(openInvoice.Sales_tab[Products = xNewProduct]);  // Look for the product in the current invoice
      
          // If the product exists, increase the quantity
          if existingProduct != null then
              existingProduct.('Quantity1' := existingProduct.'Quantity1' + xNewQuantity);  // Add the quantity to the existing quantity
          else
              // If the product doesn't exist in the invoice, create a new entry in Sales_tab
              let newSailTab := create Sales_tab;  // Create a new record in Sales_tab
              newSailTab.('Sales' := openInvoice);  // Link the new Sales_tab record to the current invoice (openInvoice)
              newSailTab.('Products' := xNewProduct);  // Link the product to the new Sales_tab record
              newSailTab.('Quantity1' := xNewQuantity);  // Set the quantity for this product in the new Sales_tab record
          end
      else
          // If no open invoice exists, create a new invoice
          let newInvoice := create Sales;  // Create a new invoice in the Sales table
          newInvoice.('Date' := today());  // Set the date for the new invoice to today
          newInvoice.('CartClosed' := 3);  // Set CartClosed to 3 (meaning the cart is open or undefined)
          newInvoice.('Clients' := selectedClient);  // Link the new invoice to the selected client
      
          // Create a new entry in Sales_tab for this new invoice
          let newSailTab := create Sales_tab;  // Create a new record in Sales_tab
          newSailTab.('Sales' := newInvoice);  // Link the new Sales_tab record to the newly created invoice
          newSailTab.('Products' := xNewProduct);  // Link the selected product to the Sales_tab record
          newSailTab.('Quantity1' := xNewQuantity);  // Set the quantity for this product in the new Sales_tab record
      end;
      
      // Reset the product and quantity fields after adding to the cart
      this.Products := null;  // Clear the selected product field
      this.Quantity := null;  // Clear the selected quantity field
      
      

      Database Structure:

      1. Clients Table:

        • This table contains information about the clients, including their details (name, contact information, etc.).
        • The Sales field within the Clients table is a linked table that stores the client’s sales records (invoices).
      2. Sales Table:

        • The Sales table is a sub-table within the Clients table. Each record in this table represents a single invoice or cart.
        • The CartClosed field in the Sales table indicates whether the cart is open, closed, or has an undefined value (3 possible states: 1 = open, 2 = closed, 3 = undefined).
      3. Sales_tab Table:

        • The Sales_tab table is another linked table within Sales that tracks individual products in each sales record.
        • Each record in the Sales_tab table represents a product added to an invoice, with fields for Products(product), Price  and Quantity1 (quantity).

      How the Tables Are Linked:

      • When I click the "Add to Cart" button, a record is created in the Sales_tab table, which is linked to the specific invoice in the Sales table.
      • When I click the "Add to Cart" button, the system first checks if there is an open invoice (a record in Sales with CartClosed set to 1 or 3).
      • If an open invoice exists, the system checks if the product is already in the Sales_tab. If the product is there, its quantity is updated. If not, a new entry is created for that product in the Sales_tab.
      • If no open invoice exists, a new Sales record is created for the client, and a new entry is added in the Sales_tab.
      • Fred
      • 2 wk ago
      • Reported - view

      Take a look at my first suggestion. The first step in troubleshooting is to figure out what kind of data you are returning. So check to see what is actually returned in the variable openInvoice.

      Copy lines 1-6 into another formula field, then at the end put:

      debugValueInfo(openInvoice)
      

      What do you get?

      • Ninox developper
      • Jacques_TUR
      • 2 wk ago
      • Reported - view

      is right; it's essential to break down the code to identify the source of the issue.
       , in the client table, add a formula that returns the open cart.

      first(Sales[CartClosed = 1 or CartClosed = 3])

      Then, check whether this formula returns undefined or a record number.

      If the formula correctly returns the open cart, then use this formula in your code:

      // If an open invoice exists, check if the product is already in the invoice
      if (selectedClient.OpenInvoice != null) {    // Your logic here}

       

      • iliper LTD
      • iliper_LTD
      • 2 wk ago
      • Reported - view

       - rid(162), fixed everything and so far everything works without problems. Thank you very much. Happy New Year, best wishes from me

      let selectedClient := this.Clients;
      let xNewProduct := this.Products;
      let xNewQuantity := this.Quantity;
      let openInvoice := first(selectedClient.Sales[CartClosed = 1 or CartClosed = 3]);
      if openInvoice != null then
          let existingProduct := first(openInvoice.Sales_tab[Products = xNewProduct]);
          if existingProduct != null then
              existingProduct.(Quantity1 := existingProduct.Quantity1 + xNewQuantity)
          else
              let newSailTab := (create Sales_tab);
              newSailTab.(Sales := openInvoice);
              newSailTab.(Products := xNewProduct);
              newSailTab.(Quantity1 := xNewQuantity)
          end
      else
          let newInvoice := (create Sales);
          newInvoice.(Date := today());
          newInvoice.(CartClosed := 3);
          newInvoice.(Clients := selectedClient);
          let newSailTab := (create Sales_tab);
          newSailTab.(Sales := newInvoice);
          newSailTab.(Products := xNewProduct);
          newSailTab.(Quantity1 := xNewQuantity)
      end;
      this.(Products := 0);
      this.(Quantity := null)

      There was one problem with the filter, can't set it up. But after the New Year, I'll do it)

      • iliper LTD
      • iliper_LTD
      • 2 wk ago
      • Reported - view

       all done and working, formula below. Thank you, happy new year to you, best wishes

      • iliper LTD
      • iliper_LTD
      • 2 wk ago
      • Reported - view

       all done and working, formula below. Thank you, happy new year to you, best wishes