def execute(run_backup: bool = True): """ Stock update """ if run_backup: backup.execute() assets = imp_asset.get_assets() for asset in assets["assets"]: if asset["type"] != "STOCK": continue if "url_suffix" not in asset: continue if asset["url_suffix"] == "": continue try: url = config.CONSTANTS["STOCK_URL"] + asset["url_suffix"] resp = requests.get(url, verify=False) str_val = _find_between(resp.text, '<span class="value">', '</span>') str_val = str_val.replace(".", "").replace(",", ".") asset["sales_value"] = float(str_val) except Exception as ex: print(str(ex)) print(f"Can't update stock {asset['name']}") imp_asset.set_assets(assets)
def _delete_click(self): deletable_assets = self._selected_assets if len(deletable_assets) == 0: return deletable_guids = [] for act in deletable_assets: deletable_guids.append(act["guid"]) backup.execute() asset.delete_assets(deletable_guids) self._fill_tree_with_assets()
def execute(self): """ Executes all updates """ if self._executed: return backup.execute() currency_update.execute(run_backup=False) stock_update.execute(run_backup=False) crypto_update.execute(run_backup=False) # Commodities are not updated because it is slow. # Update commodities via menu if needed self._executed = True
def _delete_click(self): deletable_activities = self._selected_activities if len(deletable_activities) == 0: return deletable_guids = [] for act in deletable_activities: deletable_guids.append(act.guid) backup.execute() Activity.delete_activities(deletable_guids) self._fill_tree_with_activities() PrimeSingleton.get().refresh()
def _delete_click(self): deletable_invoices = self._selected_invoices if len(deletable_invoices) == 0: return deletable_guids = [] for inv in deletable_invoices: deletable_guids.append(inv.guid) backup.execute() Invoice.delete_invoices(deletable_guids) self._fill_tree_with_invoices() PrimeSingleton.get().refresh()
def _delete_click(self): deletable_payments = self._get_selected_payments() if len(deletable_payments) == 0: return deletable_guids = [] for pay in deletable_payments: deletable_guids.append(pay.guid) backup.execute() payment.delete_payments(deletable_guids) self._fill_tree_with_payments() PrimeSingleton.get().refresh()
def execute(run_backup: bool = True): """ Crypto update """ if run_backup: backup.execute() btc_dict = json.loads( requests.get(config.CONSTANTS["BTC_URL"], verify=False).text) assets = imp_asset.get_assets() for asset in assets["assets"]: if asset["name"] == "BTC": asset["sales_value"] = btc_dict["bpi"]["USD"]["rate_float"] asset["currency"] = "USD" imp_asset.set_assets(assets)
def execute(run_backup: bool = True): """ Runs currency update """ global _EXECUTED_TODAY if _EXECUTED_TODAY: return _EXECUTED_TODAY = True try: # Backup if run_backup: backup.execute() # Currencies from TCMB resp = requests.get(config.CONSTANTS["CURRENCY_CONV_URL"], verify=False, timeout=5) resp_as_dict = xmltodict.parse(resp.text) # Gold conversion gold_resp = requests.get(config.CONSTANTS["CURRENY_GOLD_URL"], verify=False, timeout=5) pos1 = gold_resp.text.find('<table class="table table-striped">') + 113 gold_price_txt = gold_resp.text[pos1:pos1 + 7].replace("<", "").replace( ",", ".") gold_price = float(gold_price_txt.replace("/", "")) dgc_dict = { "@CrossOrder": "0", "@Kod": "DGC", "@CurrencyCode": "DGC", "Unit": "1", "Isim": "Gold", "CurrencyName": "Gold", "ForexBuying": gold_price, "ForexSelling": gold_price, "BanknoteBuying": gold_price, "BanknoteSelling": gold_price } resp_as_dict["Tarih_Date"]["Currency"].append(dgc_dict) # Save currency.save_currency_conv(resp_as_dict) except Exception as error: print(f"Currency update error: {str(error)}")
def test_mode(): """ Activates test mode """ global CONSTANTS, TEST_MODE TEST_MODE = True backup_dir = backup.execute() CONSTANTS["DATA_DIR_PATH"] = backup_dir + "/" CONSTANTS["UPDATE_ON_STARTUP"] = False CONSTANTS["UPDATE_ON_REPORT"] = False
def _execute(asset_guid: str = None): """ Stock update """ backup.execute() assets = imp_asset.get_assets() random.shuffle(assets["assets"]) first_asset = True for asset in assets["assets"]: if asset["type"] != "COMMODITY": continue if "url_suffix" not in asset: continue if asset["url_suffix"] == "": continue if asset_guid is not None and asset["guid"] != asset_guid: continue if first_asset: first_asset = False else: time.sleep(config.CONSTANTS["COMMODITY_SEARCH_SLEEP"]) IncubusFactory.get_instance().user_event() try: url = config.CONSTANTS["COMMODITY_URL"] + asset["url_suffix"] search_input = SearchInput( url=url, post_sleep=config.CONSTANTS["COMMODITY_PAGE_SLEEP"]) quick_median = QuickMedian(search_input) asset["sales_value"] = \ conv_last_usd_val_to_home_curr(asset) \ if quick_median.median == 0 \ else quick_median.median except Exception as update_error: print(f"Commodity search error: { str(update_error) } ") imp_asset.set_assets(assets)
def record_cash_movement(company: str, direction: str, amount: float, currency: str, description: str, income_tax_only: bool = False): """ Records a new cash movement """ ############################## # Preparation ############################## backup.execute() changed_payments = [] curr_conv = CurrencyConverter() open_amount = amount open_payments = get_open_payments_of_company(company) date_iso = datetime.datetime.now().isoformat() ############################## # Deduct from open payments ############################## for payment in open_payments: if open_amount <= 0: break if payment.direction != direction: continue if income_tax_only and (not payment.is_income_tax): continue payment.generate_very_long_term_recurrences() scheme = payment.scheme recurrences = scheme.recurrences for recurrence in recurrences: if open_amount <= 0: break if not recurrence.cleared: rec_open_amount, rec_curr = recurrence.open_amount open_amount_conv = curr_conv.convert_to_currency( from_amount=open_amount, from_currency=currency, to_currency=rec_curr) if open_amount_conv >= rec_open_amount: coll = { "date": date_iso, "description": description, "amount": rec_open_amount, "currency": rec_curr } recurrence.add_collection(Collection(coll)) recurrence.cleared = True remain_amount = open_amount_conv - rec_open_amount open_amount = open_amount * (remain_amount / open_amount_conv) else: coll = { "date": date_iso, "description": description, "amount": open_amount_conv, "currency": rec_curr } recurrence.add_collection(Collection(coll)) open_amount = 0 scheme.recurrences = recurrences payment.scheme = scheme if payment.open_amount[0] <= 0: payment.cleared = True changed_payments.append(payment) ############################## # Overpayment? Need to be paid back ############################## if open_amount > 0: if direction == DIRECTION_IN: pay_back_dir = DIRECTION_OUT else: pay_back_dir = DIRECTION_IN pay_back_dict = { "creation_date": date_iso, "company": company, "description": description + " - fazla ödeme iade", "direction": pay_back_dir, "amount": open_amount, "currency": currency, "cleared": False, "scheme": { "frequency": 1, "period": "D", "start": date_iso, "repeat": 1, "recurrence": [] } } changed_payments.append(Payment(pay_back_dict)) ############################## # Finish ############################## for payment in changed_payments: payment.save()
def _backup_data(self): IncubusFactory.get_instance().user_event() self._set_status("Backing up") backup.execute() self._set_status("Backup complete")