Files
dojo/lib/Dojo/Model/Vdgproc.pm

212 lines
6.1 KiB
Perl
Raw Normal View History

2020-01-15 23:42:58 -06:00
package Dojo::Model::Vdgproc; use Mojo::File 'path';
use Mojo::JSON qw(decode_json encode_json);
2020-01-20 14:49:59 -06:00
use Dojo::Support qw{ mtxt month_num2txt log dmph} ;
2020-01-15 23:42:58 -06:00
use Mojo::Base 'Mojolicious::Controller';
2020-01-20 14:49:59 -06:00
use Email::Valid;
use Data::Dumper;
2020-01-15 23:42:58 -06:00
use File::Basename;
use Encode qw(decode_utf8 encode_utf8);
use Text::Markdown qw{ markdown };
use DBI;
use Dojo::Conf;
sub new { bless {}, shift };
my $data_path = path('lib/Dojo/Model/Data')->make_path;
#just for fun
2020-01-20 14:49:59 -06:00
# Tienda de eventos ===========================================================
2020-01-15 23:42:58 -06:00
sub calendar_monthblock {
my $r = _read ($data_path->child("cal/q1Block.q")->slurp);
map { $_->{fecha}=month_num2txt($_->{month})." ".$_->{year} } @$r;
return $r;
}
sub calendar_events {
my $r = _read ($data_path->child("cal/q3Event.q")->slurp);
foreach (@$r) { $_->{fecha} = mtxt( $_->{dini},$_->{mini},$_->{dfin},$_->{mfin}) }
return $r;
}
sub event{
my ($c,$id)=@_;
my $r = shift @{ _read ($data_path->child("event/qEvent.q")->slurp,$id)};
$r->{date} = mtxt( $r->{dini},$r->{mini},$r->{dend},$r->{mend});
return $r;
}
sub spay {
my ($c,$class,$id) = @_;
2020-01-20 14:49:59 -06:00
return _read ($data_path->child("spay/qIdStore.q")->slurp,$id)->[0];
2020-01-15 23:42:58 -06:00
}
sub store_id {
my ($c,$class,$id) = @_;
2020-01-20 14:49:59 -06:00
my $r = _read ($data_path->child("store/qEstore.q")->slurp,$id)->[0];
2020-01-15 23:42:58 -06:00
$r->{fecha} = mtxt( $r->{dini},$r->{mini},$r->{dfin},$r->{mfin});
return $r;
}
2020-01-20 14:49:59 -06:00
# Stripe payment process ==============================================================
# ======================
# 4000004840000008 3, 6, 9, 12, and 18 month installment plans available
# "*p4000004840000008
# 4242424242424242 No installment plans available.
# ======================
2020-01-15 23:42:58 -06:00
sub intentCreate{
my ($c,$id) = @_;
2020-01-20 14:49:59 -06:00
return _read ($data_path->child("spay/qIntentCreate.q")->slurp,$id)->[0];
2020-01-15 23:42:58 -06:00
}
2020-01-20 14:49:59 -06:00
2020-01-15 23:42:58 -06:00
sub intentConfirm{
2020-01-20 14:49:59 -06:00
my ($c,$data,$r) = @_;
# cliente_nombre,cliente_correo,cliente_comentario,tienda_evento_id,talla_id,cantidad(1),monto,etapa_id(4),entrega(3),pago_clave,plazo,fecha_pago
my $mq = $data->{'mq'};
my $students = $data->{'students'};
my $plan = 0;
if (defined $data->{'selected_plan'}){
$plan = $data->{'selected_plan'}->{'count'};
}
for (1..$mq){
_write (
$data_path->child("/spay/qIntentConfirm.q")->slurp,
$$students[$_ -1][0],
$$students[$_ -1][1],
$$students[$_ -1][2],
$data->{'tid'},
$$students[$_ -1][3],
$data->{'mq'},
$r->{'amount'},
4,
3,
$r->{'id'},
$plan
);
} # falta plazo
return ;
}
# Check payment user info ==============
# data:
# ===============
#'selected_plan' => { 'type' => 'fixed_count', 'count' => 6, 'interval' => 'month' },
#'mq' => '2', # cantidad de alumnas
#'req' => 'check',
#'payment_intent_id' => 'pi_1G1luSFBHoXN2vhnkl3PIiL1',
#'tid' => 80, #tienda id
#'students' => [ [ 'User', 'me@me.com', 'obs', 'M' ], [ 'usr2', 'mail2', 'obs', 'G' ], undef, undef, undef, undef, undef, undef, undef, undef ] #arr(10)
# ===============
sub intentSanity{
my ($c,$d)=@_;
if (defined $d->{'selected_plan'} ){
return -1 unless defined ($d->{'selected_plan'}{'type'});
return -1 unless defined ($d->{'selected_plan'}{'count'});
return -1 unless defined ($d->{'selected_plan'}{'interval'});
}
return -1 unless defined ($d->{'tid'}) && $d->{'tid'} =~ /^\d+$/ ;
return -1 unless defined ($d->{'req'}) && $d->{'req'} eq 'check';
return -1 unless defined ($d->{'payment_intent_id'});
return userDataSanity($c,$d) ;
}
sub userDataSanity{
my ($c,$d) = @_;
return -1 unless (
defined ($d->{'students'}) && # (nombre,mail,observaciones, talla)
defined ($d->{'mq'}) &&
$d->{'mq'} =~/^\d+$/ &&
$d->{'mq'} < 10
);
my %h=qw(n 0 CH 1 M 2 G 3);
my $mq = $d->{'mq'};
my $students = $d->{'students'};
for (1..$mq){
return $_ unless defined( $$students[$_ -1][0] );
return $_ unless defined( $$students[$_ -1][1] ) &&
is_mail($$students[$_ -1][1] );
$$students[$_ -1][3] = $h{$$students[$_ -1][3] // 'n'};
}
return 0;
}
sub is_mail{
return Email::Valid->address(shift);
2020-01-15 23:42:58 -06:00
}
#==============================================================================
# admin evets
sub event_del { return _write("delete from evento where id = ?;",pop);}
sub course_del{ return _write("delete from curso where id = ?;",pop); }
sub place_del { return _write("delete from lugar where id = ?;",pop); }
sub event_up{
my ($self,@bind)=@_;
return _write($data_path->child("/admin/event/add/qEup.q")->slurp,@bind);
}
sub course_up{
my ($self,@bind)=@_;
return _write($data_path->child("/admin/event/add/qCup.q")->slurp,@bind);
}
sub place_up{
my ($self,@bind)=@_;
return _write($data_path->child("/admin/event/add/qPup.q")->slurp,@bind);
}
sub event_ch{
my ($self,@bind)=@_;
return _write($data_path->child("/admin/event/change/qEup.q")->slurp,@bind);
}
sub course_ch{
my ($self,@bind)=@_;
return _write($data_path->child("/admin/event/change/qCup.q")->slurp,@bind);
}
sub place_ch{
my ($self,@bind)=@_;
return _write($data_path->child("/admin/event/change/qPup.q")->slurp,@bind);
}
#==============================================================================
2020-01-20 14:49:59 -06:00
# lower level read =====================================
2020-01-15 23:42:58 -06:00
sub _read{
my ($q,@bind)=@_;
my @empty;
my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::VDGPROCDB.":".Dojo::Conf::VDGPROCDB_H,Dojo::Conf::VDGPROCDB_UR,Dojo::Conf::VDGPROCDB_URP, {mysql_enable_utf8 => 1});
return \@empty unless($dbh);
my $h=$dbh->selectall_arrayref($q,{ Slice => {} },@bind)//"error";
$dbh->disconnect();
#((col1=>d1,col2=>d1),(col1=>d2,col2=>d2))
return $h;
}
sub _write{
my ($q,@bind)=@_;
my (@empty);
my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::VDGPROCDB.":".Dojo::Conf::VDGPROCDB_H,Dojo::Conf::VDGPROCDB_UW,Dojo::Conf::VDGPROCDB_UWP,{mysql_enable_utf8 => 1} );
return 0 unless($dbh);
my $h=$dbh->do($q,{ Slice => {} },@bind)//"error";
$dbh->disconnect();
log("db write: $h");
return $h;
}
sub load_md{
return "" unless
my $text = path(shift)->slurp;
return markdown( decode_utf8($text) );
}
sub load_txt{
return "" unless
my $text = path(shift)->slurp;
return decode_utf8($text);
}
1;