Sales module controls the active shopping cart on the sales screen. It handles adding products, adjusting quantities, calculating totals, and finalizing transactions — either as cash payments (efectivo) or credit sales (fiado) assigned to a client.
Properties
| Property | Type | Description |
|---|---|---|
cart | array | The current in-memory cart. Each item has id, nombre, precio, and cantidad. Starts empty on every page load. |
Methods
Sales.init()
Binds all cart-related event listeners and renders the initial empty cart. Called once by App.init().
Sales.bindEvents()
Attaches event listeners to:
#searchProducts— callsProducts.search()with a 300 ms debounce.#btnClearCart— callsclearCart().#btnPayCash— callscompleteSale('efectivo').#btnPayCredit— callsselectClientForCredit().#cartItems— delegates clicks on.qty-btnelements toupdateQuantity().
Sales.addToCart(product)
Adds a product to the cart. If the product is already present (matched by id), increments cantidad by 1. Otherwise pushes a new cart item with cantidad: 1. Calls render() and shows a success toast.
A product object from
Storage. Must have id, nombre, and precio.Sales.updateQuantity(productId, action)
Adjusts the quantity of a cart item. 'increase' adds 1; 'decrease' subtracts 1. If cantidad reaches 0 after a decrease, the item is removed from the cart entirely. Calls render() after the update.
The
id of the product in the cart.The quantity operation. Either
'increase' or 'decrease'.Sales.getTotal()
Calculates the cart total by summing precio * cantidad for every item.
The total price of all items currently in the cart.
Sales.render()
Re-renders the cart UI. Writes item rows to #cartItems and the total to #cartTotal. Shows a “Carrito vacío” message when the cart is empty.
Sales.clearCart()
Prompts the user to confirm, then empties the cart array and calls render(). Shows a success toast. Does nothing if the cart is already empty.
Sales.completeSale(tipo, clientId)
Finalizes and records a sale. Builds a sale object from the current cart and calls Storage.addSale(). For credit sales (tipo === 'fiado'), also calls Storage.addDebtNote() to add the debt to the client’s record. Clears the cart, shows a toast, and if the sale was on credit, reloads the clients list via Clients.loadClients().
Shows a warning toast and returns early if the cart is empty.
Payment type. Either
'efectivo' (cash) or 'fiado' (credit).The id of the client to assign the debt to. Required when
tipo is 'fiado'.Sales.selectClientForCredit()
Validates that the cart is not empty, then opens the client selector modal by calling Clients.showClientSelector(). Passes a callback that calls completeSale('fiado', clientId) and hides the selectClient modal once a client is chosen.
Shows a warning toast if the cart is empty.
Sales.escapeHtml(text)
Sanitizes a string to prevent XSS when injecting user-provided content into HTML. Creates a temporary <div> element, assigns the text as textContent, and returns innerHTML.
The raw string to sanitize.
The HTML-escaped string safe for insertion into the DOM.