Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 2172794d

Von Jan Büren vor etwa 2 Jahren hinzugefügt

Einkaufspreis von Erzeugnissen/Sortimententen ohne after_save hook

Unterschiede anzeigen:

SL/Controller/Part.pm
$::form->{"cvar_" . $_->{name} . "_valid"} = 1 for @default_valid_configs;
} else {
$::form->{lastcost_modified} = $self->check_lastcost_modified;
$self->{lastcost_modified} = $self->check_lastcost_modified;
}
# $self->part has been loaded, parsed and validated without errors and is ready to be saved
$self->part->db->with_transaction(sub {
$self->part->save(cascade => 1);
$self->part->set_lastcost_assemblies_and_assortiments if $self->{lastcost_modified};
SL::DB::History->new(
trans_id => $self->part->id,
......
sub check_lastcost_modified {
my ($self) = @_;
return abs($self->part->lastcost - $self->part->last_price_update->lastcost) < 0.009 ? undef : 1;
}
SL/DB/Part.pm
__PACKAGE__->before_save('_before_save_set_partnumber');
__PACKAGE__->before_save('_before_save_set_assembly_weight');
__PACKAGE__->after_save('_set_lastcost_assemblies_and_assortiments');
sub _before_save_set_partnumber {
my ($self) = @_;
......
sum map { $_->linetotal_weight} @{$self->items};
};
sub _set_lastcost_assemblies_and_assortiments {
sub set_lastcost_assemblies_and_assortiments {
my ($self) = @_;
return 1 unless $self->lastcost; # not saved yet
return 1 unless $::form->{lastcost_modified};
return 1 unless $self->id; # not saved yet
require SL::DB::AssortmentItem;
require SL::DB::Assembly;
# 1. check all
my $assortments = SL::DB::Manager::AssortmentItem->get_all(where => [parts_id => $self->id ]);
......
next unless ref $assembly eq 'SL::DB::Assembly';
my $a = SL::DB::Part->load_cached($assembly->id);
$a->update_attributes(lastcost => $a->items_lastcost_sum);
$a->set_lastcost_assemblies_and_assortiments;
}
foreach my $assortment (@{ $assortments }) {
next unless ref $assortment eq 'SL::DB::AssortmentItem';
my $a = SL::DB::Part->load_cached($assortment->assortment_id);
$a->update_attributes(lastcost => $a->items_lastcost_sum);
$a->set_lastcost_assemblies_and_assortiments;
}
return 1;
}
SL/IR.pm
next if $payments_only;
# update parts table by setting lastcost to current price, don't allow negative values by using abs
# maybe allow only 2 decimal places -> rounding error with fx can be too huge for datev checks
# @values = ($form->round_amount(abs($fxsellprice * $form->{exchangerate} / $basefactor), 2), conv_i($form->{"id_$i"}));
#$query = qq|UPDATE parts SET lastcost = ? WHERE id = ?|;
#@values = (abs($fxsellprice * $form->{exchangerate} / $basefactor), conv_i($form->{"id_$i"}));
#do_query($form, $dbh, $query, @values);
# after_save hook changes lastcost for all assemblies and assortments recursively
$::form->{lastcost_modified} = 1;
my $a = SL::DB::Part->load_cached(conv_i($form->{"id_$i"}));
$a->update_attributes(lastcost => abs($fxsellprice * $form->{exchangerate} / $basefactor));
$a->set_lastcost_assemblies_and_assortiments;
# check if we sold the item already and
# make an entry for the expense and inventory

Auch abrufbar als: Unified diff