added the DeletePendingRequestCommand
[breadcrumbs.git] / src / lib / Bcd / Data / Deposits.pm
blob6fd9db218a7537248f817be5b9f0ecf28e2cdcda
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
19 # 02110-1301, USA.
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 use strict;
24 use warnings;
26 #use Math::BigInt lib => "GMP";
28 use Bcd::Constants::DepositsConstants;
29 use Bcd::Data::Token;
30 use Bcd::Data::Bank;
32 use Digest::SHA1;
34 use constant {
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,
58 DELETE_BOOKING_ID =>
59 qq{DELETE FROM deposits_and_withdrawals WHERE id = ? },
64 use constant{
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
74 #stash
75 sub init_db{
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]);
85 $sth->execute();
88 $sth->finish();
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);
97 $st->execute();
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);
106 $st->execute();
107 my $res = $st->fetchrow_arrayref();
108 $st->finish();
110 return $res;
113 =head1 are_there_pending_deposit_for_this_user
115 This function simply looks if the user has already some OR withdrawals active...
117 =cut
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);
124 $st->execute();
125 my $res = $st->fetchrow_arrayref();
126 $st->finish();
128 if ( $res->[0] != 0 ) {
129 return 1;
130 } else {
131 return 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);
158 } else {
159 $st->bind_param(5, $blinded_token);
162 $st->bind_param(6, $digest);
164 $st->execute();
166 #I return these tokens, but one of them is not returned to the user.
167 return ($booking_token, $long_token, $blinded_token);
170 =head1
172 This function should see create a user deposit
174 =cut
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);
200 =head2
202 This function simply changes the state of the booking
204 =cut
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);
242 $st->execute();
244 my $row = $st->fetchrow_arrayref();
245 $st->finish();
247 return $row;
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);
257 $st->execute();
259 my $arr = $st->fetchrow_arrayref();
260 $st->finish();
262 return $arr;
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);
273 $st->execute();
275 my $hash = $st->fetchrow_hashref();
276 $st->finish();
278 return $hash;
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);
289 $st->execute();
291 my $hash = $st->fetchrow_hashref();
292 $st->finish();
294 return $hash;
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;
305 my $new_total;
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);
314 $st->execute();
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);
326 $st->execute();
328 my $row = $st->fetchrow_arrayref();
329 $st->finish();
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;
339 my $new_total;
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);
344 } else {
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);
354 $st->execute();
356 return ($amount_deposited_or_withdrawn, $new_total);
359 sub populate_the_stash{
360 my ($self, $db_stash) = @_;
362 my $sql;
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);