package Dojo::Model::Vdgproc; use Mojo::File 'path'; use Mojo::JSON qw(decode_json encode_json); use Dojo::Support qw{ mtxt month_num2txt log dmph} ; use Mojo::Base 'Mojolicious::Controller'; use Email::Valid; use Data::Dumper; 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 # Tienda de eventos =========================================================== 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) = @_; my $r = shift @{ _read ($data_path->child("spay/qIdStore.q")->slurp,$id)}; $r->{fecha} = mtxt( $r->{dini},$r->{mini},$r->{dfin},$r->{mfin}); return $r; } # Stripe payment process ============================================================== # ====================== # 4000004840000008 3, 6, 9, 12, and 18 month installment plans available # "*p4000004840000008 # 4242424242424242 No installment plans available. # ====================== sub intentCreate{ my ($c,$id) = @_; return _read ($data_path->child("spay/qIntentCreate.q")->slurp,$id)->[0]; } sub intentConfirm{ 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 ; } sub notify{ my ($c,$d,$r) = @_; my $t =shift @{ _read ($data_path->child("spay/qNotify.q")->slurp,$d->{'tid'})}; my @ra; my %h=qw/0 na 1 ch 2 m 3 g/; my $plazo= defined( $d->{'selected_plan'})? $d->{'selected_plan'}->{'count'} : "0"; my $students = $d->{'students'}; my $basetxta = "$t->{'cnombre'} clave de pago stripe: $d->{'payment_intent_id'} plazo: $plazo meses cantidad: $t->{'cantidad'} "; my $basetxtu = $t->{'cnombre'}//""." ".$t->{'subnombre'}//""." "; foreach(1 .. $d->{'mq'}) { push (@ra, [" $basetxtu $$students[$_ -1][0] $t->{'mmail'} tu clave de pago es la sigiente. Tenla a la mano para cualquier aclaración ========================== $d->{'payment_intent_id'} =========================== ", " Inscripción ", $$students[$_ -1][1]]); $basetxta .= " Nombre: $$students[$_ -1][0] Correo: $$students[$_ -1][1] Telefono:$$students[$_ -1][0] Playera:$h{$$students[$_ -1][3]} Comentario:$$students[$_ -1][2] ===================== "; } push (@ra, [$basetxta,"Inscripciones",'david@chaos.foundation']); return @ra; } # 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); } #============================================================================== # admin evets sub event_del { return _write("delete from curso_tienda where id = ?;",pop);} sub course_del{ return _write("delete from curso where id = ?;",pop); } sub place_del { return _write("delete from curso_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); } #============================================================================== sub courses{ return _read("select nombre as name, id from curso;")} sub course { return shift @{_read($data_path->child("/admin/event/json/qCourse.q")->slurp,pop)}} sub places { return _read("select nombre as name, id from curso_lugar;")} sub place { return shift @{_read($data_path->child("/admin/event/json/qPlace.q")->slurp,pop)}} sub events { return _read($data_path->child("/admin/event/qEprev.q")->slurp)} sub eventa { return shift @{_read($data_path->child("/admin/event/json/qEvent.q")->slurp,pop)}} sub eimgList{ my $dp = path('public/proc/cal/img')->make_path; return [ map{ $_->basename} grep{/(jpg|png)$/i}@{$dp->list} ]; } # lower level read ===================================== 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;