Seeking guidance on building a stock/sales application in JavaScript with Dexie.js. I need assistance in efficiently calculating the Total Sales amount without resorting to overly complicated recursive code that triggers multiple queries for a single product's sales total.
Here is an overview of my schema:
clients: "++id, name, phone",
order: "++id, clientId, date",
order_content: "orderId, productId, qty",
product: "++id, name, mu, mk_cost, sa_cost, prod_cost",
stock: "++id, date, productId, qty, lot"
I maintain different product types in the "Product" table along with their prices and other relevant information. When an order is placed, I record the clientId in the Order table and utilize "order_content" to store the items by linking them with the orderId.
Essentially, I am looking to compute totals for each item and then aggregate those amounts.
An attempt was made to run the following code within a db.product.each() loop, but it seems like there might be a more straightforward approach:
var product1Total = 0;
function calculateTotal(productId, price){
db.order_content
.where("productId")
.equals(productId)
.each(function(item){
product1Total += (price * qty)
})
}
Your insights are greatly appreciated!