Procházet zdrojové kódy

[revo] Support fee

Innokentiy Enikeev před 3 roky
rodič
revize
a3dbe1335a
3 změnil soubory, kde provedl 21 přidání a 7 odebrání
  1. 1 0
      common.lisp
  2. 14 7
      plugins/revolut.lisp
  3. 6 0
      utils.lisp

+ 1 - 0
common.lisp

@@ -77,6 +77,7 @@
            :xml-request
            :get-by-tag
            :select-text
+           :select-attr
            :trim-nil
            :text-with-cdata
            :unspacify

+ 14 - 7
plugins/revolut.lisp

@@ -22,7 +22,8 @@
         ("health" . "expenses:life:health")))
 
 (defvar *topup-accounts*
-      '(("Top-Up by *1508" . "assets:rbcz:czk")))
+  '(("Top-Up by *1508" . "assets:rbcz:czk")))
+(defvar *fee-account* "expenses:banking:fee")
 
 (defun is-content (rest-method)
   (case rest-method
@@ -73,7 +74,7 @@
                                       :|tokenId| (agets sign-res "tokenId"))
             when (listp res) do (return res)
             unless (= (dex:response-status res) 422) do (return)
-            when (> (- (get-universal-time) start) 600) do (return)
+            when (> (- (get-universal-time) start) 30) do (return)
             do (sleep 2)))))
 
 (defun refresh-token (user-id refresh-code)
@@ -254,7 +255,7 @@
 
 (defun params-fee (tr pockets)
   (let* ((description (agets tr "description"))
-         (rcv-account "expenses:banking:fee")
+         (rcv-account *fee-account*)
          (rcv-amount (* -1 (get-amount (agets tr "amount"))))
          (rcv-currency (agets tr "currency"))
          (snd-account (format-pocket-account (find-pocket (agets tr "account" "id")
@@ -300,7 +301,8 @@
 (defun transaction->entry (tr pockets)
   (let* ((date (get-date (agets tr "startedDate")))
          (type (keyify (agets tr "type")))
-	 (state (keyify (agets tr "state"))))
+	 (state (keyify (agets tr "state")))
+	 (fee (get-amount (or (agets tr "fee") 0))))
     (case state ((:declined :failed :cancelled :reverted :deleted)
 		 (return-from transaction->entry)))
     (multiple-value-bind (description comment
@@ -319,7 +321,7 @@
          :date date
          :description description
          :comment comment
-         :postings (list
+         :postings (remove nil (list
                     (pta-ledger:make-posting
                      :account (or rcv-account (default-account rcv-currency))
                      :amount (pta-ledger:make-amount
@@ -329,11 +331,16 @@
                                     (pta-ledger:make-amount
                                      :quantity (abs snd-amount)
                                      :commodity snd-currency)))
+		    (unless (zerop fee)
+		      (pta-ledger:make-posting
+		       :account *fee-account*
+		       :amount (pta-ledger:make-amount
+				:quantity fee :commodity snd-currency)))
                     (pta-ledger:make-posting
                      :account (or snd-account (default-account snd-currency))
                      :amount (pta-ledger:make-amount
-                              :quantity snd-amount
-                              :commodity snd-currency))))))))
+                              :quantity (- snd-amount fee)
+                              :commodity snd-currency)))))))))
 
 (defun handle-auth (login pass)
   (handler-case

+ 6 - 0
utils.lisp

@@ -57,6 +57,7 @@
            :xml-request
            :get-by-tag
            :select-text
+	   :select-attr
            :trim-nil
            :text-with-cdata
            :unspacify
@@ -319,6 +320,11 @@ is replaced with replacement."
                     :test #'plump:comment-p)
     (plump:text (plump:strip node))))
 
+(defun select-attr (node attr &optional selector)
+  (ignore-errors
+   (when selector (setf node (elt (clss:select selector node) 0)))
+   (plump:get-attribute node attr)))
+
 (defun trim-nil (text)
   (when text
     (let ((text (string-trim " " text)))