|
|
发表于 2013/3/22 11:33:50
|
显示全部楼层
372 public String completeIt()
373 {
374 // Re-Check
375 if (!m_justPrepared)
376 {
377 String status = prepareIt();
378 if (!DocAction.STATUS_InProgress.equals(status))
379 return status;
380 }
381
382 m_processMsg = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_BEFORE_COMPLETE);
383 if (m_processMsg != null)
384 return DocAction.STATUS_Invalid;
385
386 // Implicit Approval
387 if (!isApproved())
388 approveIt();
389 log.info(toString());
390
391 MInventoryLine[] lines = getLines(false);
392 for (MInventoryLine line : lines)
393 {
394 if (!line.isActive())
395 continue;
396
397 MProduct product = line.getProduct();
398
399 //Get Quantity Internal Use
400 BigDecimal qtyDiff = line.getQtyInternalUse().negate();
401 //If Quantity Internal Use = Zero Then Physical Inventory Else Internal Use Inventory
402 if (qtyDiff.signum() == 0)
403 {
404 qtyDiff = line.getQtyCount().subtract(line.getQtyBook());
405 //If Quantity Count minus Quantity Book = Zero, then no change in Inventory
406 if (qtyDiff.signum() == 0)
407 continue;
408 }
409
410 //Ignore the Material Policy when is Reverse Correction
411 if(!isReversal())
412 checkMaterialPolicy(line, qtyDiff);
413
414 // Stock Movement - Counterpart MOrder.reserveStock
415 if (product != null
416 && product.isStocked() )
417 {
418 log.fine("Material Transaction");
419 MTransaction mtrx = null;
420
421 //If AttributeSetInstance = Zero then create new AttributeSetInstance use Inventory Line MA else use current AttributeSetInstance
422 if (line.getM_AttributeSetInstance_ID() == 0 || qtyDiff.compareTo(Env.ZERO) == 0)
423 {
424 MInventoryLineMA mas[] = MInventoryLineMA.get(getCtx(),
425 line.getM_InventoryLine_ID(), get_TrxName());
426
427 for (int j = 0; j < mas.length; j++)
428 {
429 MInventoryLineMA ma = mas[j];
430 BigDecimal QtyMA = ma.getMovementQty();
431 BigDecimal QtyNew = QtyMA.add(qtyDiff);
432 log.fine("Diff=" + qtyDiff
433 + " - Instance OnHand=" + QtyMA + "->" + QtyNew);
434
435 if (!MStorage.add(getCtx(), getM_Warehouse_ID(),
436 line.getM_Locator_ID(),
437 line.getM_Product_ID(),
438 ma.getM_AttributeSetInstance_ID(), 0,
439 QtyMA.negate(), Env.ZERO, Env.ZERO, get_TrxName()))
440 {
441 m_processMsg = "Cannot correct Inventory (MA)";
442 return DocAction.STATUS_Invalid;
443 }
444
445 // Only Update Date Last Inventory if is a Physical Inventory
446 if(line.getQtyInternalUse().compareTo(Env.ZERO) == 0)
447 {
448 MStorage storage = MStorage.get(getCtx(), line.getM_Locator_ID(),
449 line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(), get_TrxName());
450 storage.setDateLastInventory(getMovementDate());
451 if (!storage.save(get_TrxName()))
452 {
453 m_processMsg = "Storage not updated(2)";
454 return DocAction.STATUS_Invalid;
455 }
456 }
457
458 String m_MovementType =null;
459 if(QtyMA.negate().compareTo(Env.ZERO) > 0 )
460 m_MovementType = MTransaction.MOVEMENTTYPE_InventoryIn;
461 else
462 m_MovementType = MTransaction.MOVEMENTTYPE_InventoryOut;
463 // Transaction
464 mtrx = new MTransaction (getCtx(), line.getAD_Org_ID(), m_MovementType,
465 line.getM_Locator_ID(), line.getM_Product_ID(), ma.getM_AttributeSetInstance_ID(),
466 QtyMA.negate(), getMovementDate(), get_TrxName());
467
468 mtrx.setM_InventoryLine_ID(line.getM_InventoryLine_ID());
469 if (!mtrx.save())
470 {
471 m_processMsg = "Transaction not inserted(2)";
472 return DocAction.STATUS_Invalid;
473 }
474 if(QtyMA.signum() != 0)
475 {
476 String err = createCostDetail(line, ma.getM_AttributeSetInstance_ID() , QtyMA.negate());
477 if (err != null && err.length() > 0) {
478 m_processMsg = err;
479 return DocAction.STATUS_Invalid;
480 }
481 }
482
483 qtyDiff = QtyNew;
484
485 }
486 }
487
488 //sLine.getM_AttributeSetInstance_ID() != 0
489 // Fallback
490 if (mtrx == null)
491 {
492 //Fallback: Update Storage - see also VMatch.createMatchRecord
493 if (!MStorage.add(getCtx(), getM_Warehouse_ID(),
494 line.getM_Locator_ID(),
495 line.getM_Product_ID(),
496 line.getM_AttributeSetInstance_ID(), 0,
497 qtyDiff, Env.ZERO, Env.ZERO, get_TrxName()))
498 {
499 m_processMsg = "Cannot correct Inventory (MA)";
500 return DocAction.STATUS_Invalid;
501 }
502
503 // Only Update Date Last Inventory if is a Physical Inventory
504 if(line.getQtyInternalUse().compareTo(Env.ZERO) == 0)
505 {
506 MStorage storage = MStorage.get(getCtx(), line.getM_Locator_ID(),
507 line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(), get_TrxName());
508
509 storage.setDateLastInventory(getMovementDate());
510 if (!storage.save(get_TrxName()))
511 {
512 m_processMsg = "Storage not updated(2)";
513 return DocAction.STATUS_Invalid;
514 }
515 }
516
517 String m_MovementType = null;
518 if(qtyDiff.compareTo(Env.ZERO) > 0 )
519 m_MovementType = MTransaction.MOVEMENTTYPE_InventoryIn;
520 else
521 m_MovementType = MTransaction.MOVEMENTTYPE_InventoryOut;
522 // Transaction
523 mtrx = new MTransaction (getCtx(), line.getAD_Org_ID(), m_MovementType,
524 line.getM_Locator_ID(), line.getM_Product_ID(), line.getM_AttributeSetInstance_ID(),
525 qtyDiff, getMovementDate(), get_TrxName());
526 mtrx.setM_InventoryLine_ID(line.getM_InventoryLine_ID());
527 if (!mtrx.save())
528 {
529 m_processMsg = "Transaction not inserted(2)";
530 return DocAction.STATUS_Invalid;
531 }
532
533 if(qtyDiff.signum() != 0)
534 {
535 String err = createCostDetail(line, line.getM_AttributeSetInstance_ID(), qtyDiff);
536 if (err != null && err.length() > 0) {
537 m_processMsg = err;
538 return DocAction.STATUS_Invalid;
539 }
540 }
541 } // Fallback
542 } // stock movement
543
544 } // for all lines
545
546 // User Validation
547 String valid = ModelValidationEngine.get().fireDocValidate(this, ModelValidator.TIMING_AFTER_COMPLETE);
548 if (valid != null)
549 {
550 m_processMsg = valid;
551 return DocAction.STATUS_Invalid;
552 }
553
554 // Set the definite document number after completed (if needed)
555 setDefiniteDocumentNo();
556
557 //
558 setProcessed(true);
559 setDocAction(DOCACTION_Close);
560 return DocAction.STATUS_Completed;
561 } // completeIt
562 |
|