def test_calculate_simple_interest2(self): print("test_calculate_simple_interest2") apr = Decimal("48.74") capital = Decimal("500.00") et_capital = EntryType.objects.get(code=E_CAPITAL) entries = [ AccountEntry(type=et_capital, amount=capital, timestamp=make_datetime(2017, 1, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 3, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 5, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 7, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 9, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 11, 1)), ] timestamp = make_datetime(2020, 1, 1) interest = calculate_simple_interest(entries, apr, timestamp.date()) print("interest =", dec2(interest)) self.assertEqual(interest.quantize(Decimal("1.00")), Decimal("426.11"))
def test_calculate_simple_interest(self): print('test_calculate_simple_interest') apr = Decimal('48.74') capital = Decimal('500.00') et_capital = EntryType.objects.get(code=E_CAPITAL) entries = [ AccountEntry(type=et_capital, amount=capital, timestamp=make_datetime(2017, 1, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 3, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 5, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 7, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 9, 1)), AccountEntry(type=et_capital, amount=Decimal(-50), timestamp=make_datetime(2017, 11, 1)), AccountEntry(type=et_capital, amount=Decimal('-437.50'), timestamp=make_datetime(2018, 1, 1)), ] timestamp = make_datetime(2018, 1, 1) interest = calculate_simple_interest(entries, apr, timestamp.date()) print('interest =', dec2(interest)) self.assertEqual(interest.quantize(Decimal('1.00')), Decimal('182.41'))
def test_calculate_simple_interest3(self): print("test_calculate_simple_interest3") apr = Decimal("3.00") capital = Decimal("500.00") et_capital = EntryType.objects.get(code=E_CAPITAL) entries = [ AccountEntry(type=et_capital, amount=capital, timestamp=make_datetime(2018, 1, 10)), ] interest = calculate_simple_interest(entries, apr, date(2018, 3, 1), begin=date(2018, 2, 10)) print("interest =", dec2(interest)) self.assertEqual(interest.quantize(Decimal("1.00")), Decimal("0.78"))
def camt053_get_currency(data: dict, key: str, required: bool = True, name: str = "") -> Tuple[Optional[Decimal], str]: try: v = camt053_get_val(data, key, default=None, required=False, name=name) if v is not None: amount = dec2(v["@"]) currency_code = v["@Ccy"] return amount, currency_code except Exception: pass if required: raise ValidationError( _("camt.053 field {} type {} missing or invalid").format( name, "currency")) return None, ""
def add_reverse_charge(modeladmin, request, qs): assert hasattr(modeladmin, "reverse_charge_form") assert hasattr(modeladmin, "reverse_charge_template") cx: Dict[str, Any] = {} try: if qs.count() != 1: raise ValidationError( _("Exactly one account entry must be selected")) e = qs.first() assert isinstance(e, AccountEntry) if e.amount is None or dec2(e.amount) == Decimal("0.00"): raise ValidationError( _("Exactly one account entry must be selected")) form_cls = modeladmin.reverse_charge_form # Type: ignore initial = { "amount": -e.amount, "description": form_cls().fields["description"].initial, } if e.description: initial["description"] += " / {}".format(e.description) cx = { "qs": qs, "original": e, } if "save" in request.POST: cx["form"] = form = form_cls(request.POST, initial=initial) if not form.is_valid(): raise ValidationError(form.errors) timestamp = form.cleaned_data["timestamp"] amount = form.cleaned_data["amount"] description = form.cleaned_data["description"] reverse_e = clone_model( e, parent=e.parent, amount=amount, description=description, timestamp=timestamp, commit=False, created=now(), ) reverse_e.full_clean() reverse_e.save() messages.info(request, "{} {}".format(reverse_e, _("created"))) else: cx["form"] = form = form_cls(initial=initial) return render(request, modeladmin.reverse_charge_template, context=cx) except ValidationError as e: if cx: return render(request, modeladmin.reverse_charge_template, context=cx) messages.error(request, "{}\n".join(e.messages)) except Exception as e: logger.error("add_reverse_charge: %s", traceback.format_exc()) messages.error(request, "{}".format(e)) return None