aboutsummaryrefslogtreecommitdiff
path: root/ql/types/money.py
diff options
context:
space:
mode:
authorBicker <bickerkards@tuta.io>2019-05-22 17:45:37 +0200
committerBicker <bickerkards@tuta.io>2019-05-22 17:45:37 +0200
commit25314fd2aed81aa5281bc3d93dbcc7919368ebe1 (patch)
tree1995521ee652e419926de5fced0f2e3b655e70fb /ql/types/money.py
Initial commitHEADmaster
Diffstat (limited to 'ql/types/money.py')
-rw-r--r--ql/types/money.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/ql/types/money.py b/ql/types/money.py
new file mode 100644
index 0000000..edac58f
--- /dev/null
+++ b/ql/types/money.py
@@ -0,0 +1,76 @@
+from gui.widgets.money_spinbox import MoneySpinbox
+from ql.ast.nodes.expressions.literals.money_literal import MoneyNode
+from ql.types.boolean import QLBoolean
+from ql.types.type import QLType
+
+
+class QLMoney(QLType):
+ def __init__(self, value=0.0, currency='$'):
+ super(QLMoney, self).__init__()
+ self.__value = float(value)
+ self.__currency = currency
+
+ @property
+ def value(self):
+ return self.__value
+
+ @property
+ def currency(self):
+ return self.__currency
+
+ def __repr__(self):
+ return '{}{:.2f}'.format(self.currency, self.value)
+
+ def __str__(self):
+ return '{}{:.2f}'.format(self.currency, self.value)
+
+ def __neg__(self):
+ return QLMoney(- self.value, self.currency)
+
+ def __eq__(self, other):
+ if isinstance(other, QLMoney):
+ return QLBoolean(self.value == other.value and self.currency == other.currency)
+
+ return QLBoolean(False)
+
+ def __ne__(self, other):
+ return QLBoolean(not self == other)
+
+ def __lt__(self, other):
+ return QLBoolean(self.value < other.value and self.currency == other.currency)
+
+ def __gt__(self, other):
+ return QLBoolean(self.value > other.value and self.currency == other.currency)
+
+ def __le__(self, other):
+ return QLBoolean(self.value <= other.value and self.currency == other.currency)
+
+ def __ge__(self, other):
+ return QLBoolean(self.value >= other.value and self.currency == other.currency)
+
+ def __add__(self, other):
+ if self.currency == other.currency:
+ return QLMoney(self.value + other.value, self.currency)
+ return NotImplemented
+
+ def __sub__(self, other):
+ if self.currency == other.currency:
+ return QLMoney(self.value - other.value, self.currency)
+ return NotImplemented
+
+ def __mul__(self, other):
+ return other * self
+
+ def __truediv__(self, other):
+ return other / self
+
+ def get_json_value(self):
+ return {'value': round(self.value, 2), 'currency': self.currency}
+
+ @staticmethod
+ def get_literal_node(value):
+ return MoneyNode(None, QLMoney, value)
+
+ @staticmethod
+ def pyqt5_default_widget():
+ return MoneySpinbox()