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
/
Copy pathfarm_grazing.install
289 lines (269 loc) · 7.73 KB
/
farm_grazing.install
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
<?php
/**
* @file
* Farm grazing install.
*/
/**
* Implements hook_install().
*/
function farm_grazing_install() {
// Create an initial set of animal types.
$animal_types = array(
array(
'name' => 'Beef cattle, lactating',
'dmi_factor' => 2.25,
),
array(
'name' => 'Beef cattle, growing and finishing, slaughter stock',
'dmi_factor' => 2.3,
),
array(
'name' => 'Dairy steers',
'dmi_factor' => 2.3,
),
array(
'name' => 'Dairy heifers',
'dmi_factor' => 2.5,
),
array(
'name' => 'Dairy cows, dry (small or large breed)',
'dmi_factor' => 1.8,
),
array(
'name' => 'Goats, weaned, slaughter or replacement stock',
'dmi_factor' => 2.25,
),
array(
'name' => 'Goats, brood or lactating',
'dmi_factor' => 4.0,
),
array(
'name' => 'Sheep, weaned, slaughter or replacement stock',
'dmi_factor' => 3.3,
),
array(
'name' => 'Sheep, brood or lactating',
'dmi_factor' => 3.65,
),
);
foreach ($animal_types as $type) {
db_insert('farm_grazing_animal_types')
->fields($type)
->execute();
}
}
/**
* Implements hook_schema().
*/
function farm_grazing_schema() {
$schema['farm_grazing_rotations'] = array(
'description' => 'Defines one herd rotation in a paddock in a plan',
'fields' => array(
'id' => array(
'description' => 'ID for this rotation',
'type' => 'serial',
'not null' => TRUE,
),
'plan_id' => array(
'description' => 'FK reference to a plan',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'herd_id' => array(
'description' => 'FK reference to a herd',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'paddock_id' => array(
'description' => 'FK reference to paddock',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'duration' => array(
'description' => 'Planned number of grazing days in paddock for this herd',
'type' => 'float',
'unsigned' => TRUE,
),
#
# this is actually computed as plan start date + sum(paddock durations)
# proceeding this rotation and adjusted for actuals
#
'start_date' => array(
'description' => 'Planned start date for rotation into paddock',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
),
'log_id' => array(
'description' => 'FK reference to the movement log',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => 0,
),
),
'primary key' => array('id'),
'indexes' => array(
'plan_id' => array('plan_id'),
'herd_id' => array('herd_id'),
'paddock_id' => array('paddock_id'),
'log_id' => array('log_id'),
'start_date' => array('start_date'),
),
);
/**
* @todo
* Use {farm_plan_area} table instead.
* Depends on moving paddock quality to logs.
*/
$schema['farm_grazing_plan_paddock'] = array(
'description' => 'Relate paddocks to a plan and productivity quality',
'fields' => array(
'paddock_id' => array(
'description' => 'FK reference to a paddock',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'plan_id' => array(
'description' => 'FK reference to a plan',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'quality' => array(
'description' => 'Relative paddock productivity quality (0-10)',
'type' => 'float',
'default' => '0.0',
),
),
'primary key' => array('paddock_id', 'plan_id'),
);
$schema['farm_grazing_plan_recovery'] = array(
'description' => 'Store min/max paddock recovery in days for a plan',
'fields' => array(
'plan_id' => array(
'description' => 'FK reference to a plan',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'month' => array(
'description' => 'Year month as int like: YYYYMM',
'type' => 'int',
'not null' => TRUE,
'default' => '0',
),
'min_recovery' => array(
'description' => 'Minimum paddock recovery in days for this month',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => '0',
),
'max_recovery' => array(
'description' => 'Minimum paddock recovery in days for this month',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'default' => '0',
),
),
'primary key' => array('plan_id', 'month'),
);
/**
* @todo
* Use {farm_plan_asset} table instead.
*/
$schema['farm_grazing_herds'] = array(
'description' => 'An arbitrary collection of farmOS groups that will be rotated together as a single herd.',
'fields' => array(
'plan_id' => array(
'description' => 'FK reference to a plan',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
'herd_id' => array(
'description' => 'FK reference to formOS group asset',
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
),
'primary key' => array('plan_id', 'herd_id'),
);
$schema['farm_grazing_animal_types'] = array(
'description' => 'List of animal types and their respective daily dry matter intake as percentage of weight.',
'fields' => array(
'type_id' => array(
'description' => 'Type ID',
'type' => 'serial',
'not null' => TRUE,
),
'name' => array(
'description' => 'Descriptive name for animal type',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
),
'dmi_factor' => array(
'description' => 'Daily Dry Matter Intake as percentage of body weight.',
'type' => 'float',
'not null' => TRUE,
),
),
'primary key' => array('type_id'),
'unique keys' => array(
'name' => array('name'),
),
);
return $schema;
}
/**
* Change planned arrival and departure dates to timestamps in the database.
*/
function farm_grazing_update_7000(&$sandbox) {
$result = db_query("SELECT * FROM {farm_asset_property} WHERE name IN ('farm_grazing_planned_arrival', 'farm_grazing_planned_departure')");
foreach ($result as $row) {
$row->value = strtotime($row->value);
drupal_write_record('farm_asset_property', $row, array('id', 'name'));
}
}
/**
* Remove the weight column from {farm_grazing_rotations} table.
*/
function farm_grazing_update_7001(&$sandbox) {
db_drop_field('farm_grazing_rotations', 'weight');
}
/**
* Migrate paddock exclusions to plan considerations.
*/
function farm_grazing_update_7002(&$sandbox) {
// Enable the Farm Plan Considerations module, if it isn't already.
$module = 'farm_plan_consideration';
if (!module_exists($module)) {
module_enable(array($module));
}
// Load all records from the exclusions table.
$result = db_query('SELECT * FROM {farm_grazing_paddock_exclusions}');
// Iterate through the exclusions.
foreach ($result as $record) {
// Create a new consideration.
$consideration = farm_plan_consideration_create('farm_grazing_exclusion', $record->reason, $record->start_date, $record->end_date);
// Link it to the paddock entity.
$consideration->entities = array(
'taxonomy_term' => array(
$record->paddock_id
),
);
// Save the consideration.
farm_plan_consideration_save($consideration);
}
// Drop the exclusions table.
db_drop_table('farm_grazing_paddock_exclusions');
}