From 86de4298bfdb3c5b66b082f5f0ccb31355d2b0ba Mon Sep 17 00:00:00 2001 From: Pasqualino Ferrentino Date: Fri, 7 Sep 2007 10:55:41 +0200 Subject: [PATCH] first version to have the credits and debits --- src/lib/Bcd/Data/CreditsAndDebits.pm | 112 +++++++++++++++++++++++++++++++++++ src/t/TestCreditsAndDebits.pm | 95 +++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 src/lib/Bcd/Data/CreditsAndDebits.pm create mode 100644 src/t/TestCreditsAndDebits.pm diff --git a/src/lib/Bcd/Data/CreditsAndDebits.pm b/src/lib/Bcd/Data/CreditsAndDebits.pm new file mode 100644 index 0000000..d0e0d13 --- /dev/null +++ b/src/lib/Bcd/Data/CreditsAndDebits.pm @@ -0,0 +1,112 @@ +package Bcd::Data::CreditsAndDebits; + +# This file is part of the breadcrumbs daemon (bcd). +# Copyright (C) 2007 Pasqualino Ferrentino + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +# Contact: lino.ferrentino@yahoo.it (in Italian, English or German). + +use strict; +use warnings; + +use Data::Dumper; + +use constant PI => 4 * atan2(1, 1); +use constant CREDIT_SCORE_COEFF => - 200 / PI; + +use constant { + + GET_TOTAL_ANT_CREDIT => + qq{SELECT sum(amount) as sum FROM credits_and_debits WHERE id_creditor = ?}, + + GET_TOTAL_ANT_DEBIT => + qq{SELECT sum(amount) as sum FROM credits_and_debits WHERE id_debitor = ?}, + + GET_ANT_FROZEN_DEBITS => + qq{SELECT amount FROM frozen_credits WHERE id_debitor = ? }, + + + MINIMUM_DEBIT_CREDIT => 50_000, + + +}; + +#this method should simply get the net credit of an ant +#that is the sum (credits - debits - frozen debits) +sub get_net_ant_credit{ + my ($class, $stash, $id_user) = @_; + + my $row = $stash->select_one_row_arr(GET_TOTAL_ANT_CREDIT, $id_user); + my $credit = (defined($row->[0])) ? $row->[0] : 0; + + $row = $stash->select_one_row_arr(GET_TOTAL_ANT_DEBIT, $id_user); + my $debit = (defined($row->[0])) ? $row->[0] : 0; + + #ok, now the frozen debits + $row = $stash->select_one_row_arr(GET_ANT_FROZEN_DEBITS, $id_user); + my $frozen_debit = (defined($row)) ? $row->[0] : 0; + + return ($credit - $debit - $frozen_debit); +} + +sub get_ant_credit_score{ + my ($class, $stash, $id_user) = @_; + +} + +sub get_ant_credit_score_if_it_emits_this_cheque{ + my ($class, $stash, $id_user, $amount_cheque) = @_; + +} + +#this is the function for the credit score... It is a pure function, +#no side effects. +#precondition $credit >= 0, $debit >= 0 +sub credit_score{ + my ($credit, $debit) = @_; + + if ( + ($credit < MINIMUM_DEBIT_CREDIT) + and + ($debit < MINIMUM_DEBIT_CREDIT) + ){ + #the score has a certain hysteresis + return 100; + } + + #now... if onw of them is null, the credit score is defined + if ($credit == 0){ + return -100; + } + + if ($debit == 0){ + return 100; + } + + #ok, I can compute the formula + my $rate = $debit / $credit; + + my $arg = ($rate-1)/sqrt($rate); + my $score = CREDIT_SCORE_COEFF * atan2($arg, 1); + + return $score; +} + + + + +1; diff --git a/src/t/TestCreditsAndDebits.pm b/src/t/TestCreditsAndDebits.pm new file mode 100644 index 0000000..44405c3 --- /dev/null +++ b/src/t/TestCreditsAndDebits.pm @@ -0,0 +1,95 @@ +package t::TestCreditsAndDebits; + +# This file is part of the breadcrumbs daemon (bcd). +# Copyright (C) 2007 Pasqualino Ferrentino + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + +# Contact: lino.ferrentino@yahoo.it (in Italian, English or German). + +use strict; +use warnings; + + +use t::TestCommonDb; +our @ISA = qw(t::TestCommonDb); + + +use Data::Dumper; +use Bcd::Data::CreditsAndDebits; + + +sub test_initial_credits{ + my $self = shift; + my $stash= $self->{stash}; + + + my $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16); + $self->assert_num_equals(0, $net_credit); + + #ok, now I put some credits + + my $sql = qq{INSERT INTO credits_and_debits VALUES(12, 16, 1001)}; + $stash->prep_exec($sql); + + $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16); + $self->assert_num_equals(-1001, $net_credit); + + #a frozen debit + $sql = qq{INSERT INTO frozen_credits VALUES(16, 999)}; + $stash->prep_exec($sql); + + $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16); + $self->assert_num_equals(-2000, $net_credit); + + #a credit + $sql = qq{INSERT INTO credits_and_debits VALUES(16, 9, 1500)}; + $stash->prep_exec($sql); + + $net_credit = Bcd::Data::CreditsAndDebits->get_net_ant_credit($stash, 16); + $self->assert_num_equals(-500, $net_credit); +} + +sub test_credit_score{ + my $self = shift; + my $stash= $self->{stash}; + + + my $score; + $score = Bcd::Data::CreditsAndDebits::credit_score(32000, 10000); + $self->assert_num_equals(100, $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(32000, 0); + $self->assert_num_equals(100, $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(82000, 0); + $self->assert_num_equals(100, $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(0, 82000); + $self->assert_num_equals(-100, $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(1, 82000); + $self->assert_num_equals("-99.7776809245822", $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(82000, 1); + $self->assert_num_equals("99.7776809245822", $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(82000, 82000); + $self->assert_num_equals("0", $score); + + $score = Bcd::Data::CreditsAndDebits::credit_score(23000, 82000); + $self->assert_num_equals("-59.6048313960983", $score); +} -- 2.11.4.GIT