This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
farm_grazing.utils.inc
1084 lines (943 loc) · 33.5 KB
/
farm_grazing.utils.inc
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
<?php
/**
* @file
* Grazing utility functions.
*/
/**
* Utility function to fetch things from farmOS
*
* farm_grazing_get_herd_count_for_plan(plan_id)
*
*/
function farm_grazing_get_herd_count_for_plan($plan_id) {
// return the equiv. to SELECT count(herd_id) FROM {farm_grazing_herds}
// WHERE planid=$plan
$query = db_select('farm_grazing_herds', 'h')
->fields('h', array('herd_id'))
->condition('h.plan_id', $plan_id);
return $query->countQuery()->execute()->fetchField();
}
/**
* farm_grazing_get_paddock_count_for_plan($plan)
*/
function farm_grazing_get_paddock_count_for_plan($plan_id) {
$query = db_select('farm_grazing_plan_paddock', 'a')
->fields('a', array('paddock_id'))
->condition('a.plan_id', $plan_id);
return $query->countQuery()->execute()->fetchField();
}
/**
* farm_grazing_get_selected_paddock_for_plan($plan_id)
*
* Get a list of all paddock_ids from farm_grazing_plan_paddock
*
* @param $plan_id
* The plan ID.
*
* @return array
* An array of paddock IDs.
*/
function farm_grazing_get_selected_paddock_for_plan($plan_id) {
// query table of paddocks for this plan
// and build an array of those that are currently selected
$paddocks = array();
$results = db_query('select paddock_id from {farm_grazing_plan_paddock} where plan_id=:plan', array(':plan' => $plan_id));
foreach ($results as $record) {
$paddocks[] = $record->paddock_id;
}
return $paddocks;
}
/**
* farm_grazing_get_selected_paddock_quality_for_plan($plan)
*
* Get a list of all paddock_ids and quality from farm_grazing_plan_paddock
*/
function farm_grazing_get_selected_paddock_quality_for_plan($plan) {
// query table of paddocks for this plan
// and build an array of those that are currently selected
$selected = array();
$results = db_query('select paddock_id, quality from {farm_grazing_plan_paddock} where plan_id=:plan', array(':plan' => $plan));
foreach ($results as $record) {
$selected[$record->paddock_id] = $record->quality;
}
return $selected;
}
/**
* farm_grazing_get_herd_earliest_arrival($herd_id, $plan_id)
*
* Recursive walk through herd and find the earliest arrival date for any
* part of the herd.
*
* @param $herd_id int
* @returns string YYYY-MM-DD
*/
function farm_grazing_get_herd_earliest_arrival($herd_id, $plan_id) {
$data = array();
$summary = farm_grazing_get_herd_data_and_summary($herd_id, $plan_id, $data);
return $summary['arrival'];
}
/**
* farm_grazing_get_paddock_grazing_days()
*
* @param duration - current saved value
* @param paddock_id int
* @param mode int
* 0 = use saved value or avg if saved is null
* 1 = force return to min grazing days
* 2 = force return to avg grazing days
* 3 = force return to max grazing days
*/
function farm_grazing_get_paddock_grazing_days($duration, $paddock_id, $plan_id, $herd_id, &$cache, $mode = 0) {
if ($cache == NULL) {
// build the cache as we are going to need it
$paddocks = farm_grazing_get_grazing_days_for_plan_id($plan_id, $d1, $d2, TRUE);
$cache = array();
foreach ($paddocks as $p) {
$cache[$p[5]] = $p;
}
}
$days = 0;
switch ($mode) {
case 0:
// use the duration if set
if (isset($duration)) {
$days = $duration;
}
// Otherwise set it to the average.
else {
if (isset($cache[$paddock_id])) {
$days = ($cache[$paddock_id][3] + $cache[$paddock_id][4]) / 2.0;
}
}
break;
// Set the grazing days to minimum.
case 1:
if (isset($cache[$paddock_id])) {
$days = $cache[$paddock_id][3];
}
break;
// Set the grazing days to average.
case 2:
if (isset($cache[$paddock_id])) {
$days = ($cache[$paddock_id][3] + $cache[$paddock_id][4]) / 2.0;
}
break;
// Set the grazing days to maximum.
case 3:
if (isset($cache[$paddock_id])) {
$days = $cache[$paddock_id][4];
}
break;
}
return $days;
}
/**
* farm_grazing_get_grazing_days_for_plan_id($plan_id, &$chk_min_recovery, &$chk_max_recovery, $include_ids = true, $add_links = false)
*/
function farm_grazing_get_grazing_days_for_plan_id($plan_id, &$chk_min_recovery, &$chk_max_recovery, $include_ids = true, $add_links = false) {
// get the plan id
$plan = $plan_id;
// get the herd count
$herd_cnt = farm_grazing_get_herd_count_for_plan($plan);
if ($herd_cnt == 0) {
$herd_cnt = 1;
}
// get the paddock count
$paddock_cnt = farm_grazing_get_paddock_count_for_plan($plan);
// Load an entity metadata wrapper for the plan.
$plan_wrapper = entity_metadata_wrapper('farm_plan', $plan);
// Check to see if it is a growing or non-growing season plan.
$growing_season = $plan_wrapper->field_grazing_growing_season->value();
// If this is a growing season plan, get recovery time from the database.
if (!empty($growing_season)) {
$results = db_query('SELECT min(min_recovery) as minmin, max(max_recovery) as maxmax FROM {farm_grazing_plan_recovery} WHERE plan_id=:plan', array(':plan' => $plan));
foreach ($results as $record) {
$minmin = $record->minmin;
$maxmax = $record->maxmax;
}
}
// Otherwise, calculate the recovery time for non-growing season.
else {
// Load an entity metadata wrapper for the plan.
$wrapper = entity_metadata_wrapper('farm_plan', $plan);
// Load the necessary variables from the plan.
$start_ts = $wrapper->field_farm_date_range->value->value();
$end_ts = $wrapper->field_farm_date_range->value2->value();
$days_bulk_feeding = $wrapper->field_days_bulk_feeding->value();
$days_of_drought_reserve = $wrapper->field_days_of_drought_reserve->value();
$expected_rotations = $wrapper->field_expected_rotations->value();
// Calculate the expected days of non-growth.
$expected_days_non_growth = floor(($end_ts - $start_ts) / 3600 / 24);
// Calculate the recovery period for all paddocks.
$recovery_period = ($expected_days_non_growth + $days_of_drought_reserve
- $days_bulk_feeding) / $expected_rotations;
// Min and max grazing are the same in non-growing seasons.
$minmin = $recovery_period;
$maxmax = $recovery_period;
}
// avoid divide by zero
$divisor = round($paddock_cnt - $herd_cnt);
if ($divisor == 0) {
$divisor = 1;
}
$avg_min_graz = $minmin / $divisor;
$avg_max_graz = $maxmax / $divisor;
// get a list of selected paddocks (paddock_id, quality)
$paddocks = array();
$sum_tot_quality = 0;
$results = db_query('select paddock_id, quality from {farm_grazing_plan_paddock} where plan_id=:plan', array(':plan' => $plan));
foreach ($results as $record) {
$term = taxonomy_term_load($record->paddock_id);
$area = farm_area_calculate_area($record->paddock_id);
$quality = $record->quality;
$name = $term->name;
if ($add_links) {
$name = l($name, url('taxonomy/term/' . $record->paddock_id));
}
$data = array(
$name,
round($area, 1),
round($area * $quality, 1),
$area * $quality * $avg_min_graz,
$area * $quality * $avg_max_graz,
);
if ($include_ids) {
$data[] = $record->paddock_id;
}
$paddocks[] = $data;
$sum_tot_quality = $sum_tot_quality + $area * $quality;
}
// get average
if ($paddock_cnt > 0) {
$avg_days = $sum_tot_quality / $paddock_cnt;
}
else {
$avg_days = 0;
}
// adjust the act min/max grazing days by avg days
// and avoid divide by zero
$sum_min = 0;
$sum_max = 0;
$sum_area = 0;
$sum_quality = 0;
$max_min = 0;
$max_max = 0;
for ($i = 0; $i < count($paddocks); $i++) {
if ($avg_days > 0) {
$paddocks[$i][3] = round($paddocks[$i][3] / $avg_days, 1);
$paddocks[$i][4] = round($paddocks[$i][4] / $avg_days, 1);
}
else {
$paddocks[$i][3] = 0;
$paddocks[$i][4] = 0;
}
$sum_area = $sum_area + $paddocks[$i][1];
$sum_quality = $sum_quality + $paddocks[$i][2];
$sum_min = $sum_min + $paddocks[$i][3];
$sum_max = $sum_max + $paddocks[$i][4];
if ($paddocks[$i][3] > $max_min) {
$max_min = $paddocks[$i][3];
}
if ($paddocks[$i][4] > $max_max) {
$max_max = $paddocks[$i][4];
}
}
// append a footer row to the data
$data = array(
t('Sum of Paddocks'),
round($sum_area, 1),
round($sum_quality, 1),
$sum_min,
$sum_max,
);
if ($include_ids) {
$data[] = -1;
}
$paddocks[] = $data;
$chk_min_recovery = $sum_min - $max_min;
$chk_max_recovery = $sum_max - $max_max;
return $paddocks;
}
/**
* Get Farm Plan Dates from plan id and optionally format them.
*/
function farm_grazing_get_farm_plan_dates_from_id($plan_id, $format = NULL) {
$plan_obj = farm_plan_load($plan_id);
return farm_grazing_get_farm_plan_dates($plan_obj, $format);
}
/**
* Get Farm Plan Dates from plan object and optionally format them.
*/
function farm_grazing_get_farm_plan_dates($plan_obj, $format = NULL) {
$wrapper = entity_metadata_wrapper('farm_plan', $plan_obj->id);
$start_ts = $wrapper->field_farm_date_range->value->value();
$end_ts = $wrapper->field_farm_date_range->value2->value();
if ($format == NULL) {
return array($start_ts, $end_ts);
}
else {
return array(date($format, $start_ts), date($format, $end_ts));
}
}
/**
* farm_grazing_get_num_months_between($date1, $date2)
*
* @param $date1 string YYYY-MM-DD or timestamp
* @param $date2 string YYYY-MM-DD or timestamp
* @returns array($start_year, $start_month, $number_of_months)
*/
function farm_grazing_get_num_months_between($date1, $date2) {
if ($date1 > $date2) {
return array();
}
// convert timestamps
if (is_numeric($date1)) {
$date1 = date('Y-m-d', $date1);
}
if (is_numeric($date2)) {
$date2 = date('Y-m-d', $date2);
}
list($y1, $m1, $d1) = explode('-', $date1);
list($y2, $m2, $d2) = explode('-', $date2);
$num_months = 0;
$delta = $y2 - $y1;
if ($delta > 0) {
// add months to end of start year
// and add months into end year
$num_months += 12 - $m1 + 1 + $m2;
// add months for intervening years if any
$num_months += 12 * ($delta - 1);
}
else {
// add the number of months between start and end in same year
$num_months += $m2 - $m1 + 1;
}
return array($y1, $m1, $num_months);
}
/**
* farm_grazing_days_available_by_month($startDate1, $endDate1, $startDate2, $endDate2)
* Compute the days overlap per month between start and end dates
* for the given year
*
* @param $startDate1 string YYYY-MM-DD
* @param $endDate1 string YYYY-MM-DD
* @param $startDate2 string YYYY-MM-DD
* @param $endDate2 string YYYY-MM-DD
* @returns array of ints, number of days in each month of overlap
*/
function farm_grazing_days_available_by_month($startDate1, $endDate1, $startDate2, $endDate2) {
list($year, $mon, $num_months) = farm_grazing_get_num_months_between($startDate1, $endDate1);
for ($i = 0; $i < $num_months + 1; $i++) {
$month_start = $year . '-' . sprintf('%02d', $mon) . '-01';
if ($mon < 12) {
$month_end = $year . '-' . sprintf('%02d', $mon + 1) . '-01';
}
else {
$month_end = ($year + 1) . '-01-01';
}
// increment and adjust month, year
$mon++;
if ($mon > 12) {
$mon = 1;
$year++;
}
$days[] = farm_grazing_overlap_in_days($month_start, $month_end, $startDate2, $endDate2);
}
return $days;
}
/**
* farm_grazing_count_months($start_date, $end_date, &$months, $threshold=15)
*
* Used by Herds/History to count months grazed
*
* @param $start_date int, Timestamp
* @param $end_date int, Timestamp
* @param &$months array of 12 ints
* @param $threshold int, number of days in a month to count the month
*/
function farm_grazing_count_months($start_date, $end_date, &$months, $threshold = 15) {
// Convert timestamps to YYYY-MM-DD.
$start = date('Y-m-d', $start_date);
$end = date('Y-m-d', $end_date);
list($year, $mon, $num) = farm_grazing_get_num_months_between($start, $end);
for ($i = 0; $i < $num; $i++) {
$month_start = $year . '-' . sprintf('%02d', $mon) . '-01';
if ($mon < 12) {
$month_end = $year . '-' . sprintf('%02d', $mon + 1) . '-01';
}
else {
$month_end = ($year + 1) . '-01-01';
}
// increment and adjust month, year
$mon++;
if ($mon > 12) {
$mon = 1;
$year++;
}
// get the number of days in this month
$days = farm_grazing_overlap_in_days($month_start, $month_end, $month_start, $month_end);
if ($days >= $threshold) {
$months[$mon]++;
// Increment the total.
$months[13]++;
}
}
}
/**
* compute the farm_grazing_overlap_in_days($startDate1, $endDate1, $startDate2, $endDate2)
* What is the overlap, in whole days, of two time periods?
*
* @param $startDate1 string
* @param $endDate1 string
* @param $startDate2 string
* @param $endDate2 string
* @returns int Overlap in days
*/
function farm_grazing_overlap_in_days($startDate1, $endDate1, $startDate2, $endDate2) {
$overlap = farm_grazing_overlap_in_minutes($startDate1, $endDate1, $startDate2, $endDate2);
$days = round($overlap / 60 / 24);
return $days;
}
/**
* What is the overlap, in minutes, of two time periods?
*
* @param $startDate1 string
* @param $endDate1 string
* @param $startDate2 string
* @param $endDate2 string
* @returns int Overlap in minutes
*/
function farm_grazing_overlap_in_minutes($startDate1, $endDate1, $startDate2, $endDate2) {
// Figure out which is the later start time
$lastStart = $startDate1 >= $startDate2 ? $startDate1 : $startDate2;
// Convert that to an integer
$lastStart = strtotime($lastStart);
// Figure out which is the earlier end time
$firstEnd = $endDate1 <= $endDate2 ? $endDate1 : $endDate2;
// Convert that to an integer
$firstEnd = strtotime($firstEnd);
// Subtract the two, divide by 60 to convert seconds to minutes, and round down
$overlap = floor(($firstEnd - $lastStart) / 60);
// If the answer is greater than 0 use it.
// If not, there is no overlap.
return $overlap > 0 ? $overlap : 0;
}
/**
* compute number of days in a month
* @param year
* @param month
* @return int - number of days
*/
function farm_grazing_days_in_month($year, $month) {
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
/**
* compute days between date1 and date2
* date2 is before date1 return 0
* @param date1 - string, YYYY-MM-DD
* @param date2 - string, YYYY-MM-DD
* @return int - number of days between dates or 0
*/
function farm_grazing_get_days_between_dates($date1, $date2) {
if ($date1 > $date2) {
return 0;
}
// convert to an integer, seconds from epoch
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$minutes = ($date2 - $date1) / 60;
$days = $minutes / 60 / 24;
return $days;
}
/**
* farm_grazing_get_animal_type_name($type)
*
* Return name field from {farm_grazing_animal_types} table
*/
function farm_grazing_get_animal_type_name($type) {
return db_query('SELECT name FROM {farm_grazing_animal_types} WHERE type_id=:tid', array(':tid' => $type))->fetchField();
}
/**
* farm_grazing_get_animal_type_dmi_factor($type)
*
* Return dmi_factor field from {farm_grazing_animal_types} table
*/
function farm_grazing_get_animal_type_dmi_factor($type) {
$dmi_factor = db_query('SELECT dmi_factor FROM {farm_grazing_animal_types} WHERE type_id=:tid', array(':tid' => $type))->fetchField();
$dmi_factor = $dmi_factor / 100;
return $dmi_factor;
}
/**
* farm_grazing_get_grazing_group_detailed_info($asset_id, &$data)
*
* Recursively walk a nested group structure extracting records
* for each leaf node.
*
* Usage:
* $data = array();
* farm_grazing_get_grazing_group_detailed_info($asset_id, $data);
*
* Results:
* $data = array(
* array(
* 'id' => $asset_id,
* 'name' => $asset->name,
* 'type' => $type,
* 'arrival' => $arrival,
* 'departure' => $departure,
* 'head_count' => $head_count,
* 'weight' => $weight['value'],
* 'weight_units' => $weight['units'],
* 'dmi_factor' => $dmi_factor,
* ),
* ...
* );
*/
function farm_grazing_get_grazing_group_detailed_info($asset_id, &$data) {
// get the asset
$asset = farm_asset_load($asset_id);
// Ignore archived assets.
// This may be redundant because farm_group_members() already filters out
// archived assets by default.
if (!empty($asset->archived)) {
return;
}
// get the asset children and process them
if ($asset->type == 'group') {
$children = farm_group_members($asset);
if (!empty($children)) {
foreach ($children as $child) {
farm_grazing_get_grazing_group_detailed_info($child->id, $data);
}
}
}
else {
// collect the attributes for a leaf node
$type = farm_asset_property_get($asset_id, 'farm_grazing_animal_type');
$arrival = farm_asset_property_get($asset_id, 'farm_grazing_planned_arrival');
$departure = farm_asset_property_get($asset_id, 'farm_grazing_planned_departure');
// Get the group quantity for asset count. If it is an empty string, this
// means that no inventory adjustments have been added, so we assume that
// this is an individual animal and give it a head count of 1.
$head_count = farm_inventory($asset);
if ($head_count == '') {
$head_count = 1;
}
// Get the DMI factor.
$dmi_factor = farm_grazing_get_animal_type_dmi_factor($type);
// Get the weight value and units, if they exist.
$weight = farm_livestock_weight($asset);
$weight_value = !empty($weight['value']) ? $weight['value'] : '';
$weight_units = !empty($weight['units']) ? $weight['units'] : '';
// add the record to the results
$data[] = array(
'id' => $asset_id,
'name' => $asset->name,
'type' => $type,
'arrival' => !empty($arrival) ? date('Y-m-d', $arrival) : '',
'departure' => !empty($departure) ? date('Y-m-d', $departure) : '',
'head_count' => $head_count,
'weight' => $weight_value,
'weight_units' => $weight_units,
'dmi_factor' => $dmi_factor,
);
}
}
/**
* farm_grazing_summarize_group_detailed_info(&$data)
*
* Take the results of farm_grazing_get_grazing_group_detailed_info()
* and create a one record summary used by Herds/Manage
*
* Returns:
* array(
* 'head_count' => $head_count,
* 'herd_sau' => $sum_sau,
* 'herd_dmi' => $sum_dmi,
* 'arrival' => $min_arrival,
* 'departure' => $max_departure)
*
* Usage:
* $data = array();
* farm_grazing_get_grazing_group_detailed_info($asset_id, $data);
* $summary = farm_grazing_summarize_group_detailed_info($data);
*/
function farm_grazing_summarize_group_detailed_info(&$data) {
$sum_sau = 0;
$sum_dmi = 0;
$head_count = 0;
$min_arrival = FALSE;
$max_departure = FALSE;
// summarize the data
foreach ($data as $rec) {
$sum_sau = $sum_sau + $rec['head_count'] * $rec['weight'] / 1000.0;
$sum_dmi = $sum_dmi + $rec['head_count'] * $rec['weight'] * $rec['dmi_factor'];
$head_count = $head_count + $rec['head_count'];
// date strings MUST be for as 'Y-m-d' ie: YYYY-MM-DD
if ($min_arrival === FALSE or $rec['arrival'] < $min_arrival) {
$min_arrival = $rec['arrival'];
}
if ($max_departure === FALSE or $rec['departure'] > $max_departure) {
$max_departure = $rec['departure'];
}
}
// return result
return array(
'head_count' => $head_count,
'herd_sau' => $sum_sau,
'herd_dmi' => $sum_dmi,
'arrival' => $min_arrival,
'departure' => $max_departure,
);
}
/**
* Validate a herd in a plan.
*
* when a herd is attached to a grazing plan or otherwise modified
* this function checks that all components of the herd have required
* properties and initial observations.
*
* @param int $herd_id
* The herd (group asset) ID.
* @param int $plan_id
* The farm plan ID.
* @param array &$data
* An array of data to pass into farm_grazing_get_grazing_group_detailed_info().
* @param bool $silent
* Whether or not to output error messages using drupal_set_message().
* Defaults to FALSE (errors will show).
*
* @return int
* Returns the number of errors encountered.
*/
function farm_grazing_validate_herd($herd_id, $plan_id, &$data, $silent = FALSE) {
$errors = 0;
farm_grazing_get_grazing_group_detailed_info($herd_id, $data);
// Keep track of missing data.
$missing_type = 0;
$missing_weight = 0;
foreach ($data as &$animal) {
// Things to validate
// farm_grazing_animal_type property exists and is valid
$type = $animal['type'];
if (empty($type)) {
$missing_type++;
$errors++;
}
// has weight
$weight = $animal['weight'];
if (empty($weight)) {
$missing_weight++;
$errors++;
}
// TODO are all animals/groups are in some paddock
// TODO is asset attached to another active plan?
}
// Print errors (unless we should be silent).
if (empty($silent) && !empty($errors)) {
// General message.
drupal_set_message('Some required data is missing from the animals in this plan. This may adversely affect calculations. Please see the errors below and add any data that may be missing. Fields that are required will be highlighted in the form below, along with any values that are calculated from those fields.', 'error');
// Missing animal types.
if (!empty($missing_type)) {
drupal_set_message(t('@count animal(s) do not have an animal type.', array('@count' => $missing_type)), 'error');
}
// Missing weight.
if (!empty($missing_weight)) {
drupal_set_message(t('@count animal(s) are missing weight measurements.', array('@count' => $missing_weight)), 'error');
}
}
return $errors;
}
/**
* farm_grazing_get_herd_data_and_summary($herd_id, $plan_id, &$data)
*/
function farm_grazing_get_herd_data_and_summary($herd_id, $plan_id, &$data) {
farm_grazing_validate_herd($herd_id, $plan_id, $data);
return farm_grazing_summarize_group_detailed_info($data);
}
/**
* farm_grazing_get_herd_size_in_sau($plan_id)
*/
function farm_grazing_get_herd_size_in_sau($plan_id) {
// get list of herds in this plan
$query = db_query('SELECT * FROM {farm_grazing_herds} WHERE plan_id=:plan_id', array(':plan_id' => $plan_id));
$records = $query->fetchAll();
$tot_sau = 0;
foreach ($records as $record) {
if (empty($record->herd_id)) {
continue;
}
$data = array();
$summary = farm_grazing_get_herd_data_and_summary($record->herd_id, $plan_id, $data);
$tot_sau += $summary['herd_sau'];
}
return $tot_sau;
}
/**
* farm_grazing_get_non_growing_summary_for_plan_id($plan_id, $add_ids = FALSE, $add_links = FALSE)
*
* Compute statistics for paddocks in non-growing season plan
*/
function farm_grazing_get_non_growing_summary_for_plan_id($plan_id, $add_ids = FALSE, $add_links = FALSE) {
// get herds size if defined
$herd_size = farm_grazing_get_herd_size_in_sau($plan_id);
$wrapper = entity_metadata_wrapper('farm_plan', $plan_id);
$num_rotations = $wrapper->field_expected_rotations->value();
$num_paddocks = farm_grazing_get_paddock_count_for_plan($plan_id);
$days_drought_reserve = $wrapper->field_days_of_drought_reserve->value();
$days_bulk_feeding = $wrapper->field_days_bulk_feeding->value();
$days_drought_grazing = $days_drought_reserve / $num_paddocks;
$start_ts = $wrapper->field_farm_date_range->value->value();
$end_ts = $wrapper->field_farm_date_range->value2->value();
$plan_days = floor(($end_ts - $start_ts) / 3600 / 24);
$expected_days_non_growth = $plan_days - $days_drought_reserve + $days_bulk_feeding;
$grazing_period = ($expected_days_non_growth + $days_drought_reserve - $days_bulk_feeding) / $num_rotations / $num_paddocks;
// each row in $rows consists of
// paddock, area, ada, avail_ads, max_grazing_days, planned_harvest_per_rotation, remaining_harvest_aft_first, planned_tot_harvest, forage_excess_deficit, avail_drought_ads, max_drought_grazing_days
$rows = array();
$sums = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$results = db_query('select paddock_id, quality from {farm_grazing_plan_paddock} where plan_id=:plan', array(':plan' => $plan_id));
foreach ($results as $record) {
$name = taxonomy_term_load($record->paddock_id)->name;
$area = farm_area_calculate_area($record->paddock_id);
$ada = $record->quality;
if ($add_links) {
$name = l($name, url('taxonomy/term/' . $record->paddock_id));
}
$row = array(
$name, // paddock
$area, // 8, area (needs to be round)
$ada, // 9, ada
$area * $ada, // 0, avail_ads (needs to be round)
$area * $ada * $grazing_period, // 1, max_grazing_days (needs / avg_ads)
$herd_size * $area * $ada/$area, // 2, planned_harvest_per_rotation (needs / avg_ads)
$ada, // 3, remaining_harvest_aft_first (needs - $row[5])
$num_rotations, // 4, planned_tot_harvest (needs * $row[5])
$days_bulk_feeding, // 5, forage_excess_deficit (needs - $row[7])
$area, // 6, avail_drought_ads (needs * $row[8])
$days_drought_reserve,// 7, max_drought_grazing_days (needs *$row[9]/(sums[6]/$num_paddocks
);
if ($add_ids) {
$row[] = $record->paddock_id;
}
$rows[] = $row;
// accumulate some sums
// [0] available ads
// [1] max. grazing days
// [2] planned harvest per rotation
for ($i = 0; $i < 2; $i++) {
$sums[$i] += $row[$i + 3];
}
}
// fix up sums and rows as needed
$avg_ads = $sums[0] / $num_paddocks;
// max. grazing days.
$sums[1] = $sums[1] / $avg_ads;
for ($i = 0; $i < $num_paddocks; $i++) {
$rows[$i][4] = $rows[$i][4] / $avg_ads;
$rows[$i][5] = $herd_size * $rows[$i][4] / $rows[$i][1];
$rows[$i][6] = $rows[$i][2] - $rows[$i][5];
$rows[$i][7] = $rows[$i][5] * $num_rotations;
$rows[$i][8] = $rows[$i][2] - $rows[$i][7];
$rows[$i][9] = $rows[$i][1] * $rows[$i][8];
// accumulate more sums
$sums[2] += $rows[$i][5]; // planned harvest per rotation
$sums[3] += $rows[$i][6]; // remaining ada aft.first rotation
$sums[4] += $rows[$i][7]; // planned total harvest
$sums[5] += $rows[$i][8]; // forage excess/deficit - planned drought reserve
$sums[6] += $rows[$i][9]; // available drought ads
}
$avg_avail_drought_ads = $sums[6] / $num_paddocks;
for ($i = 0; $i < $num_paddocks; $i++) {
$rows[$i][10] = $rows[$i][9] * $days_drought_grazing / $avg_avail_drought_ads;
$sums[7] += $rows[$i][10]; // max. drought grazing days
$sums[8] += $rows[$i][1]; // area
$sums[9] += $rows[$i][2]; // ada/h
}
// push the totals
$totals = array('<b>' . t('Column Sums') . '</b>', $sums[8], $sums[9]);
for ($i = 0; $i < 8; $i++) {
$totals[] = $sums[$i];
}
$rows[] = $totals;
// push the averages
$totals[0] = '<b>' . t('Column Average') . '</b>';
for ($i = 1; $i < 11; $i++) {
$totals[$i] /= $num_paddocks;
}
$rows[] = $totals;
// round table values to one digit
for ($i = 0; $i < $num_paddocks + 2; $i++) {
for ($j = 1; $j < 11; $j++) {
$rows[$i][$j] = round($rows[$i][$j], 1);
}
}
return $rows;
}
/**
* Load rotations for a given plan and herd.
*
* @param $plan_id
* The plan ID.
* @param $herd_id
* The herd ID (group asset ID).
*
* @return array
* Returns an array of rotations loaded from the database, keyed by ID.
*/
function farm_grazing_load_rotations($plan_id, $herd_id) {
$query = db_select('farm_grazing_rotations', 'r');
$query->fields('r');
$query->leftJoin('taxonomy_term_data', 't', 'r.paddock_id = t.tid');
$query->addField('t', 'name');
$query->condition('plan_id', $plan_id);
$query->condition('herd_id', $herd_id);
$query->orderBy('start_date', 'ASC');
return $query->execute()->fetchAllAssoc('id');
}
/**
* Recalculate the rotations for a plan and save them, along with their logs.
*
* @param @plan_id
* The plan ID.
* @param @herd_id
* The herd ID (group asset ID).
* @param $rotations
* Optionally, an array of rotation records to recalculate. If this is
* omitted, the rotations will be loaded from the database.
* @param $reset_durations
* Optionally use this parameter to reset the rotation's duration (in grazing
* days) to one of the following options:
* 0 = no change
* 1 = minimum
* 2 = average
* 3 = maximum
* @param int $start_time
* Optionally set a start time for the first rotation. If omitted, the plan's
* start time will be used instead.
*/
function farm_grazing_recalculate_rotations($plan_id, $herd_id, $rotations = array(), $reset_durations = 0, $start_time = NULL) {
// If rotations are not provided, load them from the plan.
if (empty($rotations)) {
// Load rotations from the database.
$rotations = farm_grazing_load_rotations($plan_id, $herd_id);
// If no rotations are loaded, bail.
if (empty($rotations)) {
return;
}
}
// Make sure $reset_durations is a valid option.
if (!in_array($reset_durations, array(0, 1, 2, 3))) {
$reset_durations = 0;
}
// If we are resetting the grazing days, build grazing days data for the
// paddocks.
if ($reset_durations != 0) {
$paddocks = farm_grazing_get_grazing_days_for_plan_id($plan_id, $d1, $d2, TRUE);
$data = array();
if (!empty($paddocks)) {
foreach ($paddocks as $p) {
$data[$p[5]] = $p;
}
}
}
// If a $start_time is not provided, use the start time of the plan.
if (empty($start_time)) {
$plan_wrapper = entity_metadata_wrapper('farm_plan', $plan_id);
$start_time = $plan_wrapper->field_farm_date_range->value->value();
}
// Get the first rotation's array key, if it exists. This will be used to
// perform specific logic on the first rotation in the logic below.
$first = NULL;
if (!empty($rotations)) {
reset($rotations);
$first = key($rotations);
}
// Iterate through the rotations, keeping track of the timestamp after each.
$timestamp = $start_time;
foreach ($rotations as $rotation_id => $rotation) {
// Load the rotation log.
if (!empty($rotation->log_id)) {
$log = log_load($rotation->log_id);
}
// If the log is missing, stop everything and alert the user.
if (empty($log)) {
drupal_set_message(t('A log is missing from one or more rotations. Remove the rotation and recreate it to fix this issue.'));
return;
}
// If this is the first rotation, and it's timestamp is different than that
// of the plan, start with the rotation's timestamp instead of the plan's.
// This covers cases where a plan is started with one date, but the reality
// is that the first movement doesn't take place until later.
if (!empty($first) && $rotation_id == $first && $log->timestamp != $timestamp) {
$timestamp = $log->timestamp;