From: Niclas Zimmermann Date: Fri, 24 May 2013 09:49:46 +0000 (+0200) Subject: Merge branch 'currency' X-Git-Tag: release-3.1.0beta1~401^2~21 X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/commitdiff_plain/61cdba5d566357f3beabe0e7f3f0cb2d7bdccd73?hp=617b4d789429eb3f282eedd9d07ec25573cb64e7 Merge branch 'currency' --- diff --git a/SL/AM.pm b/SL/AM.pm index c85c47911..6989e3590 100644 --- a/SL/AM.pm +++ b/SL/AM.pm @@ -1060,10 +1060,6 @@ sub save_defaults { my %accnos; map { ($accnos{$_}) = split(m/--/, $form->{$_}) } qw(inventory_accno income_accno expense_accno fxgain_accno fxloss_accno ar_paid_accno); - $form->{curr} =~ s/ //g; - my @currencies = grep { $_ ne '' } split m/:/, $form->{curr}; - my $currency = join ':', @currencies; - # these defaults are database wide my $query = @@ -1087,7 +1083,6 @@ sub save_defaults { assemblynumber = ?, sdonumber = ?, pdonumber = ?, - curr = ?, businessnumber = ?, weightunit = ?, language_id = ?|; @@ -1100,11 +1095,24 @@ sub save_defaults { $form->{articlenumber}, $form->{servicenumber}, $form->{assemblynumber}, $form->{sdonumber}, $form->{pdonumber}, - $currency, $form->{businessnumber}, $form->{weightunit}, conv_i($form->{language_id})); do_query($form, $dbh, $query, @values); + $main::lxdebug->message(0, "es gibt rowcount: " . $form->{rowcount}); + + for my $i (1..$form->{rowcount}) { + if ($form->{"curr_$i"} ne $form->{"old_curr_$i"}) { + $query = qq|UPDATE currencies SET name = ? WHERE name = ?|; + do_query($form, $dbh, $query, $form->{"curr_$i"}, $form->{"old_curr_$i"}); + } + } + + if (length($form->{new_curr}) > 0) { + $query = qq|INSERT INTO currencies (name) VALUES (?)|; + do_query($form, $dbh, $query, $form->{new_curr}); + } + $dbh->commit(); $main::lxdebug->leave_sub(); @@ -1118,7 +1126,7 @@ sub save_preferences { my $dbh = $form->get_standard_dbh($myconfig); - my ($currency, $businessnumber) = selectrow_query($form, $dbh, qq|SELECT curr, businessnumber FROM defaults|); + my ($businessnumber) = selectrow_query($form, $dbh, qq|SELECT businessnumber FROM defaults|); # update name my $query = qq|UPDATE employee SET name = ? WHERE login = ?|; @@ -1126,10 +1134,6 @@ sub save_preferences { my $rc = $dbh->commit(); - # save first currency in myconfig - $currency =~ s/:.*//; - $form->{currency} = $currency; - $form->{businessnumber} = $businessnumber; $myconfig = User->new(login => $form->{login}); @@ -1286,6 +1290,15 @@ sub defaultaccounts { } $sth->finish; + + #Get currencies: + $query = qq|SELECT name AS curr FROM currencies ORDER BY id|; + $form->{CURRENCIES} = selectall_hashref_query($form, $dbh, $query); + + #Which of them is the default currency? + $query = qq|SELECT name AS defaultcurrency FROM currencies WHERE id = (SELECT currency_id FROM defaults LIMIT 1);|; + ($form->{defaultcurrency}) = selectrow_query($form, $dbh, $query); + $dbh->disconnect; $main::lxdebug->leave_sub(); diff --git a/SL/AP.pm b/SL/AP.pm index 53c1d2f5d..bc5a36202 100644 --- a/SL/AP.pm +++ b/SL/AP.pm @@ -55,7 +55,6 @@ sub post_transaction { my $exchangerate = 0; $form->{defaultcurrency} = $form->get_default_currency($myconfig); - delete $form->{currency} unless $form->{defaultcurrency}; ($null, $form->{department_id}) = split(/--/, $form->{department}); @@ -185,7 +184,7 @@ sub post_transaction { $query = qq|UPDATE ap SET invnumber = ?, transdate = ?, ordnumber = ?, vendor_id = ?, taxincluded = ?, amount = ?, duedate = ?, paid = ?, netamount = ?, - curr = ?, notes = ?, department_id = ?, storno = ?, storno_id = ?, + currency_id = (SELECT id FROM currencies WHERE name = ?), notes = ?, department_id = ?, storno = ?, storno_id = ?, globalproject_id = ?, direct_debit = ? WHERE id = ?|; @values = ($form->{invnumber}, conv_date($form->{transdate}), @@ -666,7 +665,6 @@ sub post_payment { $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); $form->{defaultcurrency} = $form->get_default_currency($myconfig); - delete $form->{currency} unless $form->{defaultcurrency}; # Get the AP accno. $query = diff --git a/SL/AR.pm b/SL/AR.pm index 5ec2b1850..b6a7376e1 100644 --- a/SL/AR.pm +++ b/SL/AR.pm @@ -56,7 +56,6 @@ sub post_transaction { my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig); $form->{defaultcurrency} = $form->get_default_currency($myconfig); - delete $form->{currency} unless $form->{defaultcurrency}; # set exchangerate $form->{exchangerate} = ($form->{currency} eq $form->{defaultcurrency}) ? 1 : @@ -135,8 +134,8 @@ sub post_transaction { } else { $query = qq|SELECT nextval('glid')|; ($form->{id}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO ar (id, invnumber, employee_id) VALUES (?, 'dummy', ?)|; - do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}); + $query = qq|INSERT INTO ar (id, invnumber, employee_id, currency_id) VALUES (?, 'dummy', ?, (SELECT id FROM currencies WHERE name=?))|; + do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}, $form->{currency}); $form->{invnumber} = $form->update_defaults($myconfig, "invnumber", $dbh) unless $form->{invnumber}; } } @@ -156,12 +155,12 @@ sub post_transaction { qq|UPDATE ar set invnumber = ?, ordnumber = ?, transdate = ?, customer_id = ?, taxincluded = ?, amount = ?, duedate = ?, paid = ?, - netamount = ?, curr = ?, notes = ?, department_id = ?, + netamount = ?, notes = ?, department_id = ?, employee_id = ?, storno = ?, storno_id = ?, globalproject_id = ?, direct_debit = ? WHERE id = ?|; my @values = ($form->{invnumber}, $form->{ordnumber}, conv_date($form->{transdate}), conv_i($form->{customer_id}), $form->{taxincluded} ? 't' : 'f', $form->{amount}, - conv_date($form->{duedate}), $form->{paid}, $form->{netamount}, $form->{currency}, $form->{notes}, conv_i($form->{department_id}), + conv_date($form->{duedate}), $form->{paid}, $form->{netamount}, $form->{notes}, conv_i($form->{department_id}), conv_i($form->{employee_id}), $form->{storno} ? 't' : 'f', $form->{storno_id}, conv_i($form->{globalproject_id}), $form->{direct_debit} ? 't' : 'f', conv_i($form->{id})); do_query($form, $dbh, $query, @values); @@ -427,7 +426,6 @@ sub post_payment { $form->{exchangerate} = $form->format_amount($myconfig, $form->{exchangerate}); $form->{defaultcurrency} = $form->get_default_currency($myconfig); - delete $form->{currency} unless $form->{defaultcurrency}; # Get the AR accno (which is normally done by Form::create_links()). $query = diff --git a/SL/CP.pm b/SL/CP.pm index 18673c215..29ac35ab0 100644 --- a/SL/CP.pm +++ b/SL/CP.pm @@ -93,10 +93,9 @@ sub paymentaccounts { } $sth->finish; - # get currencies and closedto - $query = qq|SELECT curr, closedto FROM defaults|; - ($form->{currencies}, $form->{closedto}) = - selectrow_query($form, $dbh, $query); + # get closedto + $query = qq|SELECT closedto FROM defaults|; + ($form->{closedto}) = selectrow_query($form, $dbh, $query); $dbh->disconnect; @@ -150,9 +149,10 @@ sub get_openinvoices { my $arap = $form->{arap} eq "ar" ? "ar" : "ap"; my $query = - qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, a.curr | . + qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, cu.name AS curr | . qq|FROM $arap a | . - qq|WHERE (a.${vc}_id = ?) AND (COALESCE(a.curr, '') = ?) AND NOT (a.amount = a.paid)| . + qq|LEFT JOIN currencies cu ON (cu.id=a.currency_id)| . + qq|WHERE (a.${vc}_id = ?) AND cu.name = ? AND NOT (a.amount = a.paid)| . qq|ORDER BY a.id|; my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{"${vc}_id"}), @@ -174,7 +174,7 @@ sub get_openinvoices { SELECT COUNT(*) FROM $arap WHERE (${vc}_id = ?) - AND (COALESCE(curr, '') <> ?) + AND ((SELECT cu.name FROM currencies cu WHERE cu.id=${arap}.currency_id) <> ?) AND (amount <> paid) SQL ($form->{openinvoices_other_currencies}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{"${vc}_id"}), "$form->{currency}"); @@ -250,7 +250,7 @@ sub process_payment { qq|SELECT $buysell | . qq|FROM exchangerate e | . qq|JOIN ${arap} a ON (a.transdate = e.transdate) | . - qq|WHERE (e.curr = ?) AND (a.id = ?)|; + qq|WHERE (e.currency_id = (SELECT id FROM currencies WHERE name = ?)) AND (a.id = ?)|; my ($exchangerate) = selectrow_query($form, $dbh, $query, $form->{currency}, $form->{"id_$i"}); diff --git a/SL/CT.pm b/SL/CT.pm index 02a5522f6..66b3e25b7 100644 --- a/SL/CT.pm +++ b/SL/CT.pm @@ -68,11 +68,12 @@ sub get_tuple { my $ref = $sth->fetchrow_hashref("NAME_lc"); map { $form->{$_} = $ref->{$_} } keys %$ref; + $sth->finish; - # remove any trailing whitespace - $form->{curr} =~ s/\s*$//; + #get name of currency instead of id: + $query = qq|SELECT name AS curr FROM currencies WHERE id=?|; + ($form->{curr}) = selectrow_query($form, $dbh, $query, conv_i($form->{currency_id})); - $sth->finish; if ( $form->{salesman_id} ) { my $query = qq|SELECT ct.name AS salesman | . @@ -275,7 +276,7 @@ sub save_customer { $query = qq|SELECT nextval('id')|; ($form->{id}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO customer (id, name) VALUES (?, '')|; + $query = qq|INSERT INTO customer (id, name, currency_id) VALUES (?, '', (SELECT currency_id FROM defaults))|; do_query($form, $dbh, $query, $form->{id}); } @@ -319,7 +320,7 @@ sub save_customer { qq|user_password = ?, | . qq|c_vendor_id = ?, | . qq|klass = ?, | . - qq|curr = ?, | . + qq|currency_id = (SELECT id FROM currencies WHERE name = ?), | . qq|taxincluded_checked = ? | . qq|WHERE id = ?|; my @values = ( @@ -362,7 +363,7 @@ sub save_customer { $form->{user_password}, $form->{c_vendor_id}, conv_i($form->{klass}), - substr($form->{currency}, 0, 3), + $form->{currency}, $form->{taxincluded_checked} ne '' ? $form->{taxincluded_checked} : undef, $form->{id} ); @@ -422,7 +423,7 @@ sub save_vendor { $query = qq|SELECT nextval('id')|; ($form->{id}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO vendor (id, name) VALUES (?, '')|; + $query = qq|INSERT INTO vendor (id, name, currency_id) VALUES (?, '', (SELECT currency_id FROM defaults))|; do_query($form, $dbh, $query, $form->{id}); my $vendornumber = SL::TransNumber->new(type => 'vendor', @@ -471,7 +472,7 @@ sub save_vendor { qq| username = ?, | . qq| user_password = ?, | . qq| v_customer_id = ?, | . - qq| curr = ? | . + qq| currency_id = (SELECT id FROM currencies WHERE name = ?) | . qq|WHERE id = ?|; my @values = ( $form->{vendornumber}, @@ -511,7 +512,7 @@ sub save_vendor { $form->{username}, $form->{user_password}, $form->{v_customer_id}, - substr($form->{currency}, 0, 3), + $form->{currency}, $form->{id} ); do_query($form, $dbh, $query, @values); diff --git a/SL/DB/Currency.pm b/SL/DB/Currency.pm new file mode 100644 index 000000000..14ba066e9 --- /dev/null +++ b/SL/DB/Currency.pm @@ -0,0 +1,13 @@ +# 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::Currency; + +use strict; + +use SL::DB::MetaSetup::Currency; + +# Creates get_all, get_all_count, get_all_iterator, delete_all and update_all. +__PACKAGE__->meta->make_manager_class; + +1; diff --git a/SL/DB/Default.pm b/SL/DB/Default.pm index b71b0f474..4ea7be63f 100644 --- a/SL/DB/Default.pm +++ b/SL/DB/Default.pm @@ -9,8 +9,8 @@ __PACKAGE__->meta->make_manager_class; sub get_default_currency { my $self = shift->get; - my @currencies = grep { $_ } split(/:/, $self->curr || ''); - return $currencies[0] || ''; + return $self->currency->name || '' if $self->currency_id; + return ''; } sub get { diff --git a/SL/DB/Helper/ALL.pm b/SL/DB/Helper/ALL.pm index bff5089b9..f951bf9d0 100644 --- a/SL/DB/Helper/ALL.pm +++ b/SL/DB/Helper/ALL.pm @@ -20,6 +20,7 @@ use SL::DB::Chart; use SL::DB::Contact; use SL::DB::CsvImportProfile; use SL::DB::CsvImportProfileSetting; +use SL::DB::Currency; use SL::DB::CustomVariable; use SL::DB::CustomVariableConfig; use SL::DB::CustomVariableValidity; diff --git a/SL/DB/Helper/FlattenToForm.pm b/SL/DB/Helper/FlattenToForm.pm index d5805b0c0..c2d8a5fe9 100644 --- a/SL/DB/Helper/FlattenToForm.pm +++ b/SL/DB/Helper/FlattenToForm.pm @@ -14,11 +14,11 @@ sub flatten_to_form { my $vc = $self->can('customer_id') && $self->customer_id ? 'customer' : 'vendor'; - _copy($self, $form, '', '', 0, qw(id type taxzone_id ordnumber quonumber invnumber donumber cusordnumber taxincluded shippingpoint shipvia notes intnotes curr cp_id + _copy($self, $form, '', '', 0, qw(id type taxzone_id ordnumber quonumber invnumber donumber cusordnumber taxincluded shippingpoint shipvia notes intnotes cp_id employee_id salesman_id closed department_id language_id payment_id delivery_customer_id delivery_vendor_id shipto_id proforma globalproject_id delivered transaction_description container_type accepted_by_customer invoice terms storno storno_id dunning_config_id orddate quodate reqdate gldate duedate deliverydate datepaid transdate)); - $form->{currency} = $form->{curr}; # curr is called currency in almost all forms + $form->{currency} = $form->{curr} = $self->currency_id ? $self->currency->name || '' : ''; if (_has($self, 'transdate')) { my $transdate_idx = ref($self) eq 'SL::DB::Order' ? ($self->quotation ? 'quodate' : 'orddate') diff --git a/SL/DB/Helper/Mappings.pm b/SL/DB/Helper/Mappings.pm index cc4d2abe1..f0b8be9af 100644 --- a/SL/DB/Helper/Mappings.pm +++ b/SL/DB/Helper/Mappings.pm @@ -55,6 +55,7 @@ my %lxoffice_package_names = ( csv_import_reports => 'csv_import_report', csv_import_report_rows => 'csv_import_report_row', csv_import_report_status => 'csv_import_report_status', + currencies => 'currency', custom_variable_configs => 'custom_variable_config', custom_variables => 'custom_variable', custom_variables_validity => 'custom_variable_validity', diff --git a/SL/DB/Helper/PriceTaxCalculator.pm b/SL/DB/Helper/PriceTaxCalculator.pm index 0801299fb..73613f89d 100644 --- a/SL/DB/Helper/PriceTaxCalculator.pm +++ b/SL/DB/Helper/PriceTaxCalculator.pm @@ -53,8 +53,9 @@ sub calculate_prices_and_taxes { sub _get_exchangerate { my ($self, $data, %params) = @_; - if (($self->curr || '') ne SL::DB::Default->get_default_currency) { - $data->{exchangerate} = $::form->check_exchangerate(\%::myconfig, $self->curr, $self->transdate, $data->{is_sales} ? 'buy' : 'sell'); + my $currency = $self->currency_id ? $self->currency->name || '' : ''; + if ($currency ne SL::DB::Default->get_default_currency) { + $data->{exchangerate} = $::form->check_exchangerate(\%::myconfig, $currency, $self->transdate, $data->{is_sales} ? 'buy' : 'sell'); $data->{exchangerate} ||= $params{exchangerate}; } $data->{exchangerate} ||= 1; diff --git a/SL/DB/Invoice.pm b/SL/DB/Invoice.pm index 09c7f86e7..d93de711a 100644 --- a/SL/DB/Invoice.pm +++ b/SL/DB/Invoice.pm @@ -98,9 +98,9 @@ sub new_from { my $terms = $source->can('payment_id') && $source->payment_id ? $source->payment_terms->terms_netto : 0; - my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes curr salesman_id cusordnumber ordnumber quonumber + my %args = ( map({ ( $_ => $source->$_ ) } qw(customer_id taxincluded shippingpoint shipvia notes intnotes salesman_id cusordnumber ordnumber quonumber department_id cp_id language_id payment_id delivery_customer_id delivery_vendor_id taxzone_id shipto_id - globalproject_id transaction_description)), + globalproject_id transaction_description currency_id)), transdate => DateTime->today_local, gldate => DateTime->today_local, duedate => DateTime->today_local->add(days => $terms * 1), diff --git a/SL/DB/MetaSetup/Currency.pm b/SL/DB/MetaSetup/Currency.pm new file mode 100644 index 000000000..705467f03 --- /dev/null +++ b/SL/DB/MetaSetup/Currency.pm @@ -0,0 +1,23 @@ +# This file has been auto-generated. Do not modify it; it will be overwritten +# by rose_auto_create_model.pl automatically. +package SL::DB::Currency; + +use strict; + +use base qw(SL::DB::Object); + +__PACKAGE__->meta->setup( + table => 'currencies', + + columns => [ + id => { type => 'serial', not_null => 1 }, + name => { type => 'text', not_null => 1 }, + ], + + primary_key_columns => [ 'id' ], + + unique_key => [ 'name' ], +); + +1; +; diff --git a/SL/DB/MetaSetup/Customer.pm b/SL/DB/MetaSetup/Customer.pm index 82d808cf9..0bb7cd96b 100644 --- a/SL/DB/MetaSetup/Customer.pm +++ b/SL/DB/MetaSetup/Customer.pm @@ -53,8 +53,8 @@ __PACKAGE__->meta->setup( iban => { type => 'varchar', length => 100 }, bic => { type => 'varchar', length => 100 }, direct_debit => { type => 'boolean', default => 'false' }, - curr => { type => 'text' }, taxincluded_checked => { type => 'boolean' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], @@ -67,6 +67,11 @@ __PACKAGE__->meta->setup( key_columns => { business_id => 'id' }, }, + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + language_obj => { class => 'SL::DB::Language', key_columns => { language_id => 'id' }, diff --git a/SL/DB/MetaSetup/Default.pm b/SL/DB/MetaSetup/Default.pm index bb174ebaa..7b1df394f 100644 --- a/SL/DB/MetaSetup/Default.pm +++ b/SL/DB/MetaSetup/Default.pm @@ -20,7 +20,6 @@ __PACKAGE__->meta->setup( weightunit => { type => 'varchar', length => 5 }, businessnumber => { type => 'text' }, version => { type => 'varchar', length => 8 }, - curr => { type => 'text' }, closedto => { type => 'date' }, revtrans => { type => 'boolean', default => 'false' }, ponumber => { type => 'text' }, @@ -68,9 +67,19 @@ __PACKAGE__->meta->setup( ar_show_mark_as_paid => { type => 'boolean', default => 'true' }, ap_show_mark_as_paid => { type => 'boolean', default => 'true' }, assemblynumber => { type => 'text' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], + + allow_inline_column_values => 1, + + foreign_keys => [ + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + ], ); 1; diff --git a/SL/DB/MetaSetup/DeliveryOrder.pm b/SL/DB/MetaSetup/DeliveryOrder.pm index 523312160..86cdc6406 100644 --- a/SL/DB/MetaSetup/DeliveryOrder.pm +++ b/SL/DB/MetaSetup/DeliveryOrder.pm @@ -39,7 +39,7 @@ __PACKAGE__->meta->setup( taxzone_id => { type => 'integer' }, taxincluded => { type => 'boolean' }, terms => { type => 'integer' }, - curr => { type => 'text' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], @@ -52,6 +52,11 @@ __PACKAGE__->meta->setup( key_columns => { cp_id => 'cp_id' }, }, + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + customer => { class => 'SL::DB::Customer', key_columns => { customer_id => 'id' }, diff --git a/SL/DB/MetaSetup/Exchangerate.pm b/SL/DB/MetaSetup/Exchangerate.pm index 44bce842b..ddd09438b 100644 --- a/SL/DB/MetaSetup/Exchangerate.pm +++ b/SL/DB/MetaSetup/Exchangerate.pm @@ -10,18 +10,25 @@ __PACKAGE__->meta->setup( table => 'exchangerate', columns => [ - curr => { type => 'text' }, - transdate => { type => 'date' }, - buy => { type => 'numeric', precision => 5, scale => 15 }, - sell => { type => 'numeric', precision => 5, scale => 15 }, - itime => { type => 'timestamp', default => 'now()' }, - mtime => { type => 'timestamp' }, - id => { type => 'serial', not_null => 1 }, + transdate => { type => 'date' }, + buy => { type => 'numeric', precision => 5, scale => 15 }, + sell => { type => 'numeric', precision => 5, scale => 15 }, + itime => { type => 'timestamp', default => 'now()' }, + mtime => { type => 'timestamp' }, + id => { type => 'serial', not_null => 1 }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], allow_inline_column_values => 1, + + foreign_keys => [ + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + ], ); 1; diff --git a/SL/DB/MetaSetup/Invoice.pm b/SL/DB/MetaSetup/Invoice.pm index b01b1417d..2a0e50ee2 100644 --- a/SL/DB/MetaSetup/Invoice.pm +++ b/SL/DB/MetaSetup/Invoice.pm @@ -26,7 +26,6 @@ __PACKAGE__->meta->setup( shippingpoint => { type => 'text' }, terms => { type => 'integer', default => '0' }, notes => { type => 'text' }, - curr => { type => 'text' }, ordnumber => { type => 'text' }, employee_id => { type => 'integer' }, quonumber => { type => 'text' }, @@ -57,6 +56,7 @@ __PACKAGE__->meta->setup( donumber => { type => 'text' }, invnumber_for_credit_note => { type => 'text' }, direct_debit => { type => 'boolean', default => 'false' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], @@ -69,6 +69,11 @@ __PACKAGE__->meta->setup( key_columns => { cp_id => 'cp_id' }, }, + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + customer => { class => 'SL::DB::Customer', key_columns => { customer_id => 'id' }, diff --git a/SL/DB/MetaSetup/Order.pm b/SL/DB/MetaSetup/Order.pm index f68c276ef..063ca0b0f 100644 --- a/SL/DB/MetaSetup/Order.pm +++ b/SL/DB/MetaSetup/Order.pm @@ -21,7 +21,6 @@ __PACKAGE__->meta->setup( taxincluded => { type => 'boolean' }, shippingpoint => { type => 'text' }, notes => { type => 'text' }, - curr => { type => 'character', length => 3 }, employee_id => { type => 'integer' }, closed => { type => 'boolean', default => 'false' }, quotation => { type => 'boolean', default => 'false' }, @@ -43,9 +42,10 @@ __PACKAGE__->meta->setup( delivered => { type => 'boolean', default => 'false' }, globalproject_id => { type => 'integer' }, salesman_id => { type => 'integer' }, - transaction_description => { type => 'text' }, marge_total => { type => 'numeric', precision => 5, scale => 15 }, marge_percent => { type => 'numeric', precision => 5, scale => 15 }, + transaction_description => { type => 'text' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], @@ -58,6 +58,11 @@ __PACKAGE__->meta->setup( key_columns => { cp_id => 'cp_id' }, }, + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + customer => { class => 'SL::DB::Customer', key_columns => { customer_id => 'id' }, diff --git a/SL/DB/MetaSetup/PurchaseInvoice.pm b/SL/DB/MetaSetup/PurchaseInvoice.pm index bee7eb2ae..e484fb933 100644 --- a/SL/DB/MetaSetup/PurchaseInvoice.pm +++ b/SL/DB/MetaSetup/PurchaseInvoice.pm @@ -23,7 +23,6 @@ __PACKAGE__->meta->setup( duedate => { type => 'date' }, invoice => { type => 'boolean', default => 'false' }, ordnumber => { type => 'text' }, - curr => { type => 'text' }, notes => { type => 'text' }, employee_id => { type => 'integer' }, quonumber => { type => 'text' }, @@ -44,6 +43,8 @@ __PACKAGE__->meta->setup( transaction_description => { type => 'text' }, storno_id => { type => 'integer' }, direct_debit => { type => 'boolean', default => 'false' }, + deliverydate => { type => 'date' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], @@ -56,6 +57,11 @@ __PACKAGE__->meta->setup( key_columns => { cp_id => 'cp_id' }, }, + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + department => { class => 'SL::DB::Department', key_columns => { department_id => 'id' }, diff --git a/SL/DB/MetaSetup/Vendor.pm b/SL/DB/MetaSetup/Vendor.pm index d8854fab5..14fb4a929 100644 --- a/SL/DB/MetaSetup/Vendor.pm +++ b/SL/DB/MetaSetup/Vendor.pm @@ -52,7 +52,7 @@ __PACKAGE__->meta->setup( iban => { type => 'varchar', length => 100 }, bic => { type => 'varchar', length => 100 }, direct_debit => { type => 'boolean', default => 'false' }, - curr => { type => 'text' }, + currency_id => { type => 'integer', not_null => 1 }, ], primary_key_columns => [ 'id' ], @@ -65,6 +65,11 @@ __PACKAGE__->meta->setup( key_columns => { business_id => 'id' }, }, + currency => { + class => 'SL::DB::Currency', + key_columns => { currency_id => 'id' }, + }, + language_obj => { class => 'SL::DB::Language', key_columns => { language_id => 'id' }, diff --git a/SL/DB/VC.pm b/SL/DB/VC.pm index 44b2dbb09..c41f28788 100644 --- a/SL/DB/VC.pm +++ b/SL/DB/VC.pm @@ -26,7 +26,7 @@ SQL $query = <{id}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|; + $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id, currency_id) VALUES (?, '', ?, (SELECT currency_id FROM defaults LIMIT 1))|; do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id})); } @@ -350,7 +350,7 @@ sub save { shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?, delivered = ?, department_id = ?, language_id = ?, shipto_id = ?, globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?, - is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, curr = ? + is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, currency_id = (SELECT id FROM currencies WHERE name = ?) WHERE id = ?|; @values = ($form->{donumber}, $form->{ordnumber}, @@ -364,7 +364,7 @@ sub save { conv_i($form->{salesman_id}), conv_i($form->{cp_id}), $form->{transaction_description}, $form->{type} =~ /^sales/ ? 't' : 'f', - conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), substr($form->{currency}, 0, 3), + conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), $form->{currency}, conv_i($form->{id})); do_query($form, $dbh, $query, @values); @@ -618,7 +618,7 @@ sub retrieve { d.description AS department, dord.language_id, dord.shipto_id, dord.globalproject_id, dord.delivered, dord.transaction_description, - dord.taxzone_id, dord.taxincluded, dord.terms, dord.curr AS currency + dord.taxzone_id, dord.taxincluded, dord.terms, (SELECT cu.name FROM currencies cu WHERE cu.id=dord.currency_id) AS currency FROM delivery_orders dord JOIN ${vc} cv ON (dord.${vc}_id = cv.id) LEFT JOIN employee e ON (dord.employee_id = e.id) @@ -640,9 +640,6 @@ sub retrieve { } $sth->finish(); - # remove any trailing whitespace - $form->{currency} =~ s/\s*$//; - $form->{donumber_array} =~ s/\s*$//g; $form->{saved_donumber} = $form->{donumber}; diff --git a/SL/Form.pm b/SL/Form.pm index 018526ac6..30c8bd425 100644 --- a/SL/Form.pm +++ b/SL/Form.pm @@ -1482,19 +1482,17 @@ sub update_exchangerate { $main::lxdebug->leave_sub(); return; } - $query = qq|SELECT curr FROM defaults|; - - my ($currency) = selectrow_query($self, $dbh, $query); - my ($defaultcurrency) = split m/:/, $currency; + $query = qq|SELECT name AS curr FROM currencies WHERE id=(SELECT currency_id FROM defaults)|; + my ($defaultcurrency) = selectrow_query($self, $dbh, $query); if ($curr eq $defaultcurrency) { $main::lxdebug->leave_sub(); return; } - $query = qq|SELECT e.curr FROM exchangerate e - WHERE e.curr = ? AND e.transdate = ? + $query = qq|SELECT e.currency_id FROM exchangerate e + WHERE e.currency_id = (SELECT cu.id FROM currencies cu WHERE cu.name=?) AND e.transdate = ? FOR UPDATE|; my $sth = prepare_execute_query($self, $dbh, $query, $curr, $transdate); @@ -1520,12 +1518,12 @@ sub update_exchangerate { if ($sth->fetchrow_array) { $query = qq|UPDATE exchangerate SET $set - WHERE curr = ? + WHERE currency_id = (SELECT id FROM currencies WHERE name = ?) AND transdate = ?|; } else { - $query = qq|INSERT INTO exchangerate (curr, buy, sell, transdate) - VALUES (?, $buy, $sell, ?)|; + $query = qq|INSERT INTO exchangerate (currency_id, buy, sell, transdate) + VALUES ((SELECT id FROM currencies WHERE name = ?), $buy, $sell, ?)|; } $sth->finish; do_query($self, $dbh, $query, $curr, $transdate); @@ -1565,18 +1563,17 @@ sub get_exchangerate { return 1; } - $query = qq|SELECT curr FROM defaults|; + $query = qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|; - my ($currency) = selectrow_query($self, $dbh, $query); - my ($defaultcurrency) = split m/:/, $currency; + my ($defaultcurrency) = selectrow_query($self, $dbh, $query); - if ($currency eq $defaultcurrency) { + if ($curr eq $defaultcurrency) { $main::lxdebug->leave_sub(); return 1; } $query = qq|SELECT e.$fld FROM exchangerate e - WHERE e.curr = ? AND e.transdate = ?|; + WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|; my ($exchangerate) = selectrow_query($self, $dbh, $query, $curr, $transdate); @@ -1609,7 +1606,7 @@ sub check_exchangerate { my $dbh = $self->get_standard_dbh($myconfig); my $query = qq|SELECT e.$fld FROM exchangerate e - WHERE e.curr = ? AND e.transdate = ?|; + WHERE e.currency_id = (SELECT id FROM currencies WHERE name = ?) AND e.transdate = ?|; my ($exchangerate) = selectrow_query($self, $dbh, $query, $currency, $transdate); @@ -1624,11 +1621,10 @@ sub get_all_currencies { my $self = shift; my $myconfig = shift || \%::myconfig; my $dbh = $self->get_standard_dbh($myconfig); + my @currencies =(); - my $query = qq|SELECT curr FROM defaults|; - - my ($curr) = selectrow_query($self, $dbh, $query); - my @currencies = grep { $_ } map { s/\s//g; $_ } split m/:/, $curr; + my $query = qq|SELECT name FROM currencies|; + my @currencies = map { $_->{name} } selectall_hashref_query($self, $dbh, $query); $main::lxdebug->leave_sub(); @@ -1639,11 +1635,14 @@ sub get_default_currency { $main::lxdebug->enter_sub(); my ($self, $myconfig) = @_; - my @currencies = $self->get_all_currencies($myconfig); + my $dbh = $self->get_standard_dbh($myconfig); + my $query = qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|; + + my ($defaultcurrency) = selectrow_query($self, $dbh, $query); $main::lxdebug->leave_sub(); - return $currencies[0]; + return $defaultcurrency; } sub set_payment_options { @@ -2183,9 +2182,7 @@ $main::lxdebug->enter_sub(); $key = "all_currencies" unless ($key); - my $query = qq|SELECT curr AS currency FROM defaults|; - - $self->{$key} = [split(/\:/ , selectfirst_hashref_query($self, $dbh, $query)->{currency})]; + $self->{$key} = [$self->get_all_currencies()]; $main::lxdebug->leave_sub(); } @@ -2696,7 +2693,7 @@ sub create_links { $query = qq|SELECT a.cp_id, a.invnumber, a.transdate, a.${table}_id, a.datepaid, - a.duedate, a.ordnumber, a.taxincluded, a.curr AS currency, a.notes, + a.duedate, a.ordnumber, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.notes, a.intnotes, a.department_id, a.amount AS oldinvtotal, a.paid AS oldtotalpaid, a.employee_id, a.gldate, a.type, a.globalproject_id, ${extra_columns} @@ -2714,9 +2711,6 @@ sub create_links { $self->{$key} = $ref->{$key}; } - # remove any trailing whitespace - $self->{currency} =~ s/\s*$//; - my $transdate = "current_date"; if ($self->{transdate}) { $transdate = $dbh->quote($self->{transdate}); @@ -2800,9 +2794,11 @@ sub create_links { } $sth->finish; + #check das: $query = qq|SELECT - d.curr AS currencies, d.closedto, d.revtrans, + d.closedto, d.revtrans, + (SELECT cu.name FROM currencies cu WHERE cu.id=d.currency_id) AS defaultcurrency, (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno FROM defaults d|; @@ -2814,7 +2810,8 @@ sub create_links { # get date $query = qq|SELECT - current_date AS transdate, d.curr AS currencies, d.closedto, d.revtrans, + current_date AS transdate, d.closedto, d.revtrans, + (SELECT cu.name FROM currencies cu WHERE cu.id=d.currency_id) AS defaultcurrency, (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno FROM defaults d|; @@ -2824,7 +2821,7 @@ sub create_links { if ($self->{"$self->{vc}_id"}) { # only setup currency - ($self->{currency}) = split(/:/, $self->{currencies}) if !$self->{currency}; + ($self->{currency}) = $self->{defaultcurrency} if !$self->{currency}; } else { @@ -2849,19 +2846,17 @@ sub lastname_used { my ($arap, $where); $table = $table eq "customer" ? "customer" : "vendor"; - my %column_map = ("a.curr" => "currency", - "a.${table}_id" => "${table}_id", + my %column_map = ("a.${table}_id" => "${table}_id", "a.department_id" => "department_id", "d.description" => "department", "ct.name" => $table, - "ct.curr" => "cv_curr", + "cu.name" => "currency", "current_date + ct.terms" => "duedate", ); if ($self->{type} =~ /delivery_order/) { $arap = 'delivery_orders'; - delete $column_map{"a.curr"}; - delete $column_map{"ct.curr"}; + delete $column_map{"cu.currency"}; } elsif ($self->{type} =~ /_order/) { $arap = 'oe'; @@ -2890,18 +2885,12 @@ sub lastname_used { FROM $arap a LEFT JOIN $table ct ON (a.${table}_id = ct.id) LEFT JOIN department d ON (a.department_id = d.id) + LEFT JOIN currencies cu ON (cu.id=ct.currency_id) WHERE a.id = ?|; my $ref = selectfirst_hashref_query($self, $dbh, $query, $trans_id); map { $self->{$_} = $ref->{$_} } values %column_map; - # remove any trailing whitespace - $self->{currency} =~ s/\s*$// if $self->{currency}; - $self->{cv_curr} =~ s/\s*$// if $self->{cv_curr}; - - # if customer/vendor currency is set use this - $self->{currency} = $self->{cv_curr} if $self->{cv_curr}; - $main::lxdebug->leave_sub(); } diff --git a/SL/IR.pm b/SL/IR.pm index 06f84abbc..5c335eccc 100644 --- a/SL/IR.pm +++ b/SL/IR.pm @@ -57,6 +57,7 @@ sub post_invoice { # connect to database, turn off autocommit my $dbh = $provided_dbh ? $provided_dbh : $form->dbconnect_noauto($myconfig); $form->{defaultcurrency} = $form->get_default_currency($myconfig); + my $defaultcurrency = $form->{defaultcurrency}; my $ic_cvar_configs = CVar->get_configs(module => 'IC', dbh => $dbh); @@ -70,18 +71,16 @@ sub post_invoice { my $all_units = AM->retrieve_units($myconfig, $form); +#markierung if (!$payments_only) { if ($form->{id}) { &reverse_invoice($dbh, $form); } else { ($form->{id}) = selectrow_query($form, $dbh, qq|SELECT nextval('glid')|); - do_query($form, $dbh, qq|INSERT INTO ap (id, invnumber) VALUES (?, '')|, $form->{id}); + do_query($form, $dbh, qq|INSERT INTO ap (id, invnumber, currency_id) VALUES (?, '', (SELECT id FROM currencies WHERE name=?))|, $form->{id}, $form->{currency}); } } - my ($currencies) = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|); - my $defaultcurrency = (split m/:/, $currencies)[0]; - if ($form->{currency} eq $defaultcurrency) { $form->{exchangerate} = 1; } else { @@ -686,7 +685,7 @@ sub post_invoice { orddate = ?, quodate = ?, vendor_id = ?, amount = ?, netamount = ?, paid = ?, duedate = ?, invoice = ?, taxzone_id = ?, notes = ?, taxincluded = ?, - intnotes = ?, curr = ?, storno_id = ?, storno = ?, + intnotes = ?, storno_id = ?, storno = ?, cp_id = ?, employee_id = ?, department_id = ?, globalproject_id = ?, direct_debit = ? WHERE id = ?|; @@ -695,7 +694,7 @@ sub post_invoice { conv_date($form->{orddate}), conv_date($form->{quodate}), conv_i($form->{vendor_id}), $amount, $netamount, $form->{paid}, conv_date($form->{duedate}), '1', $taxzone_id, $form->{notes}, $form->{taxincluded} ? 't' : 'f', - $form->{intnotes}, $form->{currency}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f', + $form->{intnotes}, conv_i($form->{storno_id}), $form->{storno} ? 't' : 'f', conv_i($form->{cp_id}), conv_i($form->{employee_id}), conv_i($form->{department_id}), conv_i($form->{globalproject_id}), $form->{direct_debit} ? 't' : 'f', @@ -925,8 +924,7 @@ sub retrieve_invoice { (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno, (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno, (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, - (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, - d.curr AS currencies + (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno $q_invdate FROM defaults d|; $ref = selectfirst_hashref_query($form, $dbh, $query); @@ -943,15 +941,12 @@ sub retrieve_invoice { $query = qq|SELECT cp_id, invnumber, transdate AS invdate, duedate, orddate, quodate, globalproject_id, ordnumber, quonumber, paid, taxincluded, notes, taxzone_id, storno, gldate, - intnotes, curr AS currency, direct_debit + intnotes, (SELECT cu.name FROM currencies cu WHERE cu.id=ap.currency_id) AS currency, direct_debit FROM ap WHERE id = ?|; $ref = selectfirst_hashref_query($form, $dbh, $query, conv_i($form->{id})); map { $form->{$_} = $ref->{$_} } keys %$ref; - # remove any trailing whitespace - $form->{currency} =~ s/\s*$//; - $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "sell"); # get shipto @@ -1093,21 +1088,19 @@ sub get_vendor { v.id AS vendor_id, v.name AS vendor, v.discount as vendor_discount, v.creditlimit, v.terms, v.notes AS intnotes, v.email, v.cc, v.bcc, v.language_id, v.payment_id, - v.street, v.zipcode, v.city, v.country, v.taxzone_id, v.curr, v.direct_debit, + v.street, v.zipcode, v.city, v.country, v.taxzone_id, cu.name AS curr, v.direct_debit, $duedate + COALESCE(pt.terms_netto, 0) AS duedate, b.description AS business FROM vendor v LEFT JOIN business b ON (b.id = v.business_id) LEFT JOIN payment_terms pt ON (v.payment_id = pt.id) + LEFT JOIN currencies cu ON (v.currency_id = cu.id) WHERE 1=1 $where|; my $ref = selectfirst_hashref_query($form, $dbh, $query, @values); map { $params->{$_} = $ref->{$_} } keys %$ref; - # remove any trailing whitespace - $form->{curr} =~ s/\s*$//; - - # use vendor currency if not empty - $form->{currency} = $form->{curr} if $form->{curr}; + # use vendor currency + $form->{currency} = $form->{curr}; $params->{creditremaining} = $params->{creditlimit}; @@ -1118,7 +1111,7 @@ sub get_vendor { $query = qq|SELECT o.amount, (SELECT e.sell FROM exchangerate e - WHERE (e.curr = o.curr) + WHERE (e.currency_id = o.currency_id) AND (e.transdate = o.transdate)) AS exch FROM oe o WHERE (o.vendor_id = ?) AND (o.quotation = '0') AND (o.closed = '0')|; @@ -1393,9 +1386,10 @@ sub vendor_details { # fax and phone and email as vendor* my $query = qq|SELECT ct.*, cp.*, ct.notes as vendornotes, phone as vendorphone, fax as vendorfax, email as vendoremail, - ct.curr AS currency + cu.name AS currency FROM vendor ct LEFT JOIN contacts cp ON (ct.id = cp.cp_cv_id) + LEFT JOIN currencies cu ON (ct.currency_id = cu.id) WHERE (ct.id = ?) $contact ORDER BY cp.cp_id LIMIT 1|; @@ -1412,8 +1406,6 @@ sub vendor_details { } map { $form->{$_} = $ref->{$_} } keys %$ref; - # remove any trailing whitespace - $form->{currency} =~ s/\s*$// if ($form->{currency}); my $custom_variables = CVar->get_custom_variables('dbh' => $dbh, 'module' => 'CT', diff --git a/SL/IS.pm b/SL/IS.pm index 9ecc64818..a844b7327 100644 --- a/SL/IS.pm +++ b/SL/IS.pm @@ -458,9 +458,10 @@ sub customer_details { my $query = qq|SELECT ct.*, cp.*, ct.notes as customernotes, ct.phone AS customerphone, ct.fax AS customerfax, ct.email AS customeremail, - ct.curr AS currency + cu.name AS currency FROM customer ct LEFT JOIN contacts cp on ct.id = cp.cp_cv_id + LEFT JOIN currencies cu ON (ct.currency_id = cu.id) WHERE (ct.id = ?) $where ORDER BY cp.cp_id LIMIT 1|; @@ -478,9 +479,6 @@ sub customer_details { map { $form->{$_} = $ref->{$_} } keys %$ref; - # remove any trailing whitespace - $form->{currency} =~ s/\s*$// if ($form->{currency}); - if ($form->{delivery_customer_id}) { $query = qq|SELECT *, notes as customernotes @@ -536,6 +534,8 @@ sub post_invoice { } $form->{defaultcurrency} = $form->get_default_currency($myconfig); + my $defaultcurrency = $form->{defaultcurrency}; + # Seit neuestem wird die department_id schon übergeben UND $form->department nicht mehr # korrekt zusammengebaut. Sehr wahrscheinlich beim Umstieg auf T8 kaputt gegangen # Ich lass den Code von 2005 erstmal noch stehen ;-) jb 03-2011 @@ -556,8 +556,8 @@ sub post_invoice { $query = qq|SELECT nextval('glid')|; ($form->{"id"}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO ar (id, invnumber) VALUES (?, ?)|; - do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"}); + $query = qq|INSERT INTO ar (id, invnumber, currency_id) VALUES (?, ?, (SELECT id FROM currencies WHERE name=?))|; + do_query($form, $dbh, $query, $form->{"id"}, $form->{"id"}, $form->{currency}); if (!$form->{invnumber}) { $form->{invnumber} = @@ -570,9 +570,6 @@ sub post_invoice { my ($netamount, $invoicediff) = (0, 0); my ($amount, $linetotal, $lastincomeaccno); - my ($currencies) = selectfirst_array_query($form, $dbh, qq|SELECT curr FROM defaults|); - my $defaultcurrency = (split m/:/, $currencies)[0]; - if ($form->{currency} eq $defaultcurrency) { $form->{exchangerate} = 1; } else { @@ -1085,7 +1082,8 @@ sub post_invoice { amount = ?, netamount = ?, paid = ?, duedate = ?, deliverydate = ?, invoice = ?, shippingpoint = ?, shipvia = ?, terms = ?, notes = ?, intnotes = ?, - curr = ?, department_id = ?, payment_id = ?, taxincluded = ?, + currency_id = (SELECT id FROM currencies WHERE name = ?), + department_id = ?, payment_id = ?, taxincluded = ?, type = ?, language_id = ?, taxzone_id = ?, shipto_id = ?, employee_id = ?, salesman_id = ?, storno_id = ?, storno = ?, cp_id = ?, marge_total = ?, marge_percent = ?, @@ -1559,8 +1557,7 @@ sub retrieve_invoice { (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno, (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno, (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, - (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, - d.curr AS currencies + (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno ${query_transdate} FROM defaults d|; @@ -1579,7 +1576,7 @@ sub retrieve_invoice { a.orddate, a.quodate, a.globalproject_id, a.transdate AS invdate, a.deliverydate, a.paid, a.storno, a.gldate, a.shippingpoint, a.shipvia, a.terms, a.notes, a.intnotes, a.taxzone_id, - a.duedate, a.taxincluded, a.curr AS currency, a.shipto_id, a.cp_id, + a.duedate, a.taxincluded, (SELECT cu.name FROM currencies cu WHERE cu.id=a.currency_id) AS currency, a.shipto_id, a.cp_id, a.employee_id, a.salesman_id, a.payment_id, a.language_id, a.delivery_customer_id, a.delivery_vendor_id, a.type, a.transaction_description, a.donumber, a.invnumber_for_credit_note, @@ -1591,9 +1588,6 @@ sub retrieve_invoice { $ref = selectfirst_hashref_query($form, $dbh, $query, $id); map { $form->{$_} = $ref->{$_} } keys %{ $ref }; - # remove any trailing whitespace - $form->{currency} =~ s/\s*$//; - $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{invdate}, "buy"); # get shipto @@ -1758,13 +1752,14 @@ sub get_customer { c.id AS customer_id, c.name AS customer, c.discount as customer_discount, c.creditlimit, c.terms, c.email, c.cc, c.bcc, c.language_id, c.payment_id, c.street, c.zipcode, c.city, c.country, - c.notes AS intnotes, c.klass as customer_klass, c.taxzone_id, c.salesman_id, c.curr, + c.notes AS intnotes, c.klass as customer_klass, c.taxzone_id, c.salesman_id, cu.name AS curr, c.taxincluded_checked, c.direct_debit, $duedate + COALESCE(pt.terms_netto, 0) AS duedate, b.discount AS tradediscount, b.description AS business FROM customer c LEFT JOIN business b ON (b.id = c.business_id) LEFT JOIN payment_terms pt ON ($payment_id (c.payment_id = pt.id)) + LEFT JOIN currencies cu ON (c.currency_id=cu.id) WHERE c.id = ?|; push @values, $cid; $ref = selectfirst_hashref_query($form, $dbh, $query, @values); @@ -1773,11 +1768,8 @@ sub get_customer { map { $form->{$_} = $ref->{$_} } keys %$ref; - # remove any trailing whitespace - $form->{curr} =~ s/\s*$//; - - # use customer currency if not empty - $form->{currency} = $form->{curr} if $form->{curr}; + # use customer currency + $form->{currency} = $form->{curr}; $query = qq|SELECT sum(amount - paid) AS dunning_amount @@ -1806,7 +1798,7 @@ sub get_customer { $query = qq|SELECT o.amount, (SELECT e.buy FROM exchangerate e - WHERE e.curr = o.curr + WHERE e.currency_id = o.currency_id AND e.transdate = o.transdate) FROM oe o WHERE o.customer_id = ? diff --git a/SL/InstanceConfiguration.pm b/SL/InstanceConfiguration.pm index 1e9c6a10f..850e50cb3 100644 --- a/SL/InstanceConfiguration.pm +++ b/SL/InstanceConfiguration.pm @@ -15,9 +15,9 @@ sub init { $self->{data} = selectfirst_hashref_query($::form, $::form->get_standard_dbh, qq|SELECT * FROM defaults|); - my $curr = $self->{data}->{curr} || ''; - $curr =~ s/\s+//g; - $self->{currencies} = [ split m/:/, $curr ]; + #To get all currencies and the default currency: + ($self->{data}->{curr}) = selectrow_query($::form, $::form->get_standard_dbh, qq|SELECT name AS curr FROM currencies WHERE id = (SELECT currency_id FROM defaults)|); + $self->{currencies} = [ map { $_->{name} } selectall_hashref_query($::form, $::form->get_standard_dbh, qq|SELECT name FROM currencies ORDER BY id|) ]; return $self; } @@ -25,13 +25,13 @@ sub init { sub get_default_currency { my ($self) = @_; - return ($self->get_currencies)[0]; + return $self->{data}->{curr}; } sub get_currencies { my ($self) = @_; - return $self->{currencies} ? @{ $self->{currencies} } : (); + return @{ $self->{currencies} }; } sub get_accounting_method { diff --git a/SL/OE.pm b/SL/OE.pm index 3b3845c50..d90b6aa00 100644 --- a/SL/OE.pm +++ b/SL/OE.pm @@ -91,7 +91,7 @@ sub transactions { qq|JOIN $vc ct ON (o.${vc}_id = ct.id) | . qq|LEFT JOIN employee e ON (o.employee_id = e.id) | . qq|LEFT JOIN employee s ON (o.salesman_id = s.id) | . - qq|LEFT JOIN exchangerate ex ON (ex.curr = o.curr | . + qq|LEFT JOIN exchangerate ex ON (ex.currency_id = o.currency_id | . qq| AND ex.transdate = o.transdate) | . qq|LEFT JOIN project pr ON (o.globalproject_id = pr.id) | . qq|$periodic_invoices_joins | . @@ -311,7 +311,7 @@ sub save { $query = qq|SELECT nextval('id')|; ($form->{id}) = selectrow_query($form, $dbh, $query); - $query = qq|INSERT INTO oe (id, ordnumber, employee_id) VALUES (?, '', ?)|; + $query = qq|INSERT INTO oe (id, ordnumber, employee_id, currency_id) VALUES (?, '', ?, (SELECT currency_id FROM defaults))|; do_query($form, $dbh, $query, $form->{id}, $form->{employee_id}); } @@ -494,7 +494,7 @@ sub save { qq|UPDATE oe SET ordnumber = ?, quonumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?, customer_id = ?, amount = ?, netamount = ?, reqdate = ?, taxincluded = ?, - shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, curr = ?, closed = ?, + shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, currency_id = (SELECT id FROM currencies WHERE name=?), closed = ?, delivered = ?, proforma = ?, quotation = ?, department_id = ?, language_id = ?, taxzone_id = ?, shipto_id = ?, payment_id = ?, delivery_vendor_id = ?, delivery_customer_id = ?, globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?, marge_total = ?, marge_percent = ? @@ -506,7 +506,7 @@ sub save { $amount, $netamount, conv_date($reqdate), $form->{taxincluded} ? 't' : 'f', $form->{shippingpoint}, $form->{shipvia}, $form->{notes}, $form->{intnotes}, - substr($form->{currency}, 0, 3), $form->{closed} ? 't' : 'f', + $form->{currency}, $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f", $form->{proforma} ? 't' : 'f', $quotation, conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{taxzone_id}), @@ -759,14 +759,13 @@ sub retrieve { (SELECT c.accno FROM chart c WHERE d.income_accno_id = c.id) AS income_accno, (SELECT c.accno FROM chart c WHERE d.expense_accno_id = c.id) AS expense_accno, (SELECT c.accno FROM chart c WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, - (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, - d.curr AS currencies + (SELECT c.accno FROM chart c WHERE d.fxloss_accno_id = c.id) AS fxloss_accno $query_add FROM defaults d|; my $ref = selectfirst_hashref_query($form, $dbh, $query); map { $form->{$_} = $ref->{$_} } keys %$ref; - ($form->{currency}) = split(/:/, $form->{currencies}) unless ($form->{currency}); + $form->{currency} = $form->get_default_currency($myconfig); # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure # we come from invoices, feel free. @@ -785,7 +784,7 @@ sub retrieve { $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate, o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes, - o.curr AS currency, e.name AS employee, o.employee_id, o.salesman_id, + (SELECT cu.name FROM currencies cu WHERE cu.id=o.currency_id) AS currency, e.name AS employee, o.employee_id, o.salesman_id, o.${vc}_id, cv.name AS ${vc}, o.amount AS invtotal, o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber, d.description AS department, o.payment_id, o.language_id, o.taxzone_id, @@ -807,9 +806,6 @@ sub retrieve { if ($ref) { map { $form->{$_} = $ref->{$_} } keys %$ref; - # remove any trailing whitespace - $form->{currency} =~ s/\s*$//; - $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber"}; # set all entries for multiple ids blank that yield different information diff --git a/SL/RP.pm b/SL/RP.pm index 65cb30295..095f7581c 100644 --- a/SL/RP.pm +++ b/SL/RP.pm @@ -1238,7 +1238,7 @@ sub aging { "duedate", invoice, ${arap}.id, date_part('days', now() - duedate) as overduedays, (SELECT $buysell FROM exchangerate - WHERE (${arap}.curr = exchangerate.curr) + WHERE (${arap}.currency_id = exchangerate.currency_id) AND (exchangerate.transdate = ${arap}.transdate)) AS exchangerate FROM ${arap}, ${ct} WHERE ((paid != amount) OR (datepaid > (date $todate) AND datepaid is not null)) diff --git a/SL/User.pm b/SL/User.pm index 46c35bf41..e11fda4ce 100644 --- a/SL/User.pm +++ b/SL/User.pm @@ -398,6 +398,8 @@ sub dbcreate { do_query($form, $dbh, $query, $form->{profit_determination}); $query = "UPDATE defaults SET inventory_system = ?"; do_query($form, $dbh, $query, $form->{inventory_system}); + $query = "UPDATE defaults SET curr = ?"; + do_query($form, $dbh, $query, $form->{defaultcurrency}); $dbh->disconnect; diff --git a/bin/mozilla/admin.pl b/bin/mozilla/admin.pl index 7f0b80de8..84c8b0699 100755 --- a/bin/mozilla/admin.pl +++ b/bin/mozilla/admin.pl @@ -780,6 +780,7 @@ sub dbcreate { my $locale = $main::locale; $form->isblank("db", $locale->text('Dataset missing!')); + $form->isblank("defaultcurrency", $locale->text('Default currency missing!')); User->dbcreate(\%$form); diff --git a/bin/mozilla/cp.pl b/bin/mozilla/cp.pl index a70c32eeb..b9aed484a 100644 --- a/bin/mozilla/cp.pl +++ b/bin/mozilla/cp.pl @@ -109,10 +109,9 @@ sub payment { # geben und hier reinparsen, oder besser multibox oder html auslagern? # Antwort: form->currency wird mit oldcurrency oder curr[0] überschrieben # Wofür macht das Sinn? - @curr = split(/:/, $form->{currencies}); - chomp $curr[0]; + @curr = $form->get_all_currencies(); $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} = - $curr[0]; + $form->get_default_currency(\%myconfig); # Entsprechend präventiv die Auswahlliste für Währungen # auch mit value= zusammenbauen (s.a. oben bugfix 1771) diff --git a/bin/mozilla/ic.pl b/bin/mozilla/ic.pl index 222a4d1d5..8200b0110 100644 --- a/bin/mozilla/ic.pl +++ b/bin/mozilla/ic.pl @@ -1489,8 +1489,7 @@ sub link_part { IC->create_links("IC", \%myconfig, \%$form); # currencies - map({ $form->{selectcurrency} .= "