# Selima Website Content Management System # AcctRec.pm: The accounting record form checker. # 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-22 package Selima::Checker::AcctRec; use 5.008; use strict; use warnings; use base qw(Selima::Checker); use Selima::CallForm; use Selima::ChkFunc; use Selima::ShortCut; use Selima::DataVars qw($DBH :forms); # new: Initialize the checker sub new : method { local ($_, %_); my ($class, $self); ($class, @_) = @_; $_[1] = "acctrecs" if scalar(@_) < 2 || !defined $_[1]; $self = $class->SUPER::new(@_); ${$self->{"maxlens"}}{"amount"} = 9; return $self; } # _check_trx: Check the transaction sub _check_trx : method { local ($_, %_); my ($self, $form, $error, $sth, $sql); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("trx"); return $error if defined $error; # Regularize it $self->_trim("trx"); # Check if it is filled return {"msg"=>N_("Please select a accounting transaction.")} if $form->param("trx") eq ""; # Check if the transaction exists return {"msg"=>N_("This accounting transaction does not exist anymore. Please select another one.")} if !check_sn_in ${$form->param_fetch("trx")}[0], "accttrx"; # OK return; } # _check_type: Check the type sub _check_type : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("type"); return $error if defined $error; # Regularize it $self->_trim("type"); # Check the option value return {"msg"=>N_("This option is invalid. Please select a proper type.")} unless $form->param("type") =~ /^(?:debit|credit)$/; # OK return; } # _check_ord: Check the order # Use the default order checker # _check_subj: Check the subject sub _check_subj : method { local ($_, %_); my ($self, $form, $error, $sth, $sql); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("subj"); return $error if defined $error; # Regularize it $self->_trim("subj"); # Check if it is filled return {"msg"=>N_("Please select a accounting subject.")} if $form->param("subj") eq ""; # Check if the subject exists return {"msg"=>N_("This accounting subject does not exist anymore. Please select another one.")} if !check_sn_in ${$form->param_fetch("subj")}[0], "acctsubj"; # Check if this is the last level subject $sql = "SELECT * FROM acctsubj" . " WHERE parent=" . $form->param("subj") . ";\n"; $sth = $DBH->prepare($sql); $sth->execute; return {"msg"=>N_("Only a last-level accounting subject is allowed for an accounting subject.")} if $sth->rows > 0; # OK return; } # _check_summary: Check the summary sub _check_summary : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("summary"); return $error if defined $error; # Regularize it $self->_trim("summary"); # Skip if it is not filled return if $form->param("summary") eq ""; # Check the length return {"msg"=>N_("This summary is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"summary"}]} if length $form->param("summary") > ${$self->{"maxlens"}}{"summary"}; # OK return; } # _check_amount: Check the amount sub _check_amount : method { local ($_, %_); my ($self, $form, $error); $self = $_[0]; $form = $self->{"form"}; # Check if it exists $error = $self->_missing("amount"); return $error if defined $error; # Regularize it $self->_trim("amount"); $_ = $form->param("amount"); s/NT\$ ?//; s/,//g; s/\.0+$//; $form->param("amount", $_); # Check if it is filled return {"msg"=>N_("Please fill in the amount.")} if $form->param("amount") eq ""; # Check the length return {"msg"=>N_("This amount is too long. (Max. length [#,_1])"), "margs"=>[${$self->{"maxlens"}}{"amount"}]} if length $form->param("amount") > ${$self->{"maxlens"}}{"amount"}; # Check if it is a valid positive integer return {"msg"=>N_("Please fill in a positive integer amount.")} unless $form->param("amount") =~ /^\d+$/ && $form->param("amount") > 0; # Set to an integer $_ = $form->param("amount"); $_ += 0; $form->param("amount", $_); # OK return; } # _redir_seltrx: Suspend and move to the transaction selection form sub _redir_seltrx : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if !defined $self->{"form"}->param("seltrx"); call_form FORM_ACCTTRX, undef, "import_seltrx"; } # _redir_deltrx: Remove the transaction sub _redir_deltrx : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if !defined $self->{"form"}->param("deltrx"); $self->{"form"}->delete("trx"); success_redirect undef; } # _redir_selsubj: Suspend and move to the accounting subject selection form sub _redir_selsubj : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if !defined $self->{"form"}->param("selsubj"); call_form FORM_ACCTSUBJ, ["list=lastlv"], "import_selsubj"; } # _redir_delsubj: Remove the accounting subject sub _redir_delsubj : method { local ($_, %_); my $self; $self = $_[0]; # Skip if not requested return if !defined $self->{"form"}->param("delsubj"); $self->{"form"}->delete("subj"); success_redirect undef; } return 1;