public override SdataTransactionResult Add(PayloadBase payload, List <SyncFeedEntryLink> links)
        {
            string accountUuid = "";
            SdataTransactionResult sdTrResult;
            PhoneNumberPayload     phoneNumberPayload = payload as PhoneNumberPayload;
            bool isFax   = false;;
            bool isPhone = false;


            if (phoneNumberPayload.PhoneNumbertype.type.Equals("general", StringComparison.InvariantCultureIgnoreCase))
            {
                isPhone = true;
            }
            else if (phoneNumberPayload.PhoneNumbertype.type.Equals("fax", StringComparison.InvariantCultureIgnoreCase))
            {
                isFax = true;
            }


            else
            {
                sdTrResult              = new SdataTransactionResult();
                sdTrResult.HttpMessage  = "Only primary phonenumbers with type general and fax where accepted";
                sdTrResult.HttpMethod   = "POST";
                sdTrResult.HttpStatus   = System.Net.HttpStatusCode.Forbidden;
                sdTrResult.ResourceKind = _resourceKind;
                sdTrResult.Uuid         = phoneNumberPayload.SyncUuid;
                return(sdTrResult);
            }

            foreach (SyncFeedEntryLink link in links)
            {
                if ((!String.IsNullOrEmpty(link.PayloadPath)) &&
                    link.PayloadPath.Equals(_tradingAccountUuidPayloadPath,
                                            StringComparison.InvariantCultureIgnoreCase))
                {
                    accountUuid = link.Uuid;
                    break;
                }
            }

            if (String.IsNullOrEmpty(accountUuid))
            {
                sdTrResult              = new SdataTransactionResult();
                sdTrResult.HttpMessage  = "Trading Account UUID was missing";
                sdTrResult.HttpMethod   = "POST";
                sdTrResult.HttpStatus   = System.Net.HttpStatusCode.Forbidden;
                sdTrResult.ResourceKind = _resourceKind;
                sdTrResult.Uuid         = phoneNumberPayload.SyncUuid;
                return(sdTrResult);
            }


            string accountId = GetTradingAccountLocalId(accountUuid);

            if (String.IsNullOrEmpty(accountId))
            {
                sdTrResult              = new SdataTransactionResult();
                sdTrResult.HttpMessage  = String.Format("Trading Account UUID {0} was not linked", accountUuid);
                sdTrResult.HttpMethod   = "POST";
                sdTrResult.HttpStatus   = System.Net.HttpStatusCode.Forbidden;
                sdTrResult.ResourceKind = _resourceKind;
                sdTrResult.Uuid         = phoneNumberPayload.SyncUuid;
                return(sdTrResult);
            }


            Account         account         = new Account();
            Identity        accIdentity     = new Identity(account.EntityName, accountId);
            AccountDocument accountDocument = account.GetDocument(
                accIdentity, _emptyToken, _context.Config) as AccountDocument;

            Document document = null;
            bool     doAdd    = false;

            document = GetTransformedDocument(payload, links);
            if (isPhone)
            {
                document.Id = accountId + Sage.Integration.Northwind.Application.API.Constants.PhoneIdPostfix;
            }
            if (isFax)
            {
                document.Id = accountId + Sage.Integration.Northwind.Application.API.Constants.FaxIdPostfix;
            }
            if (accountDocument.addresses.documents.Count == 0)
            {
                accountDocument.CrmId = GetTradingAccountUuid(document.Id);
                accountDocument.phones.documents.Add(document);
                doAdd = true;
            }
            else
            {
                for (int index = 0; index < accountDocument.phones.documents.Count; index++)
                {
                    if (accountDocument.phones.documents[index].Id.Equals(document.Id, StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (((PhoneDocument)accountDocument.phones.documents[index]).number.IsNull)
                        {
                            doAdd = true;
                            accountDocument.phones.documents[index] = document;
                        }
                        break;
                    }
                }
            }
            if (!doAdd)
            {
                sdTrResult              = new SdataTransactionResult();
                sdTrResult.HttpMessage  = "Trading Account has already this primary phone type";
                sdTrResult.HttpMethod   = "POST";
                sdTrResult.HttpStatus   = System.Net.HttpStatusCode.Forbidden;
                sdTrResult.ResourceKind = _resourceKind;
                sdTrResult.Uuid         = phoneNumberPayload.SyncUuid;
                return(sdTrResult);
            }

            List <TransactionResult> transactionResults = new List <TransactionResult>();

            account.Update(accountDocument, _context.Config, ref transactionResults);
            sdTrResult = Helper.GetSdataTransactionResult(transactionResults,
                                                          _context.OriginEndPoint, SupportedResourceKinds.tradingAccounts);
            if (sdTrResult != null)
            {
                sdTrResult.ResourceKind = _resourceKind;
                sdTrResult.HttpMessage  = "POST";
            }
            return(sdTrResult);
        }
Пример #2
0
        public AccountDocument GetTransformedDocument(TradingAccountPayload payload, List <SyncFeedEntryLink> links)
        {
            AccountDocument    document       = new AccountDocument();
            tradingAccounttype tradingAccount = payload.TradingAccount;

            document.addresses = new AddressDocumentCollection();
            if (tradingAccount.postalAddresses != null && tradingAccount.postalAddresses != null)
            {
                foreach (postalAddresstype postalAddress in tradingAccount.postalAddresses)
                {
                    PostalAddressPayload postalAdressPayload = new PostalAddressPayload();
                    postalAdressPayload.PostalAddresstype = postalAddress;
                    postalAdressPayload.SyncUuid          = StringToGuid(postalAddress.uuid);
                    AddressDocument address = _postalAdressTransformation.GetTransformedDocument(postalAdressPayload, Helper.ReducePayloadPath(links));
                    document.addresses.Add(address);
                }
            }
            bool hasMainAdress = false;

            for (int index = 0; index < document.addresses.documents.Count; index++)
            {
                AddressDocument address = document.addresses.documents[index] as AddressDocument;
                if ((address.primaryaddress.Value != null) && (address.primaryaddress.Value.ToString().Equals("true", StringComparison.InvariantCultureIgnoreCase)))
                {
                    hasMainAdress = true;
                }
            }
            if ((!hasMainAdress) && (document.addresses.documents.Count > 0))
            {
                AddressDocument address = document.addresses.documents[0] as AddressDocument;
                address.primaryaddress.Value = "true";
            }
            if (String.IsNullOrEmpty(payload.LocalID))
            {
                document.CrmId = payload.SyncUuid.ToString();//
                document.Id    = GetLocalId(payload.SyncUuid);
            }
            else
            {
                document.Id = payload.LocalID;
            }
            document.currencyid.Value = tradingAccount.currency;
            document.emails           = new EmailsDocumentCollection();
            if (tradingAccount.emails != null && tradingAccount.emails != null)
            {
                foreach (emailtype email in tradingAccount.emails)
                {
                    EmailPayload emailPayload = new EmailPayload();
                    emailPayload.Emailtype = email;
                    emailPayload.SyncUuid  = StringToGuid(email.uuid);
                    EmailDocument emailDocument = _emailAdressTransformation.GetTransformedDocument(emailPayload, Helper.ReducePayloadPath(links));
                    document.emails.Add(emailDocument);
                }
            }
            document.name.Value = tradingAccount.name;
            // ????? document.onhold
            document.people = new PeopleDocumentCollection();
            if (tradingAccount.contacts != null && tradingAccount.contacts != null)
            {
                foreach (contacttype contact in tradingAccount.contacts)
                {
                    ContactPayload contactPayload = new ContactPayload();
                    contactPayload.Contacttype = contact;
                    contactPayload.SyncUuid    = StringToGuid(contact.uuid);
                    PersonDocument person = _contactTransformation.GetTransformedDocument(contactPayload, Helper.ReducePayloadPath(links));
                    document.people.Add(person);
                }
            }
            bool hasMainPerson = false;

            for (int index = 0; index < document.people.documents.Count; index++)
            {
                PersonDocument person = document.people.documents[index] as PersonDocument;
                if ((person.primaryperson.Value != null) && (person.primaryperson.Value.ToString().Equals("true", StringComparison.InvariantCultureIgnoreCase)))
                {
                    hasMainPerson = true;
                }
            }
            if ((!hasMainPerson) && (document.people.documents.Count > 0))
            {
                PersonDocument person = document.people.documents[0] as PersonDocument;
                person.primaryperson.Value = "true";
            }

            document.phones = new PhonesDocumentCollection();
            if (tradingAccount.phones != null && tradingAccount.phones != null)
            {
                foreach (phoneNumbertype phoneNumber in tradingAccount.phones)
                {
                    PhoneNumberPayload phoneNumberPayload = new PhoneNumberPayload();
                    phoneNumberPayload.PhoneNumbertype = phoneNumber;
                    //phoneNumberPayload.SyncUuid = GetUuid(phoneNumber.applicationID);
                    PhoneDocument phone = _phoneNumberTransformation.GetTransformedDocument(phoneNumberPayload, Helper.ReducePayloadPath(links));
                    document.phones.Add(phone);
                }
            }
            document.customerSupplierFlag.Value = tradingAccount.customerSupplierFlag;
            return(document);
        }