-
Notifications
You must be signed in to change notification settings - Fork 0
/
foodstats.lisp
4603 lines (4462 loc) · 125 KB
/
foodstats.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Copyright 2022 ultasun. All rights reserved.
;; See the LICENSE file. Thank you for reading!
;; -----------------------------------------------------------------------------
;; a system for computing Nutrition Facts totals, given a list of UPC's.
(defpackage :foodstats
(:use "COMMON-LISP")
(:import-from "COMMON-LISP-USER" "QUIT"))
(in-package :foodstats)
;; -----------------------------------------------------------------------------
;; the following definitions pertain to re-usable components which are not
;; specific to this package. so they could be copy/pasted somewhere else and
;; would serve a purpose without issue.
;; for menu option 4
;; helper-flatten will flatten a tree by one level. for example:
;; [1]> (in-package :foodstats)
;; #<PACKAGE FOODSTATS>
;; FOODSTATS[2]> (helper-flatten '((((1) (2)) ((3) (4))) (((5) (6)) ((7) (8)))))
;; (((1) (2)) ((3) (4)) ((5) (6)) ((7) (8)))
;; FOODSTATS[3]> (helper-flatten *)
;; ((1) (2) (3) (4) (5) (6) (7) (8))
;; FOODSTATS[4]> (helper-flatten *)
;; (1 2 3 4 5 6 7 8)
;; FOODSTATS[5]>
(defun helper-flatten (l &optional (result '()))
"Flatten a tree by one level. Returns a list."
(if
l
(helper-flatten (cdr l) (append result (car l)))
result))
(defun get-value-or-zero (value)
"Returns the passed value, or a zero. "
(if value value 0))
(defun caddddr (list)
"The cadddr of the cdr of the passed list."
(cadddr (cdr list)))
;; -----------------------------------------------------------------------------
;; the following definitions pertain to handling dimensional analysis.
;; raw quantity conversion function.
(defun pounds-quantity-to-ounces-quantity (pounds)
"Returns the quantity, multiplied by sixteen."
(* pounds 16))
;; raw quantity conversion function.
(defun ounces-quantity-to-grams-quantity (ounces)
"Returns the quantity, multiplied by 28.3495."
(* ounces 28.3495))
;; convert pounds to ounces
(defun pounds-dimensional-unit-to-ounces-dimensional-unit (pounds)
"Convert a pounds dimensional unit, to an ounces dimensional unit.
Returns a cons cell."
(if
(eq (cdr pounds) 'pounds)
(cons (pounds-quantity-to-ounces-quantity (car pounds)) 'ounces)
nil))
;; convert ounces to grams
(defun ounces-dimensional-unit-to-grams-dimensional-unit (ounces)
"Convert an ounces dimensional unit, to a grams dimensional unit.
Returns a cons cell."
(if
(eq (cdr ounces) 'ounces)
(cons (ounces-quantity-to-grams-quantity (car ounces)) 'grams)
nil))
;; convert pounds to grams.
(defun pounds-dimensional-unit-to-grams-dimensional-unit (pounds)
"Convert a pounds dimensional unit, to a grams dimensional unit.
Returns a cons cell."
(ounces-dimensional-unit-to-grams-dimensional-unit
(pounds-dimensional-unit-to-ounces-dimensional-unit pounds)))
;; the "base unit" which calculations need to be performed in, is grams;
(defun any-dimensional-unit-to-grams-dimensional-unit (dimensional-unit)
"Convert any dimensional-unit to grams, if possible. Returns a cons cell."
(let
((weight-in-grams 0))
(cond
((eq (cdr dimensional-unit) 'pounds)
(setf weight-in-grams
(pounds-dimensional-unit-to-grams-dimensional-unit
dimensional-unit)))
((eq (cdr dimensional-unit) 'ounces)
(setf weight-in-grams
(ounces-dimensional-unit-to-grams-dimensional-unit
dimensional-unit)))
((eq (cdr dimensional-unit) 'grams)
dimensional-unit)
((eq (cdr dimensional-unit) 'calories)
dimensional-unit)
(t nil))))
(defun dimensional-unit-p (attribute)
"Predicate test to detect dimensional unit."
(and
attribute ;; nil check
(listp attribute)
(or
(plusp (car attribute)) ;; the scalar value is greater than
(zerop (car attribute))) ;; or equal to zero
(symbolp (cdr attribute)))) ;; the unit
;; only works if any-dimensional-unit-to-grams-dimensional-unit works
(defun add-two-dimensional-units (du1 du2)
"Only works with weights which can be resolved to grams.
Adds two dimensional units, returns a dimensional unit in grams."
(let
((du1-grams
(any-dimensional-unit-to-grams-dimensional-unit du1))
(du2-grams
(any-dimensional-unit-to-grams-dimensional-unit du2)))
(let
((result-scalar
(+
(car du1-grams)
(car du2-grams)))
(result-unit
'grams))
(cons result-scalar result-unit))))
(defun subtract-two-dimensional-units (du1 du2)
"Similar to add-two-dimensional-units, but performs a subtraction."
(let
((du1-grams
(any-dimensional-unit-to-grams-dimensional-unit du1))
(du2-grams
(any-dimensional-unit-to-grams-dimensional-unit du2)))
(let
((result-scalar
(-
(car du1-grams)
(car du2-grams)))
(result-unit
'grams))
(cons result-scalar result-unit))))
;; -----------------------------------------------------------------------------
;; the following definitions pertain to the UPC, history and inventory
;; association lists -- `get`ing particular queries, used by format functions.
;; the UPC database `alist-upc`
;; * is an association list, keyed by a UPC (as a string),
;; and whose value is a cons cell holding the dimensional unit.
;; the history list `alist-history`
;; * is indexed by universal-seconds, valued by a upc list.
;; the inventory list `alist-inventory`
;; * is indexed by upc (from `alist-upc`)
;; and valued by an alist keyed by a date string, valued by a quantity.
;; * for example,
(defvar +alist-inv-example+
'(("541359874631" . (("19-AUG-2023" . (("2.99" . 9)))
("22-JUN-2026" . (("3.99" . 11)))))
("665434455782" . (("21-SEP-2027" . (("2.39" . 5)))))))
(defvar +alist-consumption-cost-example+
'((3860000000 . (("688267027925" ("0.89" . 1))))))
;; returns the association list of expiration date to price alist
(defun get-inventory-date-group (alist-inventory upc)
"Returns the alist of expiration date groups for a upc from alist-inventory."
(cdr (assoc upc alist-inventory :test #'equalp)))
;; returns the association list of price to quantity
(defun get-inventory-price-group (alist-inventory upc date)
"Returns the alist of prices for an expiration date group in alist-inventory."
(cdr
(assoc
date
(get-inventory-date-group alist-inventory upc)
:test #'equalp)))
;; answers the question, "how many of this UPC do I have"?
(defun get-inventory-total-quantity (alist-inventory upc)
"Returns a positive integer, or zero if the UPC doesn't exist (or is 0)."
(apply
#'+
(mapcar
(lambda
(this-expiration-group)
(apply
#'+
(mapcar
(lambda
(this-price-group)
(cdr this-price-group))
(get-inventory-price-group
alist-inventory
upc
(car this-expiration-group)))))
(get-inventory-date-group alist-inventory upc))))
;; answers the question, "how many of the UPC which expire on this day?"
(defun get-inventory-date-group-quantity (alist-inventory upc date)
"Returns a positive integer, or zero if the UPC/date doesn't exist (or is 0)."
(apply
#'+
(mapcar
(lambda
(this-price-group)
(cdr this-price-group))
(get-inventory-price-group
alist-inventory
upc
date))))
;; answers the question, "how many of the UPC which
;; cost this much and expire on this day?"
(defun get-inventory-price-group-quantity (alist-inventory upc date price)
"Returns a positive integer, or zero if it doesn't exist, or is 0."
(cdr
(assoc
price
(get-inventory-price-group alist-inventory upc date)
:test #'equalp)))
;; answers the question, "what is the last date group with a quantity > 0?"
(defun get-inventory-last-date-group
(alist-inventory
upc
&optional
(date-groups
(get-inventory-date-group
alist-inventory upc)))
"Recursively search the alist-inventory association list for a UPC with the most-recently-added date-group with an inventory quantity greater than zero."
(if
date-groups
(if
(>
(get-inventory-date-group-quantity
alist-inventory
upc
(caar (last date-groups)))
0)
(caar (last date-groups))
;; else, recursive call
(get-inventory-last-date-group
alist-inventory
upc
(butlast date-groups)))
nil))
;; answers the question, "what is the last price group with a quantity > 0?"
(defun get-inventory-last-price-group
(alist-inventory
upc
date
&optional
(price-groups
(get-inventory-price-group
alist-inventory upc date)))
"Recursively search the alist-inventory association list for the most-recently added price-group within a date-group with an inventory quantity greater than zero. "
(if
price-groups ;; needs to end the recursive search
(if
(>
(get-inventory-price-group-quantity
alist-inventory
upc
date
(caar (last price-groups)))
0)
(caar (last price-groups))
;; else, recursive call
(get-inventory-last-price-group
alist-inventory
upc
date
(butlast price-groups)))
nil))
;; get an attribute from a UPC.
(defun get-upc-attribute (alist-upc upc attribute)
"Return some attribute from a UPC."
(cdr (assoc attribute (cdr (assoc upc alist-upc :test #'equalp)))))
;; return all attributes for a UPC.
(defun get-upc-attributes (alist-upc upc)
(cdr (assoc upc alist-upc :test #'equalp)))
;; get an attribute from a UPC,
;; but only if that attribute is a dimensional-unit.
(defun get-dimensional-unit-from-upc-attribute (alist-upc upc attribute)
"Return some attribute's dimensional unit from a UPC. Example: (19 . grams).
Returns nil if the attribute doesn't exist, or isn't a dimensional-unit."
(let
((result (get-upc-attribute alist-upc upc attribute)))
(if
(dimensional-unit-p result)
result
nil)))
;; for menu option 4
;; there is likely to be a standard pattern somewhere on how to do this.
;; TODO find the correct pattern to reduce the footprint of this function
(defun get-upc-list-between-times (alist-history time-range)
"Given the universal-seconds range in the second argument,
search the first argument for all the UPC's which had been used
(within that interval.)"
(helper-flatten
(mapcar
(lambda
(this-time)
(if
(and (>= (car this-time) (car time-range))
(<= (car this-time) (cdr time-range)))
(cdr this-time)))
alist-history)))
(defun get-role-alias-upc-list (alist-role-alias role-alias)
"Given the association list mapping role-aliases to a list of UPC's which
fulfill that alias, and a role-alias to look up, return the list of UPC's which
are fulfilled by that role-alias."
(cdr (assoc role-alias alist-role-alias :test #'equalp)))
(defun get-role-alias-last-upc-ignore-inventory
(alist-role-alias
role-alias)
"Returns the last UPC assigned to a role-alias list, using the function
GET-ROLE-ALIAS-UPC-LIST to retrieve the entire role-alias UPC list."
(car (last (get-role-alias-upc-list alist-role-alias role-alias))))
(defun get-role-alias-first-upc-ignore-inventory
(alist-role-alias
role-alias)
"Returns the first UPC in the role-alias UPC list, using the function
GET-ROLE-ALIAS-UPC-LIST to retrieve the entire role-alias UPC list."
(first (get-role-alias-upc-list alist-role-alias role-alias)))
(defun get-role-alias-upc-has-inventory
(alist-upc
alist-inventory
alist-role-alias
role-alias
&optional
(minimum-quantity 1)
(search-list
(get-role-alias-upc-list
alist-role-alias
role-alias)))
"Returns nil if no UPC for this role alias has inventory.
Otherwise returns a string "
(if
search-list
(if
(>=
(get-inventory-total-quantity
alist-inventory
(car
search-list))
minimum-quantity)
(car search-list) ;; return this UPC
(get-role-alias-upc-has-inventory
alist-upc
alist-inventory
alist-role-alias
role-alias
minimum-quantity
(cdr search-list)))
nil))
(defun get-role-alias-upc-has-inventory-or-leftover
(alist-upc
alist-inventory
alist-role-alias
alist-upc-leftover
role-alias
&optional ;; possibly make minimum-quantity automatic from
(minimum-quantity 1) ;; the information in minimum-dimensional-unit
(minimum-dimensional-unit '(1 . grams)) ;; and alist-upc
(skip-list '())
(search-list
(get-role-alias-upc-list
alist-role-alias
role-alias)))
""
(if
search-list
(if ;; first check the leftovers
(and
(not
(member
(car search-list)
skip-list
:test #'equalp))
(assoc
(car search-list)
alist-upc-leftover
:test #'equalp)
(>=
(cadar
(calculate-recipe-dimensional-units-from-portion-percentages
alist-upc
(list
(assoc
(car search-list)
alist-upc-leftover
:test #'equalp))))
(car
(any-dimensional-unit-to-grams-dimensional-unit
minimum-dimensional-unit))))
(values
(car search-list) ;; return this leftover if it is suitable
(cdr
(assoc
(car search-list)
alist-upc-leftover
:test #'equalp)))
(if ;; "else if" check inventory for a new unit
(and
(not
(member
(car search-list)
skip-list
:test #'equalp))
(>=
(get-inventory-total-quantity
alist-inventory
(car
search-list))
minimum-quantity))
(car search-list) ;; return this UPC, else
(get-role-alias-upc-has-inventory-or-leftover ;; continue the search
alist-upc
alist-inventory
alist-role-alias
alist-upc-leftover
role-alias
minimum-quantity
minimum-dimensional-unit
skip-list
(cdr search-list))))
nil))
(defun get-random-upc (&optional (alist-upc '()) (length 12))
"Returns a random number of 'length', which is 12 by default.
Calls recursively until a non-matching UPC from alist-upc is found.
Returns a string."
(let
((candidate
(write-to-string
(+ (expt 10 (- length 1))
(random (expt 10 (- length 1)))))))
(if
(assoc candidate alist-upc :test #'equalp)
(get-random-upc alist-upc length)
candidate)))
;; returns the first UPC for the alias
;; alist-role-alias contains a LIFO queue for each role
;; this way, the next UPC is the freshest one, and not the oldest one
;;(defun get-next-upc-from-role-alias (alist-role-alias role-alias)
;; "LIFO queue for alist-role-history "
;; (car (get-role-alias-upc-list alist-role-alias role-alias)))
(defun get-recipe-upc-dimensional-unit-list
(alist-recipe
recipe-name)
"Returns the upc list as dimensional-units.
Returns an association list."
(cdr (assoc recipe-name alist-recipe :test #'equalp)))
(defun get-recipe-upc-percent-list-ignore-inventory
(alist-upc
alist-recipe
alist-role-alias
recipe-name)
""
(calculate-recipe-portion-percentages-from-dimensional-units
alist-upc
(convert-recipe-role-alias-keys-to-upc-keys-ignore-inventory
alist-upc
alist-recipe
alist-role-alias
(get-recipe-upc-dimensional-unit-list
alist-recipe
recipe-name))))
(defun get-upc-leftover-percent-or-zero
(alist-upc-leftover
upc)
"Return the leftover UPC or a zero."
(let
((upc-leftover
(cdr
(assoc
upc
alist-upc-leftover
:test #'equalp))))
(if
(numberp upc-leftover)
upc-leftover
0.0)))
(defun get-accumulated-cost-quantity-pairs
(alist-inventory
tier-one-key
tier-two-key)
""
(cdr
(assoc
tier-two-key
(cdr
(assoc
tier-one-key
alist-inventory
:test #'equalp))
:test #'equalp)))
(defun get-accumulated-cost-quantity-pair
(alist-inventory
tier-one-key
tier-two-key
tier-three-key)
""
(assoc
tier-three-key
(cdr
(assoc
tier-two-key
(cdr
(assoc
tier-one-key
alist-inventory
:test #'equalp))
:test #'equalp))
:test #'equalp))
;; get the original UPC list to cook something which is in alist-recipe-leftover
(defun get-upc-list-from-alist-recipe-leftover
(alist-recipe-leftover
recipe-name
recipe-cook-stamp)
""
(assoc
recipe-cook-stamp
(cdr (assoc recipe-name alist-recipe-leftover :test #'equalp))))
;; "how to use mapcar to search" (as opposed to list-eater search)
(defun get-role-alias-from-leftover
(alist-upc
alist-role-alias
alist-upc-leftover
role-alias
&optional
(minimum-dimensional-unit '(1 . grams)))
""
(mapcan
(lambda
(this-leftover)
(if
(member
(car this-leftover)
(get-role-alias-upc-list
alist-role-alias
role-alias)
:test #'equalp)
(if
(>=
(cadar
(calculate-recipe-dimensional-units-from-portion-percentages
alist-upc
(list
(assoc
(car this-leftover)
alist-upc-leftover
:test #'equalp))))
(car
(any-dimensional-unit-to-grams-dimensional-unit
minimum-dimensional-unit)))
(list this-leftover))))
alist-upc-leftover))
;; compliments get-role-alias-from-leftover, uses mapcar to search
(defun get-role-alias-from-inventory
(alist-upc
alist-inventory
alist-role-alias
role-alias
&optional
(minimum-dimensional-unit '(1 . grams)))
""
(mapcan
(lambda
(this-inventory-record)
(if
(member
(car this-inventory-record)
(get-role-alias-upc-list
alist-role-alias
role-alias)
:test #'equalp)
(let
((inventory-total-quantity
(* ;; multiply by 1.0 to convert to "percentage portion" which
1.0 ;; is in the range of [0, 1)+
(get-inventory-total-quantity
alist-inventory
(car this-inventory-record)))))
(if
(>=
(cadar
(calculate-recipe-dimensional-units-from-portion-percentages
alist-upc
(list
(cons
(car this-inventory-record)
inventory-total-quantity))))
(car
(any-dimensional-unit-to-grams-dimensional-unit
minimum-dimensional-unit)))
(list
(cons
(car this-inventory-record)
inventory-total-quantity))))))
alist-inventory))
;; return the percentage of a recipe remaining
(defun get-recipe-leftover-remaining-percentage
(alist-recipe-leftover
recipe-name
cook-stamp)
""
(cadr
(assoc
cook-stamp
(cdr
(assoc
recipe-name
alist-recipe-leftover
:test #'equalp)))))
;; this function is used to incrementally build an entire alist-upc database.
;; building alist-upc database 'by hand' is shown in `alist-upc.example.lisp`
;; and this file may be found in the `data` directory.
;;
;; note this is also used for building recipes, so it may need to be
;; renamed in the future to #'adjust-alist-attribute.
(defun adjust-alist-upc-attribute (alist-upc upc attribute)
"Create or Update an attribute for the given UPC.
The UPC will be created or updated as necessary.
Returns alist-upc, with the added or updated association list structure."
(acons
upc
(let
((upc-attributes
(cdr
(assoc upc alist-upc :test #'equalp))))
(append
(remove
(car attribute)
upc-attributes
:key #'car
:test #'equalp)
(list attribute)))
(remove upc alist-upc :key #'car :test #'equalp)))
;; this function is used to incrementally build the inventory database.
;; see +alist-inv-example+ for an illustration of the embedded alist structure.
;; alist-inventory may be built "by hand" as a let statement in the text file.
(defun adjust-alist-inventory (alist-inventory upc date price quantity)
"Add or update the UPC's quantity in an expiration group.
Returns a new alist-inventory."
(acons
upc
(acons
date
(acons
price
quantity
(remove
price
(get-inventory-price-group alist-inventory upc date)
:test #'equalp :key #'car))
(remove
date
(get-inventory-date-group alist-inventory upc)
:test #'equalp :key #'car))
(remove upc alist-inventory :key #'car :test #'equalp)))
;; update the list of UPCs which fulfill a certain role
(defun adjust-alist-role-alias (alist-role-alias upc role-alias)
"Add or update to the list of UPCs which fulfill a ceratin role.
Returns a new alist-alias-role."
(acons
role-alias
(append
(list upc)
(get-role-alias-upc-list alist-role-alias role-alias))
(remove
role-alias
alist-role-alias
:key #'car
:test #'equalp)))
(defun adjust-alist-percentage-list (alist-percentage-list key value)
(acons
key
value
(remove
key
alist-percentage-list
:key #'car :test #'equalp)))
;; used to update the percentage of a leftover recipe "cook" remaining
(defun adjust-alist-recipe-leftover-percentage
(alist-recipe-leftover
recipe-name
recipe-cook-stamp
new-percentage)
""
(acons
recipe-name
(acons
recipe-cook-stamp
(cons
new-percentage
(cddr
(assoc
recipe-cook-stamp
(cdr
(assoc
recipe-name
alist-recipe-leftover
:test #'equalp)))))
(remove
recipe-cook-stamp
(cdr
(assoc
recipe-name
alist-recipe-leftover
:test #'equalp))
:key #'car :test #'equalp))
(remove
recipe-name
alist-recipe-leftover
:key #'car :test #'equalp)))
;; take a recipe with role-alias entries and convert them to
;; upc entries but only if there is inventory for those UPC's
;; does not care if the list is of percentage-portions or dimensional-units
(defun convert-recipe-role-alias-keys-to-upc-keys
(alist-upc
alist-recipe
alist-inventory
alist-role-alias
recipe-using-role-aliases
&optional
(new-upc-list '()))
""
(if
recipe-using-role-aliases
(let
((next-upc
(get-role-alias-upc-has-inventory
alist-upc
alist-inventory
alist-role-alias
(caar recipe-using-role-aliases))))
(convert-recipe-role-alias-keys-to-upc-keys
alist-upc
alist-recipe
alist-inventory
alist-role-alias
(cdr recipe-using-role-aliases)
(if
next-upc
(acons
next-upc
(cdar recipe-using-role-aliases)
new-upc-list)
(acons
(caar recipe-using-role-aliases)
(cdar recipe-using-role-aliases)
new-upc-list))))
new-upc-list))
;; need to add minimum-dimensional-unit
(defun convert-recipe-role-alias-keys-to-upc-keys-check-leftover
(alist-upc
alist-recipe
alist-inventory
alist-role-alias
alist-upc-leftover
recipe-using-role-aliases
&optional
(minimum-dimensional-unit '(1 . grams))
(accumulated-dimensional-unit '(0 . grams))
(new-upc-list '())
(skip-list '()))
""
(if
recipe-using-role-aliases
(let
((next-upc ;; verify size
(get-role-alias-upc-has-inventory-or-leftover
alist-upc
alist-inventory
alist-role-alias
alist-upc-leftover ;; update to add quick math to determine the
(caar recipe-using-role-aliases) ;; unit quantity required
1
'(0 . grams)
skip-list)))
(convert-recipe-role-alias-keys-to-upc-keys-check-leftover
alist-upc
alist-recipe
alist-inventory
alist-role-alias
alist-upc-leftover
minimum-dimensional-unit
accumulated-dimensional-unit
(cdr recipe-using-role-aliases)
(if
next-upc
(acons
next-upc
(cdar recipe-using-role-aliases)
new-upc-list)
(acons
(caar recipe-using-role-aliases)
(cdar recipe-using-role-aliases)
new-upc-list))
(append skiplist (list next-upc))))
new-upc-list))
;; list eater function eats recipe-using-role-aliases
;; and builds new-upc-list with the correct quantities available
;; in alist-upc-leftover being used before accessing unopened alist-inventory
;; menu option 43
(defun convert-recipe-role-alias-keys-to-upc-keys-sum-leftover
(alist-upc
alist-recipe
alist-inventory
alist-role-alias
alist-upc-leftover
recipe-using-role-aliases
&optional
(new-upc-list '()))
""
(if
recipe-using-role-aliases
(let
((this-role-total-required-dimensional-unit
(any-dimensional-unit-to-grams-dimensional-unit
(cdar
recipe-using-role-aliases))))
(let
((list-upc-in-leftover-sum-to-role-total-required
(calculate-upc-list-in-leftover-sum-to-total-required
alist-upc
alist-role-alias
alist-upc-leftover
role-alias
this-role-total-required-dimensional-unit)))
(values
list-upc-in-leftover-sum-to-role-total-required
this-role-total-required-dimensional-unit)))))
;; same thing as #'convert-recipe-role-alias-keys-to-upc-keys but ignores
;; inventory and returns the last upc assigned to the role-alias
(defun convert-recipe-role-alias-keys-to-upc-keys-ignore-inventory
(alist-upc
alist-recipe
alist-role-alias
recipe-using-role-aliases
&optional
(new-upc-list '()))
""
(if
recipe-using-role-aliases
(let
((next-upc
(get-role-alias-first-upc-ignore-inventory
alist-role-alias
(caar recipe-using-role-aliases))))
(convert-recipe-role-alias-keys-to-upc-keys-ignore-inventory
alist-upc
alist-recipe
alist-role-alias
(cdr recipe-using-role-aliases)
(if
next-upc
(acons
next-upc
(cdar recipe-using-role-aliases)
new-upc-list)
(acons
(caar recipe-using-role-aliases)
(cdar recipe-using-role-aliases)
new-upc-list))))
new-upc-list))
(defun convert-recipe-role-alias-keys-to-upc-keys-use-up-leftover
(alist-upc
alist-recipe
alist-inventory
alist-role-alias
alist-upc-leftover
recipe-using-role-aliases
&optional
(new-upc-list '()))
""
(if
recipe-using-role-aliases
(let
((total-leftover-options
(get-role-alias-from-leftover
alist-upc
alist-role-alias
alist-upc-leftover
(caar recipe-using-role-aliases))))
(let ;; result-calculation is a list containing 3 elements:
((result-calculation ;; result-upc-list, alist-upc-leftover
(calculate-role-alias-subtraction-from-leftover ;; and
alist-upc ;; result-recipe-using-role-aliases
alist-role-alias
alist-upc-leftover
(caar recipe-using-role-aliases)
(any-dimensional-unit-to-grams-dimensional-unit
(cdar recipe-using-role-aliases)))))
(convert-recipe-role-alias-keys-to-upc-keys-use-up-leftover
alist-upc
alist-recipe
alist-inventory
alist-role-alias
(cadr result-calculation)
(cdr recipe-using-role-aliases)
(append
new-upc-list
(car result-calculation)
(caddr result-calculation)))))
new-upc-list))
;; returns result-upc-list, alist-upc-leftover, and
;; the recipe-using-role-aliases which still need to be resolved
;; from inventory (they were unavailable as open leftovers)
(defun calculate-role-alias-subtraction-from-leftover
(alist-upc
alist-role-alias
alist-upc-leftover
role-alias
&optional
(requirement-dimensional-unit '(0 . grams))
(these-applicable-leftovers
(calculate-recipe-dimensional-units-from-portion-percentages
alist-upc
(get-role-alias-from-leftover
alist-upc
alist-role-alias
alist-upc-leftover
role-alias)))
(result-upc-list '())
(result-recipe-using-role-alias '()))
""
(if
these-applicable-leftovers
(let
((grams-available-this-upc
(cadar these-applicable-leftovers))
(grams-needed
(car requirement-dimensional-unit)))
(cond
((<= grams-needed grams-available-this-upc)
(calculate-role-alias-subtraction-from-leftover
alist-upc
alist-role-alias
(adjust-alist-percentage-list ;; result-alist-upc-leftover
alist-upc-leftover
(caar these-applicable-leftovers)
(-
(cdar
(calculate-recipe-portion-percentages-from-dimensional-units
alist-upc
(list
(car
these-applicable-leftovers))))
(cdar
(calculate-recipe-portion-percentages-from-dimensional-units
alist-upc
(list
(cons
(caar these-applicable-leftovers)
requirement-dimensional-unit))))))
role-alias
(subtract-two-dimensional-units
requirement-dimensional-unit
(cons
grams-available-this-upc
'grams))
(cdr these-applicable-leftovers)
(append ;; result-upc-list
result-upc-list
(list