Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Dev / CustomerVendor.pm
1 package SL::Dev::CustomerVendor;
2
3 use strict;
4 use base qw(Exporter);
5 our @EXPORT_OK = qw(new_customer new_vendor);
6 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
7
8 use SL::DB::TaxZone;
9 use SL::DB::Currency;
10 use SL::DB::Customer;
11
12 sub new_customer {
13   my (%params) = @_;
14
15   my $taxzone    = _check_taxzone(delete $params{taxzone_id});
16   my $currency   = _check_currency(delete $params{currency_id});
17
18   my $customer = SL::DB::Customer->new( name        => delete $params{name} || 'Testkunde',
19                                         currency_id => $currency->id,
20                                         taxzone_id  => $taxzone->id,
21                                       );
22   $customer->assign_attributes( %params );
23   return $customer;
24 }
25
26 sub new_vendor {
27   my (%params) = @_;
28
29   my $taxzone    = _check_taxzone(delete $params{taxzone_id});
30   my $currency   = _check_currency(delete $params{currency_id});
31
32   my $vendor = SL::DB::Vendor->new( name        => delete $params{name} || 'Testlieferant',
33                                     currency_id => $currency->id,
34                                     taxzone_id  => $taxzone->id,
35                                   );
36   $vendor->assign_attributes( %params );
37   return $vendor;
38 }
39
40 sub _check_taxzone {
41   my ($taxzone_id) = @_;
42   # check that taxzone_id exists or if no taxzone_id passed use 'Inland'
43   my $taxzone;
44   if ( $taxzone_id ) {
45     $taxzone = SL::DB::Manager::TaxZone->find_by( id => $taxzone_id ) || die "Can't find taxzone_id";
46   } else {
47     $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || die "No taxzone 'Inland'";
48   }
49   return $taxzone;
50 }
51
52 sub _check_currency {
53   my ($currency_id) = @_;
54   my $currency;
55   if ( $currency_id ) {
56     $currency = SL::DB::Manager::Currency->find_by( id => $currency_id ) || die "Can't find currency_id";
57   } else {
58     $currency = SL::DB::Manager::Currency->find_by( id => $::instance_conf->get_currency_id );
59   }
60   return $currency;
61 }
62
63 1;
64
65 __END__
66
67 =head1 NAME
68
69 SL::Dev::CustomerVendor - create customer and vendor objects for testing, with minimal defaults
70
71 =head1 FUNCTIONS
72
73 =head2 C<new_customer %PARAMS>
74
75 Creates a new customer.
76
77 Minimal usage, default values, without saving to database:
78
79   my $customer = SL::Dev::CustomerVendor::new_customer();
80
81 Complex usage, overwriting some defaults, and save to database:
82
83   SL::Dev::CustomerVendor::new_customer(name        => 'Test customer',
84                                            hourly_rate => 50,
85                                            taxzone_id  => 2,
86                                           )->save;
87
88 If neither taxzone_id or currency_id (both are NOT NULL) are passed as params
89 then default values are used.
90
91 =head2 C<new_vendor %PARAMS>
92
93 Creates a new vendor.
94
95 Minimal usage, default values, without saving to database:
96
97   my $vendor = SL::Dev::CustomerVendor::new_vendor();
98
99 Complex usage, overwriting some defaults, and save to database:
100
101   SL::Dev::CustomerVendor::new_vendor(name        => 'Test vendor',
102                                       taxzone_id  => 2,
103                                       notes       => "Order for 100$ for free delivery",
104                                       payment_id  => 5,
105                                      )->save;
106
107 =head1 BUGS
108
109 Nothing here yet.
110
111 =head1 AUTHOR
112
113 G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
114
115 =cut