using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// 报价单
/// </summary>
public class QuoteHelper
{
public static readonly string entityName = "quote";
public Guid quoteId = Guid.Empty;
public IOrganizationService service;
/// <summary>
/// 创建报价单
/// </summary>
public void Create(Guid accountId)
{
Entity en = new Entity() { LogicalName = entityName };
en["name"] = "报价单測试";
en["accountid"] = new EntityReference() { LogicalName = "account",Id = accountId };
quoteId = service.Create(en);
}
/// <summary>
/// 创建报价单及其相关报价单具体信息
/// </summary>
/// <param name="childEn">报价单具体信息</param>
public void CompoundCreate(EntityCollection childEn)
{
CompoundCreateRequest request = new CompoundCreateRequest();
request.Entity = new Entity() { LogicalName = entityName };
request.ChildEntities = childEn;
CompoundCreateResponse response = (CompoundCreateResponse)service.Execute(request);
quoteId = response.Id;
}
/// <summary>
/// 将报价单分派给其它用户或团队
/// </summary>
/// <param name="assignee">用户或团队引用</param>
public void Assign(EntityReference assignee)
{
AssignRequest request = new AssignRequest();
request.Target = new EntityReference() { LogicalName = entityName,Id = quoteId };
request.Assignee = assignee;
AssignResponse response = (AssignResponse)service.Execute(request);
}
/// <summary>
/// 指定结束报价单
/// </summary>
/// <param name="status">结束的状态值</param>
public void CloseQuote(int status)
{
CloseQuoteRequest request = new CloseQuoteRequest();
request.QuoteClose = new Entity() { LogicalName = entityName,Id = quoteId };
request.Status = new OptionSetValue(status);
CloseQuoteResponse response = (CloseQuoteResponse)service.Execute(request);
}
/// <summary>
/// 将报价单转换为销售订单
/// </summary>
public Entity ConvertQuoteToSalesOrder()
{
ConvertQuoteToSalesOrderRequest request = new ConvertQuoteToSalesOrderRequest();
request.QuoteId = quoteId;
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name","accountid");
ConvertQuoteToSalesOrderResponse response = (ConvertQuoteToSalesOrderResponse)service.Execute(request);
//销售订单
Entity solrorderEn = response.Entity;
return solrorderEn;
}
/// <summary>
/// 为目标值中指定的实体获取产品的数量小数值
/// </summary>
/// <param name="productId">产品id</param>
/// <param name="uoMId">单位id</param>
public int GetQuantityDecimal(Guid productId, Guid uoMId)
{
GetQuantityDecimalRequest request = new GetQuantityDecimalRequest();
request.Target = new EntityReference() { LogicalName = entityName,Id = quoteId };
request.ProductId = productId;
request.UoMId = uoMId;
GetQuantityDecimalResponse response = (GetQuantityDecimalResponse)service.Execute(request);
return response.Quantity;
}
/// <summary>
/// 从商机中检索产品并将其拷贝到报价单
/// </summary>
/// <param name="opportunityId">商机id</param>
public void GetQuoteProductsFromOpportunity(Guid opportunityId)
{
GetQuoteProductsFromOpportunityRequest request = new GetQuoteProductsFromOpportunityRequest();
request.OpportunityId = opportunityId;
request.QuoteId = quoteId;
GetQuoteProductsFromOpportunityResponse response =
(GetQuoteProductsFromOpportunityResponse)service.Execute(request);
}
/// <summary>
/// 与其它安全主体(用户或团队)共享报价单
/// </summary>
/// <param name="accessMask">訪问权限</param>
/// <param name="principal">用户或团队引用</param>
public void GrantAccessRequest(AccessRights accessMask,EntityReference principal)
{
GrantAccessRequest request = new GrantAccessRequest();
request.Target = new EntityReference() { LogicalName = entityName,Id = quoteId };
request.PrincipalAccess = new PrincipalAccess() { AccessMask = accessMask,Principal = principal };
GrantAccessResponse response = (GrantAccessResponse)service.Execute(request);
}
/// <summary>
/// 将报价单的状态设置为“草稿”
/// </summary>
public void ReviseQuote()
{
ReviseQuoteRequest request = new ReviseQuoteRequest();
request.QuoteId = quoteId;
request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name","accountid");
ReviseQuoteResponse response = (ReviseQuoteResponse)service.Execute(request);
//草稿状态的报价单
Entity quoteEn = response.Entity;
}
/// <summary>
/// 汇总或检索与指定的记录(客户或联系人)相关的全部报价单
/// </summary>
/// <param name="target">客户或联系人引用</param>
/// <param name="query">查询条件</param>
/// <param name="rollupType">关联类型</param>
public void Rollup(EntityReference target, QueryBase query, RollupType rollupType)
{
RollupRequest request = new RollupRequest();
request.Target = target;
request.Query = query;
request.RollupType = rollupType;
RollupResponse response = (RollupResponse)service.Execute(request);
EntityCollection entityCollection = response.EntityCollection;
}
/// <summary>
/// 删除报价单
/// </summary>
public void Delete()
{
service.Delete(entityName, quoteId);
}
}