1 package Bcd
::Data
::Deposits
;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
26 #use Math::BigInt lib => "GMP";
28 use Bcd
::Constants
::DepositsConstants
;
36 INSERT_DEPOSIT_OR_WITHDRAWAL_REQUEST
=>
37 qq{INSERT into deposits_and_withdrawals
(id_user
, is_deposit
, amount
, status
, booking_token
, }.
38 qq{full_receipt_token
, secret_token
) }.
39 qq{values (?
, ?
, ?
, } . Bcd
::Constants
::DepositsConstants
::CREATION_STATE
. qq{, ?
, ?
, ?
)},
41 SELECT_DEPOSIT_OR_WITHDRAWAL_USER_BOOKING
=>
42 qq{SELECT
* from deposits_and_withdrawals where id_user
= ?
and is_deposit
= ?
and booking_token
= ?
},
44 SELECT_BOOKING_FROM_TOKEN
=>
45 qq{SELECT
* from deposits_and_withdrawals where booking_token
= ?
},
47 SELECT_BOOKING_FROM_ID
=>
48 qq{SELECT
* from deposits_and_withdrawals where id
= ?
},
50 SELECT_COUNT_PENDING_USERS_REQUESTS
=>
51 qq{SELECT count
(*) from deposits_and_withdrawals where id_user
=?
and }.
52 qq{status
!=}.Bcd
::Constants
::DepositsConstants
::COLLECTED
,
54 SELECT_PENDING_USERS_REQUEST
=>
55 qq{SELECT
* from deposits_and_withdrawals where id_user
=?
and }.
56 qq{status
!=}.Bcd
::Constants
::DepositsConstants
::COLLECTED
,
59 qq{DELETE FROM deposits_and_withdrawals WHERE id
= ?
},
65 SELECT_USER_DEPOSIT_TO_CLAIM
=> "Deposits_select_user_deposit_to_claim",
66 CLAIM_USER_DEPOSIT
=> "Deposits_claim_user_deposit",
67 INSERT_NEW_DEPOSIT_REQUEST
=> "Deposits_insert_deposit_request",
68 CHANGE_STATE_TO_DEPOSIT
=> "Deposits_change_state_to_deposit",
69 SELECT_DEPOSIT_USER_WITH_TOKEN
=> "Deposits_select_deposit_user_with_token",
70 SELECT_DEPOSIT_WITH_ID
=> "Deposits_select_deposit_with_id",
73 #this function is one shot... so it does not put the commands in the
76 my ($self, $stash) = @_;
77 my $conn = $stash->get_connection();
79 #ok, now I should insert the values in the table...
80 my $sth = $conn->prepare(qq{insert into deposit_withdrawal_statuses
values(?
,?
)});
82 foreach (Bcd
::Constants
::DepositsConstants
::LIST_DEPOSITS_STATES
){
83 $sth->bind_param(1, $_->[0]);
84 $sth->bind_param(2, $_->[1]);
92 sub delete_booking_id
{
93 my ($self, $stash, $booking_id) = @_;
95 my $st = $stash->prepare_cached(DELETE_BOOKING_ID
);
96 $st->bind_param(1, $booking_id);
100 sub get_pending_booking_from_user_id_arr
{
101 my ($self, $stash, $user_id) = @_;
103 my $st = $stash->prepare_cached(SELECT_PENDING_USERS_REQUEST
);
104 $st->bind_param(1, $user_id);
107 my $res = $st->fetchrow_arrayref();
113 =head1 are_there_pending_deposit_for_this_user
115 This function simply looks if the user has already some OR withdrawals active...
118 sub is_there_a_pending_request_for_this_user
{
119 my ($self, $stash, $user_id) = @_;
121 my $st = $stash->prepare_cached(SELECT_COUNT_PENDING_USERS_REQUESTS
);
122 $st->bind_param(1, $user_id);
125 my $res = $st->fetchrow_arrayref();
128 if ( $res->[0] != 0 ) {
136 sub _create_deposit_or_withdrawal_booking
{
137 my ($self, $stash, $user_id, $amount, $is_deposit) = @_;
139 my $booking_token = Bcd
::Data
::Token
::generate_new_token
();
140 my ($long_token, $blinded_token, $secret_token) = Bcd
::Data
::Token
::generate_long_token
();
142 #ok, now I should make the hash of the secret token
143 my $sha = Digest
::SHA1
->new;
144 $sha->add($secret_token);
145 my $digest = $sha->hexdigest;
147 #ok, now I am ready to insert the data in the table
148 my $st = $stash->prepare_cached(INSERT_DEPOSIT_OR_WITHDRAWAL_REQUEST
);
150 $st->bind_param(1, $user_id);
151 $st->bind_param(2, $is_deposit);
152 $st->bind_param(3, $amount);
153 $st->bind_param(4, $booking_token);
155 #if it is a withdrawal I record in the db the blinded token, not the full one
156 if ($is_deposit != 0){
157 $st->bind_param(5, $long_token);
159 $st->bind_param(5, $blinded_token);
162 $st->bind_param(6, $digest);
166 #I return these tokens, but one of them is not returned to the user.
167 return ($booking_token, $long_token, $blinded_token);
172 This function should see create a user deposit
176 sub create_deposit_booking
{
177 my ($self, $stash, $user_id, $amount) = @_;
179 my ($booking_token, $long_token, $blinded_token) =
180 $self->_create_deposit_or_withdrawal_booking
181 ($stash, $user_id, $amount, 1);
183 #the long token is NOT returned for the deposits
184 return ($booking_token, $blinded_token);
188 sub create_withdrawal_booking
{
190 my ($self, $stash, $user_id, $amount) = @_;
192 my ($booking_token, $long_token, $blinded_token) =
193 $self->_create_deposit_or_withdrawal_booking
194 ($stash, $user_id, $amount, 0);
196 #the blinded_token is NOT returned for the withdrawals
197 return ($booking_token, $long_token);
202 This function simply changes the state of the booking
205 sub treasurer_acknowledged
{
206 my ($self, $stash, $booking_id) = @_;
208 my $st = $stash->get_statement(CHANGE_STATE_TO_DEPOSIT
, $self);
210 $st->bind_param(1, Bcd
::Constants
::DepositsConstants
::TREASURER_ACKNOWLEDGED
);
211 $st->bind_param(2, $booking_id);
213 return $st->execute();
216 sub get_deposit_booking_from_user_and_token
{
218 my ($self, $stash, $user_id, $booking) = @_;
220 return $self->_get_deposit_or_withdrawal_booking_from_user_token
221 ($stash, $user_id, $booking, '1');
224 sub get_withdrawal_booking_from_user_and_token
{
226 my ($self, $stash, $user_id, $booking) = @_;
228 return $self->_get_deposit_or_withdrawal_booking_from_user_token
229 ($stash, $user_id, $booking, '0');
232 sub _get_deposit_or_withdrawal_booking_from_user_token
{
234 my ($self, $stash, $user_id, $booking, $is_deposit) = @_;
236 my $st = $stash->prepare_cached(SELECT_DEPOSIT_OR_WITHDRAWAL_USER_BOOKING
);
238 $st->bind_param(1, $user_id);
239 $st->bind_param(2, $is_deposit);
240 $st->bind_param(3, $booking);
244 my $row = $st->fetchrow_arrayref();
250 sub get_booking_from_token_arr
{
251 my ($self, $stash, $booking) = @_;
253 my $st = $stash->prepare_cached(SELECT_BOOKING_FROM_TOKEN
);
255 $st->bind_param(1, $booking);
259 my $arr = $st->fetchrow_arrayref();
265 sub get_booking_from_token_hash
{
267 my ($self, $stash, $booking) = @_;
269 my $st = $stash->prepare_cached(SELECT_BOOKING_FROM_TOKEN
);
271 $st->bind_param(1, $booking);
275 my $hash = $st->fetchrow_hashref();
282 sub get_booking_from_id_hash
{
283 my ($self, $stash, $id) = @_;
285 my $st = $stash->prepare_cached(SELECT_BOOKING_FROM_ID
);
287 $st->bind_param(1, $id);
291 my $hash = $st->fetchrow_hashref();
297 sub claim_this_deposit_object
{
298 my ($class, $stash, $ant_nest_code, $booking) = @_;
300 my $ticket_id = $booking->[0];
301 my $user = $booking->[2];
302 my $amount = $booking->[3];
304 my $amount_deposited_or_withdrawn;
307 ($amount_deposited_or_withdrawn, $new_total) = Bcd
::Data
::Bank
->deposit_user_account
308 ($stash, $user, $ant_nest_code, $amount);
310 my $st = $stash->get_statement(CHANGE_STATE_TO_DEPOSIT
, $class);
312 $st->bind_param(1, Bcd
::Constants
::DepositsConstants
::COLLECTED
);
313 $st->bind_param(2, $ticket_id);
316 return ($amount_deposited_or_withdrawn, $new_total);
319 sub claim_this_ticket
{
320 my ($self, $stash, $ant_nest_code, $ticket_id) = @_;
322 #first of all I should get the ticket from the db.
323 my $st = $stash->get_statement(SELECT_DEPOSIT_WITH_ID
, $self);
324 $st->bind_param(1, $ticket_id);
328 my $row = $st->fetchrow_arrayref();
332 my $is_deposit = $row->[1];
333 my $user = $row->[2];
334 my $amount = $row->[3];
337 #this should be given to the bank
338 my $amount_deposited_or_withdrawn;
341 if ( $is_deposit == '1'){
342 ($amount_deposited_or_withdrawn, $new_total) = Bcd
::Data
::Bank
->deposit_user_account
343 ($stash, $user, $ant_nest_code, $amount);
345 ($amount_deposited_or_withdrawn, $new_total) = Bcd
::Data
::Bank
->withdraw_user_account
346 ($stash, $user, $ant_nest_code, $amount);
349 #then I should change the status of the ticket
350 $st = $stash->get_statement(CHANGE_STATE_TO_DEPOSIT
, $self);
352 $st->bind_param(1, Bcd
::Constants
::DepositsConstants
::COLLECTED
);
353 $st->bind_param(2, $ticket_id);
356 return ($amount_deposited_or_withdrawn, $new_total);
359 sub populate_the_stash
{
360 my ($self, $db_stash) = @_;
364 $sql = qq{INSERT into deposits_and_withdrawals
(id_user
, is_deposit
, amount
, status
, booking_token
, }.
365 qq{full_receipt_token
, secret_token
) }.
366 qq{values (?
, 't', ?
, } . Bcd
::Constants
::DepositsConstants
::CREATION_STATE
. qq{, ?
, ?
, ?
)};
367 $db_stash->record_this_statement(INSERT_NEW_DEPOSIT_REQUEST
, $sql);
369 $sql = qq{UPDATE deposits_and_withdrawals SET status
=? where id
=?
};
370 $db_stash->record_this_statement(CHANGE_STATE_TO_DEPOSIT
, $sql);
372 $sql = qq{SELECT
* from deposits_and_withdrawals where id_user
=?
and is_deposit
='1' and booking_token
= ?
};
373 $db_stash->record_this_statement(SELECT_DEPOSIT_USER_WITH_TOKEN
, $sql);
375 $sql = qq{SELECT
* from deposits_and_withdrawals where id
= ?
};
376 $db_stash->record_this_statement(SELECT_DEPOSIT_WITH_ID
, $sql);