soil_calculations.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * lib/soil_calculations.php
  4. *
  5. * Functions for soil analysis calculations and display
  6. */
  7. require_once __DIR__.'/../config/database.php';
  8. /**
  9. * Calculate and display soil program for a specific element
  10. *
  11. * @param string $symbol Element symbol (e.g., 'Ca', 'Mg', 'K')
  12. * @param string $element Database column name for the element
  13. * @param string $min Min value column (empty string uses element column)
  14. * @param string $max Max value column (empty string uses element column)
  15. * @param string $nutrient Full nutrient name
  16. * @param string $type Measurement type (e.g., 'kg', 'ppm', '%')
  17. * @param int $record_id Soil record ID
  18. * @param float $rand_id Random ID for verification
  19. * @return string HTML output for the program row
  20. */
  21. function soilProgramCalcs($symbol, $element, $min, $max, $nutrient, $type, $record_id, $rand_id) {
  22. try {
  23. $pdo = getDBConnection();
  24. // Determine which table to use for min/max values
  25. if (empty($min)) {
  26. $element_min = $element;
  27. $dbtable = "soil_specifications.";
  28. } else {
  29. $element_min = $min;
  30. $dbtable = "soil_records.";
  31. }
  32. if (empty($max)) {
  33. $element_max = $element;
  34. $dbtable = "soil_specifications.";
  35. } else {
  36. $element_max = $max;
  37. $dbtable = "soil_records.";
  38. }
  39. // Prepare and execute query
  40. $stmt = $pdo->prepare("
  41. SELECT soil_records.{$element},
  42. {$dbtable}{$element_max} AS soilMax,
  43. {$dbtable}{$element_min} AS soilMin
  44. FROM soil_records
  45. INNER JOIN soil_specifications ON soil_records.soil_type = soil_specifications.soil_type
  46. WHERE soil_records.id = ? AND soil_records.rand = ?
  47. ");
  48. $stmt->execute([$record_id, $rand_id]);
  49. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  50. if (!$row) {
  51. return "<div class='row'>
  52. <div class='col-1 border-bottom border-left'>1</div>
  53. <div class='col border-bottom border-left'>{$nutrient}</div>
  54. <div class='col border-bottom border-left'>@</div>
  55. <div class='col border-bottom border-left'>N/A</div>
  56. <div class='col border-bottom border-left'>kg/Ha</div>
  57. </div>";
  58. }
  59. $value = (float) $row[$element];
  60. $max_val = (float) $row['soilMax'];
  61. $measurement = empty($type) ? "" : $type;
  62. $value_p = floatval($value);
  63. // Calculate recommended amount (uses max instead of median like soilAnalysisReportCalcs)
  64. $recommended = $max_val - $value_p;
  65. // Convert acres to hectares (kg/Ac to kg/ha)
  66. $acHa = 2.4710559990832394739; // Acres to Hectares
  67. $value_converted = ($recommended * $acHa);
  68. // Show 0 if value is negative, otherwise round to 2 decimal places
  69. if ($value_converted < 0) {
  70. $value_converted = 0;
  71. } else {
  72. $value_converted = round($value_converted, 2);
  73. }
  74. $result = $value_converted . " " . $measurement;
  75. // Return HTML table row
  76. return "<div class='row'>
  77. <div class='col-1 border-bottom border-left'>1</div>
  78. <div class='col border-bottom border-left'>{$nutrient}</div>
  79. <div class='col border-bottom border-left'>@</div>
  80. <div class='col border-bottom border-left'>{$result}</div>
  81. <div class='col border-bottom border-left'>kg/Ha</div>
  82. </div>";
  83. } catch (PDOException $e) {
  84. error_log("Database error in soilProgramCalcs: " . $e->getMessage());
  85. }
  86. }
  87. /**
  88. * Calculate and display soil analysis report for a specific element
  89. *
  90. * @param string $symbol Element symbol (e.g., 'Ca', 'Mg', 'K')
  91. * @param string $element Database column name for the element
  92. * @param string $min Min value column (empty string uses element column)
  93. * @param string $max Max value column (empty string uses element column)
  94. * @param string $nutrient Full nutrient name
  95. * @param string $type Measurement type (e.g., 'kg', 'ppm', '%')
  96. * @param string $class CSS class for styling
  97. * @param int $record_id Soil record ID
  98. * @param float $rand_id Random ID for verification
  99. * @return string HTML output for the analysis row
  100. */
  101. function soilAnalysisReportCalcs($symbol, $element, $min, $max, $nutrient, $type, $class, $record_id, $rand_id) {
  102. try {
  103. $pdo = getDBConnection();
  104. // Determine which table to use for min/max values
  105. if (empty($min)) {
  106. $element_min = $element;
  107. $dbtable_min = "soil_specifications.";
  108. } else {
  109. $element_min = $min;
  110. $dbtable_min = "soil_records.";
  111. }
  112. if (empty($max)) {
  113. $element_max = $element;
  114. $dbtable_max = "soil_specifications.";
  115. } else {
  116. $element_max = $max;
  117. $dbtable_max = "soil_records.";
  118. }
  119. // Prepare and execute query
  120. $stmt = $pdo->prepare("
  121. SELECT soil_records.{$element},
  122. {$dbtable_min}{$element_min} AS soilMin,
  123. {$dbtable_max}{$element_max} AS soilMax
  124. FROM soil_records
  125. INNER JOIN soil_specifications ON soil_records.soil_type = soil_specifications.soil_type
  126. WHERE soil_records.id = ? AND soil_records.rand = ?
  127. ");
  128. $stmt->execute([$record_id, $rand_id]);
  129. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  130. if (!$row) {
  131. return "<div class='{$class}'>{$nutrient}: N/A</div>";
  132. }
  133. $value = (float) $row[$element];
  134. $min_val = (float) $row['soilMin'];
  135. $max_val = (float) $row['soilMax'];
  136. $measurement = empty($type) ? "" : $type;
  137. $value_p = floatval($value);
  138. // Calculate recommended amount (median between min and max)
  139. $recommended = ($min_val + $max_val) / 2;
  140. // Convert acres to hectares (kg/Ac to kg/ha)
  141. $acHa = 2.4710559990832394739; // Acres to Hectares
  142. $value_converted = ($recommended - $value_p) * $acHa;
  143. // Show 0 if value is negative, otherwise round to 2 decimal places
  144. if ($value_converted < 0) {
  145. $value_converted = 0;
  146. } else {
  147. $value_converted = round($value_converted, 2);
  148. }
  149. $result = $value_converted . " " . $measurement;
  150. // Return HTML div
  151. return "<div class='{$class}'>{$nutrient}: {$result}</div>";
  152. } catch (PDOException $e) {
  153. error_log("Database error in soilAnalysisReportCalcs: " . $e->getMessage());
  154. return "<div class='{$class}'>{$nutrient}: Error</div>";
  155. }
  156. }
  157. ?>