def NewClient(settings=None, global_list=None): """Return new client object according to settings defaults.""" # return empty client, if there is no correct settings object given if type(settings) is not Settings: return Client() # get language form settings file lang = settings.get_def_language() # get client count / id client_id = replacer( text=settings.defaults[lang].client_id, settings=settings, global_list=global_list ) # return client with default values according to chosen language return Client( client_id=client_id, company=settings.defaults[lang].client_company, salutation=settings.defaults[lang].client_salutation, attention=settings.defaults[lang].client_attention, name=settings.defaults[lang].client_name, family_name=settings.defaults[lang].client_family_name, street=settings.defaults[lang].client_street, post_code=settings.defaults[lang].client_post_code, city=settings.defaults[lang].client_city, tax_id=settings.defaults[lang].client_tax_id, language=lang, def_wage=settings.defaults[lang].get_project_wage(), def_commodity=settings.defaults[lang].commodity )
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 )
def NewProject(settings=None, global_list=None, client=None): """Return new project object according to given settings and client.""" is_settings = type(settings) is Settings is_client = type(client) is Client # return empty project if no valid settings or client object is given if not is_settings or not is_client: return Project() # get language from client lang = client.language # generate default title (according to replacements) title = replacer( text=settings.defaults[lang].project_title, settings=settings, global_list=global_list, client=client ) return Project( client_id=client.client_id, title=title, hours_per_day=settings.defaults[lang].get_project_hours_per_day(), work_days=settings.defaults[lang].get_project_work_days(), wage=client.get_def_wage(), minimum_days=settings.defaults[lang].get_project_minimum_days() )
def PresetOffer( offer_preset=None, settings=None, global_list=None, client=None, project=None, replace=False ): """Return new Offer based on given offer, but with string replacements.""" if type(offer_preset) is not Offer: return NewOffer( settings=settings, global_list=global_list, client=client, project=project ) # get replaces replace_dict = replacer( settings=settings, global_list=global_list, client=client, project=project, offerinvoice=offer_preset ) title = offer_preset.title.format_map(replace_dict) comment = offer_preset.comment.format_map(replace_dict) comment_b = offer_preset.comment_b.format_map(replace_dict) # dates off_date = offer_preset.get_date() if not replace and off_date is not None: off_date = date.today() elif replace and off_date is None: off_date = date.today() # get other values date_fmt = offer_preset.date_fmt wage = offer_preset.get_wage() commodity = offer_preset.commodity round_price = offer_preset.get_round_price() entry_list = offer_preset.get_entry_list() # return new Offer object return Offer( title=title, comment=comment, comment_b=comment_b, date_fmt=date_fmt, date=off_date, wage=wage, commodity=commodity, round_price=round_price, entry_list=entry_list )
def run_command(self): """Try to run the command.""" self.values_to_tmp() # ask for summerizing single_account = npyscreen.notify_input( 'Account:', title='Summerize into one account? (empty if not)') # get replacer dict replacer_dict = replacer(settings=self.parentApp.S, global_list=self.parentApp.L, client=self.parentApp.tmpClient, project=self.parentApp.tmpProject, offerinvoice=self.parentApp.tmpInvoice) # ask for payee def_payee = self.parentApp.S.ledgeradd_def_payee.format_map( replacer_dict) payee = npyscreen.notify_input( 'Payee:', pre_text=def_payee, title='Summerize into one account? (empty = project title)') # cancle on single_account is False or payee is False if single_account is False or payee is False: return False # get payee replaced payee = payee.format_map(replacer_dict) # generate parameters parameter = generate_parameter(single_account=single_account, payee=payee, settings=self.parentApp.S, project=self.parentApp.tmpProject, invoice=self.parentApp.tmpInvoice) if parameter is False: npyscreen.notify_confirm('Cannot generate ledgeradd parameter.', form_color='WARNING') return False parameter = parameter.format_map(replacer_dict) try: os.system('{} {}'.format(self.parentApp.S.ledgeradd_command, parameter)) except Exception: npyscreen.notify_confirm('Command not runned!', form_color='WARNING')
def NewInvoice(settings=None, global_list=None, client=None, project=None): """Return new invoice object according to settings defaults.""" # return empty invoice, if there is no correct settings object given is_settings = type(settings) is Settings is_client = type(client) is Client is_project = type(project) is Project if not is_settings or not is_project or not is_client: return Invoice() # 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].invoice_title.format_map(replace_dict) id = settings.defaults[lang].invoice_id.format_map(replace_dict) comment = settings.defaults[lang].invoice_comment.format_map(replace_dict) comment_b = settings.defaults[lang].invoice_comment_b.format_map(replace_dict) delivery = settings.defaults[lang].invoice_delivery.format_map(replace_dict) ledger_comment = settings.defaults[lang].invoice_ledger_comment.format_map(replace_dict) # date due_days = settings.defaults[lang].get_invoice_due_days() # get other values date_fmt = settings.defaults[lang].date_fmt round_price = settings.defaults[lang].get_invoice_round_price() # return new Invoice object return Invoice( title=title, id=id, comment=comment, comment_b=comment_b, date_fmt=date_fmt, date=date.today(), delivery=delivery, due_days=due_days, commodity=client.def_commodity, round_price=round_price, ledger_comment=ledger_comment )
def add_alias(self): """Add the alias for the client to the ledger alias file.""" # get default account string default_account = replacer( text=self.parentApp.S.ledger_alias_default_account, settings=self.parentApp.S, global_list=self.parentApp.L, client=self.parentApp.tmpClient ) # ask for the account replacement string account = npyscreen.notify_input( 'Account string:', pre_text=default_account ) # user canceled if account is False: return False # generate check text file = self.parentApp.S.ledger_alias_file adder = 'alias {}={}'.format( self.parentApp.tmpClient.client_id, account ) check_text = '{}\n---\n{}\n---\n{}'.format( 'Really add this alias to the file?', file, adder ) # ask user to really add this alias really = npyscreen.notify_yes_no( check_text ) # add if user chose yes if really: add_ledger_alias( ledgerfile=self.parentApp.S.ledger_alias_file, alias=self.parentApp.tmpClient.client_id, string=account )
def PresetConnectEntry( entry_preset=None, settings=None, global_list=None, client=None, project=None ): """Return ConnectEntry according to settings.""" if type(entry_preset) is not ConnectEntry: return NewConnectEntry(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 is_time = entry_preset._is_time multiplicator = entry_preset._multiplicator # return entry with default values from settings default return ConnectEntry( id=id, title=title, comment=comment, quantity=quantity, quantity_format=quantity_format, quantity_b=quantity_b, quantity_b_format=quantity_b_format, is_time=is_time, multiplicator=multiplicator )
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 NewOffer(settings=None, global_list=None, client=None, project=None): """Return new offer object according to settings defaults.""" # return empty offer, if there is no correct settings object given is_settings = type(settings) is Settings is_client = type(client) is Client is_project = type(project) is Project if not is_settings or not is_project or not is_client: return Offer() # 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].offer_title.format_map(replace_dict) comment = settings.defaults[lang].offer_comment.format_map(replace_dict) comment_b = settings.defaults[lang].offer_comment_b.format_map(replace_dict) # get other values date_fmt = settings.defaults[lang].date_fmt round_price = settings.defaults[lang].get_offer_round_price() # return new Offer object return Offer( title=title, comment=comment, comment_b=comment_b, date_fmt=date_fmt, date=date.today(), commodity=client.def_commodity, round_price=round_price )
def NewConnectEntry(settings=None, global_list=None, client=None, project=None): """Return ConnectEntry according to settings.""" is_settings = type(settings) is Settings is_client = type(client) is Client if not is_settings or not is_client: return ConnectEntry() # 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].connectentry_title.format_map(replace_dict) comment = settings.defaults[lang].connectentry_comment.format_map(replace_dict) # get other values quantity = settings.defaults[lang].get_connectentry_quantity() quantity_format = settings.defaults[lang].connectentry_quantity_format is_time = settings.defaults[lang].get_connectentry_is_time() multiplicator = settings.defaults[lang].get_connectentry_multiplicator() # return entry with default values from settings default return ConnectEntry( title=title, comment=comment, quantity=quantity, quantity_format=quantity_format, is_time=is_time, multiplicator=multiplicator )
def export_to_openoffice(self, client=None, project=None, global_list=None, settings=None, template=None, file=None): """Export the offer to an open office file with the secretary module.""" try: import secretary except Exception: return False is_client = check_objects.is_client(client) is_project = check_objects.is_project(project) is_list = check_objects.is_list(global_list) is_settings = check_objects.is_settings(settings) is_template = os.path.isfile(template) one_not_correct = (not is_client or not is_project or not is_list or not is_settings or not is_template) # cancel if one argument is not valid if one_not_correct: return False # cancel if template does not exist if not os.path.isfile(template): return False # get replacement dict replace_me = replacer(settings=settings, global_list=global_list, client=client, project=project, offerinvoice=self) # get extension from template template_name, template_ext = os.path.splitext(template) # get accordingly extension for filename if template_ext == '.ott' or template_ext == '.odt': file_ext = '.odt' elif template_ext == '.ots' or template_ext == '.ods': file_ext = '.ods' else: # cancel if template is no openoffice template return False # get filename with replaced values, if file not empty if file != '': file = file.format_map(replace_me).replace(' ', '_') # use the offer title as filename else: file = self.title.format_map(replace_me).replace(' ', '_') # check if output file exists and alter the name then file_num = 2 file_new = file + file_ext while os.path.isfile(file_new): file_new = file + '_' + str(file_num) + file_ext file_num += 1 file = file_new # replace the replacer for x in replace_me.keys(): replace_me[x] = str(replace_me[x]).format_map(replace_me) # get entries entries = [] position = 0 for e in self._entry_list: position += 1 time = e.get_time(entry_list=self._entry_list) price = e.get_price(entry_list=self._entry_list, wage=self.get_wage(project=project), round_price=self._round_price) price_unit = e.get_unit_price(entry_list=self._entry_list, wage=self.get_wage(project=project), round_price=self._round_price) tax = e.get_price_tax(entry_list=self._entry_list, wage=self.get_wage(project=project), round_price=self._round_price) tax_unit = e.get_unit_price_tax( entry_list=self._entry_list, wage=self.get_wage(project=project), round_price=self._round_price) total = price + tax total_unit = price_unit + tax_unit if type(e) is MultiplyEntry: wage_add = str(e.get_wage_add()) wage_explain = e.wage_add_explain else: wage_add = '0' wage_explain = '' tmp_replacer = ReplacementDict({ 'E_POSITION': position, 'E_TITLE': e.title, 'E_COMMENT': e.comment, 'E_TIME': e.get_time(entry_list=self._entry_list), 'E_QUANTITY': e.get_quantity_str(), 'E_QUANTITY_B': e.get_quantity_b_str(), 'E_PRICE': '{} {}'.format(price, replace_me['COMMODITY']), 'E_UNIT_PRICE': '{} {}'.format(price_unit, replace_me['COMMODITY']), 'E_PRICE_TAX': '{} {}'.format(tax, replace_me['COMMODITY']), 'E_UNIT_PRICE_TAX': '{} {}'.format(tax_unit, replace_me['COMMODITY']), 'E_TOTAL': '{} {}'.format(total, replace_me['COMMODITY']), 'E_UNIT_TOTAL': '{} {}'.format(total_unit, replace_me['COMMODITY']), 'E_TAX_PERCENT': '{}'.format(round(e.get_tax_percent())), 'E_HAS_TAX': (tax > 0), 'E_WAGE_ADD': '{} {}/h'.format(wage_add, replace_me['COMMODITY']), 'E_WAGE_EXPLAIN': wage_explain }) title = e.title.format_map(replace_me) title = title.format(**tmp_replacer) comment = e.comment.format_map(replace_me) comment = comment.format(**tmp_replacer) entries.append({ 'POSITION': position, 'TITLE': title, 'COMMENT': comment, 'TIME': time, 'QUANTITY': e.get_quantity_str(), 'QUANTITY_B': e.get_quantity_b_str(), 'PRICE': '{} {}'.format(price, replace_me['COMMODITY']), 'UNIT_PRICE': '{} {}'.format(price_unit, replace_me['COMMODITY']), 'PRICE_TAX': '{} {}'.format(tax, replace_me['COMMODITY']), 'UNIT_PRICE_TAX': '{} {}'.format(tax_unit, replace_me['COMMODITY']), 'TOTAL': '{} {}'.format(total, replace_me['COMMODITY']), 'UNIT_TOTAL': '{} {}'.format(total_unit, replace_me['COMMODITY']), 'TAX_PERCENT': '{}'.format(round(e.get_tax_percent())), 'HAS_TAX': (tax > 0) }) # final rendering engine = secretary.Renderer() # try to replace stuff in the template try: result = engine.render(template, entries=entries, data=replace_me) output = open(file, 'wb') output.write(result) output.close() return True except Exception: print('TEMPLATE PROBABLY HAS INVALID VARIABLES') return False
def PresetInvoice( invoice_preset=None, settings=None, global_list=None, client=None, project=None, replace=False ): """Return new Invoice based on given invoice, but with string replacements.""" if type(invoice_preset) is not Invoice: return NewInvoice( settings=settings, global_list=global_list, client=client, project=project ) # get replaces replace_dict = replacer( settings=settings, global_list=global_list, client=client, project=project, offerinvoice=invoice_preset ) title = invoice_preset.title.format_map(replace_dict) id = invoice_preset.id.format_map(replace_dict) comment = invoice_preset.comment.format_map(replace_dict) comment_b = invoice_preset.comment_b.format_map(replace_dict) delivery = invoice_preset.delivery.format_map(replace_dict) ledger_comment = invoice_preset.ledger_comment.format_map(replace_dict) # dates inv_date = invoice_preset.get_date() if not replace and inv_date is not None: inv_date = date.today() elif replace and inv_date is None: inv_date = date.today() due_days = invoice_preset.get_due_days() paid_date = invoice_preset.get_paid_date() if not replace and paid_date is not None: paid_date = inv_date # get other values date_fmt = invoice_preset.date_fmt wage = invoice_preset.get_wage() commodity = invoice_preset.commodity round_price = invoice_preset.get_round_price() entry_list = invoice_preset.get_entry_list() # return new Invoice object return Invoice( title=title, id=id, comment=comment, comment_b=comment_b, date_fmt=date_fmt, date=inv_date, delivery=delivery, due_days=due_days, paid_date=paid_date, wage=wage, commodity=commodity, round_price=round_price, entry_list=entry_list, ledger_comment=ledger_comment )