606 else |
606 else |
607 ui->taste_ackButton->hide(); |
607 ui->taste_ackButton->hide(); |
608 } |
608 } |
609 |
609 |
610 |
610 |
|
611 void EditProduct::reduce_fermentable(int i) |
|
612 { |
|
613 QSqlQuery query; |
|
614 |
|
615 query.prepare("SELECT record,inventory FROM inventory_fermentables WHERE name=:name AND origin=:origin AND supplier=:supplier LIMIT 1"); |
|
616 query.bindValue(":name", product->fermentables.at(i).name); |
|
617 query.bindValue(":origin", product->fermentables.at(i).origin); |
|
618 query.bindValue(":supplier", product->fermentables.at(i).supplier); |
|
619 query.exec(); |
|
620 if (query.first()) { |
|
621 int record = query.value("record").toInt(); |
|
622 double inventory = query.value("inventory").toDouble() - product->fermentables.at(i).amount; |
|
623 if (inventory < 0) |
|
624 inventory = 0; |
|
625 else |
|
626 inventory = round(inventory * 100000) / 100000; |
|
627 query.prepare("UPDATE inventory_fermentables SET inventory=:inventory WHERE record=:record"); |
|
628 query.bindValue(":inventory", inventory ); |
|
629 query.bindValue(":record", record); |
|
630 query.exec(); |
|
631 if (query.lastError().isValid()) { |
|
632 qWarning() << "UPDATE inventory_fermentables" << query.lastError(); |
|
633 } else { |
|
634 qInfo() << " Reduced" << product->fermentables.at(i).name << "to" << inventory; |
|
635 } |
|
636 } else { |
|
637 qWarning() << " Fermentable" << product->fermentables.at(i).name << "not found"; |
|
638 } |
|
639 } |
|
640 |
|
641 |
|
642 void EditProduct::reduce_hop(int i) |
|
643 { |
|
644 QSqlQuery query; |
|
645 |
|
646 query.prepare("SELECT record,inventory FROM inventory_hops WHERE name=:name AND origin=:origin AND form=:form LIMIT 1"); |
|
647 query.bindValue(":name", product->hops.at(i).name); |
|
648 query.bindValue(":origin", product->hops.at(i).origin); |
|
649 query.bindValue(":form", product->hops.at(i).form); |
|
650 query.exec(); |
|
651 if (query.first()) { |
|
652 int record = query.value("record").toInt(); |
|
653 double inventory = query.value("inventory").toDouble() - product->hops.at(i).amount; |
|
654 if (inventory < 0) |
|
655 inventory = 0; |
|
656 else |
|
657 inventory = round(inventory * 100000) / 100000; |
|
658 query.prepare("UPDATE inventory_hops SET inventory=:inventory WHERE record=:record"); |
|
659 query.bindValue(":inventory", inventory ); |
|
660 query.bindValue(":record", record); |
|
661 query.exec(); |
|
662 if (query.lastError().isValid()) { |
|
663 qWarning() << "UPDATE inventory_hops" << query.lastError(); |
|
664 } else { |
|
665 qInfo() << " Reduced" << product->hops.at(i).name << "to" << inventory; |
|
666 } |
|
667 } else { |
|
668 qWarning() << " Hop" << product->hops.at(i).name << "not found"; |
|
669 } |
|
670 } |
|
671 |
|
672 |
|
673 void EditProduct::reduce_misc(int i) |
|
674 { |
|
675 QSqlQuery query; |
|
676 |
|
677 query.prepare("SELECT record,inventory FROM inventory_miscs WHERE name=:name LIMIT 1"); |
|
678 query.bindValue(":name", product->miscs.at(i).name); |
|
679 query.exec(); |
|
680 if (query.first()) { |
|
681 int record = query.value("record").toInt(); |
|
682 double inventory = query.value("inventory").toDouble() - product->miscs.at(i).amount; |
|
683 if (inventory < 0) |
|
684 inventory = 0; |
|
685 else |
|
686 inventory = round(inventory * 100000) / 100000; |
|
687 query.prepare("UPDATE inventory_miscs SET inventory=:inventory WHERE record=:record"); |
|
688 query.bindValue(":inventory", inventory ); |
|
689 query.bindValue(":record", record); |
|
690 query.exec(); |
|
691 if (query.lastError().isValid()) { |
|
692 qWarning() << "UPDATE inventory_miscs" << query.lastError(); |
|
693 } else { |
|
694 qInfo() << " Reduced" << product->miscs.at(i).name << "to" << inventory; |
|
695 } |
|
696 } else { |
|
697 qWarning() << " Misc" << product->miscs.at(i).name << "not found"; |
|
698 } |
|
699 } |
|
700 |
|
701 |
|
702 void EditProduct::reduce_yeast(int i) |
|
703 { |
|
704 QSqlQuery query; |
|
705 |
|
706 query.prepare("SELECT record,inventory FROM inventory_yeasts WHERE name=:name AND laboratory=:laboratory AND product_id=:product_id AND form=:form LIMIT 1"); |
|
707 query.bindValue(":name", product->yeasts.at(i).name); |
|
708 query.bindValue(":laboratory", product->yeasts.at(i).laboratory); |
|
709 query.bindValue(":product_id", product->yeasts.at(i).product_id); |
|
710 query.bindValue(":form", product->yeasts.at(i).form); |
|
711 query.exec(); |
|
712 if (query.first()) { |
|
713 int record = query.value("record").toInt(); |
|
714 double inventory = query.value("inventory").toDouble() - product->yeasts.at(i).amount; |
|
715 if (inventory < 0) |
|
716 inventory = 0; |
|
717 else |
|
718 inventory = round(inventory * 100000) / 100000; |
|
719 query.prepare("UPDATE inventory_yeasts SET inventory=:inventory WHERE record=:record"); |
|
720 query.bindValue(":inventory", inventory ); |
|
721 query.bindValue(":record", record); |
|
722 query.exec(); |
|
723 if (query.lastError().isValid()) { |
|
724 qWarning() << "UPDATE inventory_yeasts" << query.lastError(); |
|
725 } else { |
|
726 qInfo() << " Reduced" << product->yeasts.at(i).name << "to" << inventory; |
|
727 } |
|
728 } else { |
|
729 qWarning() << " Yeast" << product->yeasts.at(i).name << "not found"; |
|
730 } |
|
731 } |
|
732 |
|
733 |
|
734 /* |
|
735 * Reduce inventory depending on the production stage. |
|
736 * Stage PROD_STAGE_BREW+, after brew, reduce sugars(0-mash, 1-boil), hops(0-mash, 1-fwh, 2-boil, 3-aroma, 4-whirlpool), miscs(0-starter, 1-mash, 2-boil) |
|
737 * Stage PROD_STAGE_PRIMARY+, after primary, reduce sugars(2-fermention), yeasts(0-Primary), miscs(3-primary) |
|
738 * Stage PROD_STAGE_SECONDARY+, after secondary, reduce yeasts(1-Secondary) |
|
739 * Stage PROD_STAGE_TERTIARY+, after tertiary, reduce sugars(3-lagering), hops(5-dry-hop), yeasts(2-Tertiary), miscs(4-secondary) |
|
740 * Stage PROD_STAGE_PACKAGE+, after packaging, reduce sugars(4-bottle, 5-kegs), hops( Extract ), yeasts(3-Bottle), miscs(5-bottling) |
|
741 */ |
|
742 void EditProduct::inventory_reduce() |
|
743 { |
|
744 bool savethis = false; |
|
745 QSqlQuery query; |
|
746 |
|
747 if (product->stage == product->inventory_reduced) |
|
748 return; |
|
749 |
|
750 /* |
|
751 * Nothing to reduce yet, but just set inventory reduced. |
|
752 */ |
|
753 if ((product->stage >= PROD_STAGE_WAIT) && (product->inventory_reduced < PROD_STAGE_WAIT)) { |
|
754 product->inventory_reduced = PROD_STAGE_WAIT; |
|
755 savethis = true; |
|
756 } |
|
757 if ((product->stage >= PROD_STAGE_BREW) && (product->inventory_reduced < PROD_STAGE_BREW)) { |
|
758 product->inventory_reduced = PROD_STAGE_BREW; |
|
759 savethis = true; |
|
760 } |
|
761 |
|
762 /* |
|
763 * If the brew is done, reduce the used ingredients. |
|
764 */ |
|
765 if ((product->stage >= PROD_STAGE_PRIMARY) && (product->inventory_reduced < PROD_STAGE_PRIMARY)) { |
|
766 qInfo() << "Reduce brew inventory from" << product->code << product->name; |
|
767 |
|
768 if (product->fermentables.size()) { |
|
769 for (int i = 0; i < product->fermentables.size(); i++) { |
|
770 if (product->fermentables.at(i).added <= FERMENTABLE_ADDED_BOIL) { /* Mash or boil */ |
|
771 reduce_fermentable(i); |
|
772 } |
|
773 } |
|
774 } |
|
775 if (product->hops.size()) { |
|
776 for (int i = 0; i < product->hops.size(); i++) { |
|
777 if (product->hops.at(i).useat <= HOP_USEAT_WHIRLPOOL) { /* Mash, FWH, Boil, Flameout, Whirlpool */ |
|
778 reduce_hop(i); |
|
779 } |
|
780 } |
|
781 } |
|
782 if (product->miscs.size()) { |
|
783 for (int i = 0; i < product->miscs.size(); i++) { |
|
784 if (product->miscs.at(i).use_use <= MISC_USES_BOIL) { /* Starter, Mash, Boil */ |
|
785 reduce_misc(i); |
|
786 } |
|
787 } |
|
788 } |
|
789 if ((product->w1_name != "") && (product->w1_amount > 0)) { |
|
790 query.prepare("SELECT record,inventory FROM inventory_waters WHERE name=:name AND unlimited_stock=0 LIMIT 1"); |
|
791 query.bindValue(":name", product->w1_name); |
|
792 query.exec(); |
|
793 if (query.first()) { |
|
794 int record = query.value("record").toInt(); |
|
795 double inventory = query.value("inventory").toDouble() - product->w1_amount; |
|
796 if (inventory < 0) |
|
797 inventory = 0; |
|
798 else |
|
799 inventory = round(inventory * 100) / 100; |
|
800 query.prepare("UPDATE inventory_waters SET inventory=:inventory WHERE record=:record"); |
|
801 query.bindValue(":inventory", inventory ); |
|
802 query.bindValue(":record", record); |
|
803 query.exec(); |
|
804 if (query.lastError().isValid()) { |
|
805 qWarning() << "UPDATE inventory_waters" << query.lastError(); |
|
806 } else { |
|
807 qInfo() << " Reduced" << product->w1_name << "to" << inventory; |
|
808 } |
|
809 } |
|
810 } |
|
811 if ((product->w2_name != "") && (product->w2_amount > 0)) { |
|
812 query.prepare("SELECT record,inventory FROM inventory_waters WHERE name=:name AND unlimited_stock=0 LIMIT 1"); |
|
813 query.bindValue(":name", product->w1_name); |
|
814 query.exec(); |
|
815 if (query.first()) { |
|
816 int record = query.value("record").toInt(); |
|
817 double inventory = query.value("inventory").toDouble() - product->w1_amount; |
|
818 if (inventory < 0) |
|
819 inventory = 0; |
|
820 else |
|
821 inventory = round(inventory * 100) / 100; |
|
822 query.prepare("UPDATE inventory_waters SET inventory=:inventory WHERE record=:record"); |
|
823 query.bindValue(":inventory", inventory ); |
|
824 query.bindValue(":record", record); |
|
825 query.exec(); |
|
826 if (query.lastError().isValid()) { |
|
827 qWarning() << "UPDATE inventory_waters" << query.lastError(); |
|
828 } else { |
|
829 qInfo() << " Reduced" << product->w1_name << "to" << inventory; |
|
830 } |
|
831 } |
|
832 } |
|
833 product->inventory_reduced = PROD_STAGE_PRIMARY; |
|
834 savethis = true; |
|
835 } |
|
836 |
|
837 /* |
|
838 * After the Primary fermentation |
|
839 */ |
|
840 if ((product->stage >= PROD_STAGE_SECONDARY) && (product->inventory_reduced < PROD_STAGE_SECONDARY)) { |
|
841 qInfo() << "Reduce primary inventory from" << product->code << product->name; |
|
842 |
|
843 if (product->fermentables.size()) { |
|
844 for (int i = 0; i < product->fermentables.size(); i++) { |
|
845 if (product->fermentables.at(i).added == FERMENTABLE_ADDED_FERMENTATION) { |
|
846 reduce_fermentable(i); |
|
847 } |
|
848 } |
|
849 } |
|
850 if (product->miscs.size()) { |
|
851 for (int i = 0; i < product->miscs.size(); i++) { |
|
852 if (product->miscs.at(i).use_use == MISC_USES_PRIMARY) { |
|
853 reduce_misc(i); |
|
854 } |
|
855 } |
|
856 } |
|
857 if (product->yeasts.size()) { |
|
858 for (int i = 0; i < product->yeasts.size(); i++) { |
|
859 if (product->yeasts.at(i).use == YEAST_USE_PRIMARY) { |
|
860 reduce_yeast(i); |
|
861 } |
|
862 } |
|
863 } |
|
864 product->inventory_reduced = PROD_STAGE_SECONDARY; |
|
865 savethis = true; |
|
866 } |
|
867 |
|
868 /* |
|
869 * After the Seconday fermentation |
|
870 */ |
|
871 if ((product->stage >= PROD_STAGE_TERTIARY) && (product->inventory_reduced < PROD_STAGE_TERTIARY)) { |
|
872 qInfo() << "Reduce secondary inventory from" << product->code << product->name; |
|
873 |
|
874 if (product->yeasts.size()) { |
|
875 for (int i = 0; i < product->yeasts.size(); i++) { |
|
876 if (product->yeasts.at(i).use == YEAST_USE_SECONDARY) { |
|
877 reduce_yeast(i); |
|
878 } |
|
879 } |
|
880 } |
|
881 product->inventory_reduced = PROD_STAGE_TERTIARY; |
|
882 savethis = true; |
|
883 } |
|
884 |
|
885 /* |
|
886 * After the Tertiary fermentation |
|
887 */ |
|
888 if ((product->stage >= PROD_STAGE_PACKAGE) && (product->inventory_reduced < PROD_STAGE_PACKAGE)) { |
|
889 qInfo() << "Reduce tertiary inventory from" << product->code << product->name; |
|
890 |
|
891 if (product->fermentables.size()) { |
|
892 for (int i = 0; i < product->fermentables.size(); i++) { |
|
893 if (product->fermentables.at(i).added == FERMENTABLE_ADDED_LAGERING) { |
|
894 reduce_fermentable(i); |
|
895 } |
|
896 } |
|
897 } |
|
898 if (product->hops.size()) { |
|
899 for (int i = 0; i < product->hops.size(); i++) { |
|
900 if (product->hops.at(i).useat == HOP_USEAT_DRY_HOP) { |
|
901 reduce_hop(i); |
|
902 } |
|
903 } |
|
904 } |
|
905 if (product->miscs.size()) { |
|
906 for (int i = 0; i < product->miscs.size(); i++) { |
|
907 if (product->miscs.at(i).use_use == MISC_USES_SECONDARY) { |
|
908 reduce_misc(i); |
|
909 } |
|
910 } |
|
911 } |
|
912 if (product->yeasts.size()) { |
|
913 for (int i = 0; i < product->yeasts.size(); i++) { |
|
914 if (product->yeasts.at(i).use == YEAST_USE_TERTIARY) { |
|
915 reduce_yeast(i); |
|
916 } |
|
917 } |
|
918 } |
|
919 |
|
920 product->inventory_reduced = PROD_STAGE_PACKAGE; |
|
921 savethis = true; |
|
922 } |
|
923 |
|
924 /* |
|
925 * After packaging |
|
926 * reduce sugars(4/5-bottle), yeasts(3-Bottle), miscs(5-bottling) |
|
927 */ |
|
928 if ((product->stage >= PROD_STAGE_CARBONATION) && (product->inventory_reduced < PROD_STAGE_CARBONATION)) { |
|
929 qInfo() << "Reduce package inventory from" << product->code << product->name; |
|
930 |
|
931 if (product->fermentables.size()) { |
|
932 for (int i = 0; i < product->fermentables.size(); i++) { |
|
933 if (product->fermentables.at(i).added >= FERMENTABLE_ADDED_BOTTLE) { |
|
934 reduce_fermentable(i); |
|
935 } |
|
936 } |
|
937 } |
|
938 if (product->yeasts.size()) { |
|
939 for (int i = 0; i < product->yeasts.size(); i++) { |
|
940 if (product->yeasts.at(i).use == YEAST_USE_BOTTLE) { |
|
941 reduce_yeast(i); |
|
942 } |
|
943 } |
|
944 } |
|
945 if (product->hops.size()) { |
|
946 for (int i = 0; i < product->hops.size(); i++) { |
|
947 if (product->hops.at(i).useat == HOP_USEAT_BOTTLING) { |
|
948 reduce_hop(i); |
|
949 } |
|
950 } |
|
951 } |
|
952 if (product->miscs.size()) { |
|
953 for (int i = 0; i < product->miscs.size(); i++) { |
|
954 if (product->miscs.at(i).use_use == MISC_USES_BOTTLING) { |
|
955 reduce_misc(i); |
|
956 } |
|
957 } |
|
958 } |
|
959 product->inventory_reduced = PROD_STAGE_CARBONATION; |
|
960 savethis = true; |
|
961 } |
|
962 |
|
963 if ((product->stage >= PROD_STAGE_MATURE) && (product->inventory_reduced < PROD_STAGE_MATURE)) { |
|
964 product->inventory_reduced = product->stage; |
|
965 savethis = true; |
|
966 } |
|
967 |
|
968 /* |
|
969 * Update the inventory_reduced state. |
|
970 */ |
|
971 if (savethis) { |
|
972 query.prepare("UPDATE products SET inventory_reduced=:inventory_reduced WHERE record=:record"); |
|
973 query.bindValue(":record", product->record); |
|
974 query.bindValue(":inventory_reduced", product->inventory_reduced); |
|
975 query.exec(); |
|
976 if (query.lastError().isValid()) { |
|
977 qWarning() << "UPDATE products" << query.lastError(); |
|
978 } |
|
979 } |
|
980 } |
|
981 |
|
982 |