Pflichtenhefte: Anlegen von Angeboten/Aufträgen
authorMoritz Bunkus <m.bunkus@linet-services.de>
Thu, 11 Jul 2013 09:50:29 +0000 (11:50 +0200)
committerMoritz Bunkus <m.bunkus@linet-services.de>
Tue, 1 Apr 2014 11:05:57 +0000 (13:05 +0200)
SL/Controller/RequirementSpecOrder.pm
js/locale/de.js
js/requirement_spec.js
locale/de/all
templates/webpages/requirement_spec_order/_assignment_form.html
templates/webpages/requirement_spec_order/list.html
templates/webpages/requirement_spec_order/new.html

index 696b862..1c43cb4 100644 (file)
@@ -5,17 +5,24 @@ use utf8;
 
 use parent qw(SL::Controller::Base);
 
+use List::MoreUtils qw(uniq);
+use List::Util qw(first);
+
 use SL::ClientJS;
 use SL::DB::Customer;
+use SL::DB::Order;
 use SL::DB::Part;
 use SL::DB::RequirementSpec;
 use SL::DB::RequirementSpecOrder;
 use SL::Helper::Flash;
 use SL::Locale::String;
 
+use constant TAB_ID => 'ui-tabs-4';
+
 use Rose::Object::MakeMethods::Generic
 (
-  'scalar --get_set_init' => [ qw(requirement_spec js all_customers all_parts) ],
+  scalar                  => [ qw(parts) ],
+  'scalar --get_set_init' => [ qw(requirement_spec js h_unit_name all_customers all_parts) ],
 );
 
 __PACKAGE__->run_before('setup');
@@ -24,7 +31,6 @@ __PACKAGE__->run_before('setup');
 # actions
 #
 
-
 sub action_list {
   my ($self) = @_;
 
@@ -35,7 +41,35 @@ sub action_new {
   my ($self) = @_;
 
   my $html   = $self->render('requirement_spec_order/new', { output => 0 }, make_part_title => sub { $_[0]->partnumber . ' ' . $_[0]->description });
-  $self->js->html('#ui-tabs-4', $html)
+  $self->js->html('#' . TAB_ID(), $html)
+           ->render($self);
+}
+
+sub action_create {
+  my ($self)         = @_;
+
+  # 1. Update sections with selected part IDs.
+  my $section_attrs  = $::form->{sections} || [];
+  my $sections       = SL::DB::Manager::RequirementSpecItem->get_all(where => [ id => [ map { $_->{id} } @{ $section_attrs } ] ]);
+  my %sections_by_id = map { ($_->{id} => $_) } @{ $sections };
+
+  $sections_by_id{ $_->{id} }->update_attributes(order_part_id => $_->{order_part_id}) for @{ $section_attrs };
+
+  # 2. Create actual quotation/order.
+  my $order = $self->create_order(sections => $sections);
+  $order->save;
+
+  $self->requirement_spec->orders(
+    @{ $self->requirement_spec->orders },
+    SL::DB::RequirementSpecOrder->new(order => $order, version => $self->requirement_spec->version)
+  );
+  $self->requirement_spec->save;
+  $self->init_requirement_spec;
+
+  # 3. Notify the user and return to list.
+  my $html = $self->render('requirement_spec_order/list', { output => 0 });
+  $self->js->html('#' . TAB_ID(), $html)
+           ->flash('info', $::form->{quotation} ? t8('Sales quotation #1 has been created.', $order->quonumber) : t8('Sales order #1 has been created.', $order->ordnumber))
            ->render($self);
 }
 
@@ -43,7 +77,7 @@ sub action_edit_assignment {
   my ($self) = @_;
 
   my $html   = $self->render('requirement_spec_order/edit_assignment', { output => 0 }, make_part_title => sub { $_[0]->partnumber . ' ' . $_[0]->description });
-  $self->js->html('#ui-tabs-4', $html)
+  $self->js->html('#' . TAB_ID(), $html)
            ->render($self);
 }
 
@@ -53,7 +87,7 @@ sub action_save_assignment {
   SL::DB::RequirementSpecItem->new(id => $_->{id})->load->update_attributes(order_part_id => ($_->{order_part_id} || undef)) for @{ $sections };
 
   my $html = $self->render('requirement_spec_order/list', { output => 0 });
-  $self->js->html('#ui-tabs-4', $html)
+  $self->js->html('#' . TAB_ID(), $html)
            ->render($self);
 }
 
@@ -61,7 +95,7 @@ sub action_cancel {
   my ($self) = @_;
 
   my $html = $self->render('requirement_spec_order/list', { output => 0 });
-  $self->js->html('#ui-tabs-4', $html)
+  $self->js->html('#' . TAB_ID(), $html)
            ->render($self);
 }
 
@@ -74,7 +108,7 @@ sub setup {
 
   $::auth->assert('sales_quotation_edit');
   $::request->{layout}->use_stylesheet("${_}.css") for qw(jquery.contextMenu requirement_spec);
-  $::request->{layout}->use_javascript("${_}.js") for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
+  $::request->{layout}->use_javascript("${_}.js")  for qw(jquery.jstree jquery/jquery.contextMenu client_js requirement_spec);
 
   return 1;
 }
@@ -95,5 +129,69 @@ sub init_js {
 
 sub init_all_customers { SL::DB::Manager::Customer->get_all_sorted }
 sub init_all_parts     { SL::DB::Manager::Part->get_all_sorted     }
+sub init_h_unit_name   { first { SL::DB::Manager::Unit->find_by(name => $_) } qw(Std h Stunde) };
+
+sub load_parts_for_sections {
+  my ($self, %params) = @_;
+
+  $self->parts({ map { ($_->{id} => $_) } @{ SL::DB::Manager::Part->get_all(where => [ id => [ uniq map { $_->{order_part_id} } @{ $params{sections} } ] ]) } });
+}
+
+sub create_order_item {
+  my ($self, %params) = @_;
+
+  my $section         = $params{section};
+  my $item            = $params{item} || SL::DB::OrderItem->new;
+  my $part            = $self->parts->{ $section->order_part_id };
+  my $description     = $section->{keep_description} ? $item->description : $part->description;
+
+  if (!$section->{keep_description}) {
+    $description =  '<%fb_number%> <%title%>' unless $description =~ m{<%};
+    $description =~ s{<% (.+?) %>}{$section->$1}egx;
+  }
+
+  $item->assign_attributes(
+    parts_id    => $part->id,
+    description => $description,
+    qty         => $section->time_estimation * 1,
+    unit        => $self->h_unit_name,
+    sellprice   => $::form->round_amount($self->requirement_spec->hourly_rate, 2),
+    discount    => 0,
+    project_id  => $self->requirement_spec->project_id,
+  );
+
+  return $item;
+}
+
+sub create_order {
+  my ($self, %params) = @_;
+
+  $self->load_parts_for_sections(%params);
+
+  my @orderitems = map { $self->create_order_item(section => $_) } @{ $params{sections} };
+  my $employee   = SL::DB::Manager::Employee->current;
+  my $customer   = SL::DB::Customer->new(id => $::form->{customer_id})->load;
+  my $order      = SL::DB::Order->new(
+    globalproject_id        => $self->requirement_spec->project_id,
+    transdate               => DateTime->today_local,
+    reqdate                 => $::form->{quotation} && $customer->payment_id ? $customer->payment->calc_date : undef,
+    quotation               => !!$::form->{quotation},
+    orderitems              => \@orderitems,
+    customer_id             => $customer->id,
+    taxincluded             => $customer->taxincluded,
+    intnotes                => $customer->notes,
+    language_id             => $customer->language_id,
+    payment_id              => $customer->payment_id,
+    taxzone_id              => $customer->taxzone_id,
+    employee_id             => $employee->id,
+    salesman_id             => $employee->id,
+    transaction_description => $self->requirement_spec->displayable_name,
+    currency_id             => $::instance_conf->get_currency_id,
+  );
+
+  $order->calculate_prices_and_taxes;
+
+  return $order;
+}
 
 1;
index 0739ec3..38335a8 100644 (file)
@@ -8,7 +8,9 @@ namespace("kivi").setupLocale({
 "Copy":"Kopieren",
 "Copy requirement spec":"Pflichtenheft kopieren",
 "Copy template":"Vorlage kopieren",
+"Create":"Anlegen",
 "Create PDF":"PDF erzeugen",
+"Create new quotation/order":"Neues Angebot/neuen Auftrag anlegen",
 "Create new qutoation/order":"Neues Angebot/neuen Auftrag anlegen",
 "Create new version":"Neue Version anlegen",
 "Database Connection Test":"Test der Datenbankverbindung",
index 608c644..bf1e230 100644 (file)
@@ -332,7 +332,7 @@ ns.standard_quotation_order_ajax_call = function(key, opt) {
   var data = 'action=RequirementSpecOrder/' + key
            + '&' + $('#requirement_spec_id').serialize();
 
-  if (key == 'save_assignment')
+  if ((key == 'save_assignment') || (key == 'create'))
     data += '&' + $('#quotations_and_orders_article_assignment_form').serialize();
   else
     data += '&id=' + encodeURIComponent(ns.find_quotation_order_id(opt.$trigger));
@@ -538,6 +538,15 @@ ns.create_context_menus = function(is_template) {
     }, general_actions)
   });
 
+  $.contextMenu({
+    selector: '.quotations-and-orders-new-context-menu',
+    items:    $.extend({
+        heading: { name: kivi.t8('Create new quotation/order'), className: 'context-menu-heading'    }
+      , create:  { name: kivi.t8('Create'), icon: "edit",  callback: ns.standard_quotation_order_ajax_call }
+      , cancel:  { name: kivi.t8('Cancel'), icon: "close", callback: ns.standard_quotation_order_ajax_call }
+    }, general_actions)
+  });
+
   $.contextMenu({
     selector: '#content',
     items:    general_actions
index d1cefb9..a41548b 100755 (executable)
@@ -540,6 +540,7 @@ $self->{texts} = {
   'Could not spawn the printer command.' => 'Die Druckanwendung konnte nicht gestartet werden.',
   'Could not update prices!'    => 'Preise konnten nicht aktualisiert werden!',
   'Country'                     => 'Land',
+  'Create'                      => 'Anlegen',
   'Create Assembly'             => 'Erzeugnis fertigen',
   'Create Chart of Accounts'    => 'Zu verwendender Kontenplan',
   'Create Dataset'              => 'Neue Datenbank anlegen',
@@ -593,6 +594,7 @@ $self->{texts} = {
   'Create new payment term'     => 'Neue Zahlungsbedingung anlegen',
   'Create new project type'     => 'Neuen Projekttypen anlegen',
   'Create new quotation or order' => 'Neues Angebot oder neuen Auftrag anlegen',
+  'Create new quotation/order'  => 'Neues Angebot/neuen Auftrag anlegen',
   'Create new qutoation/order'  => 'Neues Angebot/neuen Auftrag anlegen',
   'Create new templates from master templates' => 'Neue Druckvorlagen aus Vorlagensatz erstellen',
   'Create new version'          => 'Neue Version anlegen',
@@ -1951,9 +1953,11 @@ $self->{texts} = {
   'Sales margin'                => 'Marge',
   'Sales margin %'              => 'Marge prozentual',
   'Sales net amount'            => 'VK-Betrag',
+  'Sales order #1 has been created.' => 'Kundenauftrag #1 wurde angelegt.',
   'Sales price'                 => 'VK-Preis',
   'Sales price total'           => 'VK-Betrag',
   'Sales quotation'             => 'Angebot',
+  'Sales quotation #1 has been created.' => 'Angebot #1 wurde angelegt.',
   'Salesman'                    => 'Verkäufer/in',
   'Salesman (database ID)'      => 'Verkäufer (Datenbank-ID)',
   'Salesperson'                 => 'Verkäufer',
index f32e715..6a15f23 100644 (file)
@@ -1,29 +1,29 @@
 [%- USE HTML -%][%- USE LxERP -%][%- USE L -%][%- USE P -%]
 [% SET style="width: 400px" %]
 
-<table>
-[% IF for_new %]
- <tr>
-  <td>[% LxERP.t8("Record type to create") %]:</td>
-  <td>[% L.select_tag('quotation', [ [ 1, LxERP.t8('Sales quotation') ], [ 0, LxERP.t8('Sales Order') ] ], style=style, no_id=1) %]</td>
- </tr>
+<form id="quotations_and_orders_article_assignment_form">
+ <table>
+  [% IF for_new %]
+  <tr>
+   <td>[% LxERP.t8("Record type to create") %]:</td>
+   <td>[% L.select_tag('quotation', [ [ 1, LxERP.t8('Sales quotation') ], [ 0, LxERP.t8('Sales Order') ] ], style=style, no_id=1) %]</td>
+  </tr>
 
- <tr>
-  <td>[% LxERP.t8("Customer") %]:</td>
-  <td>[% L.select_tag('customer_id', SELF.all_customers, default=SELF.requirement_spec.customer_id, title_key='name', style=style, no_id=1) %]</td>
- </tr>
-[% END %]
 <tr>
+   <td>[% LxERP.t8("Customer") %]:</td>
+   <td>[% L.select_tag('customer_id', SELF.all_customers, default=SELF.requirement_spec.customer_id, title_key='name', style=style, no_id=1) %]</td>
 </tr>
+  [% END %]
 
- <tr>
-  <td>[% LxERP.t8("Assign the following article to all sections") %]:</td>
-  <td>
-   [% L.select_tag('quotations_and_orders_dummy', SELF.all_parts, default=INSTANCE_CONF.get_requirement_spec_section_order_part_id, title_sub=\make_part_title, id='quoations_and_orders_order_id', style=style) %]
-   [% L.button_tag('kivi.requirement_spec.assign_order_part_id_to_all()', LxERP.t8('Assign article')) %]
-  </td>
- </tr>
-</table>
 <tr>
+   <td>[% LxERP.t8("Assign the following article to all sections") %]:</td>
+   <td>
+    [% L.select_tag('quotations_and_orders_dummy', SELF.all_parts, default=INSTANCE_CONF.get_requirement_spec_section_order_part_id, title_sub=\make_part_title, id='quoations_and_orders_order_id', style=style) %]
+    [% L.button_tag('kivi.requirement_spec.assign_order_part_id_to_all()', LxERP.t8('Assign article')) %]
+   </td>
 </tr>
+ </table>
 
-<form id="quotations_and_orders_article_assignment_form">
  <table style="width: 100%">
   <thead>
    <tr class="listheading">
index 4e0c15d..775ce18 100644 (file)
        [% LxERP.t8("working copy") %]
       [% END %]
      </td>
-     <td>[% HTML.escape(rs_order.order.quotation ? rs_order.order.quonumber : rs_order.order.ordnumber) %]</td>
+     <td>
+      <a href="oe.pl?action=edit&id=[% HTML.url(rs_order.order_id) %]&type=[% HTML.url(rs_order.order.type) %]">
+       [% HTML.escape(rs_order.order.quotation ? rs_order.order.quonumber : rs_order.order.ordnumber) %]
+      </a>
+     </td>
      <td>[% HTML.escape(rs_order.order.transaction_description) %]</td>
      <td>[% rs_order.order.transdate.to_kivitendo(precision='day') %]</td>
      <td>[% rs_order.itime.to_kivitendo(precision='day') %]</td>
index 7aaabd7..5be9660 100644 (file)
@@ -1,5 +1,5 @@
 [%- USE LxERP -%][%- USE L -%]
-<div id="quotations_and_orders_new" class="quotations-and-orders-edit-assignment-context-menu">
+<div id="quotations_and_orders_new" class="quotations-and-orders-new-context-menu">
  <h2>[% LxERP.t8("Create new quotation or order") %]</h2>
 
  [% INCLUDE 'requirement_spec_order/_assignment_form.html'