186 lines
6.2 KiB
QBasic
186 lines
6.2 KiB
QBasic
' Shipping and Cubage Calculator
|
||
' Calculates shipping dimensions, volume, and estimated costs
|
||
|
||
PARAM product_id AS INTEGER DESCRIPTION "Product ID to calculate shipping for"
|
||
PARAM quantity AS INTEGER OPTIONAL DESCRIPTION "Quantity of items (default: 1)"
|
||
PARAM destination_zip AS STRING OPTIONAL DESCRIPTION "Destination ZIP code for shipping estimate"
|
||
|
||
DESCRIPTION "Calculate shipping dimensions, volumetric weight, and estimated shipping cost"
|
||
|
||
' Set defaults
|
||
IF NOT quantity THEN
|
||
quantity = 1
|
||
END IF
|
||
|
||
' Find product by ID
|
||
product = FIND "Products.csv", "ProductID = " + product_id
|
||
|
||
IF NOT product THEN
|
||
TALK "❌ Product not found with ID: " + product_id
|
||
RETURN { "error": "Product not found" }
|
||
END IF
|
||
|
||
' Get product dimensions from extended fields or estimate from quantity per unit
|
||
' Default dimensions in cm (length x width x height)
|
||
dimensions = {}
|
||
|
||
' Parse quantity per unit for dimension hints
|
||
qty_unit = product.QuantityPerUnit
|
||
IF qty_unit THEN
|
||
' Extract numeric values and unit types
|
||
IF CONTAINS(qty_unit, "kg") THEN
|
||
dimensions.estimated_weight = EXTRACT_NUMBER(qty_unit)
|
||
dimensions.weight_unit = "kg"
|
||
ELSE IF CONTAINS(qty_unit, "ml") OR CONTAINS(qty_unit, "cc") THEN
|
||
volume_ml = EXTRACT_NUMBER(qty_unit)
|
||
dimensions.estimated_weight = volume_ml / 1000
|
||
dimensions.weight_unit = "kg"
|
||
ELSE IF CONTAINS(qty_unit, "oz") THEN
|
||
oz_value = EXTRACT_NUMBER(qty_unit)
|
||
dimensions.estimated_weight = oz_value * 0.0283495
|
||
dimensions.weight_unit = "kg"
|
||
ELSE IF CONTAINS(qty_unit, "lb") THEN
|
||
lb_value = EXTRACT_NUMBER(qty_unit)
|
||
dimensions.estimated_weight = lb_value * 0.453592
|
||
dimensions.weight_unit = "kg"
|
||
ELSE
|
||
' Default weight estimate based on category
|
||
dimensions.estimated_weight = 0.5
|
||
dimensions.weight_unit = "kg"
|
||
END IF
|
||
END IF
|
||
|
||
' Use AI to estimate package dimensions if not available
|
||
BEGIN SYSTEM PROMPT
|
||
You are a logistics expert. Estimate realistic package dimensions for shipping.
|
||
Consider the product type, typical packaging, and quantity per unit.
|
||
Return dimensions in centimeters and weight in kilograms.
|
||
END SYSTEM PROMPT
|
||
|
||
estimation_prompt = "Estimate shipping package dimensions for: " + product.ProductName + " (Quantity per unit: " + product.QuantityPerUnit + "). Return JSON: {\"length_cm\": number, \"width_cm\": number, \"height_cm\": number, \"weight_kg\": number, \"package_type\": string}"
|
||
|
||
ai_estimate = ASK estimation_prompt
|
||
package = PARSE JSON ai_estimate
|
||
|
||
' Calculate for requested quantity
|
||
total_packages = CEIL(quantity / EXTRACT_NUMBER(product.QuantityPerUnit))
|
||
IF total_packages < 1 THEN
|
||
total_packages = 1
|
||
END IF
|
||
|
||
' Calculate cubic dimensions
|
||
length_cm = package.length_cm
|
||
width_cm = package.width_cm
|
||
height_cm = package.height_cm
|
||
weight_kg = package.weight_kg * total_packages
|
||
|
||
' Volume in cubic centimeters
|
||
volume_cm3 = length_cm * width_cm * height_cm * total_packages
|
||
|
||
' Volume in cubic meters
|
||
volume_m3 = volume_cm3 / 1000000
|
||
|
||
' Volumetric weight (dimensional weight)
|
||
' Standard divisor: 5000 for international, 6000 for domestic
|
||
volumetric_weight_intl = volume_cm3 / 5000
|
||
volumetric_weight_domestic = volume_cm3 / 6000
|
||
|
||
' Billable weight is the greater of actual and volumetric
|
||
billable_weight_intl = MAX(weight_kg, volumetric_weight_intl)
|
||
billable_weight_domestic = MAX(weight_kg, volumetric_weight_domestic)
|
||
|
||
' Calculate shipping estimates
|
||
shipping_estimates = []
|
||
|
||
' Ground shipping estimate
|
||
ground = {}
|
||
ground.carrier = "Ground"
|
||
ground.billable_weight = billable_weight_domestic
|
||
ground.estimated_days = "5-7 business days"
|
||
ground.base_rate = 5.99
|
||
ground.per_kg_rate = 1.50
|
||
ground.estimated_cost = ground.base_rate + (ground.billable_weight * ground.per_kg_rate)
|
||
PUSH shipping_estimates, ground
|
||
|
||
' Express shipping estimate
|
||
express = {}
|
||
express.carrier = "Express"
|
||
express.billable_weight = billable_weight_domestic
|
||
express.estimated_days = "2-3 business days"
|
||
express.base_rate = 12.99
|
||
express.per_kg_rate = 2.50
|
||
express.estimated_cost = express.base_rate + (express.billable_weight * express.per_kg_rate)
|
||
PUSH shipping_estimates, express
|
||
|
||
' Overnight shipping estimate
|
||
overnight = {}
|
||
overnight.carrier = "Overnight"
|
||
overnight.billable_weight = billable_weight_intl
|
||
overnight.estimated_days = "Next business day"
|
||
overnight.base_rate = 24.99
|
||
overnight.per_kg_rate = 4.00
|
||
overnight.estimated_cost = overnight.base_rate + (overnight.billable_weight * overnight.per_kg_rate)
|
||
PUSH shipping_estimates, overnight
|
||
|
||
' Build response
|
||
result = {}
|
||
result.product_id = product_id
|
||
result.product_name = product.ProductName
|
||
result.quantity = quantity
|
||
result.packages = total_packages
|
||
|
||
result.dimensions = {
|
||
"length_cm": length_cm,
|
||
"width_cm": width_cm,
|
||
"height_cm": height_cm,
|
||
"package_type": package.package_type
|
||
}
|
||
|
||
result.weight = {
|
||
"actual_kg": weight_kg,
|
||
"volumetric_domestic_kg": volumetric_weight_domestic,
|
||
"volumetric_intl_kg": volumetric_weight_intl,
|
||
"billable_domestic_kg": billable_weight_domestic,
|
||
"billable_intl_kg": billable_weight_intl
|
||
}
|
||
|
||
result.volume = {
|
||
"cm3": volume_cm3,
|
||
"m3": volume_m3
|
||
}
|
||
|
||
result.shipping_options = shipping_estimates
|
||
|
||
' Display results
|
||
TALK "📦 **Shipping Calculator Results**"
|
||
TALK ""
|
||
TALK "**Product:** " + product.ProductName
|
||
TALK "**Quantity:** " + quantity + " (" + total_packages + " package(s))"
|
||
TALK ""
|
||
TALK "**Package Dimensions:**"
|
||
TALK " 📏 " + length_cm + " × " + width_cm + " × " + height_cm + " cm"
|
||
TALK " 📦 Volume: " + FORMAT(volume_cm3, "#,##0") + " cm³ (" + FORMAT(volume_m3, "0.000") + " m³)"
|
||
TALK " ⚖️ Type: " + package.package_type
|
||
TALK ""
|
||
TALK "**Weight Analysis:**"
|
||
TALK " Actual Weight: " + FORMAT(weight_kg, "0.00") + " kg"
|
||
TALK " Volumetric (Domestic): " + FORMAT(volumetric_weight_domestic, "0.00") + " kg"
|
||
TALK " Volumetric (Intl): " + FORMAT(volumetric_weight_intl, "0.00") + " kg"
|
||
TALK " **Billable Weight:** " + FORMAT(billable_weight_domestic, "0.00") + " kg"
|
||
TALK ""
|
||
TALK "**Shipping Options:**"
|
||
|
||
FOR EACH option IN shipping_estimates
|
||
TALK " 🚚 **" + option.carrier + "**: $" + FORMAT(option.estimated_cost, "0.00") + " (" + option.estimated_days + ")"
|
||
NEXT
|
||
|
||
' Log calculation for analytics
|
||
LOG "shipping_calculation", {
|
||
"product_id": product_id,
|
||
"quantity": quantity,
|
||
"volume_cm3": volume_cm3,
|
||
"weight_kg": weight_kg,
|
||
"timestamp": NOW()
|
||
}
|
||
|
||
RETURN result
|