293 lines
9.3 KiB
Perl
Executable File
293 lines
9.3 KiB
Perl
Executable File
#! /usr/bin/perl -w
|
|
# Mandy Wu's Website
|
|
# acctsubj.cgi: The accounting subject administraion.
|
|
|
|
# Copyright (c) 2007-2021 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 <imacat@mail.imacat.idv.tw>
|
|
# First written: 2007-09-24
|
|
|
|
use 5.008;
|
|
use strict;
|
|
use warnings;
|
|
use lib $ENV{"DOCUMENT_ROOT"} . qw(/magicat/lib/perl5);
|
|
use Selima::emandy;
|
|
local $SIG{"__DIE__"} = \&http_500;
|
|
my $d = new Selima::Destroy;
|
|
# Prototype declaration
|
|
sub main();
|
|
sub check_get();
|
|
sub check_post();
|
|
sub html_page($);
|
|
sub fetch_curitem();
|
|
sub import_selparent($);
|
|
|
|
initenv(-restricted => 1,
|
|
-this_table => "acctsubj",
|
|
-dbi_lock => {"acctsubj" => LOCK_EX,
|
|
"acctrecs" => LOCK_SH},
|
|
-lastmod => 1,
|
|
-page_param => {"keywords" => N_("accounting")});
|
|
|
|
main;
|
|
exit 0;
|
|
|
|
sub main() {
|
|
local ($_, %_);
|
|
my ($error, $success, $processor);
|
|
|
|
# If the request is a GET query
|
|
if ($ENV{"REQUEST_METHOD"} ne "POST") {
|
|
$error = check_get;
|
|
# If an error occurs
|
|
if (defined $error) {
|
|
html_page $error;
|
|
|
|
# Display the page
|
|
} else {
|
|
html_page retrieve_status;
|
|
}
|
|
|
|
# If a form was POSTed from the client
|
|
} else {
|
|
$error = check_post;
|
|
# If an error occurs
|
|
if (defined $error) {
|
|
error_redirect $error;
|
|
|
|
# Else, save the data
|
|
} else {
|
|
$processor = new Selima::Processor::AcctSubj($POST);
|
|
$success = $processor->process;
|
|
success_redirect $success;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
# check_get: Check the GET arguments
|
|
sub check_get() {
|
|
local ($_, %_);
|
|
my $error;
|
|
# Only allowing to run on HTTPS
|
|
http_403 if !is_https;
|
|
|
|
# A form is requested
|
|
if (is_form) {
|
|
$_ = form_type;
|
|
# A form to create a new item
|
|
if ($_ eq "new") {
|
|
# Start from the default language
|
|
return {"msg"=>N_("Please add a new accounting subject from [_1]."),
|
|
"margs"=>["_DEFAULT_LANG"],
|
|
"isform"=>0}
|
|
if getlang ne $DEFAULT_LANG;
|
|
|
|
# A form to edit a current item
|
|
} elsif ($_ eq "cur") {
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
|
|
# A form to delete a current item
|
|
} elsif ($_ eq "del") {
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
return {"msg"=>N_("This accounting subject has [numerate,_1,an accounting sub-subject,accounting sub-subjects]. It cannot be deleted. To delete the subject, [numerate,_1,its accounting sub-subject,all of its accounting sub-subjects] must first be deleted."),
|
|
"margs"=>[$CURRENT{"ssubcount"}],
|
|
"isform"=>0}
|
|
if $CURRENT{"ssubcount"} > 0;
|
|
return {"msg"=>N_("This accounting subject has [numerate,_1,an accounting record,accounting records]. It cannot be deleted. To delete the subject, [numerate,_1,its accounting record,all of its accounting records] must first be deleted."),
|
|
"margs"=>[$CURRENT{"reccount"}],
|
|
"isform"=>0}
|
|
if $CURRENT{"reccount"} > 0;
|
|
|
|
# Not a valid form
|
|
} else {
|
|
return {"msg"=>N_("Incorrect form: [_1]."),
|
|
"margs"=>[$_],
|
|
"isform"=>0};
|
|
}
|
|
}
|
|
# List handler handles its own error
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# check_post: Check the POSTed form
|
|
sub check_post() {
|
|
local ($_, %_);
|
|
my ($checker, $error);
|
|
# Only allowing to run on HTTPS
|
|
http_403 if !is_https;
|
|
$_ = form_type;
|
|
# A form to create a new item
|
|
if ($_ eq "new") {
|
|
# Start from the default language
|
|
return {"msg"=>N_("Please add a new accounting subject from [_1]."),
|
|
"margs"=>["_DEFAULT_LANG"],
|
|
"isform"=>0}
|
|
if getlang ne $DEFAULT_LANG;
|
|
# Run the checker
|
|
$checker = new Selima::Checker::AcctSubj(curform);
|
|
$checker->redir(qw(selparent delparent));
|
|
$error = $checker->check(qw(parent code title));
|
|
return $error if defined $error;
|
|
|
|
# A form to edit a current item
|
|
} elsif ($_ eq "cur") {
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;
|
|
# Run the checker
|
|
$checker = new Selima::Checker::AcctSubj(curform);
|
|
$checker->redir(qw(del zhsync selparent delparent));
|
|
$error = $checker->check(qw(parent code title));
|
|
return $error if defined $error;
|
|
|
|
# A form to delete a current item
|
|
} elsif ($_ eq "del") {
|
|
# Check at fetch_curitem()
|
|
$error = fetch_curitem;
|
|
return $error if defined $error;;
|
|
# Run the checker
|
|
$checker = new Selima::Checker::AcctSubj(curform);
|
|
$checker->redir(qw(cancel));
|
|
return {"msg"=>N_("This accounting subject has [numerate,_1,an accounting sub-subject,accounting sub-subjects]. It cannot be deleted. To delete the subject, [numerate,_1,its accounting sub-subject,all of its accounting sub-subjects] must first be deleted."),
|
|
"margs"=>[$CURRENT{"ssubcount"}],
|
|
"isform"=>0}
|
|
if $CURRENT{"ssubcount"} > 0;
|
|
return {"msg"=>N_("This accounting subject has [numerate,_1,an accounting record,accounting records]. It cannot be deleted. To delete the subject, [numerate,_1,its accounting record,all of its accounting records] must first be deleted."),
|
|
"margs"=>[$CURRENT{"reccount"}],
|
|
"isform"=>0}
|
|
if $CURRENT{"reccount"} > 0;
|
|
|
|
# Not a valid form
|
|
} else {
|
|
return {"msg"=>N_("Incorrect form: [_1]."),
|
|
"margs"=>[$_],
|
|
"isform"=>0};
|
|
}
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# html_page: Display the page
|
|
sub html_page($) {
|
|
local ($_, %_);
|
|
my ($status, $LIST, $FORM);
|
|
$status = $_[0];
|
|
# A form is requested
|
|
if (is_form $status) {
|
|
$FORM = new Selima::Form::AcctSubj($status);
|
|
html_header $FORM->{"title"};
|
|
html_errmsg $status;
|
|
$FORM->html;
|
|
html_footer;
|
|
|
|
# List the available items
|
|
} else {
|
|
if (list_type eq "lastlv") {
|
|
$LIST = new Selima::List::Accounting::Subjects::LastLv;
|
|
} else {
|
|
$LIST = new Selima::List::Accounting::Subjects;
|
|
}
|
|
html_header $LIST->{"title"}, $LIST->page_param;
|
|
html_errmsg $status;
|
|
$LIST->html;
|
|
html_footer;
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
##################################
|
|
# Subroutines to manage the data #
|
|
##################################
|
|
# fetch_curitem: Fetch the current item
|
|
sub fetch_curitem() {
|
|
local ($_, %_);
|
|
my ($sn, $FORM, $sth, $sql, $row);
|
|
my ($lang, $lndb, $lndbdef, $title);
|
|
|
|
# Return if fetched before
|
|
return if scalar(keys %CURRENT) > 0;
|
|
|
|
# Obtain the current form
|
|
$FORM = curform;
|
|
# No item specified
|
|
return {"msg"=>N_("Please select the accounting subject."),
|
|
"isform"=>0}
|
|
if !defined $FORM->param("sn");
|
|
$sn = $FORM->param("sn");
|
|
|
|
# Find the record
|
|
%CURRENT = fetchrec $sn, $THIS_TABLE;
|
|
# If this record exist
|
|
return {"msg"=>N_("This accounting subject does not exist anymore. Please select another one."),
|
|
"isform"=>0}
|
|
if scalar(keys %CURRENT) == 0;
|
|
|
|
$lang = getlang;
|
|
$lndb = getlang LN_DATABASE;
|
|
$lndbdef = ln $DEFAULT_LANG, LN_DATABASE;
|
|
|
|
# Obtain the belonging subjects list
|
|
@_ = qw();
|
|
push @_, "sn AS sn";
|
|
if (@ALL_LINGUAS > 1) {
|
|
$title = $lang eq $DEFAULT_LANG? "title_$lndb":
|
|
"COALESCE(title_$lndb, title_$lndbdef)";
|
|
} else {;
|
|
$title = "title";
|
|
}
|
|
push @_, $DBH->strcat("code", "' '", $title) . " AS title";
|
|
$sql = "SELECT " . join(", ", @_) . " FROM acctsubj"
|
|
. " WHERE parent=$sn"
|
|
. " ORDER BY code;\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
$CURRENT{"ssubcount"} = $sth->rows;
|
|
for ($_ = 0; $_ < $CURRENT{"ssubcount"}; $_++) {
|
|
$row = $sth->fetchrow_hashref;
|
|
$CURRENT{"ssub$_" . "sn"} = $$row{"sn"};
|
|
$CURRENT{"ssub$_" . "title"} = $$row{"title"};
|
|
}
|
|
|
|
# Obtain the belonging records list
|
|
$sql = "SELECT sn FROM acctrecs"
|
|
. " WHERE subj=$sn;\n";
|
|
$sth = $DBH->prepare($sql);
|
|
$sth->execute;
|
|
$CURRENT{"reccount"} = $sth->rows;
|
|
|
|
# OK
|
|
return;
|
|
}
|
|
|
|
# import_selparent: Import the selected parent into the retrieved form
|
|
sub import_selparent($) {
|
|
local ($_, %_);
|
|
my $FORM;
|
|
$FORM = $_[0];
|
|
if ( defined $GET->param("selsn")
|
|
&& check_sn_in ${$GET->param_fetch("selsn")}[0], "acctsubj") {
|
|
$FORM->param("parent", $GET->param("selsn"));
|
|
$FORM->param("topmost", "false");
|
|
}
|
|
return;
|
|
}
|