Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6fb83ee8

Von Rolf Fluehmann vor etwa 6 Jahren hinzugefügt

  • ID 6fb83ee8d9e313519c3b4ce823dfe26dafbaa571
  • Vorgänger ed020148
  • Nachfolger 4ee71121

new controller for production batch management

Unterschiede anzeigen:

SL/Controller/Batch.pm
1
package SL::Controller::Batch;
2

  
3
use strict;
4
use parent qw(SL::Controller::Base);
5

  
6
use SL::DB::Batch;
7
use SL::Helper::Flash;
8

  
9
use Rose::Object::MakeMethods::Generic (
10
  'scalar --get_set_init' => [ qw( batch employee part vendor ) ],
11
);
12

  
13
my $may_edit = 1;
14

  
15
__PACKAGE__->run_before( '_create', except => [ 'search', 'list' ] );
16
__PACKAGE__->run_before( '_load', only => [ 'delete', 'edit', 'save', 'save_and_close' ] );
17
__PACKAGE__->run_before( '_copy', only => [ 'save', 'save_and_close' ] );
18
__PACKAGE__->run_before( '_forward_tags_hidden', only => [ 'edit', 'list' ] );
19
__PACKAGE__->run_before( '_forward_tags_redirected', except => [ 'edit', 'list', 'search' ] );
20
__PACKAGE__->run_before( '_pre_render', only => [ 'add', 'edit' ] );
21

  
22
#
23
# Actions
24
#
25

  
26
# adds a new batch
27
sub action_add {
28
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
29
  $self->batch->batchdate( DateTime->now_local() );
30
  $self->_setup_form_action_bar;
31
  $self->render(
32
    'batch/form',
33
    title => $locale->text( 'Add Batch' )
34
  );
35
}
36

  
37
# deletes an existing batch
38
sub action_delete {
39
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
40
  my( $action, @errors ) = ( '', () );
41
  @errors = $self->batch->has_children
42
         or $self->batch->delete || push( @errors, $self->batch->db->error );
43
  if( scalar @errors ) {
44
    flash_later( 'error', @errors );
45
    $action = 'edit';
46
  } else {
47
    flash_later( 'info', $locale->text( 'The batch has been deleted.' ) );
48
    $action = $self->{ callback };
49
  }
50
  $self->_redirect_to( $action );
51
}
52

  
53
# edits an existing batch
54
sub action_edit {
55
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
56
  $self->_setup_form_action_bar;
57
  $self->render(
58
    'batch/form',
59
    title => $locale->text( 'Edit Batch')
60
  );
61
}
62

  
63
# lists the filtred and sorted batches
64
sub action_list {
65
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
66

  
67
  $form->{ filter_rows }->{ producer_id } && $self->vendor( SL::DB::Vendor->new( id => $form->{ filter_rows }->{ producer_id } )->load );
68
  $form->{ filter_rows }->{ part_id } && $self->part( SL::DB::Part->new( id => $form->{ filter_rows }->{ part_id } )->load );
69
  $form->{ filter_rows }->{ employee_id } && $self->employee( SL::DB::Employee->new( id => $form->{ filter_rows }->{ employee_id } )->load );
70

  
71
  $self->{ columns } = [
72
    { key => 'producer'   , label => 'Producer' },
73
    { key => 'part'       , label => 'Part' },
74
    { key => 'batchnumber', label => 'Batch Number' },
75
    { key => 'batchdate'  , label => 'Batch Date' },
76
    { key => 'location'   , label => 'Batch Location' },
77
    { key => 'process'    , label => 'Batch Process' },
78
    { key => 'insertdate' , label => 'Insert Date' },
79
    { key => 'changedate' , label => 'Updated' },
80
    { key => 'employee'   , label => 'Employee' }
81
  ];
82

  
83
  $self->{ filter } = join( '&',
84
    map {'filter_columns.' . $_ . '=' . $self->{ filter_columns }->{ $_ } } keys %{ $self->{ filter_columns } }
85
  );
86
  my @filter = $self->_filter;
87
  @{ $self->{ all_batches } } = @{ SL::DB::Manager::Batch->get_all( where => \@filter ) }
88
    and $self->_sort( $self->{ sort_column } );
89

  
90
  $self->_setup_list_action_bar;
91
  $self->render(
92
    'batch/list',
93
    title => $locale->text( 'Batches' )
94
  );
95
}
96

  
97
# saves a new or edited batch
98
sub action_save {
99
  my $self = shift;
100
  $self->_save;
101
  $self->_redirect_to( $self->{ callback } ne 'add' ? 'edit' : 'add' );
102
}
103

  
104
# saves a new or edited batch and closes the frame
105
sub action_save_and_close {
106
  my $self = shift;
107
  $self->_save;
108
  $self->_redirect_to( $self->{ callback } );
109
}
110

  
111
# filter the batches and their fields to list
112
sub action_search {
113
  my( $self, $locale ) = ( shift, $::locale );
114
  $self->{ callback } = 'list';
115
  $self->{ sort_column } = 'producer';
116
  %{ $self->{ filter_columns } } = ();
117
  %{ $self->{ filter_rows } } = ();
118
  $self->{ all_employees } = SL::DB::Manager::Employee->get_all( sort_by => 'name' );
119
  $self->_setup_search_action_bar;
120
  $self->render(
121
    'batch/search',
122
    title => $locale->text( 'Batches' )
123
  );
124
}
125

  
126
#
127
# Helpers
128
#
129

  
130
sub _copy {
131
  my( $self, $form ) = ( shift, $::form );
132
  foreach( keys %{ $form->{ batch } } ) {
133
    $self->batch->can( "$_" ) && $self->batch->$_( $form->{ batch }->{ $_ } );
134
  }
135
}
136

  
137
sub _create {
138
  my $self = shift;
139
  $self->batch( SL::DB::Batch->new );
140
}
141

  
142
sub _filter {
143
  my( $self, $form ) = ( shift, $::form );
144
  my @filter = ( deleted => 'false' );
145
  foreach( keys %{ $self->{ filter_rows } } ) {
146
    if( $self->{ filter_rows }->{ $_ } ) {
147
      $_ =~ m/^.*?_from$/ and $_ =~ s/^(.*?)_from$/$1/
148
        and push( @filter, ( $_ => { ge => $self->{ filter_rows }->{ $_ . '_from' } } ) )
149
        and $self->{ filter } .= '&filter_rows.' . $_ . '_from' . '=' . $form->escape( $self->{ filter_rows }->{ $_ . '_from' } )
150
      or $_ =~ m/^.*?_to$/ and $_ =~ s/^(.*?)_to$/$1/
151
        and push( @filter, ( $_ => { le => $self->{ filter_rows }->{ $_ . '_to' } } ) )
152
        and $self->{ filter } .= '&filter_rows.' . $_ . '_to' . '=' . $form->escape( $self->{ filter_rows }->{ $_ . '_to' } )
153
      or $_ =~ m/^.*?_id$/
154
        and push( @filter, ( $_ => $self->{ filter_rows }->{ $_ } ) )
155
        and $self->{ filter } .= '&filter_rows.' . $_ . '=' . $form->escape( $self->{ filter_rows }->{ $_ } )
156
      or push( @filter, ( $_ => { like => $self->{ filter_rows }->{ $_ } } ) )
157
        and $self->{ filter } .= '&filter_rows.' . $_ . '=' . $form->escape( $self->{ filter_rows }->{ $_ } )
158
      ;
159
    }
160
  }
161
  return @filter;
162
}
163

  
164
sub _forward_tags_hidden {
165
  my( $self, $form ) = ( shift, $::form );
166
  $self->{ callback } = $form->{ callback } || 'add';
167
  $self->{ sort_column } = $form->{ sort_column } || 'producer';
168
  %{ $self->{ filter_columns } } = $form->{ filter_columns } ? %{ $form->{ filter_columns } } : ();
169
  %{ $self->{ filter_rows } } = $form->{ filter_rows } ? %{ $form->{ filter_rows } } : ();
170
}
171

  
172
sub _forward_tags_redirected {
173
  my( $self, $form ) = ( shift, $::form );
174
  $self->{ callback } = $form->{ callback } || 'add';
175
  $self->{ sort_column } = $form->{ sort_column } || 'producer';
176
  %{ $self->{ filter_columns } } = $form->{ filter_columns } ? split( /,/, $form->{ filter_columns } ) : ();
177
  %{ $self->{ filter_rows } } = $form->{ filter_rows } ? split( /,/, $form->{ filter_rows } ) : ();
178
}
179

  
180
sub _load {
181
  my( $self, $form ) = ( shift, $::form );
182
  $self->batch->id( $form->{ id } ) && $self->batch->load if $form->{ id };
183
}
184

  
185
sub _pre_render {
186
  my $self = shift;
187
  $self->{ all_employees } = SL::DB::Manager::Employee->get_all(
188
    where => [ or => [
189
      id      => $self->batch->employee_id,
190
      deleted => 0
191
    ] ],
192
    sort_by => 'name'
193
  );
194
}
195

  
196
sub _redirect_to {
197
  my( $self, $action ) = @_;
198
  $self->SUPER::redirect_to(
199
    script => 'contoller.pl',
200
    action => $action,
201
    id     => $self->batch->id,
202
    callback => $self->{ callback },
203
    sort_column => $self->{ sort_column },
204
    filter_columns => $self->{ filter_columns },
205
    filter_rows => $self->{ filter_rows }
206
  );
207
}
208

  
209
sub _save {
210
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
211
  my @errors = ();
212
  @errors = $self->batch->validate
213
         or $self->batch->save || push( @errors, $self->batch->db->error );
214
  if( scalar @errors ) {
215
    flash_later( 'error', @errors );
216
  } else {
217
    flash_later( 'info', $locale->text( 'The batch has been saved.' ) );
218
  }
219
}
220

  
221
sub _sort {
222
  my( $self, $column ) = @_;
223
  my (%a,%b);
224
  $column eq 'producer' and @{ $self->{ all_batches } } = sort {
225
    $a->producer->name cmp $b->producer->name
226
    || $a->part->partnumber cmp $b->part->partnumber
227
    || $a->batchnumber cmp $b->batchnumber
228
  } @{ $self->{ all_batches } }
229
  or $column eq 'part' and @{ $self->{ all_batches } } = sort {
230
    $a->part->partnumber cmp $b->part->partnumber
231
    || $a->batchnumber cmp $b->batchnumber
232
  } @{ $self->{ all_batches } }
233
  or $column eq 'batchnumber' and @{ $self->{ all_batches } } = sort {
234
    $a->batchnumber cmp $b->batchnumber
235
  } @{ $self->{ all_batches } }
236
  or $column eq 'batchdate' and @{ $self->{ all_batches } } = sort {
237
    $a->batchdate cmp $b->batchdate
238
    || $a->location cmp $b->location
239
    || $a->process cmp $b->process
240
  } @{ $self->{ all_batches } }
241
  or $column eq 'location' and @{ $self->{ all_batches } } = sort {
242
    $a->location cmp $b->location
243
    || $a->process cmp $b->process
244
  } @{ $self->{ all_batches } }
245
  or $column eq 'process' and @{ $self->{ all_batches } } = sort {
246
    $a->process cmp $b->process
247
  } @{ $self->{ all_batches } }
248
  or $column eq 'insertdate' and @{ $self->{ all_batches } } = sort {
249
    $a->itime cmp $b->itime
250
    || $a->mtime cmp $b->mtime
251
    || $a->employee->name cmp $b->employee->name
252
  } @{ $self->{ all_batches } }
253
  or $column eq 'changedate' and @{ $self->{ all_batches } } = sort {
254
    $a->mtime cmp $b->mtime
255
    || $a->employee->name cmp $b->employee->name
256
  } @{ $self->{ all_batches } }
257
  or $column eq 'employee' and @{ $self->{ all_batches } } = sort {
258
    $a->employee->name cmp $b->employee->name
259
  } @{ $self->{ all_batches } }
260
  ;
261
}
262

  
263
#
264
# Actionbars
265
#
266

  
267
sub _setup_form_action_bar {
268
  my( $self, $locale ) = ( shift, $::locale );
269
  for my $bar ($::request->layout->get( 'actionbar' )) {
270
    $bar->add(
271
      combobox => [
272
        action => [
273
          $locale->text( 'Save' ),
274
          submit    => [ '#batch_form', {
275
            action => 'Batch/save',
276
            sort_column => $self->{ sort_column },
277
            filter_columns => join( ',', %{ $self->{ filter_columns } } ),
278
            filter_rows => join( ',', %{ $self->{ filter_rows } } )
279
          } ],
280
          disabled  => !$may_edit ? $locale->text( 'You do not have the permissions to access this function.' )
281
                     : undef,
282
          accesskey => 'enter'
283
        ],
284
        action => [
285
          $locale->text( 'Save and Close' ),
286
          submit => [ '#batch_form', {
287
            action => 'Batch/save_and_close',
288
            sort_column => $self->{ sort_column },
289
            filter_columns => join( ',', %{ $self->{ filter_columns } } ),
290
            filter_rows => join( ',', %{ $self->{ filter_rows } } )
291
          } ],
292
          disabled  => !$may_edit ? $locale->text( 'You do not have the permissions to access this function.' )
293
                     : undef
294
        ]
295
      ], # end of combobox "Save"
296
      action => [
297
        $locale->text( 'Delete' ),
298
        submit   => [ '#batch_form', {
299
          action => 'Batch/delete',
300
          sort_column => $self->{ sort_column },
301
          filter_columns => join( ',', %{ $self->{ filter_columns } } ),
302
          filter_rows => join( ',', %{ $self->{ filter_rows } } )
303
         } ],
304
        confirm  => $locale->text( 'Do you really want to delete this object?' ),
305
        disabled => !$may_edit                 ? $locale->text( 'You do not have the permissions to access this function.' )
306
                  : !$self->batch->id          ? $locale->text( 'This object has not been saved yet.' )
307
                  : $self->batch->has_children ? $locale->text( 'This object has already been used.' )
308
                  : undef
309
      ],
310
#      action => [
311
#        $::locale->text( 'History' ),
312
#        call     => [ 'kivi.Batch.showHistoryWindow', $self->batch->id ],
313
#        disabled => !$may_edit        ? $locale->text( 'You do not have the permissions to access this function.' )
314
#                  : !$self->batch->id ? $locale->text( 'This object has not been saved yet.' )
315
#                  : undef,
316
#      ]
317
    );
318
  }
319
}
320

  
321
sub _setup_list_action_bar {
322
  my( $self, $locale ) = ( shift, $::locale );
323
  for my $bar ($::request->layout->get( 'actionbar' ) ) {
324
    $bar->add(
325
      action => [
326
        $locale->text( 'Add' ),
327
        submit    => [ '#batch_list', {
328
          action => 'Batch/add',
329
          callback => $self->{ callback },
330
          sort_column => $self->{ sort_column },
331
          filter_columns => join( ',', %{ $self->{ filter_columns } } ),
332
          filter_rows => join( ',', %{ $self->{ filter_rows } } )
333
        } ],
334
        accesskey => 'enter'
335
      ]
336
    );
337
  }
338
}
339

  
340
sub _setup_search_action_bar {
341
  my( $self, $locale ) = ( shift, $::locale );
342
  for my $bar ($::request->layout->get( 'actionbar' ) ) {
343
    $bar->add(
344
      action => [
345
        $locale->text( 'Search' ),
346
        submit    => [ '#batch_search', {
347
          action => 'Batch/list',
348
          callback => $self->{ callback },
349
        } ],
350
        accesskey => 'enter'
351
      ]
352
    );
353
  }
354
}
355

  
356
1;
357

  
358
__END__
359

  
360
=encoding utf-8
361

  
362
=head1 NAME
363

  
364
SL::Controller::Batch - Batch CRUD controller
365

  
366
=head1 DESCRIPTION
367

  
368
Implements the URL Actions.
369
They loads the requesting form and database objects if needed.
370
Finally prepares and calls the responding form or redirects to the according action.
371
The output list is realised by a HTML-Template to avoid "bin/mozilla".
372

  
373
=head1 URL ACTIONS
374

  
375
=over 4
376

  
377
=item C<action_add>
378

  
379
Adds a new batch.
380

  
381
=item C<action_delete
382

  
383
Deletes an existing batch.
384

  
385
=item C<action_edit>
386

  
387
Edits an existing batch.
388

  
389
=item C<action_list>
390

  
391
Lists the filtred and sorted batches.
392

  
393
=item C<action_save>
394

  
395
Saves a new or edited batch and responds the Edit-Form.
396

  
397
=item C<action_save_and_close>
398

  
399
Saves a new or edited batch and closes the frame.
400

  
401
=item C<action_search>
402

  
403
Filters the batches and their fields to list.
404

  
405
=back
406

  
407
=head1 HELPER FUNCTIONS
408

  
409
=over 4
410

  
411
=item C<_copy>
412

  
413
Copies the fields of a batch from the requesting form to the rose-object.
414

  
415
=item C<_create>
416

  
417
Creates a new batch-rose-object.
418

  
419
=item C<_filter>
420

  
421
Returns the filter of the batches-query and sets those of the responding form.
422

  
423
=item C<_forward_tags_hidden>
424

  
425
Sets the searche and sort criteria for the callback of the responding form.
426

  
427
=item C<_forward_tags_redirected>
428

  
429
Sets the searche and sort criteria for the callback of the redirected action.
430

  
431
=item C<_load>
432

  
433
Loads the batch-rose-object at the redirected action.
434

  
435
=item C<_pre_render>
436

  
437
Prepares the responding form.
438

  
439
=item C<_redirect_to>
440

  
441
Redirects to the passed action.
442

  
443
=item C<_save>
444

  
445
Saves a new or edited batch.
446

  
447
=item C<_sort>
448

  
449
Sorts the loaded batches by the key of the requesting form.
450

  
451
=back
452

  
453
=head1 ACTION BARS
454

  
455
=over 4
456

  
457
_setup_form_action_bar
458
_setup_list_action_bar
459
_setup_search_action_bar
460

  
461
=back
462

  
463
=head1 TODO
464

  
465
=over 4
466

  
467
=item *
468

  
469
The action "delete" still deletes physically.
470
To mark the table-entries as deletet could be advantageous.
471

  
472
=item *
473

  
474
Actually the filter- and sort-criteria for the callbacked list are passed through the responds.
475
They should be cached and restored by the list.
476

  
477
=item *
478

  
479
The History-Button of the form_action_bar isn,t yet implemented
480

  
481
=item *
482

  
483
The rights aren,t yet implemented. They should be "read", "insert", "update", "delete".
484

  
485
=back
486

  
487
=head1 AUTHOR
488

  
489
Rolf Flühmann E<lt>rolf.fluehmann@revamp-it.chE<gt>,
490
ROlf Flühmann E<lt>rolf_fluehmann@gmx.chE<gt>
491

  
492
=cut

Auch abrufbar als: Unified diff