Pflichtenheft: Zugriff auf nicht vorhandenes »visible_item« verhindern
[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     custom_variables => [ map { $_->clone_and_reset } @{ $self->custom_variables } ],
65   );
66
67   return $new_shipto;
68 }
69
70 1;
71
72 __END__
73
74 =pod
75
76 =encoding utf8
77
78 =head1 NAME
79
80 SL::DB::Shipto - Database model for shipping addresses
81
82 =head1 SYNOPSIS
83
84   my $order = SL::DB::Order->new(id => …)->load;
85   if ($order->custom_shipto) {
86     my $cloned_shipto = $order->custom_shipto->clone('SL::DB::Invoice');
87   }
88
89 =head1 FUNCTIONS
90
91 =over 4
92
93 =item C<clone $target>
94
95 Creates and returns a clone of the current object. The mandatory
96 parameter C<$target> must be either an instance of a Rose DB class or
97 the name of one. It's used for setting the new instance's C<module>
98 attribute to the correct value.
99
100 Currently the following classes are supported:
101
102 =over 2
103
104 =item C<SL::DB::Order>
105
106 =item C<SL::DB::DeliveryOrder>
107
108 =item C<SL::DB::Invoice>
109
110 =item C<SL::DB::Customer>
111
112 =item C<SL::DB::Vendor>
113
114 =back
115
116 =back
117
118 =head1 BUGS
119
120 Nothing here yet.
121
122 =head1 AUTHOR
123
124 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
125
126 =cut