+ my %queued = split / /, $self->{queued};
+
+ if ($self->{formname} =~ /(check|receipt)/) {
+ # this is a check or receipt, add one entry for each lineitem
+ my ($accno) = split /--/, $self->{account};
+ $query = qq|INSERT INTO status (trans_id, printed, spoolfile, formname,
+ chart_id) VALUES (?, '$printed',|
+ .$dbh->quote($queued{$self->{formname}}).qq|, |
+ .$dbh->quote($self->{formname}).qq|,
+ (SELECT id FROM chart WHERE accno = |
+ .$dbh->quote($accno).qq|))|;
+ $sth = $dbh->prepare($query) || $self->dberror($query);
+
+ for $i (1 .. $self->{rowcount}) {
+ if ($self->{"checked_$i"}) {
+ $sth->execute($self->{"id_$i"}) || $self->dberror($query);
+ $sth->finish;
+ }
+ }
+ } else {
+ $query = qq|INSERT INTO status (trans_id, printed, emailed,
+ spoolfile, formname)
+ VALUES ($self->{id}, '$printed', '$emailed', |
+ .$dbh->quote($queued{$self->{formname}}).qq|, |
+ .$dbh->quote($self->{formname}).qq|)|;
+ $dbh->do($query) || $self->dberror($query);
+ }
+
+ $dbh->commit;
+ $dbh->disconnect;
+
+}
+
+
+sub save_status {
+ my ($self, $dbh) = @_;
+
+ my ($query, $printed, $emailed);
+
+ my $formnames = $self->{printed};
+ my $emailforms = $self->{emailed};
+
+ my $query = qq|DELETE FROM status
+ WHERE formname = '$self->{formname}'
+ AND trans_id = $self->{id}|;
+ $dbh->do($query) || $self->dberror($query);
+
+ if ($self->{queued}) {
+ $query = qq|DELETE FROM status
+ WHERE spoolfile IS NOT NULL
+ AND trans_id = $self->{id}|;
+ $dbh->do($query) || $self->dberror($query);
+
+ my %queued = split / /, $self->{queued};
+
+ foreach my $formname (keys %queued) {
+ $printed = ($self->{printed} =~ /$self->{formname}/) ? "1" : "0";
+ $emailed = ($self->{emailed} =~ /$self->{formname}/) ? "1" : "0";
+
+ $query = qq|INSERT INTO status (trans_id, printed, emailed,
+ spoolfile, formname)
+ VALUES ($self->{id}, '$printed', '$emailed',
+ '$queued{$formname}', '$formname')|;
+ $dbh->do($query) || $self->dberror($query);
+ $formnames =~ s/$formname//;
+ $emailforms =~ s/$formname//;
+
+ }
+ }
+
+ # save printed, emailed info
+ $formnames =~ s/^ +//g;
+ $emailforms =~ s/^ +//g;
+
+ my %status = ();
+ map { $status{$_}{printed} = 1 } split / +/, $formnames;
+ map { $status{$_}{emailed} = 1 } split / +/, $emailforms;
+
+ foreach my $formname (keys %status) {
+ $printed = ($formnames =~ /$self->{formname}/) ? "1" : "0";
+ $emailed = ($emailforms =~ /$self->{formname}/) ? "1" : "0";
+
+ $query = qq|INSERT INTO status (trans_id, printed, emailed, formname)
+ VALUES ($self->{id}, '$printed', '$emailed', '$formname')|;
+ $dbh->do($query) || $self->dberror($query);
+ }
+
+}
+
+
+sub save_intnotes {
+ my ($self, $myconfig, $table) = @_;
+
+ # no id return
+ return unless $self->{id};
+
+ my $dbh = $self->dbconnect($myconfig);
+
+ my $query = qq|UPDATE $table SET
+ intnotes = |.$dbh->quote($self->{intnotes}).qq|
+ WHERE id = $self->{id}|;
+ $dbh->do($query) || $self->dberror($query);
+
+ $dbh->disconnect;
+
+}
+
+
+sub update_defaults {
+ my ($self, $myconfig, $fld, $dbh) = @_;
+
+ my $closedb;
+
+ if (! defined $dbh) {
+ $dbh = $self->dbconnect_noauto($myconfig);
+ $closedb = 1;
+ }
+
+ my $query = qq|SELECT $fld FROM defaults FOR UPDATE|;
+ ($_) = $dbh->selectrow_array($query);
+
+ $_ = "0" unless $_;
+
+ # check for and replace
+ # <%DATE%>, <%YYMMDD%> or variations of
+ # <%NAME 1 1 3%>, <%BUSINESS%>, <%BUSINESS 10%>, <%CURR...%>
+ # <%DESCRIPTION 1 1 3%>, <%ITEM 1 1 3%>, <%PARTSGROUP 1 1 3%> only for parts
+ # <%PHONE%> for customer and vendors
+
+ my $num = $_;
+ $num =~ s/(<%.*?%>)//g;
+ ($num) = $num =~ /(\d+)/;
+ if (defined $num) {
+ my $incnum;
+ # if we have leading zeros check how long it is
+ if ($num =~ /^0/) {
+ my $l = length $num;
+ $incnum = $num + 1;
+ $l -= length $incnum;
+
+ # pad it out with zeros
+ my $padzero = "0" x $l;
+ $incnum = ("0" x $l) . $incnum;
+ } else {
+ $incnum = $num + 1;
+ }
+
+ s/$num/$incnum/;
+ }
+
+ my $dbvar = $_;
+ my $var = $_;
+ my $str;
+ my $param;
+
+ if (/<%/) {
+ while (/<%/) {
+ s/<%.*?%>//;
+ last unless $&;
+ $param = $&;
+ $str = "";
+
+ if ($param =~ /<%date%>/i) {
+ $str = ($self->split_date($myconfig->{dateformat}, $self->{transdate}))[0];
+ $var =~ s/$param/$str/;
+ }
+
+ if ($param =~ /<%(name|business|description|item|partsgroup|phone|custom)/i) {
+ my $fld = lc $&;
+ $fld =~ s/<%//;
+ if ($fld =~ /name/) {
+ if ($self->{type}) {
+ $fld = $self->{vc};
+ }
+ }
+
+ my $p = $param;
+ $p =~ s/(<|>|%)//g;
+ my @p = split / /, $p;
+ my @n = split / /, uc $self->{$fld};
+ if ($#p > 0) {
+ for (my $i = 1; $i <= $#p; $i++) {
+ $str .= substr($n[$i-1], 0, $p[$i]);
+ }
+ } else {
+ ($str) = split /--/, $self->{$fld};
+ }
+ $var =~ s/$param/$str/;
+
+ $var =~ s/\W//g if $fld eq 'phone';
+ }
+
+ if ($param =~ /<%(yy|mm|dd)/i) {
+ my $p = $param;
+ $p =~ s/(<|>|%)//g;
+ my $spc = $p;
+ $spc =~ s/\w//g;
+ $spc = substr($spc, 0, 1);
+ my %d = ( yy => 1, mm => 2, dd => 3 );
+ my @p = ();
+
+ my @a = $self->split_date($myconfig->{dateformat}, $self->{transdate});
+ map { push @p, $a[$d{$_}] if ($p =~ /$_/) } sort keys %d;
+ $str = join $spc, @p;
+
+ $var =~ s/$param/$str/;
+ }
+
+ if ($param =~ /<%curr/i) {
+ $var =~ s/$param/$self->{currency}/;
+ }
+
+ }
+ }
+
+ $query = qq|UPDATE defaults
+ SET $fld = '$dbvar'|;
+ $dbh->do($query) || $form->dberror($query);
+
+ if ($closedb) {
+ $dbh->commit;
+ $dbh->disconnect;
+ }
+
+ $var;
+
+}
+
+
+sub split_date {
+ my ($self, $dateformat, $date) = @_;
+
+ my @d = localtime;
+ my $mm;
+ my $dd;
+ my $yy;
+ my $rv;
+
+ if (! $date) {
+ $dd = $d[3];
+ $mm = $d[4]++;
+ $yy = substr($d[5],-2);
+ $mm *= 1;
+ $dd *= 1;
+ $mm = "0$mm" if $mm < 10;
+ $dd = "0$dd" if $dd < 10;
+ }
+
+ if ($dateformat =~ /^yy/) {
+ if ($date) {
+ if ($date =~ /\D/) {
+ ($yy, $mm, $dd) = split /\D/, $date;
+ $mm *= 1;
+ $dd *= 1;
+ $mm = "0$mm" if $mm < 10;
+ $dd = "0$dd" if $dd < 10;
+ $yy = substr($yy, -2);
+ $rv = "$yy$mm$dd";
+ } else {
+ $rv = $date;
+ }
+ } else {
+ $rv = "$yy$mm$dd";
+ }
+ }
+
+ if ($dateformat =~ /^mm/) {
+ if ($date) {
+ if ($date =~ /\D/) {
+ ($mm, $dd, $yy) = split /\D/, $date if $date;
+ $mm *= 1;
+ $dd *= 1;
+ $mm = "0$mm" if $mm < 10;
+ $dd = "0$dd" if $dd < 10;
+ $yy = substr($yy, -2);
+ $rv = "$mm$dd$yy";
+ } else {
+ $rv = $date;
+ }
+ } else {
+ $rv = "$mm$dd$yy";
+ }
+ }
+
+ if ($dateformat =~ /^dd/) {
+ if ($date) {
+ if ($date =~ /\D/) {
+ ($dd, $mm, $yy) = split /\D/, $date if $date;
+ $mm *= 1;
+ $dd *= 1;
+ $mm = "0$mm" if $mm < 10;
+ $dd = "0$dd" if $dd < 10;
+ $yy = substr($yy, -2);
+ $rv = "$dd$mm$yy";
+ } else {
+ $rv = $date;
+ }
+ } else {
+ $rv = "$dd$mm$yy";
+ }
+ }
+
+ ($rv, $yy, $mm, $dd);
+
+}
+
+
+sub from_to {
+ my ($self, $yy, $mm, $interval) = @_;
+
+ use Time::Local;
+
+ my @t;
+ my $dd = 1;
+ my $fromdate = "$yy${mm}01";
+ my $bd = 1;
+
+ if (defined $interval) {
+ if ($interval == 12) {
+ $yy++ if $mm > 1;
+ } else {
+ if (($mm += $interval) > 12) {
+ $mm -= 12;
+ $yy++ if $mm > 1;
+ }
+ if ($interval == 0) {
+ @t = localtime(time);
+ $dd = $t[3];
+ $mm = $t[4] + 1;
+ $yy = $t[5] + 1900;
+ $bd = 0;
+ }
+ }
+ } else {
+ if ($mm++ > 12) {
+ $mm -= 12;
+ $yy++;
+ }
+ }
+
+ $mm--;
+ @t = localtime(timelocal(0,0,0,$dd,$mm,$yy) - $bd);
+
+ $t[4]++;
+ $t[4] = substr("0$t[4]",-2);
+ $t[3] = substr("0$t[3]",-2);
+
+ ($fromdate, "$yy$t[4]$t[3]");
+
+}
+
+
+sub audittrail {
+ my ($self, $dbh, $myconfig, $audittrail) = @_;
+
+# table, $reference, $formname, $action, $id, $transdate) = @_;
+
+ my $query;
+ my $rv;
+
+ # if we have an id add audittrail, otherwise get a new timestamp
+
+ if ($audittrail->{id}) {
+ $dbh = $self->dbconnect($myconfig) if $myconfig;
+
+ $query = qq|SELECT audittrail FROM defaults|;
+
+ if ($dbh->selectrow_array($query)) {
+ my ($null, $employee_id) = $self->get_employee($dbh);
+
+ if ($self->{audittrail} && !$myconfig) {
+ chop $self->{audittrail};
+
+ my @a = split /\|/, $self->{audittrail};
+ my %newtrail = ();
+ my $key;
+ my $i;
+ my @flds = qw(tablename reference formname action transdate);
+
+ # put into hash and remove dups
+ while (@a) {
+ $key = "$a[2]$a[3]";
+ $i = 0;
+ $newtrail{$key} = { map { $_ => $a[$i++] } @flds };
+ splice @a, 0, 5;
+ }
+
+ $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
+ formname, action, employee_id, transdate)
+ VALUES ($audittrail->{id}, ?, ?,
+ ?, ?, $employee_id, ?)|;
+ my $sth = $dbh->prepare($query) || $self->dberror($query);
+
+ foreach $key (sort { $newtrail{$a}{transdate} cmp $newtrail{$b}{transdate} } keys %newtrail) {
+ $i = 1;
+ map { $sth->bind_param($i++, $newtrail{$key}{$_}) } @flds;
+
+ $sth->execute || $self->dberror;
+ $sth->finish;
+ }
+ }
+
+
+ if ($audittrail->{transdate}) {
+ $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
+ formname, action, employee_id, transdate) VALUES (
+ $audittrail->{id}, '$audittrail->{tablename}', |
+ .$dbh->quote($audittrail->{reference}).qq|',
+ '$audittrail->{formname}', '$audittrail->{action}',
+ $employee_id, '$audittrail->{transdate}')|;
+ } else {
+ $query = qq|INSERT INTO audittrail (trans_id, tablename, reference,
+ formname, action, employee_id) VALUES ($audittrail->{id},
+ '$audittrail->{tablename}', |
+ .$dbh->quote($audittrail->{reference}).qq|,
+ '$audittrail->{formname}', '$audittrail->{action}',
+ $employee_id)|;
+ }
+ $dbh->do($query);
+ }
+ } else {
+ $dbh = $self->dbconnect($myconfig);
+
+ $query = qq|SELECT current_timestamp FROM defaults|;
+ my ($timestamp) = $dbh->selectrow_array($query);
+
+ $rv = "$audittrail->{tablename}|$audittrail->{reference}|$audittrail->{formname}|$audittrail->{action}|$timestamp|";
+ }
+
+ $dbh->disconnect if $myconfig;
+
+ $rv;
+
+}
+
+
+