def test_update(self):
        self.dbu.create_some_test_well_royalty_masters()

        # change all types of attributes, read another record and then read the record again to make sure
        # the changes were made.
        well = self.db.select('WellRoyaltyMaster', ID=2)
        well[0].WellEvent = 'Changed'
        well[0].LeaseID = 100
        well[0].CommencementDate = '2016-02-01 00:00:00'
        well[0].WellType = None
        self.db.update(well[0])
        well = self.db.select('WellRoyaltyMaster', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, 'SKWI111062705025W300')
        well = self.db.select('WellRoyaltyMaster', ID=2)
        self.assertEqual(well[0].ID, 2)
        self.assertEqual(well[0].WellEvent, 'Changed')
        self.assertEqual(well[0].CommencementDate, datetime(2016, 2, 1, 0, 0))
        self.assertEqual(well[0].WellType, None)

        ds = DataStructure()
        self.assertRaises(AttributeError, self.db.update, ds)
        
        ds._table_name = 'WellRoyaltyMaster'
        self.assertRaises(AttributeError, self.db.update, ds)
        ds.ID = 100
        self.assertRaises(AppError, self.db.update, ds)
Example #2
0
    def dict_to_obj(dic, obj=None):
        if not obj:
            obj = DataStructure()
        for k in dic:
            obj.__dict__[k] = dic[k]

        return obj
    def test_calc_sask_oil_prov_crown_deductions(self):

        pr = ProcessRoyalties()
        calc = DataStructure()
        calc.TransBaseValue = 0
        calc.BaseRoyaltyRate = 0.1

        m = DataStructure()
        m.TransRate = 0.123
        m.ProdVol = 150.0

        lease_royalty_master = DataStructure()
        lease_royalty_master.TransDeducted = "All"
        lease_royalty_master.CrownMultiplier = 1.0

        fn_interest = 1.0
        rp_interest = 100.0

        # Note: this is a round even situation... it's questionable
        self.assertEqual(
            1.84, pr.calc_sask_oil_prov_crown_deductions(m, fn_interest, rp_interest, lease_royalty_master, calc)
        )

        lease_royalty_master.CrownMultiplier = 0.9

        fn_interest = 0.8
        rp_interest = 90.0

        self.assertEqual(
            1.2, pr.calc_sask_oil_prov_crown_deductions(m, fn_interest, rp_interest, lease_royalty_master, calc)
        )
 def test_formatter_date(self):
     monthly = DataStructure()
     monthly.ID = 123
     monthly.ExtractMonth = datetime(2016, 2, 22)
     self.assertEqual(monthly._format.yyyy_mm_dd(monthly.ExtractMonth),
                      '2016-02-22')
     self.assertEqual(monthly._format.yyyy_mm_dd(None), '')
    def test_update(self):
        self.dbu.create_some_test_well_royalty_masters()

        # change all types of attributes, read another record and then read the record again to make sure
        # the changes were made.
        well = self.db.select("WellRoyaltyMaster", ID=2)
        well[0].WellEvent = "Changed"
        well[0].LeaseID = 100
        well[0].CommencementDate = "2016-02-01 00:00:00"
        well[0].WellType = None
        self.db.update(well[0])
        well = self.db.select("WellRoyaltyMaster", ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, "SKWI111062705025W300")
        well = self.db.select("WellRoyaltyMaster", ID=2)
        self.assertEqual(well[0].ID, 2)
        self.assertEqual(well[0].WellEvent, "Changed")
        self.assertEqual(well[0].CommencementDate, datetime(2016, 2, 1, 0, 0))
        self.assertEqual(well[0].WellType, None)

        ds = DataStructure()
        self.assertRaises(AttributeError, self.db.update, ds)

        ds._table_name = "WellRoyaltyMaster"
        self.assertRaises(AttributeError, self.db.update, ds)
        ds.ID = 100
        self.assertRaises(AppError, self.db.update, ds)
    def dict_to_obj(self,dic,obj=None):
        if not obj:
            obj = DataStructure()
        for k in dic:
            obj.__dict__[k] = dic[k]

        return obj
    def test_html_lf(self):
        well = DataStructure()
        well.Msg = 'abc;def;geh;'
        self.assertEqual('abc<br>def<br>geh<br>',
                         well._format.html_lf(well.Msg))

        well.Msg = '$abc;$def;$geh;'
        self.assertEqual('\$abc<br>\$def<br>\$geh<br>',
                         well._format.html_lf(well.Msg))
    def test_formatter_lease(self):
        well = DataStructure()
        well.ID = 123
        well.LeaseType = 'OL'

        # If there is no attribute in the object called 'LeaseID' use the ID attribute to format the lease string
        self.assertEqual(well._format.Lease, 'OL-0123')

        well.LeaseID = 1
        # If there is an attribute in the object called 'LeaseID' use it to format the lease string
        self.assertEqual(well._format.Lease, 'OL-0001')
    def test_data_structure(self):
        well = DataStructure()
        well.ID = 123
        well.Name = "WellName"

        # This returns a dictionary object for the attributes in an object
        dd = vars(well)
        self.assertEqual(len(dd), 3)
        self.assertIn("_format", dd)  # The format attribute is used for formatting.
        self.assertEqual(dd["ID"], 123)
        self.assertEqual(dd["Name"], "WellName")
    def test_formatter_lease(self):
        well = DataStructure()
        well.ID = 123
        well.LeaseType = "OL"

        # If there is no attribute in the object called 'LeaseID' use the ID attribute to format the lease string
        self.assertEqual(well._format.Lease, "OL-0123")

        well.LeaseID = 1
        # If there is an attribute in the object called 'LeaseID' use it to format the lease string
        self.assertEqual(well._format.Lease, "OL-0001")
Example #11
0
    def test_string_is_formula(self):
        expression = Expression()

        s = "prod + sales + gj"
        monthly = DataStructure()
        monthly.ProdVol = 100
        monthly.SalesVol = 90
        monthly.GJ = 1000

        self.assertEqual(1190, expression.evaluate_expression(s, monthly))
        self.assertEqual("100 + 90 + 1000",
                         expression.resolve_expression(s, monthly))
    def test_format_lease(self):
        """ This is the old way of formatting attributes and will be deprecated shortly: test_formatter_lease
        for the new way """
        well = DataStructure()
        well.ID = 123
        well.LeaseType = 'OL'

        # If there is no attribute in the object called 'LeaseID' use the ID attribute to format the lease string
        self.assertEqual(well.Lease, 'OL-0123')

        well.LeaseID = 1
        # If there is an attribute in the object called 'LeaseID' use it to format the lease string
        self.assertEqual(well.Lease, 'OL-0001')
    def test_data_structure(self):
        well = DataStructure()
        well.ID = 123
        well.Name = 'WellName'

        # This returns a dictionary object for the attributes in an object
        dd = vars(well)
        self.assertEqual(len(dd), 4)
        self.assertIn('_format',
                      dd)  # The format attribute is used for formatting.
        self.assertIn('_original', dd)  # Used for comparing differences.
        self.assertEqual(dd['ID'], 123)
        self.assertEqual(dd['Name'], 'WellName')
    def test_format_lease(self):
        """ This is the old way of formatting attributes and will be deprecated shortly: test_formatter_lease
        for the new way """
        well = DataStructure()
        well.ID = 123
        well.LeaseType = "OL"

        # If there is no attribute in the object called 'LeaseID' use the ID attribute to format the lease string
        self.assertEqual(well.Lease, "OL-0123")

        well.LeaseID = 1
        # If there is an attribute in the object called 'LeaseID' use it to format the lease string
        self.assertEqual(well.Lease, "OL-0001")
Example #15
0
    def test_evaluate_and_resolve_expression(self):
        expression = Expression()

        util = DatabaseUtilities()
        util.delete_lookups()
        util.insert_lookups('somevalue', 0, 2)
        util.insert_lookups('m.monthval', 201703, 4)

        monthly = DataStructure()
        monthly.ProdMonth = 201703
        monthly.ProdVol = 100
        monthly.SalesVol = 90
        monthly.GJ = 1000
        monthly.Heat = 65.01

        s = "asdf =(prod + sales + gj + heat + somevalue + m.monthval) more stuff in here"

        self.assertEqual(1196 + 65.01,
                         expression.evaluate_expression(s, monthly))
        self.assertEqual(
            "asdf =(100 + 90 + 1000 + 65.01 + 2 + 4) more stuff in here",
            expression.resolve_expression(s, monthly))

        s = "asdf =(prod + sales + gj + notfoundvalue) more stuff in here"
        self.assertRaises(AppError, expression.evaluate_expression, s, monthly)
Example #16
0
def generate_pdfworksheet_from_calc(calc):
    try:
        db = config.get_database()
        if calc.Entity == 'Well':
            well = db.select1('WellRoyaltyMaster', ID=calc.EntityID)
        else:
            well = None
        entity_lease_link_array = db.select('EntityLeaseLink', Entity=calc.Entity, EntityID=calc.EntityID)
        if len(entity_lease_link_array) == 0:
            raise AppError("There were no well_lease_link records for " + str(calc.WellID) + str(calc.ProdMonth))
        entity_lease_link = entity_lease_link_array[0]
        royalty = db.select1('LeaseRoyaltyMaster', ID=calc.LeaseID)
        lease = db.select1('Lease', ID=calc.LeaseID)

        history = db.select_sql("""SELECT ID, ExtractDate, BaseNetRoyaltyValue, GorrNetRoyaltyValue
                   from Calc
                   WHERE ProdMonth = "{}" and LeaseID = "{}" and Entity = "{}" and EntityID = "{}"
                   and Product = "{}" and RPBA = "{}"
                   order by ExtractDate""".format(calc.ProdMonth, calc.LeaseID, calc.Entity, calc.EntityID,
                                                  calc.Product, calc.RPBA))

        prev_BaseNetRoyaltyValue = 0.0
        prev_GorrNetRoyaltyValue = 0.0
        for h in history:
            h.BookedBaseNetRoyaltyValue = h.BaseNetRoyaltyValue - prev_BaseNetRoyaltyValue
            h.BookedGorrNetRoyaltyValue = h.GorrNetRoyaltyValue - prev_GorrNetRoyaltyValue
            h.Booked = h.BookedBaseNetRoyaltyValue + h.BookedGorrNetRoyaltyValue
            prev_BaseNetRoyaltyValue = h.BaseNetRoyaltyValue
            prev_GorrNetRoyaltyValue = h.GorrNetRoyaltyValue

        monthly_array = db.select('Monthly', ExtractDate=calc.ExtractDate, Entity=calc.Entity, EntityID=calc.EntityID,
                                  prodMonth=calc.ProdMonth, product=calc.Product, RPBA=calc.RPBA)
        if len(monthly_array) == 0:
            raise AppError("There were no monthly records for well: " + str(calc.WellID) + " ProdDate: " +
                           str(calc.ProdMonth) + " Product: " + calc.Product)
        monthly = monthly_array[0]  # if there are multiple pick the first one

        ba = db.select1('BAInfo', BAid=monthly.RPBA, BAType='RTP')

        calc_specific = DataStructure(calc.RoyaltySpecific)
        rtp_info = db.select1('RTPInfo', WellEvent=well.WellEvent, Product=calc.Product, Payer=monthly.RPBA,
                              Date=prod_month_to_date(calc.ProdMonth))

        royalty.format_gorr = format_gorr(calc.Gorr)

        return render_template('worksheet/pdfcalc_worksheet.html',
                               well=well, rm=royalty, m=monthly, lease=lease,
                               calc=calc, calc_sp=calc_specific, entity_lease_link=entity_lease_link,
                               ba=ba, rtp_info=rtp_info, history=history)
    except Exception as e:
        print('views.worksheet: ***Error:', e)
        traceback.print_exc(file=sys.stdout)
        tb = traceback.format_exc()
        return "<h2>Error displaying worksheet for " + calc.Entity + " %s</h2><br>" % calc.EntityID + str(e) + \
               '<plaintext>' + tb + '</plaintext>'
    def test_determineRoyaltyPrice(self):

        m = DataStructure()
        m.SalesPrice = 221.123456
        m.TransRate = 2.123455
        m.ProcessingRate = 0.123455

        pr = ProcessRoyalties()
        self.assertAlmostEqual(pr.determine_royalty_price("ActSales", m), 221.123456)

        m.WellHeadPrice = 225
        m.TransRate = 3
        m.ProcessingRate = 1

        self.assertAlmostEqual(pr.determine_royalty_price("ActSales", m), 221.123456)
Example #18
0
def new():
    db = config.get_database()
    if request.method == 'GET':
        return render_template('wells/details.html', new=True, well=None)
    elif request.method == 'POST' and request.form['action'] == 'cancel':
        return redirect(url_for('wells.search'))
    elif request.method == 'POST' and request.form['action'] == 'add':
        ds = DataStructure()
        setattr(ds, '_table_name', 'WellRoyaltyMaster')
        setattr(ds, 'StartDate', '1999-01-01 00:00:00')
        setattr(ds, 'EndDate', '9999-01-01 00:00:00')
        for i in request.form:
            if i != 'action':
                setattr(ds, i, request.form[i])
        try:
            new_id = db.insert(ds)
            flash('Well successfully added')
            return redirect(url_for('wells.details', well_num=new_id))
        except Exception as e:
            flash('Couldn\'t add a new well')
            print('Couldn\'t add a new well: ', e)
            return redirect(url_for('wells.search'))
Example #19
0
def new():
    db = config.get_database()
    if request.method == 'GET':
        return render_template('leases/details.html',
                               new=True,
                               lease=None,
                               royaltymaster=None)
    elif request.method == 'POST' and request.form['action'] == 'cancel':
        return redirect(url_for('leases.search'))
    elif request.method == 'POST' and request.form['action'] == 'add':
        ds = DataStructure()
        setattr(ds, '_table_name', 'Lease')
        for i in request.form:
            if i != 'action':
                setattr(ds, i, request.form[i])
        try:
            new_id = db.insert(ds)
            flash('Lease successfully added')
            return redirect(url_for('leases.details', lease_num=new_id))
        except Exception as e:
            flash('Couldn\'t add a new lease')
            print('Couldn\'t add a new lease: ', e)
            return redirect(url_for('leases.search'))
 def test_json(self):
     well = DataStructure(json_string='{"ID": 123, "LeaseType": "OL"}')
     self.assertEqual(123, well.ID)
     self.assertEqual('OL', well.LeaseType)
     self.assertEqual('{"ID": 123, "LeaseType": "OL"}', well.json_dumps())
    def test_formatter_prod_month(self):
        well = DataStructure()
        well.ProdMonth = 201602

        # If there is no attribute in the object called 'LeaseID' use the ID attribute to format the lease string
        self.assertEqual(well._format.ProdMonth, "2016-02")
    def test_formatter_extract_date(self):
        monthly = DataStructure()
        monthly.ExtractDate = 20170507

        self.assertEqual(monthly._format.ExtractDate, '2017-05-07')
    def test_formatter_prod_month(self):
        well = DataStructure()
        well.ProdMonth = 201602

        self.assertEqual(well._format.ProdMonth, '2016-02')
    def test_calc_royalties(self):
        dbu = DatabaseUtilities()
        dbu.delete_all_tables()
        dbu.create_some_test_econdata()

        pr = ProcessRoyalties()
        well = DataStructure()
        royalty = DataStructure()
        calc = DataStructure()
        monthly = DataStructure()
        well_lease_link = DataStructure()
        rtp_info = DataStructure()

        monthly.Product = "OIL"
        monthly.ProdMonth = 201501
        monthly.ProdVol = 100
        monthly.ProdHours = 744
        monthly.SalesPrice = 210.0
        royalty.RoyaltyScheme = "SKProvCrownVar"
        royalty.OverrideRoyaltyClassification = None
        royalty.ValuationMethod = "ActSales"
        royalty.CrownModifier = None
        royalty.MinRoyaltyRate = None
        royalty.CrownMultiplier = 1.0
        royalty.MinRoyaltyDollar = None
        royalty.TransDeducted = None
        well.CommencementDate = date(2015, 1, 22)
        well.RoyaltyClassification = "Old Oil"
        well.Classification = "Other"
        well.SRC = 0.0
        well_lease_link.PEFNInterest = 1.0
        rtp_info.Percent = 100.0
        calc.SuppRoyaltyValue = 0.0
        calc.GorrRoyaltyValue = 0.0
        calc.TransGorrValue = 0.0

        pr.calc_royalties(well, royalty, calc, monthly, well_lease_link, rtp_info)

        royalty.RoyaltyScheme = "IOGR1995"
        pr.calc_royalties(well, royalty, calc, monthly, well_lease_link, rtp_info)

        royalty.RoyaltyScheme = "IOGR1995,GORR"
        royalty.Gorr = "fixed,0,.02"
        pr.calc_royalties(well, royalty, calc, monthly, well_lease_link, rtp_info)
Example #25
0
    def test_lookup_vars(self):
        expression = Expression()

        util = DatabaseUtilities()
        util.delete_lookups()
        util.insert_lookups('somevalue', 0, 1234)
        util.insert_lookups('m.monthval', 201703, 5678)

        monthly = DataStructure()
        monthly.ProdMonth = 201703
        monthly.ProdVol = 100
        monthly.SalesVol = 90
        monthly.GJ = 1000
        monthly.Heat = 65.01
        monthly.SalesPrice = 50.00
        monthly.TransRate = 10.00
        monthly.ProcessingRate = 5.00
        monthly.GCARate = 3.00
        calc = DataStructure()
        calc.RoyaltyPrice = 27.12

        rv = expression.lookup_vars(
            {
                "prod", "sales", "gj", "heat", "price", "trans", "processing",
                "gca", "royalty_price", "somevalue", "m.monthval"
            }, monthly, calc)

        self.assertEqual(100, rv["prod"])
        self.assertEqual(90, rv["sales"])
        self.assertEqual(1000, rv["gj"])
        self.assertEqual(65.01, rv["heat"])
        self.assertEqual(50, rv["price"])
        self.assertEqual(10, rv["trans"])
        self.assertEqual(5, rv["processing"])
        self.assertEqual(3, rv["gca"])
        self.assertEqual(27.12, rv["royalty_price"])
        self.assertEqual(1234, rv["somevalue"])
        self.assertEqual(5678, rv["m.monthval"])

        self.assertRaises(AppError, expression.lookup_vars, {"notfooundvalue"},
                          monthly)

        monthly.GJ = None
        self.assertRaises(AppError, expression.lookup_vars, {"gj", "sales"},
                          monthly)
    def test_diff(self):
        calc1 = DataStructure()
        self.assertEqual('', calc1.diff('SomeValue'))

        calc2 = DataStructure()
        calc2.original(calc1)
        self.assertRaises(AttributeError, calc2.diff, 'SomeValue')

        calc1.SomeValue = 123
        calc2.SomeValue = 123
        self.assertEqual('', calc1.diff('SomeValue'))

        calc2.SomeValue = 1234
        self.assertEqual('class="diff"', calc2.diff('SomeValue'))
Example #27
0
 def get_data_structure(self, table_name):
     """ This method must be called to create a valid database data structure. """
     ds = DataStructure()
     ds._table_name = table_name
     return ds
    def test_insert(self):
        self.db_create.well_royalty_master()
        
        well = DataStructure()
        well.WellEvent = 'WellEvent for this well'
        # Should raise this error since we need to get the structure from the database
        self.assertRaises(TypeError, self.db.insert)

        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.WellEvent = 'WellEvent for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 1)
        
        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.WellEvent = 'Different WellEvent for this well'
        self.db.insert(well)
        self.assertEqual(well.ID, 2)
        
        well = self.db.select('WellRoyaltyMaster', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, 'WellEvent for this well')
        
        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.WellEvent = 'Next Well WellEvent'
        well.ID = 10
        self.db.insert(well)
        
        well = self.db.select('WellRoyaltyMaster', ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, 'WellEvent for this well')
        
        well = self.db.select('WellRoyaltyMaster', ID=10)
        self.assertEqual(well[0].ID, 10)
        self.assertEqual(well[0].WellEvent, 'Next Well WellEvent')

        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.WellEvent = 'Just One More'
        self.db.insert(well)
        self.assertEqual(well.ID, 11)

        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.BadAttr = 'Just another value'
        self.assertRaises(AppError, self.db.insert, well)
        
        # if the ID is None,Blank,or zero we shold still be able to insert a record
        well = self.db.get_data_structure('WellRoyaltyMaster')
        well.ID = None
        well.WellEvent = 'Just One More'
        self.db.insert(well)
        self.assertEqual(well.ID, 12)
        well.ID = 0
        self.db.insert(well)
        self.assertEqual(well.ID, 13)
        well.ID = ''
        self.db.insert(well)
        self.assertEqual(well.ID, 14)
    def test_calcSaskOilIOGR1995(self):
        m = DataStructure()
        # m.WellHeadPrice = 221.123456
        m.SalesPrice = 221.123456
        m.TransRate = 2.123455
        m.ProcessingRate = 0.123455
        m.ProdVol = 70
        m.ProdMonth = 201501

        calc = DataStructure()
        calc.BaseRoyaltyValue = 0.0
        calc.CommencementPeriod = 0
        calc.BaseRoyaltyVolume = 0.0
        calc.RoyaltyPrice = 0.0

        crown_multiplier = 1.2
        fn_interest = 0.25
        rp_interest = 100

        pr = ProcessRoyalties()
        # all tests for SaskWellHead
        pr.calc_sask_oil_iogr1995(
            datetime(2015, 1, 1), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(464.36, calc.BaseRoyaltyValue)
        self.assertEqual(calc.CommencementPeriod, 0)
        self.assertEqual(calc.BaseRoyaltyVolume, 7)
        self.assertEqual(calc.RoyaltyPrice, 221.123456)

        m.ProdVol = 100
        crown_multiplier = 0.25
        fn_interest = 3
        rp_interest = 100
        pr.calc_sask_oil_iogr1995(
            datetime(2015, 4, 2), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 1990.11)

        m.ProdVol = 170
        crown_multiplier = 1.0
        fn_interest = 1
        rp_interest = 100
        pr.calc_sask_oil_iogr1995(
            datetime(2015, 5, 1), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 5881.88)

        m.ProdVol = 79.9
        crown_multiplier = 3
        fn_interest = 2
        rp_interest = 100
        pr.calc_sask_oil_iogr1995(
            datetime(2010, 1, 1), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 10600.66)

        m.ProdVol = 150
        crown_multiplier = 2
        fn_interest = 4
        rp_interest = 100
        pr.calc_sask_oil_iogr1995(
            datetime(2009, 7, 3), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 38917.73)

        m.ProdVol = 500
        crown_multiplier = 1
        fn_interest = 5
        rp_interest = 100
        pr.calc_sask_oil_iogr1995(
            datetime(2007, 8, 2), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 124271.38)

        m.ProdVol = 800
        crown_multiplier = 5
        fn_interest = 0.1
        rp_interest = 100
        pr.calc_sask_oil_iogr1995(
            datetime(2008, 9, 9), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 21117.29)

        m.ProdVol = 800
        crown_multiplier = 5
        fn_interest = 0.1
        rp_interest = 50
        pr.calc_sask_oil_iogr1995(
            datetime(2008, 9, 9), "SaskWellHead", crown_multiplier, fn_interest, rp_interest, m, calc
        )
        self.assertEqual(calc.BaseRoyaltyValue, 10558.65)
 def test_list_str(self):
     # this silly method is just to get 100% coverage. Don't test this because the order sometimes changes.
     well = DataStructure(json_string='{"ID": 123, "LeaseType": "OL"}')
     s = str(well)
     s = well.headers()
     s = well.data()
 def test_formatter_date(self):
     monthly = DataStructure()
     monthly.ID = 123
     monthly.ExtractMonth = datetime(2016, 2, 22)
     self.assertEqual(monthly._format.yyyy_mm_dd(monthly.ExtractMonth), "2016-02-22")
    def test_calc_sask_oil_prov_crown_royalty_volume_value(self):
        pr = ProcessRoyalties()
        m = DataStructure()
        m.ProdVol = 100

        lease_rm = DataStructure()
        lease_rm.MinRoyaltyRate = 0.0
        lease_rm.MinRoyaltyDollar = 0.0
        lease_rm.CrownMultiplier = 1
        lease_rm.ValuationMethod = "ActSales"
        lease_rm.CrownModifier = None

        calc = DataStructure()
        calc.BaseRoyaltyRate = 0.25
        calc.BaseRoyaltyCalcRate = 0.25
        calc.RoyaltyPrice = 210
        calc.BaseRoyaltyVolume = 0.0
        calc.BaseRoyaltyValue = 0.0
        m.SalesPrice = 223.370366
        m.TransRate = 2.123455
        m.ProcessingRate = 0.123455

        fn_interest = 1.0
        rp_interest = 100.0

        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyVolume, 0.0)
        self.assertEqual(calc.BaseRoyaltyValue, 5584.26)

        calc.BaseRoyaltyCalcRate = -0.01
        calc.BaseRoyaltyRate = -0.01
        lease_rm.MinRoyaltyRate = None
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyRate, 0)

        calc.BaseRoyaltyCalcRate = -0.01
        calc.BaseRoyaltyRate = -0.01
        lease_rm.MinRoyaltyRate = 0.02
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyRate, 0.02)
        self.assertEqual(calc.BaseRoyaltyValue, 446.74)

        lease_rm.MinRoyaltyDollar = 500.0
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyValue, 500.0)

        lease_rm.MinRoyaltyDollar = None
        lease_rm.MinRoyaltyRate = None
        calc.BaseRoyaltyCalcRate = 0.35
        lease_rm.CrownModifier = 0.02
        calc.BaseRoyaltyRate = None
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyRate, 0.37)

        # Reset to normal again
        lease_rm.CrownModifier = 0.0
        calc.BaseRoyaltyCalcRate = 0.25
        calc.RoyaltyPrice = 210
        calc.BaseRoyaltyVolume = 0.0
        calc.BaseRoyaltyValue = 0.0
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyVolume, 0.0)
        self.assertEqual(calc.BaseRoyaltyValue, 5584.26)

        m.ProdVol = 100
        rp_interest = 50.0
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyVolume, 0.0)
        self.assertEqual(calc.BaseRoyaltyValue, 2792.13)

        m.ProdVol = 100
        rp_interest = 50.0
        fn_interest = 0.5
        pr.calc_sask_oil_prov_crown_royalty_volume_value(m, fn_interest, rp_interest, lease_rm, calc)
        self.assertEqual(calc.BaseRoyaltyVolume, 0.0)
        self.assertEqual(calc.BaseRoyaltyValue, 1396.06)
Example #33
0
def generate_worksheet_from_calc(calc, with_js='y'):
    """
    Generate html for the calc row that is passed
    :param calc: a full calc record
    :param with_js: default is 'y' only other option is 'n' and should only be used to work around a
                    pdf generation issue
    :return: html of the worksheet for the calc record
    """
    try:
        db = config.get_database()
        if calc.Entity == 'Well':
            well = db.select1('WellRoyaltyMaster', ID=calc.EntityID)
        else:
            well = None
        entity_lease_link_array = db.select('EntityLeaseLink', Entity=calc.Entity, EntityID=calc.EntityID)
        if len(entity_lease_link_array) == 0:
            raise AppError("There were no well_lease_link records for " + str(calc.WellID) + str(calc.ProdMonth))
        entity_lease_link = entity_lease_link_array[0]
        royalty = db.select1('LeaseRoyaltyMaster', ID=calc.LeaseID)
        lease = db.select1('Lease', ID=calc.LeaseID)

        history = db.select_sql("""SELECT *
                   from Calc
                   WHERE ProdMonth = "{}" and LeaseID = "{}" and Entity = "{}" and EntityID = "{}"
                   and Product = "{}" and RPBA = "{}"
                   order by ExtractDate""".format(calc.ProdMonth, calc.LeaseID, calc.Entity, calc.EntityID,
                                                  calc.Product, calc.RPBA))
        # history = db.select_sql("""SELECT ID, ExtractDate, BaseNetRoyaltyValue, GorrNetRoyaltyValue
        #            from Calc
        #            WHERE ProdMonth = "{}" and LeaseID = "{}" and Entity = "{}" and EntityID = "{}"
        #            and Product = "{}" and RPBA = "{}"
        #            order by ExtractDate""".format(calc.ProdMonth, calc.LeaseID, calc.Entity, calc.EntityID,
        #                                           calc.Product, calc.RPBA))

        prev_BaseNetRoyaltyValue = 0.0
        prev_GorrNetRoyaltyValue = 0.0
        i = 0
        for h in history:
            h.BookedBaseNetRoyaltyValue = h.BaseNetRoyaltyValue - prev_BaseNetRoyaltyValue
            h.BookedGorrNetRoyaltyValue = h.GorrNetRoyaltyValue - prev_GorrNetRoyaltyValue
            h.Booked = h.BookedBaseNetRoyaltyValue + h.BookedGorrNetRoyaltyValue
            prev_BaseNetRoyaltyValue = h.BaseNetRoyaltyValue
            prev_GorrNetRoyaltyValue = h.GorrNetRoyaltyValue
            # Set the original calc record to the history record the one before this one
            if h.ExtractDate == calc.ExtractDate and i > 0:
                calc.original(history[i-1])

            i += 1

        monthly_array = db.select('Monthly', ExtractDate=calc.ExtractDate, Entity=calc.Entity, EntityID=calc.EntityID,
                                  prodMonth=calc.ProdMonth, product=calc.Product, RPBA=calc.RPBA)
        if len(monthly_array) == 0:
            raise AppError("There were no monthly records for well: " + str(calc.WellID) + " ProdDate: " +
                           str(calc.ProdMonth) + " Product: " + calc.Product)
        monthly = monthly_array[0]  # if there are multiple pick the first one

        ba = db.select1('BAInfo', BAid=monthly.RPBA, BAType='RTP')

        calc_specific = DataStructure(calc.RoyaltySpecific)
        rtp_info = db.select1('RTPInfo', WellEvent=well.WellEvent, Product=calc.Product, Payer=monthly.RPBA,
                              Date=prod_month_to_date(calc.ProdMonth))

        royalty.format_gorr = format_gorr(calc.Gorr)

        return render_template('worksheet/calc_worksheet.html',
                               with_js=with_js,
                               well=well, rm=royalty, m=monthly, lease=lease,
                               calc=calc, calc_sp=calc_specific, entity_lease_link=entity_lease_link,
                               ba=ba, rtp_info=rtp_info, history=history)
    except Exception as e:
        print('views.worksheet: ***Error:', e)
        traceback.print_exc(file=sys.stdout)
        tb = traceback.format_exc()
        return "<h2>Error displaying worksheet for " + calc.Entity + " %s</h2><br>" % calc.EntityID + str(e) + \
               '<plaintext>' + tb + '</plaintext>'
 def get_data_structure(self, table_name):
     """ This method must be called to create a valid database data structure. """
     ds = DataStructure()
     ds._table_name = table_name
     return ds
    def test_insert(self):
        self.db_create.well_royalty_master()

        well = DataStructure()
        well.WellEvent = "WellEvent for this well"
        # Should raise this error since we need to get the structure from the database
        self.assertRaises(TypeError, self.db.insert)

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "WellEvent for this well"
        self.db.insert(well)
        self.assertEqual(well.ID, 1)

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "Different WellEvent for this well"
        self.db.insert(well)
        self.assertEqual(well.ID, 2)

        well = self.db.select("WellRoyaltyMaster", ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, "WellEvent for this well")

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "Next Well WellEvent"
        well.ID = 10
        self.db.insert(well)

        well = self.db.select("WellRoyaltyMaster", ID=1)
        self.assertEqual(well[0].ID, 1)
        self.assertEqual(well[0].WellEvent, "WellEvent for this well")

        well = self.db.select("WellRoyaltyMaster", ID=10)
        self.assertEqual(well[0].ID, 10)
        self.assertEqual(well[0].WellEvent, "Next Well WellEvent")

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.WellEvent = "Just One More"
        self.db.insert(well)
        self.assertEqual(well.ID, 11)

        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.BadAttr = "Just another value"
        self.assertRaises(AppError, self.db.insert, well)

        # if the ID is None,Blank,or zero we shold still be able to insert a record
        well = self.db.get_data_structure("WellRoyaltyMaster")
        well.ID = None
        well.WellEvent = "Just One More"
        self.db.insert(well)
        self.assertEqual(well.ID, 12)
        well.ID = 0
        self.db.insert(well)
        self.assertEqual(well.ID, 13)
        well.ID = ""
        self.db.insert(well)
        self.assertEqual(well.ID, 14)
    def test_gorr_royalty(self):

        pr = ProcessRoyalties()
        leaserm = DataStructure()
        calc = DataStructure()
        monthly = DataStructure()
        well_lease_link = DataStructure()
        rtp_info = DataStructure()

        monthly.ProdVol = 100.0
        monthly.ProdHours = 10.0
        monthly.TransRate = 0.1234
        leaserm.TransDeducted = "All"
        leaserm.Gorr = "fixed,0,.02"

        well_lease_link.PEFNInterest = 1.0

        rtp_info.Percent = 100.0

        calc.RoyaltyPrice = 200.0
        calc.TransGorralue = 0.0
        calc.GorrRoyaltyValue = 0.0
        calc.TransGorrValue = 0.0

        pr.calc_gorr(leaserm, calc, monthly, well_lease_link, rtp_info)
        self.assertEqual(400.0, calc.GorrRoyaltyValue)
        self.assertEqual(0.25, calc.TransGorrValue)

        monthly.ProdVol = 100.0
        rtp_info.Percent = 50.0
        calc.TransGorrValue = 0.0
        pr.calc_gorr(leaserm, calc, monthly, well_lease_link, rtp_info)
        self.assertEqual(200.0, calc.GorrRoyaltyValue)
        self.assertEqual(0.12, calc.TransGorrValue)