GnuCash 2.4.99
gnucash_business.py
Go to the documentation of this file.
00001 # gnucash_business.py -- High level python wrapper classes for the business
00002 #                        parts of GnuCash
00003 #
00004 # Copyright (C) 2008,2010 ParIT Worker Co-operative <paritinfo@parit.ca>
00005 # This program is free software; you can redistribute it and/or
00006 # modify it under the terms of the GNU General Public License as
00007 # published by the Free Software Foundation; either version 2 of
00008 # the License, or (at your option) any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful,
00011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 # GNU General Public License for more details.
00014 #
00015 # You should have received a copy of the GNU General Public License
00016 # along with this program; if not, contact:
00017 # Free Software Foundation           Voice:  +1-617-542-5942
00018 # 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00019 # Boston, MA  02110-1301,  USA       gnu@gnu.org
00020 #
00021 # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
00022 # @author Jeff Green,   ParIT Worker Co-operative <jeff@parit.ca>
00023 ##  @file
00024 #   @brief High level python wrapper classes for the business parts of GnuCash
00025 #   @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
00026 #   @author Jeff Green,   ParIT Worker Co-operative <jeff@parit.ca>
00027 #   @ingroup python_bindings
00028 
00029 import gnucash_core_c
00030 
00031 from function_class import \
00032      ClassFromFunctions, extract_attributes_with_prefix, \
00033      default_arguments_decorator, method_function_returns_instance, \
00034      methods_return_instance, methods_return_instance_lists
00035 
00036 from gnucash_core import \
00037      GnuCashCoreClass, GncNumeric, GncCommodity, Transaction, \
00038      Split, Book, GncLot, Account
00039 
00040 from gnucash_core_c import GNC_OWNER_CUSTOMER, GNC_OWNER_JOB, \
00041     GNC_OWNER_EMPLOYEE, GNC_OWNER_VENDOR, \
00042     GNC_PAYMENT_CASH, GNC_PAYMENT_CARD, \
00043     GNC_DISC_PRETAX, GNC_DISC_SAMETIME, GNC_DISC_POSTTAX, \
00044     GNC_TAXINCLUDED_YES, GNC_TAXINCLUDED_NO, GNC_TAXINCLUDED_USEGLOBAL, \
00045     GNC_AMT_TYPE_VALUE, GNC_AMT_TYPE_PERCENT, GNC_ID_INVOICE
00046 
00047 import datetime
00048 
00049 class GnuCashBusinessEntity(GnuCashCoreClass):
00050     def __init__(self, book=None, id=None, currency=None, name=None,
00051                  instance=None):
00052         if instance == None:
00053             if book==None or id==None or currency==None:
00054                 raise Exception(
00055                     "you must call GnuCashBusinessEntity.__init__ "
00056                     "with either a book, id, and currency, or an existing "
00057                     "low level swig proxy in the argument instance")
00058             GnuCashCoreClass.__init__(self, book)
00059             self.SetID(id)
00060             self.SetCurrency(currency)
00061             if name != None:
00062                 self.SetName(name)
00063         else:
00064             GnuCashCoreClass.__init__(self, instance=instance)
00065 
00066 class Customer(GnuCashBusinessEntity): pass
00067                          
00068 class Employee(GnuCashBusinessEntity): pass
00069 
00070 class Vendor(GnuCashBusinessEntity): pass
00071 
00072 class Job(GnuCashBusinessEntity):
00073     # override the superclass contructor, as Job doesn't require
00074     # a currency but it does require an owner
00075     def __init__(self, book=None, id=None, owner=None, name=None,
00076                  instance=None):
00077         if instance == None:
00078             if book==None or id==None or owner==None:
00079                 raise Exception(
00080                     "you must call Job.__init__ "
00081                     "with either a book, id, and owner or an existing "
00082                     "low level swig proxy in the argument instance")
00083             GnuCashCoreClass.__init__(self, book)
00084             self.SetID(id)
00085             self.SetOwner(owner)
00086             if name != None:
00087                 self.SetName(name)
00088         else:
00089             GnuCashCoreClass.__init__(self, instance=instance)    
00090 
00091 class Address(GnuCashCoreClass): pass
00092     
00093 class BillTerm(GnuCashCoreClass): pass
00094 
00095 class TaxTable(GnuCashCoreClass):
00096     def __init__(self, book=None, name=None, first_entry=None, instance=None):
00097         if instance == None:
00098             if book==None or name==None or first_entry==None:
00099                 raise Exception(
00100                     "you must call TaxTable.__init__  with either a "
00101                     "book, name, and first_entry, or an existing "
00102                     "low level swig proxy in the argument instance")
00103             GnuCashCoreClass.__init__(self, book)
00104             self.SetName(name)
00105             self.AddEntry(first_entry)
00106         else:
00107             GnuCashCoreClass.__init__(self, instance=instance)
00108 
00109 class TaxTableEntry(GnuCashCoreClass):
00110     def __init__(self, account=None, percent=True, amount=None, instance=None):
00111         """TaxTableEntry constructor
00112         
00113         You must provide an account, or be initizing this with an existing
00114         swig proxy object via the instance keyword argument.
00115         
00116         You may also optionally set the percent keyword argument to False to get
00117         a fixed value instead of percentage based tax (which is the default, or
00118         when percent=True).
00119         
00120         The tax will be zero percent or zero unless you set the amount keyword
00121         argument to a GncNumeric value as well.
00122         """
00123 
00124         if instance == None:
00125             if account==None:
00126                 raise Exception(
00127                     "you must call TaxTableEntry.__init__  with either a "
00128                     "account or an existing "
00129                     "low level swig proxy in the argument instance")
00130             GnuCashCoreClass.__init__(self)
00131             self.SetAccount(account)
00132             if percent:
00133                 self.SetType(GNC_AMT_TYPE_PERCENT)
00134             else:
00135                 self.SetType(GNC_AMT_TYPE_VALUE)
00136             if amount != None:
00137                 self.SetAmount(amount)
00138         else:
00139             GnuCashCoreClass.__init__(self, instance=instance)        
00140 
00141 class Invoice(GnuCashCoreClass):
00142     def __init__(self, book=None, id=None, currency=None, owner=None,
00143                  date_opened=None, instance=None):
00144         """Invoice Contstructor
00145 
00146         You must provide a book, id, currency and owner
00147         (Customer, Job, Employee, Vendor) or an existing swig proxy object
00148         in the keyword argument instance.
00149 
00150         Optionally, you may provide a date the invoice is opened on
00151         (datetime.date or datetime.datetime), otherwise today's date is used.
00152         """
00153         if instance == None:
00154             if book==None or id==None or currency==None or owner==None:
00155                 raise Exception(
00156                     "you must call Invoice.__init__ "
00157                     "with either a book, id, currency and owner, or an existing"
00158                     "low level swig proxy in the argument instance")
00159             GnuCashCoreClass.__init__(self, book)
00160             self.SetID(id)
00161             self.SetCurrency(currency)
00162             self.SetOwner(owner)
00163             if date_opened == None:
00164                 date_opened = datetime.date.today()
00165             self.SetDateOpened(date_opened)
00166         else:
00167             GnuCashCoreClass.__init__(self, instance=instance)
00168 
00169 class Bill(Invoice):
00170     pass
00171 
00172 def decorate_to_return_instance_instead_of_owner(dec_function):
00173     def new_get_owner_function(self):
00174         (owner_type, instance) = dec_function(self)
00175         if owner_type == GNC_OWNER_CUSTOMER:
00176             return Customer(instance=instance)
00177         elif owner_type == GNC_OWNER_JOB:
00178             return Job(instance=instance)
00179         elif owner_type == GNC_OWNER_EMPLOYEE:
00180             return Employee(instance=instance)
00181         elif owner_type == GNC_OWNER_VENDOR:
00182             return Vendor(instance=instance)
00183         else:
00184             return None
00185     return new_get_owner_function
00186 
00187 class Entry(GnuCashCoreClass):
00188     def __init__(self, book=None, invoice=None, date=None, instance=None):
00189         """Invoice Entry constructor
00190         
00191         You must provide a book or be initizing this with an existing
00192         swig proxy object via the instance keyword argument.
00193 
00194         The optional invoice argument can be set to a Bill or Invoice
00195         that you would like to associate the entry with. You might as well
00196         assign one now, as an Entry can't exist without one, but you can
00197         always use Invoice.AddEntry or Bill.AddEntry later on.
00198 
00199         By default, the entry will be set to today's date unless you
00200         override with the date argument.
00201         """
00202         if instance == None:
00203             if book==None:
00204                 raise Exception(
00205                     "you must call Entry.__init__  with either a "
00206                     "book or an existing "
00207                     "low level swig proxy in the argument instance")
00208             GnuCashCoreClass.__init__(self, book)
00209 
00210             if date == None:
00211                 date = datetime.date.today()
00212             self.SetDate(date)
00213             if invoice != None:
00214                 invoice.AddEntry(self)
00215         else:
00216             GnuCashCoreClass.__init__(self, instance=instance)    
00217 
00218 # Owner
00219 GnuCashBusinessEntity.add_methods_with_prefix('gncOwner')
00220 
00221 owner_dict = {
00222                     'GetCustomer' : Customer,
00223                     'GetVendor' : Vendor,
00224                     'GetEmployee' : Employee,
00225                     'GetJob' : Job,
00226                     'GetAddr' : Address,
00227                     'GetCurrency' : GncCommodity,
00228                     'GetEndOwner': GnuCashBusinessEntity,
00229                     'GetBalanceInCurrency': GncNumeric,
00230               }
00231 methods_return_instance(GnuCashBusinessEntity, owner_dict)
00232 
00233 methods_return_instance_lists(
00234     GnuCashBusinessEntity, {
00235         'GetCommoditiesList': GncCommodity
00236     })
00237 
00238 # Customer
00239 Customer.add_constructor_and_methods_with_prefix('gncCustomer', 'Create')
00240 
00241 customer_dict = {
00242                     'GetAddr' : Address,
00243                     'GetShipAddr' : Address,
00244                     'GetDiscount' : GncNumeric,
00245                     'GetCredit' : GncNumeric,
00246                     'GetTerms' : BillTerm,
00247                     'GetCurrency' : GncCommodity,
00248                     'GetTaxTable': TaxTable,
00249                 }
00250 methods_return_instance(Customer, customer_dict)
00251 
00252 # Employee
00253 Employee.add_constructor_and_methods_with_prefix('gncEmployee', 'Create')
00254 
00255 employee_dict = {
00256                     'GetBook' : Book,
00257                     'GetAddr' : Address,
00258                     'GetWorkday' : GncNumeric,
00259                     'GetRate' : GncNumeric,
00260                     'GetCurrency' : GncCommodity
00261                 }
00262 methods_return_instance(Employee, employee_dict)
00263 
00264 # Vendor
00265 Vendor.add_constructor_and_methods_with_prefix('gncVendor', 'Create')
00266 
00267 vendor_dict =   {
00268                     'GetAddr' : Address,
00269                     'GetTerms' : BillTerm,
00270                     'GetCurrency' : GncCommodity,
00271                     'GetTaxTable': TaxTable,
00272                 }
00273 methods_return_instance(Vendor, vendor_dict)
00274 
00275 # Job
00276 Job.add_constructor_and_methods_with_prefix('gncJob', 'Create')
00277 Job.decorate_functions(
00278     decorate_to_return_instance_instead_of_owner,
00279     'GetOwner')
00280 
00281 # Address
00282 Address.add_constructor_and_methods_with_prefix('gncAddress', 'Create')
00283 
00284 # BillTerm
00285 BillTerm.add_constructor_and_methods_with_prefix('gncBillTerm', 'Create')
00286 
00287 billterm_dict = {
00288                     'LookupByName' : BillTerm,
00289                     'GetDiscount' : GncNumeric,
00290                     'GetParent' : BillTerm,
00291                     'ReturnChild' : BillTerm
00292                 }
00293 methods_return_instance(BillTerm, billterm_dict)
00294 
00295 # TaxTable
00296 TaxTable.add_constructor_and_methods_with_prefix('gncTaxTable', 'Create')
00297 
00298 taxtable_dict = {
00299                     'GetParent': TaxTable,
00300                 }
00301 methods_return_instance(TaxTable, taxtable_dict)
00302 
00303 # TaxTableEntry
00304 TaxTableEntry.add_constructor_and_methods_with_prefix(
00305     'gncTaxTableEntry', 'Create')
00306 
00307 taxtableentry_dict = {
00308                          'GetAccount': Account,
00309                          'GetAmount': GncNumeric,
00310                      }
00311 
00312 # Invoice
00313 Invoice.add_constructor_and_methods_with_prefix('gncInvoice', 'Create')
00314 methods_return_instance_lists(
00315     Invoice, { 'GetEntries': Entry })
00316 
00317 # Bill
00318 Bill.add_methods_with_prefix('gncBill')
00319 
00320 invoice_dict = {
00321                    'GetTerms': BillTerm,
00322                    'GetCurrency': GncCommodity,
00323                    'GetToChargeAmount': GncNumeric,
00324                    'GetPostedLot': GncLot,
00325                    'GetPostedTxn': Transaction,
00326                    'GetPostedAcc': Account,
00327                    'GetTotal': GncNumeric,
00328                    'GetTotalOf': GncNumeric,
00329                    'GetTotalSubtotal': GncNumeric,
00330                    'GetTotalTax': GncNumeric,
00331                    'PostToAccount': Transaction,
00332                    'GetBook': Book,
00333                }
00334 methods_return_instance(Invoice, invoice_dict)
00335 Invoice.decorate_functions(
00336     decorate_to_return_instance_instead_of_owner,
00337     'GetOwner', 'GetBillTo')
00338 
00339 # Entry
00340 Entry.add_constructor_and_methods_with_prefix('gncEntry', 'Create')
00341 
00342 entry_dict = {
00343                  'GetQuantity': GncNumeric,
00344                  'GetInvAccount': Account,
00345                  'GetInvPrice': GncNumeric,
00346                  'GetInvDiscount': GncNumeric,
00347                  'GetInvTaxTable': TaxTable,
00348                  'GetBillAccount': Account,
00349                  'GetBillPrice': GncNumeric,
00350                  'GetBillTaxTable': TaxTable,
00351                  'Copy': Entry,
00352                  'ReturnValue': GncNumeric,
00353                  'ReturnDiscountValue': GncNumeric,
00354                  'ReturnTaxValue': GncNumeric,
00355                  'GetInvoice': Invoice,
00356                  'GetBill': Invoice
00357              }
00358 Entry.decorate_functions(
00359     decorate_to_return_instance_instead_of_owner,
00360     'GetBillTo' )
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines