soil-analysis.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /**
  3. * dashboard/crop-analysis/soil-analysis.php
  4. *
  5. * Soil Analysis Results Display Page
  6. * Shows detailed soil test results with calculations and recommendations
  7. */
  8. require_once __DIR__.'/../../config/database.php';
  9. require_once __DIR__.'/../../lib/auth.php';
  10. require_once __DIR__.'/../../lib/validation.php';
  11. // Start session if not already started
  12. if (session_status() === PHP_SESSION_NONE) {
  13. session_start();
  14. }
  15. // Require authentication
  16. requireLogin();
  17. // Get and validate parameters
  18. $client_id = (int)($_GET['cid'] ?? 0);
  19. $record_id = (float)($_GET['rid'] ?? 0);
  20. $rand_id = (float)($_GET['rand'] ?? 0);
  21. // Validate required parameters
  22. if (!$record_id || !$rand_id) {
  23. die('Invalid request parameters');
  24. }
  25. // Get soil record data securely
  26. try {
  27. $pdo = getDBConnection();
  28. $stmt = $pdo->prepare("SELECT * FROM soil_records WHERE id = ? AND rand = ?");
  29. $stmt->execute([$record_id, $rand_id]);
  30. $row = $stmt->fetch(PDO::FETCH_ASSOC);
  31. if (!$row) {
  32. die('Soil record not found');
  33. }
  34. // Extract data
  35. $client = htmlspecialchars($row['client_name'] ?? '', ENT_QUOTES, 'UTF-8');
  36. $address = htmlspecialchars($row['site_address'] ?? '', ENT_QUOTES, 'UTF-8');
  37. $state = htmlspecialchars($row['state_postcode'] ?? '', ENT_QUOTES, 'UTF-8');
  38. $email = htmlspecialchars($row['email'] ?? '', ENT_QUOTES, 'UTF-8');
  39. $labNo = htmlspecialchars($row['lab_no'] ?? '', ENT_QUOTES, 'UTF-8');
  40. $sampleDate = htmlspecialchars($row['date_sampled'] ?? '', ENT_QUOTES, 'UTF-8');
  41. $sample = htmlspecialchars($row['site_id'] ?? '', ENT_QUOTES, 'UTF-8');
  42. $crop = htmlspecialchars($row['sample_id'] ?? '', ENT_QUOTES, 'UTF-8');
  43. } catch (PDOException $e) {
  44. error_log("Database error in soil-analysis.php: " . $e->getMessage());
  45. die('Database error occurred');
  46. }
  47. $today = date('jS F Y');
  48. $pageTitle = 'Soil Analysis Results - ' . $client;
  49. $siteName = 'Crop Management Platform';
  50. $activeItem = 'Soil Analysis';
  51. include __DIR__.'/../../layouts/header.php';
  52. include __DIR__.'/../../layouts/navbar.php';
  53. ?>
  54. <div id="layoutSidenav">
  55. <div id="layoutSidenav_nav">
  56. <?php include __DIR__.'/../../layouts/sidebar.php'; ?>
  57. </div>
  58. <div id="layoutSidenav_content">
  59. <main>
  60. <div class="container-fluid px-4"> <div class="row">
  61. <?php
  62. // Replace Logo with Customer Logo if supplied.
  63. $client = '';
  64. echo "<div class='col-md-3'>";
  65. if ($client === "") {
  66. echo "<img class='img-fluid' src='client-assets/images/crop-monitor.png' alt='Crop Monitor' >";
  67. } else {
  68. echo "<img class='img-fluid' src='client-assets/images/crop-monitor.png' alt='Crop Monitor' >";
  69. }
  70. //echo "<span class='col'></span>";
  71. //Client Test Description
  72. if ($client === "") {
  73. echo "";
  74. } else {
  75. echo "<img class='img-fluid' src='client-assets/images/crop-monitor.png' alt='Crop Monitor' >";
  76. }
  77. echo "</div>";
  78. echo "<div class='col-md-9'></div>";
  79. ?>
  80. </div>
  81. <table class='title'>
  82. <tbody>
  83. <tr>
  84. <th class='col-20'></th>
  85. <th class='col-20'></th>
  86. <th class='col-20'></th>
  87. <th class='col-20'></th>
  88. <th class='col-20'></th>
  89. </tr>
  90. <tr>
  91. <td class='right'><b>DATE:</b></td>
  92. <td class='left'><?php echo $today; ?></td>
  93. <td></td>
  94. <td class='right'><b>SAMPLE ID:</b></td>
  95. <td class='left'><?php echo $sample; ?></td>
  96. </tr>
  97. <tr>
  98. <td class='right'><b>CLIENT:</b></td>
  99. <td class='left'><?php echo $client; ?></td>
  100. <td></td>
  101. <td class='right'><b>DATE SAMPLED:</b></td>
  102. <td class='left'><?php echo $sampleDate; ?></td>
  103. </tr>
  104. <tr>
  105. <td class='right'><b>ADDRESS:</b></td>
  106. <td class='left'><?php echo $address; ?></td>
  107. <td></td>
  108. <td class='right'><b>LAB NUMBER:</b></td>
  109. <td class='left'><?php echo $labNo; ?></td>
  110. </tr>
  111. <tr>
  112. <td class='right'><b> </b></td>
  113. <td class='left'><?php echo $state; ?></td>
  114. <td></td>
  115. <td class='right'><b>CROP:</b></td>
  116. <td class='left'><?php echo $crop; ?></td>
  117. </tr>
  118. <tr>
  119. <td class='right'><b> </b></td>
  120. <td class='left'><?php echo $email; ?></td>
  121. <td></td>
  122. <td class='right'></td>
  123. <td class='left'></td>
  124. </tr>
  125. </tbody>
  126. </table>
  127. <!-- Graph Button -->
  128. <div class="d-print-none">
  129. <div class="row p-2">
  130. <div class="col">
  131. <button type="button" class="btn btn-primary" onclick="generateGraph()">
  132. <i class="fas fa-chart-bar"></i> Generate Graph
  133. </button>
  134. </div>
  135. <div class="col">
  136. <div class="form-status-holder"></div>
  137. </div>
  138. </div>
  139. </div>
  140. <!-- GRAPH BANNER -->
  141. <div class="row">
  142. <div class="col-md-12 text-center font-weight-bold h4">Soil Analysis Summary</div>
  143. </div>
  144. <!-- CHART HEADER -->
  145. <div class="row bg-dark text-white p-2 mt-3">
  146. <div class="text-center col-md-12 h5">Total kilograms per hectare of each element needed to balance soil in this test</div>
  147. </div>
  148. <div class="row">
  149. <div class="">
  150. <?php
  151. require_once __DIR__.'/../../lib/soil_calculations.php';
  152. echo soilAnalysisReportCalcs('Ca', 'BS_ca_ppm', 'ca_ppm_min', 'ca_ppm_max', 'Calcium', 'kg', 'col', $record_id, $rand_id);
  153. echo soilAnalysisReportCalcs('Mg', 'BS_mg_ppm', 'mg_ppm_min', 'mg_ppm_max', 'Magnesium', 'kg', 'col', $record_id, $rand_id);
  154. echo soilAnalysisReportCalcs('K', 'BS_k_ppm', 'k_ppm_min', 'k_ppm_max', 'Potasium', 'kg', 'col', $record_id, $rand_id);
  155. echo soilAnalysisReportCalcs('Na', 'BS_na_ppm', 'na_ppm_min', 'na_ppm_max', 'Sodium', 'kg', 'col', $record_id, $rand_id);
  156. echo soilAnalysisReportCalcs('P', 'p_colwell', '', '', 'Phosphate', 'kg', 'col', $record_id, $rand_id);
  157. echo soilAnalysisReportCalcs('S', 's_morgan', '', '', 'Sulfur', 'kg', 'col', $record_id, $rand_id);
  158. echo soilAnalysisReportCalcs('Mn', 'mn_dtpa', '', '', 'Manganese', 'kg', 'col', $record_id, $rand_id);
  159. echo soilAnalysisReportCalcs('Fe', 'fe_dtpa', '', '', 'Iron', 'kg', 'col', $record_id, $rand_id);
  160. echo soilAnalysisReportCalcs('Zn', 'zn_dtpa', '', '', 'Zinc', 'kg', 'col', $record_id, $rand_id);
  161. echo soilAnalysisReportCalcs('Cu', 'cu_dtpa', '', '', 'Copper', 'kg', 'col', $record_id, $rand_id);
  162. echo soilAnalysisReportCalcs('AmN', 'NH3_N', '', '', 'AmNitrogen', 'kg', 'col', $record_id, $rand_id);
  163. echo soilAnalysisReportCalcs('B', 'b_cacl2', '', '', 'Boron', 'kg', 'col', $record_id, $rand_id);
  164. echo soilAnalysisReportCalcs('NN', 'NO3_N', '', '', 'NNitrogen', 'kg', 'col', $record_id, $rand_id);
  165. ?>
  166. </div>
  167. </div>
  168. <hr>
  169. <!-- **************** START OF FORM DATA **************** -->
  170. <form class="report-form" method="post">
  171. <input type="hidden" name="record_id" value="<?php echo htmlspecialchars($record_id, ENT_QUOTES, 'UTF-8'); ?>">
  172. <input type="hidden" name="rand_id" value="<?php echo htmlspecialchars($rand_id, ENT_QUOTES, 'UTF-8'); ?>">
  173. <!-- **************** OVERVIEW SECTION **************** -->
  174. <div class="row bg-dark text-white p-2 mt-3">
  175. <div class="text-center col-md-12 h5 ">Overview</div>
  176. </div>
  177. <div class="row">
  178. <div class="col-md-12 p-0" >
  179. <textarea class="form-control rounded-0" rows="5" id="overview" name="overview" ></textarea>
  180. </div>
  181. </div>
  182. <hr>
  183. <div class="row bg-dark text-white p-2 mt-3">
  184. <div class="text-center col-md-12 h5 ">Ideal Soil Balancing Program for One Season of a FIVE YEAR Plan</div>
  185. </div>
  186. <div class="">
  187. <?php
  188. // Generate 5-year soil balancing program
  189. for ($year = 1; $year <= 5; $year++) {
  190. echo soilProgramCalcs('Ca', 'BS_ca_ppm', 'ca_ppm_min', 'ca_ppm_max', 'Calcium', 'kg', $record_id, $rand_id);
  191. }
  192. ?>
  193. </div>
  194. <hr>
  195. <!-- **************** FOLIAR PROGRAM SECTION **************** -->
  196. <div class="row bg-dark text-white p-2 mt-3">
  197. <div class="text-center col-md-12 h5" ><input type="text" class="text-center form-control-plaintext text-white border-dark bg-dark" name="header1" id="header1" value="Foliar Program"></div>
  198. </div>
  199. <div class="row">
  200. <div class="col-md-12 p-0" >
  201. <textarea class="form-control rounded-0" rows="5" id="foliar_Details" name="foliar_Details"></textarea>
  202. </div>
  203. </div>
  204. <!-- **************** MICROBE PROGRAM SECTION **************** -->
  205. <div class="row bg-dark text-white p-2 mt-3">
  206. <div class="text-center col-md-12 h5">Microbe Program</div>
  207. </div>
  208. <div class="row">
  209. <div class="col-md-12 p-0" >
  210. <textarea class="form-control rounded-0" rows="5" id="microbe_Program" name="microbe_Program" ></textarea>
  211. </div>
  212. </div>
  213. <div class="row">
  214. <p style="font-style: italic; font-size: 9px;">Any recommendations provided by Cropmonitor are advice only, We are not paid consultants and we are not covered to accept responsibiliy for any of our suggestions. As no control can be exercised over storage, handling, mixing application or use, or weather, plant or soil conditions before, during or after application (all of which may affect the preformance of our program), no responsibility for, or liability for any failure in performance, losses, damage or injuries consequential or otherwise, arisiing form such storage mixng application or use will be accepted under any circumstances whatsoever. The buyer assumes all responsibility for the use of any of our products.</p>
  215. </div>
  216. </form>
  217. </div>
  218. </main>
  219. <footer class="py-4 bg-light mt-auto">
  220. <div class="container-fluid px-4">
  221. <div class="d-flex align-items-center justify-content-between small">
  222. <div class="text-muted">&copy; <?= date('Y') ?> Crop Management Platform. All Rights Reserved.</div>
  223. <div><a href="/privacy-policy.php">Privacy Policy</a> &middot; <a href="/terms.php">Terms &amp; Conditions</a></div>
  224. </div>
  225. </div>
  226. </footer>
  227. </div>
  228. </div>
  229. <?php include __DIR__.'/../../layouts/footer.php'; ?>
  230. <script>
  231. function generateGraph() {
  232. alert('Graph generation functionality will be implemented here.');
  233. }
  234. $(document).ready(function(){
  235. var timeoutId;
  236. $('form textarea, form input').on('input propertychange change', function() {
  237. console.log('Textarea Change');
  238. clearTimeout(timeoutId);
  239. timeoutId = setTimeout(function() {
  240. // Runs 1 second (1000 ms) after the last change
  241. saveToDB();
  242. }, 1000);
  243. });
  244. function saveToDB() {
  245. console.log('Saving to the db');
  246. form = $('.report-form');
  247. $.ajax({
  248. url: "/controllers/save_soil_analysis.php",
  249. type: "POST",
  250. data: form.serialize(), // serializes the form's elements.
  251. beforeSend: function(xhr) {
  252. // Let them know we are saving
  253. $('.form-status-holder').html('Saving...');
  254. },
  255. success: function(data) {
  256. var jqObj = jQuery(data); // You can get data returned from your ajax call here. ex. jqObj.find('.returned-data').html()
  257. // Now show them we saved and when we did
  258. var d = new Date();
  259. $('.form-status-holder').html('Saved! Last: ' + d.toLocaleTimeString());
  260. },
  261. });
  262. }
  263. // This is just so we don't go anywhere
  264. // and still save if you submit the form
  265. $('.report-form').submit(function(e) {
  266. saveToDB();
  267. e.preventDefault();
  268. });
  269. });
  270. </script>