def NewMultiplyEntry(settings=None, global_list=None, client=None, project=None): """Return MultiplyEntry according to settings.""" is_settings = type(settings) is Settings is_client = type(client) is Client if not is_settings or not is_client: return MultiplyEntry() # get language from client lang = client.language # get replaces replace_dict = replacer( settings=settings, global_list=global_list, client=client, project=project ) title = settings.defaults[lang].multiplyentry_title.format_map(replace_dict) comment = settings.defaults[lang].multiplyentry_comment.format_map(replace_dict) # get other values quantity = settings.defaults[lang].get_multiplyentry_quantity() quantity_format = settings.defaults[lang].multiplyentry_quantity_format hour_rate = settings.defaults[lang].get_multiplyentry_hour_rate() # return entry with default values from settings default return MultiplyEntry( title=title, comment=comment, quantity=quantity, quantity_format=quantity_format, hour_rate=hour_rate )
class TestOfferB(object): """Simple test offer.""" # generate some example entries b = BaseEntry( title='Base title', comment='Base comment', quantity=1, time=2.5, price=125 ) m = MultiplyEntry( title='Multiply title', comment='Multiply comment', quantity=0.5, hour_rate=4 ) c = MultiplyEntry( title='Multiply title 2', comment='Multiply comment 2', quantity=4, hour_rate=3.15 ) d = ConnectEntry( title='Connect title 2', comment='Connect comment 2', quantity=9, is_time=False, multiplicator=0.25 ) # init the offer out = Offer( title='Testing offer B' ) # add entries to its list out.append(b) out.append(m) out.append(c) out.append(d) # connect 2nd entry to 4th entry out.get_entry_list()[3].connect_entry( entry_list=out.get_entry_list(), entry_id=out.get_entry_list()[1].get_id() ) # 4th entry get_price() should now return 0.5 * 4 * 9 * 0.25 * 50.00 = 225.00 wage = Decimal('50.00') p = round(Decimal(0.5 * 4 * 9 * 0.25) * wage, 2) assert out.get_entry_list()[3].get_price( entry_list=out.get_entry_list(), wage=wage ) == p assert len(out.get_entry_list()) == 4
def test_unit_price(): """Test unit price and unit price tax methods.""" # BaseEntry a_unitp = BaseEntry(quantity=2, price=100) assert a_unitp.get_price() == Decimal('200.00') assert a_unitp.get_unit_price() == Decimal('100.00') # MultiplyEntry b_unitp = MultiplyEntry(quantity=2, hour_rate='0:30') assert b_unitp.get_price(wage=Decimal('50.00')) == Decimal('50.00') assert b_unitp.get_unit_price(wage=Decimal('50.00')) == Decimal('25.00') # ConnectEntry c_unitp = ConnectEntry(quantity=3, multiplicator=2) # list creation liste = [] liste.append(b_unitp) liste.append(c_unitp) # linking liste[1].connect_entry(entry_list=liste, entry_id=liste[0].get_id()) assert c_unitp.get_price(entry_list=liste, wage=Decimal('50.00')) == Decimal('300.00') assert c_unitp.get_unit_price(entry_list=liste, wage=Decimal('50.00')) == Decimal('100.00')
def test_connectentry_wage_add(): """ The new wage_add feature lets the user add an amount to the wage rate by setting wage_add in the MultiplyEntry class. The ConnectEntry should be able to know this and calculate the wage correctly. """ a = MultiplyEntry(quantity=1, hour_rate=1) b = MultiplyEntry(quantity=1, hour_rate=1) c = ConnectEntry(quantity=1, multiplicator=2, is_time=True) # add to list and connect the stuff liste = [] liste.append(a) liste.append(b) c.connect_entry(liste, a.get_id()) c.connect_entry(liste, b.get_id()) liste.append(c) wage = Decimal('50') # default it should just multiply the wage by 1 now # and the ConnectEntry doubles it (x2) assert c.get_price(liste, wage) == Decimal('200.00') # now b gets wage_add +25 thus the new connect should # output a new total of: # 2x 50 = 100 + 2x 75 = 150 == 250 b.set_wage_add('25.00') assert c.get_price(liste, wage) == Decimal('250.00')
class TestOffer(object): """Simple test offer.""" # generate some example entries b = BaseEntry(title='Base title', comment='Base comment', quantity=1, time=2.5, price=125) m = MultiplyEntry(title='Multiply title', comment='Multiply comment', quantity=0.5, hour_rate=4) c = ConnectEntry(title='Connect title', comment='Connect comment', quantity=3, is_time=True, multiplicator=2) # init the offer out = Offer(title='Testing offer') # add entries to its list out.append(b) out.append(m) out.append(c) assert len(out.get_entry_list()) == 3
def test_entry_tax_price(): """Test the price calculation with tax.""" blubb = MultiplyEntry(quantity=1.0, hour_rate=2.0) wage = Decimal('50') assert blubb.get_price(wage=wage) == Decimal('100') assert blubb.get_price_tax(wage=wage) == Decimal('0') # set tax to 19 % blubb.set_tax(19) assert blubb.get_price(wage=wage) == Decimal('100') assert blubb.get_price_tax(wage=wage) == Decimal('19') assert blubb.get_tax_percent() == Decimal('19')
def load_item_from_file(self, filename=None): """Load the item from the file.""" filename = str(filename) # cancel if the file does not exist if not os.path.isfile(filename): return False # try to load the file try: with open(filename, 'r') as f: dic = json.load(f) except Exception: return False # get type typ = False if 'item' in dic.keys(): if 'type' in dic['item'].keys(): typ = dic['item']['type'] # has no type, so cancel if not typ: return False # convert item json to Offer if typ == 'Offer': dic['item'] = Offer().from_json(js=dic['item']) # convert item json to Invoice elif typ == 'Invoice': dic['item'] = Invoice().from_json(js=dic['item']) # convert item json to BaseEntry elif typ == 'BaseEntry': dic['item'] = BaseEntry().from_json(js=dic['item']) # convert item json to MultiplyEntry elif typ == 'MultiplyEntry': dic['item'] = MultiplyEntry().from_json(js=dic['item']) # convert item json to ConnectEntry elif typ == 'ConnectEntry': dic['item'] = ConnectEntry().from_json(js=dic['item']) # otherwise cancel else: return False return dic
def PresetMultiplyEntry( entry_preset=None, settings=None, global_list=None, client=None, project=None ): """Return MultiplyEntry according to settings.""" if type(entry_preset) is not MultiplyEntry: return NewMultiplyEntry(settings=settings, client=client, project=project) # get replaces replace_dict = replacer( settings=settings, global_list=global_list, client=client, project=project ) title = entry_preset.title.format_map(replace_dict) comment = entry_preset.comment.format_map(replace_dict) # get other values id = entry_preset._id quantity = entry_preset._quantity quantity_format = entry_preset.quantity_format quantity_b = entry_preset._quantity_b quantity_b_format = entry_preset.quantity_b_format hour_rate = entry_preset._hour_rate # return entry with default values from settings default return MultiplyEntry( id=id, title=title, comment=comment, quantity=quantity, quantity_format=quantity_format, quantity_b=quantity_b, quantity_b_format=quantity_b_format, hour_rate=hour_rate )
def load_entry_list_from_js(self, lis=None): """Convert list to entry object list.""" entry_list = [] # cycle through the list of dicts for entry in lis: # it should have a type key if 'type' in entry.keys(): # entry is BaseEntry if entry['type'] == 'BaseEntry': # convert this dict to an offer objetc then! entry_list.append(BaseEntry().from_json(js=entry)) # entry is MultiplyEntry if entry['type'] == 'MultiplyEntry': # convert this dict to an offer objetc then! entry_list.append(MultiplyEntry().from_json(js=entry)) # entry is ConnectEntry if entry['type'] == 'ConnectEntry': # convert this dict to an offer objetc then! entry_list.append(ConnectEntry().from_json(js=entry)) return entry_list
def test_multiplyentry_wage_add(): """ The new wage_add feature lets the user add an amount to the wage rate by setting wage_add in the MultiplyEntry class. This simply adds the amount to the hourly wage and calculates the new amount for this specific entry then. """ a = MultiplyEntry(quantity=1, hour_rate=0.5) wage = Decimal('50') # default it should just multiply the wage by 0.5 now assert a.get_price(wage) == Decimal('25.00') # now I set the wage_add so that the wage should become # 75.00 in the calculation (+25.00) a.set_wage_add('25.00') assert a.get_price(wage) == Decimal('37.50')
def test_integrety_entry(): """Test if BaseEntry, ConnectEntry and MultiplyEntry work as they should.""" a = BaseEntry(title='Title A', comment='Comment A', quantity=1.0, time=1.0, price=50.00) b = MultiplyEntry(title='Title B', comment='Comment B', quantity=2.0, hour_rate=0.5) c = ConnectEntry(title='Title C', comment='Comment C', quantity=2.5, is_time=False, multiplicator=0.5) d = ConnectEntry(title='Title D', comment='Comment D', quantity=3.0, is_time=True, multiplicator=0.75) # init entries in list entries = [] entries.append(a) entries.append(b) entries.append(c) entries.append(d) # connect first two entries two the first ConnectEntry entries[2].connect_entry(entry_list=entries, entry_id=entries[0].get_id()) entries[2].connect_entry(entry_list=entries, entry_id=entries[1].get_id()) # connect first ConnectEntry to the second ConnectEntry entries[3].connect_entry(entry_list=entries, entry_id=entries[2].get_id()) # connect second ConnectEntry to the first ConnectEntry # and it should not work anymore entries[2].connect_entry(entry_list=entries, entry_id=entries[3].get_id()) assert entries[3].get_id() not in entries[2].get_connected() # connect MultiplyEntry to the second ConnectEntry entries[3].connect_entry(entry_list=entries, entry_id=entries[1].get_id()) # set wage to work with wage = Decimal('50.00') # check values for BaseEntry assert entries[0].title == 'Title A' assert entries[0].comment == 'Comment A' assert entries[0].get_quantity() == Decimal('1.0') assert entries[0].get_time() == QuantityTime(1.0) assert entries[0].get_price() == Decimal('50.00') # check values for MultiplyEntry assert entries[1].title == 'Title B' assert entries[1].comment == 'Comment B' assert entries[1].get_quantity() == Decimal('2.0') assert entries[1].get_hour_rate() == QuantityTime(0.5) assert entries[1].get_time() == QuantityTime(1.0) assert entries[1].get_price(wage=wage) == Decimal('50.00') # check values for first ConnectEntry assert entries[2].title == 'Title C' assert entries[2].comment == 'Comment C' assert entries[2].get_quantity() == Decimal('2.5') assert entries[2].get_is_time() is False assert entries[2].get_time(entry_list=entries) == QuantityTime(0) assert entries[2].get_price(entry_list=entries, wage=wage) == Decimal('125.00') # check values for second ConnectEntry assert entries[3].title == 'Title D' assert entries[3].comment == 'Comment D' assert entries[3].get_quantity() == Decimal('3.0') assert entries[3].get_is_time() is True assert entries[3].get_time(entry_list=entries) == QuantityTime('2:15:00') assert entries[3].get_price(entry_list=entries, wage=wage) == Decimal('112.50')
def test_json_conversion_multiplyentry(): """Test the json conversion.""" # init the individual object a = MultiplyEntry(title='Total individual', comment='Individual comment!', quantity=1.25, quantity_format='{F}:{R} min', hour_rate=0.75) # make new default object b = MultiplyEntry() # for now the values must not be the same assert b.get_id() != a.get_id() assert b.title != a.title assert b.comment != a.comment assert b.get_quantity() != a.get_quantity() assert b.quantity_format != a.quantity_format assert b.get_hour_rate() != a.get_hour_rate() # load a into b with json as object with same attrbutes b = MultiplyEntry().from_json(js=a.to_json()) # now the values must be the same assert b.get_id() == a.get_id() assert b.title == a.title assert b.comment == a.comment assert b.get_quantity() == a.get_quantity() assert b.quantity_format == a.quantity_format assert b.get_hour_rate() == a.get_hour_rate() # load a into b with json as a preset b = MultiplyEntry().from_json(js=a.to_json(), keep_id=False) # now the values must be the same - preset alike assert b.get_id() != a.get_id() # not same, since "preset loading" assert b.title == a.title assert b.comment == a.comment assert b.get_quantity() == a.get_quantity() assert b.quantity_format == a.quantity_format assert b.get_hour_rate() == a.get_hour_rate()