Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 5775d2c6

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 5775d2c6bcce644cde37254ccdbca2e01531b147
  • Vorgänger 381b617c
  • Nachfolger 36c6b73d

Model::Record: übergebe Typ für new_from_workflow und ..._multi

Unterschiede anzeigen:

SL/Controller/DeliveryOrder.pm
108 108
  my $order = SL::DB::Order->new(id => $self->{converted_from_oe_id})->load;
109 109

  
110 110
  my $target_type = $::form->{type};
111
  my $delivery_order = SL::Model::Record->new_from_workflow($order, $target_type);
111
  my $delivery_order = SL::Model::Record->new_from_workflow($order, ref($self->order), $target_type);
112 112
  $self->order($delivery_order);
113 113

  
114 114
  $self->action_add;
......
120 120
  my $reclamation = SL::DB::Reclamation->new(id => $::form->{from_id})->load;
121 121
  my $target_type = $reclamation->is_sales ? 'rma_delivery_order'
122 122
                                           : 'supplier_delivery_order';
123
  my $delivery_order = SL::Model::Record->new_from_workflow($reclamation, $target_type);
123
  my $delivery_order = SL::Model::Record->new_from_workflow($reclamation, ref($self->order), $target_type);
124 124
  $self->{converted_from_reclamation_id} = $::form->{from_id};
125 125
  $self->order($delivery_order);
126 126

  
......
185 185
  my @multi_orders = map { SL::DB::DeliveryOrder->new(id => $_)->load } @multi_ids;
186 186
  $self->{converted_from_oe_id} = join ' ', map { $_->id } @multi_orders;
187 187
  my $target_type = SALES_DELIVERY_ORDER_TYPE();
188
  my $delivery_order = SL::Model::Record->new_from_workflow_multi(\@multi_orders, $target_type, sort_sources_by => 'transdate');
188
  my $delivery_order = SL::Model::Record->new_from_workflow_multi(\@multi_orders, ref($self->order), $target_type, sort_sources_by => 'transdate');
189 189
  $self->order($delivery_order);
190 190

  
191 191
  $self->action_edit();
......
1629 1629
  # always save
1630 1630
  $self->save();
1631 1631

  
1632
  my $delivery_order = SL::Model::Record->new_from_workflow($self->order, $destination_type, {});
1632
  my $delivery_order = SL::Model::Record->new_from_workflow($self->order, ref($self->order), $destination_type);
1633 1633
  $self->order($delivery_order);
1634 1634
  $self->{converted_from_oe_id} = delete $::form->{id};
1635 1635

  
......
1665 1665
  # always save
1666 1666
  $self->save();
1667 1667

  
1668
  my $delivery_order = SL::Model::Record->new_from_workflow($self->order, $destination_type, {});
1668
  my $delivery_order = SL::Model::Record->new_from_workflow($self->order, ref($self->order), $destination_type);
1669 1669
  $self->order($delivery_order);
1670 1670
  $self->{converted_from_oe_id} = delete $::form->{id};
1671 1671

  
SL/Controller/Order.pm
102 102
  my %params;
103 103
  my $target_type = $reclamation->is_sales ? SALES_ORDER_TYPE()
104 104
                                           : PURCHASE_ORDER_TYPE();
105
  my $order = SL::Model::Record->new_from_workflow($reclamation, $target_type);
105
  my $order = SL::Model::Record->new_from_workflow($reclamation, ref($self->order), $target_type);
106 106
  $self->{converted_from_reclamation_id} = $::form->{from_id};
107 107

  
108 108
  $self->order($order);
......
180 180
  my @multi_orders = map { SL::DB::Order->new(id => $_)->load } @multi_ids;
181 181
  $self->{converted_from_oe_id} = join ' ', map { $_->id } @multi_orders;
182 182
  my $target_type = "sales_order";
183
  my $order = SL::Model::Record->new_from_workflow_multi(\@multi_orders, $target_type, sort_sources_by => 'transdate');
183
  my $order = SL::Model::Record->new_from_workflow_multi(\@multi_orders, ref($self->order), $target_type, sort_sources_by => 'transdate');
184 184
  $self->order($order);
185 185

  
186 186
  $self->action_edit();
SL/Controller/Reclamation.pm
125 125
  }
126 126

  
127 127
  my $order = SL::DB::Order->new(id => $::form->{from_id})->load;
128
  my $reclamation = SL::Model::Record->new_from_workflow($order, $self->type);
128
  my $reclamation = SL::Model::Record->new_from_workflow($order, ref($self->reclamaiton), $self->type);
129 129

  
130 130
  $self->reclamation($reclamation);
131 131

  
......
151 151
  }
152 152

  
153 153
  my $delivery_order = SL::DB::DeliveryOrder->new(id => $::form->{from_id})->load;
154
  my $reclamation = SL::Model::Record->new_from_workflow($delivery_order, $self->type);
154
  my $reclamation = SL::Model::Record->new_from_workflow($delivery_order, ref($self->reclamaiton), $self->type);
155 155

  
156 156
  $self->reclamation($reclamation);
157 157

  
......
177 177
  }
178 178

  
179 179
  my $invoice = SL::DB::Invoice->new(id => $::form->{from_id})->load;
180
  my $reclamation = SL::Model::Record->new_from_workflow($invoice, $self->type);
180
  my $reclamation = SL::Model::Record->new_from_workflow($invoice, ref($self->reclamaiton), $self->type);
181 181

  
182 182
  $self->reclamation($reclamation);
183 183

  
......
205 205
  require SL::DB::PurchaseInvoice;
206 206
  my $invoice = SL::DB::PurchaseInvoice->new(id => $::form->{from_id})->load;
207 207
  $invoice->{type} = $invoice->invoice_type; #can't add type → invoice_type in SL/DB/PurchaseInvoice
208
  my $reclamation = SL::Model::Record->new_from_workflow($invoice, $self->type);
208
  my $reclamation = SL::Model::Record->new_from_workflow($invoice, ref($self->reclamaiton), $self->type);
209 209

  
210 210
  $self->reclamation($reclamation);
211 211

  
SL/Model/Record.pm
36 36
}
37 37

  
38 38
sub new_from_workflow {
39
  my ($class, $source_object, $target_subtype, %flags) = @_;
39
  my ($class, $source_object, $target_type, $target_subtype, %flags) = @_;
40 40

  
41 41
  $flags{destination_type} = $target_subtype;
42 42
  my %defaults_flags = (
......
44 44
  );
45 45
  %flags = (%defaults_flags, %flags);
46 46

  
47
  my %subtype_to_type = (
48
    # Order
49
    REQUEST_QUOTATION_TYPE() => "SL::DB::Order",
50
    PURCHASE_ORDER_TYPE()    => "SL::DB::Order",
51
    SALES_QUOTATION_TYPE()   => "SL::DB::Order",
52
    SALES_ORDER_TYPE()       => "SL::DB::Order",
53
    # DeliveryOrder
54
    SALES_DELIVERY_ORDER_TYPE()    => "SL::DB::DeliveryOrder",
55
    PURCHASE_DELIVERY_ORDER_TYPE() => "SL::DB::DeliveryOrder",
56
    RMA_DELIVERY_ORDER_TYPE()      => "SL::DB::DeliveryOrder",
57
    SUPPLIER_DELIVERY_ORDER_TYPE() => "SL::DB::DeliveryOrder",
58
    # Reclamation
59
    SALES_RECLAMATION_TYPE()    => "SL::DB::Reclamation",
60
    PURCHASE_RECLAMATION_TYPE() => "SL::DB::Reclamation",
61
  );
62
  my $target_type = $subtype_to_type{$target_subtype};
63
  unless ($target_type) {
64
    croak("Conversion not supported to $target_subtype");
65
  }
66

  
67 47
  my $target_object = ${target_type}->new_from($source_object, %flags);
68 48
  return $target_object;
69 49
}
70 50

  
71 51
sub new_from_workflow_multi {
72
  my ($class, $source_objects, $target_subtype, %flags) = @_;
73

  
74
  my %subtype_to_type = (
75
    # Order
76
    SALES_ORDER_TYPE() => "SL::DB::Order",
77
  );
78
  my $target_type = $subtype_to_type{$target_subtype};
79
  unless ($target_type) {
80
    croak("Conversion not supported to $target_subtype");
81
  }
52
  my ($class, $source_objects, $target_type, $target_subtype, %flags) = @_;
82 53

  
83 54
  my $target_object = ${target_type}->new_from_multi($source_objects, %flags);
84 55

  
......
255 226
  $new_attrs{employee}  = SL::DB::Manager::Employee->current;
256 227

  
257 228

  
258
  my $new_record = SL::Model::Record->new_from_workflow($changed_record, $saved_record->type, no_linked_records => 1, attributes => \%new_attrs);
229
  my $new_record = SL::Model::Record->new_from_workflow($changed_record, ref($changed_record), $saved_record->type, no_linked_records => 1, attributes => \%new_attrs);
259 230

  
260 231
  # return: nichts
261 232
  # fehler: exception
......
343 314

  
344 315
=item C<new_from_workflow>
345 316

  
346
Expects source_object, target_object, can have flags.
347
Creates a new record from a source_subtype by target_type->new_from(source_record).
348
Set default flag no_link_record to false and looks up the correct target_type.
317
Expects source_object, target_type, target_subtype and can have flags.
318
Creates a new record from a target_subtype by target_type->new_from(source_record).
319
Set default flag no_link_record to false.
349 320

  
350 321
Throws an error if the target_type doesn't exist.
351 322

  
......
353 324

  
354 325
=item C<new_from_workflow_multi>
355 326

  
356
Expects an arrayref with source_objects and a target_object, can have flags.
357
Creates a new record object from one or more source objects. For now only for orders.
358
Looks up the correct target type and throws an error if it doesn't exist.
327
Expects an arrayref with source_objects, target_type, target_subtype and can have flags.
328
Creates a new record object from one or more source objects.
359 329

  
360 330
Returns the new record object.
361 331

  
t/model/records.t
9 9
use Data::Dumper;
10 10
use Support::TestSetup;
11 11
use Test::Exception;
12
use List::Util qw(pairs);
12 13

  
13 14
use SL::DB::PaymentTerm;
14 15
use SL::DB::DeliveryTerm;
......
73 74

  
74 75

  
75 76
note "testing new_from_workflow for quotation";
76
foreach my $target_record_type ( qw(sales_order sales_delivery_order) ) {
77
foreach my $pair ( pairs @{['SL::DB::Order' => 'sales_order', 'SL::DB::DeliveryOrder' => 'sales_delivery_order']} ) {
77 78
  # TODO: invoice
78
  note "  testing from quotation -> $target_record_type";
79
  my $new_record = SL::Model::Record->new_from_workflow($sales_quotation1, $target_record_type);
79
  my ($target_record_type, $target_record_subtype) = @$pair;
80
  note "  testing from quotation -> $target_record_subtype";
81
  my $new_record = SL::Model::Record->new_from_workflow($sales_quotation1, $target_record_type, $target_record_subtype);
80 82

  
81 83
  is($new_record->closed, 0, "new quotation is open");
82 84
  # in the future closing sales quotations should probably happen as an after-save hook of orders,
......
84 86
  SL::Model::Record->save($new_record, objects_to_close => [ $sales_quotation1 ]);
85 87

  
86 88
  $new_record->load;
87
  cmp_ok($new_record->netamount, '==', 710, "converted $target_record_type netamount ok") if $new_record->can('netamount');
89
  cmp_ok($new_record->netamount, '==', 710, "converted $target_record_subtype netamount ok") if $new_record->can('netamount');
88 90

  
89 91
  # test whether quotations get closed when sales_order is created
90
  if ( $target_record_type eq 'sales_order' ) {
92
  if ( $target_record_subtype eq 'sales_order' ) {
91 93
    $sales_quotation1->load;
92 94
    is($sales_quotation1->closed, 1, "quotation is closed after creating an order");
93 95
  }
......
96 98

  
97 99
  my $record_history = SL::DB::Manager::History->find_by(trans_id => $new_record->id, addition => 'SAVED');
98 100
  ok($record_history->snumbers =~ m/_/, "history snumbers of record " . $record_history->snumbers . " ok");
99
  test_record_links($new_record, "converted $target_record_type");
101
  test_record_links($new_record, "converted $target_record_subtype");
100 102
};
101 103

  
102 104
note "testing new_from_workflow for order";
103
foreach my $target_record_type ( qw(sales_delivery_order sales_reclamation) ) {
105
foreach my $pair (pairs @{['SL::DB::DeliveryOrder' => 'sales_delivery_order', 'SL::DB::Reclamation' => 'sales_reclamation']}) {
104 106
  # TODO: invoice
105
  note "  testing from quotation -> $target_record_type";
106
  my $new_record = SL::Model::Record->new_from_workflow($sales_order1, $target_record_type);
107
  my ($target_record_type, $target_record_subtype) = @$pair;
108
  note "  testing from quotation -> $target_record_subtype";
109
  my $new_record = SL::Model::Record->new_from_workflow($sales_order1, $target_record_type, $target_record_subtype);
107 110
  if ( 'SL::DB::Reclamation' eq ref($new_record) ) {
108 111
    $_->reason($reclamation_reason) foreach @{ $new_record->items };
109 112
  };
110 113
  SL::Model::Record->save($new_record);
111 114
  $new_record->load;
112
  my $record_history = SL::DB::Manager::History->find_by(trans_id => $new_record->id, what_done => $target_record_type, addition => 'SAVED');
115
  my $record_history = SL::DB::Manager::History->find_by(trans_id => $new_record->id, what_done => $target_record_subtype, addition => 'SAVED');
113 116

  
114 117
  ok($record_history->snumbers =~ m/_/, "history snumbers of record " . $record_history->snumbers . " ok");
115 118

  
116
  cmp_ok($new_record->netamount, '==', 710, "converted $target_record_type netamount ok") if $new_record->can('netamount');
117
  test_record_links($new_record, "converted $target_record_type");
119
  cmp_ok($new_record->netamount, '==', 710, "converted $target_record_subtype netamount ok") if $new_record->can('netamount');
120
  test_record_links($new_record, "converted $target_record_subtype");
118 121
};
119 122

  
120 123
note ('testing multi');
......
124 127

  
125 128
note('combining several sales orders to one combined order');
126 129
my @sales_orders;
127
push(@sales_orders, SL::Model::Record->new_from_workflow($sales_quotation1, 'sales_order')->save->load) for 1 .. 3;
128
my $combined_order = SL::Model::Record->new_from_workflow_multi(\@sales_orders, 'sales_order', sort_sources_by => 'transdate');
130
push(@sales_orders, SL::Model::Record->new_from_workflow($sales_quotation1, 'SL::DB::Order', 'sales_order')->save->load) for 1 .. 3;
131
my $combined_order = SL::Model::Record->new_from_workflow_multi(\@sales_orders, 'SL::DB::Order','sales_order', sort_sources_by => 'transdate');
129 132
SL::Model::Record->save($combined_order);
130 133
cmp_ok($combined_order->netamount, '==', 3*710, "netamount of combined order ok");
131 134

  
t/workflow/delivery_order_reclamation.t
182 182
)->load;
183 183

  
184 184
# convert order → reclamation
185
my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($sales_delivery_order, 'sales_reclamation');
185
my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($sales_delivery_order, 'SL::DB::Reclamation', 'sales_reclamation');
186 186
$converted_sales_reclamation->items_sorted->[0]->reason($reclamation_reason);
187 187
$converted_sales_reclamation->items_sorted->[1]->reason($reclamation_reason);
188 188
$converted_sales_reclamation->save->load;
189
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_delivery_order, 'purchase_reclamation');
189
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_delivery_order, 'SL::DB::Reclamation', 'purchase_reclamation');
190 190
$converted_purchase_reclamation->items_sorted->[0]->reason($reclamation_reason);
191 191
$converted_purchase_reclamation->items_sorted->[1]->reason($reclamation_reason);
192 192
$converted_purchase_reclamation->save->load;
193 193

  
194 194
# convert reclamation → order
195
my $converted_sales_delivery_order = SL::Model::Record->new_from_workflow($sales_reclamation, 'rma_delivery_order')->save->load;
196
my $converted_purchase_delivery_order = SL::Model::Record->new_from_workflow($purchase_reclamation, 'supplier_delivery_order')->save->load;
195
my $converted_sales_delivery_order = SL::Model::Record->new_from_workflow($sales_reclamation, 'SL::DB::DeliveryOrder', 'rma_delivery_order')->save->load;
196
my $converted_purchase_delivery_order = SL::Model::Record->new_from_workflow($purchase_reclamation, 'SL::DB::DeliveryOrder', 'supplier_delivery_order')->save->load;
197 197

  
198 198

  
199 199
#get items before strip
t/workflow/invoice_to_reclamation.t
182 182
)->load;
183 183

  
184 184
# convert invoice → reclamation
185
my $converted_sales_reclamation =SL::Model::Record->new_from_workflow($sales_invoice, "sales_reclamation");
185
my $converted_sales_reclamation =SL::Model::Record->new_from_workflow($sales_invoice, 'SL::DB::Reclamation', "sales_reclamation");
186 186
$converted_sales_reclamation->items_sorted->[0]->reason($reclamation_reason);
187 187
$converted_sales_reclamation->items_sorted->[1]->reason($reclamation_reason);
188 188
$converted_sales_reclamation->save->load;
189
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_invoice, "purchase_reclamation");
189
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_invoice, 'SL::DB::Reclamation', "purchase_reclamation");
190 190
$converted_purchase_reclamation->items_sorted->[0]->reason($reclamation_reason);
191 191
$converted_purchase_reclamation->items_sorted->[1]->reason($reclamation_reason);
192 192
$converted_purchase_reclamation->save->load;
t/workflow/order_reclamation.t
182 182
)->load;
183 183

  
184 184
# convert order → reclamation
185
my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($sales_order, "sales_reclamation");
185
my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($sales_order, 'SL::DB::Reclamation', "sales_reclamation");
186 186
$converted_sales_reclamation->items_sorted->[0]->reason($reclamation_reason);
187 187
$converted_sales_reclamation->items_sorted->[1]->reason($reclamation_reason);
188 188
$converted_sales_reclamation->save->load;
189
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_order, "purchase_reclamation");
189
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_order, 'SL::DB::Reclamation', "purchase_reclamation");
190 190
$converted_purchase_reclamation->items_sorted->[0]->reason($reclamation_reason);
191 191
$converted_purchase_reclamation->items_sorted->[1]->reason($reclamation_reason);
192 192
$converted_purchase_reclamation->save->load;
193 193

  
194 194
# convert reclamation → order
195
my $converted_sales_order = SL::Model::Record->new_from_workflow($sales_reclamation, 'sales_order')->save->load;
196
my $converted_purchase_order = SL::Model::Record->new_from_workflow($purchase_reclamation, 'purchase_order')->save->load;
195
my $converted_sales_order = SL::Model::Record->new_from_workflow($sales_reclamation, 'SL::DB::Order', 'sales_order')->save->load;
196
my $converted_purchase_order = SL::Model::Record->new_from_workflow($purchase_reclamation, 'SL::DB::Order', 'purchase_order')->save->load;
197 197

  
198 198

  
199 199
#get items before strip
t/workflow/reclamation_reclamation.t
152 152
)->load;
153 153

  
154 154
# new
155
my $new_sales_reclamation = SL::Model::Record->new_from_workflow($sales_reclamation, 'sales_reclamation')->save->load;
156
my $new_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_reclamation, 'purchase_reclamation')->save->load;
155
my $new_sales_reclamation = SL::Model::Record->new_from_workflow($sales_reclamation, 'SL::DB::Reclamation', 'sales_reclamation')->save->load;
156
my $new_purchase_reclamation = SL::Model::Record->new_from_workflow($purchase_reclamation, 'SL::DB::Reclamation', 'purchase_reclamation')->save->load;
157 157

  
158 158
# convert
159
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($sales_reclamation, 'purchase_reclamation');
159
my $converted_purchase_reclamation = SL::Model::Record->new_from_workflow($sales_reclamation, 'SL::DB::Reclamation', 'purchase_reclamation');
160 160
$converted_purchase_reclamation->vendor_id($purchase_reclamation->{vendor_id});
161 161
$converted_purchase_reclamation->save->load;
162
my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($purchase_reclamation, 'sales_reclamation');
162
my $converted_sales_reclamation = SL::Model::Record->new_from_workflow($purchase_reclamation, 'SL::DB::Reclamation', 'sales_reclamation');
163 163
$converted_sales_reclamation->customer_id($sales_reclamation->{customer_id});
164 164
$converted_sales_reclamation->save->load;
165 165

  

Auch abrufbar als: Unified diff