Skip to content

Instantly share code, notes, and snippets.

@gugell
Created April 14, 2019 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugell/913f2d1f76f8bcaaab8615caac343159 to your computer and use it in GitHub Desktop.
Save gugell/913f2d1f76f8bcaaab8615caac343159 to your computer and use it in GitHub Desktop.
var total:Double {
var result = self.subtotal
if let coupon = self.coupon, coupon.isactive{
//percentage
//fixed
let sums = products.filter{$0.discounted}.compactMap({ ($0.price ?? 0.0) * Double($0.qty ?? 0) })
let totalDiscounted = sums.reduce(0.0, +)
if coupon.type == "fixed"{
result -= (totalDiscounted - coupon.value)
}else if coupon.type == "percentage"{
result -= totalDiscounted*coupon.value
}
}
return result
}
let discount:String?{
var result:String = nil
if let coupon = self.coupon , coupon.isactive{
result = coupon.type == "fixed" ? "\(coupon.value) $" : "\(coupon.value) %%"
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment