Auftrags-Controller: Speichern und schließen, …
[kivitendo-erp.git] / sql / Pg-upgrade2 / convert_drafts_to_record_templates.pl
index 7507f9a..68c6a48 100644 (file)
@@ -6,17 +6,16 @@ package SL::DBUpgrade2::convert_drafts_to_record_templates;
 use strict;
 use utf8;
 
-use YAML;
-
 use parent qw(SL::DBUpgrade2::Base);
 
 use SL::DBUtils;
+use SL::YAML;
 
 sub prepare_statements {
   my ($self) = @_;
 
   $self->{q_draft} = qq|
-    SELECT description, form
+    SELECT description, form, employee_id
     FROM drafts
     WHERE module = ?
 |;
@@ -77,9 +76,9 @@ sub migrate_ar_drafts {
   $self->{h_draft}->execute('ar') || die $self->{h_draft}->errstr;
 
   while (my $draft_record = $self->{h_draft}->fetchrow_hashref) {
-    my $draft         = YAML::Load($draft_record->{form});
-    my $currency_id   = $self->{currency_ids_by_name}->{$draft->{currency}};
-    my ($employee_id) = $draft->{employee_id} || (split m{--}, $draft->{employee})[1] || undef;
+    my $draft       = SL::YAML::Load($draft_record->{form});
+    my $currency_id = $self->{currency_ids_by_name}->{$draft->{currency}};
+    my $employee_id = $draft_record->{employee_id} || $draft->{employee_id} || (split m{--}, $draft->{employee})[1] || undef;
 
     next unless $currency_id;
 
@@ -123,8 +122,7 @@ sub migrate_ar_drafts {
 
       # $tax_id may be 0 as there's an entry in tax with id = 0.
       # $chart_id must not be 0 as there's no entry in chart with id = 0.
-      # No $amount means empty row.
-      next unless $amount && $chart_id && (($tax_id // '') ne '');
+      next unless $chart_id && (($tax_id // '') ne '');
 
       @values = (
         # record_template_id,
@@ -153,8 +151,9 @@ sub migrate_ap_drafts {
   $self->{h_draft}->execute('ap') || die $self->{h_draft}->errstr;
 
   while (my $draft_record = $self->{h_draft}->fetchrow_hashref) {
-    my $draft       = YAML::Load($draft_record->{form});
+    my $draft       = SL::YAML::Load($draft_record->{form});
     my $currency_id = $self->{currency_ids_by_name}->{$draft->{currency}};
+    my $employee_id = $draft_record->{employee_id} || $draft->{employee_id} || (split m{--}, $draft->{employee})[1] || undef;
 
     next unless $currency_id;
 
@@ -169,7 +168,7 @@ sub migrate_ap_drafts {
       $currency_id,
       $draft->{department_id}    || undef,
       $draft->{globalproject_id} || undef,
-      undef,
+      $employee_id,
 
       # taxincluded,   direct_debit, ob_transaction, cb_transaction,
       $draft->{taxincluded}   ? 1 : 0,
@@ -198,8 +197,7 @@ sub migrate_ap_drafts {
 
       # $tax_id may be 0 as there's an entry in tax with id = 0.
       # $chart_id must not be 0 as there's no entry in chart with id = 0.
-      # No $amount means empty row.
-      next unless $amount && $chart_id && (($tax_id // '') ne '');
+      next unless $chart_id && (($tax_id // '') ne '');
 
       @values = (
         # record_template_id,
@@ -228,7 +226,8 @@ sub migrate_gl_drafts {
   $self->{h_draft}->execute('gl') || die $self->{h_draft}->errstr;
 
   while (my $draft_record = $self->{h_draft}->fetchrow_hashref) {
-    my $draft = YAML::Load($draft_record->{form});
+    my $draft       = SL::YAML::Load($draft_record->{form});
+    my $employee_id = $draft_record->{employee_id} || $draft->{employee_id} || (split m{--}, $draft->{employee})[1] || undef;
 
     my @values = (
       # template_name, template_type, customer_id, vendor_id,
@@ -241,7 +240,7 @@ sub migrate_gl_drafts {
       $self->{default_currency_id},
       $draft->{department_id} || undef,
       undef,
-      undef,
+      $employee_id,
 
       # taxincluded,   direct_debit, ob_transaction, cb_transaction,
       $draft->{taxincluded}    ? 1 : 0,
@@ -271,8 +270,7 @@ sub migrate_gl_drafts {
 
       # $tax_id may be 0 as there's an entry in tax with id = 0.
       # $chart_id must not be 0 as there's no entry in chart with id = 0.
-      # No $debit and no $credit means empty row.
-      next unless ($debit || $credit) && $chart_id && (($tax_id // '') ne '');
+      next unless $chart_id && (($tax_id // '') ne '');
 
       @values = (
         # record_template_id,
@@ -320,11 +318,9 @@ sub run {
   $self->migrate_ar_drafts;
   $self->migrate_ap_drafts;
   $self->migrate_gl_drafts;
-  $self->clean_drafts;
+  $self->clean_drafts;
   $self->finish_statements;
 
-  # die "boom!";
-
   return 1;
 }