Revision d1f44c84
Von Sven Schöling vor mehr als 7 Jahren hinzugefügt
SL/DO.pm | ||
---|---|---|
1237 | 1237 |
$main::lxdebug->leave_sub(); |
1238 | 1238 |
} |
1239 | 1239 |
|
1240 |
|
|
1241 |
sub get_shipped_qty { |
|
1242 |
$main::lxdebug->enter_sub(); |
|
1243 |
|
|
1244 |
# Drei Fälle: |
|
1245 |
# $params{oe_id} : Alle Lieferscheine zu diesem Auftrag durchsuchen und pro Auftragsposition die Mengen zurückgeben |
|
1246 |
# Wird zur Darstellung der gelieferten Mengen im Auftrag benötigt |
|
1247 |
# $params{do_id} : Alle Aufträge zu diesem Lieferschein durchsuchen und pro Lieferscheinposition die Mengen zurückgeben |
|
1248 |
# Wird für LaTeX benötigt um im Lieferschein pro Position die Mengen auszugeben |
|
1249 |
# $params{delivered}: Alle Aufträge zum Lieferschein $params{do_id} prüfen ob sie vollständiger ausgeliefert sind |
|
1250 |
# Wird für das Setzen des 'delivered' Flag in der Datenbank beim "save" des Lieferscheins benötigt |
|
1251 |
|
|
1252 |
my $self = shift; |
|
1253 |
my %params = @_; |
|
1254 |
|
|
1255 |
# Eigentlich unkritisch: wenn keine der Parameter gesetzt ist kommt ein leerer Hash zurück |
|
1256 |
croak ("Need at least one parameter oe_id or do_id") unless $params{oe_id} || $params{do_id}; |
|
1257 |
|
|
1258 |
my $myconfig = \%main::myconfig; |
|
1259 |
my $form = $main::form; |
|
1260 |
my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig); |
|
1261 |
my %ship = (); |
|
1262 |
|
|
1263 |
my @oe_ids; |
|
1264 |
|
|
1265 |
if ($params{oe_id} ) { |
|
1266 |
push @oe_ids, $params{oe_id}; |
|
1267 |
} elsif ($params{do_id}) { |
|
1268 |
my @links = RecordLinks->get_links( 'dbh' => $dbh, |
|
1269 |
'from_table' => 'oe', |
|
1270 |
'to_table' => 'delivery_orders', |
|
1271 |
'to_id' => $params{do_id}); |
|
1272 |
|
|
1273 |
@oe_ids = map { $_->{from_id} } @links; |
|
1274 |
} |
|
1275 |
|
|
1276 |
if (scalar (@oe_ids) > 0 ) { |
|
1277 |
my $all_units = AM->retrieve_all_units(); |
|
1278 |
my $placeholders = join(', ', ('?') x scalar @oe_ids); |
|
1279 |
my $query = qq|SELECT oi.id, oi.position, oi.parts_id, oi.qty, oi.unit, oi.trans_id, |
|
1280 |
p.unit AS partunit FROM orderitems oi |
|
1281 |
LEFT JOIN parts p ON (oi.parts_id = p.id) |
|
1282 |
WHERE trans_id IN (${placeholders}) |
|
1283 |
ORDER BY position ASC|; |
|
1284 |
|
|
1285 |
my $orderitems = selectall_hashref_query($form, $dbh, $query, @oe_ids); |
|
1286 |
foreach my $oe_entry (@{ $orderitems }) { |
|
1287 |
$oe_entry->{qty} *= AM->convert_unit($oe_entry->{unit}, $oe_entry->{partunit}, $all_units); |
|
1288 |
$oe_entry->{qty_notdelivered} = $oe_entry->{qty}; |
|
1289 |
|
|
1290 |
# Bei oe Modus auf jeden Fall einen Record anlegen |
|
1291 |
if ( $params{oe_id} ) { |
|
1292 |
$ship{$oe_entry->{position}} = { |
|
1293 |
'qty_ordered' => $oe_entry->{qty} , |
|
1294 |
'qty_notdelivered' => $oe_entry->{qty} |
|
1295 |
}; |
|
1296 |
} |
|
1297 |
} |
|
1298 |
|
|
1299 |
my @dolinks = RecordLinks->get_links('dbh' => $dbh, |
|
1300 |
'from_table' => 'oe', |
|
1301 |
'to_table' => 'delivery_orders', |
|
1302 |
'from_id' => \@oe_ids); |
|
1303 |
|
|
1304 |
my @do_ids = map { $_->{to_id} } @dolinks ; |
|
1305 |
if (scalar (@do_ids) == 0) { |
|
1306 |
$main::lxdebug->leave_sub(); |
|
1307 |
return %ship; |
|
1308 |
} |
|
1309 |
|
|
1310 |
my %oeitems_by_id = map { $_->{id} => $_ } @{ $orderitems }; |
|
1311 |
|
|
1312 |
|
|
1313 |
$placeholders = join(', ', ('?') x scalar @do_ids); |
|
1314 |
$query = qq|SELECT doi.parts_id, doi.id, doi.qty, doi.unit, doi.position, |
|
1315 |
doi.delivery_order_id, COALESCE(rlitem.from_id,0) as from_id, |
|
1316 |
p.unit AS partunit |
|
1317 |
FROM delivery_order_items doi |
|
1318 |
LEFT JOIN parts p ON (doi.parts_id = p.id) |
|
1319 |
LEFT JOIN record_links rlitem |
|
1320 |
ON (rlitem.to_id = doi.id AND rlitem.to_table='delivery_order_items') |
|
1321 |
WHERE doi.delivery_order_id IN (${placeholders})|; |
|
1322 |
|
|
1323 |
my $deliveryorderitems = selectall_hashref_query($form, $dbh, $query, @do_ids); |
|
1324 |
|
|
1325 |
# erst mal qty der links bearbeiten |
|
1326 |
foreach my $do_entry (@{ $deliveryorderitems }) { |
|
1327 |
$do_entry->{qty} *= AM->convert_unit($do_entry->{unit}, $do_entry->{partunit}, $all_units); |
|
1328 |
if ($do_entry->{from_id} > 0 ) { |
|
1329 |
# record link zwischen items vorhanden, kann auch von anderem Auftrag sein |
|
1330 |
my $oe_entry = $oeitems_by_id{$do_entry->{from_id}}; |
|
1331 |
if ( $oe_entry ) { |
|
1332 |
$oe_entry->{qty_notdelivered} -= $do_entry->{qty}; |
|
1333 |
# derzeit nur ein link pro do_item |
|
1334 |
$do_entry->{oe_entry} = $oe_entry; |
|
1335 |
} |
|
1336 |
} else { |
|
1337 |
$main::lxdebug->message(LXDebug->DEBUG2(),"no entry for=".$do_entry->{id}." part=".$do_entry->{parts_id}); |
|
1338 |
} |
|
1339 |
} |
|
1340 |
# nun den rest ohne links bearbeiten |
|
1341 |
foreach my $do_entry (@{ $deliveryorderitems }) { |
|
1342 |
next if $do_entry->{from_id} > 0; |
|
1343 |
next if $do_entry->{qty} == 0; |
|
1344 |
|
|
1345 |
foreach my $oe_entry (@{ $orderitems }) { |
|
1346 |
next if $oe_entry->{qty_notdelivered} == 0; |
|
1347 |
if ( $do_entry->{parts_id} == $oe_entry->{parts_id} ) { |
|
1348 |
# zu viele geliefert auf andere position ? |
|
1349 |
if ( $oe_entry->{qty_notdelivered} < 0 ) { |
|
1350 |
$do_entry->{qty} += - $oe_entry->{qty_notdelivered}; |
|
1351 |
$oe_entry->{qty_notdelivered} = 0; |
|
1352 |
} else { |
|
1353 |
if ( $do_entry->{qty} < $oe_entry->{qty_notdelivered} ) { |
|
1354 |
$oe_entry->{qty_notdelivered} -= $do_entry->{qty}; |
|
1355 |
$do_entry->{qty} = 0; |
|
1356 |
} else { |
|
1357 |
$do_entry->{qty} -= $oe_entry->{qty_notdelivered}; |
|
1358 |
$oe_entry->{qty_notdelivered} = 0; |
|
1359 |
} |
|
1360 |
# derzeit nur ein link pro do_item |
|
1361 |
$do_entry->{oe_entry} = $oe_entry if !$do_entry->{oe_entry}; |
|
1362 |
} |
|
1363 |
} |
|
1364 |
last if $do_entry->{qty} <= 0; |
|
1365 |
} |
|
1366 |
} |
|
1367 |
if ( $params{oe_id} ) { |
|
1368 |
$ship{$_->{position}}->{qty_notdelivered} = $_->{qty_notdelivered} for @{ $orderitems }; |
|
1369 |
} |
|
1370 |
elsif ($params{do_id} && $params{delivered}) { |
|
1371 |
foreach my $oe_entry (@{ $orderitems }) { |
|
1372 |
if ( !$ship{$oe_entry->{trans_id}} ) { |
|
1373 |
$ship{$oe_entry->{trans_id}} = { 'delivered' => 1 }; |
|
1374 |
} |
|
1375 |
$ship{$oe_entry->{trans_id}}->{delivered} = 0 if $oe_entry->{qty_notdelivered} > 0; |
|
1376 |
} |
|
1377 |
} |
|
1378 |
elsif ($params{do_id}) { |
|
1379 |
foreach my $do_entry (@{ $deliveryorderitems }) { |
|
1380 |
next if $params{do_id} != $do_entry->{delivery_order_id}; |
|
1381 |
my $position = $do_entry->{position}; |
|
1382 |
if ( $position > 0 && $do_entry->{oe_entry}) { |
|
1383 |
if ( !$ship{$position} ) { |
|
1384 |
$ship{$position} = { |
|
1385 |
'qty_ordered' => $do_entry->{oe_entry}->{qty} , |
|
1386 |
'qty_notdelivered' => $do_entry->{oe_entry}->{qty_notdelivered} |
|
1387 |
}; |
|
1388 |
} else { |
|
1389 |
$ship{$position}->{qty_ordered} += $do_entry->{oe_entry}->{qty}; |
|
1390 |
$ship{$position}->{qty_notdelivered} += $do_entry->{oe_entry}->{qty_notdelivered}; |
|
1391 |
} |
|
1392 |
} |
|
1393 |
} |
|
1394 |
} |
|
1395 |
} |
|
1396 |
$main::lxdebug->leave_sub(); |
|
1397 |
return %ship; |
|
1398 |
} |
|
1399 |
|
|
1400 | 1240 |
sub is_marked_as_delivered { |
1401 | 1241 |
$main::lxdebug->enter_sub(); |
1402 | 1242 |
|
Auch abrufbar als: Unified diff
DO: get_shipped_qty entfernt