Skip to content

Commit

Permalink
Merge pull request #133 from rruckley/PricingTrait-70
Browse files Browse the repository at this point in the history
Common Pricing Module
  • Loading branch information
rruckley authored Jun 6, 2024
2 parents cd930d5 + 716ec0c commit aae793a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 85 deletions.
3 changes: 2 additions & 1 deletion examples/create_quote.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Create Quote Example
use tmflib::tmf648::{quote::Quote, quote_item::QuoteItem, quote_price::{Price,QuotePrice}};
use tmflib::tmf648::{quote::Quote, quote_item::QuoteItem, quote_price::QuotePrice};
use tmflib::common::price::Price;

fn main() {
// Create a quote using various components
Expand Down
3 changes: 2 additions & 1 deletion examples/create_quote_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
use tmflib::tmf648::{
quote::{Quote,QuoteEventType},
quote_item::QuoteItem,
quote_price::{Price,QuotePrice}
quote_price::QuotePrice
};
use tmflib::common::event::EventPayload;
use tmflib::common::price::Price;

fn main() {
// First create a quote item
Expand Down
3 changes: 2 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ const MOD_PATH: &str = "common";

pub mod attachment;
pub mod contact;
pub mod note;
pub mod event;
pub mod money;
pub mod note;
pub mod price;
pub mod related_party;
pub mod related_place;
pub mod tax_item;
82 changes: 82 additions & 0 deletions src/common/price.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//! Price Module
use serde::{Deserialize, Serialize};
use super::money::Money;

/// Default tax rate for Australian market.
const AUS_TAX_RATE : f32 = 0.10;
const AUS_CURRENCY : &str = "AUD";

/// Common Pricing structure
#[derive(Clone, Debug, Default, Deserialize,PartialEq, Serialize)]
pub struct Price {
///
pub percentage: f32,
/// Tax Rate
pub tax_rate: f32,
/// Amount excluding taxes
pub duty_free_amount: Money,
/// Amount including taxes
pub tax_included_amount: Money,
}

impl Price {
/// Create a new Price object using a tax inclusive price
pub fn new_inc(inc_price : f32) -> Price {
let mut price = Price {
tax_rate : AUS_TAX_RATE,
..Default::default()
};
price.set_inc_price(inc_price,None);
price
}

/// Create a new Price object using a tax exclusive price
pub fn new_ex(ex_price : f32) -> Price {
let mut price = Price {
tax_rate : AUS_TAX_RATE,
..Default::default()
};
price.set_ex_price(ex_price,None);
let _result = price.tax_included_amount.currency(AUS_CURRENCY);
price
}

fn set_currency(&mut self, currency_code : &str) -> Result<String,String> {
let inc_result = self.tax_included_amount.currency(currency_code)?;
let ex_result = self.duty_free_amount.currency(currency_code)?;
Ok(format!("INC: {}, EX: {}",inc_result,ex_result))
}

/// Set the tax inclusive price
pub fn set_inc_price(&mut self, inc_price : f32, currency_code : Option<&str>) {
self.tax_included_amount.value = inc_price;
self.duty_free_amount.value = inc_price / (1.0 + self.tax_rate);
let currency_code = currency_code.unwrap_or(AUS_CURRENCY);
let _result = self.set_currency(currency_code);
}

/// Set the tax exclusive price
pub fn set_ex_price(&mut self, ex_price : f32, currency_code : Option<&str>) {
self.duty_free_amount.value = ex_price;
self.tax_included_amount.value = ex_price * (1.0+self.tax_rate);
let currency_code = currency_code.unwrap_or(AUS_CURRENCY);
let _result = self.set_currency(currency_code);
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_price_inc() {
let price = Price::new_inc(100.0);
assert_eq!(price.duty_free_amount.value,100.0/(1.0+price.tax_rate));
}

#[test]
fn test_price_ex() {
let price = Price::new_ex(100.0);
assert_eq!(price.tax_included_amount.value,100.0*(1.0+price.tax_rate));
}
}
73 changes: 1 addition & 72 deletions src/tmf648/quote_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,7 @@
use serde::{Deserialize,Serialize};

use crate::tmf620::product_offering_price::ProductOfferingPriceRef;
use crate::common::money::Money;

/// Default tax rate for Australian market.
const AUS_TAX_RATE : f32 = 0.10;
const AUS_CURRENCY : &str = "AUD";

/// Price Structure
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Price {
percentage : f32,
tax_rate: f32,
duty_free_amount : Money,
tax_included_amount : Money,
}

impl Price {
/// Create a new Price object using a tax inclusive price
pub fn new_inc(inc_price : f32) -> Price {
let mut price = Price {
tax_rate : AUS_TAX_RATE,
..Default::default()
};
price.set_inc_price(inc_price,None);
price
}

/// Create a new Price object using a tax exclusive price
pub fn new_ex(ex_price : f32) -> Price {
let mut price = Price {
tax_rate : AUS_TAX_RATE,
..Default::default()
};
price.set_ex_price(ex_price,None);
let _result = price.tax_included_amount.currency(AUS_CURRENCY);
price
}

fn set_currency(&mut self, currency_code : &str) -> Result<String,String> {
let inc_result = self.tax_included_amount.currency(currency_code)?;
let ex_result = self.duty_free_amount.currency(currency_code)?;
Ok(format!("INC: {}, EX: {}",inc_result,ex_result))
}

/// Set the tax inclusive price
pub fn set_inc_price(&mut self, inc_price : f32, currency_code : Option<&str>) {
self.tax_included_amount.value = inc_price;
self.duty_free_amount.value = inc_price / (1.0 + self.tax_rate);
let currency_code = currency_code.unwrap_or(AUS_CURRENCY);
let _result = self.set_currency(currency_code);
}

/// Set the tax exclusive price
pub fn set_ex_price(&mut self, ex_price : f32, currency_code : Option<&str>) {
self.duty_free_amount.value = ex_price;
self.tax_included_amount.value = ex_price * (1.0+self.tax_rate);
let currency_code = currency_code.unwrap_or(AUS_CURRENCY);
let _result = self.set_currency(currency_code);
}
}
use crate::common::price::Price;

/// Quote Price Structure
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -128,18 +69,6 @@ impl QuotePrice {
mod test {
use super::*;

#[test]
fn test_price_inc() {
let price = Price::new_inc(100.0);
assert_eq!(price.duty_free_amount.value,100.0/(1.0+price.tax_rate));
}

#[test]
fn test_price_ex() {
let price = Price::new_ex(100.0);
assert_eq!(price.tax_included_amount.value,100.0*(1.0+price.tax_rate));
}

#[test]
fn test_quote_price_none() {
let quote_price = QuotePrice::new("MyQuotePrice");
Expand Down
12 changes: 2 additions & 10 deletions src/tmf663/shopping_cart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
use serde::{Deserialize, Serialize};

use crate::common::money::Money;
use crate::{HasId, LIB_PATH,HasValidity,TimePeriod};
use tmflib_derive::{HasId,HasValidity};
use crate::common::contact::ContactMedium;
use crate::common::related_party::RelatedParty;
use crate::common::price::Price;


use super::MOD_PATH;
Expand All @@ -23,15 +23,7 @@ pub struct CartPrice {
price_type: String,
recurring_charge_period: String,
unit_of_measure: String,
}

/// Pricing for Shopping Cart
#[derive(Clone, Debug, Default, Deserialize,PartialEq, Serialize)]
pub struct Price {
percentage: f32,
tax_rate: f32,
duty_free_amount: Money,
tax_included_amount: Money,
price: Option<Price>,
}

/// Shopping Cart
Expand Down

0 comments on commit aae793a

Please sign in to comment.