Shipto: Methode zum Clonen in SL::DB::Shipto und nicht in DeliveryOrder->new_from
[kivitendo-erp.git] / SL / DB / Shipto.pm
1 package SL::DB::Shipto;
2
3 use strict;
4
5 use Carp;
6
7 use SL::DB::MetaSetup::Shipto;
8 use SL::DB::Manager::Shipto;
9 use SL::DB::Helper::CustomVariables (
10   module      => 'ShipTo',
11   cvars_alias => 1,
12 );
13
14 our @SHIPTO_VARIABLES = qw(shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptogln shiptocontact
15                            shiptophone shiptofax shiptoemail shiptodepartment_1 shiptodepartment_2);
16
17 __PACKAGE__->meta->initialize;
18
19 sub displayable_id {
20   my $self = shift;
21   my $text = join('; ', grep { $_ } (map({ $self->$_ } qw(shiptoname shiptostreet)),
22                                      join(' ', grep { $_ }
23                                                map  { $self->$_ }
24                                                qw(shiptozipcode shiptocity))));
25
26   return $text;
27 }
28
29 sub used {
30   my ($self) = @_;
31
32   return unless $self->shipto_id;
33
34   require SL::DB::Order;
35   require SL::DB::Invoice;
36   require SL::DB::DeliveryOrder;
37
38   return SL::DB::Manager::Order->get_all_count(query => [ shipto_id => $self->shipto_id ])
39       || SL::DB::Manager::Invoice->get_all_count(query => [ shipto_id => $self->shipto_id ])
40       || SL::DB::Manager::DeliveryOrder->get_all_count(query => [ shipto_id => $self->shipto_id ]);
41 }
42
43 sub detach {
44   $_[0]->trans_id(undef);
45   $_[0];
46 }
47
48 sub clone {
49   my ($self, $target) = @_;
50
51   my $type   = ref($target) || $target;
52   my $module = $type =~ m{::Order$}               ? 'OE'
53              : $type =~ m{::DeliveryOrder$}       ? 'DO'
54              : $type =~ m{::Invoice$}             ? 'AR'
55              : $type =~ m{::(?:Customer|Vendor)$} ? 'CT'
56              :                                      croak "Unsupported target class '$type'";
57
58   my $new_shipto = SL::DB::Shipto->new(
59     (map  { +($_ => $self->$_) }
60      grep { !m{^ (?: itime | mtime | shipto_id | trans_id ) $}x }
61      map  { $_->name }
62      @{ $self->meta->columns }),
63     module => $module,
64   );
65
66   return $new_shipto;
67 }
68
69 1;
70
71 __END__
72
73 =pod
74
75 =encoding utf8
76
77 =head1 NAME
78
79 SL::DB::Shipto - Database model for shipping addresses
80
81 =head1 SYNOPSIS
82
83   my $order = SL::DB::Order->new(id => …)->load;
84   if ($order->custom_shipto) {
85     my $cloned_shipto = $order->custom_shipto->clone('SL::DB::Invoice');
86   }
87
88 =head1 FUNCTIONS
89
90 =over 4
91
92 =item C<clone $target>
93
94 Creates and returns a clone of the current object. The mandatory
95 parameter C<$target> must be either an instance of a Rose DB class or
96 the name of one. It's used for setting the new instance's C<module>
97 attribute to the correct value.
98
99 Currently the following classes are supported:
100
101 =over 2
102
103 =item C<SL::DB::Order>
104
105 =item C<SL::DB::DeliveryOrder>
106
107 =item C<SL::DB::Invoice>
108
109 =item C<SL::DB::Customer>
110
111 =item C<SL::DB::Vendor>
112
113 =back
114
115 =back
116
117 =head1 BUGS
118
119 Nothing here yet.
120
121 =head1 AUTHOR
122
123 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
124
125 =cut