aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--elisp/grocy.el64
1 files changed, 16 insertions, 48 deletions
diff --git a/elisp/grocy.el b/elisp/grocy.el
index 923d36d..c5ec6d6 100644
--- a/elisp/grocy.el
+++ b/elisp/grocy.el
@@ -27,6 +27,7 @@
(defvar grocy-units)
(defvar grocy-locations)
+(defvar grocy-product-groups)
(defcustom grocy-server ""
"Grocy main domain."
@@ -51,55 +52,22 @@ Set METHOD to other request types and include the already encoded DATA."
(concat grocy-server "/api/" endpoint)
callback)))
-(defun grocy-update-units ()
- "Update localdb with units."
+(defun grocy-update-info (endpoint save-var &rest vars)
+ "Update SAVE-VAR with information from ENDPOINT with the values of VARS."
(grocy--request
- "objects/quantity_units"
+ (format "objects/%s" endpoint)
(lambda (_status)
(goto-char url-http-end-of-headers)
- (let ((result (make-hash-table)))
- (mapc
- (lambda (unit)
- (-let (((&hash "id" "name" "name_plural") unit))
- (puthash id
- (cons name name_plural)
- result)))
- (json-parse-buffer))
- (setq grocy-units result)))))
+ (clrhash save-var)
+ (mapc
+ (lambda (entry)
+ (let ((data (mapcar (lambda (v) (gethash v entry)) vars)))
+ (puthash (gethash "id" entry) (if (length= data 1) (car data) data) save-var)))
+ (json-parse-buffer)))))
-(defun grocy-update-locations ()
- "Update localdb with locations."
- (grocy--request
- "objects/locations"
- (lambda (_status)
- (goto-char url-http-end-of-headers)
- (let ((result (make-hash-table)))
- (mapc
- (lambda (location)
- (-let (((&plist :id) location))
- (puthash id (cdddr location)
- result)))
- (json-parse-buffer :object-type 'plist))
- (setq grocy-locations result)))))
-
-(defun grocy-update-product-groups ()
- "Update localdb with product groups."
- (grocy--request
- "objects/product_groups"
- (lambda (_status)
- (goto-char url-http-end-of-headers)
- (let ((result (make-hash-table)))
- (mapc
- (lambda (groups)
- (-let (((&plist :id :name) groups))
- (puthash id name result)))
- (json-parse-buffer :object-type 'plist))
- (setq grocy-product-groups result)))))
-
-
-(grocy-update-units)
-(grocy-update-locations)
-(grocy-update-product-groups)
+(grocy-update-info "quantity_units" grocy-units "name" "name_plural")
+(grocy-update-info "locations" grocy-locations "name")
+(grocy-update-info "product_groups" grocy-product-groups "name")
(defun grocy-stock--row (row)
"Compose ROW into tabulated entry for stock view."
@@ -109,7 +77,7 @@ Set METHOD to other request types and include the already encoded DATA."
(concat (number-to-string amount)
" "
(funcall
- (if (= 1 amount) #'car #'cdr)
+ (if (= 1 amount) #'car #'cadr)
(gethash qu_id_stock grocy-units))
(when (< 0 amount_opened)
(concat " / "
@@ -146,7 +114,7 @@ Set METHOD to other request types and include the already encoded DATA."
(prod-group (gethash product_group_id grocy-product-groups)))
(list product
(vector name
- (car (gethash location_id grocy-locations))
+ (gethash location_id grocy-locations)
(car (gethash qu_id_stock grocy-units))
(number-to-string min_stock_amount)
(if prod-group prod-group "NULL" )))))
@@ -253,7 +221,7 @@ Set METHOD to other request types and include the already encoded DATA."
"C-c C-c"
(grocy-push-buffer-and-kill-fn "objects/products" "POST"))
(insert "(\n:name \n;; ")
- (maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-locations)
+ (maphash (lambda (key val) (insert (format "%d:'%s' " key val))) grocy-locations)
(insert "\n:location_id \n;; ")
(maphash (lambda (key val) (insert (format "%d:'%s' " key (car val)))) grocy-units)
(insert "\n:qu_id_stock 3