11 |
11 |
sub calculate_prices_and_taxes {
|
12 |
12 |
my ($self, %params) = @_;
|
13 |
13 |
|
|
14 |
require SL::DB::Chart;
|
|
15 |
require SL::DB::Currency;
|
|
16 |
require SL::DB::Default;
|
|
17 |
require SL::DB::InvoiceItem;
|
|
18 |
require SL::DB::Part;
|
14 |
19 |
require SL::DB::PriceFactor;
|
15 |
20 |
require SL::DB::Unit;
|
16 |
21 |
|
|
22 |
SL::DB::Part->load_cached(map { $_->parts_id } @{ $self->items });
|
|
23 |
|
17 |
24 |
my %units_by_name = map { ( $_->name => $_ ) } @{ SL::DB::Manager::Unit->get_all };
|
18 |
25 |
my %price_factors_by_id = map { ( $_->id => $_ ) } @{ SL::DB::Manager::PriceFactor->get_all };
|
19 |
26 |
|
... | ... | |
54 |
61 |
|
55 |
62 |
sub _get_exchangerate {
|
56 |
63 |
my ($self, $data, %params) = @_;
|
57 |
|
require SL::DB::Default;
|
58 |
64 |
|
59 |
|
my $currency = $self->currency_id ? $self->currency->name || '' : '';
|
|
65 |
my $currency = $self->currency_id ? SL::DB::Currency->load_cached($self->currency_id)->name || '' : '';
|
60 |
66 |
if ($currency ne SL::DB::Default->get_default_currency) {
|
61 |
67 |
$data->{exchangerate} = $::form->check_exchangerate(\%::myconfig, $currency, $self->transdate, $data->{is_sales} ? 'buy' : 'sell');
|
62 |
68 |
$data->{exchangerate} ||= $params{exchangerate};
|
... | ... | |
67 |
73 |
sub _calculate_item {
|
68 |
74 |
my ($self, $item, $idx, $data, %params) = @_;
|
69 |
75 |
|
70 |
|
my $part_unit = $data->{units_by_name}->{ $item->part->unit };
|
71 |
|
my $item_unit = $data->{units_by_name}->{ $item->unit };
|
|
76 |
my $part = SL::DB::Part->load_cached($item->parts_id);
|
|
77 |
my $part_unit = $data->{units_by_name}->{ $part->unit };
|
|
78 |
my $item_unit = $data->{units_by_name}->{ $item->unit };
|
72 |
79 |
|
73 |
|
croak("Undefined unit " . $item->part->unit) if !$part_unit;
|
|
80 |
croak("Undefined unit " . $part->unit) if !$part_unit;
|
74 |
81 |
croak("Undefined unit " . $item->unit) if !$item_unit;
|
75 |
82 |
|
76 |
83 |
$item->base_qty($item_unit->convert_to($item->qty, $part_unit));
|
... | ... | |
88 |
95 |
my $sellprice = _round($item->sellprice - $discount, $num_dec);
|
89 |
96 |
|
90 |
97 |
$item->price_factor( ! $item->price_factor_obj ? 1 : ($item->price_factor_obj->factor || 1));
|
91 |
|
$item->marge_price_factor(! $item->part->price_factor ? 1 : ($item->part->price_factor->factor || 1));
|
|
98 |
$item->marge_price_factor(! $part->price_factor ? 1 : ($part->price_factor->factor || 1));
|
92 |
99 |
my $linetotal = _round($sellprice * $item->qty / $item->price_factor, 2) * $data->{exchangerate};
|
93 |
100 |
$linetotal = _round($linetotal, 2);
|
94 |
101 |
|
... | ... | |
99 |
106 |
$item->marge_percent(0);
|
100 |
107 |
|
101 |
108 |
} else {
|
102 |
|
my $lastcost = ! ($item->lastcost * 1) ? ($item->part->lastcost || 0) : $item->lastcost;
|
|
109 |
my $lastcost = ! ($item->lastcost * 1) ? ($part->lastcost || 0) : $item->lastcost;
|
103 |
110 |
my $linetotal_cost = _round($lastcost * $item->qty / $item->marge_price_factor, 2);
|
104 |
111 |
|
105 |
112 |
$item->marge_total( $linetotal - $linetotal_cost);
|
... | ... | |
109 |
116 |
$data->{lastcost_total} += $linetotal_cost;
|
110 |
117 |
}
|
111 |
118 |
|
112 |
|
my $taxkey = $item->part->get_taxkey(date => $self->transdate, is_sales => $data->{is_sales}, taxzone => $self->taxzone_id);
|
|
119 |
my $taxkey = $part->get_taxkey(date => $self->transdate, is_sales => $data->{is_sales}, taxzone => $self->taxzone_id);
|
113 |
120 |
my $tax_rate = $taxkey->tax->rate;
|
114 |
121 |
my $tax_amount = undef;
|
115 |
122 |
|
... | ... | |
130 |
137 |
|
131 |
138 |
$self->netamount($self->netamount + $sellprice * $item->qty / $item->price_factor);
|
132 |
139 |
|
133 |
|
my $chart = $item->part->get_chart(type => $data->{is_sales} ? 'income' : 'expense', taxzone => $self->taxzone_id);
|
|
140 |
my $chart = $part->get_chart(type => $data->{is_sales} ? 'income' : 'expense', taxzone => $self->taxzone_id);
|
134 |
141 |
$data->{amounts}->{ $chart->id } ||= { taxkey => $taxkey->taxkey_id, tax_id => $taxkey->tax_id, amount => 0 };
|
135 |
142 |
$data->{amounts}->{ $chart->id }->{amount} += $linetotal;
|
136 |
143 |
$data->{amounts}->{ $chart->id }->{amount} -= $tax_amount if $self->taxincluded;
|
137 |
144 |
|
138 |
145 |
push @{ $data->{assembly_items} }, [];
|
139 |
|
if ($item->part->is_assembly) {
|
140 |
|
_calculate_assembly_item($self, $data, $item->part, $item->base_qty, $item->unit_obj->convert_to(1, $item->part->unit_obj));
|
141 |
|
} elsif ($item->part->is_part) {
|
|
146 |
if ($part->is_assembly) {
|
|
147 |
_calculate_assembly_item($self, $data, $part, $item->base_qty, $item_unit->convert_to(1, $part_unit));
|
|
148 |
} elsif ($part->is_part) {
|
142 |
149 |
if ($data->{is_invoice}) {
|
143 |
|
$item->allocated(_calculate_part_item($self, $data, $item->part, $item->base_qty, $item->unit_obj->convert_to(1, $item->part->unit_obj)));
|
|
150 |
$item->allocated(_calculate_part_item($self, $data, $part, $item->base_qty, $item_unit->convert_to(1, $part_unit)));
|
144 |
151 |
}
|
145 |
152 |
}
|
146 |
153 |
|
SL::DB::Part,Helper::PriceTaxCalculator: agressiveres Caching
Damit wird für ziemlich jeden Objekttyp nur noch ein einziges Query
abgesetzt.