balancesheet.h

Go to the documentation of this file.
00001 // OSB library ********************************************* -*- C++ -*-
00016 /*
00017   AUTHOR(S): Vipul Gupta (vg)
00018 
00019   RCS information
00020    $Name: OSB_060808 $
00021    $Revision: 1.115 $
00022 
00023   License
00024    OSB rating and billing library for communication networks
00025    Copyright (C) 2004, 2005, 2006  OSB systems
00026 
00027    This file may be distributed and/or modify under the terms of the
00028    GNU General Public License (GPL) as published by the Free Software
00029    Foundation which is provided in the file LICENSE.GPL included in the
00030    packaging of this file.
00031 
00032    The file is distributed in the hope that it will be useful, but WITHOUT
00033    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00034    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00035    for more details.
00036 
00037    Holders of a OSB Commercial License may use this file under the terms
00038    and conditions of this commercial license.
00039 */
00040 #ifndef _BALANCESHEET_H_
00041 #define _BALANCESHEET_H_
00042 
00043 // *********************************************************************
00044 // included header files
00045 // + standard includes
00046 #include <iosfwd>
00047 #include <vector>
00048 #include <list>
00049 
00050 // + local headers
00051 #include "cdrlist.h"
00052 #include "osbid.h"
00053 #include "contract.h"
00054 #include "billcycle.h"
00055 #include "osbunit.h"
00056 #include "chargetraits.h"
00057 
00058 // + class declarations
00059 namespace OSB_DB {
00060     class  Session;
00061     struct BalanceSheetGw;
00062 }
00063 
00064 namespace OSB_LIB {
00065     class Asn1Writer;
00066     class CdrIo;
00067     class ChargeDetail;
00068     class SumCdrList;
00069     class Udr;
00070 }
00071 
00072 // *********************************************************************
00073 // namespace extensions
00074 namespace OSB_LIB {
00075 
00076 // *********************************************************************
00077 // forward declarations
00078     class BsPage;
00079     class BalanceSheet;
00080 
00081 // *********************************************************************
00082 // class definitions
00083 
00084     // *****************************************************************
00085     // struct SubsCharge
00095     struct SubsCharge {
00097         SubsCharge(
00098             const PersProduct::Oid& persId,
00099             const ProductNode::Oid& nodeId
00100         ) : persProductId(persId), productNodeId(nodeId),
00101             periodLength(0), numDays(-1), daysCharged(-1) {}
00102 
00104         typedef std::pair<Date, Date> Period;
00105 
00107         PersProduct::Oid persProductId;
00109         ProductNode::Oid productNodeId;
00110 
00112 
00113 
00114         Decimal baseCharge;
00116         size_t periodLength;
00117         // Period unit.
00118         Unit::Oid unitId;
00120         long numDays;
00122 
00124 
00125 
00126         Decimal charge;
00128         ChargeTraits::Oid ctId;
00130         Status status;
00132         Decimal baseFractions;
00134         long daysCharged;
00136         std::vector<Period> periods;
00138     };
00139 
00141     typedef std::vector<SubsCharge> SubsCharges;
00143     typedef SubsCharges::iterator SubsChargesI;
00144 
00145     // *****************************************************************
00146     // struct AdvanceCharge
00160     struct AdvanceCharge {
00162         typedef Id<AdvanceCharge> Oid;
00163 
00165         Oid oid;
00167         Id<BsPage> pageId;
00169         PersProduct::Oid persProductId;
00171         ProductNode::Oid productNodeId;
00173         Decimal amount;
00175         ChargeTraits::Oid chargeTraitsId;
00177         Date periodStart;
00179         Date periodEnd;
00181         DateTime assigned;
00183         std::string remark;
00184 
00186         AdvanceCharge() {}
00187     };
00188 
00190     typedef std::vector<AdvanceCharge> AdvanceCharges;
00191 
00192     // *****************************************************************
00193     // struct OnetimeCharge
00202     struct OnetimeCharge {
00204         friend class BsPage;
00205 
00207         typedef Id<OnetimeCharge> Oid;
00208 
00210         Oid oid;
00212         Id<BsPage> pageId;
00214         PersProduct::Oid persProductId;
00216         ProductNode::Oid productNodeId;
00218         StatusChange::Oid statusChangeId;
00220         Money charge;
00222         ChargeTraits::Oid chargeTraitsId;
00224         DateTime assigned;
00226         std::string remark;
00228         EditStatus::Status editStatus() const
00229         {
00230             return status_.status();
00231         }
00232     private:
00234         EditStatus status_;
00235     };
00236 
00238     typedef std::vector<OnetimeCharge> OnetimeCharges;
00239 
00240     // *****************************************************************
00241     // class BsTotal
00252     class BsTotal {
00256         friend class BsPage;
00257 
00262         friend struct OSB_DB::BalanceSheetGw;
00263 
00264     public:
00277         struct SubTotal {
00279             Currency::Oid currency_;
00281             Decimal curUsg_;
00283             Decimal curOtc_;
00285             Decimal curAdv_;
00287             Decimal deltaUsg_;
00289             Decimal deltaOtc_;
00291             Decimal deltaAdv_;
00292         };
00293 
00295         typedef std::map<Currency::Oid, SubTotal> List;
00296 
00297     public:
00303 
00304         const List& list() const { return list_; }
00306         List::size_type size() const { return list_.size(); }
00308         List::const_iterator begin() const { return list_.begin(); }
00310         List::const_iterator end() const { return list_.end(); }
00312 
00314         BsTotal& operator+=(const BsTotal& rhs);
00315 
00316     private:
00318 
00319 
00326         SubTotal& operator[](const Currency::Oid& cc);
00328         void clear() { list_.clear(); }
00330 
00339         void resetCurrentCharges();
00340 
00346         void add(const std::vector<ChargeDetail>& cd);
00347 
00356         void substractCurrentUsage();
00357 
00358     private:
00360         List list_;
00361     };                                  // class BsTotal
00362 
00363     // *****************************************************************
00364     // class BsPage
00384     class BsPage {
00389         friend struct OSB_DB::BalanceSheetGw;
00390 
00399         friend class BalanceSheet;
00400 
00401     public:
00403         typedef Id<BsPage> Oid;
00404 
00405         // class specific exceptions: defined and documented below
00406         class NotFound;
00407         class ChargesAssigned;
00408         class PageClosed;
00409         class NoEndDate;
00410         class CdrRecoveryNeeded;
00411         class SetRecoveryFailed;
00412         class UpdateUsageFailed;
00413         class CommitCdrFailed;
00414 
00415     public:
00420         enum Status {
00422             bpUnknown       = 0,
00424             bpNormal        = 1,
00426             bpRerate        = 2,
00428             bpDontBill      = 4,
00429 
00431             bpBillingFailed = 3
00432         };
00433 
00440         enum CdrFileStatus {
00442             fsUnchanged,
00449             fsPending,
00456             fsDbCommitted
00457         };
00458 
00459     public:
00467         static long toLong(
00468             Status status
00469         );
00470 
00478         static Status toStatus(
00479             long status
00480         );
00481 
00490         explicit BsPage(const Oid& oid = Oid());
00491 
00501         BsPage(
00502             const Contract::Oid&  coId,
00503             const Billcycle::Oid& bcId
00504         );
00505 
00506     private:
00513         void initialize();
00514 
00515     public:
00517         BsPage(const BsPage& rhs);
00519         BsPage& operator=(const BsPage& rhs);
00521         ~BsPage();
00522 
00524 
00525 
00526         const Oid& oid() const { return oid_; }
00528         const Contract::Oid& coId() const { return coId_; }
00530         const Date& startDate() const { return startDate_; }
00532         const Date endDate() const
00533         {
00534             if (Date::max() == endDate_) return Date();
00535             return endDate_;
00536         }
00538         const Billcycle::Oid& billcycleId() const
00539         {
00540             return billcycleId_;
00541         }
00543         const DateTime& opened()  const { return opened_; }
00545         const DateTime& closed()  const { return closed_; }
00547         bool isOpen() const { return closed_.notSet(); }
00549         bool justBilled() const { return justBilled_; }
00551         bool rerating() const { return rerating_; }
00553         const DateTime& lastCharged() const { return lastCharged_; }
00555         const Status& status() const { return status_; }
00561         long recoveryId() const { return recoveryId_; }
00563         const BsTotal& charges() const { return charges_; }
00565         const SubsCharges& subsCharges() const { return subsCharges_; }
00567         const AdvanceCharges& advanceCharges() const
00568         {
00569             return advanceCharges_;
00570         }
00572         const OnetimeCharges& onetimeCharges() const
00573         {
00574             return onetimeCharges_;
00575         }
00581         const SumCdrList* sumCdrList() const;
00587         const CdrList& cdrList() const;
00589 
00597         bool usageModified() const;
00598 
00606         void setEndDate(const Date& endDate);
00607 
00613         void setBilled() { justBilled_ = true; }
00614 
00621         void setRerating();
00622 
00624         OnetimeCharges& getOnetimeCharges() { return onetimeCharges_; }
00625 
00634         SubsCharges& getSubsCharges() { return subsCharges_; }
00635 
00642         bool setStatus(
00643             const OSB_DB::Session& session,
00644             const Status&          status
00645         );
00646 
00648 
00649 
00659         bool readInfo(const OSB_DB::Session& session);
00660 
00677         bool readAll(
00678             const OSB_DB::Session& session,
00679                   bool             withCdr
00680         );
00681 
00696         void readCharges(
00697             const OSB_DB::Session& session,
00698                   bool             withCdr
00699         );
00700 
00731         void readUsageCharges(
00732             const OSB_DB::Session& session,
00733                   bool             withCdr
00734         );
00735 
00739         void readCdrs();
00740 
00748         void readCdrs(CdrList& dest);
00749 
00758         void readOnetimeCharges(
00759             const OSB_DB::Session& session
00760         );
00761 
00770         void readAdvanceCharges(
00771             const OSB_DB::Session& session
00772         );
00774 
00775     public:
00787         void addCdr(const Cdr& cdr);
00788 
00789     private:
00799         void updateUsage(
00800             const OSB_DB::Session& session
00801         );
00802 
00820         void writeCdrs(
00821             long             recoveryId,
00822             OSB_DB::Session& session,
00823             Asn1Writer&      writer
00824         );
00825 
00836         void clearRecoveryId(
00837             const OSB_DB::Session& session
00838         );
00839 
00851         void commitCdrs();
00852 
00862         void removeUsage(const OSB_DB::Session& session);
00863 
00889         bool restoreCdrs(OSB_DB::Session& session);
00890 
00891     private:
00902         bool restoreCdrFiles(OSB_DB::Session& session);
00903 
00904     public:
00911         const OnetimeCharge* findOtc(const OnetimeCharge::Oid& otcId);
00918         void addOtc(const OnetimeCharge& otc);
00929         bool delOtc(const OnetimeCharge::Oid& otcId);
00930     private:
00937         bool saveOtCharges(const OSB_DB::Session& session);
00938 
00939     public:
00941         void addAdvCharge(const Money& charge);
00942     private:
00947         bool saveAdvCharges(const OSB_DB::Session& session);
00948 
00949     private:
00965         bool insert(const OSB_DB::Session& session);
00966 
00974         bool updLastCharged(const OSB_DB::Session& session);
00975 
00986         void close(
00987             const OSB_DB::Session& session
00988         );
00989 
01005         bool remove(OSB_DB::Session& session);
01006 
01007     private:
01009         Oid oid_;
01011         Contract::Oid coId_;
01013         Date startDate_;
01022         Date endDate_;
01024         Billcycle::Oid billcycleId_;
01026         DateTime opened_;
01028         DateTime closed_;
01030         bool justBilled_;
01032         bool rerating_;
01036         DateTime lastCharged_;
01037 
01039         Status status_;
01040 
01050         bool usageChanged_;
01051 
01053         CdrFileStatus cdrFileStatus_;
01067         long recoveryId_;
01068 
01070         BsTotal charges_;
01072         SumCdrList* sumCdrs_;
01074         CdrList cdrs_;
01075 
01081         mutable CdrIo* cdrIo_;
01082 
01083     private:
01085 
01086 
01087         SubsCharges subsCharges_;
01089         AdvanceCharges advanceCharges_;
01091         OnetimeCharges onetimeCharges_;
01093 
01094     private:
01096         OnetimeCharge* find(const OnetimeCharge::Oid& otcId);
01102         CdrIo& cdrIo() const;
01103     };                                  // class BsPage
01104 
01105     // *****************************************************************
01106     // exceptions of BsPage
01110     class BsPage::NotFound : public OsbException {
01111     public:
01113         static const ErrorNo errNo = 588;
01115         NotFound(const BsPage& page);
01116     };
01117 
01122     class BsPage::ChargesAssigned : public OsbException {
01123     public:
01125         static const ErrorNo errNo = 589;
01127         ChargesAssigned(const BsPage& page);
01128     };
01129 
01137     class BsPage::PageClosed : public OsbException {
01138     public:
01140         static const ErrorNo errNo = 590;
01142         PageClosed(const BsPage& page);
01143     };
01144 
01149     class BsPage::NoEndDate : public OsbException {
01150     public:
01152         static const ErrorNo errNo = 591;
01154         NoEndDate(const BsPage& page);
01155     };
01156 
01164     class BsPage::CdrRecoveryNeeded : public OsbException {
01165     public:
01167         static const ErrorNo errNo = 592;
01169         CdrRecoveryNeeded(const BsPage& page);
01170     };
01171 
01175     class BsPage::SetRecoveryFailed : public OsbException {
01176     public:
01178         static const ErrorNo errNo = 621;
01183         SetRecoveryFailed(const BsPage& page, long recoveryId);
01184     };
01185 
01189     class BsPage::UpdateUsageFailed : public OsbException {
01190     public:
01192         static const ErrorNo errNo = 650;
01196         UpdateUsageFailed(const BsPage& page);
01197     };
01198 
01202     class BsPage::CommitCdrFailed : public OsbException {
01203     public:
01205         static const ErrorNo errNo = 651;
01209         CommitCdrFailed(const BsPage& page);
01210     };
01211 
01212     // *****************************************************************
01213     // class BalanceSheet
01252     class BalanceSheet {
01254         friend struct OSB_DB::BalanceSheetGw;
01255 
01256     public:
01258         typedef std::list<BsPage> Pages;
01259 
01260     public:                             // class specific exceptions
01261         // defined and documented after the class definition
01262         class NotLocked;
01263         class NoOpenPage;
01264         class Deadlock;
01265         class LockFailed;
01266         class BillcycleInvalid;
01267         class PageNotFound;
01268 
01269     public:
01273         explicit BalanceSheet(
01274             const Contract::Oid& coId = Contract::Oid()
01275         ) : coId_(coId), locked_(false), objVs_(0),
01276             allPages_(false) {}
01277 
01279 
01280 
01281         const Contract::Oid& coId() const { return coId_; }
01283         bool locked() const { return locked_; }
01285         long objVs() const { return objVs_; }
01289         bool usageModified() const;
01296         bool allPages() const { return allPages_; }
01298         const Pages& pages() const { return pages_; }
01305         void totalCharges(BsTotal& dest, bool openOnly) const;
01307 
01309         Pages& getPages() { return pages_; }
01310 
01317         BsPage* findPage(const BsPage::Oid& pageId);
01318 
01348         bool read(
01349             const OSB_DB::Session& session,
01350                   bool             readAll = false
01351         );
01352 
01362         void sortPages(bool desc = true);
01363 
01389         bool readCharges(
01390             const OSB_DB::Session& session,
01391                   bool             withUsg,
01392                   bool             withCdr,
01393                   bool             withOtc,
01394                   bool             withAdv
01395         );
01396 
01398         void readCdrs();
01399 
01413         void getCdrsToRerate(
01414             CdrList& dest
01415         );
01416 
01456         ObjectStatus lock(
01457             const OSB_DB::Session& session,
01458                   bool             noWait = false
01459         );
01460 
01469         bool setUnlocked();
01470 
01484         BsPage* getOpenPage(const DateTime& ts);
01485 
01486     private:
01545         bool createPage(
01546             const OSB_DB::Session& session,
01547             const Date&            tsDate
01548         );
01549 
01550     public:
01552 
01553 
01561         void addUsage(
01562             const Cdr&             cdr,
01563             const OSB_DB::Session& session
01564         );
01565 
01573         void addUsage(
01574             const CdrList&         cdrs,
01575             const OSB_DB::Session& session
01576         );
01577 
01588         void updateUsage(
01589             const OSB_DB::Session& session
01590         );
01591 
01605         void writeCdrs(
01606             long             recoveryId,
01607             OSB_DB::Session& session,
01608             Asn1Writer&      writer
01609         );
01610 
01625         bool restoreCdrs(OSB_DB::Session& session);
01626 
01636         void removeUsage(const OSB_DB::Session& session);
01637 
01649         void clearRecoveryId(
01650             const OSB_DB::Session& session
01651         );
01652 
01666         void commitCdrs();
01668 
01670 
01671 
01681         BsPage::Oid addOtc(
01682             const OnetimeCharge&   otc,
01683             const OSB_DB::Session& session
01684         );
01685 
01696         bool saveOtCharges(const OSB_DB::Session& session);
01698 
01700 
01701 
01702         void addAdvCharge(
01703             const Money&           charge,
01704             const DateTime&        valueDate,
01705             const OSB_DB::Session& session
01706         );
01708         bool saveAdvCharges(const OSB_DB::Session& session);
01710 
01725         void closeBilledPages(
01726             const OSB_DB::Session& session,
01727             const DateTime&        billed,
01728             const Contract&        contract
01729         );
01730 
01749         bool openPage(
01750             const OSB_DB::Session& session,
01751             const DateTime&        ts,
01752             const Contract&        contract
01753         );
01754 
01755     public:
01767         bool insert(const OSB_DB::Session& session);
01768 
01776         ObjectStatus checkObjVs(
01777             const OSB_DB::Session& session
01778         ) const;
01779 
01780     private:
01782         Contract::Oid coId_;
01791         bool locked_;
01793         long objVs_;
01795         bool allPages_;
01797         Pages pages_;
01807         Date nextPageStart_;
01808 
01810         Billcycle::Oid bcId_;
01812         Billcycle::Type bcType_;
01813     };                                  // class BalanceSheet
01814 
01815     // *****************************************************************
01816     // exceptions of BalanceSheet
01817 
01819     class BalanceSheet::NotLocked : public OsbException {
01820     public:
01822         static const ErrorNo errNo = 582;
01824         NotLocked(const BalanceSheet& bs);
01825     };
01826 
01828     class BalanceSheet::NoOpenPage : public OsbException {
01829     public:
01831         static const ErrorNo errNo = 586;
01836         NoOpenPage(
01837             const BalanceSheet& bs,
01838             const DateTime&     ts
01839         );
01840     };
01841 
01848     class BalanceSheet::Deadlock : public OsbException {
01849     public:
01851         static const ErrorNo errNo = 584;
01853         Deadlock(const BalanceSheet& bs);
01854     };
01855 
01863     class BalanceSheet::LockFailed : public OsbException {
01864     public:
01866         static const ErrorNo errNo = 583;
01868         LockFailed(const BalanceSheet& bs);
01869     };
01870 
01876     class BalanceSheet::BillcycleInvalid : public OsbException {
01877     public:
01879         static const ErrorNo errNo = 585;
01881         BillcycleInvalid(
01882             const BalanceSheet&   bs,
01883             const Billcycle::Oid& bcId,
01884                   Billcycle::Type bcType
01885         );
01886     };
01887 
01891     class BalanceSheet::PageNotFound : public OsbException {
01892     public:
01894         static const ErrorNo errNo = 587;
01899         PageNotFound(
01900             const BalanceSheet& bs,
01901             const BsPage::Oid&  pageId
01902         );
01903     };
01904 }                                       // namespace OSB_LIB
01905 #endif                                  // #ifndef _BALANCESHEET_H_

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