Calculation of Cost of Goods Sold

Cost of Goods Sold (COGS) Calculator & Guide :root { –primary-color: #004a99; –success-color: #28a745; –background-color: #f8f9fa; –text-color: #333; –border-color: #ddd; –card-background: #fff; –shadow: 0 2px 5px rgba(0,0,0,0.1); } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(–background-color); color: var(–text-color); line-height: 1.6; margin: 0; padding: 0; } .container { max-width: 1000px; margin: 20px auto; padding: 20px; background-color: var(–card-background); border-radius: 8px; box-shadow: var(–shadow); } header { background-color: var(–primary-color); color: white; padding: 20px 0; text-align: center; margin-bottom: 20px; border-radius: 8px 8px 0 0; } header h1 { margin: 0; font-size: 2.5em; } h2, h3 { color: var(–primary-color); margin-top: 1.5em; margin-bottom: 0.5em; } .calculator-section { background-color: var(–card-background); padding: 30px; border-radius: 8px; box-shadow: var(–shadow); margin-bottom: 30px; } .calculator-section h2 { text-align: center; margin-bottom: 20px; } .loan-calc-container { display: flex; flex-direction: column; gap: 15px; } .input-group { display: flex; flex-direction: column; gap: 5px; } .input-group label { font-weight: bold; color: var(–primary-color); } .input-group input[type="number"], .input-group select { padding: 10px; border: 1px solid var(–border-color); border-radius: 4px; font-size: 1em; width: calc(100% – 22px); /* Adjust for padding and border */ } .input-group .helper-text { font-size: 0.85em; color: #666; } .error-message { color: red; font-size: 0.8em; margin-top: 5px; min-height: 1.2em; /* Prevent layout shift */ } .button-group { display: flex; gap: 10px; margin-top: 20px; justify-content: center; flex-wrap: wrap; } button { padding: 12px 25px; border: none; border-radius: 5px; cursor: pointer; font-size: 1em; font-weight: bold; transition: background-color 0.3s ease; } .btn-primary { background-color: var(–primary-color); color: white; } .btn-primary:hover { background-color: #003366; } .btn-success { background-color: var(–success-color); color: white; } .btn-success:hover { background-color: #218838; } .btn-secondary { background-color: #6c757d; color: white; } .btn-secondary:hover { background-color: #5a6268; } #results { margin-top: 30px; padding: 25px; background-color: var(–primary-color); color: white; border-radius: 8px; text-align: center; box-shadow: inset 0 0 10px rgba(0,0,0,0.2); } #results h3 { color: white; margin-bottom: 15px; } .result-item { margin-bottom: 10px; font-size: 1.1em; } .result-item strong { display: inline-block; min-width: 200px; /* Align labels */ text-align: right; margin-right: 10px; } .main-result { font-size: 2em; font-weight: bold; margin-top: 15px; padding: 10px; background-color: rgba(255, 255, 255, 0.2); border-radius: 5px; } .formula-explanation { font-size: 0.9em; color: #eee; margin-top: 15px; padding-top: 10px; border-top: 1px solid rgba(255, 255, 255, 0.3); } table { width: 100%; border-collapse: collapse; margin-top: 20px; margin-bottom: 30px; box-shadow: var(–shadow); } th, td { padding: 12px 15px; text-align: left; border: 1px solid var(–border-color); } thead { background-color: var(–primary-color); color: white; } tbody tr:nth-child(even) { background-color: #f2f2f2; } caption { font-size: 1.1em; font-weight: bold; color: var(–primary-color); margin-bottom: 10px; text-align: left; } canvas { display: block; margin: 20px auto; background-color: white; border-radius: 5px; box-shadow: var(–shadow); } .article-content { background-color: var(–card-background); padding: 30px; border-radius: 8px; box-shadow: var(–shadow); margin-top: 30px; } .article-content h2, .article-content h3 { color: var(–primary-color); } .article-content p { margin-bottom: 1em; } .article-content ul, .article-content ol { margin-left: 20px; margin-bottom: 1em; } .article-content li { margin-bottom: 0.5em; } .faq-item { margin-bottom: 15px; padding-bottom: 10px; border-bottom: 1px dashed var(–border-color); } .faq-item:last-child { border-bottom: none; } .faq-item strong { display: block; color: var(–primary-color); margin-bottom: 5px; } .internal-links ul { list-style: none; padding: 0; } .internal-links li { margin-bottom: 10px; } .internal-links a { color: var(–primary-color); text-decoration: none; font-weight: bold; } .internal-links a:hover { text-decoration: underline; } .internal-links span { font-size: 0.9em; color: #555; display: block; margin-top: 3px; } .highlight { background-color: var(–success-color); color: white; padding: 2px 5px; border-radius: 3px; font-weight: bold; } .variable-table th, .variable-table td { border: 1px solid var(–border-color); } .variable-table th { background-color: var(–primary-color); color: white; } .variable-table td { background-color: var(–card-background); } .variable-table tr:nth-child(even) { background-color: #f2f2f2; } .variable-table { box-shadow: none; } .chart-container { text-align: center; margin-top: 30px; padding: 20px; background-color: var(–card-background); border-radius: 8px; box-shadow: var(–shadow); } .chart-container h3 { margin-bottom: 15px; } .chart-caption { font-size: 0.9em; color: #555; margin-top: 10px; display: block; }

Cost of Goods Sold (COGS) Calculator

Calculate Your Cost of Goods Sold

Value of inventory at the start of the period.
Cost of all inventory acquired during the period.
Value of inventory remaining at the end of the period.

Calculation Summary

Goods Available for Sale:
Cost of Goods Sold (COGS):
Gross Profit (if Sales Revenue provided):
COGS = Beginning Inventory + Purchases – Ending Inventory

Inventory Flow Over Period

Visualizing inventory levels and COGS.
COGS Calculation Breakdown
Component Value Unit
Beginning Inventory Currency
Purchases Currency
Goods Available for Sale Currency
Ending Inventory Currency
Cost of Goods Sold (COGS) Currency

What is Cost of Goods Sold (COGS)?

Cost of Goods Sold (COGS) is a crucial accounting term that represents the direct costs attributable to the production or purchase of the goods sold by a company during a period. It includes the cost of materials used in the creation of a product, as well as the direct labor costs used to produce that product. COGS does not include indirect expenses such as distribution costs, sales force costs, or general administrative expenses. Understanding and accurately calculating your Cost of Goods Sold is fundamental for assessing a business's profitability and operational efficiency.

Businesses that hold inventory, such as retailers, manufacturers, and wholesalers, need to track their Cost of Goods Sold. This metric is vital for determining gross profit, which is a key indicator of how well a company is managing its production or procurement costs relative to its revenue. Miscalculating COGS can lead to flawed financial statements, incorrect pricing strategies, and poor business decisions.

Common Misconceptions about COGS:

  • COGS includes all business expenses: This is incorrect. COGS only includes direct costs tied to producing or acquiring the goods sold. Marketing, rent, and salaries of administrative staff are operating expenses, not COGS.
  • COGS is the same as inventory value: COGS is the cost of inventory *sold*, while inventory value is the cost of inventory *on hand*.
  • COGS is fixed: While some components might be stable, COGS fluctuates with sales volume, material costs, and production efficiency.

Cost of Goods Sold (COGS) Formula and Mathematical Explanation

The fundamental formula for calculating the Cost of Goods Sold is straightforward and relies on tracking inventory levels over a specific accounting period (e.g., a month, quarter, or year).

The COGS Formula:

COGS = Beginning Inventory + Purchases - Ending Inventory

Let's break down each component:

  • Beginning Inventory: This is the value of all inventory a company had on hand at the start of the accounting period. It's essentially the ending inventory from the previous period.
  • Purchases: This represents the total cost of all inventory acquired during the current accounting period. This includes the purchase price of goods, plus any freight-in costs, and any adjustments for returns or allowances. For manufacturers, this would include the cost of raw materials and direct labor added during the period.
  • Goods Available for Sale: This is an intermediate calculation representing the total cost of inventory that was available to be sold during the period. It's calculated as: Goods Available for Sale = Beginning Inventory + Purchases.
  • Ending Inventory: This is the value of all inventory remaining unsold at the end of the accounting period. This value is determined through physical inventory counts or perpetual inventory systems.

By adding the cost of inventory acquired (Purchases) to the inventory already on hand (Beginning Inventory), we get the total value of goods that *could have been sold*. Subtracting the value of inventory that *wasn't* sold (Ending Inventory) leaves us with the cost of the inventory that *was* sold, which is the Cost of Goods Sold.

COGS Formula Variables
Variable Meaning Unit Typical Range
Beginning Inventory Inventory value at the start of the period. Currency ($) $0 to Millions
Purchases Cost of inventory acquired during the period. Currency ($) $0 to Millions
Goods Available for Sale Total inventory cost available for sale. Currency ($) $0 to Millions
Ending Inventory Inventory value remaining at the end of the period. Currency ($) $0 to Millions
Cost of Goods Sold (COGS) Direct costs of goods sold during the period. Currency ($) $0 to Millions

Practical Examples (Real-World Use Cases)

Let's illustrate the Cost of Goods Sold calculation with two practical examples.

Example 1: A Small Retail Boutique

"Chic Threads," a women's clothing boutique, wants to calculate its COGS for the month of March.

  • Beginning Inventory (March 1st): $15,000 (Value of clothes, accessories, etc., on hand)
  • Purchases (During March): $8,000 (Cost of new inventory received, including shipping)
  • Ending Inventory (March 31st): $12,000 (Value of remaining inventory after a physical count)

Calculation:

Goods Available for Sale = $15,000 (Beginning Inv.) + $8,000 (Purchases) = $23,000

COGS = $23,000 (Goods Available) – $12,000 (Ending Inv.) = $11,000

Interpretation: Chic Threads incurred $11,000 in direct costs for the merchandise it sold during March. If their sales revenue for March was $25,000, their Gross Profit would be $14,000 ($25,000 – $11,000).

Example 2: A Craft Brewery

"Hop Haven Brewery" needs to determine its COGS for the quarter ending June 30th. For a brewery, COGS includes raw materials (malt, hops, yeast), packaging (bottles, cans, labels), and direct labor involved in brewing and packaging.

  • Beginning Inventory (April 1st): $5,000 (Raw materials, packaging, finished goods)
  • Purchases (During Q2): $12,000 (Cost of new ingredients, cans, labels, direct labor wages for brewers/packagers)
  • Ending Inventory (June 30th): $7,000 (Remaining raw materials, packaging, and unsold finished beer)

Calculation:

Goods Available for Sale = $5,000 (Beginning Inv.) + $12,000 (Purchases) = $17,000

COGS = $17,000 (Goods Available) – $7,000 (Ending Inv.) = $10,000

Interpretation: Hop Haven Brewery's direct costs for the beer sold in the second quarter amounted to $10,000. This figure helps them understand the cost efficiency of their brewing and packaging operations.

How to Use This Cost of Goods Sold (COGS) Calculator

Our free online Cost of Goods Sold calculator is designed for simplicity and accuracy. Follow these steps to get your COGS:

  1. Enter Beginning Inventory: Input the total value of inventory you had on hand at the very start of your accounting period (e.g., month, quarter, year).
  2. Enter Purchases: Add the total cost of all inventory you acquired or produced during that same period. This includes raw materials, direct labor, and manufacturing overhead for producers, or the purchase cost plus freight-in for retailers.
  3. Enter Ending Inventory: Input the total value of inventory remaining unsold at the end of the accounting period. This usually requires a physical inventory count or data from a perpetual inventory system.
  4. Click 'Calculate COGS': The calculator will instantly compute the "Goods Available for Sale" and your final "Cost of Goods Sold."

Reading the Results:

  • Goods Available for Sale: This intermediate value shows the total cost of inventory that was potentially available to sell.
  • Cost of Goods Sold (COGS): This is your primary result – the direct cost associated with the goods you actually sold.
  • Gross Profit: If you know your Sales Revenue, you can mentally subtract COGS to find your Gross Profit. A higher Gross Profit margin (Gross Profit / Sales Revenue) generally indicates better efficiency.

Decision-Making Guidance:

Use the COGS figure to:

  • Set Pricing: Ensure your selling prices adequately cover COGS and contribute to overall profitability.
  • Manage Inventory: Analyze trends in COGS relative to sales. High COGS might indicate rising material costs or inefficiencies. Low COGS relative to sales is generally positive.
  • Assess Profitability: Compare COGS to revenue to understand your gross profit margins. This is a key metric for financial health. For insights into broader financial planning, consider exploring financial planning tools.

Key Factors That Affect Cost of Goods Sold Results

Several factors can significantly influence your Cost of Goods Sold calculations and the resulting profitability. Understanding these is key to effective cost management.

  1. Material Costs: Fluctuations in the price of raw materials (e.g., metals, fabrics, agricultural products) directly impact the cost of producing goods. Global supply chain issues, commodity prices, and supplier negotiations all play a role.
  2. Direct Labor Costs: Wages, benefits, and payroll taxes paid to employees directly involved in production or assembly are part of COGS. Minimum wage changes, overtime, and labor availability can affect these costs.
  3. Manufacturing Overhead (for Manufacturers): While indirect, certain overhead costs directly tied to the production facility (like factory utilities, depreciation of manufacturing equipment) are often allocated to COGS under specific accounting principles (e.g., absorption costing).
  4. Inventory Valuation Method: The method used to value inventory (FIFO – First-In, First-Out; LIFO – Last-In, First-Out; Weighted-Average) can significantly alter COGS, especially during periods of changing prices. FIFO generally results in a lower COGS during inflation, while LIFO results in a higher COGS. This choice impacts taxable income.
  5. Purchase Volume and Discounts: Buying inventory in larger quantities often leads to volume discounts from suppliers, potentially lowering the per-unit cost and thus impacting COGS. Negotiating favorable terms is crucial.
  6. Shrinkage and Spoilage: Inventory lost due to theft, damage, or obsolescence reduces the ending inventory value. This effectively increases COGS because the cost of the lost items cannot be recovered through sales. Proper inventory management minimizes this.
  7. Freight-In Costs: The cost of transporting purchased inventory to your business location is typically added to the cost of purchases, thereby increasing COGS.
  8. Currency Exchange Rates: For businesses importing materials or finished goods, fluctuations in exchange rates can significantly alter the cost in your local currency, directly affecting COGS.

Frequently Asked Questions (FAQ)

Q1: What is the difference between COGS and Operating Expenses?

COGS are direct costs tied to producing or acquiring goods sold. Operating Expenses (OpEx) are indirect costs related to running the business, such as rent, marketing, salaries of non-production staff, and utilities. Both are important for profitability, but COGS directly impacts Gross Profit, while OpEx impacts Net Profit.

Q2: Can COGS be negative?

Generally, no. COGS represents a cost. However, in very rare, specific accounting adjustments or inventory write-downs that exceed purchases and beginning inventory, a negative COGS might appear temporarily, but it's usually an anomaly requiring investigation.

Q3: How often should COGS be calculated?

COGS is typically calculated for each accounting period, which could be monthly, quarterly, or annually, depending on the business's reporting needs and accounting methods. Perpetual inventory systems update COGS continuously.

Q4: Does COGS include shipping costs?

Shipping costs paid by the buyer to receive inventory (freight-in) are typically included in the cost of purchases and therefore part of COGS. Shipping costs paid by the seller to deliver goods to customers (freight-out) are usually considered a selling expense or operating expense, not part of COGS.

Q5: How does inventory valuation method affect COGS?

Methods like FIFO, LIFO, and Weighted-Average assign costs to inventory differently. During periods of rising prices (inflation), FIFO assigns the oldest (cheaper) costs to COGS, resulting in lower COGS and higher profit. LIFO assigns the newest (more expensive) costs to COGS, resulting in higher COGS and lower profit (and lower taxes). The choice impacts reported profitability and tax liability.

Q6: What if my ending inventory is higher than goods available for sale?

This scenario is mathematically impossible if calculations are correct. Ending Inventory cannot logically be greater than the sum of Beginning Inventory plus Purchases. If this occurs, it indicates a significant error in data entry, inventory counting, or calculation.

Q7: How can I reduce my COGS?

Reducing COGS involves strategies like negotiating better prices with suppliers, finding cheaper raw materials (without sacrificing quality), improving production efficiency to reduce labor and waste, optimizing inventory levels to minimize holding costs and spoilage, and potentially exploring alternative suppliers or manufacturing locations.

Q8: Is COGS the same for service businesses?

No. Service businesses typically do not have physical goods to sell, so they do not calculate COGS in the traditional sense. Instead, they track "Cost of Services," which includes direct labor and materials consumed in providing the service.

var beginningInventoryInput = document.getElementById('beginningInventory'); var purchasesInput = document.getElementById('purchases'); var endingInventoryInput = document.getElementById('endingInventory'); var beginningInventoryError = document.getElementById('beginningInventoryError'); var purchasesError = document.getElementById('purchasesError'); var endingInventoryError = document.getElementById('endingInventoryError'); var goodsAvailableForSaleSpan = document.getElementById('goodsAvailableForSale'); var cogsResultSpan = document.getElementById('cogsResult'); var grossProfitSpan = document.getElementById('grossProfit'); var tableBeginningInventory = document.getElementById('tableBeginningInventory'); var tablePurchases = document.getElementById('tablePurchases'); var tableGoodsAvailableForSale = document.getElementById('tableGoodsAvailableForSale'); var tableEndingInventory = document.getElementById('tableEndingInventory'); var tableCogsResult = document.getElementById('tableCogsResult'); var inventoryChart; var chartContext; function validateInput(inputElement, errorElement, minValue = 0) { var value = parseFloat(inputElement.value); var error = ""; if (isNaN(value)) { error = "Please enter a valid number."; inputElement.style.borderColor = 'red'; } else if (value < minValue) { error = "Value cannot be negative."; inputElement.style.borderColor = 'red'; } else { inputElement.style.borderColor = '#ddd'; } errorElement.textContent = error; return error === ""; } function calculateCOGS() { var isValid = true; isValid &= validateInput(beginningInventoryInput, beginningInventoryError); isValid &= validateInput(purchasesInput, purchasesError); isValid &= validateInput(endingInventoryInput, endingInventoryError); if (!isValid) { clearResults(); return; } var beginningInventory = parseFloat(beginningInventoryInput.value); var purchases = parseFloat(purchasesInput.value); var endingInventory = parseFloat(endingInventoryInput.value); var goodsAvailableForSale = beginningInventory + purchases; var cogs = goodsAvailableForSale – endingInventory; // Ensure COGS is not negative due to potential data entry issues or unusual scenarios if (cogs < 0) { cogs = 0; // Or handle as an error/warning if preferred endingInventoryError.textContent = "Ending inventory value seems too high relative to beginning inventory and purchases."; endingInventoryInput.style.borderColor = 'orange'; } else { endingInventoryInput.style.borderColor = '#ddd'; // Reset if it was previously orange endingInventoryError.textContent = ""; } goodsAvailableForSaleSpan.textContent = formatCurrency(goodsAvailableForSale); cogsResultSpan.textContent = formatCurrency(cogs); cogsResultSpan.style.color = 'white'; // Ensure main result is visible // Placeholder for Gross Profit – requires Sales Revenue input // For now, display a message or zero grossProfitSpan.textContent = "Enter Sales Revenue to calculate"; grossProfitSpan.style.color = 'white'; // Update table tableBeginningInventory.textContent = formatCurrency(beginningInventory); tablePurchases.textContent = formatCurrency(purchases); tableGoodsAvailableForSale.textContent = formatCurrency(goodsAvailableForSale); tableEndingInventory.textContent = formatCurrency(endingInventory); tableCogsResult.textContent = formatCurrency(cogs); updateChart(beginningInventory, purchases, endingInventory, goodsAvailableForSale, cogs); } function formatCurrency(amount) { if (isNaN(amount)) return "–"; return "$" + amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,'); } function clearResults() { goodsAvailableForSaleSpan.textContent = "–"; cogsResultSpan.textContent = "–"; grossProfitSpan.textContent = "–"; tableBeginningInventory.textContent = "–"; tablePurchases.textContent = "–"; tableGoodsAvailableForSale.textContent = "–"; tableEndingInventory.textContent = "–"; tableCogsResult.textContent = "–"; if (chartContext) { chartContext.clearRect(0, 0, chartContext.canvas.width, chartContext.canvas.height); } } function resetCalculator() { beginningInventoryInput.value = "10000"; purchasesInput.value = "50000"; endingInventoryInput.value = "15000"; beginningInventoryError.textContent = ""; purchasesError.textContent = ""; endingInventoryError.textContent = ""; beginningInventoryInput.style.borderColor = '#ddd'; purchasesInput.style.borderColor = '#ddd'; endingInventoryInput.style.borderColor = '#ddd'; calculateCOGS(); } function copyResults() { var resultsText = "Cost of Goods Sold Calculation:\n\n"; resultsText += "Beginning Inventory: " + formatCurrency(parseFloat(beginningInventoryInput.value)) + "\n"; resultsText += "Purchases: " + formatCurrency(parseFloat(purchasesInput.value)) + "\n"; resultsText += "Ending Inventory: " + formatCurrency(parseFloat(endingInventoryInput.value)) + "\n"; resultsText += "————————————\n"; resultsText += "Goods Available for Sale: " + goodsAvailableForSaleSpan.textContent + "\n"; resultsText += "Cost of Goods Sold (COGS): " + cogsResultSpan.textContent + "\n"; resultsText += "Formula: COGS = Beginning Inventory + Purchases – Ending Inventory\n"; try { var textArea = document.createElement("textarea"); textArea.value = resultsText; document.body.appendChild(textArea); textArea.select(); document.execCommand("copy"); document.body.removeChild(textArea); alert("Results copied to clipboard!"); } catch (err) { alert("Failed to copy results. Please copy manually."); } } function updateChart(beginInv, purchases, endInv, goodsAvail, cogs) { var canvas = document.getElementById('inventoryChart'); if (!chartContext) { chartContext = canvas.getContext('2d'); } // Clear previous drawing chartContext.clearRect(0, 0, canvas.width, canvas.height); var chartHeight = canvas.height – 40; // Leave space for labels var chartWidth = canvas.width – 60; // Leave space for labels var maxValue = Math.max(beginInv, purchases, endInv, goodsAvail, cogs); if (maxValue === 0) maxValue = 1; // Avoid division by zero var scaleY = chartHeight / maxValue; // Draw axes chartContext.strokeStyle = '#ccc'; chartContext.lineWidth = 1; chartContext.beginPath(); chartContext.moveTo(40, chartHeight + 20); // Y-axis origin chartContext.lineTo(40, 20); // Y-axis top chartContext.lineTo(chartWidth + 40, 20); // X-axis right chartContext.stroke(); // Draw labels chartContext.fillStyle = '#333'; chartContext.font = '10px Arial'; chartContext.textAlign = 'center'; chartContext.fillText('0', 40, chartHeight + 35); chartContext.fillText(formatCurrency(maxValue), chartWidth + 40, 20); // Draw bars var barWidth = chartWidth / 4; // Divide space for 4 main bars var spacing = barWidth * 0.2; barWidth -= spacing; // Bar 1: Beginning Inventory var x1 = 40 + spacing; var h1 = beginInv * scaleY; chartContext.fillStyle = 'rgba(0, 74, 153, 0.7)'; // Primary color chartContext.fillRect(x1, chartHeight + 20 – h1, barWidth, h1); chartContext.fillText('Begin Inv', x1 + barWidth / 2, chartHeight + 35); // Bar 2: Purchases var x2 = x1 + barWidth + spacing; var h2 = purchases * scaleY; chartContext.fillStyle = 'rgba(40, 167, 69, 0.7)'; // Success color chartContext.fillRect(x2, chartHeight + 20 – h2, barWidth, h2); chartContext.fillText('Purchases', x2 + barWidth / 2, chartHeight + 35); // Bar 3: Goods Available for Sale (Represents total inflow) var x3 = x2 + barWidth + spacing; var h3 = goodsAvail * scaleY; chartContext.fillStyle = 'rgba(108, 117, 125, 0.7)'; // Secondary color chartContext.fillRect(x3, chartHeight + 20 – h3, barWidth, h3); chartContext.fillText('Available', x3 + barWidth / 2, chartHeight + 35); // Bar 4: COGS (Represents outflow) var x4 = x3 + barWidth + spacing; var h4 = cogs * scaleY; chartContext.fillStyle = 'rgba(255, 193, 7, 0.7)'; // Warning color (for outflow) chartContext.fillRect(x4, chartHeight + 20 – h4, barWidth, h4); chartContext.fillText('COGS', x4 + barWidth / 2, chartHeight + 35); // Draw Ending Inventory as a line or smaller bar for context chartContext.beginPath(); chartContext.moveTo(40, chartHeight + 20 – (endInv * scaleY)); chartContext.lineTo(chartWidth + 40, chartHeight + 20 – (endInv * scaleY)); chartContext.strokeStyle = 'rgba(0,0,0,0.5)'; chartContext.lineWidth = 2; chartContext.setLineDash([5, 3]); // Dashed line chartContext.stroke(); chartContext.setLineDash([]); // Reset line dash chartContext.fillText('End Inv: ' + formatCurrency(endInv), 40, chartHeight + 20 – (endInv * scaleY) – 5); // Add legend chartContext.textAlign = 'left'; chartContext.fillStyle = '#333'; chartContext.fillText('Inventory Components', 50, 25); chartContext.fillStyle = 'rgba(0, 74, 153, 0.7)'; chartContext.fillRect(180, 15, 15, 10); chartContext.fillText('Beginning Inv.', 200, 25); chartContext.fillStyle = 'rgba(40, 167, 69, 0.7)'; chartContext.fillRect(280, 15, 15, 10); chartContext.fillText('Purchases', 300, 25); chartContext.fillStyle = 'rgba(108, 117, 125, 0.7)'; chartContext.fillRect(370, 15, 15, 10); chartContext.fillText('Available', 390, 25); chartContext.fillStyle = 'rgba(255, 193, 7, 0.7)'; chartContext.fillRect(450, 15, 15, 10); chartContext.fillText('COGS', 470, 25); chartContext.strokeStyle = 'rgba(0,0,0,0.5)'; chartContext.setLineDash([5, 3]); chartContext.beginPath(); chartContext.moveTo(550, 20); chartContext.lineTo(565, 20); chartContext.stroke(); chartContext.setLineDash([]); chartContext.fillText('Ending Inv. Level', 570, 25); } // Initial calculation on load window.onload = function() { var canvas = document.getElementById('inventoryChart'); canvas.width = 600; // Set a default width canvas.height = 300; // Set a default height chartContext = canvas.getContext('2d'); calculateCOGS(); }; // Add event listeners for real-time updates beginningInventoryInput.addEventListener('input', calculateCOGS); purchasesInput.addEventListener('input', calculateCOGS); endingInventoryInput.addEventListener('input', calculateCOGS);

Leave a Comment