Skip to content

Instantly share code, notes, and snippets.

@shardiwal
Created August 4, 2011 05:44
Show Gist options
  • Save shardiwal/1124568 to your computer and use it in GitHub Desktop.
Save shardiwal/1124568 to your computer and use it in GitHub Desktop.
doubt
package User;
use Moose;
use TypeConstraints qw/Gender DOB MySQLTimestamp/;
use DateTime;
use Date::Manip;
use DateTime::Format::MySQL;
has 'id' => (
is => 'rw',
isa => 'Int',
lazy_build => 1
);
has 'first_name' => (
is => 'rw',
isa => 'Str',
lazy_build => 1
);
has 'last_name' => (
is => 'rw',
isa => 'Str',
lazy_build => 1
);
has 'full_name' => (
is => 'ro',
isa => 'Str',
lazy => 1,
default => sub {
my ($self) = @_;
return $self->first_name . " " . $self->last_name;
}
);
has 'father_name' => (
is => 'rw',
isa => 'Str',
lazy_build => 1
);
has 'gender' => (
is => 'rw',
isa => Gender,
lazy_build => 1
);
has 'dateofbirth' => (
is => 'rw',
isa => DOB,
lazy_build => 1
);
has 'age' => (
is => 'ro',
isa => 'Int',
lazy_build => 1
);
has 'address' => (
is => 'rw',
isa => 'Maybe[Str]',
lazy_build => 1
);
has 'phone' => (
is => 'rw',
isa => 'Int',
lazy_build => 1
);
has 'registration_date' => (
is => 'rw',
isa => MySQLTimestamp,
lazy_build => 1,
);
has 'reg_date_human_format' => (
is => 'ro',
isa => 'Str',
lazy_build => 1
);
has 'notes' => (
is => 'ro',
isa => 'ArrayRef[User::Notes]',
lazy_build => 1,
traits => ['Array'],
handles => {
add_note => 'push',
get_notes => 'get',
remove_note => 'pop',
}
);
sub _build_get_notes {
my ( $self ) = @_;
my @notes = search->resultset->search('Notes')->search( {
user_id => $self->id
} )->all;
my @user_notes;
foreach my $note (@notes) {
push( @user_notes, User::Notes->new(
user => $self,
note_no => $note->appointment_no,
content => $note->content,
date => $note->date,
time => $note->time
) );
}
return \@user_notes;
}
sub _build_registration_date{
my ( $self ) = @_;
return UnixDate( ParseDate('now'), "%Y-%m-%d %H:%M:%S" );
}
sub _build_reg_date_readable_format{
my ( $self ) = @_;
my $timestamp = $self->registration_date;
my $today = DateTime->now;
$today->set_time_zone('Asia/Kolkata');
my $date1 = DateTime::Format::MySQL->parse_datetime($timestamp);
$date1->set_time_zone('Asia/Kolkata');
my $diff = $today->subtract_datetime($date1);
my ( $datestemp, $months, $days, $hours, $minutes, $seconds );
if ( $diff->{'months'} ) {
$months = $diff->{'months'} . " Months ";
}
if ( $diff->{'days'} ) {
$days = $diff->{'days'} . " Days ";
$days = '' if $days eq " Days ";
}
if ( !$days ) {
if ( $diff->{'minutes'} ) {
$hours = int( $diff->{'minutes'} / 60 ) . " hours ";
$hours = '' if $hours eq " hours ";
$minutes = int( $diff->{'minutes'} % 60 ) . " Minutes ";
}
if ( !$hours ) {
if ( $diff->{'seconds'} ) {
$seconds = $diff->{'seconds'} . " Seconds ";
$seconds = '' if $seconds eq " Seconds ";
}
}
}
$datestemp .= $months if $months;
$datestemp .= $days if $days;
$datestemp .= $hours if $hours;
$datestemp .= $seconds if $seconds;
$datestemp .= "ago";
return $datestemp;
}
sub _build_age {
my ($self) = @_;
return 0 if !$self->dateofbirth;
my @dob = split( /-/, $self->dateofbirth );
# Assuming $birth_month is 0..11
my $birth_day = $dob[0] if $dob[2] =~ /\d\d/;
my $birth_month = $dob[1] if $dob[2] =~ /\d\d/;
my $birth_year = $dob[2] if $dob[2] =~ /\d\d\d\d/;
return 0 if $birth_year && $birth_month;
my ( $day, $month, $year ) = (localtime)[ 3 .. 5 ];
$year += 1900;
my $age = $year - $birth_year;
$age--
unless sprintf( "%02d%02d", $month, $day ) >=
sprintf( "%02d%02d", $birth_month, $birth_day );
return $age;
}
sub _build_last_name {
my ($self) = @_;
return ' ';
}
sub store {
my ( $self ) = @_;
my $user = schema->resultset('User')->create( {
first_name => $self->first_name,
last_name => $self->last_name,
father_name => $self->father_name,
gender => $self->gender,
dateofbirth => $self->dateofbirth,
address => $self->address,
phone => $self->phone,
registration_date => $self->registration_date
} );
return $user ? $user : undef;
}
sub remove {
my ( $self ) = @_;
my $user = schema->resultset('User')->search( {
user_id => $self->id
} )->delete;
return $user ? $user : undef;
}
sub get {
my ( $self ) = @_;
return undef if ! $self->id;
my $user = schema->resultset('User')->search( {
user_id => $self->id
} )->single;
if ( $user ) {
return User->new(
id => $user->patient_id,
first_name => $user->first_name,
last_name => $user->last_name,
father_name => $user->father_name,
gender => $user->gender,
dateofbirth => $user->dateofbirth,
address => $user->address,
phone => $user->phone,
registration_date => $user->registration_date,
);
}
}
no Moose;
__PACKAGE__->meta->make_immutable;
1; # End of SoniHealthSolution::Patient;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment