do not attempt get the person object when no one is logged in...
[sgn.git] / lib / CXGN / Stock / Order.pm
blobcb7a597d4f36d574cc710285b610fbebf4549a87
2 package CXGN::Stock::Order;
4 use Moose;
5 use Data::Dumper;
6 use CXGN::Stock::OrderBatch;
7 use CXGN::People::Person;
8 use JSON;
9 use SGN::Model::Cvterm;
12 has 'people_schema' => ( isa => 'Ref', is => 'rw', required => 1 );
14 has 'dbh' => (is => 'rw', required => 1,);
16 has 'sp_order_id' => (isa => 'Int', is => 'rw' );
18 has 'order_from_id' => ( isa => 'Int', is => 'rw' );
20 has 'order_to_id' => ( isa => 'Int', is => 'rw' );
22 has 'order_status' => ( isa => 'Str', is => 'rw' );
24 has 'comments' => ( isa => 'Str', is => 'rw');
26 has 'create_date' => ( isa => 'Str', is => 'rw');
28 has 'completion_date' => ( isa => 'Str', is => 'rw');
30 has 'batches' => ( isa => 'Ref', is => 'rw', default => sub { return []; } );
32 has 'bcs_schema' => ( isa => 'Bio::Chado::Schema', is => 'rw');
35 sub BUILD {
36 my $self = shift;
37 my $args = shift;
38 my $people_schema = $self->people_schema();
40 if (! $args->{sp_order_id}) {
41 print STDERR "Creating empty object...\n";
42 return $self;
45 my $row = $people_schema->resultset('SpOrder')->find( { sp_order_id => $args->{sp_order_id} } );
47 if (!$row) {
48 die "The database has no order entry with id $args->{sp_order_id}";
51 $self->order_from_id($row->order_from_id);
52 $self->order_to_id($row->order_to_id);
53 $self->create_date($row->create_date);
58 sub store {
59 my $self = shift;
60 my %data = (
61 order_status => $self->order_status(),
62 order_from_id => $self->order_from_id(),
63 order_to_id => $self->order_to_id(),
64 comments => $self->comments(),
65 create_date => $self->create_date(),
66 completion_date => $self->completion_date(),
69 if ($self->sp_order_id()) { $data{sp_order_id} = $self->sp_order_id(); }
71 my $rs = $self->people_schema()->resultset('SpOrder');
73 my $row = $rs->update_or_create( \%data );
75 return $row->sp_order_id();
79 sub get_orders_from_person_id {
80 my $self = shift;
81 my $schema = $self->bcs_schema();
82 my $people_schema = $self->people_schema();
83 my $person_id = $self->order_from_id();
84 my $dbh = $self->dbh();
86 my $order_batch_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_batch_json', 'sp_order_property')->cvterm_id();
88 my $order_rs = $people_schema->resultset('SpOrder')->search( { order_from_id => $person_id } );
89 my @orders;
90 while (my $result = $order_rs->next()){
91 my $item_list;
92 my $order_id = $result->sp_order_id();
93 my $order_to_id = $result->order_to_id();
94 my $order_status = $result->order_status();
95 my $create_date = $result->create_date();
96 my $completion_date = $result->completion_date();
97 my $comments = $result->comments();
98 my $person= CXGN::People::Person->new($dbh, $order_to_id);
99 my $order_to_name=$person->get_first_name()." ".$person->get_last_name();
101 my $orderprop_rs = $people_schema->resultset('SpOrderprop')->search( { sp_order_id => $order_id, type_id => $order_batch_json_cvterm_id } );
102 my $all_items = ();
103 while (my $item_result = $orderprop_rs->next()){
104 my @list;
105 my $item_json = $item_result->value();
106 my $item_hash = JSON::Any->jsonToObj($item_json);
107 $all_items = $item_hash->{'clone_list'};
110 push @orders, {
111 order_id => $order_id,
112 create_date => $create_date,
113 clone_list => $all_items,
114 order_status => $order_status,
115 completion_date => $completion_date,
116 order_to_name => $order_to_name,
117 comments => $comments
121 return \@orders;
125 sub get_orders_to_person_id {
126 my $self = shift;
127 my $schema = $self->bcs_schema();
128 my $people_schema = $self->people_schema();
129 my $person_id = $self->order_to_id();
130 my $dbh = $self->dbh();
131 my $order_batch_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_batch_json', 'sp_order_property')->cvterm_id();
133 my $order_rs = $people_schema->resultset('SpOrder')->search( { order_to_id => $person_id } );
134 my @orders;
135 while (my $result = $order_rs->next()){
136 my $item_list;
137 my $order_id = $result->sp_order_id();
138 my $order_from_id = $result->order_from_id();
139 my $order_status = $result->order_status();
140 my $create_date = $result->create_date();
141 my $completion_date = $result->completion_date();
142 my $comments = $result->comments();
143 my $person= CXGN::People::Person->new($dbh, $order_from_id);
144 my $order_from_name=$person->get_first_name()." ".$person->get_last_name();
146 my $orderprop_rs = $people_schema->resultset('SpOrderprop')->search( { sp_order_id => $order_id, type_id => $order_batch_json_cvterm_id } );
147 my $all_items = ();
148 while (my $item_result = $orderprop_rs->next()){
149 my @list;
150 my $item_json = $item_result->value();
151 my $item_hash = JSON::Any->jsonToObj($item_json);
152 $all_items = $item_hash->{'clone_list'};
155 push @orders, {
156 order_id => $order_id,
157 order_from_name => $order_from_name,
158 create_date => $create_date,
159 clone_list => $all_items,
160 order_status => $order_status,
161 completion_date => $completion_date,
162 contact_person_comments => $comments
166 return \@orders;
170 sub get_order_details {
171 my $self = shift;
172 my $schema = $self->bcs_schema();
173 my $people_schema = $self->people_schema();
174 my $dbh = $self->dbh();
175 my $order_id = $self->sp_order_id();
176 my @order_details;
177 my $order_batch_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_batch_json', 'sp_order_property')->cvterm_id();
179 my $order_rs = $people_schema->resultset('SpOrder')->find( { sp_order_id => $order_id } );
181 my $order_from_id = $order_rs->order_from_id();
182 my $from_person= CXGN::People::Person->new($dbh, $order_from_id);
183 my $order_from_name=$from_person->get_first_name()." ".$from_person->get_last_name();
185 my $order_to_id = $order_rs->order_to_id();
186 my $to_person= CXGN::People::Person->new($dbh, $order_to_id);
187 my $order_to_name=$to_person->get_first_name()." ".$to_person->get_last_name();
189 my $order_status = $order_rs->order_status();
190 my $create_date = $order_rs->create_date();
191 my $completion_date = $order_rs->completion_date();
192 my $comments = $order_rs->comments();
194 my $orderprop_rs = $people_schema->resultset('SpOrderprop')->find( { sp_order_id => $order_id, type_id => $order_batch_json_cvterm_id } );
195 my $item_json = $orderprop_rs->value();
196 my $item_hash = JSON::Any->jsonToObj($item_json);
197 my $all_items = $item_hash->{'clone_list'};
199 push @order_details, $order_id, $order_from_name, $create_date, $all_items, $order_to_name, $order_status, $comments, $order_to_id;
201 return \@order_details;
206 sub get_tracking_info {
207 my $self = shift;
208 my $schema = $self->bcs_schema();
209 my $people_schema = $self->people_schema();
210 my $dbh = $self->dbh();
211 my $order_id = $self->sp_order_id();
212 my $order_batch_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_batch_json', 'sp_order_property')->cvterm_id();
214 my $orderprop_rs = $people_schema->resultset('SpOrderprop')->find( { sp_order_id => $order_id, type_id => $order_batch_json_cvterm_id } );
215 my $item_json = $orderprop_rs->value();
216 my $item_hash = JSON::Any->jsonToObj($item_json);
217 my $all_items = $item_hash->{'clone_list'};
219 my @all_tracking_info;
220 my $item_number = 0;
221 foreach my $item (@$all_items) {
222 my $item_number_string;
223 my %item_details = %$item;
224 my ($name, $value) = %item_details;
225 my $item_rs = $schema->resultset("Stock::Stock")->find({uniquename => $name});
226 my $item_stock_id = $item_rs->stock_id();
227 my $required_quantity = $value->{'Required Quantity'};
228 my $required_stage = $value->{'Required Stage'};
229 $item_number++;
230 $item_number_string = $order_id.'-'.$item_number;
232 push @all_tracking_info, [ "order".$order_id.":".$name, "order".$order_id.":".$item_stock_id, $name, $order_id, $item_number_string, $required_quantity, $required_stage,]
235 return \@all_tracking_info;
239 sub get_active_item_tracking_info {
240 my $self = shift;
241 my $people_schema = $self->people_schema();
242 my $person_id = $self->order_to_id();
243 my $schema = $self->bcs_schema();
244 my $dbh = $self->dbh();
245 my @all_tracking_info;
246 my $order_batch_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_batch_json', 'sp_order_property')->cvterm_id();
248 my $order_rs = $people_schema->resultset('SpOrder')->search( { order_to_id => $person_id } );
249 my @orders;
250 while (my $result = $order_rs->next()){
251 my $item_list;
252 my $order_id = $result->sp_order_id();
253 my $order_from_id = $result->order_from_id();
254 my $order_status = $result->order_status();
255 my $create_date = $result->create_date();
256 my $completion_date = $result->completion_date();
257 my $comments = $result->comments();
258 my $person= CXGN::People::Person->new($dbh, $order_from_id);
259 my $order_from_name=$person->get_first_name()." ".$person->get_last_name();
261 my $orderprop_rs = $people_schema->resultset('SpOrderprop')->search( { sp_order_id => $order_id, type_id => $order_batch_json_cvterm_id } );
262 my $all_items = ();
263 while (my $item_result = $orderprop_rs->next()){
264 my @list;
265 my $item_json = $item_result->value();
266 my $item_hash = JSON::Any->jsonToObj($item_json);
267 $all_items = $item_hash->{'clone_list'};
268 my $item_number = 0;
269 foreach my $item (@$all_items) {
270 my $item_number_string;
271 my %item_details = %$item;
272 my ($name, $value) = %item_details;
273 my $item_rs = $schema->resultset("Stock::Stock")->find({uniquename => $name});
274 my $item_stock_id = $item_rs->stock_id();
275 my $required_quantity = $value->{'Required Quantity'};
276 my $required_stage = $value->{'Required Stage'};
277 $item_number++;
278 $item_number_string = $order_id.'-'.$item_number;
280 push @all_tracking_info, [ "order".$order_id.":".$name, "order".$order_id.":".$item_stock_id, $name, $order_id, $item_number_string, $required_quantity, $required_stage,]
286 return \@all_tracking_info;
291 sub get_orders_to_person_id_progress {
292 my $self = shift;
293 my $schema = $self->bcs_schema();
294 my $people_schema = $self->people_schema();
295 my $person_id = $self->order_to_id();
296 my $dbh = $self->dbh();
298 my $tracking_identifier_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'tracking_identifier', 'stock_type')->cvterm_id();
299 my $tracking_data_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'tracking_tissue_culture_json', 'stock_property')->cvterm_id();
300 my $material_of_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'material_of', 'stock_relationship')->cvterm_id();
301 my $order_identifier_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_tracking_identifiers', 'sp_order_property')->cvterm_id();
302 my $order_batch_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'order_batch_json', 'sp_order_property')->cvterm_id();
304 my $order_rs = $people_schema->resultset('SpOrder')->search( { order_to_id => $person_id } );
305 my @activity_info;
306 while (my $result = $order_rs->next()){
307 my %required_info = ();
308 my $order_id = $result->sp_order_id();
309 my $orderprop_item_info_rs = $people_schema->resultset('SpOrderprop')->find( { sp_order_id => $order_id, type_id => $order_batch_json_cvterm_id } );
310 my $item_json = $orderprop_item_info_rs->value();
311 my $item_hash = JSON::Any->jsonToObj($item_json);
312 my $all_items_info = $item_hash->{'clone_list'};
313 foreach my $item (@$all_items_info) {
314 my %item_details = %$item;
315 my ($name, $value) = %item_details;
316 $required_info{$name} = $value;
319 my $orderprop_identifiers_rs = $people_schema->resultset('SpOrderprop')->search( { sp_order_id => $order_id, type_id => $order_identifier_cvterm_id } );
320 while (my $order_details_result = $orderprop_identifiers_rs->next()){
321 my @list = ();
322 my $order_details_json = $order_details_result->value();
323 my $order_details_hash = JSON::Any->jsonToObj($order_details_json);
324 my $tracking_identifier = $order_details_hash->{'tracking_identifiers'};
325 @list = @$tracking_identifier;
327 foreach my $identifier_id (@list) {
328 my $order_info = {};
329 my $activity_hash = {};
330 my $identifier_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $identifier_id, type_id => $tracking_identifier_cvterm_id });
331 my $identifier_name = $identifier_rs->uniquename();
332 my $material_info = $schema->resultset("Stock::StockRelationship")->find( { object_id => $identifier_id, type_id => $material_of_cvterm_id} );
333 my $material_id = $material_info->subject_id();
334 my $material_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $material_id });
335 my $material_name = $material_rs->uniquename();
336 my $material_type = $material_rs->type_id();
337 $order_info = $required_info{$material_name};
338 my $activity_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $identifier_id, type_id => $tracking_data_json_cvterm_id});
339 if ($activity_info_rs) {
340 my $activity_json = $activity_info_rs->value();
341 $activity_hash = JSON::Any->jsonToObj($activity_json);
344 push @activity_info, [$order_id, $identifier_name, $identifier_id, $material_name, $material_id, $material_type, $activity_hash, $order_info];
349 return \@activity_info;