seed lot transactions shows proper links
[sgn.git] / lib / CXGN / Stock / Seedlot / Transaction.pm
bloba0a961147c487a72eb8089ad7b6dcba02df16828
2 package CXGN::Stock::Seedlot::Transaction;
4 use Moose;
5 use JSON::Any;
6 use SGN::Model::Cvterm;
8 has 'schema' => ( isa => 'Bio::Chado::Schema',
9 is => 'rw',
10 required => 1,
13 has 'transaction_id' => ( isa => 'Int',
14 is => 'rw',
15 predicate => 'has_transaction_id',
18 has 'from_stock' => ( isa => 'ArrayRef',
19 is => 'rw',
22 has 'to_stock' => (isa => 'ArrayRef',
23 is => 'rw',
26 has 'amount' => (isa => 'Num',
27 is => 'rw',
31 has 'operator' => ( isa => 'Maybe[Str]',
32 is => 'rw',
35 has 'timestamp' => ( isa => 'Maybe[Str]',
36 is => 'rw',
39 has 'factor' => ( isa => 'Int',
40 is => 'rw',
41 default => 1,
44 has 'description' => ( isa => 'Maybe[Str]',
45 is => 'rw',
48 sub BUILD {
49 my $self = shift;
51 if ($self->transaction_id()) {
52 my $row = $self->schema()->resultset("Stock::StockRelationship")
53 ->find( { stock_relationship_id => $self->transaction_id() }, { join => ['subject', 'object'], '+select' => ['subject.uniquename', 'subject.type_id', 'object.uniquename', 'object.type_id'], '+as' => ['subject_uniquename', 'subject_type_id', 'object_uniquename', 'object_type_id'] } );
55 $self->from_stock([$row->object_id(), $row->get_column('object_uniquename'), $row->get_column('object_type_id')]);
56 $self->to_stock([$row->subject_id(), $row->get_column('subject_uniquename'), $row->get_column('subject_type_id')]);
57 my $data = JSON::Any->decode($row->value());
58 $self->amount($data->{amount});
59 $self->timestamp($data->{timestamp});
60 $self->operator($data->{operator});
61 $self->description($data->{description});
65 # class method
66 sub get_transactions_by_seedlot_id {
67 my $class = shift;
68 my $schema = shift;
69 my $seedlot_id = shift;
71 print STDERR "Get transactions by seedlot...$seedlot_id\n";
72 my $type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "seed transaction", "stock_relationship")->cvterm_id();
73 my $rs = $schema->resultset("Stock::StockRelationship")->search({ subject_id => $seedlot_id , type_id => $type_id });
74 print STDERR "Found ".$rs->count()." transactions...\n";
75 my @transactions;
76 while (my $row = $rs->next()) {
78 my $t_obj = CXGN::Stock::Seedlot::Transaction->new( schema => $schema, transaction_id => $row->stock_relationship_id() );
80 push @transactions, $t_obj;
83 $rs = $schema->resultset("Stock::StockRelationship")->search({ object_id => $seedlot_id, type_id => $type_id });
85 while (my $row = $rs->next()) {
86 my $t_obj = CXGN::Stock::Seedlot::Transaction->new( schema => $schema, transaction_id => $row->stock_relationship_id() );
87 print STDERR "Found negative transaction...\n";
88 $t_obj->factor(-1);
89 push @transactions, $t_obj;
92 return \@transactions;
95 sub store {
96 my $self = shift;
97 my $transaction_type_id = SGN::Model::Cvterm->get_cvterm_row($self->schema(), "seed transaction", "stock_relationship")->cvterm_id();
99 if (!$self->has_transaction_id()) {
100 my $row = $self->schema()->resultset("Stock::StockRelationship")
101 ->find({
102 object_id => $self->from_stock()->[0],
103 subject_id => $self->to_stock()->[0],
104 type_id => $transaction_type_id,
107 my $new_rank = 0;
108 if ($row) {
109 $new_rank = $row->rank()+1;
112 $row = $self->schema()->resultset("Stock::StockRelationship")
113 ->create({
114 object_id => $self->from_stock()->[0],
115 subject_id => $self->to_stock()->[0],
116 type_id => $transaction_type_id,
117 rank => $new_rank,
118 value => JSON::Any->encode({
119 amount => $self->amount(),
120 timestamp => $self->timestamp(),
121 operator => $self->operator(),
122 description => $self->description()
125 return $row->stock_relationship_id();
128 else {
129 # update not implemented yet.
133 sub delete {