Now I have the CreateAdCommand. It seems working... but it is not tied to the web...
[breadcrumbs.git] / src / lib / Bcd / Data / WebSite.pm
blob8dcae52caa6872100b550e7bccb3c65c34f65dd7
1 package Bcd::Data::WebSite;
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;
25 use Data::Dumper;
27 use Bcd::Constants::it::TransactionsTemplates;
29 use constant {
30 MINIMUM_AD_TAX => 0.00005,
31 MAXIMUM_AD_TAX => 0.00495,
34 #this module is responsible for the bookkeeping of the users accounts
35 #whenever they put ads in the system
37 sub get_price_for_this_ad{
38 my ($class, $stash, $id_user, $p_min, $p_max, $t_e, $t_c) = @_;
40 my ($price, $reachable_ants, $total_ants) =
41 $class->get_price_report_for_this_ad
42 ($stash, $id_user, $p_min, $p_max, $t_e, $t_c);
44 return $price;
47 sub get_price_report_for_this_ad{
48 my ($class, $stash, $id_user, $p_min, $p_max, $t_e, $t_c) = @_;
50 #ok, this function should simply define the price for an ad in
51 #this version of the system the price is given by p_min (p_max is
52 #not used).
54 my $t_max = ($t_c > $t_e) ? $t_c : $t_e;
56 #I should get the percentage of ants which can pay by credit...
58 #mmm, I get the nets of this user
60 #I should ask them to the trustnet
61 my $net = Bcd::Data::TrustsNet->get_nets_for_user($stash, $id_user);
63 #print Dumper($net);
65 my $reachable_ants = 0;
67 #let's get the percentage of ants which can pay by credit
68 foreach (keys(%{$net})){
69 if ($net->{$_}->[0] > $t_max){
70 $reachable_ants ++;
73 my $total_ants = scalar(keys(%{$net}));
74 my $percentage = $reachable_ants / $total_ants;;
76 #now I compute the price... the price is in tao, I don't use the
77 #big* library
78 my $price = $p_min * ( MINIMUM_AD_TAX + MAXIMUM_AD_TAX * ( 1 - ($percentage * $percentage)));
80 #my $price_p = $price / $p_min * 100;
81 #print "reach = $reachable_ants, perc = $percentage, price = $p_min tax = $price %p = $price_p \n";
83 return ($price, $reachable_ants, $total_ants);
86 #this function should simply debit the user account
87 sub charge_user_for_this_ad{
88 my ($class, $stash, $user, $ant_nest, $p_min, $p_max, $t_e, $t_c) = @_;
90 #firs of all I compute the price for this ad
92 my $price = $class->get_price_for_this_ad($stash, $user, $p_min, $p_max, $t_e, $t_c);
94 #ok, now I get the accounts
95 my ($us_act_e, $us_act_t) = Bcd::Data::Users->get_users_accounts($stash, $user);
97 #ok, now I get the partial income euro for the ant nest...
98 my $par_tao_ads_incomes = Bcd::Data::AntNests->get_tao_ads_partial_income_id($stash, $ant_nest);
100 #ok, now I can have a simple transaction...
101 my $list_debit_accounts = [$us_act_t];
102 my $list_debit_amounts = [$price];
104 my $list_credit_accounts= [$par_tao_ads_incomes];
105 my $list_credit_amounts = [$price];
108 my ($debits, $credits) =
109 Bcd::Data::Accounting::Transaction->add_new_transaction_in_db
110 ($list_debit_accounts, $list_debit_amounts,
111 $list_credit_accounts, $list_credit_amounts,
112 Bcd::Constants::it::TransactionsTemplates::USER_PUT_AD, $stash);
114 my $new_total_tao = $debits->[0];
116 return $new_total_tao;