added the list of management factor types in controllers
[sgn.git] / mason / breeders_toolbox / seedlot_details.mas
blobd4198e408ba0243d1d3c48ed0d9e966ca2fdf29b
2 <%args>
3 $seedlot_id
4 $uniquename
5 $current_count => 0
6 $current_weight => 0
7 $content_html
8 $content_accession_name
9 $content_cross_name
10 $organization_name
11 $population_name
12 $timestamp
13 $box_name=>undef
14 $owners_string => undef
15 </%args>
17 <& /util/import_javascript.mas, classes => [ 'bootstrap_min.js', 'jquery.iframe-post-form','CXGN.List','CXGN.BreedersToolbox.Accessions', 'CXGN.BreedersToolbox.UploadPedigrees', 'jquery', 'jquery.dataTables' ] &>
19 <style>
20 .ui-autocomplete {
21     z-index: 5000;
23 </style>
27 <& /page/page_title.mas, title => 'Seedlot '.$uniquename &>
29 <&| /page/info_section.mas, title=>"Details",  collapsible => 1, collapsed=>0, subtitle=>"[<a id='edit_seedlot_details_link' >Edit Seedlot Details</a>]" &>
31 <table class="table table-bordered table-hover">
32 <tr><td><b>Breeding Program</b></td><td><div id="seedlot_breeding_program_div">[LOADING...]</div></td></tr>
33 <tr><td><b>Seedlot Name</b></td><td><a href="/stock/<% $seedlot_id %>/view"><% $uniquename %></a></td></tr>
34 <tr><td><b>Organization</b></td><td><% $organization_name %></td></tr>
35 <tr><td><b>Location Code</b><td><div id="seedlot_location_code_div">[LOADING...]</div></td></tr>
36 <tr><td><b>Box Name</b></td><td><% $box_name %></td></tr>
37 <tr><td><b>Contents</b></td><td><% $content_html %></td></tr>
38 <!--<tr><td><b>Population</b></td><td><% $population_name %></td></tr>-->
39 <tr><td><b>Current count</b></td><td><% $current_count %></td></tr>
40 <tr><td><b>Current weight (g)</b></td><td><% $current_weight %></td></tr>
41 <tr><td><b>Submitters</b></td><td><% $owners_string %></td></tr>
42 </table>
44 </&>
46 <center>
47 <h3>Transactions</h3>
48 </center>
50 <&| /page/info_section.mas, title=>"Transactions Table",  collapsible => 1, collapsed=>0, subtitle=>"[<a id='add_seedlot_transaction_button' >Add New Transaction</a>]" &>
52 <div style="overflow:scroll">
53     <table id="available_seedlot_transactions_table" class="table table-hover table-bordered">
54     </table>
55 </div>
57 </&>
59 <div class="modal fade" id="add_seedlot_transaction_dialog" name="add_seedlot_transaction_dialog" tabindex="-1" role="dialog" aria-labelledby="seedlot_transaction_dialog">
60   <div class="modal-dialog modal-xl" role="document">
61     <div class="modal-content">
62       <div class="modal-header">
63         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
64         <h4 class="modal-title" id="seedlot_transaction_dialog">Add New Seedlot Transaction</h4>
65       </div>
66       <div class="modal-body">
67         <div class="container-fluid">
68             <form class="form-horizontal" role="form" method="post" id="create_new_seedlot_transaction_form" name="create_new_seedlot_transaction_form">
70                 <div class="form-group">
71                     <label class="col-sm-3 control-label">Transaction Type: </label>
72                     <div class="col-sm-9" >
73                         <select class="form-control" id="seedlot_transaction_factor">
74                             <option value="1">Added to this Seedlot (<% $uniquename %>)</option>
75                             <option value="-1">Taken from this Seedlot (<% $uniquename %>)</option>
76                         </select>
77                     </div>
78                 </div>
80                 <div id="seedlot_transaction_from_seedlot">
81                     <div class="form-group">
82                         <label class="col-sm-3 control-label">Taken From Existing Seedlot:<br/><small>Only showing seedlots with matching content</small></label>
83                         <div class="col-sm-9" >
84                             <div id="seedlot_transaction_seedlot_div"></div>
85                         </div>
86                     </div>
87                 </div>
89                 <div id="seedlot_transaction_to_seedlot" style="display: none">
90                     <div class="form-group">
91                         <label class="col-sm-3 control-label">Transferring to a new seed lot?: </label>
92                         <div class="col-sm-9" >
93                             <select class="form-control" id="seedlot_transaction_to_seedlot_new">
94                                 <option value="no">No</option>
95                                 <option value="yes">Yes</option>
96                             </select>
97                         </div>
98                     </div>
99                     <div id="seedlot_transaction_to_seedlot_new_no">
100                         <div class="form-group">
101                             <label class="col-sm-3 control-label">To Existing Seedlot:<br/><small>Only showing seedlots with matching content</small> </label>
102                             <div class="col-sm-9" >
103                                 <div id="seedlot_transaction_to_seedlot_existing"></div>
104                             </div>
105                         </div>
106                     </div>
107                     <div id="seedlot_transaction_to_seedlot_new_yes" style="display: none">
108                         <div class="well well-sm">
109                             <div class="panel panel-default">
110                                 <div class="panel-body">
111                                     <div class="form-group">
112                                         <label class="col-sm-3 control-label">To New Seedlot: </label>
113                                         <div class="col-sm-9" >
115                                             <div class="form-group">
116                                                 <label class="col-sm-3 control-label">Name: </label>
117                                                 <div class="col-sm-9" >
118                                                     <input class="form-control" id="transaction_new_seedlot_name" placeholder="Required">
119                                                 </div>
120                                             </div>
121                                             <div class="form-group">
122                                                 <label class="col-sm-3 control-label">Breeding Program: </label>
123                                                 <div class="col-sm-9" >
124                                                     <div id="transaction_new_seedlot_breeding_program_div"></div>
125                                                 </div>
126                                             </div>
127                                             <div class="form-group">
128                                                 <label class="col-sm-3 control-label">Location: </label>
129                                                 <div class="col-sm-9" >
130                                                     <input class="form-control" id="transaction_new_seedlot_location" placeholder="Required">
131                                                 </div>
132                                             </div>
133                                             <div class="form-group">
134                                                 <label class="col-sm-3 control-label">Box Name: </label>
135                                                 <div class="col-sm-9" >
136                                                     <input class="form-control" id="transaction_new_seedlot_box_name" placeholder="Required">
137                                                 </div>
138                                             </div>
140                                             <div class="well well-sm">
141                                                 <div class="row">
142                                                     <div class="col-sm-3">
143                                                         <div class="form-group">
144                                                             <label class="col-sm-12 control-label">Contents:<br/><small>Contents must match between seedlots</small> </label>
145                                                         </div>
146                                                     </div>
147                                                     <div class="col-sm-9">
148                                                         <div class="panel panel-default">
149                                                             <div class="panel-body">
150                                                                 <div class="form-group">
151 % if ($content_accession_name){
152                                                                     <label class="col-sm-3 control-label">Accession name: </label>
153                                                                     <div class="col-sm-9" >
154                                                                         <input class="form-control" id="transaction_new_seedlot_accession_uniquename" disabled value="<% $content_accession_name %>">
155                                                                     </div>
156 % }
157 % if ($content_cross_name){
158                                                                     <label class="col-sm-3 control-label">Cross Unique ID: </label>
159                                                                     <div class="col-sm-9" >
160                                                                         <input class="form-control" id="transaction_new_seedlot_cross_uniquename" disabled value="<% $content_cross_name %>">
161                                                                     </div>
162 % }
163                                                                 </div>
164                                                             </div>
165                                                         </div>
166                                                     </div>
167                                                 </div>
168                                             </div>
170                                             <div class="form-group">
171                                                 <label class="col-sm-3 control-label">Starting Amount (number of seeds): </label>
172                                                 <div class="col-sm-9" >
173                                                     <input class="form-control" id="transaction_new_seedlot_amount" placeholder="Amount OR Weight Required" disabled value="0">
174                                                 </div>
175                                             </div>
176                                             <div class="form-group">
177                                                 <label class="col-sm-3 control-label">Starting Weight (g): </label>
178                                                 <div class="col-sm-9" >
179                                                     <input class="form-control" id="transaction_new_seedlot_weight" placeholder="Amount OR Weight Required" disabled value="0">
180                                                 </div>
181                                             </div>
182                                             <!--
183                                             <div class="form-group">
184                                                 <label class="col-sm-3 control-label">Population: </label>
185                                                 <div class="col-sm-9" >
186                                                     <input class="form-control" id="transaction_new_seedlot_population_name" placeholder="Optional">
187                                                 </div>
188                                             </div>
189                                             -->
190                                             <div class="form-group">
191                                                 <label class="col-sm-3 control-label">Organization: </label>
192                                                 <div class="col-sm-9" >
193                                                     <input class="form-control" id="transaction_new_seedlot_organization" placeholder="Optional">
194                                                 </div>
195                                             </div>
196                                             <div class="form-group">
197                                                 <label class="col-sm-3 control-label">Timestamp: </label>
198                                                 <div class="col-sm-9" >
199                                                     <input class="form-control" id="transaction_new_seedlot_timestamp" value="<% $timestamp %>" placeholder="<% $timestamp %>">
200                                                 </div>
201                                             </div>
202                                             <div class="form-group">
203                                                 <label class="col-sm-3 control-label">Description: </label>
204                                                 <div class="col-sm-9" >
205                                                     <input class="form-control" id="transaction_new_seedlot_description" placeholder="Optional">
206                                                 </div>
207                                             </div>
209                                         </div>
210                                     </div>
211                                 </div>
212                             </div>
213                         </div>
214                     </div>
215                 </div>
217                 <div class="form-group">
218                     <label class="col-sm-3 control-label">Transaction Amount (number seeds): </label>
219                     <div class="col-sm-9" >
220                         <input class="form-control" id="seedlot_transaction_amount" placeholder="Amount OR Weight Required">
221                     </div>
222                 </div>
223                 <div class="form-group">
224                     <label class="col-sm-3 control-label">Transaction Weight (g): </label>
225                     <div class="col-sm-9" >
226                         <input class="form-control" id="seedlot_transaction_weight" placeholder="Amount OR Weight Required">
227                     </div>
228                 </div>
229                 <div class="form-group">
230                     <label class="col-sm-3 control-label">Timestamp: </label>
231                     <div class="col-sm-9" >
232                         <input class="form-control" id="seedlot_transaction_timestamp" value="<% $timestamp %>" placeholder="<% $timestamp %>">
233                     </div>
234                 </div>
235                 <div class="form-group">
236                     <label class="col-sm-3 control-label">Description: </label>
237                     <div class="col-sm-9" >
238                         <input class="form-control" id="seedlot_transaction_description" placeholder="Required">
239                     </div>
240                 </div>
241             </form>
242         </div>
243       </div>
244       <div class="modal-footer">
245         <button id="create_new_seedlot_transaction_button" type="button" class="btn btn-primary" >OK</button>
246       </div>
247     </div>
248   </div>
249 </div>
251 <div class="modal fade" id="edit_seedlot_transaction_dialog" name="edit_seedlot_transaction_dialog" tabindex="-1" role="dialog" aria-labelledby="seedlot_edit_transaction_dialog">
252   <div class="modal-dialog modal-lg" role="document">
253     <div class="modal-content">
254       <div class="modal-header">
255         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
256         <h4 class="modal-title" id="seedlot_edit_transaction_dialog">Edit Seedlot Transaction</h4>
257       </div>
258       <div class="modal-body">
259         <div class="container-fluid">
260             <div id="edit_seedlot_transaction_div">
261             </div>
262         </div>
263       </div>
264       <div class="modal-footer">
265         <button id="edit_seedlot_transaction_button" type="button" class="btn btn-primary" >Save</button>
266       </div>
267     </div>
268   </div>
269 </div>
271 <div class="modal fade" id="edit_seedlot_details_dialog" name="edit_seedlot_details_dialog" tabindex="-1" role="dialog" aria-labelledby="seedlot_edit_details_dialog">
272   <div class="modal-dialog modal-lg" role="document">
273     <div class="modal-content">
274       <div class="modal-header">
275         <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
276         <h4 class="modal-title" id="seedlot_edit_details_dialog">Edit Seedlot Details</h4>
277       </div>
278       <div class="modal-body">
279         <div class="container-fluid">
280             <form class="form-horizontal">
281                 <div class="form-group">
282                     <label class="col-sm-3 control-label">Seedlot Name: </label>
283                     <div class="col-sm-9" >
284                         <input class="form-control" type="text" id="edit_seedlot_details_uniquename" value="LOADING..."/>
285                     </div>
286                 </div>
287                 <div class="form-group">
288                     <label class="col-sm-3 control-label">Breeding Program: </label>
289                     <div class="col-sm-9" >
290                         <input class="form-control" type="text" id="edit_seedlot_details_breeding_program" value="LOADING..."/>
291                     </div>
292                 </div>
293                 <div class="form-group">
294                     <label class="col-sm-3 control-label">Organization: </label>
295                     <div class="col-sm-9" >
296                         <input class="form-control" type="text" id="edit_seedlot_details_organization" value="LOADING..."/>
297                     </div>
298                 </div>
299                 <div class="form-group">
300                     <label class="col-sm-3 control-label">Location Code: </label>
301                     <div class="col-sm-9" >
302                         <input class="form-control" type="text" id="edit_seedlot_details_location" value="LOADING..."/>
303                     </div>
304                 </div>
305                 <div class="form-group">
306                     <label class="col-sm-3 control-label">Box Name: </label>
307                     <div class="col-sm-9" >
308                         <input class="form-control" type="text" id="edit_seedlot_details_box_name" value="LOADING..."/>
309                     </div>
310                 </div>
312                 <div class="well well-sm">
313                     <div class="row">
314                         <div class="col-sm-3">
315                             <div class="form-group">
316                                 <label class="col-sm-12 control-label">Contents: </label>
317                             </div>
318                         </div>
319                         <div class="col-sm-9">
320                             <div class="panel panel-default">
321                                 <div class="panel-body">
322                                     <div class="form-group">
323                                         <label class="col-sm-3 control-label">Accession name: </label>
324                                         <div class="col-sm-9" >
325                                             <input class="form-control" id="edit_seedlot_details_accession" placeholder="One Content Required">
326                                         </div>
327                                     </div>
328                                 </div>
329                             </div>
330                             <center><h4>OR</h4></center>
331                             <div class="panel panel-default">
332                                 <div class="panel-body">
333                                     <div class="form-group">
334                                         <label class="col-sm-3 control-label">Cross Unique ID: </label>
335                                         <div class="col-sm-9" >
336                                             <input class="form-control" id="edit_seedlot_details_cross" placeholder="One Content Required">
337                                         </div>
338                                     </div>
339                                 </div>
340                             </div>
341                         </div>
342                     </div>
343                 </div>
345                 <!--
346                 <div class="form-group">
347                     <label class="col-sm-3 control-label">Population: </label>
348                     <div class="col-sm-9" >
349                         <input class="form-control" type="text" id="edit_seedlot_details_population" value="LOADING..."/>
350                     </div>
351                 </div>
352                 -->
353             </form>
354         </div>
355       </div>
356       <div class="modal-footer">
357         <button id="edit_seedlot_details_button" type="button" class="btn btn-primary" >Save</button>
358       </div>
359     </div>
360   </div>
361 </div>
364 <script>
365   jQuery(document).ready(function () {
367     jQuery('#add_seedlot_transaction_button').click( function() {
368         get_select_box('seedlots', 'seedlot_transaction_seedlot_div', { 'name' : 'seedlot_transaction_seedlot_id', 'id' : 'seedlot_transaction_seedlot_id', 'multiple':'0', 'seedlot_content_accession_name':'<% $content_accession_name %>', 'seedlot_content_cross_name':'<% $content_cross_name %>' });
369         get_select_box('seedlots', 'seedlot_transaction_to_seedlot_existing', { 'name' : 'seedlot_transaction_to_seedlot_existing_id', 'id' : 'seedlot_transaction_to_seedlot_existing_id', 'multiple':'0', 'seedlot_content_accession_name':'<% $content_accession_name %>', 'seedlot_content_cross_name':'<% $content_cross_name %>' });
370         get_select_box('breeding_programs', 'transaction_new_seedlot_breeding_program_div', { 'name' : 'transaction_new_seedlot_breeding_program_id', 'id' : 'transaction_new_seedlot_breeding_program_id' });
371         jQuery('#add_seedlot_transaction_dialog').modal('show')
372     });
374     jQuery("#transaction_new_seedlot_location").autocomplete({
375        source: '/ajax/stock/geolocation_autocomplete',
376     });
377     jQuery("#edit_seedlot_details_accession").autocomplete({
378        source: '/ajax/stock/accession_autocomplete',
379     });
380     jQuery("#edit_seedlot_details_cross").autocomplete({
381        source: '/ajax/stock/cross_autocomplete',
382     });
384     var seedlot_transactions_table = jQuery('#available_seedlot_transactions_table').DataTable( {
385         'ajax': '/ajax/breeders/seedlot/<% $seedlot_id %>/transactions',
386         columns: [
387             { title: "Transaction Id", "data": "transaction_id" },
388             { title: "Transaction Date", "data": "timestamp" },
389             { title: "From", "data": "from" },
390             { title: "To", "data": "to" },
391             { title: "Transaction Num Seeds", "data": "value" },
392             { title: "Transaction Weight (g)", "data": "weight" },
393             { title: "Operator", "data": "operator" },
394             { title: "Description", "data": "description" },
395             { title: "Options", "data": "null", "render" : function ( data, type, row ) { return "<a onclick='editSeedlotTransaction("+row.transaction_id+")' >[Edit]</a>"; } },
396         ],
397     });
399     jQuery.ajax({
400         url: '/ajax/breeders/seedlot/<% $seedlot_id %>',
401         success: function(response) {
402             //console.log(response);
403             if (response.success == 1) {
404                 jQuery('#seedlot_location_code_div').html(response.location_code);
405                 jQuery('#seedlot_breeding_program_div').html(response.breeding_program);
406                 jQuery('#edit_seedlot_details_uniquename').val(response.uniquename);
407                 jQuery('#edit_seedlot_details_breeding_program').val(response.breeding_program);
408                 jQuery('#edit_seedlot_details_organization').val(response.organization_name);
409                 jQuery('#edit_seedlot_details_population').val(response.population_name);
410                 jQuery('#edit_seedlot_details_location').val(response.location_code);
411                 jQuery('#edit_seedlot_details_box_name').val(response.box_name);
412                 var accession_name = '';
413                 if (response.accession){
414                     accession_name = response.accession[1];
415                 }
416                 var cross_name = '';
417                 if (response.cross){
418                     cross_name = response.cross[1];
419                 }
420                 jQuery('#edit_seedlot_details_accession').val(accession_name);
421                 jQuery('#edit_seedlot_details_cross').val(cross_name);
422             }
423             if (response.error) {
424                 alert(response.error);
425             }
426         },
427         error: function(response) {
428             alert('An error occurred retrieving seedlot details');
429         }
430     });
432     jQuery('#create_new_seedlot_transaction_button').click( function() {
433         var factor = jQuery('#seedlot_transaction_factor').val();
434         var to_new = jQuery('#seedlot_transaction_to_seedlot_new').val();
435         var from_existing_seedlot_id_array;
436         var from_existing_seedlot_id;
437         var to_existing_seedlot_id_array;
438         var to_existing_seedlot_id;
439         var to_new_seedlot_name;
440         var to_new_seedlot_breeding_program_id;
441         var to_new_seedlot_location_name;
442         var to_new_seedlot_box_name;
443         var to_new_seedlot_accession_name;
444         var to_new_seedlot_cross_name;
445         var to_new_seedlot_amount;
446         var to_new_seedlot_weight;
447         var to_new_seedlot_population_name;
448         var to_new_seedlot_organization;
449         var to_new_seedlot_timestamp;
450         var to_new_seedlot_description;
452         if (factor == 1){
453             from_existing_seedlot_id_array = jQuery('#seedlot_transaction_seedlot_id').val();
454             if (from_existing_seedlot_id_array.length > 1) {
455                 alert("Please select only one seedlot.");
456                 return;
457             } else {
458                 from_existing_seedlot_id = from_existing_seedlot_id_array[0];
459             }
460         }
461         if (factor == -1){
462             if (to_new == 'no'){
463                 to_existing_seedlot_id_array = jQuery('#seedlot_transaction_to_seedlot_existing_id').val();
464                 if (to_existing_seedlot_id_array.length > 1) {
465                     alert("Please select only one seedlot.");
466                     return;
467                 } else {
468                     to_existing_seedlot_id = to_existing_seedlot_id_array[0];
469                 }
470             }
471             if (to_new == 'yes'){
472                 to_new_seedlot_name = jQuery('#transaction_new_seedlot_name').val();
473                 to_new_seedlot_breeding_program_id = jQuery('#transaction_new_seedlot_breeding_program_id').val();
474                 to_new_seedlot_location_name = jQuery('#transaction_new_seedlot_location').val();
475                 to_new_seedlot_box_name = jQuery('#transaction_new_seedlot_box_name').val();
476                 to_new_seedlot_accession_name = "<% $content_accession_name %>";
477                 to_new_seedlot_cross_name = "<% $content_cross_name %>";
478                 to_new_seedlot_amount = jQuery('#transaction_new_seedlot_amount').val();
479                 to_new_seedlot_weight = jQuery('#transaction_new_seedlot_weight').val();
480                 to_new_seedlot_population_name = jQuery('#transaction_new_seedlot_population_name').val();
481                 to_new_seedlot_organization = jQuery('#transaction_new_seedlot_organization').val();
482                 to_new_seedlot_timestamp = jQuery('#transaction_new_seedlot_timestamp').val();
483                 to_new_seedlot_description = jQuery('#transaction_new_seedlot_description').val();
485                 if (to_new_seedlot_accession_name == '' && to_new_seedlot_cross_name == '') {
486                     alert("Please provide an accession name or a cross unique id as the content of the seedlot."); return;
487                 }
488                 if (to_new_seedlot_accession_name != '' && to_new_seedlot_cross_name != '') {
489                     alert("Please provide an accession name OR a cross unique id as the content of the seedlot. Not both."); return;
490                 }
491                 if (to_new_seedlot_location_name == ''){
492                     alert("Please provide a location for new seedlot."); return;
493                 }
494                 if (to_new_seedlot_box_name == ''){
495                     alert("Please provide a box name for new seedlot."); return;
496                 }
497                 if (to_new_seedlot_amount == '' && to_new_seedlot_weight == ''){
498                     alert("Please provide a starting amount OR weight in grams for new seedlot."); return;
499                 }
500                 if (to_new_seedlot_timestamp == ''){
501                     alert("Please provide a timestamp for new seedlot created."); return;
502                 }
503             }
504         }
505         var amount = jQuery('#seedlot_transaction_amount').val();
506         var weight = jQuery('#seedlot_transaction_weight').val();
507         var timestamp = jQuery('#seedlot_transaction_timestamp').val();
508         var description = jQuery('#seedlot_transaction_description').val();
510         if (factor == '') { alert("Please provide a factor"); }
511         else if (amount == '' && weight == '') { alert("Please provide an amount OR a weight in grams"); }
512         else if (timestamp == '') { alert("Please provide a timestamp"); }
513         else if (description == '') { alert("Please provide a description"); }
514         else if (from_existing_seedlot_id == '' && to_existing_seedlot_id == '' && to_new_seedlot_name == '') { alert("Please provide seed lot"); }
515         else {
517             jQuery.ajax( {
518                 url: '/ajax/breeders/seedlot/<% $seedlot_id %>/transaction/add',
519                 data : {
520                     'factor' : factor,
521                     'seedlot_id': <% $seedlot_id %>,
522                     'amount': amount,
523                     'weight': weight,
524                     'timestamp': timestamp,
525                     'description': description,
526                     'from_existing_seedlot_id': from_existing_seedlot_id,
527                     'to_existing_seedlot_id': to_existing_seedlot_id,
528                     'to_new_seedlot_name' : to_new_seedlot_name,
529                     'to_new_seedlot_breeding_program_id': to_new_seedlot_breeding_program_id,
530                     'to_new_seedlot_location_name': to_new_seedlot_location_name,
531                     'to_new_seedlot_box_name': to_new_seedlot_box_name,
532                     'to_new_seedlot_accession_name': to_new_seedlot_accession_name,
533                     'to_new_seedlot_cross_name': to_new_seedlot_cross_name,
534                     'to_new_seedlot_amount': to_new_seedlot_amount,
535                     'to_new_seedlot_weight': to_new_seedlot_weight,
536                     'to_new_seedlot_population_name': to_new_seedlot_population_name,
537                     'to_new_seedlot_organization': to_new_seedlot_organization,
538                     'to_new_seedlot_timestamp': to_new_seedlot_timestamp,
539                     'to_new_seedlot_description': to_new_seedlot_description
540                 },
541                 beforeSend: function(){
542                     jQuery('#working_modal').modal('show');
543                 },
544                 success: function(response) {
545                     jQuery('#working_modal').modal('hide');
546                     if (response.success == 1) {
547                         alert("The seedlot transaction has been created.");
548                         jQuery('#add_seedlot_transaction_dialog').modal('hide');
549                         seedlot_transactions_table.ajax.reload();
550                     }
551                     if (response.error) {
552                         alert(response.error);
553                     }
554                 },
555                 error: function(response) {
556                     jQuery('#working_modal').modal('hide');
557                     alert('An error occurred creating seedlot transaction');
558                 }
559             });
560         }
561     });
563     jQuery('#seedlot_transaction_factor').change(function(){
564         var transaction_factor = jQuery('#seedlot_transaction_factor').val();
565         if (transaction_factor == 1){
566             jQuery('#seedlot_transaction_to_seedlot').hide();
567             jQuery('#seedlot_transaction_from_seedlot').show();
568         }
569         if (transaction_factor == -1){
570             jQuery('#seedlot_transaction_from_seedlot').hide();
571             jQuery('#seedlot_transaction_to_seedlot').show();
572         }
573     });
575     jQuery('#seedlot_transaction_to_seedlot_new').change(function(){
576         var option = jQuery('#seedlot_transaction_to_seedlot_new').val();
577         if (option == 'yes'){
578             jQuery('#seedlot_transaction_to_seedlot_new_no').hide();
579             jQuery('#seedlot_transaction_to_seedlot_new_yes').show();
580         }
581         if (option == 'no'){
582             jQuery('#seedlot_transaction_to_seedlot_new_no').show();
583             jQuery('#seedlot_transaction_to_seedlot_new_yes').hide();
584         }
585     });
587     jQuery('#edit_seedlot_transaction_button').click(function(){
588         var transaction_id = jQuery('#edit_seedlot_transaction_id').val();
589         var operator = jQuery('#edit_seedlot_transaction_operator').val();
590         var amount = jQuery('#edit_seedlot_transaction_amount').val();
591         var weight = jQuery('#edit_seedlot_transaction_weight').val();
592         var description = jQuery('#edit_seedlot_transaction_description').val();
593         var timestamp = jQuery('#edit_seedlot_transaction_timestamp').val();
594         if (transaction_id == ''){
595             alert('Transaction ID must not be blank');
596         } else if (operator == ''){
597             alert('Operator must not be blank');
598         } else if (amount == '' && weight == ''){
599             alert('Amount OR weight must not be blank');
600         } else if (description == ''){
601             alert('Description must not be blank');
602         } else if (timestamp == ''){
603             alert('Timestamp must not be blank');
604         } else {
605             jQuery.ajax({
606                 url: '/ajax/breeders/seedlot/<% $seedlot_id %>/transaction/'+transaction_id+'/edit',
607                 data: {
608                     'operator':operator,
609                     'amount':amount,
610                     'weight_gram':weight,
611                     'description':description,
612                     'timestamp':timestamp
613                 },
614                 beforeSend: function(){
615                     jQuery('#working_modal').modal('show');
616                 },
617                 success: function(response) {
618                     jQuery('#working_modal').modal('hide');
619                     if (response.success == 1) {
620                         alert('Transaction updated!');
621                         seedlot_transactions_table.ajax.reload();
622                     }
623                     if (response.error) {
624                         alert(response.error);
625                     }
626                 },
627                 error: function(response) {
628                     jQuery('#working_modal').modal('hide');
629                     alert('An error occurred editing seedlot transaction details');
630                 }
631             });
632         }
633     });
635     jQuery('#edit_seedlot_details_link').click(function(){
636         jQuery('#edit_seedlot_details_dialog').modal('show');
637     });
639     jQuery('#edit_seedlot_details_button').click(function(){
640         var uniquename = jQuery('#edit_seedlot_details_uniquename').val();
641         var breeding_program = jQuery('#edit_seedlot_details_breeding_program').val();
642         var organization = jQuery('#edit_seedlot_details_organization').val();
643         var population = jQuery('#edit_seedlot_details_population').val();
644         var location = jQuery('#edit_seedlot_details_location').val();
645         var box_name = jQuery('#edit_seedlot_details_box_name').val();
646         var accession = jQuery('#edit_seedlot_details_accession').val();
647         var cross = jQuery('#edit_seedlot_details_cross').val();
648         if (uniquename == ''){
649             alert('Seedlot name must not be blank');
650         } else if (breeding_program == ''){
651             alert('Breeding program must not be blank');
652         } else if (location == ''){
653             alert('Location must not be blank');
654         } else if (accession == '' && cross == '') {
655             alert("Please provide an accession name or a cross unique id as the content of the seedlot."); return;
656         }
657         else if (accession != '' && cross != '') {
658             alert("Please provide an accession name OR a cross unique id as the content of the seedlot. Not both."); return;
659         }
660         else {
661             jQuery.ajax({
662                 url: '/ajax/breeders/seedlot/<% $seedlot_id %>/edit',
663                 data: {
664                     'uniquename':uniquename,
665                     'breeding_program':breeding_program,
666                     'organization':organization,
667                     'population':population,
668                     'location':location,
669                     'box_name':box_name,
670                     'accession':accession,
671                     'cross':cross
672                 },
673                 beforeSend: function(){
674                     jQuery('#working_modal').modal('show');
675                 },
676                 success: function(response) {
677                     jQuery('#working_modal').modal('hide');
678                     if (response.success == 1) {
679                         alert('Seedlot updated!');
680                         document.location.reload();
681                     }
682                     if (response.error) {
683                         alert(response.error);
684                     }
685                 },
686                 error: function(response) {
687                     jQuery('#working_modal').modal('hide');
688                     alert('An error occurred editing seedlot details');
689                 }
690             });
691         }
692     });
696 function editSeedlotTransaction(transaction_id){
697     jQuery.ajax({
698         url: '/ajax/breeders/seedlot/<% $seedlot_id %>/transaction/'+transaction_id,
699         beforeSend: function(){
700             jQuery('#working_modal').modal('show');
701         },
702         success: function(response) {
703             jQuery('#working_modal').modal('hide');
704             if (response.success == 1) {
705                 html = '<form class="form-horizontal"><div class="form-group"><label class="col-sm-3 control-label">Transaction ID: </label><div class="col-sm-9" ><input type="text" class="form-control" id="edit_seedlot_transaction_id" value="'+response.transaction_id+'" disabled /></div></div><div class="form-group"><label class="col-sm-3 control-label">Operator: </label><div class="col-sm-9" ><input type="text" id="edit_seedlot_transaction_operator" class="form-control" value="'+response.operator+'" /></div></div><div class="form-group"><label class="col-sm-3 control-label">Amount (Number seeds): </label><div class="col-sm-9" ><input type="number" class="form-control" id="edit_seedlot_transaction_amount" value="'+response.amount+'" /></div></div><div class="form-group"><label class="col-sm-3 control-label">Weight (g): </label><div class="col-sm-9" ><input type="text" class="form-control" id="edit_seedlot_transaction_weight" value="'+response.weight_gram+'" /></div></div><div class="form-group"><label class="col-sm-3 control-label">Timestamp: </label><div class="col-sm-9" ><input type="text" class="form-control" id="edit_seedlot_transaction_timestamp" value="'+response.timestamp+'" disabled /></div></div><div class="form-group"><label class="col-sm-3 control-label">Description: </label><div class="col-sm-9" ><input type="text" class="form-control" id="edit_seedlot_transaction_description" value="'+response.description+'" /></div></div></form>';
706                 jQuery('#edit_seedlot_transaction_div').html(html);
707                 jQuery('#edit_seedlot_transaction_dialog').modal('show');
708             }
709             if (response.error) {
710                 alert(response.error);
711             }
712         },
713         error: function(response){
714             jQuery('#working_modal').modal('hide');
715             alert('An error occurred getting seedlot transaction');
716         }
717     });
720 </script>