Mahnwesen: Die Tabelle dunning so umgebaut, dass gemeinsam gestartete Mahnungen auch...
[kivitendo-erp.git] / bin / mozilla / is.pl
index f210457..51f3762 100644 (file)
@@ -37,6 +37,7 @@ use Data::Dumper;
 
 require "$form->{path}/io.pl";
 require "$form->{path}/arap.pl";
+require "bin/mozilla/drafts.pl";
 
 1;
 
@@ -44,7 +45,9 @@ require "$form->{path}/arap.pl";
 
 sub add {
   $lxdebug->enter_sub();
-  
+
+  return $lxdebug->leave_sub() if (load_draft_maybe());
+
   if ($form->{type} eq "credit_note") {
     $form->{title} = $locale->text('Add Credit Note');
 
@@ -118,6 +121,7 @@ sub invoice_links {
   if ($form->{all_customer}) {
     unless ($form->{customer_id}) {
       $form->{customer_id} = $form->{all_customer}->[0]->{id};
+      $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
     }
   }
 
@@ -364,7 +368,8 @@ sub form_header {
                    "shipto" => "ALL_SHIPTO",
                    "projects" => { "key" => "ALL_PROJECTS",
                                    "all" => 0,
-                                   "old_id" => \@old_project_ids });
+                                   "old_id" => \@old_project_ids },
+                   "employees" => "ALL_SALESMEN");
 
   my (%labels, @values);
   foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
@@ -402,6 +407,22 @@ sub form_header {
                          '-labels' => \%labels,
                          '-default' => $form->{"globalproject_id"}));
 
+  %labels = ();
+  @values = ("");
+  foreach my $item (@{ $form->{ALL_SALESMEN} }) {
+    push(@values, $item->{id});
+    $labels{$item->{id}} = $item->{name} ne "" ? $item->{name} : $item->{login};
+  }
+
+  $salesman =
+    qq|<tr>
+          <th align="right">| . $locale->text('Salesman') . qq|</th>
+          <td>| .
+     NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{salesman_id},
+                               '-values' => \@values, '-labels' => \%labels))
+     . qq|</td>
+         </tr>|;
+
   # set option selected
   foreach $item (qw(AR customer currency department employee)) {
     $form->{"select$item"} =~ s/ selected//;
@@ -621,7 +642,7 @@ sub form_header {
 <input type=hidden name=storno value=$form->{storno}>
 <input type=hidden name=storno_id value=$form->{storno_id}>
 
-
+| . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
 
 <table width=100%>
   <tr class=listtop>
@@ -710,11 +731,13 @@ print qq|     </table>
          <td align=right>
            <table>
              <tr>
-               <th align=right nowrap>| . $locale->text('Salesperson') . qq|</th>
+               <th align=right nowrap>| . $locale->text('Employee') . qq|</th>
                <td colspan=2><select name=employee>$form->{selectemployee}</select></td>
                <input type=hidden name=selectemployee value="$form->{selectemployee}">
                 <td></td>
-             </tr>|;
+             </tr>
+        $salesman
+|;
 if ($form->{type} eq "credit_note") {
 print qq|     <tr>
                <th align=right nowrap>| . $locale->text('Credit Note Number') . qq|</th>
@@ -853,7 +876,8 @@ sub form_footer {
 
         $tax .= qq|
              <tr>
-               <th align=right>$form->{"${item}_description"}</th>
+                <th align=right>$form->{"${item}_description"}&nbsp;|
+                                    . $form->{"${item}_rate"} * 100 .qq|%</th>
                <td align=right>$form->{"${item}_total"}</td>
              </tr>
 |;
@@ -892,7 +916,8 @@ sub form_footer {
 
         $tax .= qq|
              <tr>
-               <th align=right>Enthaltene $form->{"${item}_description"}</th>
+               <th align=right>Enthaltene $form->{"${item}_description"}&nbsp;|
+                                   . $form->{"${item}_rate"} * 100 .qq|%</th>
                <td align=right>$form->{"${item}_total"}</td>
              </tr>
              <tr>
@@ -1097,6 +1122,8 @@ if ($form->{type} eq "credit_note") {
   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
   if ($form->{id}) {
+    my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar");
+
     print qq|
     <input class=submit type=submit accesskey="u" name=action id=update_button value="|
       . $locale->text('Update') . qq|">
@@ -1105,9 +1132,9 @@ if ($form->{type} eq "credit_note") {
     <input class=submit type=submit name=action value="|
       . $locale->text('Print') . qq|">
     <input class=submit type=submit name=action value="|
-      . $locale->text('E-mail') . qq|">|;
+      . $locale->text('E-mail') . qq|"> |;
     print qq|<input class=submit type=submit name=action value="|
-      . $locale->text('Storno') . qq|">| unless ($form->{storno});
+      . $locale->text('Storno') . qq|"> | if ($show_storno);
     print qq|<input class=submit type=submit name=action value="|
       . $locale->text('Post Payment') . qq|">
 |;
@@ -1148,18 +1175,20 @@ if ($form->{type} eq "credit_note") {
       <input class=submit type=submit name=action value="|
         . $locale->text('Print and Post') . qq|">
       <input class=submit type=submit name=action value="|
-        . $locale->text('Post') . qq|">|;
+        . $locale->text('Post') . qq|"> | .
+        NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
+                         '-class' => 'submit'));
     }
   }
 
   # button for saving history
   if($form->{id} ne "") {
     print qq|
-         <input type=button class=submit onclick=set_history_window(|
-         . $form->{id} 
-         . qq|); name=history id=history value=|
-         . $locale->text('history') 
-         . qq|>|;
+         <input type="button" class="submit" onclick="set_history_window(|
+         . Q($form->{id})
+         . qq|);" name="history" id="history" value="|
+         . $locale->text('history')
+         . qq|">|;
   }
   # /button for saving history
 
@@ -1170,7 +1199,10 @@ if ($form->{type} eq "credit_note") {
 <input type=hidden name=rowcount value=$form->{rowcount}>
 
 <input name=callback type=hidden value="$form->{callback}">
-
+|
+. $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
+. $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
+. qq|
 <input type=hidden name=path value=$form->{path}>
 <input type=hidden name=login value=$form->{login}>
 <input type=hidden name=password value=$form->{password}>
@@ -1431,44 +1463,28 @@ sub post {
   $form->{id} = 0 if $form->{postasnew};
 
   # get new invnumber in sequence if no invnumber is given or if posasnew was requested
-  if (!$form->{invnumber} || $form->{postasnew}) {
+  if ($form->{postasnew}) {
     if ($form->{type} eq "credit_note") {
-      $form->{invnumber} = $form->update_defaults(\%myconfig, "cnnumber");
+      undef($form->{cnnumber});
     } else {
-      $form->{invnumber} = $form->update_defaults(\%myconfig, "invnumber");
+      undef($form->{invnumber});
     }
   }
+
   relink_accounts();
-  if ($print_post) {
-    if (!(IS->post_invoice(\%myconfig, \%$form))) {
-      $form->error($locale->text('Cannot post invoice!'));
-    }
-    # saving the history
-       if(!exists $form->{addition}) {
-         $form->{addition} = "PRINTED AND POSTED";
-         $form->save_history($form->dbconnect(\%myconfig));
-    }
-    # /saving the history
-    
-  } else {
-      if (IS->post_invoice(\%myconfig, \%$form)){
-       # saving the history
-               if(!exists $form->{addition}) {
-                       if($form->{storno}) {
-                               $form->{addition} = "STORNO";
-                       }
-                       else {
-                               $form->{addition} = "POSTED";
-                       }
-                       $form->save_history($form->dbconnect(\%myconfig));
-       }
-       # /saving the history
-    
-       $form->redirect(
-            $form->{label} . " $form->{invnumber} " . $locale->text('posted!'));
-       }
-    $form->error($locale->text('Cannot post invoice!'));
+  $form->error($locale->text('Cannot post invoice!'))
+    unless IS->post_invoice(\%myconfig, \%$form);
+  remove_draft() if $form->{remove_draft};
+
+  if(!exists $form->{addition}) {
+    $form->{addition} = $print_post     ? "PRINTED AND POSTED" :
+                        $form->{storno} ? "STORNO"             :
+                                          "POSTED";
+    $form->save_history($form->dbconnect(\%myconfig));
   }
+  
+  $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
+    unless $print_post;
 
   $lxdebug->leave_sub();
 }
@@ -1505,6 +1521,18 @@ sub storno {
     $form->error($locale->text('Cannot storno storno invoice!'));
   }
 
+  if (IS->has_storno(\%myconfig, $form, "ar")) {
+    $form->error($locale->text("Invoice has already been storno'd!"));
+  }
+
+  map({ my $key = $_; delete($form->{$key})
+          unless (grep({ $key eq $_ } qw(path login password id type))); }
+      keys(%{ $form }));
+
+  &invoice_links;
+  &prepare_invoice;
+  relink_accounts();
+
   $form->{storno_id} = $form->{id};
   $form->{storno} = 1;
   $form->{id} = "";
@@ -1635,3 +1663,15 @@ sub yes {
 
   $lxdebug->leave_sub();
 }
+
+sub e_mail {
+  $lxdebug->enter_sub();
+
+  $print_post = 1;
+
+  &post;
+
+  &edit_e_mail;
+
+  $lxdebug->leave_sub();
+}