Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 77f30228

Von Tamino Steinert vor mehr als 2 Jahren hinzugefügt

  • ID 77f30228c677b48ce8dceb50793ed8f5829c14ab
  • Vorgänger c1ed1e91
  • Nachfolger 0743ffa0

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
36 36

  
37 37
# methods
38 38

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

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

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

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

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

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

  
115
  return $item;
116
}
117

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

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

  
124
  return $cloned;
125
}
126

  
39 127
sub record { goto &delivery_order }
40 128
sub record_id { goto &delivery_order_id }
41 129

  
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
118 118
  $main::lxdebug->leave_sub();
119 119
}
120 120

  
121
sub add_from_reclamation {
122

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

  
130
  # edit new saved delivery order
131
  $::form->{id} = $delivery_order->id;
132
  edit();
133
}
134

  
121 135
sub edit {
122 136
  $main::lxdebug->enter_sub();
123 137

  
......
366 380

  
367 381
      'separator',
368 382

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

  
379 400
      combobox => [
......
1303 1324
  $main::lxdebug->leave_sub();
1304 1325
}
1305 1326

  
1327
sub save_and_reclamation {
1328
  my $form     = $main::form;
1329
  my $type     = $form->{type};
1330

  
1331
  # save the delivery order
1332
  save(no_redirect => 1);
1333

  
1334
  my $to_reclamation_type =
1335
    $type eq 'sales_delivery_order' ? 'sales_reclamation'
1336
                                    : 'purchase_reclamation';
1337
  $form->{callback} =
1338
    'controller.pl?action=Reclamation/add_from_delivery_order' .
1339
    '&type=' . $to_reclamation_type .
1340
    '&from_id=' . $form->escape($form->{id});
1341
  $form->redirect;
1342
}
1343

  
1306 1344
sub save_as_new {
1307 1345
  $main::lxdebug->enter_sub();
1308 1346

  

Auch abrufbar als: Unified diff