using System;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    using Microsoft.Crm.Sdk.Messages;

/// <summary>

    /// 发票

    /// </summary>

    public class InvoiceHelper

    {

        public static readonly string entityName = "invoice";

        public Guid invoiceId = Guid.Empty;

        public IOrganizationService service;

/// <summary>

        /// 创建发票

        /// </summary>

        public void Create(Guid accountId)

        {

            Entity en = new Entity() { LogicalName = entityName, Id = accountId };

            en["name"] = "发票測试";

            en["accountid"] = new EntityReference() { LogicalName = "account", Id = accountId };

            invoiceId = service.Create(en);

        }

/// <summary>

        /// 将发票分派给其它用户或团队

        /// </summary>

        /// <param name="assignee">用户或团队引用</param>

        public void Assign(EntityReference assignee)

        {

            AssignRequest request = new AssignRequest();

            request.Target = new EntityReference() { LogicalName = entityName, Id = invoiceId };

            request.Assignee = assignee;

            AssignResponse response = (AssignResponse)service.Execute(request);

        }

/// <summary>

        /// 锁定指定发票中产品的单位价格

        /// </summary>

        public void LockInvoicePricing()

        {

            LockInvoicePricingRequest request = new LockInvoicePricingRequest();

            request.InvoiceId = invoiceId;

            LockInvoicePricingResponse response = (LockInvoicePricingResponse)service.Execute(request);

        }

/// <summary>

        /// 解锁指定发票中产品的单位价格

        /// </summary>

        public void UnlockInvoicePricing()

        {

            UnlockInvoicePricingRequest request = new UnlockInvoicePricingRequest();

            request.InvoiceId = invoiceId;

            UnlockInvoicePricingResponse response = (UnlockInvoicePricingResponse)service.Execute(request);

        }

/// <summary>

        /// 取消指定安全主体(用户或团队)对发票的全部訪问权限

        /// </summary>

        /// <param name="revokee">用户或团队引用</param>

        public void RevokeAccess(EntityReference revokee)

        {

            RevokeAccessRequest request = new RevokeAccessRequest();

            request.Target = new EntityReference() { LogicalName = entityName, Id = invoiceId };

            request.Revokee = revokee;

            RevokeAccessResponse response = (RevokeAccessResponse)service.Execute(request);

        }

/// <summary>

        /// 删除发票

        /// </summary>

        public void Delete()

        {

            service.Delete(entityName, invoiceId);

        }

    }

05-13 05:08