From 2ff2f6c9c9f95845ddb83d1cef264e580977759c Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 16 Jun 2011 15:23:03 +0200 Subject: [PATCH] Verwaltung von Abteilungen auf Controller umgestellt --- SL/AM.pm | 106 +---------- SL/Controller/Department.pm | 104 +++++++++++ SL/DB/Department.pm | 24 ++- SL/DB/Manager/Department.pm | 21 +++ bin/mozilla/am.pl | 236 ------------------------ locale/de/all | 12 +- menu.ini | 8 +- templates/webpages/department/form.html | 42 +++++ templates/webpages/department/list.html | 47 +++++ 9 files changed, 247 insertions(+), 353 deletions(-) create mode 100644 SL/Controller/Department.pm create mode 100644 SL/DB/Manager/Department.pm create mode 100644 templates/webpages/department/form.html create mode 100644 templates/webpages/department/list.html diff --git a/SL/AM.pm b/SL/AM.pm index 4f5b9718f..ff9fc46e2 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -250,7 +250,7 @@ sub save_account { # if charttype is heading make sure certain values are empty # specifically, if charttype is changed from an existing account, empty the - # fields unnecessary for headings, so that e.g. heading doesn't appear in + # fields unnecessary for headings, so that e.g. heading doesn't appear in # drop-down menues due to still having a valid "link" entry if ( $form->{charttype} eq 'H' ) { @@ -474,110 +474,6 @@ sub delete_account { return $rc; } -sub departments { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT d.id, d.description, d.role - FROM department d - ORDER BY 2|; - - my $sth = $dbh->prepare($query); - $sth->execute || $form->dberror($query); - - $form->{ALL} = []; - while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { - push @{ $form->{ALL} }, $ref; - } - - $sth->finish; - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub get_department { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my $query = qq|SELECT d.description, d.role - FROM department d - WHERE d.id = ?|; - my $sth = $dbh->prepare($query); - $sth->execute($form->{id}) || $form->dberror($query . " ($form->{id})"); - - my $ref = $sth->fetchrow_hashref("NAME_lc"); - - map { $form->{$_} = $ref->{$_} } keys %$ref; - - $sth->finish; - - # see if it is in use - $query = qq|SELECT count(*) FROM dpt_trans d - WHERE d.department_id = ?|; - ($form->{orphaned}) = selectrow_query($form, $dbh, $query, $form->{id}); - - $form->{orphaned} = !$form->{orphaned}; - $sth->finish; - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub save_department { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - my ($query); - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - my @values = ($form->{description}, $form->{role}); - if ($form->{id}) { - $query = qq|UPDATE department SET - description = ?, role = ? - WHERE id = ?|; - push(@values, $form->{id}); - } else { - $query = qq|INSERT INTO department - (description, role) - VALUES (?, ?)|; - } - do_query($form, $dbh, $query, @values); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - -sub delete_department { - $main::lxdebug->enter_sub(); - - my ($self, $myconfig, $form) = @_; - my ($query); - - # connect to database - my $dbh = $form->dbconnect($myconfig); - - $query = qq|DELETE FROM department - WHERE id = ?|; - do_query($form, $dbh, $query, $form->{id}); - - $dbh->disconnect; - - $main::lxdebug->leave_sub(); -} - sub lead { $main::lxdebug->enter_sub(); diff --git a/SL/Controller/Department.pm b/SL/Controller/Department.pm new file mode 100644 index 000000000..d1ba94239 --- /dev/null +++ b/SL/Controller/Department.pm @@ -0,0 +1,104 @@ +package SL::Controller::Department; + +use strict; + +use parent qw(SL::Controller::Base); + +use SL::DB::Department; +use SL::Helper::Flash; + +use Rose::Object::MakeMethods::Generic +( + scalar => [ qw(department) ], +); + +__PACKAGE__->run_before('check_auth'); +__PACKAGE__->run_before('load_department', only => [ qw(edit update destroy) ]); + +# +# actions +# + +sub action_list { + my ($self) = @_; + + $self->render('department/list', + title => $::locale->text('Departments'), + DEPARTMENTS => SL::DB::Manager::Department->get_all_sorted); +} + +sub action_new { + my ($self) = @_; + + $self->{department} = SL::DB::Department->new(role => 'P'); + $self->render('department/form', title => $::locale->text('Create a new department')); +} + +sub action_edit { + my ($self) = @_; + $self->render('department/form', title => $::locale->text('Edit department')); +} + +sub action_create { + my ($self) = @_; + + $self->{department} = SL::DB::Department->new; + $self->create_or_update; +} + +sub action_update { + my ($self) = @_; + $self->create_or_update; +} + +sub action_destroy { + my ($self) = @_; + + if (eval { $self->{department}->delete; 1; }) { + flash_later('info', $::locale->text('The department has been deleted.')); + } else { + flash_later('error', $::locale->text('The department is in use and cannot be deleted.')); + } + + $self->redirect_to(action => 'list'); +} + +# +# filters +# + +sub check_auth { + $::auth->assert('config'); +} + +# +# helpers +# + +sub create_or_update { + my $self = shift; + my $is_new = !$self->{department}->id; + my $params = delete($::form->{department}) || { }; + + $self->{department}->assign_attributes(%{ $params }); + + my @errors = $self->{department}->validate; + + if (@errors) { + flash('error', @errors); + $self->render('department/form', title => $is_new ? $::locale->text('Create a new department') : $::locale->text('Edit department')); + return; + } + + $self->{department}->save; + + flash_later('info', $is_new ? $::locale->text('The department has been created.') : $::locale->text('The department has been saved.')); + $self->redirect_to(action => 'list'); +} + +sub load_department { + my ($self) = @_; + $self->{department} = SL::DB::Department->new(id => $::form->{id})->load; +} + +1; diff --git a/SL/DB/Department.pm b/SL/DB/Department.pm index ea62791cd..133886cdb 100644 --- a/SL/DB/Department.pm +++ b/SL/DB/Department.pm @@ -1,13 +1,27 @@ -# This file has been auto-generated only because it didn't exist. -# Feel free to modify it at will; it will not be overwritten automatically. - package SL::DB::Department; use strict; use SL::DB::MetaSetup::Department; +use SL::DB::Manager::Department; + +use SL::DB::DptTrans; + +sub validate { + my ($self) = @_; + + my @errors; + push @errors, $::locale->text('The description is missing.') if !$self->description; + + return @errors; +} + +sub is_used { + my ($self) = @_; -# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. -__PACKAGE__->meta->make_manager_class; + return undef if !$self->id; + my $is_used = SL::DB::Manager::DptTrans->find_by(department_id => $self->id); + return !!$is_used; +} 1; diff --git a/SL/DB/Manager/Department.pm b/SL/DB/Manager/Department.pm new file mode 100644 index 000000000..c2375308b --- /dev/null +++ b/SL/DB/Manager/Department.pm @@ -0,0 +1,21 @@ +package SL::DB::Manager::Department; + +use strict; + +use SL::DB::Helper::Manager; +use base qw(SL::DB::Helper::Manager); + +use SL::DB::Helper::Sorted; + +sub object_class { 'SL::DB::Department' } + +__PACKAGE__->make_manager_methods; + +sub _sort_spec { + return ( default => [ 'description', 1 ], + columns => { SIMPLE => 'ALL', + map { ( $_ => "lower(department.$_)" ) } qw(description) + }); +} + +1; diff --git a/bin/mozilla/am.pl b/bin/mozilla/am.pl index c6bb8e47c..0030eb51b 100644 --- a/bin/mozilla/am.pl +++ b/bin/mozilla/am.pl @@ -620,242 +620,6 @@ sub delete_account { $main::lxdebug->leave_sub(); } -sub add_department { - $main::lxdebug->enter_sub(); - - my $form = $main::form; - - $main::auth->assert('config'); - - $form->{title} = "Add"; - $form->{role} = "P"; - - $form->{callback} = "am.pl?action=add_department" unless $form->{callback}; - - &department_header; - &form_footer; - - $main::lxdebug->leave_sub(); -} - -sub edit_department { - $main::lxdebug->enter_sub(); - - my $form = $main::form; - my %myconfig = %main::myconfig; - - $main::auth->assert('config'); - - $form->{title} = "Edit"; - - AM->get_department(\%myconfig, \%$form); - - &department_header; - &form_footer; - - $main::lxdebug->leave_sub(); -} - -sub list_department { - $main::lxdebug->enter_sub(); - - my $form = $main::form; - my %myconfig = %main::myconfig; - my $locale = $main::locale; - - $main::auth->assert('config'); - - AM->departments(\%myconfig, \%$form); - - $form->{callback} = "am.pl?action=list_department"; - - my $callback = $form->escape($form->{callback}); - - $form->{title} = $locale->text('Departments'); - - my @column_index = qw(description cost profit); - my %column_header; - $column_header{description} = - qq|| - . $locale->text('Description') - . qq||; - $column_header{cost} = - qq|| - . $locale->text('Cost Center') - . qq||; - $column_header{profit} = - qq|| - . $locale->text('Profit Center') - . qq||; - - $form->header; - - print qq| - - - - - - - - - - - - - -
$form->{title}
- - -|; - - map { print "$column_header{$_}\n" } @column_index; - - print qq| - -|; - - my ($i, %column_data); - foreach my $ref (@{ $form->{ALL} }) { - - $i++; - $i %= 2; - - print qq| - -|; - - my $costcenter = ($ref->{role} eq "C") ? "X" : ""; - my $profitcenter = ($ref->{role} eq "P") ? "X" : ""; - - $column_data{description} = - qq||; - $column_data{cost} = qq||; - $column_data{profit} = qq||; - - map { print "$column_data{$_}\n" } @column_index; - - print qq| - -|; - } - - print qq| -
$ref->{description}$costcenter$profitcenter
-

- -
-
- - - - - - - -
- - - -|; - - $main::lxdebug->leave_sub(); -} - -sub department_header { - $main::lxdebug->enter_sub(); - - my $form = $main::form; - my $locale = $main::locale; - - $main::auth->assert('config'); - - $form->{title} = $locale->text("$form->{title} Department"); - - # $locale->text('Add Department') - # $locale->text('Edit Department') - - $form->{description} =~ s/\"/"/g; - - my ($rows, $description); - if (($rows = $form->numtextrows($form->{description}, 60)) > 1) { - $description = - qq||; - } else { - $description = - qq||; - } - - my $costcenter = "checked" if $form->{role} eq "C"; - my $profitcenter = "checked" if $form->{role} eq "P"; - - $form->header; - - print qq| - - -
- -{id}> - - - - - - - - - - - - - - - - - -
$form->{title}
| . $locale->text('Description') . qq|$description
| - . $locale->text('Cost Center') . qq| - | - . $locale->text('Profit Center') . qq| -

-|; - - $main::lxdebug->leave_sub(); -} - -sub save_department { - $main::lxdebug->enter_sub(); - - my $form = $main::form; - my %myconfig = %main::myconfig; - my $locale = $main::locale; - - $main::auth->assert('config'); - - $form->isblank("description", $locale->text('Description missing!')); - AM->save_department(\%myconfig, \%$form); - $form->redirect($locale->text('Department saved!')); - - $main::lxdebug->leave_sub(); -} - -sub delete_department { - $main::lxdebug->enter_sub(); - - my $form = $main::form; - my %myconfig = %main::myconfig; - my $locale = $main::locale; - - $main::auth->assert('config'); - - AM->delete_department(\%myconfig, \%$form); - $form->redirect($locale->text('Department deleted!')); - - $main::lxdebug->leave_sub(); -} - sub add_lead { $main::lxdebug->enter_sub(); diff --git a/locale/de/all b/locale/de/all index 8659da886..b89e04c2c 100644 --- a/locale/de/all +++ b/locale/de/all @@ -196,6 +196,7 @@ $self->{texts} = { 'Are you sure you want to delete Order Number' => 'Soll der Auftrag mit folgender Nummer wirklich gelöscht werden:', 'Are you sure you want to delete Quotation Number' => 'Sind Sie sicher, dass Angebotnummer gelöscht werden soll?', 'Are you sure you want to delete Transaction' => 'Buchung wirklich löschen?', + 'Are you sure you want to delete this department?' => 'Sind Sie sicher, dass Sie diese Abteilung löschen wollen?', 'Are you sure you want to delete this payment term?' => 'Wollen Sie diese Zahlungsbedingungen wirklich löschen?', 'Are you sure you want to remove the marked entries from the queue?' => 'Sind Sie sicher, dass die markierten Einträge von der Warteschlange gelöscht werden sollen?', 'Are you sure you want to update the prices' => 'Sind Sie sicher, dass Sie die Preise aktualisieren wollen?', @@ -430,6 +431,7 @@ $self->{texts} = { 'Create Chart of Accounts' => 'Zu verwendender Kontenplan', 'Create Dataset' => 'Neue Datenbank anlegen', 'Create Date' => 'Erstelldatum', + 'Create a new department' => 'Eine neue Abteilung erfassen', 'Create a new payment term' => 'Neue Zahlungsbedingungen anlegen', 'Create a standard group' => 'Eine Standard-Benutzergruppe anlegen', 'Create and edit RFQs' => 'Lieferantenanfragen erfassen und bearbeiten', @@ -450,6 +452,7 @@ $self->{texts} = { 'Create bank transfer via SEPA XML' => 'Überweisung via SEPA XML erzeugen', 'Create invoice?' => 'Rechnung erstellen?', 'Create new' => 'Neu erfassen', + 'Create new department' => 'Neue Abteilung erfassen', 'Create new payment term' => 'Neue Zahlungsbedingung anlegen', 'Create tables' => 'Tabellen anlegen', 'Created by' => 'Erstellt von', @@ -570,8 +573,6 @@ $self->{texts} = { 'Department 1' => 'Abteilung (1)', 'Department 2' => 'Abteilung (2)', 'Department Id' => 'Reservierung', - 'Department deleted!' => 'Abteilung gelöscht.', - 'Department saved!' => 'Abteilung gespeichert.', 'Departments' => 'Abteilungen', 'Dependency loop detected:' => 'Schleife in den Abhängigkeiten entdeckt:', 'Deposit' => 'Gutschrift', @@ -672,7 +673,6 @@ $self->{texts} = { 'Edit Business' => 'Kunden-/Lieferantentyp bearbeiten', 'Edit Credit Note' => 'Gutschrift bearbeiten', 'Edit Customer' => 'Kunde editieren', - 'Edit Department' => 'Abteilung bearbeiten', 'Edit Dunning' => 'Mahnungen konfigurieren', 'Edit Dunning Process Config' => 'Mahnwesenkonfiguration bearbeiten', 'Edit Follow-Up' => 'Wiedervorlage bearbeiten', @@ -705,6 +705,7 @@ $self->{texts} = { 'Edit and delete a group' => 'Gruppen bearbeiten und löschen', 'Edit bank account' => 'Bankkonto bearbeiten', 'Edit custom variable' => 'Benutzerdefinierte Variable bearbeiten', + 'Edit department' => 'Abteilung bearbeiten', 'Edit file' => 'Datei bearbeiten', 'Edit greetings' => 'Anreden bearbeiten', 'Edit group ' => 'Gruppe bearbeiten', @@ -1161,6 +1162,7 @@ $self->{texts} = { 'No data was found.' => 'Es wurden keine Daten gefunden.', 'No databases have been found on this server.' => 'Auf diesem Server wurden keine Datenbanken gefunden.', 'No datasets have been selected.' => 'Es wurden keine Datenbanken ausgewählt.', + 'No department has been created yet.' => 'Es wurde noch keine Abteilung erfasst.', 'No dunnings have been selected for printing.' => 'Es wurden keine Mahnungen zum Drucken ausgewählt.', 'No entries were found which had no unit assigned to them.' => 'Es wurden keine Einträge gefunden, denen keine Einheit zugeordnet war.', 'No file has been uploaded yet.' => 'Es wurde noch keine Datei hochgeladen.', @@ -1746,6 +1748,10 @@ $self->{texts} = { 'The deductible amount' => 'Der abziehbare Skontobetrag', 'The default value depends on the variable type:' => 'Die Bedeutung des Standardwertes hängt vom Variablentypen ab:', 'The delivery order has not been marked as delivered. The warehouse contents have not changed.' => 'Der Lieferschein wurde nicht als geliefert markiert. Der Lagerinhalt wurde nicht verändert.', + 'The department has been created.' => 'Die Abteilung wurde angelegt.', + 'The department has been deleted.' => 'Die Abteiltung wurde gelöscht.', + 'The department has been saved.' => 'Die abteilung wurde gespeichert.', + 'The department is in use and cannot be deleted.' => 'Die Abteilung wird benutzt und kann nicht gelöscht werden.', 'The description is missing.' => 'Die Beschreibung fehlt.', 'The description is shown on the form. Chose something short and descriptive.' => 'Die Beschreibung wird in der jeweiligen Maske angezeigt. Sie sollte kurz und prägnant sein.', 'The directory "%s" could not be created:\n%s' => 'Das Verzeichnis "%s" konnte nicht erstellt werden:\n%s', diff --git a/menu.ini b/menu.ini index de5ca683b..46292a0e7 100644 --- a/menu.ini +++ b/menu.ini @@ -633,12 +633,12 @@ target=acc_menu submenu=1 [System--Departments--Add Department] -module=am.pl -action=add_department +module=controller.pl +action=Department/new [System--Departments--List Departments] -module=am.pl -action=list_department +module=controller.pl +action=Department/list [System--Type of Business] module=menu.pl diff --git a/templates/webpages/department/form.html b/templates/webpages/department/form.html new file mode 100644 index 000000000..f95836d77 --- /dev/null +++ b/templates/webpages/department/form.html @@ -0,0 +1,42 @@ +[% USE HTML %][% USE T8 %][% USE L %][% USE LxERP %] +[% SET is_used = SELF.department.is_used %] + + + +
[% FORM.title %]
+ +[%- INCLUDE 'common/flash.html' %] + + + + + + + + + + + +
[%- 'Description' | $T8 %][% L.input_tag("department.description", SELF.department.description) %]
[%- 'Type' | $T8 %] + [%- IF is_used %] + [% L.hidden_tag("role", SELF.department.role) %] + [%- IF SELF.department.role == "C" %][%- LxERP.t8('Cost Center') %][%- ELSE %][%- LxERP.t8('Profit Center') %][%- END %] + [%- ELSE %] + [% L.radio_button_tag("department.role", "value", "C", "label", LxERP.t8("Cost Center"), "checked", SELF.department.role == "C") %] +
+ [% L.radio_button_tag("department.role", "value", "P", "label", LxERP.t8("Profit Center"), "checked", SELF.department.role == "P") %] + [%- END %] +
+ +

+ [% L.hidden_tag("id", SELF.department.id) %] + [% L.hidden_tag("action", "Department/dispatch") %] + + [%- IF SELF.department.id && !is_used %] + [% L.submit_tag("action_destroy", LxERP.t8("Delete"), "confirm", LxERP.t8("Are you sure you want to delete this department?")) %] + [%- END %] + [%- 'Abort' | $T8 %] +

+
+ + diff --git a/templates/webpages/department/list.html b/templates/webpages/department/list.html new file mode 100644 index 000000000..059aa33b0 --- /dev/null +++ b/templates/webpages/department/list.html @@ -0,0 +1,47 @@ +[% USE HTML %][% USE T8 %][% USE L %][% USE LxERP %] + + +
[% FORM.title %]
+ +[%- INCLUDE 'common/flash.html' %] + +
+ [% IF !DEPARTMENTS.size %] +

+ [%- 'No department has been created yet.' | $T8 %] +

+ + [%- ELSE %] + + + + + + + + + + + [%- FOREACH department = DEPARTMENTS %] + + + + + + [%- END %] + +
[%- 'Description' | $T8 %][%- 'Cost Center' | $T8 %][%- 'Profit Center' | $T8 %]
+ + [%- HTML.escape(department.description) %] + + [%- IF department.role == 'C' %]X[%- END %][%- IF department.role == 'P' %]X[%- END %]
+ [%- END %] + +
+ +

+ [%- 'Create new department' | $T8 %] +

+
+ + -- 2.20.1