+ # Show form again if there were any errors. Nothing's been changed
+ # yet in the database.
+ if (@errors) {
+ flash('error', @errors);
+ return $self->edit_form;
+ }
+
+ # Save currencies. As the names must be unique we cannot simply save
+ # them as they are -- the user might want to swap to names. So make
+ # them unique first and assign the actual names in a second step.
+ my %currency_names_by_id = map { ($_->id => $_->name) } @{ $self->all_currencies };
+ $_->update_attributes(name => '__039519735__' . $_->{id}) for @{ $self->all_currencies };
+ $_->update_attributes(name => $currency_names_by_id{ $_->{id} }) for @{ $self->all_currencies };
+
+ # Create new currency if required
+ my $new_currency;
+ if ($::form->{new_currency}) {
+ $new_currency = SL::DB::Currency->new(name => $::form->{new_currency});
+ $new_currency->save;
+ }
+
+ # If the user wants the new currency to be the default then replace
+ # the ID placeholder with the proper value. However, if no new
+ # currency has been created then don't change the value at all.
+ if (-1 == $self->defaults->currency_id) {
+ $self->defaults->currency_id($new_currency ? $new_currency->id : $original_currency_id);
+ }
+
+ # Create new templates if requested.
+ if ($::form->{use_templates} eq 'new') {
+ local $File::Copy::Recursive::SkipFlop = 1;
+ File::Copy::Recursive::dircopy('templates/print/' . $::form->{new_master_templates}, 'templates/' . $::form->{new_templates});
+ $self->defaults->templates('templates/' . $::form->{new_templates});
+ }