2 package SGN
::Controller
::AJAX
::Order
;
5 use CXGN
::Stock
::Order
;
6 use CXGN
::Stock
::OrderBatch
;
10 use CXGN
::People
::Person
;
12 use CXGN
::Trial
::Download
;
13 use CXGN
::Stock
::TrackingActivity
::TrackingIdentifier
;
14 use CXGN
::Stock
::OrderTrackingIdentifier
;
15 use CXGN
::TrackingActivity
::AddActivityProject
;
16 use CXGN
::Location
::LocationLookup
;
18 use File
::Basename qw
| basename dirname
|;
21 use File
::Spec
::Functions
;
23 use File
::Path
qw(make_path);
24 use File
::Spec
::Functions qw
/ catfile catdir/;
29 use URI
::Encode
qw(uri_encode uri_decode);
30 use Tie
::UrlEncoder
; our(%urlencode);
33 BEGIN { extends
'Catalyst::Controller::REST' }
36 default => 'application/json',
38 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
42 sub submit_order
: Path
('/ajax/order/submit') : ActionClass
('REST'){ }
44 sub submit_order_POST
: Args
(0) {
47 my $sp_person_id = $c->user() ?
$c->user->get_object()->get_sp_person_id() : undef;
48 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado', $sp_person_id);
49 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $sp_person_id);
50 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema", undef, $sp_person_id);
51 my $dbh = $c->dbc->dbh();
52 my $list_id = $c->req->param('list_id');
53 my $time = DateTime
->now();
54 my $timestamp = $time->ymd()."_".$time->hms();
55 my $request_date = $time->ymd();
56 my $order_properties = $c->config->{order_properties
};
57 my @properties = split ',',$order_properties;
60 print STDERR
"User not logged in... not adding a catalog item.\n";
61 $c->stash->{rest
} = {error_string
=> "You must be logged in to add a catalog item." };
64 my $user_id = $c->user()->get_object()->get_sp_person_id();
65 my $user_name = $c->user()->get_object()->get_username();
66 my $user_role = $c->user->get_object->get_user_type();
68 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado', $user_id);
69 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
71 my $list = CXGN
::List
->new( { dbh
=>$dbh, list_id
=>$list_id });
72 my $items = $list->elements();
74 my $catalog_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'stock_catalog_json', 'stock_property')->cvterm_id();
75 my %group_by_contact_id;
77 my @all_items = @
$items;
78 foreach my $ordered_item (@all_items) {
79 my $order_details_ref = decode_json
($ordered_item);
80 my %order_details = %{$order_details_ref};
81 my $item_name = $order_details{'Item Name'};
82 my %each_item_details;
83 foreach my $field (@properties) {
84 $each_item_details{$field} = $order_details{$field};
87 my $item_rs = $schema->resultset("Stock::Stock")->find( { uniquename
=> $item_name });
88 my $item_id = $item_rs->stock_id();
89 my $item_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id
=> $item_id, type_id
=> $catalog_cvterm_id});
90 my $item_info_string = $item_info_rs->value();
91 my $item_info_hash = decode_json
$item_info_string;
92 my $contact_person_id = $item_info_hash->{'contact_person_id'};
93 my $item_type = $item_info_hash->{'item_type'};
94 my $item_source = $item_info_hash->{'material_source'};
95 my $breeding_program_id = $item_info_hash->{'breeding_program'};
96 $group_by_contact_id{$contact_person_id}{'item_list'}{$item_name}{'item_type'} = $item_type;
97 $group_by_contact_id{$contact_person_id}{'item_list'}{$item_name}{'material_source'} = $item_source;
98 $group_by_contact_id{$contact_person_id}{'item_list'}{$item_name} = \
%each_item_details;
99 $group_by_contact_id{$contact_person_id}{'breeding_program'} = $breeding_program_id;
102 my $tracking_activity = $c->config->{tracking_order_activity
};
105 my @contact_email_list;
106 foreach my $contact_id (keys %group_by_contact_id) {
108 my $history_info = {};
109 my $item_ref = $group_by_contact_id{$contact_id}{'item_list'};
110 my %item_hashes = %{$item_ref};
111 my @names = keys %item_hashes;
113 my @item_list = map { { $_ => $item_hashes{$_} } } sort keys %item_hashes;
115 my $new_order = CXGN
::Stock
::Order
->new( { bcs_schema
=> $schema, people_schema
=> $people_schema, dbh
=> $dbh});
116 $new_order->order_from_id($user_id);
117 $new_order->order_to_id($contact_id);
118 $new_order->order_status("submitted");
119 $new_order->create_date($timestamp);
120 my $order_id = $new_order->store();
122 $c->stash->{rest
} = {error_string
=> "Error saving your order",};
126 $history_info ->{'submitted'} = $timestamp;
127 push @history, $history_info;
129 my $order_prop = CXGN
::Stock
::OrderBatch
->new({ bcs_schema
=> $schema, people_schema
=> $people_schema});
130 $order_prop->clone_list(\
@item_list);
131 $order_prop->parent_id($order_id);
132 $order_prop->history(\
@history);
133 my $order_prop_id = $order_prop->store_sp_orderprop();
134 # print STDERR "ORDER PROP ID =".($order_prop_id)."\n";
136 if (!$order_prop_id){
137 $c->stash->{rest
} = {error_string
=> "Error saving your order",};
141 my @tracking_identifiers = ();
142 my @identifier_stock_ids = ();
143 my @tracking_ids = ();
144 my $activity_project_id;
145 if ($tracking_activity) {
147 foreach my $name (sort @names) {
149 push @tracking_identifiers, ["order".$order_id.":".$name."_"."T".(sprintf "%04d", $n), $name];
152 my $activity_project_name = $user_name."_"."order_progress";
153 my $activity_project_rs = $schema->resultset('Project::Project')->find({name
=>$activity_project_name});
154 if ($activity_project_rs) {
155 $activity_project_id = $activity_project_rs->project_id();
157 my $geolocation_lookup = CXGN
::Location
::LocationLookup
->new(schema
=>$schema);
158 $geolocation_lookup->set_location_name('[Computation]');
159 if(!$geolocation_lookup->get_geolocation()){
160 $c->stash->{rest
}={error
=> "Location not found"};
164 my $breeding_program_id = $group_by_contact_id{$contact_id}{'breeding_program'};
166 my $add_activity_project = CXGN
::TrackingActivity
::AddActivityProject
->new({
167 bcs_schema
=> $schema,
169 breeding_program_id
=> $breeding_program_id,
171 project_description
=> 'Tracking order progress',
172 activity_project_name
=> $activity_project_name,
173 activity_type
=> 'tissue_culture',
174 nd_geolocation_id
=> $geolocation_lookup->get_geolocation()->nd_geolocation_id(),
175 owner_id
=> $contact_id,
176 project_vendor
=> $contact_id
179 $activity_project_id = $add_activity_project->save_activity_project();
182 foreach my $identifier_info (@tracking_identifiers) {
183 my $tracking_identifier = $identifier_info->[0];
184 my $material = $identifier_info->[1];
185 my $tracking_obj = CXGN
::Stock
::TrackingActivity
::TrackingIdentifier
->new({
187 phenome_schema
=> $phenome_schema,
188 tracking_identifier
=> $tracking_identifier,
189 material
=> $material,
190 project_id
=> $activity_project_id,
191 user_id
=> $contact_id
193 my $tracking_stock_id = $tracking_obj->store();
194 push @identifier_stock_ids, $tracking_stock_id;
197 my $order_tracking_identifier_prop = CXGN
::Stock
::OrderTrackingIdentifier
->new({ bcs_schema
=> $schema, people_schema
=> $people_schema});
198 $order_tracking_identifier_prop->tracking_identifiers(\
@identifier_stock_ids);
199 $order_tracking_identifier_prop->parent_id($order_id);
200 my $prop_id = $order_tracking_identifier_prop->store_sp_orderprop();
203 $c->stash->{rest
} = {error_string
=> "Error saving your tracking identifiers",};
208 my $contact_person = CXGN
::People
::Person
-> new
($dbh, $contact_id);
209 my $contact_email = $contact_person->get_contact_email();
210 push @contact_email_list, $contact_email;
214 my $host = $c->config->{main_production_site_url
};
215 my $project_name = $c->config->{project_name
};
216 my $subject="Ordering Notification from $project_name";
217 my $body=<<END_HEREDOC;
219 You have an order submitted to $project_name ($host/order/stocks/view).
220 Please do *NOT* reply to this message.
227 foreach my $each_email (@contact_email_list) {
228 CXGN::Contact::send_email($subject,$body,$each_email);
231 $c->stash->{rest}->{success} .= 'Your order has been submitted successfully and the vendor has been notified.';
236 sub get_user_current_orders :Path('/ajax/order/current') Args(0) {
239 my $dbh = $c->dbc->dbh;
240 my $order_properties = $c->config->{order_properties};
241 my @properties = split ',',$order_properties;
245 $c->stash->{rest} = {error=>'You must be logged in to view your current orders!'};
250 $user_id = $c->user()->get_object()->get_sp_person_id();
253 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $user_id);
254 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
256 my $orders = CXGN::Stock::Order->new({ bcs_schema => $schema, dbh => $dbh, people_schema => $people_schema, order_from_id => $user_id});
257 my $all_orders_ref = $orders->get_orders_from_person_id();
260 my @all_orders = @$all_orders_ref;
261 foreach my $order (@all_orders) {
262 if (($order->{'order_status'}) ne 'completed') {
263 my $clone_list = $order->{'clone_list'};
265 my @all_item_details = ();
266 my $all_details_string;
267 my $empty_string = '';
268 foreach my $each_item (@$clone_list) {
269 my @request_details = ();
270 $item_name = (keys %$each_item)[0];
271 push @request_details, "<b>"."Item Name"."<b>". ":"."".$item_name;
272 foreach my $field (@properties) {
273 my $each_detail = $each_item->{$item_name}->{$field};
274 my $detail_string = $field. ":"."".$each_detail;
275 push @request_details, $detail_string;
277 push @request_details, $empty_string;
278 my $details_string = join("<br>", @request_details);
279 push @all_item_details, $details_string;
281 $all_details_string = join("<br>", @all_item_details);
282 push @current_orders, [qq{<a href="/order/details/view/$order->{'order_id'}">$order->{'order_id'}</a>}, $order->{'create_date'}, $all_details_string, $order->{'order_status'}, $order->{'order_to_name'}, $order->{'comments'}]
286 $c->stash->{rest} = {data => \@current_orders};
289 sub get_user_completed_orders :Path('/ajax/order/completed') Args(0) {
292 my $dbh = $c->dbc->dbh;
293 my $order_properties = $c->config->{order_properties};
294 my @properties = split ',',$order_properties;
298 $c->stash->{rest} = {error=>'You must be logged in to view your completed orders!'};
303 $user_id = $c->user()->get_object()->get_sp_person_id();
306 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $user_id);
307 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
308 my $orders = CXGN::Stock::Order->new({ bcs_schema => $schema, dbh => $dbh, people_schema => $people_schema, order_from_id => $user_id});
309 my $all_orders_ref = $orders->get_orders_from_person_id();
310 my @completed_orders;
311 my @all_orders = @$all_orders_ref;
313 foreach my $order (@all_orders) {
314 if (($order->{'order_status'}) eq 'completed') {
315 my $clone_list = $order->{'clone_list'};
317 my @all_item_details = ();
318 my $empty_string = '';
319 my $all_details_string;
320 foreach my $each_item (@$clone_list) {
321 my @request_details = ();
322 $item_name = (keys %$each_item)[0];
323 push @request_details, "<b>"."Item Name"."<b>". ":"."".$item_name;
324 foreach my $field (@properties) {
325 my $each_detail = $each_item->{$item_name}->{$field};
326 my $detail_string = $field. ":"."".$each_detail;
327 push @request_details, $detail_string;
329 push @request_details, $empty_string;
330 my $details_string = join("<br>", @request_details);
331 push @all_item_details, $details_string;
333 $all_details_string = join("<br>", @all_item_details);
335 push @completed_orders, [qq{<a href="/order/details/view/$order->{'order_id'}">$order->{'order_id'}</a>}, $order->{'create_date'}, $all_details_string, $order->{'order_status'}, $order->{'completion_date'}, $order->{'order_to_name'}, $order->{'comments'}]
339 $c->stash->{rest} = {data => \@completed_orders};
344 sub get_vendor_current_orders :Path('/ajax/order/vendor_current_orders') Args(0) {
347 my $dbh = $c->dbc->dbh;
348 my $order_properties = $c->config->{order_properties};
349 my @properties = split ',',$order_properties;
353 $c->stash->{rest} = {error=>'You must be logged in to view your orders!'};
358 $user_id = $c->user()->get_object()->get_sp_person_id();
361 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $user_id);
362 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
363 my $orders = CXGN::Stock::Order->new({ bcs_schema => $schema, dbh => $dbh, people_schema => $people_schema, order_to_id => $user_id});
364 my $vendor_orders_ref = $orders->get_orders_to_person_id();
366 my @vendor_current_orders;
367 my @all_vendor_orders = @$vendor_orders_ref;
368 foreach my $vendor_order (@all_vendor_orders) {
369 if (($vendor_order->{'order_status'}) ne 'completed') {
370 my $clone_list = $vendor_order->{'clone_list'};
372 my @all_item_details = ();
373 my $all_details_string;
374 my $empty_string = '';
375 foreach my $each_item (@$clone_list) {
376 my @request_details = ();
377 $item_name = (keys %$each_item)[0];
378 push @request_details, "<b>"."Item Name"."<b>". ":"."".$item_name;
379 foreach my $field (@properties) {
380 my $each_detail = $each_item->{$item_name}->{$field};
381 my $detail_string = $field. ":"."".$each_detail;
382 push @request_details, $detail_string;
384 push @request_details, $empty_string;
385 my $details_string = join("<br>", @request_details);
387 push @all_item_details, $details_string;
389 $all_details_string = join("<br>", @all_item_details);
391 $vendor_order->{'order_details'} = $all_details_string;
392 push @vendor_current_orders, $vendor_order
396 $c->stash->{rest} = {data => \@vendor_current_orders};
401 sub get_vendor_completed_orders :Path('/ajax/order/vendor_completed_orders') Args(0) {
404 my $dbh = $c->dbc->dbh;
405 my $order_properties = $c->config->{order_properties};
406 my @properties = split ',',$order_properties;
410 $c->stash->{rest} = {error=>'You must be logged in to view your orders!'};
415 $user_id = $c->user()->get_object()->get_sp_person_id();
418 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $user_id);
419 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
420 my $orders = CXGN::Stock::Order->new({ bcs_schema => $schema, dbh => $dbh, people_schema => $people_schema, order_to_id => $user_id});
421 my $vendor_orders_ref = $orders->get_orders_to_person_id();
423 my @vendor_completed_orders;
424 my @all_vendor_orders = @$vendor_orders_ref;
425 foreach my $vendor_order (@all_vendor_orders) {
426 if (($vendor_order->{'order_status'}) eq 'completed') {
427 my $clone_list = $vendor_order->{'clone_list'};
429 my @all_item_details = ();
430 my $empty_string = '';
431 my $all_details_string;
432 foreach my $each_item (@$clone_list) {
433 my @request_details = ();
434 $item_name = (keys %$each_item)[0];
435 push @request_details, "<b>"."Item Name"."<b>". ":"."".$item_name;
436 foreach my $field (@properties) {
437 my $each_detail = $each_item->{$item_name}->{$field};
438 my $detail_string = $field. ":"."".$each_detail;
439 push @request_details, $detail_string;
441 push @request_details, $empty_string;
442 my $details_string = join("<br>", @request_details);
443 push @all_item_details, $details_string;
445 $all_details_string = join("<br>", @all_item_details);
446 $vendor_order->{'order_details'} = $all_details_string;
448 push @vendor_completed_orders, $vendor_order
452 $c->stash->{rest} = {data => \@vendor_completed_orders};
456 sub update_order : Path('/ajax/order/update') : ActionClass('REST'){ }
458 sub update_order_POST : Args(0) {
461 my $dbh = $c->dbc->dbh;
462 my $order_id = $c->req->param('order_id');
463 my $new_status = $c->req->param('new_status');
464 my $contact_person_comments = $c->req->param('contact_person_comments');
465 my $time = DateTime->now();
466 my $timestamp = $time->ymd()."_".$time->hms();
470 $c->stash->{rest} = {error=>'You must be logged in to update the orders!'};
475 $user_id = $c->user()->get_object()->get_sp_person_id();
478 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
479 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado', $user_id);
481 if ($new_status eq 're-opened') {
482 my $re_open_by_person= CXGN::People::Person->new($dbh, $user_id);
483 my $re_open_name = $re_open_by_person->get_first_name()." ".$re_open_by_person->get_last_name();
484 $new_status = 're-opened by'." ".$re_open_name;
486 my $order_obj = CXGN::Stock::Order->new({ bcs_schema => $schema, dbh => $dbh, people_schema => $people_schema, sp_order_id => $order_id});
487 my $order_result = $order_obj->get_order_details();
488 my $vendor_id = $order_result->[7];
489 if ($user_id != $vendor_id) {
490 my $contact_person = CXGN::People::Person -> new($dbh, $vendor_id);
491 my $contact_email = $contact_person->get_contact_email();
493 my $host = $c->config->{main_production_site_url};
494 my $project_name = $c->config->{project_name};
495 my $subject="Ordering Notification from $project_name";
496 my $body=<<END_HEREDOC
;
498 You have a re
-opened order submitted to
$project_name ($host/order/stocks
/view
).
499 Please
do *NOT
* reply to this message
.
506 CXGN
::Contact
::send_email
($subject,$body,$contact_email);
511 if ($new_status eq 'completed') {
512 $order_obj = CXGN
::Stock
::Order
->new({ bcs_schema
=> $schema, dbh
=> $dbh, people_schema
=> $people_schema, sp_order_id
=> $order_id, order_to_id
=> $user_id, order_status
=> $new_status, completion_date
=> $timestamp, comments
=> $contact_person_comments});
514 $order_obj = CXGN
::Stock
::Order
->new({ bcs_schema
=> $schema, dbh
=> $dbh, people_schema
=> $people_schema, sp_order_id
=> $order_id, order_to_id
=> $user_id, order_status
=> $new_status, comments
=> $contact_person_comments});
517 my $updated_order = $order_obj->store();
518 # print STDERR "UPDATED ORDER ID =".Dumper($updated_order)."\n";
519 if (!$updated_order){
520 $c->stash->{rest
} = {error_string
=> "Error updating the order",};
524 my $orderprop_rs = $people_schema->resultset('SpOrderprop')->find( { sp_order_id
=> $order_id } );
525 my $orderprop_id = $orderprop_rs->sp_orderprop_id();
526 my $details_json = $orderprop_rs->value();
527 print STDERR
"ORDER PROP ID =".Dumper
($orderprop_id)."\n";
528 my $detail_hash = JSON
::Any
->jsonToObj($details_json);
530 my $order_history_ref = $detail_hash->{'history'};
531 my @order_history = @
$order_history_ref;
532 my $new_status_record = {};
533 $new_status_record->{$new_status}{'Date'} = $timestamp;
534 $new_status_record->{$new_status}{'Comments'} = $contact_person_comments;
535 push @order_history, $new_status_record;
536 $detail_hash->{'history'} = \
@order_history;
538 my $order_prop = CXGN
::Stock
::OrderBatch
->new({ bcs_schema
=> $schema, people_schema
=> $people_schema, sp_order_id
=> $order_id, prop_id
=> $orderprop_id});
539 $order_prop->history(\
@order_history);
540 my $updated_orderprop = $order_prop->store_sp_orderprop();
542 if (!$updated_orderprop){
543 $c->stash->{rest
} = {error_string
=> "Error updating the order",};
547 $c->stash->{rest
} = {success
=> "1",};
552 sub single_step_submission
: Path
('/ajax/order/single_step_submission') : ActionClass
('REST'){ }
554 sub single_step_submission_POST
: Args
(0) {
557 my $dbh = $c->dbc->dbh();
558 my $time = DateTime
->now();
559 my $timestamp = $time->ymd()."_".$time->hms();
560 my $request_date = $time->ymd();
561 my $item_name = $c->req->param('item_name');
562 my $order_details = decode_json
($c->req->param('order_details'));
565 $details{$item_name} = $order_details;
567 print STDERR
"User not logged in... not adding a catalog item.\n";
568 $c->stash->{rest
} = {error_string
=> "You must be logged in to add a catalog item." };
571 my $user_id = $c->user()->get_object()->get_sp_person_id();
572 my $user_name = $c->user()->get_object()->get_username();
573 my $user_role = $c->user->get_object->get_user_type();
575 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado', $user_id);
576 my $people_schema = $c->dbic_schema('CXGN::People::Schema', undef, $user_id);
578 my $catalog_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($schema, 'stock_catalog_json', 'stock_property')->cvterm_id();
579 my $item_rs = $schema->resultset("Stock::Stock")->find( { uniquename
=> $item_name });
580 my $item_id = $item_rs->stock_id();
581 my $item_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id
=> $item_id, type_id
=> $catalog_cvterm_id});
582 my $item_info_string = $item_info_rs->value();
583 my $item_info_hash = decode_json
$item_info_string;
584 my $contact_person_id = $item_info_hash->{'contact_person_id'};
585 my $item_type = $item_info_hash->{'item_type'};
586 $details{$item_name}{'item_type'} = $item_type;
587 push my @item_list, \
%details;
588 print STDERR
"REQUEST DETAILS =".Dumper
(\
%details)."\n";
590 my $history_info = {};
592 my $new_order = CXGN
::Stock
::Order
->new( { bcs_schema
=> $schema, people_schema
=> $people_schema, dbh
=> $dbh});
593 $new_order->order_from_id($user_id);
594 $new_order->order_to_id($contact_person_id);
595 $new_order->order_status("submitted");
596 $new_order->create_date($timestamp);
597 my $order_id = $new_order->store();
598 # print STDERR "ORDER ID =".($order_id)."\n";
600 $c->stash->{rest
} = {error_string
=> "Error saving your order",};
604 $history_info ->{'submitted'} = $timestamp;
605 push @history, $history_info;
608 my $order_prop = CXGN
::Stock
::OrderBatch
->new({ bcs_schema
=> $schema, people_schema
=> $people_schema});
609 $order_prop->clone_list(\
@item_list);
610 $order_prop->parent_id($order_id);
611 $order_prop->history(\
@history);
612 my $order_prop_id = $order_prop->store_sp_orderprop();
613 # print STDERR "ORDER PROP ID =".($order_prop_id)."\n";
615 if (!$order_prop_id){
616 $c->stash->{rest
} = {error_string
=> "Error saving your order",};
620 my $contact_person = CXGN
::People
::Person
-> new
($dbh, $contact_person_id);
621 my $contact_email = $contact_person->get_contact_email();
623 my $host = $c->config->{main_production_site_url
};
624 my $project_name = $c->config->{project_name
};
625 my $subject="Ordering Notification from $project_name";
626 my $body=<<END_HEREDOC;
628 You have an order submitted to $project_name ($host/order/stocks/view).
629 Please do *NOT* reply to this message.
636 CXGN::Contact::send_email($subject,$body,$contact_email);
638 $c->stash->{rest}->{success} .= 'Your request has been submitted successfully and the vendor has been notified.';
643 sub get_order_tracking_ids :Path('/ajax/order/order_tracking_ids') Args(0) {
646 my $schema = $c->dbic_schema("Bio::Chado::Schema");
647 my $people_schema = $c->dbic_schema('CXGN::People::Schema');
648 my $dbh = $c->dbc->dbh;
649 my $order_number = $c->req->param('order_id');
653 $c->stash->{rest} = {error=>'You must be logged in to view your orders!'};
658 $user_id = $c->user()->get_object()->get_sp_person_id();
661 my $order_obj = CXGN::Stock::Order->new({ bcs_schema => $schema, dbh => $dbh, people_schema => $people_schema, sp_order_id => $order_number});
662 my $tracking_info = $order_obj->get_tracking_info();
664 $c->stash->{rest} = {tracking_info => $tracking_info};
669 sub download_order_item_file : Path('/ajax/order/download_order_item_file') Args(0) {
672 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
673 my $people_schema = $c->dbic_schema('CXGN::People::Schema');
674 my $dbh = $c->dbc->dbh;
676 my $user = $c->user();
678 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
681 my $user_id = $user->get_object()->get_sp_person_id();
683 my $order_id = $c->req->param('order_id');
684 # print STDERR "ORDER ID =".Dumper($order_id)."\n";
685 my $file_format = "xls";
687 my $time = DateTime->now();
688 my $timestamp = $time->ymd();
689 my $dir = $c->tempfiles_subdir('download');
690 my $temp_file_name = "order_items". "XXXX";
691 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
692 $rel_file = $rel_file . ".$file_format";
693 my $tempfile = $c->config->{basepath}."/".$rel_file;
694 # print STDERR "TEMPFILE : $tempfile\n";
697 if (!defined $order_id || $order_id eq '') {
698 $download = CXGN::Trial::Download->new({
699 bcs_schema => $schema,
700 people_schema => $people_schema,
702 filename => $tempfile,
703 format => 'OrderItemFileXLS',
707 $download = CXGN::Trial::Download->new({
708 bcs_schema => $schema,
709 people_schema => $people_schema,
711 filename => $tempfile,
712 format => 'OrderItemFileXLS',
714 trial_id => $order_id
718 my $error = $download->download();
720 my $file_name = "order_items" . "_" . "$timestamp" . ".$file_format";
721 $c->res->content_type('Application/'.$file_format);
722 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
724 my $output = read_file($tempfile);
726 $c->res->body($output);
732 sub get_active_order_tracking_ids :Path('/ajax/order/active_order_tracking_ids') Args(0) {
736 my $schema = $c->dbic_schema("Bio::Chado::Schema");
737 my $people_schema = $c->dbic_schema('CXGN::People::Schema');
738 my $dbh = $c->dbc->dbh;
739 my $order_properties = $c->config->{order_properties};
740 my @properties = split ',',$order_properties;
744 $c->stash->{rest} = {error=>'You must be logged in to view your orders!'};
749 $user_id = $c->user()->get_object()->get_sp_person_id();
752 my $orders = CXGN::Stock::Order->new({ dbh => $dbh, people_schema => $people_schema, order_to_id => $user_id, bcs_schema => $schema});
753 my $active_item_tracking_ids = $orders->get_active_item_tracking_info();
754 # print STDERR "ACTIVE TRACKING IDS =".Dumper($active_item_tracking_ids)."\n";
756 $c->stash->{rest} = {tracking_info => $active_item_tracking_ids};
761 sub get_order_progress :Path('/ajax/order/progress') Args(0) {
764 my $schema = $c->dbic_schema("Bio::Chado::Schema");
765 my $people_schema = $c->dbic_schema('CXGN::People::Schema');
766 my $dbh = $c->dbc->dbh;
767 my $tracking_activities = $c->config->{tracking_activities};
768 my @activity_types = split ',',$tracking_activities;
770 my $order_properties = $c->config->{order_properties};
771 my @properties = split ',',$order_properties;
776 $c->stash->{rest} = {error=>'You must be logged in to view your orders!'};
781 $user_id = $c->user()->get_object()->get_sp_person_id();
782 my $check_vendor_role = $c->user->check_roles('vendor');
785 my $orders = CXGN::Stock::Order->new({ dbh => $dbh, people_schema => $people_schema, bcs_schema => $schema, order_to_id => $user_id});
786 my $order_activities = $orders->get_orders_to_person_id_progress();
788 foreach my $activity_info (@$order_activities) {
790 my @request_details = ();
792 my $order_id = $activity_info->[0];
793 my $identifier_name = $activity_info->[1];
794 my $identifier_id = $activity_info->[2];
795 my $material_name = $activity_info->[3];
796 my $material_id = $activity_info->[4];
797 my $material_type = $activity_info->[5];
798 my $order_info = $activity_info->[7];
800 foreach my $field (@properties) {
801 my $each_detail = $order_info->{$field};
802 my $detail_string = $field. ":"."".$each_detail;
803 push @request_details, $detail_string;
805 push @request_details, $empty_string;
806 my $details_string = join("<br>", @request_details);
808 push @row, qq{<a href="/order/details/view/$order_id">$order_id</a>};
809 push @row, $details_string;
810 push @row, qq{<a href="/activity/details/$identifier_id">$identifier_name</a>};
811 push @row, qq{<a href="/stock/$material_id/view">$material_name</a>};
812 my $progress = $activity_info->[6];
816 my %progress_hash = %{$progress};
817 foreach my $type (@activity_types){
818 if ($progress_hash{$type}) {
820 my %details_hash = ();
821 $details = $progress_hash{$type};
822 %details_hash = %{$details};
824 foreach my $key (keys %details_hash) {
825 $input += $details_hash{$key}{'input'};
833 foreach my $type (@activity_types) {
838 push @order_progress,[@row];
841 print STDERR "ORDER PROGRESS =".Dumper(\@order_progress)."\n";
843 $c->stash->{rest} = {data => \@order_progress};