7 use SGN::Test::Fixture;
8 use CXGN::People::Schema;
9 use CXGN::People::Roles;
11 use CXGN::Stock::Catalog;
12 use CXGN::People::Person;
13 use CXGN::Stock::Order;
14 use CXGN::Stock::OrderBatch;
16 use CXGN::Stock::ParseUpload;
17 use Test::WWW::Mechanize;
22 local $Data::Dumper::Indent = 0;
24 my $f = SGN::Test::Fixture->new();
25 my $schema = $f->bcs_schema();
26 my $people_schema = $f->people_schema;
29 my $mech = Test::WWW::Mechanize->new;
31 $mech->post_ok('http://localhost:3010/brapi/v1/token', [ "username" => "janedoe", "password" => "secretpw", "grant_type" => "password" ]);
32 my $response = decode_json $mech->content;
33 is($response->{'metadata'}->{'status'}->[2]->{'message'}, 'Login Successfull');
34 my $sgn_session_id = $response->{access_token};
36 #add vendor role for johndoe
37 my $johndoe_id = CXGN::People::Person->get_person_by_username($dbh, 'johndoe');
38 my $role_rs = $people_schema->resultset("SpRole")->find({ name => 'vendor' });
39 my $vendor_id = $role_rs->sp_role_id();
40 my $person_roles = CXGN::People::Roles->new({ bcs_schema => $schema });
41 my $add_role = $person_roles->add_sp_person_role($johndoe_id, $vendor_id);
43 #test adding catalog item
44 my $catalog_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "stock_catalog_json", "stock_property")->cvterm_id();
45 my $before_adding_catalog_item = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
46 my $all_stockprop_before_adding = $schema->resultset("Stock::Stockprop")->search({})->count();
48 my $program_id = $schema->resultset('Project::Project')->find({ name => 'test' })->project_id();
50 $mech->post_ok('http://localhost:3010/ajax/catalog/add_item', [ 'name' => 'UG120001', 'category' => 'released variety', 'additional_info' => 'test', 'material_source' => 'BTI', 'breeding_program_id' => $program_id, 'contact_person' => 'johndoe' ]);
52 $response = decode_json $mech->content;
53 is($response->{'success'}, '1');
55 my $after_adding_catalog_item = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
56 my $after_adding_all_stockprop = $schema->resultset("Stock::Stockprop")->search({})->count();
58 is($after_adding_catalog_item, $before_adding_catalog_item + 1);
59 is($after_adding_all_stockprop, $all_stockprop_before_adding + 1);
62 #test uploading catalog items
63 my $extension = "xls";
65 my $before_uploading_catalog_items = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
66 my $before_uploading_all_stockprop = $schema->resultset("Stock::Stockprop")->search({})->count();
68 my $file = $f->config->{basepath} . "/t/data/stock/catalog_items.$extension";
69 my $ua = LWP::UserAgent->new;
70 $response = $ua->post(
71 'http://localhost:3010/ajax/catalog/upload_items',
72 Content_Type => 'form-data',
74 "catalog_items_upload_file" => [
76 "catalog_items.$extension",
77 Content_Type => ($extension eq "xls") ? 'application/vnd.ms-excel' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
79 "sgn_session_id" => $sgn_session_id
82 ok($response->is_success);
83 my $message = $response->decoded_content;
84 my $message_hash = decode_json $message;
85 is_deeply($message_hash, { 'success' => 1 });
87 my $after_uploading_catalog_items = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
88 my $after_uploading_all_stockprop = $schema->resultset("Stock::Stockprop")->search({})->count();
90 is($after_uploading_catalog_items, $before_uploading_catalog_items + 2);
91 is($after_uploading_all_stockprop, $before_uploading_all_stockprop + 2);
93 #test adding catalog items using list
94 my $list_id = CXGN::List::create_list($schema->storage->dbh(), 'accessions_for_catalog', 'test', $johndoe_id );
95 my $list = CXGN::List->new( { dbh => $schema->storage->dbh(), list_id => $list_id });
96 $list->type('accessions');
97 $list->add_bulk( [ 'UG120005', 'UG120006', 'UG120007']);
99 my $before_adding_catalog_list = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
100 my $before_adding_catalog_list_all_stockprop = $schema->resultset("Stock::Stockprop")->search({})->count();
102 $mech->post_ok('http://localhost:3010/ajax/catalog/add_item_list', [ 'list_type' => 'accessions', 'catalog_list' => $list_id, 'category' => 'released variety', 'additional_info' => 'test', 'material_source' => 'BTI', 'breeding_program_id' => $program_id, 'contact_person' => 'johndoe' ]);
104 $response = decode_json $mech->content;
105 is($response->{'success'}, '1');
107 my $after_adding_catalog_list = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
108 my $after_adding_catalog_list_all_stockprop = $schema->resultset("Stock::Stockprop")->search({})->count();
110 is($after_adding_catalog_list, $before_adding_catalog_list + 3);
111 is($after_adding_catalog_list_all_stockprop, $before_adding_catalog_list_all_stockprop + 3);
113 #delete list after testing
114 CXGN::List::delete_list($dbh, $list_id);
117 $mech->post_ok("http://localhost:3010/ajax/catalog/items");
118 $response = decode_json $mech->content;
119 my $catalog_info = $response->{'data'};
120 my $total_number_of_items = scalar @$catalog_info;
121 is($total_number_of_items, 6 );
123 #creating shopping cart with 'catalog_items' list type
124 my $janedoe_id = CXGN::People::Person->get_person_by_username($dbh, 'janedoe');
125 my $your_cart_id = CXGN::List::create_list($dbh, 'your_cart', 'test shopping cart', $janedoe_id);
126 my $list = CXGN::List->new({ dbh => $dbh, list_id => $your_cart_id });
127 my $your_cart_type = $list->type('catalog_items');
128 my $item1 = $list->add_element('{"Item Name":"UG120001","Quantity":"2","Comments":""}');
129 my $item2 = $list->add_element('{"Item Name":"UG120002","Quantity":"3","Comments":""}');
131 my $list_items = $list->elements();
133 #test storing an order from janedoe, to johndoe
135 my $before_adding_an_order = $people_schema->resultset('SpOrder')->search( { order_to_id => $johndoe_id })->count();
137 $mech->post_ok('http://localhost:3010/ajax/order/submit', ['list_id' => $your_cart_id,]);
138 $response = decode_json $mech->content;
139 is($response->{'success'}, 'Your order has been submitted successfully and the vendor has been notified.');
141 my $after_adding_an_order = $people_schema->resultset('SpOrder')->search( { order_to_id => $johndoe_id })->count();
142 is($after_adding_an_order, $before_adding_an_order + 1);
145 CXGN::List::delete_list($dbh, $your_cart_id);
147 #test retrieving order from janedoe
148 my $buyer_order_obj = CXGN::Stock::Order->new({ dbh => $dbh, bcs_schema => $schema, people_schema => $people_schema, order_from_id => $janedoe_id });
149 my $buyer_orders = $buyer_order_obj->get_orders_from_person_id();
150 my $first_order_info = $buyer_orders->[0];
151 is($first_order_info->{'order_id'}, '1');
152 is($first_order_info->{'order_status'}, 'submitted');
153 is($first_order_info->{'order_to_name'}, 'John Doe');
155 my $items = $first_order_info->{'clone_list'};
156 my $buyer_num_items = @$items;
157 is($buyer_num_items, '2');
159 #test retrieving order to johndoe
160 my $vendor_order_obj = CXGN::Stock::Order->new({ dbh => $dbh, bcs_schema => $schema, people_schema => $people_schema, order_to_id => $johndoe_id });
161 my $vendor_orders = $vendor_order_obj->get_orders_to_person_id();
163 my $order = $vendor_orders->[0];
164 is($order->{'order_id'}, '1');
165 is($order->{'order_status'}, 'submitted');
166 is($order->{'order_from_name'}, 'Jane Doe');
167 is($order->{'completion_date'}, undef);
168 is($order->{'contact_person_comments'}, undef);
170 my $clone_list = $order->{'clone_list'};
171 my $vendor_num_items = @$clone_list;
172 is($vendor_num_items, '2');
174 #test updating order status by johndoe
175 my $time = DateTime->now();
176 my $timestamp = $time->ymd()."_".$time->hms();
177 my $order_obj = CXGN::Stock::Order->new({ dbh => $dbh, bcs_schema => $schema, people_schema => $people_schema, sp_order_id => '1', order_to_id => $johndoe_id, order_status => 'completed', completion_date => $timestamp, comments => 'updated by johndoe'});
178 my $updated_order = $order_obj->store();
179 my $after_updating_an_order = $people_schema->resultset('SpOrder')->search( { order_to_id => $johndoe_id })->count();
180 is($after_updating_an_order, $after_adding_an_order);
182 #test re-opening an order by janedoe
183 $mech->post_ok('http://localhost:3010/ajax/order/update', ['order_id' => '1', 'new_status' => 're-opened', 'contact_person_comments' => 'test re-opening an order' ]);
184 $response = decode_json $mech->content;
185 is($response->{'success'}, '1');
187 my $re_opened_order = CXGN::Stock::Order->new({ dbh => $dbh, bcs_schema => $schema, people_schema => $people_schema, sp_order_id => '1' });
188 my $order_result = $re_opened_order->get_order_details();
189 my $order_status = $order_result->[5];
190 is($order_status, 're-opened by Jane Doe');
192 #test_single_step_submission
193 $mech->post_ok('http://localhost:3010/ajax/order/single_step_submission', ['item_name' => 'UG120001', 'order_details' => '{"Quantity":"2","Comments":""}']);
194 $response = decode_json $mech->content;
195 is($response->{'success'}, 'Your request has been submitted successfully and the vendor has been notified.');
197 #test deleting catalog
198 my $catalog_rs = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id });
200 while (my $catalog = $catalog_rs->next()) {
201 my $catalog_stockprop_id = $catalog->stockprop_id();
202 my $catalog_obj = CXGN::Stock::Catalog->new({ bcs_schema => $schema, prop_id => $catalog_stockprop_id });
203 $catalog_obj->delete();
206 my $after_deleting_catalog_items = $schema->resultset("Stock::Stockprop")->search({ type_id => $catalog_type_id })->count();
207 my $all_stockprop_after_deleting_catalog = $schema->resultset("Stock::Stockprop")->search({})->count();
209 is($after_deleting_catalog_items, $before_adding_catalog_item);
210 is($all_stockprop_after_deleting_catalog, $all_stockprop_before_adding);