1 package Bcd
::Data
::Invoices
;
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).
28 use Bcd
::Constants
::InvoicesConstants
;
34 qq{INSERT INTO invoices
(id_status
, id_ad
, id_locus
, user_to
, amount
) VALUES
(?
,?
,?
,?
,?
)},
37 qq{SELECT id_status
, id_ad
, id_locus
, user_to
, date
, amount
, quantity FROM invoices WHERE id
= ?
},
40 qq{UPDATE invoices set quantity
= ?
, id_status
= ? WHERE id
= ?
},
42 UPDATE_INVOICE_STATE
=>
43 qq{UPDATE invoices SET id_status
= ? WHERE id
= ?
},
45 CREATE_INVOICE_TOKEN
=>
46 qq{INSERT INTO invoices_tokens
(id_invoice
, amount_paid_by_credit
, }.
47 qq{ full_token
, secret_token
) VALUES
(?
,?
,?
,?
)},
50 qq{SELECT amount_paid_by_credit
, full_token
, secret_token FROM invoices_tokens WHERE id_invoice
= ?
},
52 DELETE_INVOICE_TOKEN
=>
53 qq{DELETE FROM invoices_tokens WHERE id_invoice
= ?
},
55 GET_RECEIVED_INVOICES_SUMMARY_BY_STATE
=>
56 qq{SELECT count
(*), sum
(quantity
* amount
) as total FROM invoices WHERE user_to
= ? AND id_status
= ?
},
58 GET_RECEIVED_INVOICES_LIST_BY_STATE
=>
59 qq{SELECT i
.id
, a
.id_user as seller
, i
.amount
, i
.quantity
, (i
.amount
* i
.quantity
) AS total
}.
60 qq{FROM invoices AS i
, ads AS a
}.
61 qq{WHERE i
.id_ad
= a
.id AND i
.user_to
= ? AND i
.id_status
= ?
},
63 GET_EMITTED_INVOICES_SUMMARY_BY_STATE
=>
64 qq{SELECT count
(i
.*), sum
(i
.quantity
* i
.amount
) as total FROM invoices AS i
, ads AS a
}.
65 qq{WHERE i
.id_ad
= a
.id AND a
.id_user
= ? AND i
.id_status
= ?
},
67 GET_EMITTED_INVOICES_LIST_BY_STATE
=>
68 qq{SELECT i
.id
, i
.user_to as buyer
, i
.amount
, i
.quantity
, (i
.amount
* i
.quantity
) AS total
}.
69 qq{FROM invoices AS i
, ads AS a
}.
70 qq{WHERE i
.id_ad
= a
.id AND a
.id_user
= ? AND id_status
= ?
},
74 sub get_received_invoices_list_by_state_ds
{
75 my ($class, $stash, $buyer, $state) = @_;
77 return $stash->select_all_ds
78 (GET_RECEIVED_INVOICES_LIST_BY_STATE
,
83 sub get_emitted_invoices_list_by_state_ds
{
84 my ($class, $stash, $seller, $state) = @_;
86 return $stash->select_all_ds
87 (GET_EMITTED_INVOICES_LIST_BY_STATE
,
91 sub get_received_invoices_summary_by_state_arr
{
92 my ($class, $stash, $buyer, $state) = @_;
94 return $stash->select_one_row_arr
95 (GET_RECEIVED_INVOICES_SUMMARY_BY_STATE
,
99 sub get_emitted_invoices_summary_by_state_arr
{
100 my ($class, $stash, $seller, $state) = @_;
102 return $stash->select_one_row_arr
103 (GET_EMITTED_INVOICES_SUMMARY_BY_STATE
,
107 sub get_invoice_token_hash
{
108 my ($class, $stash, $invoice_id) = @_;
109 return $stash->select_one_row_hash(GET_INVOICE_TOKEN
, $invoice_id);
113 #this function should create a new invoice
114 sub create_new_invoice
{
115 my ($class, $stash, $buyer, $id_ad, $id_locus, $amount) = @_;
117 $stash->prep_exec(INSERT_NEW_INVOICE
,
118 Bcd
::Constants
::InvoicesConstants
::AUTHORIZED
,
119 $id_ad,$id_locus, $buyer, $amount);
121 return $stash->get_last_invoice_id();
124 #this function should simply emit the invoice, with a quantity
126 my ($class, $stash, $invoice_id, $quantity) = @_;
128 $stash->prep_exec(EMIT_INVOICE
,
130 Bcd
::Constants
::InvoicesConstants
::EMITTED
,
135 #when I pay the invoice I generate the token...
136 sub pay_invoice_and_get_token
{
137 my ($class, $stash, $invoice_id, $amount_paid_by_credit) = @_;
139 #ok, first of all I update the state of the invoice.
140 $stash->prep_exec(UPDATE_INVOICE_STATE
,
141 Bcd
::Constants
::InvoicesConstants
::PAID
,
144 #ok, then I generate two tokens...
145 my ($long_token, $blinded_token, $secret_token) =
146 Bcd
::Data
::Token
::generate_long_token
();
148 #digest the secret token
149 my $sha = Digest
::SHA1
->new;
150 $sha->add($secret_token);
151 my $digest = $sha->hexdigest;
153 #ok, I will now add this row
154 $stash->prep_exec(CREATE_INVOICE_TOKEN
,
156 $amount_paid_by_credit,
161 #I return the long token for the buyer.
162 return ($long_token, $blinded_token);
166 my ($class, $stash, $invoice_id) = @_;
168 #I should simply change the state and delete the tokens row (which is useless now).
169 $stash->prep_exec(UPDATE_INVOICE_STATE
,
170 Bcd
::Constants
::InvoicesConstants
::COLLECTED
,
173 $stash->prep_exec(DELETE_INVOICE_TOKEN
,
178 my ($class, $stash, $invoice_id) = @_;
179 return $stash->select_one_row_arr(GET_INVOICE
, $invoice_id);
182 sub get_invoice_hash
{
183 my ($class, $stash, $invoice_id) = @_;
184 return $stash->select_one_row_hash(GET_INVOICE
, $invoice_id);
189 my ($class, $stash) = @_;
191 my $conn = $stash->get_connection();
193 #ok, now I should insert the values in the table...
194 my $sth = $conn->prepare(qq{insert into invoices_statuses
values(?
,?
)});
196 foreach (Bcd
::Constants
::InvoicesConstants
::LIST_INVOICES_STATES
){
197 $sth->bind_param(1, $_->[0]);
198 $sth->bind_param(2, $_->[1]);