X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FIR.pm;h=c85daa334086420d75a14f93ea874df8dd85f747;hb=5142828d6dea553b3e54e936a2f1af368170f660;hp=7518ed4b55571543c5bb386a0e36b079c89f1235;hpb=5067d7bd31514962af9730b33323b831d87164f8;p=kivitendo-erp.git diff --git a/SL/IR.pm b/SL/IR.pm index 7518ed4b5..c85daa334 100644 --- a/SL/IR.pm +++ b/SL/IR.pm @@ -25,7 +25,8 @@ # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1335, USA. #====================================================================== # # Inventory received module @@ -40,6 +41,7 @@ use SL::Common; use SL::CVar; use SL::DATEV qw(:CONSTANTS); use SL::DBUtils; +use SL::DB::Draft; use SL::DO; use SL::GenericTranslations; use SL::HTML::Restrict; @@ -47,24 +49,30 @@ use SL::IO; use SL::MoreCommon; use SL::DB::Default; use SL::DB::TaxZone; +use SL::DB::MakeModel; use SL::DB; +use SL::Presenter::Part qw(type_abbreviation classification_abbreviation); use List::Util qw(min); use strict; +use constant PCLASS_OK => 0; +use constant PCLASS_NOTFORSALE => 1; +use constant PCLASS_NOTFORPURCHASE => 2; sub post_invoice { - my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_; + my ($self, $myconfig, $form, $provided_dbh, %params) = @_; $main::lxdebug->enter_sub(); - my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, $payments_only); + my $rc = SL::DB->client->with_transaction(\&_post_invoice, $self, $myconfig, $form, $provided_dbh, %params); $::lxdebug->leave_sub; return $rc; } sub _post_invoice { - my ($self, $myconfig, $form, $provided_dbh, $payments_only) = @_; + my ($self, $myconfig, $form, $provided_dbh, %params) = @_; + my $payments_only = $params{payments_only}; my $dbh = $provided_dbh || SL::DB->client->dbh; my $restricter = SL::HTML::Restrict->create; @@ -584,6 +592,8 @@ SQL $form->{amount}{ $form->{id} }{ $form->{AP} } = $form->{paid} if $form->{amount}{$form->{id}}{$form->{AP}} == 0; + my %already_cleared = %{ $params{already_cleared} // {} }; + # record payments and offsetting AP for my $i (1 .. $form->{paidaccounts}) { if ($form->{"acc_trans_id_$i"} @@ -600,9 +610,16 @@ SQL $amount = $form->round_amount($form->{"paid_$i"} * $form->{exchangerate} + $paiddiff, 2) * -1; + my $new_cleared = !$form->{"acc_trans_id_$i"} ? 'f' + : !$already_cleared{$form->{"acc_trans_id_$i"}} ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{amount} != $form->{"paid_$i"} ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{accno} != $accno ? 'f' + : $already_cleared{$form->{"acc_trans_id_$i"}}->{cleared} ? 't' + : 'f'; + # record AP if ($form->{amount}{ $form->{id} }{ $form->{AP} } != 0) { - $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, tax_id, chart_link) + $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, taxkey, project_id, cleared, tax_id, chart_link) VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, (SELECT taxkey_id FROM taxkeys @@ -611,7 +628,7 @@ SQL WHERE accno = ?) AND startdate <= ? ORDER BY startdate DESC LIMIT 1), - ?, + ?, ?, (SELECT tax_id FROM taxkeys WHERE chart_id= (SELECT id @@ -621,7 +638,7 @@ SQL ORDER BY startdate DESC LIMIT 1), (SELECT link FROM chart WHERE accno = ?))|; @values = (conv_i($form->{id}), $form->{AP}, $amount, - $form->{"datepaid_$i"}, $form->{AP}, conv_date($form->{"datepaid_$i"}), $project_id, $form->{AP}, conv_date($form->{"datepaid_$i"}), $form->{AP}); + $form->{"datepaid_$i"}, $form->{AP}, conv_date($form->{"datepaid_$i"}), $project_id, $new_cleared, $form->{AP}, conv_date($form->{"datepaid_$i"}), $form->{AP}); do_query($form, $dbh, $query, @values); } @@ -629,7 +646,7 @@ SQL my $gldate = (conv_date($form->{"gldate_$i"}))? conv_date($form->{"gldate_$i"}) : conv_date($form->current_date($myconfig)); $query = - qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, taxkey, project_id, tax_id, chart_link) + qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate, gldate, source, memo, taxkey, project_id, cleared, tax_id, chart_link) VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, ?, (SELECT taxkey_id FROM taxkeys @@ -637,7 +654,7 @@ SQL FROM chart WHERE accno = ?) AND startdate <= ? ORDER BY startdate DESC LIMIT 1), - ?, + ?, ?, (SELECT tax_id FROM taxkeys WHERE chart_id= (SELECT id @@ -646,7 +663,7 @@ SQL ORDER BY startdate DESC LIMIT 1), (SELECT link FROM chart WHERE accno = ?))|; @values = (conv_i($form->{id}), $accno, $form->{"paid_$i"}, $form->{"datepaid_$i"}, - $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, conv_date($form->{"datepaid_$i"}), $project_id, $accno, conv_date($form->{"datepaid_$i"}), $accno); + $gldate, $form->{"source_$i"}, $form->{"memo_$i"}, $accno, conv_date($form->{"datepaid_$i"}), $project_id, $new_cleared, $accno, conv_date($form->{"datepaid_$i"}), $accno); do_query($form, $dbh, $query, @values); $exchangerate = 0; @@ -806,21 +823,19 @@ SQL do_query($form, $dbh, $query, @orphaned_ids); } + if ($form->{draft_id}) { + SL::DB::Manager::Draft->delete_all(where => [ id => delete($form->{draft_id}) ]); + } + # safety check datev export if ($::instance_conf->get_datev_check_on_purchase_invoice) { - # if we need department for kostenstelle in DATEV check - $form->{department} = SL::DB::Manager::Department->find_by(id => $form->{department_id})->description if $form->{department_id}; - my $transdate = $::form->{invdate} ? DateTime->from_lxoffice($::form->{invdate}) : undef; - $transdate ||= DateTime->today; my $datev = SL::DATEV->new( - exporttype => DATEV_ET_BUCHUNGEN, - format => DATEV_FORMAT_KNE, dbh => $dbh, trans_id => $form->{id}, ); - $datev->export; + $datev->generate_datev_data; if ($datev->errors) { die join "\n", $::locale->text('DATEV check returned errors:'), $datev->errors; @@ -1168,43 +1183,6 @@ sub get_vendor { } $sth->finish(); - if (!$params->{id} && $params->{type} !~ /_(order|quotation)/) { - # setup last accounts used - $query = - qq|SELECT c.id, c.accno, c.description, c.link, c.category - FROM chart c - JOIN acc_trans ac ON (ac.chart_id = c.id) - JOIN ap a ON (a.id = ac.trans_id) - WHERE (a.vendor_id = ?) - AND (NOT ((c.link LIKE '%_tax%') OR (c.link LIKE '%_paid%'))) - AND (a.id IN (SELECT max(a2.id) FROM ap a2 WHERE a2.vendor_id = ?))|; - my $refs = selectall_hashref_query($form, $dbh, $query, $vid, $vid); - - my $i = 0; - for $ref (@$refs) { - if ($ref->{category} eq 'E') { - $i++; - my ($tax_id, $rate); - if ($params->{initial_transdate}) { - my $tax_query = qq|SELECT tk.tax_id, t.rate FROM taxkeys tk - LEFT JOIN tax t ON (tk.tax_id = t.id) - WHERE (tk.chart_id = ?) AND (startdate <= ?) - ORDER BY tk.startdate DESC - LIMIT 1|; - ($tax_id, $rate) = selectrow_query($form, $dbh, $tax_query, $ref->{id}, $params->{initial_transdate}); - $params->{"taxchart_$i"} = "${tax_id}--${rate}"; - } - - $params->{"AP_amount_$i"} = "$ref->{accno}--$tax_id"; - } - - if ($ref->{category} eq 'L') { - $params->{APselected} = $params->{AP_1} = $ref->{accno}; - } - } - $params->{rowcount} = $i if ($i && !$params->{type}); - } - $main::lxdebug->leave_sub(); } @@ -1278,7 +1256,7 @@ sub retrieve_item { p.id, p.partnumber, p.description, p.lastcost AS sellprice, p.listprice, p.unit, p.part_type, p.onhand, p.formel, p.notes AS partnotes, p.notes AS longdescription, p.not_discountable, - p.inventory_accno_id, p.price_factor_id, + p.price_factor_id, p.ean, p.classification_id, @@ -1313,7 +1291,7 @@ sub retrieve_item { FROM taxzone_charts tc WHERE tc.taxzone_id = '$taxzone_id' and tc.buchungsgruppen_id = p.buchungsgruppen_id) = c3.id) LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id) - LEFT JOIN parts_classifications pt ON (pt.id = p.classification_id) + LEFT JOIN part_classifications pt ON (pt.id = p.classification_id) LEFT JOIN price_factors pfac ON (pfac.id = p.price_factor_id) WHERE $where|; my $sth = prepare_execute_query($form, $dbh, $query, @values); @@ -1332,7 +1310,7 @@ sub retrieve_item { map { push @{ $_ }, prepare_query($form, $dbh, $_->[0]) } @translation_queries; $form->{item_list} = []; - my $has_wrong_pclass = 0; + my $has_wrong_pclass = PCLASS_OK; while (my $ref = $sth->fetchrow_hashref("NAME_lc")) { if ($mm_by_id{$ref->{id}}) { @@ -1343,11 +1321,11 @@ sub retrieve_item { if (($::form->{"partnumber_$i"} ne '') && ($ref->{ean} eq $::form->{"partnumber_$i"})) { push @{ $ref->{matches} ||= [] }, $::locale->text('EAN') . ': ' . $ref->{ean}; } - $ref->{type_and_classific} = $::request->presenter->type_abbreviation($ref->{part_type}). - $::request->presenter->classification_abbreviation($ref->{classification_id}); + $ref->{type_and_classific} = type_abbreviation($ref->{part_type}) . + classification_abbreviation($ref->{classification_id}); if (! $ref->{used_for_purchase} ) { - $has_wrong_pclass = 2; + $has_wrong_pclass = PCLASS_NOTFORPURCHASE; next; } # In der Buchungsgruppe ist immer ein Bestandskonto verknuepft, auch wenn @@ -1411,7 +1389,6 @@ sub retrieve_item { chop $ref->{taxaccounts}; $ref->{onhand} *= 1; - push @{ $form->{item_list} }, $ref; } @@ -1420,12 +1397,14 @@ sub retrieve_item { $_->[1]->finish for @translation_queries; $form->{is_wrong_pclass} = $has_wrong_pclass; + $form->{NOTFORSALE} = PCLASS_NOTFORSALE; + $form->{NOTFORPURCHASE} = PCLASS_NOTFORPURCHASE; foreach my $item (@{ $form->{item_list} }) { my $custom_variables = CVar->get_custom_variables(module => 'IC', trans_id => $item->{id}, dbh => $dbh, ); - $form->{is_wrong_pclass} = 0; # one correct type + $form->{is_wrong_pclass} = PCLASS_OK; # one correct type map { $item->{"ic_cvar_" . $_->{name} } = $_->{value} } @{ $custom_variables }; } @@ -1479,6 +1458,13 @@ sub vendor_details { 'trans_id' => $form->{vendor_id}); map { $form->{"vc_cvar_$_->{name}"} = $_->{value} } @{ $custom_variables }; + if ($form->{cp_id}) { + $custom_variables = CVar->get_custom_variables(dbh => $dbh, + module => 'Contacts', + trans_id => $form->{cp_id}); + $form->{"cp_cvar_$_->{name}"} = $_->{value} for @{ $custom_variables }; + } + $form->{cp_greeting} = GenericTranslations->get('dbh' => $dbh, 'translation_type' => 'greetings::' . ($form->{cp_gender} eq 'f' ? 'female' : 'male'), 'allow_fallback' => 1); @@ -1572,6 +1558,15 @@ sub _post_payment { $old_form = save_form(); + $query = <{id}); + # Delete all entries in acc_trans from prior payments. if (SL::DB::Default->get->payments_changeable != 0) { $self->_delete_payments($form, $dbh); @@ -1618,7 +1613,7 @@ sub _post_payment { ($form->{AP}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{id})); # Post the new payments. - $self->post_invoice($myconfig, $form, $dbh, 1); + $self->post_invoice($myconfig, $form, $dbh, payments_only => 1, already_cleared => \%already_cleared); restore_form($old_form);