Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6b349b9d

Von Rolf Fluehmann vor etwa 6 Jahren hinzugefügt

  • ID 6b349b9d277b982a5a04454e56dbc6eed82ab149
  • Vorgänger 67ec6200
  • Nachfolger 40b7b7b6

new controller for production piece management

Unterschiede anzeigen:

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

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

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

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

  
14
my $may_edit = 1;
15

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

  
23
#
24
# Actions
25
#
26

  
27
# add a new piece
28
sub action_add {
29
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
30
  $self->_setup_form_action_bar;
31
  $self->render(
32
    'piece/form',
33
    title => $locale->text( 'Add Piece' )
34
  );
35
}
36

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

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

  
62
# list the filtred and sorted pieces
63
sub action_list {
64
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
65

  
66
  $form->{ filter_rows }->{ producer_id } && $self->vendor( SL::DB::Vendor->new( id => $form->{ filter_rows }->{ producer_id } )->load );
67
  $form->{ filter_rows }->{ part_id } && $self->part( SL::DB::Part->new( id => $form->{ filter_rows }->{ part_id } )->load );
68
  $form->{ filter_rows }->{ batch_id } && $self->batch( SL::DB::Batch->new( id => $form->{ filter_rows }->{ batch_id } )->load );
69
  $form->{ filter_rows }->{ employee_id_id } && $self->employee( SL::DB::Employee->new( id => $form->{ filter_rows }->{ employee_in_id } )->load );
70
  $form->{ filter_rows }->{ employee_out_id } && $self->employee( SL::DB::Employee->new( id => $form->{ filter_rows }->{ employee_out_id } )->load );
71

  
72
  $self->{ columns } = [
73
    { key => 'producer'     , label => 'Producer' },
74
    { key => 'part'         , label => 'Part' },
75
    { key => 'batch'        , label => 'Batch' },
76
    { key => 'serialnumber' , label => 'Serial Number' },
77
    { key => 'weight'       , label => 'Weight' },
78
    { key => 'delivery_in'  , label => 'Incoming Delivery Order' },
79
    { key => 'bin'          , label => 'Bin' },
80
    { key => 'delivery_out' , label => 'Outgoing Delivery Order' },
81
    { key => 'insertdate'   , label => 'Insert Date' },
82
    { key => 'changedate'   , label => 'Updated' },
83
    { key => 'employee'     , label => 'Employee' }
84
  ];
85

  
86
  $self->{ filter } = join( '&',
87
    map {'filter_columns.' . $_ . '=' . $self->{ filter_columns }->{ $_ } } keys %{ $self->{ filter_columns } }
88
  );
89
  my @filter = $self->_filter;
90
  @{ $self->{ all_pieces } } = @{ SL::DB::Manager::Piece->get_all( where => \@filter ) }
91
    and $self->_sort( $self->{ sort_column } );
92

  
93
  $self->_setup_list_action_bar;
94
  $self->render(
95
    'piece/list',
96
    title => $locale->text( 'Pieces' )
97
  );
98
}
99

  
100
# save a new or edited piece
101
sub action_save {
102
  my $self = shift;
103
  $self->_save;
104
  $self->_redirect_to( $self->{ callback } ne 'add' ? 'edit' : 'add' );
105
}
106

  
107
# save a new or edited piece and close the frame
108
sub action_save_and_close {
109
  my $self = shift;
110
  $self->_save;
111
  $self->_redirect_to( $self->{ callback } );
112
}
113

  
114
# filter the pieces to list
115
sub action_search {
116
  my( $self, $locale ) = ( shift, $::locale );
117
  $self->{ callback } = 'list';
118
  $self->{ sort_column } = 'producer';
119
  %{ $self->{ filter_columns } } = ();
120
  %{ $self->{ filter_rows } } = ();
121
  $self->{ all_employees } = SL::DB::Manager::Employee->get_all( sort_by => 'name' );
122
  $self->_setup_search_action_bar;
123
  $self->render(
124
    'piece/search',
125
    title => $locale->text( 'Pieces' )
126
  );
127
}
128

  
129
#
130
# Helpers
131
#
132

  
133
sub _copy {
134
  my( $self, $form ) = ( shift, $::form );
135
  foreach( keys %{ $form->{ piece } } ) {
136
    $self->piece->can( "$_" ) && $self->piece->$_( $form->{ piece }->{ $_ } );
137
  }
138
}
139

  
140
sub _create {
141
  my $self = shift;
142
  $self->piece( SL::DB::Piece->new );
143
}
144

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

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

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

  
183
sub _load {
184
  my( $self, $form ) = ( shift, $::form );
185
  $self->piece->id( $form->{ id } ) && $self->piece->load if $form->{ id };
186
}
187

  
188
sub _pre_render {
189
  my $self = shift;
190
  $self->{ all_batches } = SL::DB::Manager::Batch->get_all(
191
    where => [ or => [
192
      id      => $self->piece->batch_id,
193
      deleted => 0
194
    ] ],
195
    sort_by => 'batchnumber'
196
  );
197
  unshift( $self->{ all_batches }, undef );
198
  $self->{ all_bins } = SL::DB::Manager::Bin->get_all( sort_by => 'description' );
199
  unshift( $self->{ all_bins }, undef );
200
  $self->{ all_deliveries } = SL::DB::Manager::DeliveryOrder->get_all( sort_by => 'donumber' );
201
  $self->{ all_employees } = SL::DB::Manager::Employee->get_all(
202
    where => [ or => [
203
      id      => $self->piece->employee_id,
204
      deleted => 0
205
    ] ],
206
    sort_by => 'name'
207
  );
208
}
209

  
210
sub _redirect_to {
211
  my( $self, $action ) = @_;
212
  $self->SUPER::redirect_to(
213
    script => 'contoller.pl',
214
    action => $action,
215
    id     => $self->piece->id,
216
    callback => $self->{ callback },
217
    sort_column => $self->{ sort_column },
218
    filter_columns => $self->{ filter_columns },
219
    filter_rows => $self->{ filter_rows }
220
  );
221
}
222

  
223
sub _save {
224
  my( $self, $form, $locale ) = ( shift, $::form, $::locale );
225
  $self->piece->undefine;
226
  my @errors = ();
227
  @errors = $self->piece->validate
228
         or $self->piece->save || push( @errors, $self->piece->db->error );
229
  if( scalar @errors ) {
230
    flash_later( 'error', @errors );
231
  } else {
232
    flash_later( 'info', $locale->text( 'The piece has been saved.' ) );
233
  }
234
}
235

  
236
sub _sort {
237
  my( $self, $column ) = @_;
238
  my (%a,%b);
239
  $column eq 'producer' and @{ $self->{ all_pieces } } = sort {
240
    $a->producer->name cmp $b->producer->name
241
    || $a->part->partnumber cmp $b->part->partnumber
242
    || $a->batch->batchnumber cmp $b->batch->batchnumber
243
    || $a->serialnumber cmp $b->serialnumber
244
  } @{ $self->{ all_pieces } }
245
  or $column eq 'part' and @{ $self->{ all_pieces } } = sort {
246
    $a->part->partnumber cmp $b->part->partnumber
247
    || $a->batch->batchnumber cmp $b->batch->batchnumber
248
    || $a->serialnumber cmp $b->serialnumber
249
  } @{ $self->{ all_pieces } }
250
  or $column eq 'batch' and @{ $self->{ all_pieces } } = sort {
251
    $a->batch->batchnumber cmp $b->batch->batchnumber
252
    || $a->serialnumber cmp $b->serialnumber
253
  } @{ $self->{ all_pieces } }
254
  or $column eq 'piecenumber' and @{ $self->{ all_pieces } } = sort {
255
    $a->serialnumber cmp $b->serialnumber
256
  } @{ $self->{ all_pieces } }
257
  or $column eq 'weight' and @{ $self->{ all_pieces } } = sort {
258
    $a->weight cmp $b->weight
259
  } @{ $self->{ all_pieces } }
260
  or $column eq 'delivery_in' and @{ $self->{ all_pieces } } = sort {
261
    $a->delivery->donumber cmp $b->delivery_orders->donumber
262
  } @{ $self->{ all_pieces } }
263
  or $column eq 'bin' and @{ $self->{ all_pieces } } = sort {
264
    $a->bin->description cmp $b->bin->description
265
  } @{ $self->{ all_pieces } }
266
  or $column eq 'insertdate' and @{ $self->{ all_pieces } } = sort {
267
    $a->itime cmp $b->itime
268
    || $a->mtime cmp $b->mtime
269
    || $a->employee->name cmp $b->employee->name
270
  } @{ $self->{ all_pieces } }
271
  or $column eq 'changedate' and @{ $self->{ all_pieces } } = sort {
272
    $a->mtime cmp $b->mtime
273
    || $a->employee->name cmp $b->employee->name
274
  } @{ $self->{ all_pieces } }
275
  or $column eq 'employee' and @{ $self->{ all_pieces } } = sort {
276
    $a->employee->name cmp $b->employee->name
277
  } @{ $self->{ all_pieces } }
278
  ;
279
}
280

  
281
#
282
# Actionbars
283
#
284

  
285
sub _setup_form_action_bar {
286
  my( $self, $locale ) = ( shift, $::locale );
287
  for my $bar ($::request->layout->get( 'actionbar' )) {
288
    $bar->add(
289
      combobox => [
290
        action => [
291
          $locale->text( 'Save' ),
292
          submit    => [ '#piece_form', {
293
            action => 'Piece/save',
294
            sort_column => $self->{ sort_column },
295
            filter_columns => join( ',', %{ $self->{ filter_columns } } ),
296
            filter_rows => join( ',', %{ $self->{ filter_rows } } )
297
          } ],
298
          disabled  => !$may_edit ? $locale->text( 'You do not have the permissions to access this function.' )
299
                     : undef,
300
          accesskey => 'enter'
301
        ],
302
        action => [
303
          $locale->text( 'Save and Close' ),
304
          submit => [ '#piece_form', {
305
            action => 'Piece/save_and_close',
306
            sort_column => $self->{ sort_column },
307
            filter_columns => join( ',', %{ $self->{ filter_columns } } ),
308
            filter_rows => join( ',', %{ $self->{ filter_rows } } )
309
          } ],
310
          disabled  => !$may_edit ? $locale->text( 'You do not have the permissions to access this function.' )
311
                     : undef
312
        ]
313
      ], # end of combobox "Save"
314
      action => [
315
        $locale->text( 'Delete' ),
316
        submit   => [ '#piece_form', {
317
          action => 'Piece/delete',
318
          sort_column => $self->{ sort_column },
319
          filter_columns => join( ',', %{ $self->{ filter_columns } } ),
320
          filter_rows => join( ',', %{ $self->{ filter_rows } } )
321
         } ],
322
        confirm  => $locale->text( 'Do you really want to delete this object?' ),
323
        disabled => !$may_edit                 ? $locale->text( 'You do not have the permissions to access this function.' )
324
                  : !$self->piece->id          ? $locale->text( 'This object has not been saved yet.' )
325
                  : undef
326
      ],
327
#      action => [
328
#        $::locale->text( 'History' ),
329
#        call     => [ 'kivi.Batch.showHistoryWindow', $self->piece->id ],
330
#        disabled => !$may_edit        ? $locale->text( 'You do not have the permissions to access this function.' )
331
#                  : !$self->piece->id ? $locale->text( 'This object has not been saved yet.' )
332
#                  : undef,
333
#      ]
334
    );
335
  }
336
}
337

  
338
sub _setup_list_action_bar {
339
  my( $self, $locale ) = ( shift, $::locale );
340
  for my $bar ($::request->layout->get( 'actionbar' ) ) {
341
    $bar->add(
342
      action => [
343
        $locale->text( 'Add' ),
344
        submit    => [ '#piece_list', {
345
          action => 'Piece/add',
346
          callback => $self->{ callback },
347
          sort_column => $self->{ sort_column },
348
          filter_columns => join( ',', %{ $self->{ filter_columns } } ),
349
          filter_rows => join( ',', %{ $self->{ filter_rows } } )
350
        } ],
351
        accesskey => 'enter'
352
      ]
353
    );
354
  }
355
}
356

  
357
sub _setup_search_action_bar {
358
  my( $self, $locale ) = ( shift, $::locale );
359
  for my $bar ($::request->layout->get( 'actionbar' ) ) {
360
    $bar->add(
361
      action => [
362
        $locale->text( 'Search' ),
363
        submit    => [ '#piece_search', {
364
          action => 'Piece/list',
365
          callback => $self->{ callback },
366
        } ],
367
        accesskey => 'enter'
368
      ]
369
    );
370
  }
371
}
372

  
373
1;
374

  
375
__END__
376

  
377
=encoding utf-8
378

  
379
=head1 NAME
380

  
381
SL::Controller::Piece - Piece CRUD controller
382

  
383
=head1 DESCRIPTION
384

  
385
Implements the URL Actions.
386
They loads the requesting form and database objects if needed.
387
Finally prepares and calls the responding form or redirects to the according action.
388
The output list is realised by a HTML-Template to avoid "bin/mozilla".
389

  
390
=head1 URL ACTIONS
391

  
392
=over 4
393

  
394
=item C<action_add>
395

  
396
Adds a new piece.
397

  
398
=item C<action_delete
399

  
400
Deletes an existing piece.
401

  
402
=item C<action_edit>
403

  
404
Edits an existing piece.
405

  
406
=item C<action_list>
407

  
408
Lists the filtred and sorted piecees.
409

  
410
=item C<action_save>
411

  
412
Saves a new or edited piece and responds the Edit-Form.
413

  
414
=item C<action_save_and_close>
415

  
416
Saves a new or edited piece and closes the frame.
417

  
418
=item C<action_search>
419

  
420
Filters the pieces and their fields to list.
421

  
422
=back
423

  
424
=head1 HELPER FUNCTIONS
425

  
426
=over 4
427

  
428
=item C<_copy>
429

  
430
Copies the fields of a piece from the requesting form to the rose-object.
431

  
432
=item C<_create>
433

  
434
Creates a new piece-rose-object.
435

  
436
=item C<_filter>
437

  
438
Returns the filter of the pieces-query and sets those of the responding form.
439

  
440
=item C<_forward_tags_hidden>
441

  
442
Sets the searche and sort criteria for the callback of the responding form.
443

  
444
=item C<_forward_tags_redirected>
445

  
446
Sets the searche and sort criteria for the callback of the redirected action.
447

  
448
=item C<_load>
449

  
450
Loads the piece-rose-object at the redirected action.
451

  
452
=item C<_pre_render>
453

  
454
Prepares the responding form.
455

  
456
=item C<_redirect_to>
457

  
458
Redirects to the passed action.
459

  
460
=item C<_save>
461

  
462
Saves a new or edited piece.
463

  
464
=item C<_sort>
465

  
466
Sorts the loaded batches by the key of the requesting form.
467

  
468
=back
469

  
470
=head1 ACTION BARS
471

  
472
=over 4
473

  
474
_setup_form_action_bar
475
_setup_list_action_bar
476
_setup_search_action_bar
477

  
478
=back
479

  
480
=head1 TODO
481

  
482
=over 4
483

  
484
=item *
485

  
486
The action "delete" still deletes physically.
487
To mark the table-entries as deletet could be advantageous.
488

  
489
=item *
490

  
491
Actually the filter- and sort-criteria for the callbacked list are passed through the responds.
492
They should be cached and restored by the list.
493

  
494
=item *
495

  
496
The History-Button of the form_action_bar isn,t yet implemented
497

  
498
=item *
499

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

  
502
=back
503

  
504
=head1 AUTHOR
505

  
506
Rolf Flühmann E<lt>rolf.fluehmann@revamp-it.chE<gt>,
507
ROlf Flühmann E<lt>rolf_fluehmann@gmx.chE<gt>
508

  
509
=cut

Auch abrufbar als: Unified diff