2 package CXGN
::Stock
::Seedlot
::Transaction
;
6 use SGN
::Model
::Cvterm
;
8 has
'schema' => ( isa
=> 'Bio::Chado::Schema',
13 has
'transaction_id' => ( isa
=> 'Int',
15 predicate
=> 'has_transaction_id',
18 has
'from_stock' => ( isa
=> 'ArrayRef',
22 has
'to_stock' => (isa
=> 'ArrayRef',
26 has
'amount' => (isa
=> 'Num',
31 has
'operator' => ( isa
=> 'Maybe[Str]',
35 has
'timestamp' => ( isa
=> 'Maybe[Str]',
39 has
'factor' => ( isa
=> 'Int',
44 has
'description' => ( isa
=> 'Maybe[Str]',
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
});
66 sub get_transactions_by_seedlot_id
{
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";
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";
89 push @transactions, $t_obj;
92 return \
@transactions;
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")
102 object_id
=> $self->from_stock()->[0],
103 subject_id
=> $self->to_stock()->[0],
104 type_id
=> $transaction_type_id,
109 $new_rank = $row->rank()+1;
112 $row = $self->schema()->resultset("Stock::StockRelationship")
114 object_id
=> $self->from_stock()->[0],
115 subject_id
=> $self->to_stock()->[0],
116 type_id
=> $transaction_type_id,
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();
129 # update not implemented yet.