# Selima Website Content Management System # AcctTrx.pm: The accounting transaction form. # Copyright (c) 2007-2018 imacat. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Author: imacat # First written: 2007-09-20 package Selima::Form::AcctTrx; use 5.008; use strict; use warnings; use base qw(Selima::Form); use Selima::Accounting; use Selima::AddGet; use Selima::CommText; use Selima::DataVars qw($DBH :requri :scptconf); use Selima::FormFunc; use Selima::Format; use Selima::HTTP; use Selima::MarkAbbr; use Selima::ShortCut; # new: Initialize the HTML form table displayer sub new : method { local ($_, %_); my ($class, $status, $args, $self); ($class, $status, $args) = @_; $args = {} if !defined $args; # $args must be a hash reference http_500 "type of argument 2 must be a hash reference" if ref($args) ne "HASH"; $$args{"type"} = form_type if !exists $$args{"type"}; $$args{"table"} = "accttrx" if !exists $$args{"table"}; $$args{"deltext"} = C_("Delete this accounting transaction") if !exists $$args{"deltext"}; # The hidden columns if (!exists $$args{"summary"}) { # A form to create a new item if ($$args{"type"} eq "new") { $$args{"summary"} = C_("This table provides you a form to add a new accounting transaction."); # A form to edit a current item } elsif ($$args{"type"} eq "cur") { $$args{"summary"} = C_("This table provides you a form to edit a current accounting transaction."); # A form to delete a current item } elsif ($$args{"type"} eq "del") { $$args{"summary"} = C_("This table provides you a form to delete an accounting transaction."); } } if (!exists $$args{"cols"}) { # A form to create a new item if ($$args{"type"} eq "new") { $$args{"cols"} = [qw(date ord recs note)]; # A form to edit a current item # A form to delete a current item } elsif ($$args{"type"} eq "cur" || $$args{"type"} eq "del") { $$args{"cols"} = [qw(sn date ord recs note created createdby updated updatedby)]; } } $self = $class->SUPER::new($status, $args); ${$self->{"maxlens"}}{"ord"} = 2; # Set the subform type if ($$args{"type"} eq "new") { $_ = curform; $self->{"subtype"} = $_->param("formsub"); } elsif ($$args{"type"} eq "cur") { $self->{"subtype"} = $self->{"form"}->param("formsub"); } elsif ($$args{"type"} eq "del") { $self->{"subtype"} = $self->{"cur"}->param("formsub"); } # Only expense, income or trans are allowed http_500 "invalid form sub-type: " . $self->{"type"} if $self->{"subtype"} !~ /^(?:expense|income|trans)$/; if ($self->{"subtype"} eq "expense" || $self->{"subtype"} eq "income") { $self->{"colspan"} = 3; } else { $self->{"colspan"} = 6; } $self->{"hidcols"} = [] if !exists $self->{"hidcols"}; push @{$self->{"hidcols"}}, { "name" => "formsub", "value" => $self->{"subtype"}, }; if ($self->{"type"} eq "cur" && $self->{"subtype"} ne "trans") { $self->{"header_buttons"} = [ { "name" => undef, "value" => h(C_("Submit")) }, { "name" => "confirm", "value" => h(C_("Save")) }, { "name" => "del", "value" => h($self->{"deltext"}) }, { "name" => "cnvttrans", "value" => h(C_("Convert to a transfer transaction")) }, ]; $self->{"footer_buttons"} = [ { "name" => undef, "value" => h(C_("Submit")) }, { "name" => "confirm", "value" => h(C_("Save")) }, { "name" => "del", "value" => h($self->{"deltext"}) }, { "name" => "cnvttrans", "value" => h(C_("Convert to a transfer transaction")) }, ]; } # A form to create a new item if ($self->{"type"} eq "new") { if ($self->{"subtype"} eq "expense") { $self->{"title"} = C_("Add a New Cache Expense Transaction"); } elsif ($self->{"subtype"} eq "income") { $self->{"title"} = C_("Add a New Cache Income Transaction"); } elsif ($self->{"subtype"} eq "trans") { $self->{"title"} = C_("Add a New Transfer Transaction"); } # A form to edit a current item } elsif ($self->{"type"} eq "cur") { if ($self->{"subtype"} eq "expense") { $self->{"title"} = C_("Edit a Current Cache Expense Transaction"); } elsif ($self->{"subtype"} eq "income") { $self->{"title"} = C_("Edit a Current Cache Income Transaction"); } elsif ($self->{"subtype"} eq "trans") { $self->{"title"} = C_("Edit a Current Transfer Transaction"); } # A form to delete a current item } elsif ($self->{"type"} eq "del") { if ($self->{"subtype"} eq "expense") { $self->{"title"} = C_("Delete a Cache Expense Transaction"); } elsif ($self->{"subtype"} eq "income") { $self->{"title"} = C_("Delete a Cache Income Transaction"); } elsif ($self->{"subtype"} eq "trans") { $self->{"title"} = C_("Delete a Transfer Transaction"); } } %_ = $DBH->col_lens("acctrec"); ${$self->{"maxlens"}}{"recsubj"} = $_{"subj"}; ${$self->{"maxlens"}}{"recsummary"} = $_{"summary"}; ${$self->{"maxlens"}}{"recamount"} = $_{"amount"}; return $self; } # _html_coltmpl_ro_loop_rec: Display a read-only record row column sub _html_coltmpl_ro_loop_rec : method { local ($_, %_); local ($_, %_); my ($self, $i, $current, $rowhdrs); my ($curlsubj, $curlsummary, $curlamount); my ($curbsubj, $curbsummary, $curbamount); ($self, $i) = @_; $current = $self->{"cur"}; $rowhdrs = "threcs"; $rowhdrs .= " thold" if $self->{"type"} eq "cur"; # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { $curlsubj = defined($_ = $current->param("debt$i" . "subj"))? h_abbr(acctsubj_title $_): ""; $curlsummary = defined($_ = $current->param("debt$i" . "summary"))? h_abbr($_): ""; $curlamount = defined($_ = $current->param("debt$i" . "amount"))? h_abbr(fmtntamount $_): ""; print << "EOT"; $curlsubj $curlsummary $curlamount EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { $curbsubj = defined($_ = $current->param("crdt$i" . "subj"))? h_abbr(acctsubj_title $_): ""; $curbsummary = defined($_ = $current->param("crdt$i" . "summary"))? h_abbr($_): ""; $curbamount = defined($_ = $current->param("crdt$i" . "amount"))? h_abbr(fmtntamount $_): ""; print << "EOT"; $curbsubj $curbsummary $curbamount EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { $curlsubj = defined($_ = $current->param("debt$i" . "subj"))? h_abbr(acctsubj_title $_): ""; $curlsummary = defined($_ = $current->param("debt$i" . "summary"))? h_abbr($_): ""; $curlamount = defined($_ = $current->param("debt$i" . "amount"))? h_abbr(fmtntamount $_): ""; $curbsubj = defined($_ = $current->param("crdt$i" . "subj"))? h_abbr(acctsubj_title $_): ""; $curbsummary = defined($_ = $current->param("crdt$i" . "summary"))? h_abbr($_): ""; $curbamount = defined($_ = $current->param("crdt$i" . "amount"))? h_abbr(fmtntamount $_): ""; print << "EOT"; $curlsubj $curlsummary $curlamount $curbsubj $curbsummary $curbamount EOT } return; } # _html_coltmpl_loop_rec: Display a record row column sub _html_coltmpl_loop_rec : method { local ($_, %_); my ($self, $i, $form, $choose, $rowhdrs); my ($coldsubj, $coldsummary, $valdsummary, $coldamount, $valdamount); my ($colcsubj, $colcsummary, $valcsummary, $colcamount, $valcamount); ($self, $i) = @_; $form = $self->{"form"}; $choose = h_abbr(C_("Choose")); $rowhdrs = "threcs"; $rowhdrs .= " thnew" if $self->{"type"} eq "cur"; # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { $coldsubj = "debt$i" . "subj"; $coldsubj = h($coldsubj); $coldsummary = "debt$i" . "summary"; $valdsummary = $self->_val_text($coldsummary, "recsummary"); $coldsummary = h($coldsummary); $coldamount = "debt$i" . "amount"; $valdamount = $self->_val_text($coldamount, "recamount"); $coldamount = h($coldamount); print << "EOT"; EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { $colcsubj = "crdt$i" . "subj"; $colcsubj = h($colcsubj); $colcsummary = "crdt$i" . "summary"; $valcsummary = $self->_val_text($colcsummary, "recsummary"); $colcsummary = h($colcsummary); $colcamount = "crdt$i" . "amount"; $valcamount = $self->_val_text($colcamount, "recamount"); $colcamount = h($colcamount); print << "EOT"; EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { $coldsubj = "debt$i" . "subj"; $coldsubj = h($coldsubj); $coldsummary = "debt$i" . "summary"; $valdsummary = $self->_val_text($coldsummary, "recsummary"); $coldsummary = h($coldsummary); $coldamount = "debt$i" . "amount"; $valdamount = $self->_val_text($coldamount, "recamount"); $coldamount = h($coldamount); $colcsubj = "crdt$i" . "subj"; $colcsubj = h($colcsubj); $colcsummary = "crdt$i" . "summary"; $valcsummary = $self->_val_text($colcsummary, "recsummary"); $colcsummary = h($colcsummary); $colcamount = "crdt$i" . "amount"; $valcamount = $self->_val_text($colcamount, "recamount"); $colcamount = h($colcamount); print << "EOT"; EOT } return; } # _html_col_ord: The order sub _html_col_ord : method { local ($_, %_); my ($self, $form); $self = $_[0]; $form = $self->{"form"}; # Set the default order to maximum $form->param("ord", 99) if $self->{"is_first_form"} && $self->{"type"} eq "new"; $self->_html_coltmpl_text("ord", h_abbr(C_("Order:")), undef, ${$self->{"maxlens"}}{"ord"}); } # _html_col_recs: The accounting records sub _html_col_recs : method { local ($_, %_); my ($self, $form, $current, $label, $mark, $orig, $new); my ($labeldebit, $labelcredit, $labelsubj, $labelsummary, $labelamount); my ($labelsum, $sumdebit, $sumcredit); my ($count_new, $rows_new, $count_cur, $rows_cur, $rowspan); $self = $_[0]; $form = $self->{"form"}; $current = $self->{"cur"}; $mark = $self->_mark("recs"); $labeldebit = h_abbr(C_("Debit")); $labelcredit = h_abbr(C_("Credit")); $labelsubj = h_abbr(C_("Accounting subject")); $labelsummary = h_abbr(C_("Summary")); $labelamount = h_abbr(C_("Amount")); $labelsum = h_abbr(C_("Total")); # A form to create a new item if ($self->{"type"} eq "new") { # Find the total number of records $count_new = 0; # Find the last-used debit record for ( $_ = 0; defined $form->param("debt$_" . "subj") || defined $form->param("debt$_" . "summary") || defined $form->param("debt$_" . "amount"); $_++) {}; if ($_ > 0) { for ( $_--; $_ >= 0 && $form->param("debt$_" . "subj") eq "" && $form->param("debt$_" . "summary") eq "" && $form->param("debt$_" . "amount") eq ""; $_--) {}; $_++; } $count_new = $_ if $count_new < $_; # Find the last-used credit record for ( $_ = 0; defined $form->param("crdt$_" . "subj") || defined $form->param("crdt$_" . "summary") || defined $form->param("crdt$_" . "amount"); $_++) {}; if ($_ > 0) { for ( $_--; $_ >= 0 && $form->param("crdt$_" . "subj") eq "" && $form->param("crdt$_" . "summary") eq "" && $form->param("crdt$_" . "amount") eq ""; $_--) {}; $_++; } $count_new = $_ if $count_new < $_; # We need at least 5 blank records $count_new += 5; $_ = $count_new + 2; $_++ if $self->{"subtype"} eq "trans"; $rows_new = " rowspan=\"" . h($_) . "\""; $rows_new = "" if $_ == 1; $label = h_abbr(C_("[numerate,_1,Content]:", 0)); # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { print << "EOT"; EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { print << "EOT"; EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { print << "EOT"; EOT } for ($_ = 0, @_ = qw(); $_ < $count_new; $_++) { $self->_html_coltmpl_loop_rec($_); } # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { for (my $i = 0, $sumdebit = 0; $i < $count_new; $i++) { $sumdebit += $_ if defined($_ = $form->param("debt$i" . "amount")) && /^\d+$/; } print << "EOT"; EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { for (my $i = 0, $sumcredit = 0; $i < $count_new; $i++) { $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")) && /^\d+$/; } print << "EOT"; EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { for (my $i = 0, $sumdebit = 0, $sumcredit = 0; $i < $count_new; $i++) { $sumdebit += $_ if defined($_ = $current->param("debt$i" . "amount")) && /^\d+$/; $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")) && /^\d+$/; } print << "EOT"; EOT } # A form to edit a current item } elsif ($self->{"type"} eq "cur") { $count_cur = 0; $count_cur = $current->param("debtcount") if $count_cur < $current->param("debtcount"); $count_cur = $current->param("crdtcount") if $count_cur < $current->param("crdtcount"); $_ = $count_cur + 2; $_++ if $self->{"subtype"} eq "trans"; $rows_cur = " rowspan=\"" . h($_) . "\""; $rows_cur = "" if $_ == 1; # Find the total number of records $count_new = 0; # Find the last-used debit record for ( $_ = 0; defined $form->param("debt$_" . "subj") || defined $form->param("debt$_" . "summary") || defined $form->param("debt$_" . "amount"); $_++) {}; if ($_ > 0) { for ( $_--; $_ > 0 && $form->param("debt$_" . "subj") eq "" && $form->param("debt$_" . "summary") eq "" && $form->param("debt$_" . "amount") eq ""; $_--) {}; } $_++; $count_new = $_ if $count_new < $_; # Find the last-used credit record for ( $_ = 0; defined $form->param("crdt$_" . "subj") || defined $form->param("crdt$_" . "summary") || defined $form->param("crdt$_" . "amount"); $_++) {}; if ($_ > 0) { for ( $_--; $_ > 0 && $form->param("crdt$_" . "subj") eq "" && $form->param("crdt$_" . "summary") eq "" && $form->param("crdt$_" . "amount") eq ""; $_--) {}; } $_++; $count_new = $_ if $count_new < $_; # We need at least 5 blank records $count_new += 5; $_ = $count_new + 2; $_++ if $self->{"subtype"} eq "trans"; $rows_new = " rowspan=\"" . h($_) . "\""; $rows_new = "" if $_ == 1; $_ = $count_cur + $count_new + 4; $_ += 2 if $self->{"subtype"} eq "trans"; $rowspan = " rowspan=\"" . h($_) . "\""; $rowspan = "" if $_ == 1; $label = h_abbr(C_("[numerate,_1,Content]:", 0)); $orig = h_abbr(C_("Original:")); $new = h_abbr(C_("New:")); # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { print << "EOT"; $mark$label $orig $labelsubj $labelsummary $labelamount EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { print << "EOT"; $mark$label $orig $labelsubj $labelsummary $labelamount EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { print << "EOT"; $mark$label $orig $labeldebit $labelcredit $labelsubj $labelsummary $labelamount $labelsubj $labelsummary $labelamount EOT } for ($_ = 0, @_ = qw(); $_ < $count_cur; $_++) { $self->_html_coltmpl_ro_loop_rec($_); } # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { for (my $i = 0, $sumdebit = 0; $i < $count_cur; $i++) { $sumdebit += $_ if defined($_ = $current->param("debt$i" . "amount")); } $sumdebit = h_abbr(fmtntamount $sumdebit); print << "EOT"; $labelsum $sumdebit EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { for (my $i = 0, $sumcredit = 0; $i < $count_cur; $i++) { $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")); } $sumcredit = h_abbr(fmtntamount $sumcredit); print << "EOT"; $labelsum $sumcredit EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { for (my $i = 0, $sumdebit = 0, $sumcredit = 0; $i < $count_cur; $i++) { $sumdebit += $_ if defined($_ = $current->param("debt$i" . "amount")); $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")); } $sumdebit = h_abbr(fmtntamount $sumdebit); $sumcredit = h_abbr(fmtntamount $sumcredit); print << "EOT"; $labelsum $sumdebit $labelsum $sumcredit EOT } # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { print << "EOT"; $new EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { print << "EOT"; $new EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { print << "EOT"; $new EOT } for ($_ = 0, @_ = qw(); $_ < $count_new; $_++) { $self->_html_coltmpl_loop_rec($_); } # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { for (my $i = 0, $sumdebit = 0; $i < $count_new; $i++) { $sumdebit += $_ if defined($_ = $form->param("debt$i" . "amount")) && /^\d+$/; } print << "EOT"; EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { for (my $i = 0, $sumcredit = 0; $i < $count_new; $i++) { $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")) && /^\d+$/; } print << "EOT"; EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { for (my $i = 0, $sumdebit = 0, $sumcredit = 0; $i < $count_new; $i++) { $sumdebit += $_ if defined($_ = $current->param("debt$i" . "amount")) && /^\d+$/; $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")) && /^\d+$/; } print << "EOT"; EOT } # A form to delete a current item } elsif ($self->{"type"} eq "del") { $count_cur = 0; $count_cur = $current->param("debtcount") if $count_cur < $current->param("debtcount"); $count_cur = $current->param("crdtcount") if $count_cur < $current->param("crdtcount"); $_ = $count_cur + 2; $_++ if $self->{"subtype"} eq "trans"; $rows_cur = " rowspan=\"" . h($_) . "\""; $rows_cur = "" if $_ == 1; $label = h_abbr(C_("[numerate,_1,Content]:", $count_cur)); # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { print << "EOT"; $mark$label $labelsubj $labelsummary $labelamount EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { print << "EOT"; $mark$label $labelsubj $labelsummary $labelamount EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { print << "EOT"; $mark$label $labeldebit $labelcredit $labelsubj $labelsummary $labelamount $labelsubj $labelsummary $labelamount EOT } for ($_ = 0, @_ = qw(); $_ < $count_cur; $_++) { $self->_html_coltmpl_ro_loop_rec($_); } # A form to fill in a cash expense transaction if ($self->{"subtype"} eq "expense") { for (my $i = 0, $sumdebit = 0; $i < $count_cur; $i++) { $sumdebit += $_ if defined($_ = $current->param("debt$i" . "amount")); } $sumdebit = h_abbr(fmtntamount $sumdebit); print << "EOT"; $labelsum $sumdebit EOT # A form to fill in a cash income transaction } elsif ($self->{"subtype"} eq "income") { for (my $i = 0, $sumcredit = 0; $i < $count_cur; $i++) { $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")); } $sumcredit = h_abbr(fmtntamount $sumcredit); print << "EOT"; $labelsum $sumcredit EOT # A form to fill in a transfer transaction } elsif ($self->{"subtype"} eq "trans") { for (my $i = 0, $sumdebit = 0, $sumcredit = 0; $i < $count_cur; $i++) { $sumdebit += $_ if defined($_ = $current->param("debt$i" . "amount")); $sumcredit += $_ if defined($_ = $current->param("crdt$i" . "amount")); } $sumdebit = h_abbr(fmtntamount $sumdebit); $sumcredit = h_abbr(fmtntamount $sumcredit); print << "EOT"; $labelsum $sumdebit $labelsum $sumcredit EOT } } return; } # _html_col_note: The note sub _html_col_note : method { $_[0]->_html_coltmpl_textarea("note", h_abbr(C_("Note:")), h_abbr(C_("Fill in the note here.")), undef, 2); } return 1;