pub struct Tax { /* private fields */ }Expand description
Tax calculation and management interface.
Provides tax rate management, exemption handling, and tax calculation for commerce operations.
§Example
use stateset_embedded::{Commerce, TaxAddress, TaxCalculationRequest, TaxLineItem, ProductTaxCategory};
use rust_decimal_macros::dec;
let commerce = Commerce::new("./store.db")?;
// Calculate tax for a transaction
let result = commerce.tax().calculate(TaxCalculationRequest {
line_items: vec![TaxLineItem {
id: "item-1".into(),
quantity: dec!(2),
unit_price: dec!(29.99),
tax_category: ProductTaxCategory::Standard,
..Default::default()
}],
shipping_address: TaxAddress {
country: "US".into(),
state: Some("CA".into()),
postal_code: Some("90210".into()),
..Default::default()
},
..Default::default()
})?;
println!("Subtotal: ${}", result.subtotal);
println!("Tax: ${}", result.total_tax);
println!("Total: ${}", result.total);Implementations§
Source§impl Tax
impl Tax
Sourcepub fn calculate(
&self,
request: TaxCalculationRequest,
) -> Result<TaxCalculationResult>
pub fn calculate( &self, request: TaxCalculationRequest, ) -> Result<TaxCalculationResult>
Calculate tax for a transaction.
Given line items and shipping address, calculates applicable taxes based on jurisdiction rules, product categories, and customer exemptions.
§Example
use rust_decimal_macros::dec;
let result = commerce.tax().calculate(TaxCalculationRequest {
line_items: vec![TaxLineItem {
id: "item-1".into(),
quantity: dec!(2),
unit_price: dec!(29.99),
tax_category: ProductTaxCategory::Standard,
..Default::default()
}],
shipping_address: TaxAddress {
country: "US".into(),
state: Some("CA".into()),
..Default::default()
},
..Default::default()
})?;
println!("Tax breakdown:");
for breakdown in &result.tax_breakdown {
println!(" {}: {}% = ${}", breakdown.rate_name, breakdown.rate * dec!(100), breakdown.tax_amount);
}Sourcepub fn calculate_for_item(
&self,
unit_price: Decimal,
quantity: Decimal,
category: ProductTaxCategory,
shipping_address: &TaxAddress,
) -> Result<Decimal>
pub fn calculate_for_item( &self, unit_price: Decimal, quantity: Decimal, category: ProductTaxCategory, shipping_address: &TaxAddress, ) -> Result<Decimal>
Calculate tax for a single item (convenience method).
§Example
use rust_decimal_macros::dec;
let tax = commerce.tax().calculate_for_item(
dec!(99.99), // unit price
dec!(2), // quantity
ProductTaxCategory::Standard, // category
&TaxAddress {
country: "US".into(),
state: Some("TX".into()),
..Default::default()
},
)?;
println!("Tax: ${}", tax);Sourcepub fn get_effective_rate(
&self,
address: &TaxAddress,
category: ProductTaxCategory,
) -> Result<Decimal>
pub fn get_effective_rate( &self, address: &TaxAddress, category: ProductTaxCategory, ) -> Result<Decimal>
Get the effective tax rate for an address and category.
Returns the combined tax rate that would apply to a standard purchase.
§Example
let rate = commerce.tax().get_effective_rate(
&TaxAddress {
country: "US".into(),
state: Some("CA".into()),
city: Some("Los Angeles".into()),
..Default::default()
},
ProductTaxCategory::Standard,
)?;
println!("Effective tax rate: {}%", rate * rust_decimal_macros::dec!(100));Sourcepub fn get_jurisdiction(&self, id: Uuid) -> Result<Option<TaxJurisdiction>>
pub fn get_jurisdiction(&self, id: Uuid) -> Result<Option<TaxJurisdiction>>
Get a tax jurisdiction by ID.
Sourcepub fn get_jurisdiction_by_code(
&self,
code: &str,
) -> Result<Option<TaxJurisdiction>>
pub fn get_jurisdiction_by_code( &self, code: &str, ) -> Result<Option<TaxJurisdiction>>
Sourcepub fn list_jurisdictions(
&self,
filter: TaxJurisdictionFilter,
) -> Result<Vec<TaxJurisdiction>>
pub fn list_jurisdictions( &self, filter: TaxJurisdictionFilter, ) -> Result<Vec<TaxJurisdiction>>
List tax jurisdictions with optional filtering.
§Example
// List all US state jurisdictions
let states = commerce.tax().list_jurisdictions(TaxJurisdictionFilter {
country_code: Some("US".into()),
level: Some(JurisdictionLevel::State),
active_only: true,
..Default::default()
})?;
for state in states {
println!("{}: {}", state.code, state.name);
}Sourcepub fn create_jurisdiction(
&self,
input: CreateTaxJurisdiction,
) -> Result<TaxJurisdiction>
pub fn create_jurisdiction( &self, input: CreateTaxJurisdiction, ) -> Result<TaxJurisdiction>
Create a new tax jurisdiction.
§Example
let jurisdiction = commerce.tax().create_jurisdiction(CreateTaxJurisdiction {
name: "Los Angeles".into(),
code: "US-CA-LA".into(),
level: JurisdictionLevel::City,
country_code: "US".into(),
state_code: Some("CA".into()),
city: Some("Los Angeles".into()),
..Default::default()
})?;Sourcepub fn list_rates(&self, filter: TaxRateFilter) -> Result<Vec<TaxRate>>
pub fn list_rates(&self, filter: TaxRateFilter) -> Result<Vec<TaxRate>>
List tax rates with optional filtering.
§Example
// Get all active rates for a jurisdiction
let rates = commerce.tax().list_rates(TaxRateFilter {
jurisdiction_id: Some(jurisdiction_id),
active_only: true,
..Default::default()
})?;
for rate in rates {
println!("{}: {}%", rate.name, rate.rate * rust_decimal_macros::dec!(100));
}Sourcepub fn create_rate(&self, input: CreateTaxRate) -> Result<TaxRate>
pub fn create_rate(&self, input: CreateTaxRate) -> Result<TaxRate>
Create a new tax rate.
§Example
use rust_decimal_macros::dec;
let rate = commerce.tax().create_rate(CreateTaxRate {
jurisdiction_id,
tax_type: TaxType::SalesTax,
product_category: ProductTaxCategory::Standard,
rate: dec!(0.0825), // 8.25%
name: "City Sales Tax".into(),
effective_from: chrono::Utc::now().date_naive(),
..Default::default()
})?;Sourcepub fn get_rates_for_address(
&self,
address: &TaxAddress,
category: ProductTaxCategory,
date: NaiveDate,
) -> Result<Vec<TaxRate>>
pub fn get_rates_for_address( &self, address: &TaxAddress, category: ProductTaxCategory, date: NaiveDate, ) -> Result<Vec<TaxRate>>
Get rates for a specific address and product category.
Returns all applicable tax rates sorted by priority.
Sourcepub fn get_exemption(&self, id: Uuid) -> Result<Option<TaxExemption>>
pub fn get_exemption(&self, id: Uuid) -> Result<Option<TaxExemption>>
Get an exemption by ID.
Sourcepub fn get_customer_exemptions(
&self,
customer_id: Uuid,
) -> Result<Vec<TaxExemption>>
pub fn get_customer_exemptions( &self, customer_id: Uuid, ) -> Result<Vec<TaxExemption>>
Sourcepub fn create_exemption(
&self,
input: CreateTaxExemption,
) -> Result<TaxExemption>
pub fn create_exemption( &self, input: CreateTaxExemption, ) -> Result<TaxExemption>
Create a tax exemption for a customer.
§Example
let exemption = commerce.tax().create_exemption(CreateTaxExemption {
customer_id,
exemption_type: ExemptionType::Resale,
certificate_number: Some("RS-12345".into()),
issuing_authority: Some("California".into()),
effective_from: chrono::Utc::now().date_naive(),
expires_at: Some(chrono::Utc::now().date_naive() + chrono::Duration::days(365)),
..Default::default()
})?;Sourcepub fn customer_is_exempt(&self, customer_id: Uuid) -> Result<bool>
pub fn customer_is_exempt(&self, customer_id: Uuid) -> Result<bool>
Sourcepub fn get_settings(&self) -> Result<TaxSettings>
pub fn get_settings(&self) -> Result<TaxSettings>
Sourcepub fn update_settings(&self, settings: TaxSettings) -> Result<TaxSettings>
pub fn update_settings(&self, settings: TaxSettings) -> Result<TaxSettings>
Sourcepub fn set_enabled(&self, enabled: bool) -> Result<TaxSettings>
pub fn set_enabled(&self, enabled: bool) -> Result<TaxSettings>
Enable or disable tax calculation.
Sourcepub fn is_enabled(&self) -> Result<bool>
pub fn is_enabled(&self) -> Result<bool>
Check if tax calculation is enabled.
Sourcepub fn get_us_state_info(state_code: &str) -> Option<UsStateTaxInfo>
pub fn get_us_state_info(state_code: &str) -> Option<UsStateTaxInfo>
Get US state tax information.
Returns pre-configured tax information for a US state.
§Example
if let Some(info) = stateset_core::get_us_state_tax_info("CA") {
println!("California state rate: {}%", info.state_rate * rust_decimal_macros::dec!(100));
println!("Has local taxes: {}", info.has_local_taxes);
println!("Tax shipping: {}", info.tax_shipping);
}Sourcepub fn get_eu_vat_info(country_code: &str) -> Option<EuVatInfo>
pub fn get_eu_vat_info(country_code: &str) -> Option<EuVatInfo>
Get EU VAT information.
Returns pre-configured VAT rates for an EU country.
§Example
if let Some(info) = stateset_core::get_eu_vat_info("DE") {
println!("Germany standard VAT: {}%", info.standard_rate * rust_decimal_macros::dec!(100));
if let Some(reduced) = info.reduced_rate {
println!("Reduced rate: {}%", reduced * rust_decimal_macros::dec!(100));
}
}Sourcepub fn get_canadian_tax_info(province_code: &str) -> Option<CanadianTaxInfo>
pub fn get_canadian_tax_info(province_code: &str) -> Option<CanadianTaxInfo>
Get Canadian tax information.
Returns pre-configured tax rates for a Canadian province.
§Example
if let Some(info) = stateset_core::get_canadian_tax_info("ON") {
println!("Ontario total rate: {}%", info.total_rate * rust_decimal_macros::dec!(100));
if let Some(hst) = info.hst_rate {
println!("HST: {}%", hst * rust_decimal_macros::dec!(100));
}
}Sourcepub fn is_eu_country(country_code: &str) -> bool
pub fn is_eu_country(country_code: &str) -> bool
Check if a country is in the EU.