Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Price,QuotePrice to Quote #17

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions examples/create_quote.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
//! Create Quote Example
use tmflib::tmf648::{quote::Quote, quote_item::QuoteItem};
use tmflib::tmf648::{quote::Quote, quote_item::QuoteItem, quote_price::{Price,QuotePrice}};

fn main() {
// Create a quote
let item = QuoteItem::new();
// Create a quote using various components

// First create a quote item
let mut item = QuoteItem::new();
// Create a price for this item
let price = Price::new_ex(100.0);
// Add price to QuotePrice and set period
let quote_price = QuotePrice::new("Subscription").price(price).period("Monthly");
// add QuotePrice to item
item.price(quote_price);
// Create the new Quote
let mut quote = Quote::new();
// Add the item to the quote
let _result = quote.add_quote(item);
let _result = quote.with_external_id(String::from("ExternalId"));
// Set the external Id
let _result = quote.with_external_id(String::from("EXT123"));

// Create a total price for the quote
let total_price = Price::new_ex(3600.0);

// Create QuotePrice object for the total price and set period
let quote_total_price = QuotePrice::new("Total Contract").price(total_price).period("Contract");
// Add QuotePrice to quote
quote.price(quote_total_price);

dbg!(&quote);
}
1 change: 1 addition & 0 deletions src/tmf648/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ const MOD_PATH: &str = "tmf648/v4";

pub mod quote;
pub mod quote_item;
pub mod quote_price;
10 changes: 10 additions & 0 deletions src/tmf648/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use serde::{Deserialize, Serialize};

use super::quote_item::QuoteItem;
use super::MOD_PATH;
use super::quote_price::QuotePrice;
use crate::common::note::Note;
use crate::{LIB_PATH, HasId, CreateTMF};

Expand Down Expand Up @@ -62,6 +63,9 @@ pub struct Quote {
/// Requested Start Date
#[serde(skip_serializing_if = "Option::is_none")]
pub start_date: Option<String>,
/// Total Quote Pricing
#[serde(skip_serializing_if = "Option::is_none")]
pub quote_total_price : Option<Vec<QuotePrice>>,
}

impl Quote {
Expand All @@ -71,6 +75,7 @@ impl Quote {
quote.version = Some(QUOTE_VERS.to_string());
quote.state = Some(QuoteStateType::Accepted);
quote.quote_item = Some(vec![]);
quote.quote_total_price = Some(vec![]);
quote
}

Expand All @@ -85,6 +90,11 @@ impl Quote {
Ok(String::from("Quote Item Added"))
}

/// Add a price entry to this quote
pub fn price(&mut self, price : QuotePrice) {
self.quote_total_price.as_mut().unwrap().push(price);
}

/// Get a description for this quote
pub fn description(&self) -> String {
match &self.description {
Expand Down
11 changes: 11 additions & 0 deletions src/tmf648/quote_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::common::note::Note;
use crate::common::related_party::RelatedParty;
use crate::tmf620::product_specification::ProductSpecificationRef;

use super::quote_price::QuotePrice;

/// Status of product for Quote Item
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
pub enum ProductStatusType {
Expand Down Expand Up @@ -90,6 +92,9 @@ pub struct QuoteItem {
/// Product
#[serde(skip_serializing_if = "Option::is_none")]
pub product : Option<ProductRefOrValue>,
/// Quote Item Pricing
#[serde(skip_serializing_if = "Option::is_none")]
quote_item_price : Option<Vec<QuotePrice>>,
}

impl QuoteItem {
Expand All @@ -99,7 +104,13 @@ impl QuoteItem {
QuoteItem {
id : Some(id),
quantity : 1,
quote_item_price : Some(vec![]),
..Default::default()
}
}

/// Add QuotePrice to this QuoteItem
pub fn price(&mut self, price : QuotePrice) {
self.quote_item_price.as_mut().unwrap().push(price);
}
}
112 changes: 112 additions & 0 deletions src/tmf648/quote_price.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! Quote Price Struct
//!

use serde::{Deserialize,Serialize};

use crate::tmf620::product_offering_price::ProductOfferingPriceRef;

/// Default tax rate for Australian market.
const AUS_TAX_RATE : f32 = 0.10;

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

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);
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);
price
}

/// Set the tax inclusive price
pub fn set_inc_price(&mut self, inc_price : f32) {
self.tax_included_amount = inc_price;
self.duty_free_amount = inc_price / self.tax_rate;
}

/// Set the tax exclusive price
pub fn set_ex_price(&mut self, ex_price : f32) {
self.duty_free_amount = ex_price;
self.tax_included_amount = ex_price * (1.0+self.tax_rate);
}
}

/// Quote Price Structure
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct QuotePrice {
/// Description
#[serde(skip_serializing_if = "Option::is_none")]
pub description : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Name of price entry
pub name : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
price_type : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
recurring_charge_period: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
unit_of_measure : Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Pricing information
pub price : Option<Price>,
#[serde(skip_serializing_if = "Option::is_none")]
product_offering_price : Option<ProductOfferingPriceRef>,
}

impl QuotePrice {
/// Create a new QuotePrice object with a given name
pub fn new(name : &str) -> QuotePrice {
QuotePrice {
name : Some(name.to_owned()),
..Default::default()
}
}
/// Return the price inclusive of Tax
pub fn inc_tax(&self) -> f32 {
match self.price {
Some(p) => p.tax_included_amount,
None => 0.0,
}
}
/// Return the price exclusive of Tax
pub fn ex_tax(&self) -> f32 {
match self.price {
Some(p) => p.duty_free_amount,
None => 0.0,
}
}

/// Add pricing to this QuotePrice
pub fn price(mut self, price : Price) -> QuotePrice {
self.price = Some(price);
self
}

/// Set the period
pub fn period(mut self, period : &str) -> QuotePrice {
self.recurring_charge_period = Some(period.to_owned());
self
}
}