Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision c8774e89

Von Tamino Steinert vor etwa 2 Jahren hinzugefügt

  • ID c8774e89fea91da739dd83b9b867ad0ab6eb9956
  • Vorgänger 08e12ce7
  • Nachfolger 6fb26f9a

Workflow: delivery_order ↔ reclamation

Unterschiede anzeigen:

SL/Controller/Reclamation.pm
69 69
                          save_and_show_email_dialog
70 70
                          workflow_save_and_sales_or_purchase_reclamation
71 71
                          save_and_order
72
                          save_and_delivery_order
72 73
                       )]);
73 74

  
74 75
__PACKAGE__->run_before('get_unalterable_data',
......
77 78
                          save_and_show_email_dialog
78 79
                          workflow_save_and_sales_or_purchase_reclamation
79 80
                          save_and_order
81
                          save_and_delivery_order
80 82
                        )]);
81 83

  
82 84
#
......
121 123
  );
122 124
}
123 125

  
126
sub action_add_from_delivery_order {
127
  my ($self) = @_;
128

  
129
  unless ($::form->{from_id}) {
130
    $self->js->flash('error', t8("Can't create new reclamation. No 'from_id' was given."));
131
    return $self->js->render();
132
  }
133

  
134
  require SL::DB::DeliveryOrder;
135
  my $delivery_order = SL::DB::DeliveryOrder->new(id => $::form->{from_id})->load;
136
  my $reclamation = $delivery_order->convert_to_reclamation();
137

  
138
  $self->reclamation($reclamation);
139

  
140
  $self->reinit_after_new_reclamation();
141

  
142
  $self->render(
143
    'reclamation/form',
144
    title => $self->get_title_for('add'),
145
    %{$self->{template_args}},
146
  );
147
}
148

  
124 149
# edit an existing reclamation
125 150
sub action_edit {
126 151
  my ($self) = @_;
......
515 540
  $_[0]->workflow_save_and_sales_or_purchase_reclamation();
516 541
}
517 542

  
543
# save the reclamation and redirect to the frontend subroutine for a new
544
# delivery order
545
sub action_save_and_delivery_order {
546
  my ($self) = @_;
547

  
548
  my $to_type = $self->reclamation->is_sales ? 'sales_delivery_order'
549
                                             : 'purchase_delivery_order';
550
  $self->save_and_redirect_to(
551
    controller => 'do.pl',
552
    action     => 'add_from_reclamation',
553
    type       => $to_type,
554
    from_id    => $self->reclamation->id,
555
  );
556
}
557

  
518 558
# set form elements in respect to a changed customer or vendor
519 559
#
520 560
# This action is called on an change of the customer/vendor picker.
......
1617 1657
  my %allowed_linked_records = map {$_ => 1} qw(
1618 1658
    SL::DB::Reclamation
1619 1659
    SL::DB::Order
1660
    SL::DB::DeliveryOrder
1620 1661
  );
1621 1662
  my %allowed_linked_record_items = map {$_ => 1} qw(
1622 1663
    SL::DB::ReclamationItem
1623 1664
    SL::DB::OrderItem
1665
    SL::DB::DeliveryOrderItem
1624 1666
  );
1625 1667

  
1626 1668
  my $from_record_id = delete $::form->{converted_from_record_id};
......
2133 2175
            $::instance_conf->get_reclamation_warn_no_reqdate,
2134 2176
          ],
2135 2177
        ],
2178
        action => [
2179
          t8('Save and Delivery Order'),
2180
          call      => [
2181
            'kivi.Reclamation.save', 'save_and_delivery_order',
2182
            $::instance_conf->get_reclamation_warn_duplicate_parts,
2183
            $::instance_conf->get_reclamation_warn_no_reqdate,
2184
          ],
2185
        ],
2136 2186
      ], # end of combobox "Workflow"
2137 2187

  
2138 2188
      combobox => [
SL/DB/DeliveryOrder.pm
124 124
  return $cloned;
125 125
}
126 126

  
127
sub convert_to_reclamation {
128
  my ($self, %params) = @_;
129

  
130
  $params{destination_type} = $self->is_sales ? 'sales_reclamation'
131
                                              : 'purchase_reclamation';
132

  
133
  my $reclamation = SL::DB::Reclamation->new_from($self, %params);
134

  
135
  return $reclamation;
136
}
137

  
127 138
sub new_from {
128 139
  my ($class, $source, %params) = @_;
129 140

  
130
  croak("Unsupported source object type '" . ref($source) . "'") unless ref($source) eq 'SL::DB::Order';
141
  my %allowed_sources = map { $_ => 1 } qw(
142
    SL::DB::Reclamation
143
    SL::DB::Order
144
  );
145
  unless( $allowed_sources{ref $source} ) {
146
    croak("Unsupported source object type '" . ref($source) . "'");
147
  }
131 148

  
132
  my ($item_parent_id_column, $item_parent_column);
149
  my %record_args = (
150
    donumber => undef,
151
    employee => SL::DB::Manager::Employee->current,
152
    closed    => 0,
153
    delivered => 0,
154
    order_type => $params{type},
155
    transdate => DateTime->today_local,
156
  );
133 157

  
134
  if (ref($source) eq 'SL::DB::Order') {
135
    $item_parent_id_column = 'trans_id';
136
    $item_parent_column    = 'order';
158
  if ( ref($source) eq 'SL::DB::Order' ) {
159
    map{ ( $record_args{$_} = $source->$_ ) } # {{{ for vim folds
160
    qw(
161
      billing_address_id
162
      cp_id
163
      currency_id
164
      cusordnumber
165
      customer_id
166
      delivery_term_id
167
      department_id
168
      globalproject_id
169
      intnotes
170
      language_id
171
      notes
172
      ordnumber
173
      payment_id
174
      reqdate
175
      salesman_id
176
      shippingpoint
177
      shipvia
178
      taxincluded
179
      taxzone_id
180
      transaction_description
181
      vendor_id
182
    );
183
    # }}} for vim folds
184
  } elsif ( ref($source) eq 'SL::DB::Reclamation' ) {
185
    map{ ( $record_args{$_} = $source->$_ ) } # {{{ for vim folds
186
      #billing_address_id #TODO(Tamino): add billing_address_id to reclamation
187
    qw(
188
      currency_id
189
      customer_id
190
      delivery_term_id
191
      department_id
192
      globalproject_id
193
      intnotes
194
      language_id
195
      notes
196
      payment_id
197
      reqdate
198
      salesman_id
199
      shippingpoint
200
      shipvia
201
      taxincluded
202
      taxzone_id
203
      transaction_description
204
      vendor_id
205
    );
206
    $record_args{cp_id} = $source->contact_id;
207
    $record_args{cusordnumber} = $source->cv_record_number;
208
    # }}} for vim folds
137 209
  }
138 210

  
139
  my %args = ( map({ ( $_ => $source->$_ ) } qw(cp_id currency_id customer_id cusordnumber delivery_term_id department_id employee_id globalproject_id intnotes language_id notes
140
                                                ordnumber payment_id reqdate salesman_id shippingpoint shipvia taxincluded taxzone_id transaction_description vendor_id billing_address_id
141
                                             )),
142
               closed    => 0,
143
               delivered => 0,
144
               order_type => $params{type},
145
               transdate => DateTime->today_local,
146
            );
147

  
148 211
  # Custom shipto addresses (the ones specific to the sales/purchase
149 212
  # record and not to the customer/vendor) are only linked from
150 213
  # shipto → delivery_orders. Meaning delivery_orders.shipto_id
151 214
  # will not be filled in that case.
152 215
  if (!$source->shipto_id && $source->id) {
153
    $args{custom_shipto} = $source->custom_shipto->clone($class) if $source->can('custom_shipto') && $source->custom_shipto;
154

  
216
    $record_args{custom_shipto} = $source->custom_shipto->clone($class) if $source->can('custom_shipto') && $source->custom_shipto;
155 217
  } else {
156
    $args{shipto_id} = $source->shipto_id;
218
    $record_args{shipto_id} = $source->shipto_id;
157 219
  }
158 220

  
159 221
  # infer type from legacy fields if not given
160
  $args{order_type} //= $source->customer_id ? 'sales_delivery_order'
222
  $record_args{order_type} //= $source->customer_id ? 'sales_delivery_order'
161 223
                      : $source->vendor_id   ? 'purchase_delivery_order'
162 224
                      : $source->is_sales    ? 'sales_delivery_order'
163 225
                      : croak "need some way to set delivery order type from source";
164 226

  
165
  my $delivery_order = $class->new(%args);
227
  my $delivery_order = $class->new(%record_args);
166 228
  $delivery_order->assign_attributes(%{ $params{attributes} }) if $params{attributes};
167
  my $items          = delete($params{items}) || $source->items_sorted;
168
  my %item_parents;
169

  
170
  # do not copy items when converting to supplier delivery order
171
  my @items = $delivery_order->is_type(SUPPLIER_DELIVERY_ORDER_TYPE) ? () : map {
172
    my $source_item      = $_;
173
    my $source_item_id   = $_->$item_parent_id_column;
174
    my @custom_variables = map { _clone_orderitem_cvar($_) } @{ $source_item->custom_variables };
175

  
176
    $item_parents{$source_item_id} ||= $source_item->$item_parent_column;
177
    my $item_parent                  = $item_parents{$source_item_id};
178

  
179
    my $current_do_item = SL::DB::DeliveryOrderItem->new(map({ ( $_ => $source_item->$_ ) }
180
                                         qw(base_qty cusordnumber description discount lastcost longdescription marge_price_factor parts_id price_factor price_factor_id
181
                                            project_id qty reqdate sellprice serialnumber transdate unit active_discount_source active_price_source
182
                                         )),
183
                                   custom_variables => \@custom_variables,
184
                                   ordnumber        => ref($item_parent) eq 'SL::DB::Order' ? $item_parent->ordnumber : $source_item->ordnumber,
185
                                 );
186
    $current_do_item->{"converted_from_orderitems_id"} = $_->{id} if ref($item_parent) eq 'SL::DB::Order';
187
    $current_do_item;
188
  } @{ $items };
229

  
230
  my $items = delete($params{items}) || $source->items_sorted;
231
  my @items = $delivery_order->is_type(SUPPLIER_DELIVERY_ORDER_TYPE) ? ()
232
              : map { SL::DB::DeliveryOrderItem->new_from($_) } @{ $items };
189 233

  
190 234
  @items = grep { $params{item_filter}->($_) } @items if $params{item_filter};
191 235
  @items = grep { $_->qty * 1 } @items if $params{skip_items_zero_qty};
SL/DB/DeliveryOrderItem.pm
35 35

  
36 36
# methods
37 37

  
38
sub new_from {
39
  my ($class, $source, %params) = @_;
40

  
41
  my %allowed_sources = map { $_ => 1 } qw(
42
      SL::DB::ReclamationItem
43
      SL::DB::OrderItem
44
  );
45
  unless( $allowed_sources{ref $source} ) {
46
    croak("Unsupported source object type '" . ref($source) . "'");
47
  }
48

  
49
  my @custom_variables = map { _clone_cvar_for_delivery_order_item($_) } @{ $source->custom_variables };
50

  
51
  my %item_args;
52
  if (ref($source) eq 'SL::DB::ReclamationItem') {
53
    map { $item_args{$_} = $source->$_ } # {{{ for vim folds
54
    qw(
55
      active_discount_source
56
      active_price_source
57
      base_qty
58
      description
59
      discount
60
      lastcost
61
      longdescription
62
      parts_id
63
      position
64
      price_factor
65
      price_factor_id
66
      pricegroup_id
67
      project_id
68
      qty
69
      reqdate
70
      sellprice
71
      serialnumber
72
      unit
73
    );
74
    $item_args{custom_variables} = \@custom_variables;
75
    # }}} for vim folds
76
  } elsif (ref($source) eq 'SL::DB::OrderItem') {
77
    map { $item_args{$_} = $source->$_ } # {{{ for vim folds
78
    qw(
79
      active_discount_source
80
      active_price_source
81
      base_qty
82
      cusordnumber
83
      description
84
      discount
85
      lastcost
86
      longdescription
87
      marge_price_factor
88
      parts_id
89
      price_factor
90
      price_factor_id
91
      project_id
92
      qty
93
      reqdate
94
      sellprice
95
      serialnumber
96
      transdate
97
      unit
98
    );
99
    $item_args{custom_variables} = \@custom_variables;
100
    $item_args{ordnumber}        = ref($source->record) eq 'SL::DB::Order' ? $source->record->ordnumber : $source->ordnumber;
101
    # }}} for vim folds
102
  }
103

  
104
  my $item = $class->new(%item_args);
105

  
106
  my $source_table = '';
107
  if( ref($source) eq 'SL::DB::OrderItem' ) {
108
    $source_table = 'orderitems';
109
  } elsif ( ref($source) eq 'SL::DB::ReclamationItem' ) {
110
    $source_table = 'reclamation_items';
111
  }
112
  $item->{"converted_from_". $source_table ."_id"} = $_->{id};
113

  
114
  return $item;
115
}
116

  
117
sub _clone_cvar_for_delivery_order_item {
118
  my ($cvar) = @_;
119

  
120
  my $cloned = $_->clone_and_reset;
121
  $cloned->sub_module('delivery_order_items');
122

  
123
  return $cloned;
124
}
125

  
38 126
sub record { goto &delivery_order }
39 127
sub record_id { goto &delivery_order_id }
40 128

  
SL/DB/Reclamation.pm
216 216
  return $order;
217 217
}
218 218

  
219
sub convert_to_delivery_order {
220
  my ($self, %params) = @_;
221

  
222
  my $delivery_order;
223
  if (!$self->db->with_transaction(sub {
224
    require SL::DB::DeliveryOrder;
225
    $delivery_order = SL::DB::DeliveryOrder->new_from($self, %params);
226
    $delivery_order->save;
227
    $self->link_to_record($delivery_order);
228
    # TODO extend link_to_record for items, otherwise long-term no d.r.y.
229
    foreach my $item (@{ $delivery_order->items }) {
230
      foreach (qw(reclamation_items)) {
231
        if ($item->{"converted_from_${_}_id"}) {
232
          die unless $item->{id};
233
          RecordLinks->create_links('dbh'        => $self->db->dbh,
234
                                    'mode'       => 'ids',
235
                                    'from_table' => $_,
236
                                    'from_ids'   => $item->{"converted_from_${_}_id"},
237
                                    'to_table'   => 'delivery_order_items',
238
                                    'to_id'      => $item->{id},
239
          ) || die;
240
          delete $item->{"converted_from_${_}_id"};
241
        }
242
      }
243
    }
244

  
245
    $self->update_attributes(delivered => 1) unless $::instance_conf->get_shipped_qty_require_stock_out;
246
    1;
247
  })) {
248
    return undef, $self->db->error->db_error->db_error;
249
  }
250

  
251
  return $delivery_order, undef;
252
}
253

  
219 254
#TODO(Werner): überprüfen ob alle Felder richtig gestetzt werden
220 255
sub new_from {
221 256
  my ($class, $source, %params) = @_;
222 257
  my %allowed_sources = map { $_ => 1 } qw(
223 258
    SL::DB::Reclamation
224 259
    SL::DB::Order
260
    SL::DB::DeliveryOrder
225 261
  );
226 262
  unless( $allowed_sources{ref $source} ) {
227 263
    croak("Unsupported source object type '" . ref($source) . "'");
......
239 275
    #Order
240 276
    { from => 'sales_order',             to => 'sales_reclamation',    abbr => 'sosr', },
241 277
    { from => 'purchase_order',          to => 'purchase_reclamation', abbr => 'popr', },
278
    #Delivery Order
279
    { from => 'sales_delivery_order',    to => 'sales_reclamation',    abbr => 'sdsr', },
280
    { from => 'purchase_delivery_order', to => 'purchase_reclamation', abbr => 'pdpr', },
242 281
  );
243 282
  my $from_to = (grep { $_->{from} eq $source->type && $_->{to} eq $destination_type} @from_tos)[0];
244 283
  if (!$from_to) {
......
310 349
    $record_args{contact_id} = $source->cp_id;
311 350
    $record_args{cv_record_number} = $source->cusordnumber;
312 351
    # }}} for vim folds
352
  } elsif ( $is_abbr_any->(qw(sdsr pdpr)) ) { #DeliveryOrder
353
    map { $record_args{$_} = $source->$_ } # {{{ for vim folds
354
    qw(
355
      currency_id
356
      customer_id
357
      delivery_term_id
358
      department_id
359
      globalproject_id
360
      intnotes
361
      language_id
362
      notes
363
      payment_id
364
      salesman_id
365
      shippingpoint
366
      shipvia
367
      tax_point
368
      taxincluded
369
      taxzone_id
370
      transaction_description
371
      vendor_id
372
    );
373
    $record_args{contact_id} = $source->cp_id;
374
    $record_args{cv_record_number} = $source->cusordnumber;
375
    # }}} for vim folds
313 376
  }
314 377

  
315 378
  if ( ($from_to->{from} =~ m{sales}) && ($from_to->{to} =~ m{purchase}) ) {
SL/DB/ReclamationItem.pm
54 54
    qw(
55 55
      SL::DB::ReclamationItem
56 56
      SL::DB::OrderItem
57
      SL::DB::DeliveryOrderItem
57 58
    )
58 59
  ) {
59 60
    croak("Unsupported source object type '" . ref($source) . "'");
......
78 79
      pricegroup_id project_id qty reqdate sellprice serialnumber unit
79 80
    );
80 81
    $item_args{custom_variables} = \@custom_variables;
82
  } elsif (ref($source) eq 'SL::DB::DeliveryOrderItem') {
83
    map { $item_args{$_} = $source->$_ } qw(
84
      active_discount_source active_price_source base_qty description discount
85
      lastcost longdescription parts_id position price_factor price_factor_id
86
      pricegroup_id project_id qty reqdate sellprice serialnumber unit
87
    );
88
    $item_args{custom_variables} = \@custom_variables;
81 89
  }
82 90

  
83 91
  my $item = $class->new(%item_args);
bin/mozilla/do.pl
121 121
  $main::lxdebug->leave_sub();
122 122
}
123 123

  
124
sub add_from_reclamation {
125

  
126
  require SL::DB::Reclamation;
127
  my $reclamation = SL::DB::Reclamation->new(id => $::form->{from_id})->load;
128
  my ($delivery_order, $error) = $reclamation->convert_to_delivery_order();
129
  if($error) {
130
    croak("Error while converting: " . $error);
131
  }
132

  
133
  # edit new saved delivery order
134
  $::form->{id} = $delivery_order->id;
135
  edit();
136
}
137

  
124 138
sub edit {
125 139
  $main::lxdebug->enter_sub();
126 140

  
......
369 383

  
370 384
      'separator',
371 385

  
372
      action => [
373
        t8('Invoice'),
374
        submit => [ '#form', { action => "invoice" } ],
375
        disabled => !$::form->{id} ? t8('This record has not been saved yet.') : undef,
376
        confirm  => $::form->{delivered}                                                                         ? undef
377
                  : ($::form->{vc} eq 'customer' && $::instance_conf->get_sales_delivery_order_check_stocked)    ? t8('This record has not been stocked out. Proceed?')
378
                  : ($::form->{vc} eq 'vendor'   && $::instance_conf->get_purchase_delivery_order_check_stocked) ? t8('This record has not been stocked in. Proceed?')
379
                  :                                                                                                undef,
386
      combobox => [
387
        action => [ t8('Workflow') ],
388
        action => [
389
          t8('Invoice'),
390
          submit => [ '#form', { action => "invoice" } ],
391
          disabled => !$::form->{id} ? t8('This record has not been saved yet.') : undef,
392
          confirm  => $::form->{delivered}                                                                         ? undef
393
                    : ($::form->{vc} eq 'customer' && $::instance_conf->get_sales_delivery_order_check_stocked)    ? t8('This record has not been stocked out. Proceed?')
394
                    : ($::form->{vc} eq 'vendor'   && $::instance_conf->get_purchase_delivery_order_check_stocked) ? t8('This record has not been stocked in. Proceed?')
395
                    :                                                                                                undef,
396
        ],
397
        action => [
398
          t8('Save and Reclamation'),
399
          submit => [ '#form', { action => "save_and_reclamation" } ],
400
        ],
380 401
      ],
381 402

  
382 403
      combobox => [
......
1319 1340
  $main::lxdebug->leave_sub();
1320 1341
}
1321 1342

  
1343
sub save_and_reclamation {
1344
  my $form     = $main::form;
1345
  my $type     = $form->{type};
1346

  
1347
  # save the delivery order
1348
  save(no_redirect => 1);
1349

  
1350
  my $to_reclamation_type =
1351
    $type eq 'sales_delivery_order' ? 'sales_reclamation'
1352
                                    : 'purchase_reclamation';
1353
  $form->{callback} =
1354
    'controller.pl?action=Reclamation/add_from_delivery_order' .
1355
    '&type=' . $to_reclamation_type .
1356
    '&from_id=' . $form->escape($form->{id});
1357
  $form->redirect;
1358
}
1359

  
1322 1360
sub save_as_new {
1323 1361
  $main::lxdebug->enter_sub();
1324 1362

  

Auch abrufbar als: Unified diff