]> wagnertech.de Git - mfinanz.git/blob - SL/DB/ReclamationItem.pm
date error in mapping
[mfinanz.git] / SL / DB / ReclamationItem.pm
1 package SL::DB::ReclamationItem;
2
3 use utf8;
4 use strict;
5
6 use List::MoreUtils qw(any);
7 use Rose::DB::Object::Helpers qw(as_tree strip);
8
9 use SL::DB::MetaSetup::ReclamationItem;
10 use SL::DB::Manager::ReclamationItem;
11 use SL::DB::Helper::ActsAsList;
12 use SL::DB::Helper::LinkedRecords;
13 use SL::DB::Helper::RecordItem;
14 use SL::DB::Helper::RecordLink qw(RECORD_ITEM_ID RECORD_ITEM_TYPE_REF);
15 use SL::DB::Helper::CustomVariables (
16   sub_module  => 'reclamation_items',
17   cvars_alias => 1,
18   overloads   => {
19     parts_id => {
20       class => 'SL::DB::Part',
21       module => 'IC',
22     }
23   },
24 );
25
26 __PACKAGE__->meta->initialize;
27
28 __PACKAGE__->configure_acts_as_list(group_by => [qw(reclamation_id)]);
29
30 sub is_linked_to_record {
31   my ($self) = @_;
32
33   if(scalar(@{$self->linked_records}) || $self->{RECORD_ITEM_TYPE_REF()}) {
34     return 1;
35   }
36
37   return 0;
38 }
39
40 #TODO(Werner): überprüfen ob alle Felder richtig gestetzt werden
41 sub new_from {
42   my ($class, $source, $parent_type, %params) = @_;
43   unless (any {ref($source) eq $_}
44     qw(
45       SL::DB::ReclamationItem
46       SL::DB::OrderItem
47       SL::DB::DeliveryOrderItem
48       SL::DB::InvoiceItem
49     )
50   ) {
51     croak("Unsupported source object type '" . ref($source) . "'");
52   }
53   my @custom_variables = map { _clone_cvar_for_reclamation_item($_) } @{ $source->custom_variables };
54
55
56   my %item_args;
57   if (ref($source) eq 'SL::DB::ReclamationItem') {
58     map { $item_args{$_} = $source->$_ } qw(
59       active_discount_source active_price_source base_qty description discount
60       lastcost longdescription parts_id position price_factor price_factor_id
61       pricegroup_id project_id qty reason_description_ext reason_description_int
62       reason_id reqdate sellprice serialnumber
63       unit
64     );
65     $item_args{custom_variables} = \@custom_variables;
66   } elsif (ref($source) eq 'SL::DB::OrderItem') {
67     map { $item_args{$_} = $source->$_ } qw(
68       active_discount_source active_price_source base_qty description discount
69       lastcost longdescription parts_id position price_factor price_factor_id
70       pricegroup_id project_id qty reqdate sellprice serialnumber unit
71     );
72     $item_args{custom_variables} = \@custom_variables;
73   } elsif (ref($source) eq 'SL::DB::DeliveryOrderItem') {
74     map { $item_args{$_} = $source->$_ } qw(
75       active_discount_source active_price_source base_qty description discount
76       lastcost longdescription parts_id position price_factor price_factor_id
77       pricegroup_id project_id qty reqdate sellprice serialnumber unit
78     );
79     $item_args{custom_variables} = \@custom_variables;
80   } elsif (ref($source) eq 'SL::DB::InvoiceItem') {
81     map { $item_args{$_} = $source->$_ } qw(
82       active_discount_source active_price_source base_qty description discount
83       lastcost longdescription parts_id position price_factor price_factor_id
84       pricegroup_id project_id qty sellprice serialnumber unit
85     );
86     $item_args{custom_variables} = \@custom_variables;
87   }
88
89   my $item = $class->new(%item_args);
90
91   if ( $source->record->is_sales() && ($parent_type =~ m{purchase}) ) {
92     $item->sellprice($source->lastcost);
93     $item->discount(0);
94   }
95   if ( !$source->record->is_sales() && ($parent_type =~ m{sales}) ) {
96     $item->lastcost($source->sellprice);
97   }
98
99   $item->assign_attributes(%{ $params{attributes} }) if $params{attributes};
100
101   unless ($params{no_linked_records}) {
102     $item->{RECORD_ITEM_TYPE_REF()} = ref($source);
103     $item->{RECORD_ITEM_ID()} = $source->id;
104   }
105
106   return $item;
107 }
108
109 sub _clone_cvar_for_reclamation_item {
110   my ($cvar) = @_;
111
112   my $cloned = $_->clone_and_reset;
113   $cloned->sub_module('reclamation_items');
114
115   return $cloned;
116 }
117
118 sub customervendor {
119   my ($self) = @_;
120
121   return $self->reclamation->customervendor;
122 }
123
124 sub record { goto &reclamation }
125 sub record_id { goto &reclamation_id }
126 sub trans_id { goto &reclamation_id }
127 sub date { goto &reqdate }
128
129 1;
130
131 __END__
132
133 =pod
134
135 =head1 NAME
136
137 SL::DB::ReclamationItems: Rose model for reclamationitems
138
139 =head1 FUNCTIONS
140
141 =head1 AUTHORS
142
143 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
144
145 =cut