public void CreateNewDetails(ObjVerEx objVer, List <ObjVerEx> objPOs)
        {
            List <ObjVerEx> objInvoices = FindObjects(objVer.Vault, InvoiceDetail_CD, Invoice_PD, MFDataType.MFDatatypeLookup, objVer.ID.ToString());
            var             nextNo      = objInvoices.Count;

            foreach (var objPO in objPOs)
            {
                var propertyValues = new PropertyValues();

                //set class
                var classPropertyValue = new PropertyValue()
                {
                    PropertyDef = (int)MFBuiltInPropertyDef.MFBuiltInPropertyDefClass
                };

                classPropertyValue.Value.SetValue(MFDataType.MFDatatypeLookup, objVer.Vault.ClassOperations.GetObjectClass(InvoiceDetail_CD).ID);
                propertyValues.Add(-1, classPropertyValue);

                // set Name or Title
                var TitleProperties          = objVer.Vault.ObjectPropertyOperations.GetProperties(objVer.ObjVer, true);
                var propTitle                = TitleProperties.SearchForProperty((int)MFBuiltInPropertyDef.MFBuiltInPropertyDefNameOrTitle);
                var nameOrTitlePropertyValue = new PropertyValue()
                {
                    PropertyDef = (int)MFBuiltInPropertyDef.MFBuiltInPropertyDefNameOrTitle
                };
                var DisplayValue = propTitle.TypedValue.DisplayValue + " - Line " + (++nextNo).ToString();
                nameOrTitlePropertyValue.Value.SetValue(propTitle.TypedValue.DataType, DisplayValue);
                propertyValues.Add(-1, nameOrTitlePropertyValue);

                // set Invoice
                var NewInvoiceLookup = new Lookup()
                {
                    ObjectType   = objVer.ObjVer.Type,
                    Item         = objVer.ObjVer.ID,
                    DisplayValue = TitleProperties.SearchForProperty(InvoiceName_PD).TypedValue.DisplayValue
                };
                var newInvoice = new PropertyValue()
                {
                    PropertyDef = Invoice_PD.ID      //1058
                };
                newInvoice.Value.SetValue(MFDataType.MFDatatypeLookup, NewInvoiceLookup);
                propertyValues.Add(-1, newInvoice);

                // set PODetail
                var NewPOLookup = new Lookup()
                {
                    ObjectType   = objPO.ObjVer.Type,
                    Item         = objPO.ObjVer.ID,
                    DisplayValue = objPO.Title
                };
                var newPO = new PropertyValue()
                {
                    PropertyDef = PurchaseOrderDetail_PD.ID      //1177
                };
                newPO.Value.SetValue(MFDataType.MFDatatypeLookup, NewPOLookup);
                //propertyValues.Add(-1, newPO);

                PropertyValues PO = objVer.Vault.ObjectPropertyOperations.GetProperties(objPO.ObjVer);
                propertyValues.Add(-1, GetPropertyValue(objVer.Vault, PO, POLine_PD, InvoiceLineNumber_PD));
                propertyValues.Add(-1, GetPropertyValue(objVer.Vault, PO, POItem_PD, ItemNumber_PD));
                //propertyValues.Add(-1, GetPropertyValue(objVer.Vault, PO, OrderedQty_PD, Quantity_PD));
                propertyValues.Add(-1, GetPropertyValue(objVer.Vault, PO, UnitPrice_PD, UnitPrice_PD));
                propertyValues.Add(-1, GetPropertyValue(objVer.Vault, PO, GLAccount_PD, GLAccount_PD));

                ObjectVersionAndProperties ppts = objVer.Vault.ObjectOperations.CreateNewObject(InvoiceDetail_OT, propertyValues);

                objVer.Vault.ObjectOperations.CheckIn(ppts.ObjVer);
            }
        }
        public void CreateChequeTest(StateEnvironment env)
        {
            var Vault        = env.ObjVerEx.Vault;
            var oCurrObjVals = Vault.ObjectPropertyOperations.GetProperties(env.ObjVerEx.ObjVer);
            var VendorID     = SearchPropertyValue(oCurrObjVals, Vendor_PD);

            List <InvoiceValue> InvoiceValues = new List <InvoiceValue>();

            InvoiceValues.Add(new InvoiceValue()
            {
                PropertyID = Date_PD, TypedValue = SearchPropertyValue(oCurrObjVals, InvoiceDate_PD)
            });
            InvoiceValues.Add(new InvoiceValue()
            {
                PropertyID = InvoiceNumber_PD, TypedValue = SearchPropertyValue(oCurrObjVals, InvoiceNumber_PD)
            });
            InvoiceValues.Add(new InvoiceValue()
            {
                PropertyID = Vendor_PD, TypedValue = VendorID
            });
            List <ObjVerEx> InvoiceObjVers = SearchForObjects(env, Invoice_CD, InvoiceValues);

            if (InvoiceObjVers != null)
            {
                InvoiceValues = new List <InvoiceValue>();
                InvoiceValues.Add(new InvoiceValue()
                {
                    PropertyID = Vendor_PD, TypedValue = VendorID
                });
                InvoiceValues.Add(new InvoiceValue()
                {
                    PropertyID = Date_PD, TypedValue = SearchPropertyValue(oCurrObjVals, ChequeDate_PD)
                });
                InvoiceValues.Add(new InvoiceValue()
                {
                    PropertyID = ChequeNumber_PD, TypedValue = SearchPropertyValue(oCurrObjVals, ChequeNumber_PD)
                });
                InvoiceValues.Add(new InvoiceValue()
                {
                    PropertyID = Amount_PD, TypedValue = SearchPropertyValue(oCurrObjVals, ChequeAmount_PD)
                });
                List <ObjVerEx> ChequeObjVers = SearchForObjects(env, Cheque_CD, InvoiceValues);

                ObjVer oCheque;
                var    propertyValues = new PropertyValues();

                if (ChequeObjVers != null)
                {
                    oCheque = Vault.ObjectOperations.CheckOut(ChequeObjVers[0].ObjID).ObjVer;
                }
                else
                {
                    var classPropertyValue = new PropertyValue()
                    {
                        PropertyDef = (int)MFBuiltInPropertyDef.MFBuiltInPropertyDefClass
                    };
                    classPropertyValue.Value.SetValue(MFDataType.MFDatatypeLookup, Vault.ClassOperations.GetObjectClass(Cheque_CD).ID);
                    propertyValues.Add(-1, classPropertyValue);

                    propertyValues.Add(-1, GetPropertyValue(oCurrObjVals, ChequeNumber_PD));
                    propertyValues.Add(-1, GetPropertyValue(oCurrObjVals, Date_PD, ChequeDate_PD));
                    propertyValues.Add(-1, GetPropertyValue(oCurrObjVals, Amount_PD, ChequeAmount_PD));
                    propertyValues.Add(-1, GetPropertyValue(oCurrObjVals, Vendor_PD));

                    ObjectVersionAndProperties ppts = Vault.ObjectOperations.CreateNewObject(Cheque_OT, propertyValues);
                    oCheque = ppts.ObjVer;
                }

                var     ChequeProps  = Vault.ObjectPropertyOperations.GetProperties(oCheque);
                Lookups PaidInvoices = ChequeProps.SearchForProperty(PaidInvoices_PD).TypedValue.GetValueAsLookups();
                bool    FoundInvoice = false;

                foreach (Lookup PaidInvoicesItem in PaidInvoices)
                {
                    if (PaidInvoicesItem.Item == InvoiceObjVers[0].ObjVer.ID)
                    {
                        FoundInvoice = true;
                        break;
                    }
                }

                if (!FoundInvoice)
                {
                    var NewInvoice = new Lookup();

                    NewInvoice.ObjectType   = InvoiceObjVers[0].ObjVer.Type;
                    NewInvoice.Item         = InvoiceObjVers[0].ObjVer.ID;
                    NewInvoice.DisplayValue = InvoiceObjVers[0].Title;
                    PaidInvoices.Add(-1, NewInvoice);

                    var PaidIvc = new PropertyValue()
                    {
                        PropertyDef = PaidInvoices_PD   //oCurrObjVals.SearchForProperty(PaidInvoices_PD).PropertyDef
                    };
                    PaidIvc.Value.SetValueToMultiSelectLookup(PaidInvoices);
                    Vault.ObjectPropertyOperations.SetProperty(oCheque, PaidIvc);
                }
                env.ObjVerEx.Vault.ObjectOperations.CheckIn(oCheque);
            }
            env.ObjVerEx.SetWorkflowState(SagePaymentWorkFlow, Processed_State);
            env.ObjVerEx.SaveProperties();
        }
        public static List <T> search_property_definition <T, T2>(this IObjVerEx obj, int propertyDefinitionID, T2 value, int propertyDefinition2ID, IObjVerEx value2, int propertyDefinition3ID, IObjVerEx value3, bool includeDeleted = false)
            where T : IObjVerEx, new()
        {
            SearchCondition  searchCondition;
            SearchConditions searchConditions = new SearchConditions();

            Type   t = typeof(T);
            object o = Activator.CreateInstance <T>();

            FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
            int         oid    = -1;

            foreach (FieldInfo f in fields)
            {
                if (f.Name == "TypeID")
                {
                    oid = Convert.ToInt32(f.GetValue(o));
                }
            }

            searchCondition = new SearchCondition();
            searchCondition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeObjectTypeID);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeLookup, oid);
            searchConditions.Add(-1, searchCondition);

            searchCondition = new SearchCondition();
            searchCondition.Expression.SetPropertyValueExpression(propertyDefinitionID, MFParentChildBehavior.MFParentChildBehaviorNone);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeText, value);
            searchConditions.Add(-1, searchCondition);

            searchCondition = new SearchCondition();
            searchCondition.Expression.SetPropertyValueExpression(propertyDefinition2ID, MFParentChildBehavior.MFParentChildBehaviorNone);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeLookup, value2.objVerEx.ToLookup());
            searchConditions.Add(-1, searchCondition);

            searchCondition = new SearchCondition();
            searchCondition.Expression.SetPropertyValueExpression(propertyDefinition3ID, MFParentChildBehavior.MFParentChildBehaviorNone);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeLookup, value3.objVerEx.ToLookup());
            searchConditions.Add(-1, searchCondition);

            if (!includeDeleted)
            {
                searchCondition = new SearchCondition();
                searchCondition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeDeleted);
                searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
                searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeBoolean, false);
                searchConditions.Add(-1, searchCondition);
            }

            ObjectSearchResults objectSearchResults =
                obj.objVerEx.Vault.ObjectSearchOperations.SearchForObjectsByConditionsEx(searchConditions, MFSearchFlags.MFSearchFlagNone, false);

            List <T> searchedOT = new List <T>();

            if (objectSearchResults != null && objectSearchResults.Count > 0)
            {
                IEnumerator   searchResultObjVerEnum = objectSearchResults.GetAsObjectVersions().GetEnumerator();
                ObjectVersion objectVersion          = null;
                while (searchResultObjVerEnum.MoveNext())
                {
                    objectVersion = (ObjectVersion)searchResultObjVerEnum.Current;
                    ObjectVersionAndProperties objVerAndProps =
                        obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(objectVersion.ObjVer.ObjID, true);
                    T item = new T()
                    {
                        objVerEx = new ObjVerEx(obj.objVerEx.Vault, objVerAndProps)
                    };
                    searchedOT.Add(item);
                }
            }
            return(searchedOT);
        }
        public static bool update(this IObjVerEx obj, bool returnUpdatedMetadata = true, bool skipVafCalculation = false, int?modifiedby_userid = null, string[] replace_files = null)
        {
            if (obj == null)
            {
                return(false);
            }
            // Check if the objcet is deleted or destroyed
            if (obj.objVerEx.Deleted() || obj.objVerEx.IsDestroyed)
            {
                return(false);
            }
            ObjectVersionAndProperties checkedInObject =
                obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(obj.objVerEx.ObjID, true);

            if (checkedInObject.VersionData.ObjectCheckedOut)
            {
                return(false);
            }
            //
            bool          checkedOutByVAF = false;
            ObjectVersion objVersion      = null;
            PropertyValue prop_modifiedby = null;

            try {
                prop_modifiedby = obj.objVerEx.GetProperty(PD_Last_modified_by.id);
                objVersion      = obj.objVerEx.Vault.ObjectOperations.CheckOut(obj.objVerEx.ObjID);
                checkedOutByVAF = true;
                //
                PropertyValues propsToUpdate = obj.objVerEx.Properties.Clone();
                if (checkedInObject.Properties.Exists(PD_Comment.id) && obj.objVerEx.Properties.Exists(PD_Comment.id) &&
                    obj.objVerEx.VersionComment == checkedInObject.Properties.GetProperty(PD_Comment.id).GetValueAsUnlocalizedText())
                {
                    propsToUpdate.Remove(propsToUpdate.IndexOf(PD_Comment.id));
                }
                if (skipVafCalculation)
                {
                    string keyword = string.Empty;
                    if (propsToUpdate.TryGetProperty(26, out PropertyValue kw))
                    {
                        keyword = kw.GetValueAsUnlocalizedText();
                    }
                    propsToUpdate.SetProperty(26, MFDataType.MFDatatypeText, $"{keyword}SKIP_VAF_CALCULATIONS");
                }
                ObjectVersionAndProperties objVerAndProps =
                    obj.objVerEx.Vault.ObjectPropertyOperations.SetAllProperties(objVersion.ObjVer, false, propsToUpdate);

                if (prop_modifiedby != null)
                {
                    if (modifiedby_userid != null)
                    {
                        prop_modifiedby.TypedValue.SetValue(MFDataType.MFDatatypeLookup, modifiedby_userid);
                    }
                    objVerAndProps = obj.objVerEx.Vault.ObjectPropertyOperations.SetLastModificationInfoAdmin(objVersion.ObjVer, true, prop_modifiedby.TypedValue, false, null);
                }

                //replace files
                if (replace_files?.Length > 0 && objVersion.ObjVer.Type == (int)MFBuiltInObjectType.MFBuiltInObjectTypeDocument)
                {
                    SourceObjectFiles objFiles = new SourceObjectFiles();
                    foreach (string file_name in replace_files)
                    {
                        FileInfo fi    = new FileInfo(file_name);
                        string   title = fi.Name.Replace(fi.Extension, "");
                        string   ext   = fi.Extension.Replace(".", "");
                        objFiles.AddFile(title, ext, fi.FullName);
                        if (objVersion.SingleFile)
                        {
                            IEnumerator fileEnum = objVersion.Files.GetEnumerator();
                            fileEnum.MoveNext();
                            obj.objVerEx.Vault.ObjectFileOperations.RenameFile(objVersion.ObjVer, (fileEnum.Current as ObjectFile).FileVer, title, ext, false);
                            break;
                        }
                    }
                    ObjVerEx checkedOutVerEx = new ObjVerEx(obj.objVerEx.Vault, objVersion);
                    checkedOutVerEx.ReplaceFiles(objFiles);
                }

                objVersion                  = obj.objVerEx.Vault.ObjectOperations.CheckIn(objVerAndProps.ObjVer);
                checkedOutByVAF             = false;
                obj.objVerEx.ObjVer.Version = objVersion.ObjVer.Version;
                //
                if (returnUpdatedMetadata)
                {
                    objVerAndProps = obj.objVerEx.Vault.ObjectOperations.GetLatestObjectVersionAndProperties(obj.objVerEx.ObjID, false);
                    obj.objVerEx   = new ObjVerEx(obj.objVerEx.Vault, objVerAndProps);
                    obj.ClearAllCachedProperties();
                }
            } catch (Exception ex) {
                throw ex;
            } finally {
                if (objVersion != null && checkedOutByVAF)
                {
                    // Undo checkout and enforces the operation.
                    obj.objVerEx.Vault.ObjectOperations.ForceUndoCheckout(objVersion.ObjVer);
                }
            }

            return(true);
        }
        public static T search <T>(Vault vault, int objTypeId, int objOrExtObjId, bool includeDeleted)
            where T : IObjVerEx, new()
        {
            SearchConditions searchConditions = new SearchConditions();
            SearchCondition  searchCondition  = new SearchCondition();

            // Object type id condition
            searchCondition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeObjectTypeID);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeLookup, objTypeId);
            searchConditions.Add(1, searchCondition);

            // External objects condition
            searchCondition = new SearchCondition();
            searchCondition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeExtID);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeText, objOrExtObjId);
            searchConditions.Add(2, searchCondition);

            // Include deleted objects condition
            searchCondition = new SearchCondition();
            searchCondition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeDeleted);
            searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
            searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeBoolean, includeDeleted);
            searchConditions.Add(3, searchCondition);

            // Search for external objects
            ObjectSearchResults objSearchResults =
                vault.ObjectSearchOperations.SearchForObjectsByConditionsEx(
                    searchConditions, MFSearchFlags.MFSearchFlagNone, false);

            ObjectVersionAndProperties objVerAndProps = null;

            if (objSearchResults != null && objSearchResults.Count > 0)
            {
                IEnumerator   searchResultObjVerEnum = objSearchResults.GetAsObjectVersions().GetEnumerator();
                ObjectVersion objectVersion          = null;
                searchResultObjVerEnum.MoveNext();
                objectVersion  = (ObjectVersion)searchResultObjVerEnum.Current;
                objVerAndProps = vault.ObjectOperations.GetLatestObjectVersionAndProperties(objectVersion.ObjVer.ObjID, false);
                return(new T()
                {
                    objVerEx = new ObjVerEx(vault, objVerAndProps)
                });
            }
            else
            {
                // Non-external objects condition
                searchCondition = new SearchCondition();
                searchCondition.Expression.SetStatusValueExpression(MFStatusType.MFStatusTypeObjectID);
                searchCondition.ConditionType = MFConditionType.MFConditionTypeEqual;
                searchCondition.TypedValue.SetValue(MFDataType.MFDatatypeInteger, objOrExtObjId);
                searchConditions.Add(2, searchCondition);

                // Search for non-external objects
                objSearchResults =
                    vault.ObjectSearchOperations.SearchForObjectsByConditionsEx(
                        searchConditions, MFSearchFlags.MFSearchFlagNone, false);

                if (objSearchResults != null && objSearchResults.Count > 0)
                {
                    IEnumerator   searchResultObjVerEnum = objSearchResults.GetAsObjectVersions().GetEnumerator();
                    ObjectVersion objectVersion          = null;
                    searchResultObjVerEnum.MoveNext();
                    objectVersion  = (ObjectVersion)searchResultObjVerEnum.Current;
                    objVerAndProps = vault.ObjectOperations.GetLatestObjectVersionAndProperties(objectVersion.ObjVer.ObjID, false);
                    return(new T()
                    {
                        objVerEx = new ObjVerEx(vault, objVerAndProps)
                    });
                }
                else
                {
                    return(default(T));
                }
            }
        }