amount.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00006 /*
00007   AUTHOR(S): Andreas Huggel (ahu)
00008 
00009   RCS information
00010    $Name: OSB_060808 $
00011    $Revision: 1.41 $
00012 
00013   License
00014    OSB rating and billing library for communication networks
00015    Copyright (C) 2004, 2005, 2006  OSB systems
00016 
00017    This file may be distributed and/or modify under the terms of the
00018    GNU General Public License (GPL) as published by the Free Software
00019    Foundation which is provided in the file LICENSE.GPL included in the
00020    packaging of this file.
00021 
00022    The file is distributed in the hope that it will be useful, but WITHOUT
00023    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025    for more details.
00026 
00027    Holders of a OSB Commercial License may use this file under the terms
00028    and conditions of this commercial license.
00029  */
00030 // ********************************************************* -*- C++ -*-
00031 #ifndef _AMOUNT_H_
00032 #define _AMOUNT_H_
00033 
00034 // *********************************************************************
00035 // included header files
00036 // + standard includes
00037 #include<iosfwd>
00038 #include<vector>
00039 #include<map>
00040 
00041 // + local headers
00042 #include "chargetraits.h"
00043 #include "osbid.h"
00044 #include "decimal.h"
00045 #include "money.h"
00046 
00047 // *********************************************************************
00048 // namespace extensions
00049 namespace OSB_LIB {
00050 
00052     class TaxAmount {
00053     public:
00066         TaxAmount(
00067             const TaxTraits& tt,
00068             const MoneyList& pt,
00069             const Money& td
00070         );
00071 
00073 
00074 
00075         TaxIdVs taxIdVs() const { return traits_.taxIdVs(); }
00077         TaxTraits traits() const { return traits_; }
00079         void preciseTax(MoneyList& ml) const { ml = preciseTax_; }
00081         void tax(MoneyList& ml) const { ml = tax_; }
00083         void taxed(MoneyList& ml) const { ml = taxed_; }
00085         TaxAmount& operator+=(const TaxAmount&);
00086 
00087     private:
00089         TaxTraits traits_;
00091         MoneyList preciseTax_;
00093         MoneyList tax_;
00097         MoneyList taxed_;
00098     };
00099 
00100     typedef std::vector<TaxAmount> TaxAmountList;
00101 
00102     std::ostream& operator<<(std::ostream&, const TaxAmount&);
00103 
00115     class BaseAmount {
00116     public:
00118         BaseAmount() : taxesReady_(false) {}
00125         BaseAmount(
00126             const ChargeTraits& ct,
00127             const Date& dt = Date()
00128         ) : traits_(ct), refDate_(dt), taxesReady_(false) {}
00130         virtual ~BaseAmount() =0;
00132         BaseAmount(const BaseAmount& rhs);
00134         BaseAmount& operator=(const BaseAmount& rhs);
00135 
00137         virtual void precise(MoneyList& ml) const =0;
00139         virtual void rounded(MoneyList& ml) const =0;
00141         void gross(MoneyList& ml) const;
00143         void net(MoneyList& ml) const;
00150         virtual bool accumulateTaxes() =0;
00151         /*
00152          @brief Write amount to a given standard output.
00153 
00154          @param o Write to this output.
00155          @returns Writen output.
00156          */
00157         virtual std::ostream& write(std::ostream& o) const =0;
00159         virtual bool taxesReady() const { return taxesReady_; }
00161         virtual ChargeTraits traits() const { return traits_; }
00163         virtual Date refDate() const { return refDate_; }
00170         virtual void setTraits(const ChargeTraits& ct,
00171                                const Date& dt = Date());
00173         void addTax(const TaxAmount& bta);
00175         void deleteTaxes();
00178         void totalTax(MoneyList& ml) const;
00180         void taxed(MoneyList& ml, TaxIdVs) const;
00182         void setTaxesReady(bool flag =true) { taxesReady_ = flag; }
00184         typedef std::map<TaxIdVs, TaxAmount*> Taxes;
00185         typedef Taxes::const_iterator TaxesCI;
00186         typedef Taxes::iterator TaxesI;
00187 
00188         TaxesCI taxesBegin()          const { return taxes_.begin(); }
00189         TaxesI  taxesBegin()                { return taxes_.begin(); }
00190         TaxesCI taxesEnd()            const { return taxes_.end(); }
00191         TaxesI  taxesEnd()                  { return taxes_.end(); }
00192         TaxesCI taxesFind(TaxIdVs ti) const { return taxes_.find(ti); }
00193 
00194     private:
00196         ChargeTraits traits_;
00197 
00200         Date refDate_;
00201 
00206         bool taxesReady_;
00208         Taxes taxes_;
00209     };
00210 
00211     std::ostream& operator<<(std::ostream&, const BaseAmount&);
00212 
00215     class Amount : public BaseAmount {
00216     public:
00218         Amount() : BaseAmount(), precise_(0) {}
00220         Amount(
00221             const ChargeTraits& ct,
00222             const Money&        p,
00223             const Date&         dt  = Date()
00224         ) : BaseAmount(ct, dt), precise_(p.amount()) {}
00226         Amount(const Amount& rhs)
00227             : BaseAmount(rhs), precise_(rhs.precise_) {}
00229         Amount& operator=(const Amount& rhs);
00231         ~Amount() {}
00233         void precise(MoneyList& ml) const;
00235         void rounded(MoneyList& ml) const;
00237         bool accumulateTaxes() { return taxesReady(); }
00238         Amount& operator+=(const Money&);
00239 
00240         /*
00241          @brief Write amount to a given standard output.
00242 
00243          @param o Write to this output.
00244          @returns Writen output.
00245          */
00246         virtual std::ostream& write(std::ostream&) const;
00247 
00248     private:
00250         Decimal precise_;
00251     };
00252 
00263     class Subtotal : public BaseAmount {
00264     public:
00266         Subtotal() : BaseAmount(), primaryAttribute_(computed) {}
00268         Subtotal(const ChargeTraits& ct, const Date& dt = Date())
00269             : BaseAmount(ct, dt), primaryAttribute_(assigned) {}
00271         Subtotal(const Subtotal&);
00273         Subtotal& operator=(const Subtotal&);
00275         ~Subtotal() {}
00276 
00278         void precise(MoneyList& ml) const {
00279             sum(ml, su_precise);
00280         }
00282         void rounded(MoneyList& ml) const {
00283             sum(ml, su_rounded);
00284         }
00286         bool taxesReady() const;
00288         bool accumulateTaxes();
00289 
00291         void addItem(BaseAmount&);
00292 
00302         enum AttributeType { assigned, computed };
00304         ChargeTraits traits() const
00305             { return traitsByType(primaryAttribute_); }
00307         ChargeTraits traitsByType(AttributeType tt) const
00308             { return tt == assigned ?
00309                   BaseAmount::traits() : computedTraits_; }
00310 
00312         Date refDate() const
00313             { return dateByType(primaryAttribute_); }
00315         Date dateByType(AttributeType dt) const
00316             { return dt == assigned ?
00317                   BaseAmount::refDate() : computedRefDate_; }
00318 
00321         void setTraits(const ChargeTraits& ct,
00322                        const Date& dt = Date())
00323         {
00324             BaseAmount::setTraits(ct, dt);
00325             primaryAttribute_ = assigned;
00326         }
00327 
00328         // shorthands and functions to deal with the amounts
00329         typedef std::vector<BaseAmount*> AmountList;
00330         typedef AmountList::const_iterator AmountListCI;
00331         typedef AmountList::iterator AmountListI;
00332 
00333         AmountListCI amountListBegin() const { return items_.begin(); }
00334         AmountListI  amountListBegin()       { return items_.begin(); }
00335         AmountListCI amountListEnd()   const { return items_.end(); }
00336         AmountListI  amountListEnd()         { return items_.end(); }
00337 
00338         /*
00339          @brief Write amount to a given standard output.
00340 
00341          @param o Write to this output.
00342          @returns Writen output.
00343          */
00344         virtual std::ostream& write(std::ostream&) const;
00345 
00346     private:
00347         enum SumType { su_precise, su_rounded };
00348         void sum(MoneyList& ml, SumType) const;
00349 
00352         AttributeType primaryAttribute_;
00353 
00356         mutable ChargeTraits computedTraits_;
00357         mutable Date computedRefDate_;
00359         AmountList items_;
00360     };
00361 
00362 }                                   // namespace OSB_LIB
00363 #endif                              // #ifndef _AMOUNT_H_

Generated on Sat Sep 2 14:06:31 2006 for OSB Library by  doxygen 1.4.7