859 } |
860 } |
860 $brews .= ']'; |
861 $brews .= ']'; |
861 header("Content-type: application/json"); |
862 header("Content-type: application/json"); |
862 echo $brews; |
863 echo $brews; |
863 } |
864 } |
|
865 |
|
866 |
|
867 |
|
868 function reduce_fermentables($item) { |
|
869 |
|
870 global $connect; |
|
871 |
|
872 $amount = round($item['f_amount'] * 1000) / 1000; |
|
873 $sql2 = "UPDATE inventory_fermentables SET inventory = inventory - " . $amount; |
|
874 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['f_name']); |
|
875 $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['f_origin']); |
|
876 $sql2 .= "' AND supplier='" . mysqli_real_escape_string($connect, $item['f_supplier']); |
|
877 $sql2 .= "' AND inventory >= " . $amount; |
|
878 $sql2 .= " LIMIT 1;"; |
|
879 $result2 = mysqli_query($connect, $sql2); |
|
880 $ar = mysqli_affected_rows($connect); |
|
881 if ($ar == 1) { |
|
882 syslog(LOG_NOTICE, "Reduced fermentable `".$item['f_name']."' from `".$item['f_supplier']."' with ".$amount." kg"); |
|
883 } else if ($ar == 0) { |
|
884 $sql2 = "UPDATE inventory_fermentables SET inventory = 0"; |
|
885 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['f_name']); |
|
886 $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['f_origin']); |
|
887 $sql2 .= "' AND supplier='" . mysqli_real_escape_string($connect, $item['f_supplier']); |
|
888 $sql2 .= "' AND inventory < " . $amount; |
|
889 $sql2 .= " LIMIT 1;"; |
|
890 $result2 = mysqli_query($connect, $sql2); |
|
891 $ar = mysqli_affected_rows($connect); |
|
892 if ($ar == 1) { |
|
893 syslog(LOG_NOTICE, "Reduced fermentable `".$item['f_name']."' from `".$item['f_supplier']."' to 0 kg"); |
|
894 } else if ($ar == 0) { |
|
895 syslog(LOG_NOTICE, "Reduce fermentable `".$item['f_name']."' from `".$item['f_supplier']."' failed"); |
|
896 } |
|
897 } |
|
898 } |
|
899 |
|
900 |
|
901 |
|
902 function reduce_hops($item) { |
|
903 |
|
904 global $connect; |
|
905 |
|
906 $amount = round($item['h_amount'] * 1000) / 1000; |
|
907 $sql2 = "UPDATE inventory_hops SET inventory = inventory - " . $amount; |
|
908 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['h_name']); |
|
909 $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['h_origin']); |
|
910 $sql2 .= "' AND form=" . $item['h_form']; |
|
911 $sql2 .= " AND inventory >= " . $amount . " LIMIT 1;"; |
|
912 $result2 = mysqli_query($connect, $sql2); |
|
913 $ar = mysqli_affected_rows($connect); |
|
914 if ($ar == 1) { |
|
915 syslog(LOG_NOTICE, "Reduced hop `".$item['h_name']."' from `".$item['h_origin']."' with ".$amount." kg"); |
|
916 } else if ($ar == 0) { |
|
917 $sql2 = "UPDATE inventory_hops SET inventory = 0"; |
|
918 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['h_name']); |
|
919 $sql2 .= "' AND origin='" . mysqli_real_escape_string($connect, $item['h_origin']); |
|
920 $sql2 .= "' AND form=" . $item['h_form']; |
|
921 $sql2 .= " AND inventory < " . $amount . " LIMIT 1;"; |
|
922 $result2 = mysqli_query($connect, $sql2); |
|
923 $ar = mysqli_affected_rows($connect); |
|
924 if ($ar == 1) { |
|
925 syslog(LOG_NOTICE, "Reduced hop `".$item['h_name']."' from `".$item['h_origin']."' to 0 kg"); |
|
926 } else if ($ar == 0) { |
|
927 syslog(LOG_NOTICE, "Reduce hop `".$item['h_name']."' from `".$item['h_origin']."' failed"); |
|
928 } |
|
929 } |
|
930 } |
|
931 |
|
932 |
|
933 |
|
934 function reduce_miscs($item) { |
|
935 |
|
936 global $connect; |
|
937 |
|
938 $amount = round($item['m_amount'] * 100000) / 100000; |
|
939 $sql2 = "UPDATE inventory_miscs SET inventory = inventory - " . $amount; |
|
940 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['m_name']); |
|
941 $sql2 .= "' AND inventory >= " . $amount . " LIMIT 1;"; |
|
942 $result2 = mysqli_query($connect, $sql2); |
|
943 $ar = mysqli_affected_rows($connect); |
|
944 if ($ar == 1) { |
|
945 syslog(LOG_NOTICE, "Reduced misc `".$item['m_name']."' with ".$amount); |
|
946 } else if ($ar == 0) { |
|
947 $sql2 = "UPDATE inventory_miscs SET inventory = 0"; |
|
948 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['m_name']); |
|
949 $sql2 .= "' AND inventory < " . $amount . " LIMIT 1;"; |
|
950 $result2 = mysqli_query($connect, $sql2); |
|
951 $ar = mysqli_affected_rows($connect); |
|
952 if ($ar == 1) { |
|
953 syslog(LOG_NOTICE, "Reduced misc `".$item['m_name']."' to 0"); |
|
954 } else if ($ar == 0) { |
|
955 syslog(LOG_NOTICE, "Reduce misc `".$item['m_name']."' failed"); |
|
956 } |
|
957 } |
|
958 } |
|
959 |
|
960 |
|
961 |
|
962 function reduce_yeast($item) { |
|
963 |
|
964 global $connect; |
|
965 |
|
966 $amount = round($item['y_amount'] * 10000) / 10000; |
|
967 $sql2 = "UPDATE inventory_yeasts SET inventory = inventory - " . $amount; |
|
968 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['y_name']); |
|
969 $sql2 .= "' AND laboratory='" . mysqli_real_escape_string($connect, $item['y_laboratory']); |
|
970 $sql2 .= "' AND product_id='" . mysqli_real_escape_string($connect, $item['y_product_id']); |
|
971 $sql2 .= "' AND form=" . $item['y_form']; |
|
972 $sql2 .= " AND inventory >= " . $amount . " LIMIT 1;"; |
|
973 $result2 = mysqli_query($connect, $sql2); |
|
974 $ar = mysqli_affected_rows($connect); |
|
975 if ($ar == 1) { |
|
976 syslog(LOG_NOTICE, "Reduced yeast `".$item['y_product_id'].' '.$item['y_name']."' from `".$item['y_laboratory']."' with ".$amount); |
|
977 } else if ($ar == 0) { |
|
978 $sql2 = "UPDATE inventory_yeasts SET inventory = 0"; |
|
979 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $item['y_name']); |
|
980 $sql2 .= "' AND laboratory='" . mysqli_real_escape_string($connect, $item['y_laboratory']); |
|
981 $sql2 .= "' AND product_id='" . mysqli_real_escape_string($connect, $item['y_product_id']); |
|
982 $sql2 .= "' AND form=" . $item['y_form']; |
|
983 $sql2 .= " AND inventory < " . $amount . " LIMIT 1;"; |
|
984 $result2 = mysqli_query($connect, $sql2); |
|
985 $ar = mysqli_affected_rows($connect); |
|
986 if ($ar == 1) { |
|
987 syslog(LOG_NOTICE, "Reduced yeast `".$item['y_product_id'].' '.$item['y_name']."' from `".$item['y_laboratory']."' to 0"); |
|
988 } else if ($ar == 0) { |
|
989 syslog(LOG_NOTICE, "Reduce yeast `".$item['y_product_id'].' '.$item['y_name']."' from `".$item['y_laboratory']."' failed"); |
|
990 } |
|
991 } |
|
992 } |
|
993 |
|
994 |
|
995 |
|
996 /* |
|
997 * Reduce inventory depending on the production stage. |
|
998 * Stage 3+, 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) |
|
999 * Stage 4+, after primary, reduce sugars(2-fermention), yeasts(0-Primary), miscs(3-primary) |
|
1000 * Stage 5+, after secondary, reduce yeasts(1-Secondary) |
|
1001 * Stage 6+, after tertiary, reduce sugars(3-lagering), hops(5-dry-hop), yeasts(2-Tertiary), miscs(4-secondary) |
|
1002 * Stage 7+, after packaging, reduce sugars(4-bottle), yeasts(3-Bottle), miscs(5-bottling) |
|
1003 */ |
|
1004 function inventory_reduce() { |
|
1005 |
|
1006 global $connect; |
|
1007 |
|
1008 $savethis = 0; |
|
1009 $stage = $_POST['stage']; |
|
1010 $inventory_reduced = $_POST['inventory_reduced']; |
|
1011 syslog(LOG_NOTICE, "inventory_reduce() stage: ".$stage." inventory_reduced: ".$inventory_reduced); |
|
1012 |
|
1013 /* |
|
1014 * If the brew is done, reduce the used ingredients. |
|
1015 */ |
|
1016 if (($stage >= 3) && ($inventory_reduced < 3)) { |
|
1017 syslog(LOG_NOTICE, "Reduce brew inventory from " . $_POST['code'] . " " . $_POST['name']); |
|
1018 |
|
1019 if (isset($_POST['fermentables'])) { |
|
1020 $array = $_POST['fermentables']; |
|
1021 foreach($array as $key => $item) { |
|
1022 if ($item['f_added'] <= 1) { // Mash, Boil |
|
1023 reduce_fermentables($item); |
|
1024 } |
|
1025 } |
|
1026 } |
|
1027 |
|
1028 if (isset($_POST['hops'])) { |
|
1029 $array = $_POST['hops']; |
|
1030 foreach($array as $key => $item) { |
|
1031 if ($item['h_useat'] <= 4) { // Mash, FWH, Boil, Flameout, Whirlpool |
|
1032 reduce_hops($item); |
|
1033 } |
|
1034 } |
|
1035 } |
|
1036 |
|
1037 if (isset($_POST['miscs'])) { |
|
1038 $array = $_POST['miscs']; |
|
1039 foreach($array as $key => $item) { |
|
1040 if ($item['m_use_use'] <= 2) { // Starter, Mash, Boil |
|
1041 reduce_miscs($item); |
|
1042 } |
|
1043 } |
|
1044 } |
|
1045 |
|
1046 if ($_POST['w1_name'] != '') { |
|
1047 $sql2 = "UPDATE inventory_waters SET inventory = inventory - ".$_POST['w1_amount']; |
|
1048 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w1_name']); |
|
1049 $sql2 .= "' AND unlimited_stock=0 AND inventory >= ".$_POST['w1_amount']." LIMIT 1;"; |
|
1050 $result2 = mysqli_query($connect, $sql2); |
|
1051 $ar = mysqli_affected_rows($connect); |
|
1052 if ($ar == 1) { |
|
1053 syslog(LOG_NOTICE, "Reduced water `".$_POST['w1_name']."' with ".$_POST['w1_amount']." liter"); |
|
1054 } else if ($ar == 0) { |
|
1055 $sql2 = "UPDATE inventory_waters SET inventory = 0"; |
|
1056 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w1_name']); |
|
1057 $sql2 .= "' AND unlimited_stock=0 AND inventory < ".$_POST['w1_amount']." LIMIT 1;"; |
|
1058 $result2 = mysqli_query($connect, $sql2); |
|
1059 $ar = mysqli_affected_rows($connect); |
|
1060 if ($ar == 1) { |
|
1061 syslog(LOG_NOTICE, "Reduced water `".$_POST['w1_name']."' to 0 liters"); |
|
1062 } else { |
|
1063 syslog(LOG_NOTICE, "Reduce water `".$_POST['w1_name']."' not reduced is maybe tapwater"); |
|
1064 } |
|
1065 } |
|
1066 } |
|
1067 if ($_POST['w2_name'] != '') { |
|
1068 $sql2 = "UPDATE inventory_waters SET inventory = inventory - ".$_POST['w2_amount']; |
|
1069 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w2_name']); |
|
1070 $sql2 .= "' AND unlimited_stock=0 AND inventory >= ".$_POST['w2_amount']." LIMIT 1;"; |
|
1071 $result2 = mysqli_query($connect, $sql2); |
|
1072 $ar = mysqli_affected_rows($connect); |
|
1073 if ($ar == 1) { |
|
1074 syslog(LOG_NOTICE, "Reduced water `".$_POST['w2_name']."' with ".$_POST['w2_amount']." liter"); |
|
1075 } else if ($ar == 0) { |
|
1076 $sql2 = "UPDATE inventory_waters SET inventory = 0"; |
|
1077 $sql2 .= " WHERE name='" . mysqli_real_escape_string($connect, $_POST['w2_name']); |
|
1078 $sql2 .= "' AND unlimited_stock=0 AND inventory < ".$_POST['w2_amount']." LIMIT 1;"; |
|
1079 $result2 = mysqli_query($connect, $sql2); |
|
1080 $ar = mysqli_affected_rows($connect); |
|
1081 if ($ar == 1) { |
|
1082 syslog(LOG_NOTICE, "Reduced water `".$_POST['w2_name']."' to 0 liters"); |
|
1083 } else { |
|
1084 syslog(LOG_NOTICE, "Reduce water `".$_POST['w2_name']."' failed"); |
|
1085 } |
|
1086 } |
|
1087 } |
|
1088 |
|
1089 $inventory_reduced = 3; |
|
1090 $savethis = 1; |
|
1091 } |
|
1092 |
|
1093 /* |
|
1094 * After the Primary fermentation |
|
1095 */ |
|
1096 if (($stage >= 4) && ($inventory_reduced < 4)) { |
|
1097 syslog(LOG_NOTICE, "Reduce Primary inventory from " . $_POST['code'] . " " . $_POST['name']); |
|
1098 |
|
1099 if (isset($_POST['fermentables'])) { |
|
1100 $array = $_POST['fermentables']; |
|
1101 foreach($array as $key => $item) { |
|
1102 if ($item['f_added'] == 2) { // Fermentation |
|
1103 reduce_fermentables($item); |
|
1104 } |
|
1105 } |
|
1106 } |
|
1107 |
|
1108 if (isset($_POST['miscs'])) { |
|
1109 $array = $_POST['miscs']; |
|
1110 foreach($array as $key => $item) { |
|
1111 if ($item['m_use_use'] == 3) { // Fermentation |
|
1112 reduce_miscs($item); |
|
1113 } |
|
1114 } |
|
1115 } |
|
1116 |
|
1117 if (isset($_POST['yeasts'])) { |
|
1118 $array = $_POST['yeasts']; |
|
1119 foreach($array as $key => $item) { |
|
1120 if ($item['y_use'] == 0) { // Primary |
|
1121 reduce_yeast($item); |
|
1122 } |
|
1123 } |
|
1124 } |
|
1125 |
|
1126 $inventory_reduced = 4; |
|
1127 $savethis = 1; |
|
1128 } |
|
1129 |
|
1130 |
|
1131 /* |
|
1132 * After the Seconday fermentation |
|
1133 */ |
|
1134 if (($stage >= 5) && ($inventory_reduced < 5)) { |
|
1135 syslog(LOG_NOTICE, "Reduce Secondary inventory from " . $_POST['code'] . " " . $_POST['name']); |
|
1136 |
|
1137 if (isset($_POST['yeasts'])) { |
|
1138 $array = $_POST['yeasts']; |
|
1139 foreach($array as $key => $item) { |
|
1140 if ($item['y_use'] == 1) { // Secondary |
|
1141 reduce_yeast($item); |
|
1142 } |
|
1143 } |
|
1144 } |
|
1145 |
|
1146 $inventory_reduced = 5; |
|
1147 $savethis = 1; |
|
1148 } |
|
1149 |
|
1150 |
|
1151 /* |
|
1152 * After the Tertiary fermentation |
|
1153 */ |
|
1154 if (($stage >= 6) && ($inventory_reduced < 6)) { |
|
1155 syslog(LOG_NOTICE, "Reduce Tertiary inventory from " . $_POST['code'] . " " . $_POST['name']); |
|
1156 |
|
1157 if (isset($_POST['fermentables'])) { |
|
1158 $array = $_POST['fermentables']; |
|
1159 foreach($array as $key => $item) { |
|
1160 if ($item['f_added'] == 3) { // Lagering |
|
1161 reduce_fermentables($item); |
|
1162 } |
|
1163 } |
|
1164 } |
|
1165 |
|
1166 if (isset($_POST['hops'])) { |
|
1167 $array = $_POST['hops']; |
|
1168 foreach($array as $key => $item) { |
|
1169 if ($item['h_useat'] == 5) { // Dry hop |
|
1170 reduce_hops($item); |
|
1171 } |
|
1172 } |
|
1173 } |
|
1174 |
|
1175 if (isset($_POST['yeasts'])) { |
|
1176 $array = $_POST['yeasts']; |
|
1177 foreach($array as $key => $item) { |
|
1178 if ($item['y_use'] == 2) { // Tertiary |
|
1179 reduce_yeast($item); |
|
1180 } |
|
1181 } |
|
1182 } |
|
1183 |
|
1184 if (isset($_POST['miscs'])) { |
|
1185 $array = $_POST['miscs']; |
|
1186 foreach($array as $key => $item) { |
|
1187 if ($item['m_use_use'] == 4) { // Secondary or Tertiary |
|
1188 reduce_miscs($item); |
|
1189 } |
|
1190 } |
|
1191 } |
|
1192 |
|
1193 $inventory_reduced = 6; |
|
1194 $savethis = 1; |
|
1195 } |
|
1196 |
|
1197 /* |
|
1198 * After packaging |
|
1199 * reduce sugars(4-bottle), yeasts(3-Bottle), miscs(5-bottling) |
|
1200 */ |
|
1201 if (($stage >= 6) && ($inventory_reduced < 7)) { |
|
1202 syslog(LOG_NOTICE, "Reduce Packaging inventory from " . $_POST['code'] . " " . $_POST['name']); |
|
1203 |
|
1204 // Bottle sugar, how? |
|
1205 |
|
1206 if (isset($_POST['yeasts'])) { |
|
1207 $array = $_POST['yeasts']; |
|
1208 foreach($array as $key => $item) { |
|
1209 if ($item['y_use'] == 3) { // Bottle |
|
1210 reduce_yeast($item); |
|
1211 } |
|
1212 } |
|
1213 } |
|
1214 |
|
1215 if (isset($_POST['miscs'])) { |
|
1216 $array = $_POST['miscs']; |
|
1217 foreach($array as $key => $item) { |
|
1218 if ($item['m_use_use'] == 5) { // Bottle |
|
1219 reduce_miscs($item); |
|
1220 } |
|
1221 } |
|
1222 } |
|
1223 |
|
1224 if ($stage < 7) |
|
1225 $stage = 7; |
|
1226 $inventory_reduced = $stage; |
|
1227 $savethis = 1; |
|
1228 } |
|
1229 |
|
1230 /* |
|
1231 * Save only if something was reduced. |
|
1232 */ |
|
1233 if ($savethis == 1) { |
|
1234 $sql2 = "UPDATE products SET stage=".$stage.", inventory_reduced=".$inventory_reduced." WHERE record='".$_POST['record']."';"; |
|
1235 syslog(LOG_NOTICE, $sql2); |
|
1236 $result2 = mysqli_query($connect, $sql2); |
|
1237 $ar = mysqli_affected_rows($connect); |
|
1238 if ($ar != 1) { |
|
1239 syslog(LOG_NOTICE, $sql2." error, affected rows: ".$ar); |
|
1240 } |
|
1241 } |
|
1242 } |
|
1243 |
|
1244 |
864 ?> |
1245 ?> |