use of org.compiere.model.PO in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method createLabelPORecord.
private static final void createLabelPORecord(@NonNull final Object listValueObj, final int linkId, @NonNull final LabelsLookup lookup) {
final String listValue = listValueObj.toString();
final PO labelPO = TableModelLoader.instance.newPO(lookup.getLabelsTableName());
labelPO.set_ValueNoCheck(lookup.getLabelsLinkColumnName(), linkId);
labelPO.set_ValueNoCheck(lookup.getLabelsValueColumnName(), listValue);
InterfaceWrapperHelper.save(labelPO);
}
use of org.compiere.model.PO in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method save.
@Override
public void save(final Document document) {
Services.get(ITrxManager.class).assertThreadInheritedTrxExists();
assertThisRepository(document.getEntityDescriptor());
DocumentPermissionsHelper.assertCanEdit(document);
// Runnables to be executed after the PO is saved
final List<Runnable> afterSaveRunnables = new ArrayList<>();
//
// Load the PO / Create new PO instance
final PO po = retrieveOrCreatePO(document);
//
// Set values to PO
final boolean isNew = document.isNew();
boolean changes = false;
for (final IDocumentFieldView documentField : document.getFieldViews()) {
if (!isNew && !documentField.hasChangesToSave()) {
logger.trace("Skip setting PO value because document field has no changes: {}", documentField);
continue;
}
if (DocumentFieldWidgetType.Labels == documentField.getWidgetType()) {
// save labels after PO is saved because we want to make sure it's not new (so we can link to it)
afterSaveRunnables.add(() -> saveLabels(document, documentField));
}
if (setPOValue(po, documentField)) {
changes = true;
}
}
//
// Save the PO
boolean needsRefresh = false;
if (changes) {
//
// Actual save
// TODO: advice the PO to not reload after save.
InterfaceWrapperHelper.save(po);
document.markAsNotNew();
needsRefresh = true;
} else {
logger.trace("Skip saving {} because there was no actual change", po);
}
// Execute after save runnables
if (!afterSaveRunnables.isEmpty()) {
afterSaveRunnables.forEach(r -> r.run());
needsRefresh = true;
}
// Reload the document
if (needsRefresh) {
final SqlDocumentEntityDataBindingDescriptor dataBinding = document.getEntityDescriptor().getDataBinding(SqlDocumentEntityDataBindingDescriptor.class);
final DocumentId idNew = extractDocumentId(po, dataBinding);
refresh(document, idNew);
}
// Notify the parent document that one of it's children were saved
if (!document.isRootDocument()) {
document.getParentDocument().onChildSaved(document);
}
}
use of org.compiere.model.PO in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method delete.
@Override
public void delete(final Document document) {
Services.get(ITrxManager.class).assertThreadInheritedTrxExists();
assertThisRepository(document.getEntityDescriptor());
DocumentPermissionsHelper.assertCanEdit(document, UserSession.getCurrentPermissions());
if (document.isNew()) {
throw new IllegalArgumentException("Cannot delete new document: " + document);
}
final PO po = retrieveOrCreatePO(document);
InterfaceWrapperHelper.delete(po);
}
use of org.compiere.model.PO in project metasfresh-webui-api by metasfresh.
the class DocumentCollection method duplicateDocumentInTrx.
private DocumentPath duplicateDocumentInTrx(final DocumentPath fromDocumentPath) {
// NOTE: assume it's already running in transaction
final TableRecordReference fromRecordRef = getTableRecordReference(fromDocumentPath);
final Object fromModel = fromRecordRef.getModel(PlainContextAware.newWithThreadInheritedTrx());
final String tableName = InterfaceWrapperHelper.getModelTableName(fromModel);
final PO fromPO = InterfaceWrapperHelper.getPO(fromModel);
final PO toPO = TableModelLoader.instance.newPO(Env.getCtx(), tableName, ITrx.TRXNAME_ThreadInherited);
// set "getValueToCopy" advisor
toPO.setDynAttribute(PO.DYNATTR_CopyRecordSupport, CopyRecordFactory.getCopyRecordSupport(tableName));
PO.copyValues(fromPO, toPO, true);
InterfaceWrapperHelper.save(toPO);
final CopyRecordSupport childCRS = CopyRecordFactory.getCopyRecordSupport(tableName);
childCRS.setAD_Window_ID(fromDocumentPath.getAD_Window_ID(-1));
childCRS.setParentPO(toPO);
childCRS.setBase(true);
childCRS.copyRecord(fromPO, ITrx.TRXNAME_ThreadInherited);
final DocumentPath toDocumentPath = DocumentPath.rootDocumentPath(fromDocumentPath.getWindowId(), DocumentId.of(toPO.get_ID()));
return toDocumentPath;
}
use of org.compiere.model.PO in project lar_361 by comitsrl.
the class LAR_Validator method changeVoidDocumentNo.
/**
* Change DocumentNo in order to avoid modified sequence for given document
*/
// TODO - Improve and add this behavior to ADempiere and make it configurable (ideal)
private String changeVoidDocumentNo(final PO po) {
final Properties ctx = po.getCtx();
PO revPo = null;
MSequence seq = null;
// Corrije el nro de documento de la factura anulada y su reversa asociada
if (po.get_TableName().equals(MInvoice.Table_Name)) {
final MInvoice invoice = (MInvoice) po;
// Si no se tiene la referencia a la reversión, no se procesa
if (invoice.getReversal_ID() == 0)
return null;
revPo = new MInvoice(ctx, invoice.getReversal_ID(), invoice.get_TrxName());
final MInvoice revInvoice = (MInvoice) revPo;
log.info("Change DocumentNo of " + revInvoice);
// Intenta recuperar la secuencia "definitiva". Si no tiene, intenta
// recupera la secuencia "normal". Si no tiene, no sea hace nada debido
// a que le documento NO tiene secuencia configurada
int ad_Sequence_ID = invoice.getC_DocType().getDefiniteSequence_ID();
if (ad_Sequence_ID == 0)
ad_Sequence_ID = invoice.getC_DocType().getDocNoSequence_ID();
if (ad_Sequence_ID != 0)
seq = new MSequence(ctx, ad_Sequence_ID, invoice.get_TrxName());
// Redefine los nros de documento y las descripciones de las facturas
String revDocumentNo = "Rev-" + invoice.getDocumentNo() + "-" + invoice.getC_Invoice_ID();
String voidDocumentNo = "Anu-" + invoice.getDocumentNo() + "-" + invoice.getC_Invoice_ID();
revInvoice.setDocumentNo(revDocumentNo);
revInvoice.setDescription("(" + voidDocumentNo + "<-)");
invoice.setDocumentNo(voidDocumentNo);
invoice.setDescription("(" + revDocumentNo + "<-)");
// Si la secuencia es automática, retrocede la numeración
if (seq != null && seq.isAutoSequence())
seq.setCurrentNext(seq.getCurrentNext() - 1);
}
// Corrije el nro de documento del remito anulado y su reverso asociado
if (po.get_TableName().equals(MInOut.Table_Name)) {
final MInOut shipment = (MInOut) po;
// Si no se tiene la referencia a la reversión, no se procesa
if (shipment.getReversal_ID() == 0)
return null;
revPo = new MInOut(ctx, shipment.getReversal_ID(), shipment.get_TrxName());
final MInOut revShipment = (MInOut) revPo;
log.info("Change DocumentNo of " + revShipment);
// Intenta recuperar la secuencia "definitiva". En caso que sea nula,
// recupera la secuencia "normal"
int AD_Sequence_ID = shipment.getC_DocType().getDefiniteSequence_ID();
if (AD_Sequence_ID == 0)
AD_Sequence_ID = shipment.getC_DocType().getDocNoSequence_ID();
if (AD_Sequence_ID != 0)
seq = new MSequence(ctx, AD_Sequence_ID, shipment.get_TrxName());
String revDocumentNo = "Rev-" + shipment.getDocumentNo() + "-" + shipment.getM_InOut_ID();
String voidDocumentNo = "Anu-" + shipment.getDocumentNo() + "-" + shipment.getM_InOut_ID();
revShipment.setDocumentNo(revDocumentNo);
revShipment.setDescription("(" + voidDocumentNo + "<-)");
shipment.setDocumentNo(voidDocumentNo);
shipment.setDescription("(" + revDocumentNo + "<-)");
// Si encontró una secuencia, y la misma es automática, retrocede la numeración
if (seq != null && seq.isAutoSequence())
seq.setCurrentNext(seq.getCurrentNext() - 1);
}
// (siempre y cuando hayan cambiado y no sean nulos)
if (revPo != null && !revPo.save())
return "Error al guardar el documento inverso";
if (po.is_Changed() && !po.save())
return "Error al guardar el documento anulado";
if (seq != null && seq.is_Changed() && !seq.save())
return "Error al guardar la secuencia";
return null;
}
Aggregations