Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision d707f7ac

Von Moritz Bunkus vor fast 17 Jahren hinzugefügt

  • ID d707f7ac60b9dbe7da50d733db1e73eae84952f7
  • Vorgänger da734ed4
  • Nachfolger 3a83b0d2

Lieferscheine im Einkauf und Verkauf. Bisher nur gemerget, noch nicht getestet.

Unterschiede anzeigen:

SL/DO.pm
1
#====================================================================
2
# LX-Office ERP
3
# Copyright (C) 2004
4
# Based on SQL-Ledger Version 2.1.9
5
# Web http://www.lx-office.org
6
#
7
#=====================================================================
8
# SQL-Ledger Accounting
9
# Copyright (C) 1999-2003
10
#
11
#  Author: Dieter Simader
12
#   Email: dsimader@sql-ledger.org
13
#     Web: http://www.sql-ledger.org
14
#
15
#  Contributors:
16
#
17
# This program is free software; you can redistribute it and/or modify
18
# it under the terms of the GNU General Public License as published by
19
# the Free Software Foundation; either version 2 of the License, or
20
# (at your option) any later version.
21
#
22
# This program is distributed in the hope that it will be useful,
23
# but WITHOUT ANY WARRANTY; without even the implied warranty of
24
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
# GNU General Public License for more details.
26
# You should have received a copy of the GNU General Public License
27
# along with this program; if not, write to the Free Software
28
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29
#======================================================================
30
#
31
# Delivery Order entry module
32
#======================================================================
33

  
34
package DO;
35

  
36
use List::Util qw(max);
37
use YAML;
38

  
39
use SL::AM;
40
use SL::Common;
41
use SL::DBUtils;
42

  
43
sub transactions {
44
  $main::lxdebug->enter_sub();
45

  
46
  my ($self)   = @_;
47

  
48
  my $myconfig = \%main::myconfig;
49
  my $form     = $main::form;
50

  
51
  # connect to database
52
  my $dbh = $form->get_standard_dbh($myconfig);
53

  
54
  my (@where, @values, $where);
55

  
56
  my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
57

  
58
  $query =
59
    qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.oreqnumber, dord.transdate,
60
         ct.name, dord.${vc}_id, dord.globalproject_id,
61
         dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
62
         dord.transaction_description,
63
         pr.projectnumber AS globalprojectnumber,
64
         e.name AS employee,
65
         sm.name AS salesman,
66
         oreq.id AS oreq_id,
67
         oe.id AS oe_id
68
       FROM delivery_orders dord
69
       LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
70
       LEFT JOIN employee e ON (dord.employee_id = e.id)
71
       LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
72
       LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
73
       LEFT JOIN order_request oreq ON (dord.oreqnumber = oreq.oreqnumber)
74
       LEFT JOIN oe ON ((dord.ordnumber = oe.ordnumber) AND NOT COALESCE(oe.quotation, FALSE))|;
75

  
76
  push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
77

  
78
  my $department_id = (split /--/, $form->{department})[1];
79
  if ($department_id) {
80
    push @where,  qq|dord.department_id = ?|;
81
    push @values, conv_i($department_id);
82
  }
83

  
84
  if ($form->{project_id}) {
85
    $query .=
86
      qq|(dord.globalproject_id = ?) OR EXISTS
87
          (SELECT * FROM delivery_order_items doi
88
           WHERE (doi.project_id = ?) AND (oi.delivery_order_id = dord.id))|;
89
    push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
90
  }
91

  
92
  if ($form->{"${vc}_id"}) {
93
    push @where,  qq|dord.${vc}_id = ?|;
94
    push @values, $form->{"${vc}_id"};
95

  
96
  } elsif ($form->{$vc}) {
97
    push @where,  qq|ct.name ILIKE ?|;
98
    push @values, '%' . $form->{$vc} . '%';
99
  }
100

  
101
  foreach my $item (qw(employee_id salesman_id)) {
102
    next unless ($form->{$item});
103
    push @where, "dord.$item = ?";
104
    push @values, conv_i($form->{$item});
105
  }
106

  
107
  foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
108
    next unless ($form->{$item});
109
    push @where,  qq|dord.$item ILIKE ?|;
110
    push @values, '%' . $form->{$item} . '%';
111
  }
112

  
113
  if (!($form->{open} && $form->{closed})) {
114
    push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
115
  }
116

  
117
  if (($form->{notdelivered} || $form->{delivered}) &&
118
      ($form->{notdelivered} ne $form->{delivered})) {
119
    push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
120
  }
121

  
122
  if($form->{transdatefrom}) {
123
    push @where,  qq|dord.transdate >= ?|;
124
    push @values, conv_date($form->{transdatefrom});
125
  }
126

  
127
  if($form->{transdateto}) {
128
    push @where,  qq|dord.transdate <= ?|;
129
    push @values, conv_date($form->{transdateto});
130
  }
131

  
132
  if (@where) {
133
    $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
134
  }
135

  
136
  my %allowed_sort_columns = (
137
    "transdate"               => "dord.transdate",
138
    "id"                      => "dord.id",
139
    "donumber"                => "dord.donumber",
140
    "ordnumber"               => "dord.ordnumber",
141
    "oreqnumber"              => "dord.oreqnumber",
142
    "name"                    => "ct.name",
143
    "employee"                => "e.name",
144
    "salesman"                => "sm.name",
145
    "shipvia"                 => "dord.shipvia",
146
    "transaction_description" => "dord.transaction_description"
147
  );
148

  
149
  my $sortoder = "dord.id";
150
  if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
151
    $sortorder = $allowed_sort_columns{$form->{sort}};
152
  }
153

  
154
  $query .= qq| ORDER by | . $sortorder;
155

  
156
  $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
157

  
158
  $main::lxdebug->dump(0, "DO", $form->{DO});
159

  
160
  $main::lxdebug->leave_sub();
161
}
162

  
163
sub save {
164
  $main::lxdebug->enter_sub();
165

  
166
  my ($self)   = @_;
167

  
168
  my $myconfig = \%main::myconfig;
169
  my $form     = $main::form;
170

  
171
  # connect to database, turn off autocommit
172
  my $dbh = $form->get_standard_dbh($myconfig);
173

  
174
  my ($query, @values, $sth, $null);
175

  
176
  my $all_units = AM->retrieve_units($myconfig, $form);
177
  $form->{all_units} = $all_units;
178

  
179
  $form->{donumber}    = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
180
  $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
181
  $form->get_employee($dbh) unless ($form->{employee_id});
182

  
183
  my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
184

  
185
  if ($form->{id}) {
186

  
187
    $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
188
    do_query($form, $dbh, $query, conv_i($form->{id}));
189

  
190
    $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
191
    do_query($form, $dbh, $query, conv_i($form->{id}));
192

  
193
    $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
194
    do_query($form, $dbh, $query, conv_i($form->{id}));
195

  
196
  } else {
197

  
198
    $query = qq|SELECT nextval('id')|;
199
    ($form->{id}) = selectrow_query($form, $dbh, $query);
200

  
201
    $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
202
    do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
203
  }
204

  
205
  my $project_id;
206
  my $reqdate;
207

  
208
  $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
209
  my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
210
  my $price_factor;
211

  
212
  my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
213
  my @part_ids    = keys %part_id_map;
214
  my %part_unit_map;
215

  
216
  if (@part_ids) {
217
    $query         = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
218
    %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
219
  }
220

  
221
  my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
222
  my $h_item_id = prepare_query($form, $dbh, $q_item_id);
223

  
224
  my $q_item =
225
    qq|INSERT INTO delivery_order_items (
226
         id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
227
         sellprice, discount, unit, reqdate, project_id, serialnumber,
228
         ordnumber, transdate, cusordnumber,
229
         lastcost, price_factor_id, price_factor, marge_price_factor,
230
         v_partnumber, v_description)
231
       VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
232
         (SELECT factor FROM price_factors WHERE id = ?), ?, ?, ?)|;
233
  my $h_item = prepare_query($form, $dbh, $q_item);
234

  
235
  my $q_item_stock =
236
    qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber)
237
       VALUES (?, ?, ?, ?, ?, ?)|;
238
  my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
239

  
240
  my $in_out       = $form->{type} =~ /^sales/ ? 'out' : 'in';
241

  
242
  for my $i (1 .. $form->{rowcount}) {
243
    next if (!$form->{"id_$i"});
244

  
245
    $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
246

  
247
    my $item_unit = $part_unit_map{$form->{"id_$i"}};
248

  
249
    my $basefactor = 1;
250
    if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
251
      $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
252
    }
253
    my $baseqty = $form->{"qty_$i"} * $basefactor;
254

  
255
    $form->{"lastcost_$i"} *= 1;
256

  
257
    # set values to 0 if nothing entered
258
    $form->{"discount_$i"}  = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
259
    $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
260

  
261
    $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
262
    $linetotal    = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
263

  
264
    $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
265

  
266
    do_statement($form, $h_item_id, $q_item_id);
267
    my ($item_id) = $h_item_id->fetchrow_array();
268

  
269
    # save detail record in delivery_order_items table
270
    @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
271
               $form->{"description_$i"}, $form->{"longdescription_$i"},
272
               $form->{"qty_$i"}, $baseqty,
273
               $form->{"sellprice_$i"}, $form->{"discount_$i"},
274
               $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
275
               $form->{"serialnumber_$i"},
276
               $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
277
               $form->{"cusordnumber_$i"},
278
               $form->{"lastcost_$i"},
279
               conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
280
               conv_i($form->{"marge_price_factor_$i"}),
281
               $form->{"v_partnumber_$i"}, $form->{"v_description_$i"});
282
    do_statement($form, $h_item, $q_item, @values);
283

  
284
    my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
285

  
286
    foreach my $sinfo (@{ $stock_info }) {
287
      @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
288
                 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber});
289
      do_statement($form, $h_item_stock, $q_item_stock, @values);
290
    }
291
  }
292

  
293
  $h_item_id->finish();
294
  $h_item->finish();
295
  $h_item_stock->finish();
296

  
297
  ($null, $form->{department_id}) = split(/--/, $form->{department});
298

  
299
  # save DO record
300
  $query =
301
    qq|UPDATE delivery_orders SET
302
         donumber = ?, ordnumber = ?, cusordnumber = ?, oreqnumber = ?, transdate = ?, vendor_id = ?,
303
         customer_id = ?, reqdate = ?,
304
         shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
305
         delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
306
         globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
307
         is_sales = ?
308
       WHERE id = ?|;
309

  
310
  @values = ($form->{donumber}, $form->{ordnumber},
311
             $form->{cusordnumber}, $form->{oreqnumber}, conv_date($form->{transdate}),
312
             conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
313
             conv_date($reqdate), $form->{shippingpoint}, $form->{shipvia},
314
             $form->{notes}, $form->{intnotes},
315
             $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
316
             conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
317
             conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
318
             conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
319
             $form->{transaction_description},
320
             $form->{type} =~ /^sales/ ? 't' : 'f',
321
             conv_i($form->{id}));
322
  do_query($form, $dbh, $query, @values);
323

  
324
  # add shipto
325
  $form->{name} = $form->{ $form->{vc} };
326
  $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
327

  
328
  if (!$form->{shipto_id}) {
329
    $form->add_shipto($dbh, $form->{id}, "DO");
330
  }
331

  
332
  # save printed, emailed, queued
333
  $form->save_status($dbh);
334

  
335
  my $rc = $dbh->commit();
336

  
337
  $form->{saved_donumber} = $form->{donumber};
338

  
339
  Common::webdav_folder($form) if ($main::webdav);
340

  
341
  $main::lxdebug->leave_sub();
342

  
343
  return $rc;
344
}
345

  
346
sub close_order {
347
  $main::lxdebug->enter_sub();
348

  
349
  my ($self)   = @_;
350

  
351
  my $myconfig = \%main::myconfig;
352
  my $form     = $main::form;
353

  
354
  return $main::lxdebug->leave_sub() unless ($form->{id});
355

  
356
  my $dbh = $form->get_standard_dbh($myconfig);
357
  do_query($form, $dbh, qq|UPDATE do SET closed = TRUE where id = ?|, conv_i($form->{id}));
358
  $dbh->commit();
359

  
360
  $main::lxdebug->leave_sub();
361
}
362

  
363
sub delete {
364
  $main::lxdebug->enter_sub();
365

  
366
  my ($self)   = @_;
367

  
368
  my $myconfig = \%main::myconfig;
369
  my $form     = $main::form;
370
  my $spool    = $main::spool;
371

  
372
  # connect to database
373
  my $dbh = $form->get_standard_dbh($myconfig);
374

  
375
  # delete spool files
376
  my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
377
  my $sth   = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
378

  
379
  my $spoolfile;
380
  my @spoolfiles = ();
381

  
382
  while (($spoolfile) = $sth->fetchrow_array) {
383
    push @spoolfiles, $spoolfile;
384
  }
385
  $sth->finish();
386

  
387
  # delete-values
388
  @values = (conv_i($form->{id}));
389

  
390
  # delete status entries
391
  $query = qq|DELETE FROM status
392
              WHERE trans_id = ?|;
393
  do_query($form, $dbh, $query, @values);
394

  
395
  # delete individual entries
396
  $query = qq|DELETE FROM delivery_order_items_stock
397
              WHERE delivery_order_item_id IN (
398
                SELECT id FROM delivery_order_items
399
                WHERE delivery_order_id = ?
400
              )|;
401
  do_query($form, $dbh, $query, @values);
402

  
403
  # delete individual entries
404
  $query = qq|DELETE FROM delivery_order_items
405
              WHERE delivery_order_id = ?|;
406
  do_query($form, $dbh, $query, @values);
407

  
408
  # delete DO record
409
  $query = qq|DELETE FROM delivery_orders
410
              WHERE id = ?|;
411
  do_query($form, $dbh, $query, @values);
412

  
413
  $query = qq|DELETE FROM shipto
414
              WHERE trans_id = ? AND module = 'DO'|;
415
  do_query($form, $dbh, $query, @values);
416

  
417
  my $rc = $dbh->commit();
418

  
419
  if ($rc) {
420
    foreach $spoolfile (@spoolfiles) {
421
      unlink "$spool/$spoolfile" if $spoolfile;
422
    }
423
  }
424

  
425
  $main::lxdebug->leave_sub();
426

  
427
  return $rc;
428
}
429

  
430
sub retrieve {
431
  $main::lxdebug->enter_sub();
432

  
433
  my ($self)   = @_;
434

  
435
  my $myconfig = \%main::myconfig;
436
  my $form     = $main::form;
437

  
438
  # connect to database
439
  my $dbh = $form->get_standard_dbh($myconfig);
440

  
441
  my ($query, $query_add, @values, $sth, $ref);
442

  
443
  if (!$form->{id}) {
444
    $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate, current_date AS reqdate|);
445
    map { $form->{$_} = $ref->{$_} } keys %$ref;
446
  }
447

  
448
  my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
449

  
450
  if ($form->{id}) {
451

  
452
    # retrieve order for single id
453
    # NOTE: this query is intended to fetch all information only ONCE.
454
    # so if any of these infos is important (or even different) for any item,
455
    # it will be killed out and then has to be fetched from the item scope query further down
456
    $query =
457
      qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.oreqnumber, dord.transdate, dord.reqdate,
458
           dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
459
           e.name AS employee, dord.employee_id, dord.salesman_id,
460
           dord.${vc}_id, cv.name AS ${vc},
461
           dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
462
           d.description AS department, dord.language_id,
463
           dord.shipto_id,
464
           dord.globalproject_id, dord.delivered, dord.transaction_description
465
         FROM delivery_orders dord
466
         JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
467
         LEFT JOIN employee e ON (dord.employee_id = e.id)
468
         LEFT JOIN department d ON (dord.department_id = d.id)
469
         WHERE dord.id = ?|;
470
    $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
471

  
472
    $ref = $sth->fetchrow_hashref(NAME_lc);
473
    $sth->finish();
474

  
475
    map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
476

  
477
    $form->{saved_donumber} = $form->{donumber};
478

  
479
    # if not given, fill transdate with current_date
480
    $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
481

  
482
    $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
483
    $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
484

  
485
    $ref = $sth->fetchrow_hashref(NAME_lc);
486
    delete($ref->{id});
487
    map { $form->{$_} = $ref->{$_} } keys %$ref;
488
    $sth->finish;
489

  
490
    # get printed, emailed and queued
491
    $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
492
    $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
493

  
494
    while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
495
      $form->{printed} .= "$ref->{formname} " if $ref->{printed};
496
      $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
497
      $form->{queued}  .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
498
    }
499
    $sth->finish;
500
    map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
501

  
502
    my %oid = ('Pg'     => 'oid',
503
               'Oracle' => 'rowid');
504

  
505
    my $transdate = $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
506

  
507
    # retrieve individual items
508
    # this query looks up all information about the items
509
    # stuff different from the whole will not be overwritten, but saved with a suffix.
510
    $query =
511
      qq|SELECT doi.id AS delivery_order_items_id,
512
           p.partnumber, p.assembly, doi.description, doi.qty,
513
           doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
514
           doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
515
           doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
516
           doi.price_factor_id, doi.price_factor, doi.marge_price_factor,
517
           doi.v_partnumber, doi.v_description,
518
           pr.projectnumber,
519
           pg.partsgroup
520
         FROM delivery_order_items doi
521
         JOIN parts p ON (doi.parts_id = p.id)
522
         JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
523
         LEFT JOIN project pr ON (doi.project_id = pr.id)
524
         LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
525
         WHERE doi.delivery_order_id = ?
526
         ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
527

  
528
    $form->{form_details} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
529

  
530
    my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
531

  
532
    $query =
533
      qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber
534
           FROM delivery_order_items_stock
535
           WHERE delivery_order_item_id = ?|;
536
    my $sth = prepare_query($form, $dbh, $query);
537

  
538
    foreach my $doi (@{ $form->{form_details} }) {
539
      do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
540
      my $requests = [];
541
      while (my $ref = $sth->fetchrow_hashref()) {
542
        push @{ $requests }, $ref;
543
      }
544

  
545
      $doi->{"stock_${in_out}"} = YAML::Dump($requests);
546
    }
547

  
548
    $sth->finish();
549

  
550
  } else {
551
    # get last name used
552
    $form->lastname_used($dbh, $myconfig, $form->{vc}) unless $form->{"$form->{vc}_id"};
553

  
554
  }
555

  
556
  Common::webdav_folder($form) if ($main::webdav);
557

  
558
  $main::lxdebug->leave_sub();
559
}
560

  
561
sub order_details {
562
  $main::lxdebug->enter_sub();
563

  
564
  my ($self)   = @_;
565

  
566
  my $myconfig = \%main::myconfig;
567
  my $form     = $main::form;
568

  
569
  # connect to database
570
  my $dbh = $form->get_standard_dbh($myconfig);
571
  my $query;
572
  my @values = ();
573
  my $sth;
574
  my $item;
575
  my $i;
576
  my @partsgroup = ();
577
  my $partsgroup;
578
  my $position = 0;
579

  
580
  my %oid = ('Pg'     => 'oid',
581
             'Oracle' => 'rowid');
582

  
583
  my (@project_ids, %projectnumbers);
584

  
585
  push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
586

  
587
  # sort items by partsgroup
588
  for $i (1 .. $form->{rowcount}) {
589
    $partsgroup = "";
590
    if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
591
      $partsgroup = $form->{"partsgroup_$i"};
592
    }
593
    push @partsgroup, [$i, $partsgroup];
594
    push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
595
  }
596

  
597
  if (@project_ids) {
598
    $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
599
      join(", ", map("?", @project_ids)) . ")";
600
    $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
601
    while (my $ref = $sth->fetchrow_hashref()) {
602
      $projectnumbers{$ref->{id}} = $ref->{projectnumber};
603
    }
604
    $sth->finish();
605
  }
606

  
607
  $form->{"globalprojectnumber"} =
608
    $projectnumbers{$form->{"globalproject_id"}};
609

  
610
  my $q_pg     = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
611
                    FROM assembly a
612
                    JOIN parts p ON (a.parts_id = p.id)
613
                    LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
614
                    WHERE a.bom = '1'
615
                      AND a.id = ? $sortorder|;
616
  my $h_pg     = prepare_query($form, $dbh, $q_pg);
617

  
618
  my $q_bin_wh = qq|SELECT (SELECT description FROM bin       WHERE id = ?) AS bin,
619
                           (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
620
  my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
621

  
622
  my $in_out   = $form->{type} =~ /^sales/ ? 'out' : 'in';
623

  
624
  my $num_si   = 0;
625

  
626
  my @arrays =
627
    qw(runningnumber number description longdescription qty unit
628
       partnotes serialnumber reqdate projectnumber
629
       si_runningnumber si_number si_description
630
       si_warehouse si_bin si_chargenumber si_qty si_unit
631
       v_partnumber v_description);
632

  
633
  my $sameitem = "";
634
  foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
635
    $i = $item->[0];
636

  
637
    next if (!$form->{"id_$i"});
638

  
639
    $position++;
640

  
641
    if ($item->[1] ne $sameitem) {
642
      push(@{ $form->{description} }, qq|$item->[1]|);
643
      $sameitem = $item->[1];
644

  
645
      map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
646
    }
647

  
648
    $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
649

  
650
    # add number, description and qty to $form->{number}, ....
651

  
652
    my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
653

  
654
    push @{ $form->{runningnumber} },   $position;
655
    push @{ $form->{number} },          $form->{"partnumber_$i"};
656
    push @{ $form->{description} },     $form->{"description_$i"};
657
    push @{ $form->{longdescription} }, $form->{"longdescription_$i"};
658
    push @{ $form->{qty} },             $form->format_amount($myconfig, $form->{"qty_$i"});
659
    push @{ $form->{unit} },            $form->{"unit_$i"};
660
    push @{ $form->{partnotes} },       $form->{"partnotes_$i"};
661
    push @{ $form->{serialnumber} },    $form->{"serialnumber_$i"};
662
    push @{ $form->{reqdate} },         $form->{"reqdate_$i"};
663
    push @{ $form->{projectnumber} },   $projectnumbers{$form->{"project_id_$i"}};
664
    push @{ $form->{v_partnumber} },    $form->{"v_partnumber_$i"};
665
    push @{ $form->{v_description} },   $form->{"v_description_$i"};
666

  
667
    if ($form->{"assembly_$i"}) {
668
      $sameitem = "";
669

  
670
      # get parts and push them onto the stack
671
      my $sortorder = "";
672
      if ($form->{groupitems}) {
673
        $sortorder =
674
          qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
675
      } else {
676
        $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
677
      }
678

  
679
      do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
680

  
681
      while (my $ref = $h_pg->fetchrow_hashref(NAME_lc)) {
682
        if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
683
          map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
684
          $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
685
          push(@{ $form->{description} }, $sameitem);
686
        }
687

  
688
        push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
689

  
690
        map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
691
      }
692
    }
693

  
694
    if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
695
      my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
696

  
697
      foreach my $si (@{ $stock_info }) {
698
        $num_si++;
699

  
700
        do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
701
        my $bin_wh = $h_bin_wh->fetchrow_hashref();
702

  
703
        push @{ $form->{si_runningnumber} }, $num_si;
704
        push @{ $form->{si_number} },        $form->{"partnumber_$i"};
705
        push @{ $form->{si_description} },   $form->{"description_$i"};
706
        push @{ $form->{si_warehouse} },     $bin_wh->{warehouse};
707
        push @{ $form->{si_bin} },           $bin_wh->{bin};
708
        push @{ $form->{si_chargenumber} },  $si->{chargenumber};
709
        push @{ $form->{si_qty} },           $form->format_amount($myconfig, $si->{qty} * 1);
710
        push @{ $form->{si_unit} },          $si->{unit};
711
      }
712
    }
713
  }
714

  
715
  $h_pg->finish();
716
  $h_bin_wh->finish();
717

  
718
  $form->{username} = $myconfig->{name};
719

  
720
  $main::lxdebug->leave_sub();
721
}
722

  
723
sub project_description {
724
  $main::lxdebug->enter_sub();
725

  
726
  my ($self, $dbh, $id) = @_;
727

  
728
  my $query = qq|SELECT description FROM project WHERE id = ?|;
729
  my ($value) = selectrow_query($form, $dbh, $query, $id);
730

  
731
  $main::lxdebug->leave_sub();
732

  
733
  return $value;
734
}
735

  
736
sub unpack_stock_information {
737
  $main::lxdebug->enter_sub();
738

  
739
  my $self   = shift;
740
  my %params = @_;
741

  
742
  Common::check_params_x(\%params, qw(packed));
743

  
744
  my $unpacked;
745

  
746
  eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
747

  
748
  $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
749

  
750
  foreach my $entry (@{ $unpacked }) {
751
    next if ('HASH' eq ref $entry);
752
    $unpacked = [];
753
    last;
754
  }
755

  
756
  $main::lxdebug->leave_sub();
757

  
758
  return $unpacked;
759
}
760

  
761
sub get_item_availability {
762
  $main::lxdebug->enter_sub();
763

  
764
  my $self     = shift;
765
  my %params   = @_;
766

  
767
  Common::check_params(\%params, qw(parts_id));
768

  
769
  my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
770
  my $form      = $main::form;
771

  
772
  my $query     =
773
    qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, SUM(qty) AS qty, i.parts_id,
774
         w.description AS warehousedescription,
775
         b.description AS bindescription
776
       FROM inventory i
777
       LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
778
       LEFT JOIN bin b       ON (i.bin_id       = b.id)
779
       WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
780
         AND qty > 0
781
       GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.parts_id, w.description, b.description
782
       ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber)|;
783

  
784
  my $contents = selectall_hashref_query($form, $form->get_standard_dbh($myconfig), $query, @parts_ids);
785

  
786
  $main::lxdebug->leave_sub();
787

  
788
  return @{ $contents };
789
}
790

  
791

  
792
sub check_stock_availability {
793
  $main::lxdebug->enter_sub();
794

  
795
  my $self     = shift;
796
  my %params   = @_;
797

  
798
  Common::check_params(\%params, qw(requests parts_id));
799

  
800
  my $myconfig    = \%main::myconfig;
801
  my $form        =  $main::form;
802

  
803
  my $dbh         = $form->get_standard_dbh($myconfig);
804

  
805
  my $units       = AM->retrieve_units($myconfig, $form, "dimension");
806

  
807
  my ($partunit)  = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
808
  my $unit_factor = $units->{$partunit}->{factor} || 1;
809

  
810
  my @contents    = $self->get_item_availability(%params);
811

  
812
  my @errors;
813

  
814
  foreach my $sinfo (@{ $params{requests} }) {
815
    my $found = 0;
816

  
817
    foreach my $row (@contents) {
818
      next if (($row->{bin_id}       != $sinfo->{bin_id}) ||
819
               ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
820
               ($row->{chargenumber} ne $sinfo->{chargenumber}));
821

  
822
      $found       = 1;
823

  
824
      my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
825

  
826
      if ($base_qty > $row->{qty}) {
827
        $sinfo->{error} = 1;
828
        push @errors, $sinfo;
829

  
830
        last;
831
      }
832
    }
833

  
834
    push @errors, $sinfo if (!$found);
835
  }
836

  
837
  $main::lxdebug->leave_sub();
838

  
839
  return @errors;
840
}
841

  
842
sub transfer_in_out {
843
  $main::lxdebug->enter_sub();
844

  
845
  my $self     = shift;
846
  my %params   = @_;
847

  
848
  Common::check_params(\%params, qw(direction requests));
849

  
850
  if (!@{ $params{requests} }) {
851
    $main::lxdebug->leave_sub();
852
    return;
853
  }
854

  
855
  my $myconfig = \%main::myconfig;
856
  my $form     = $main::form;
857

  
858
  my $prefix   = $params{direction} eq 'in' ? 'dst' : 'src';
859

  
860
  my @transfers;
861

  
862
  foreach my $request (@{ $params{requests} }) {
863
    push @transfers, {
864
      'parts_id'               => $request->{parts_id},
865
      "${prefix}_warehouse_id" => $request->{warehouse_id},
866
      "${prefix}_bin_id"       => $request->{bin_id},
867
      'chargenumber'           => $request->{chargenumber},
868
      'qty'                    => $request->{qty},
869
      'unit'                   => $request->{unit},
870
      'oe_id'                  => $form->{id},
871
      'shippingdate'           => 'current_date',
872
      'transfer_type'          => $params{direction} eq 'in' ? 'stock' : 'shipped',
873
    };
874
  }
875

  
876
  WH->transfer(@transfers);
877

  
878
  $main::lxdebug->leave_sub();
879
}
880

  
881
1;
SL/Form.pm
53 53
use SL::Template;
54 54
use SL::User;
55 55
use Template;
56
use List::Util qw(max min sum);
56
use List::Util qw(first max min sum);
57 57

  
58 58
my $standard_dbh;
59 59

  
......
1257 1257
  $formname ||= $self->{formname};
1258 1258

  
1259 1259
  my %formname_translations = (
1260
    bin_list            => $main::locale->text('Bin List'),
1261
    credit_note         => $main::locale->text('Credit Note'),
1262
    invoice             => $main::locale->text('Invoice'),
1263
    packing_list        => $main::locale->text('Packing List'),
1264
    pick_list           => $main::locale->text('Pick List'),
1265
    proforma            => $main::locale->text('Proforma Invoice'),
1266
    purchase_order      => $main::locale->text('Purchase Order'),
1267
    request_quotation   => $main::locale->text('RFQ'),
1268
    sales_order         => $main::locale->text('Confirmation'),
1269
    sales_quotation     => $main::locale->text('Quotation'),
1270
    storno_invoice      => $main::locale->text('Storno Invoice'),
1271
    storno_packing_list => $main::locale->text('Storno Packing List'),
1260
    bin_list                => $main::locale->text('Bin List'),
1261
    credit_note             => $main::locale->text('Credit Note'),
1262
    invoice                 => $main::locale->text('Invoice'),
1263
    packing_list            => $main::locale->text('Packing List'),
1264
    pick_list               => $main::locale->text('Pick List'),
1265
    proforma                => $main::locale->text('Proforma Invoice'),
1266
    purchase_order          => $main::locale->text('Purchase Order'),
1267
    request_quotation       => $main::locale->text('RFQ'),
1268
    sales_order             => $main::locale->text('Confirmation'),
1269
    sales_quotation         => $main::locale->text('Quotation'),
1270
    storno_invoice          => $main::locale->text('Storno Invoice'),
1271
    storno_packing_list     => $main::locale->text('Storno Packing List'),
1272
    sales_delivery_order    => $main::locale->text('Delivery Order'),
1273
    purchase_delivery_order => $main::locale->text('Delivery Order'),
1272 1274
  );
1273 1275

  
1274 1276
  return $formname_translations{$formname}
......
1278 1280
  my ($self) = @_;
1279 1281

  
1280 1282
  my $attachment_filename = $self->unquote_html($self->get_formname_translation());
1281
  my $prefix = 
1282
      (grep { $self->{"type"} eq $_ } qw(invoice credit_note)) ? "inv"
1283
    : ($self->{"type"} =~ /_quotation$/)                       ? "quo"
1284
    :                                                            "ord";
1283
  my $prefix =
1284
      (first { $self->{type} eq $_ } qw(invoice credit_note)) ? 'inv'
1285
    : ($self->{type} =~ /_quotation$/)                        ? 'quo'
1286
    : ($self->{type} =~ /_delivery_order$/)                   ? 'do'
1287
    :                                                           'ord';
1285 1288

  
1286 1289
  if ($attachment_filename && $self->{"${prefix}number"}) {
1287 1290
    $attachment_filename .= "_" . $self->{"${prefix}number"}
SL/User.pm
955 955
    printer role sid signature stylesheet tel templates vclimit angebote
956 956
    bestellungen rechnungen anfragen lieferantenbestellungen einkaufsrechnungen
957 957
    taxnumber co_ustid duns menustyle template_format default_media
958
    default_printer_id copies show_form_details favorites);
958
    default_printer_id copies show_form_details favorites
959
    pdonumber sdonumber);
959 960

  
960 961
  $main::lxdebug->leave_sub();
961 962

  
bin/mozilla/do.pl
1
# #=====================================================================
2
# LX-Office ERP
3
# Copyright (C) 2004
4
# Based on SQL-Ledger Version 2.1.9
5
# Web http://www.lx-office.org
6
#
7
#=====================================================================
8
# SQL-Ledger, Accounting
9
# Copyright (c) 1998-2003
10
#
11
#  Author: Dieter Simader
12
#   Email: dsimader@sql-ledger.org
13
#     Web: http://www.sql-ledger.org
14
#
15
#
16
# This program is free software; you can redistribute it and/or modify
17
# it under the terms of the GNU General Public License as published by
18
# the Free Software Foundation; either version 2 of the License, or
19
# (at your option) any later version.
20
#
21
# This program is distributed in the hope that it will be useful,
22
# but WITHOUT ANY WARRANTY; without even the implied warranty of
23
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
# GNU General Public License for more details.
25
# You should have received a copy of the GNU General Public License
26
# along with this program; if not, write to the Free Software
27
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28
#======================================================================
29
#
30
# Delivery orders
31
#======================================================================
32

  
33
use List::Util qw(max sum);
34
use POSIX qw(strftime);
35
use YAML;
36

  
37
use SL::DO;
38
use SL::IR;
39
use SL::IS;
40
use SL::ReportGenerator;
41
use SL::WH;
42

  
43
require "bin/mozilla/arap.pl";
44
require "bin/mozilla/common.pl";
45
require "bin/mozilla/invoice_io.pl";
46
require "bin/mozilla/io.pl";
47
require "bin/mozilla/reportgenerator.pl";
48

  
49
1;
50

  
51
# end of main
52

  
53
sub check_do_access {
54
  $auth->assert($form->{type} . '_edit');
55
}
56

  
57
sub set_headings {
58
  $lxdebug->enter_sub();
59

  
60
  check_do_access();
61

  
62
  my ($action) = @_;
63

  
64
  if ($form->{type} eq 'purchase_delivery_order') {
65
    $form->{vc}    = 'vendor';
66
    $form->{title} = $action eq "edit" ? $locale->text('Edit Purchase Delivery Order') : $locale->text('Add Purchase Delivery Order');
67
  } else {
68
    $form->{vc}    = 'customer';
69
    $form->{title} = $action eq "edit" ? $locale->text('Edit Sales Delivery Order') : $locale->text('Add Sales Delivery Order');
70
  }
71

  
72
  $form->{heading} = $locale->text('Delivery Order');
73

  
74
  $lxdebug->leave_sub();
75
}
76

  
77
sub add {
78
  $lxdebug->enter_sub();
79

  
80
  check_do_access();
81

  
82
  set_headings("add");
83

  
84
  $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
85

  
86
  order_links();
87
  prepare_order();
88
  display_form();
89

  
90
  $lxdebug->leave_sub();
91
}
92

  
93
sub edit {
94
  $lxdebug->enter_sub();
95

  
96
  check_do_access();
97

  
98
  # show history button
99
  $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
100
  #/show hhistory button
101

  
102
  $form->{simple_save} = 0;
103

  
104
  set_headings("edit");
105

  
106
  # editing without stuff to edit? try adding it first
107
  if ($form->{rowcount} && !$form->{print_and_save}) {
108
#     map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
109
#     if (!$id) {
110

  
111
      # reset rowcount
112
      undef $form->{rowcount};
113
      add();
114
      $lxdebug->leave_sub();
115
      return;
116
#     }
117
  } elsif (!$form->{id}) {
118
    add();
119
    $lxdebug->leave_sub();
120
    return;
121
  }
122

  
123
  if ($form->{print_and_save}) {
124
    $form->{action}   = "print";
125
    $form->{resubmit} = 1;
126
    $language_id      = $form->{language_id};
127
    $printer_id       = $form->{printer_id};
128
  }
129

  
130
  set_headings("edit");
131

  
132
  order_links();
133
  prepare_order();
134

  
135
  if ($form->{print_and_save}) {
136
    $form->{language_id} = $language_id;
137
    $form->{printer_id}  = $printer_id;
138
  }
139

  
140
  display_form();
141

  
142
  $lxdebug->leave_sub();
143
}
144

  
145
sub order_links {
146
  $lxdebug->enter_sub();
147

  
148
  check_do_access();
149

  
150
  # get customer/vendor
151
  $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
152

  
153
  # retrieve order/quotation
154
  $form->{webdav}   = $webdav;
155
  $form->{jsscript} = 1;
156

  
157
  my $editing = $form->{id};
158

  
159
  DO->retrieve();
160

  
161
  $payment_id  = $form->{payment_id}  if ($form->{payment_id});
162
  $language_id = $form->{language_id} if ($form->{language_id});
163
  $taxzone_id  = $form->{taxzone_id}  if ($form->{taxzone_id});
164
  $salesman_id = $form->{salesman_id} if ($editing);
165

  
166

  
167
  $taxincluded    = $form->{taxincluded};
168
  $form->{shipto} = 1 if $form->{id};
169

  
170
  if ($form->{"all_$form->{vc}"}) {
171
    unless ($form->{"$form->{vc}_id"}) {
172
      $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
173
    }
174
  }
175

  
176
  $cp_id    = $form->{cp_id};
177
  $intnotes = $form->{intnotes};
178

  
179
  $form->{cp_id} = $cp_id;
180

  
181
  $form->{payment_id}  = $payment_id  if ($payment_id);
182
  $form->{language_id} = $language_id if ($language_id);
183
  $form->{taxzone_id}  = $taxzone_id  if ($taxzone_id);
184
  $form->{intnotes}    = $intnotes    if ($intnotes);
185

  
186
  ($form->{ $form->{vc} })  = split /--/, $form->{ $form->{vc} };
187
  $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
188

  
189
  $form->{taxincluded} = $taxincluded if ($form->{id});
190

  
191
  $form->{employee} = "$form->{employee}--$form->{employee_id}";
192

  
193
  $form->{salesman_id} = $salesman_id if ($editing);
194

  
195
  $lxdebug->leave_sub();
196
}
197

  
198
sub prepare_order {
199
  $lxdebug->enter_sub();
200

  
201
  check_do_access();
202

  
203
  $form->{formname} = $form->{type} unless $form->{formname};
204

  
205
  my $i = 0;
206
  foreach $ref (@{ $form->{form_details} }) {
207
    $form->{rowcount} = ++$i;
208

  
209
    map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
210
  }
211
  for my $i (1 .. $form->{rowcount}) {
212
    if ($form->{id}) {
213
      $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
214
    } else {
215
      $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
216
    }
217
    ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
218
    $dec           = length $dec;
219
    $decimalplaces = ($dec > 2) ? $dec : 2;
220

  
221
    # copy reqdate from deliverydate for invoice -> order conversion
222
    $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
223

  
224
    $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
225

  
226
    (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
227
    $dec_qty = length $dec_qty;
228
    $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
229

  
230
    map { $form->{"${_}_$i"} =~ s/\"/&quot;/g } qw(partnumber description unit);
231
  }
232

  
233
  $lxdebug->leave_sub();
234
}
235

  
236
sub form_header {
237
  $lxdebug->enter_sub();
238

  
239
  check_do_access();
240

  
241
  $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
242
  $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
243

  
244
  # use JavaScript Calendar or not
245
  $form->{jsscript} = 1;
246

  
247
  #write Trigger
248
  $jsscript = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
249

  
250
  my @old_project_ids = ($form->{"globalproject_id"});
251
  map({ push(@old_project_ids, $form->{"project_id_$_"})
252
          if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
253

  
254
  my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
255
  $form->get_lists("contacts"       => "ALL_CONTACTS",
256
                   "shipto"         => "ALL_SHIPTO",
257
                   "projects"       => {
258
                     "key"          => "ALL_PROJECTS",
259
                     "all"          => 0,
260
                     "old_id"       => \@old_project_ids
261
                   },
262
                   "employees"      => "ALL_EMPLOYEES",
263
                   "salesmen"       => "ALL_SALESMEN",
264
                   $vc              => "ALL_VC",
265
                   "price_factors"  => "ALL_PRICE_FACTORS",
266
                   "departments"    => "ALL_DEPARTMENTS",
267
                   "business_types" => "ALL_BUSINESS_TYPES",
268
    );
269

  
270
  map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
271
  map { $_->{value} = "$_->{name}--$_->{id}"        } @{ $form->{ALL_VC} };
272

  
273
  $form->{SHOW_VC_DROP_DOWN} =  $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
274

  
275
  $form->{oldvcname}         =  $form->{"old$form->{vc}"};
276
  $form->{oldvcname}         =~ s/--.*//;
277

  
278
  $form->{onload} = "";
279
  if ($form->{resubmit}) {
280
    if ($form->{format} eq "html") {
281
      $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
282
    }
283
    $form->{onload} .= "document.do.submit();"
284
  }
285

  
286
  $form->header();
287
  print $form->parse_html_template('do/form_header');
288

  
289
  $lxdebug->leave_sub();
290
}
291

  
292
sub form_footer {
293
  $lxdebug->enter_sub();
294

  
295
  check_do_access();
296

  
297
  $form->{PRINT_OPTIONS} = print_options('inline' => 1);
298

  
299
  print $form->parse_html_template('do/form_footer');
300

  
301
  $lxdebug->leave_sub();
302
}
303

  
304
sub update_delivery_order {
305
  $lxdebug->enter_sub();
306

  
307
  check_do_access();
308

  
309
  set_headings($form->{"id"} ? "edit" : "add");
310

  
311
  $form->{update} = 1;
312

  
313
  $payment_id = $form->{payment_id} if $form->{payment_id};
314

  
315
  check_name($form->{vc});
316

  
317
  $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
318

  
319
  # for pricegroups
320
  $i = $form->{rowcount};
321

  
322
  if (   ($form->{"partnumber_$i"} eq "")
323
      && ($form->{"description_$i"} eq "")
324
      && ($form->{"partsgroup_$i"}  eq "")) {
325

  
326
    check_form();
327

  
328
  } else {
329

  
330
    if ($form->{type} eq 'purchase_delivery_order') {
331
      IR->retrieve_item(\%myconfig, $form);
332
    } else {
333
      IS->retrieve_item(\%myconfig, $form);
334
    }
335

  
336
    my $rows = scalar @{ $form->{item_list} };
337

  
338
    if ($rows) {
339
      $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
340

  
341
      if ($rows > 1) {
342

  
343
        select_item();
344
        exit;
345

  
346
      } else {
347

  
348
        map { $form->{item_list}[$i]{$_} =~ s/\"/&quot;/g }    qw(partnumber description unit);
349
        map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
350

  
351
        $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
352
        $form->{"sellprice_$i"}          = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
353
        $form->{"qty_$i"}                = $form->format_amount(\%myconfig, $form->{"qty_$i"});
354
      }
355

  
356
      display_form();
357

  
358
    } else {
359

  
360
      # ok, so this is a new part
361
      # ask if it is a part or service item
362

  
363
      if (   $form->{"partsgroup_$i"}
364
          && ($form->{"partsnumber_$i"} eq "")
365
          && ($form->{"description_$i"} eq "")) {
366
        $form->{rowcount}--;
367
        $form->{"discount_$i"} = "";
368
        display_form();
369

  
370
      } else {
371
        $form->{"id_$i"}   = 0;
372
        new_item();
373
      }
374
    }
375
  }
376

  
377
  $lxdebug->leave_sub();
378
}
379

  
380
sub search {
381
  $lxdebug->enter_sub();
382

  
383
  check_do_access();
384

  
385
  $form->{vc} = $form->{type} eq 'purchase_order' ? 'vendor' : 'customer';
386

  
387
  $form->get_lists("projects"     => { "key" => "ALL_PROJECTS",
388
                                       "all" => 1 },
389
                   "employees"    => "ALL_EMPLOYEES",
390
                   "salesmen"     => "ALL_SALESMEN",
391
                   "$form->{vc}s" => "ALL_VC");
392

  
393
  $form->{SHOW_VC_DROP_DOWN} =  $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
394
  $form->{jsscript}          = 1;
395
  $form->{title}             = $locale->text('Delivery Orders');
396

  
397
  $form->header();
398

  
399
  print $form->parse_html_template('do/search');
400

  
401
  $lxdebug->leave_sub();
402
}
403

  
404
sub orders {
405
  $lxdebug->enter_sub();
406

  
407
  check_do_access();
408

  
409
  ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
410

  
411
  $form->{sort} ||= 'transdate';
412

  
413
  DO->transactions();
414

  
415
  $form->{rowcount} = scalar @{ $form->{DO} };
416

  
417
  my @columns = qw(
418
    transdate
419
    id                      donumber
420
    ordnumber               oreqnumber
421
    name                    employee
422
    shipvia                 globalprojectnumber
423
    transaction_description
424
    open                    delivered
425
  );
426

  
427
  $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
428
  $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
429

  
430
  $form->{title}       = $locale->text('Delivery Orders');
431

  
432
  my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
433

  
434
  my $report = SL::ReportGenerator->new(\%myconfig, $form);
435

  
436
  my @hidden_variables = map { "l_${_}" } @columns;
437
  push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber
438
                                          transaction_description transdatefrom transdateto type vc employee_id salesman_id);
439

  
440
  my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
441

  
442
  my %column_defs = (
443
    'ids'                     => { 'text' => '', },
444
    'transdate'               => { 'text' => $locale->text('Date'), },
445
    'id'                      => { 'text' => $locale->text('ID'), },
446
    'donumber'                => { 'text' => $locale->text('Delivery Order'), },
447
    'ordnumber'               => { 'text' => $locale->text('Order'), },
448
    'oreqnumber'              => { 'text' => $locale->text('Order Request Number'), },
449
    'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
450
    'employee'                => { 'text' => $locale->text('Salesperson'), },
451
    'shipvia'                 => { 'text' => $locale->text('Ship via'), },
452
    'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
453
    'transaction_description' => { 'text' => $locale->text('Transaction description'), },
454
    'open'                    => { 'text' => $locale->text('Open'), },
455
    'delivered'               => { 'text' => $locale->text('Delivered'), },
456
  );
457

  
458
  foreach my $name (qw(id transdate donumber ordnumber oreqnumber name employee shipvia)) {
459
    $column_defs{$name}->{link} = $href . "&sort=$name";
460
  }
461

  
462
  $form->{"l_type"} = "Y";
463
  map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
464
  $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
465

  
466
  $report->set_columns(%column_defs);
467
  $report->set_column_order(@columns);
468

  
469
  $report->set_export_options('orders', @hidden_variables);
470

  
471
  $report->set_sort_indicator($form->{sort}, 1);
472

  
473
  my @options;
474
  if ($form->{customer}) {
475
    push @options, $locale->text('Customer') . " : $form->{customer}";
476
  }
477
  if ($form->{vendor}) {
478
    push @options, $locale->text('Vendor') . " : $form->{vendor}";
479
  }
480
  if ($form->{department}) {
481
    ($department) = split /--/, $form->{department};
482
    push @options, $locale->text('Department') . " : $department";
483
  }
484
  if ($form->{donumber}) {
485
    push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
486
  }
487
  if ($form->{ordnumber}) {
488
    push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
489
  }
490
  if ($form->{transaction_description}) {
491
    push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
492
  }
493
  if ($form->{transdatefrom}) {
494
    push @options, $locale->text('From') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
495
  }
496
  if ($form->{transdateto}) {
497
    push @options, $locale->text('Bis') . "&nbsp;" . $locale->date(\%myconfig, $form->{transdateto}, 1);
498
  }
499
  if ($form->{open}) {
500
    push @options, $locale->text('Open');
501
  }
502
  if ($form->{closed}) {
503
    push @options, $locale->text('Closed');
504
  }
505
  if ($form->{delivered}) {
506
    push @options, $locale->text('Delivered');
507
  }
508
  if ($form->{notdelivered}) {
509
    push @options, $locale->text('Not delivered');
510
  }
511

  
512
  $report->set_options('top_info_text'       => join("\n", @options),
513
                       'output_format'       => 'HTML',
514
                       'title'               => $form->{title},
515
                       'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
516
    );
517
  $report->set_options_from_form();
518

  
519
  # add sort and escape callback, this one we use for the add sub
520
  $form->{callback} = $href .= "&sort=$form->{sort}";
521

  
522
  # escape callback for href
523
  $callback = $form->escape($href);
524

  
525
  my $edit_url       = build_std_url('action=edit', 'type', 'vc');
526
  my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
... Dieser Diff wurde abgeschnitten, weil er die maximale Anzahl anzuzeigender Zeilen überschreitet.

Auch abrufbar als: Unified diff