Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 400d16e3

Von Sven Schöling vor mehr als 7 Jahren hinzugefügt

  • ID 400d16e35476ed8fc0e15baf456c7e2667844d43
  • Vorgänger e05e7574
  • Nachfolger ecb95993

OrderItem: shipped_qty mit SL::Helper::ShippedQty berechnen

Unterschiede anzeigen:

SL/DB/OrderItem.pm
20 20
    }
21 21
  },
22 22
);
23
use SL::Helper::ShippedQty;
23 24

  
24 25
__PACKAGE__->meta->initialize;
25 26

  
......
31 32
}
32 33

  
33 34
sub shipped_qty {
34
  my ($self) = @_;
35
  my ($self, %params) = @_;
36

  
37
  my $force = delete $params{force};
35 38

  
36
  my $d_orders = $self->order->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrder', with_objects => [ 'orderitems' ]);
37
  my @doi      = grep { $_->parts_id == $self->parts_id } map { $_->orderitems } @$d_orders;
39
  SL::Helper::ShippedQty->new(%params)->calculate($self)->write_to_objects if $force || !defined $self->{shipped_qty};
38 40

  
39
  return sum(map { AM->convert_unit($_->unit => $self->unit) * $_->qty } @doi);
41
  $self->{shipped_qty};
40 42
}
41 43

  
42 44
sub linked_delivery_order_items {
......
45 47
  return $self->linked_records(direction => 'to', to => 'SL::DB::DeliveryOrderItem');
46 48
}
47 49

  
48
sub delivered_qty {
49
  # checks for delivery_order_stock_id entries, which have been converted to inventory entries
50
  # uses several rose relationships
51
  # doesn't differentiate between sales and orders
52

  
53
  my ($self) = @_;
54
  my $delivered_qty = 0;
55
  foreach my $doi ( @{$self->linked_delivery_order_items} ) {
56
    next unless scalar @{$doi->delivery_order_stock_entries};
57
    $delivered_qty += sum map { $_->inventory ? $_->qty : 0 } @{$doi->delivery_order_stock_entries};
58
  };
59
  return $delivered_qty;
60
};
61

  
62
sub delivered_qty_sql {
63
  # checks for delivery_order_stock_id entries, which have been converted to inventory entries
64
  my ($self) = @_;
65

  
66
my $query = <<SQL;
67
SELECT (sum(i.qty) * CASE WHEN oe.customer_id IS NULL THEN 1 ELSE -1 END) AS delivered
68
 FROM orderitems oi
69
 INNER JOIN record_links rl                 ON (    oi.id             = rl.FROM_id
70
                                                and rl.FROM_table     = 'orderitems'
71
                                                and rl.to_table::text = 'delivery_order_items'::text
72
                                               )
73
 INNER JOIN delivery_order_items doi        ON (doi.id =rl.to_id)
74
 INNER JOIN delivery_order_items_stock dois ON (dois.delivery_order_item_id = doi.id)
75
 INNER JOIN inventory i                     ON (dois.id = i.delivery_order_items_stock_id)
76
 INNER JOIN oe                              ON (oe.id = oi.trans_id)
77
 WHERE oi.id = ?
78
 GROUP BY oi.id, oe.id
79
SQL
80
  my ($delivered_qty) = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id);
81

  
82
  return $delivered_qty;
83
};
84

  
85
sub delivered_qty_sql_multi {
86
  # checks for delivery_order_stock_id entries, which have been converted to inventory entries
87
  my ($self) = @_;
88

  
89
my $query = <<SQL;
90
SELECT sum(dois.qty) from delivery_order_items_stock dois
91
  LEFT OUTER JOIN inventory i ON (dois.id = i.delivery_order_items_stock_id)
92
WHERE
93
  dois.delivery_order_item_id in (
94
SELECT
95
  to_id
96
FROM
97
  record_links
98
WHERE
99
  (
100
    from_id = in AND
101
    from_table = 'orderitems' AND
102
    to_table = 'delivery_order_items'
103
  )
104
)
105
SQL
106
  my ($delivered_qty) = selectfirst_array_query($::form, $self->db->dbh, $query, $self->id);
107

  
108
  return $delivered_qty;
109
};
50
sub delivered_qty { goto &shipped_qty }
110 51

  
111 52
sub record { goto &order }
112 53

  
......
124 65

  
125 66
=over 4
126 67

  
127
=item C<shipped_qty>
128

  
129
=item C<shipped_qty>
68
=item C<shipped_qty PARAMS>
130 69

  
131
returns the number of orderitems which are already linked to Delivery Orders.
132
The linked key is parts_id and not orderitems (id) -> delivery_order_items (id).
133
Therefore this function is not safe for identical parts_id.
134
Sample call:
135
C<$::form-E<gt>format_amount(\%::myconfig, $_[0]-E<gt>shipped_qty);>
136

  
137
=item C<delivered_qty>
70
Calculates the shipped qty for this orderitem (measured in the current unit)
71
and returns it.
138 72

  
139
Returns the amount of this orderitem that has been delivered, meaning it
140
appears in a delivery order AND has been transferred. The delivery order items
141
are found by direct record_links from orderitems to delivery order items.
142
Delivery order items that were added manually and not via the record workflow
143
will therefore not be calculated correctly.
73
Note that the shipped qty is expected not to change within the request and is
74
cached in C<shipped_qty> once calculated. If C<< force => 1 >> is passed, the
75
existibng cache is ignored.
144 76

  
145
Orders that were created before the individual items were linked (this feature
146
was added in kivitendo 3.2.0) will therefore return incorrect results.
77
Given parameters will be passed to L<SL::Helper::ShippedQty>, so you can force
78
the shipped/delivered distinction like this:
147 79

  
148
=item C<delivered_qty_sql>
80
  $_->shipped_qty(require_stock_out => 0);
149 81

  
150
Does the same as delivered_qty, but via sql.
82
Note however that calculating shipped_qty on individual Orderitems is generally
83
a bad idea. See L<SL::Helper::ShippedQty> for way to compute these all at once.
151 84

  
85
=item C<delivered_qty>
152 86

  
153

  
154
=item C<delivered_qty_sql>
155

  
156
Returns the amount of the orderitem that has actually been shipped, not just where delivery_order_items exist (how delivered_qty works).
157

  
158
Doesn't deal with different units yet.
87
Alias for L</shipped_qty>.
159 88

  
160 89
=back
161 90

  
162
=head1 TODO
163

  
164
Older versions of OrderItem.pm had more functions which where used for calculating the
165
qty for the different states of the Delivery Order.
166
For example to get the qty in already marked as delivered Delivery Orders:
167

  
168
C<delivered_qty>
169

  
170
return $self-E<gt>_delivered_qty;
171

  
172
  sub _delivered_qty {
173
  (..)
174
    my @d_orders_delivered = grep { $_-E<gt>delivered } @$d_orders;
175
    my @doi_delivered      = grep { $_-E<gt>parts_id == $self-E<gt>parts_id } map { $_-E<gt>orderitems } @d_orders_delivered;
176
  }
177

  
178
In general the function C<shipped_qty> and all (project) related functions should be marked deprecate,
179
 because of the better linked item to item data in the record_links table.
180

  
181 91
=head1 AUTHORS
182 92

  
183 93
G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>

Auch abrufbar als: Unified diff