SL::Dev::* - neue Helpermodule für Testcases und console
authorG. Richardson <information@kivitendo-premium.de>
Fri, 14 Oct 2016 12:55:16 +0000 (14:55 +0200)
committerG. Richardson <information@kivitendo-premium.de>
Mon, 17 Oct 2016 14:46:16 +0000 (16:46 +0200)
Mit Funktionen zum Generieren von Artikeln, Kunden und Lager- und
Lagerplätzen.

SL/Dev/ALL.pm [new file with mode: 0644]
SL/Dev/CustomerVendor.pm [new file with mode: 0644]
SL/Dev/Inventory.pm [new file with mode: 0644]
SL/Dev/Part.pm [new file with mode: 0644]

diff --git a/SL/Dev/ALL.pm b/SL/Dev/ALL.pm
new file mode 100644 (file)
index 0000000..96d1054
--- /dev/null
@@ -0,0 +1,42 @@
+package SL::Dev::ALL;
+
+use strict;
+
+use SL::Dev::Part;
+use SL::Dev::CustomerVendor;
+use SL::Dev::Inventory;
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+SL::Dev::ALL: Dependency-only package for all SL::Dev::* modules
+
+=head1 SYNOPSIS
+
+  use SL::Dev::ALL;
+
+=head1 DESCRIPTION
+
+This module depends on all modules in SL/Dev/*.pm for the convenience of being
+able to write a simple C<use SL::Dev::ALL> and having everything loaded. This
+is supposed to be used only for test cases or in the kivitendo console. Normal
+modules should C<use> only the modules they actually need.
+
+To automatically include it in the console, add a line in the client section of
+the kivitendo.config, e.g.
+
+[console]
+autorun = require "bin/mozilla/common.pl";
+        = use SL::DB::Helper::ALL;
+        = use SL::Dev::ALL;
+
+=head1 AUTHOR
+
+G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
+
+=cut
diff --git a/SL/Dev/CustomerVendor.pm b/SL/Dev/CustomerVendor.pm
new file mode 100644 (file)
index 0000000..3a8fa86
--- /dev/null
@@ -0,0 +1,69 @@
+package SL::Dev::CustomerVendor;
+
+use base qw(Exporter);
+@EXPORT = qw(create_customer);
+
+use SL::DB::TaxZone;
+use SL::DB::Currency;
+use SL::DB::Customer;
+
+sub create_customer {
+  my (%params) = @_;
+
+  my ($taxzone, $currency);
+
+  if ( my $taxzone_id = delete $params{taxzone_id} ) {
+    $taxzone = SL::DB::Manager::TaxZone->find_by( id => $taxzone_id ) || die "Can't find taxzone_id";
+  } else {
+    $taxzone = SL::DB::Manager::TaxZone->find_by( description => 'Inland') || die "No taxzone 'Inland'";
+  }
+
+  if ( my $currency_id = delete $params{currency_id} ) {
+    $currency = SL::DB::Manager::Currency->find_by( id => $currency_id ) || die "Can't find currency_id";
+  } else {
+    $currency = SL::DB::Manager::Currency->find_by( id => $::instance_conf->get_currency_id );
+  }
+
+  my $customer = SL::DB::Customer->new( name        => delete $params{name} || 'Testkunde',
+                                        currency_id => $currency->id,
+                                        taxzone_id  => $taxzone->id,
+                                      );
+  $customer->assign_attributes( %params );
+  return $customer;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+SL::Dev::CustomerVendor - create customer and vendor objects for testing, with minimal defaults
+
+=head1 FUNCTIONS
+
+=head2 C<create_customer %PARAMS>
+
+Creates a new customer.
+
+Minimal usage, default values, without saving to database:
+
+  my $customer = SL::Dev::CustomerVendor::create_customer();
+
+Complex usage, overwriting some defaults, and save to database:
+  SL::Dev::CustomerVendor::create_customer(name        => 'Test customer',
+                                           hourly_rate => 50,
+                                           taxzone_id  => 2,
+                                          )->save;
+
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
+
+=cut
+1;
diff --git a/SL/Dev/Inventory.pm b/SL/Dev/Inventory.pm
new file mode 100644 (file)
index 0000000..2ea6a50
--- /dev/null
@@ -0,0 +1,103 @@
+package SL::Dev::Inventory;
+
+use base qw(Exporter);
+@EXPORT = qw(create_warehouse_and_bins set_stock);
+
+use SL::DB::Warehouse;
+use SL::DB::Bin;
+use SL::DB::Inventory;
+use SL::DB::TransferType;
+use SL::DB::Employee;
+
+sub create_warehouse_and_bins {
+  my (%params) = @_;
+
+  my $number_of_bins = $params{number_of_bins} || 5;
+  my $wh = SL::DB::Warehouse->new(description => $params{warehouse_description} || "Warehouse", invalid => 0);
+  for my $i ( 1 .. $number_of_bins ) {
+    $wh->add_bins( SL::DB::Bin->new(description => ( $params{bin_description} || "Bin" ) . " $i" ) );
+  }
+  $wh->save;
+  return ($wh, $wh->bins->[0]);
+}
+
+sub set_stock {
+  my ($part, %params) = @_;
+
+  die "first argument is not a part" unless ref($part) eq 'SL::DB::Part';
+
+  die "no default warehouse" unless $part->warehouse_id or $part->bin_id;
+
+  die "Can't determine employee" unless SL::DB::Manager::Employee->current;
+
+  die "qty is missing or not positive" unless $params{qty} and $params{qty} > 0;
+
+  my $transfer_type_description = delete $params{transfer_type} || 'stock';
+  my $transfer_type = SL::DB::Manager::TransferType->find_by( description => $transfer_type_description, direction => 'in' );
+
+  my $shippingdate;
+  if ( $params{shippingdate} ) {
+    $shippingdate = $::locale->parse_date_to_object(delete $params{shippingdate});
+  } else {
+    $shippingdate = DateTime->today;
+  };
+
+  my ($trans_id) = $part->db->dbh->selectrow_array("select nextval('id')", {});
+
+  SL::DB::Inventory->new(
+    parts_id         => $part->id,
+    bin_id           => $part->bin_id,
+    warehouse_id     => $part->warehouse_id,
+    employee_id      => $params{employee_id} || SL::DB::Manager::Employee->current->id,
+    trans_type_id    => $transfer_type->id,
+    comment          => $params{comment},
+    shippingdate     => $shippingdate,
+    qty              => $params{qty},
+    trans_id         => $trans_id,
+  )->save;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+SL::Dev::Inventory - create inventory-related objects for testing, with minimal
+defaults
+
+=head1 FUNCTIONS
+
+=head2 C<create_warehouse_and_bins %PARAMS>
+
+Creates a new warehouse and bins, and immediately saves them. Returns the
+warehouse and the first bin object.
+  my ($wh, $bin) = SL::Dev::Inventory::create_warehouse_and_bins();
+
+Create named warehouse with 10 bins:
+  my ($wh, $bin) = SL::Dev::Inventory::create_warehouse_and_bins(warehouse_description => 'Testlager',
+                                                                 bin_description       => 'Testlagerplatz',
+                                                                 number_of_bins        => 10,
+                                                                );
+To access the second bin:
+  my $bin2 = $wh->bins->[1];
+
+=head2 C<set_stock $part, %PARAMS>
+
+Increase the stock level of a certain part by creating an inventory event. Currently
+only positive stock levels can be set. To access the updated onhand the part
+object needs to be loaded afterwards.
+
+  my $part = SL::DB::Manager::Part->find_by(partnumber => '1');
+  SL::Dev::Inventory::set_stock($part, 5);
+  $part->load;
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
+
+=cut
diff --git a/SL/Dev/Part.pm b/SL/Dev/Part.pm
new file mode 100644 (file)
index 0000000..29751d6
--- /dev/null
@@ -0,0 +1,89 @@
+package SL::Dev::Part;
+
+use base qw(Exporter);
+@EXPORT = qw(create_part create_service);
+
+use SL::DB::Part;
+use SL::DB::Unit;
+use SL::DB::Buchungsgruppe;
+
+sub create_part {
+  my (%params) = @_;
+
+  my ($buchungsgruppe, $unit);
+  $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%') || die "No accounting group";
+  $unit            = SL::DB::Manager::Unit->find_by(name => 'Stck')                          || die "No unit";
+
+  my $part = SL::DB::Part->new_part(
+    description        => 'Test part',
+    sellprice          => '10',
+    lastcost           => '5',
+    buchungsgruppen_id => $buchungsgruppe->id,
+    unit               => $unit->name,
+  );
+  $part->assign_attributes( %params );
+  return $part;
+}
+
+sub create_service {
+  my (%params) = @_;
+
+  my ($buchungsgruppe, $unit);
+  $buchungsgruppe  = SL::DB::Manager::Buchungsgruppe->find_by(description => 'Standard 19%') || die "No accounting group";
+  $unit            = SL::DB::Manager::Unit->find_by(name => 'Stck')                          || die "No unit";
+
+  my $part = SL::DB::Part->new_service(
+    description        => 'Test service',
+    sellprice          => '10',
+    lastcost           => '5',
+    buchungsgruppen_id => $buchungsgruppe->id,
+    unit               => $unit->name,
+  );
+  $part->assign_attributes( %params );
+  return $part;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+SL::Dev::Part - create part objects for testing, with minimal defaults
+
+=head1 FUNCTIONS
+
+=head2 C<create_part %PARAMS>
+
+Creates a new part (part_type = part).
+
+Minimal usage, default values, without saving to database:
+
+  my $part = SL::Dev::Part::create_part();
+
+Create a test part with a default warehouse and bin and save it:
+
+  my $wh    = SL::Dev::Inventory::create_warehouse_and_bins()->save;
+  my $part1 = SL::Dev::Part::create_part(partnumber   => 'a123',
+                                         description  => 'Testpart 1',
+                                         warehouse_id => $wh->id,
+                                         bin_id       => $wh->bins->[0]->id,
+                                        )->save;
+
+=head1 TODO
+
+=over 2
+
+=item * create_assembly
+
+=back
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+G. Richardson E<lt>grichardson@kivitendo-premium.deE<gt>
+
+=cut