Client needs to create a return(s) from
warranty claims that are processed by the external dealers. Claims will be entered via the dealer in the
portal and could have multiple lines of detail for multiple parts that will
need to be automatically added to a return(s) when the claim has been
approved. Returns will be created as a
CRM Quote with a Quote type of “Return”.
Plugin Trigger
·
Record
col_claiminitiation
·
Fire plugin
when the claim status is changed to “Approved”.
Field name is statuscode and the option value is “181400004”.
Plugin logic
Once
the claim has been approved, loop through the associated line items (col_ claimdetail)
to add each claim detail item to the related return. (Quote)
Step 1:
Create
a new return record for each unique value of col_invoiceid within the claim
detail lines. Claim detail records where
col_invoiceid are null should also create a return. Only claim detail records where col_approved
is equal to true should be added to the return.
Step 2:
A line
items to the return based on the field mapping provided at the bottom of this
document. Only claim detail records
where col_approved is equal to true should be added to the return.
Step 3:
Sum the
amount stored in col_approvedlaboramount by col_invoiceid create a single line
item on the return for dealer labor. Only
labor amounts on the claim detail records where col_approved is equal to true
should be added to the return.
If the
labor amount is > 0 then create a new line item according to the table
below, but use the following replacement values.
Source Record
|
Source Field
|
Target Record
|
Target Field
|
Static Value
|
ProductID
associated with productnumber “4211002”
|
Quotedetail
|
productid
|
Static Value
|
1
|
Quotedetail
|
ispriceoverridden
|
Static Value
|
“EACH”
D30CEE58-B8D6-E311-88F0-005056884483
|
Quotedetail
|
uomid
|
Static Value
|
1
|
Quotedetail
|
quantity
|
Col_claimdetail
|
Sum(col_approvedlaboramount) grouped by invoice
|
Quotedetail
|
priceperunit
|
Col_claimdetail
|
Sum(col_approvedlaboramount) grouped by invoice
|
Quotedetail
|
extendedamount
|
Field Mapping
Source Record
|
Source Field
|
Target Record
|
Target Field
|
Col_claimInitiation
|
Col_dealer
|
Quote(Purchasing Dealer)
|
Customerid
|
Col_claimInitiation
|
Col_name + Last 3 digits of Invoice #
|
Quote
|
QuoteNumber
|
Static Value
|
“Dealer” list
|
Quote
|
Pricelist
|
Static Value
|
181400002
|
Quote
|
Col_QuoteType
|
Col_claiminitiation
|
Col_name
|
Quote
|
Col_ponumber
|
Col_claiminitiaition
|
Col_name
|
Quote
|
Name
|
col_claiminitiation
|
col_boatid
|
Quote
|
Col_boatid
|
Static Value
|
“USD”
|
Quote
|
TransactionCurrencyid
|
Col_claimdetail
|
Col_invoicedpartid
|
QuoteDetail
|
productid
|
Static Value
|
1
|
QuoteDetail
|
ispriceoverridden
|
Static Value
|
“EACH”
|
QuoteDetail
|
uomid
|
col_claimdetail
|
Col_quantity
|
QuoteDetail
|
quantity
|
Col_claimdetail
|
ExtendCostAmt
|
QuoteDetail
|
priceperunit
|
Col_claimdetail
|
ExtendCostAmt
|
QuoteDetail
|
extendedamount
|
Refer to example Below Example
Claim Number: | C012115-0002 | ||||
Claim Date: | 05-03-2015 | ||||
Customer: | Test Dealer | ||||
Item Number |
Item Description | Invoice | Qty | Amount | Labour Amt |
006047.1 | Freight Reimbursement, Warranty Claim | 1 | $75.00 | ||
6395209 | Cable, iPod Extension w/SD, UBS, Aux, '14 | 1 | $73.14 | ||
005022 | Labor Reimbursement, Warranty Claim | 1 | $300.00 | ||
3541601 | Propeller, 14" x 16" x 1-1/8", 4 Blade, Promo - OJ | INV-01163-G2H8P9 | 1 | $362.16 | 75 |
3962059 | Bluetooth, Model Year 2015, Malibu Bluetooth Backup | INV-01162-J6F1Y8 | 1 | $86.29 | 112.5 |
4211006 | Pump, Bilge, Automatic, Whale 650 GPH, w/Delphi WeatherPack, '14 | INV-01162-J6F1Y8 | 1 | $124.68 | 150 |
Total Claim | $1,021.27 | 337.5 | |||
The claim above would generate 3 unique returns based on the fact that 3 of the items are related to 2 separate invoices and the other 3 are not related to any invoice. | |||||
Return Number: | C012115-0002-01 (Claim Number plus unique 2 digit number) | ||||
Return Date | 03/05/15 | ||||
Invoice # | INV00101 (Based on Invoice INV-01163-G2H8P9) | ||||
Customer: | Test Dealer | ||||
Item | Description | Quantity | U of M | Unit Price | Extended Price |
3541601 | Propeller, 14" x 16" x 1-1/8", 4 Blade, Promo - OJ | 1 | Each | $362.16 | $362.16 |
05022 | Labor Reimbursement, Warranty Claim | 1 | EACH | $75.00 | $75.00 |
Return Total | $225.00 | ||||
Return Number: | C012115-0002-02 (Claim Number plus unique 2 digit number) | ||||
Return Date | 03/05/15 | ||||
Invoice # | INV00101 (Based on Invoice INV-01162-J6F1Y8) | ||||
Customer: | Test Dealer | ||||
Item | Description | Quantity | U of M | Unit Price | Extended Price |
3962059 | Bluetooth, Model Year 2015, Malibu Bluetooth Backup | 1 | Each | $86.29 | $86.29 |
4211006 | Pump, Bilge, Automatic, Whale 650 GPH, w/Delphi WeatherPack, '14 | 1 | Each | $124.68 | $124.68 |
005022 | Labor Reimbursement, Warranty Claim | 1 | Each | $262.50 | $262.50 |
Return Total | $210.97 | ||||
Return Number: | C012115-0002-03 (Claim Number plus unique 2 digit number) | ||||
Return Date | 03/05/15 | ||||
Invoice # | |||||
Customer: | Test Dealer | ||||
Item | Description | Quantity | U of M | Unit Price | Extended Price |
006047.1 | Freight Reimbursement, Warranty Claim | 1 | Each | $75.00 | $75.00 |
6395209 | Cable, iPod Extension w/SD, UBS, Aux, '14 | 1 | Each | $73.14 | $73.14 |
005022 | Labor Reimbursement, Warranty Claim | 1 | Each | $300.00 | $300.00 |
Return Total | $448.14 |
#region Reference
using System;
using System.ServiceModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using System.Data;
#endregion
namespace MalibuReturnPlugins
{
public class ReturnClaim : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
StringBuilder sb = new StringBuilder("starting; ");
if (context.Depth == 1 && string.Equals(context.MessageName, "Update", StringComparison.OrdinalIgnoreCase))
{
#region Variable
Guid ClaimID = Guid.Empty;
Guid Dealer = Guid.Empty;
string ClaimName = string.Empty;
Guid BoatID = Guid.Empty;
Guid QuoteID = Guid.Empty;
Guid QuoteDetailID = Guid.Empty;
Guid InvoiceID = Guid.Empty;
string Quantity = string.Empty;
Guid Customerid = Guid.Empty;
string Ordernumber = string.Empty;
Guid Pricelevel = Guid.Empty;
string quoteType = "181400002";
Guid currencyID = Guid.Empty;
decimal ApprLaboutAmount = 0;
decimal ApprAmt = 0;
bool _ispriceoverridden = true;
Guid uomid = Guid.Empty;
decimal _quantity = 1;
Guid _labourProductID = Guid.Empty;
#endregion
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
ClaimID = entity.Id;
if (entity.LogicalName != "col_claiminitiation") return;
try
{
// Price list fetchxml for Reurn
string queryPricelist = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='pricelevel'>
<attribute name='name' />
<attribute name='pricelevelid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='name' operator='eq' value='Dealer' />
</filter>
</entity>
</fetch>";
queryPricelist = string.Format(queryPricelist);
EntityCollection FetchPricelistResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryPricelist));
if (FetchPricelistResult.Entities.Count > 0)
{
foreach (Entity Pricelistent in FetchPricelistResult.Entities)
{
if (Pricelistent.Attributes.Contains("pricelevelid"))
{
Pricelevel = Pricelistent.GetAttributeValue<Guid>("pricelevelid");
}
}
}
// Currency Fetch XML for Reurn
string querycurrency = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='transactioncurrency'>
<attribute name='transactioncurrencyid' />
<attribute name='currencyname' />
<attribute name='isocurrencycode' />
<order attribute='currencyname' descending='false' />
<filter type='and'>
<condition attribute='isocurrencycode' operator='eq' value='USD' />
</filter>
</entity>
</fetch>";
querycurrency = string.Format(querycurrency);
EntityCollection FetchcurrencyResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(querycurrency));
if (FetchcurrencyResult.Entities.Count > 0)
{
foreach (Entity Currencyent in FetchcurrencyResult.Entities)
{
if (Currencyent.Attributes.Contains("transactioncurrencyid"))
{
currencyID = Currencyent.GetAttributeValue<Guid>("transactioncurrencyid");
}
}
}
// Product ID Fetch XML for Return Detail Labour line item
string queryLabourProduct = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='product'>
<attribute name='name' />
<attribute name='productnumber' />
<attribute name='productid' />
<order attribute='productnumber' descending='false' />
<filter type='and'>
<condition attribute='productnumber' operator='eq' value='005022' />
</filter>
</entity>
</fetch>";
queryLabourProduct = string.Format(queryLabourProduct);
EntityCollection FetchLabourProductResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryLabourProduct));
if (FetchLabourProductResult.Entities.Count > 0)
{
foreach (Entity LabourProductent in FetchLabourProductResult.Entities)
{
if (LabourProductent.Attributes.Contains("productid"))
{
_labourProductID = LabourProductent.GetAttributeValue<Guid>("productid");
}
}
}
// Claim FetchXML
string query = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='col_claiminitiation'>
<attribute name='col_claiminitiationid' />
<attribute name='col_name' />
<attribute name='col_dealer' />
<attribute name='col_boat' />
<filter type='and'>
<condition attribute='col_claiminitiationid' operator='eq' value='{0}' />
<condition attribute='statuscode' operator='eq' value='181400004' />
</filter>
</entity>
</fetch>";
query = string.Format(query, ClaimID);
EntityCollection FetchClamResults = (EntityCollection)service.RetrieveMultiple(new FetchExpression(query));
if (FetchClamResults.Entities.Count > 0)
{
foreach (Entity ent in FetchClamResults.Entities)
{
if (ent.Attributes.Contains("col_dealer") && ent.GetAttributeValue<EntityReference>("col_dealer") != null)
{
Dealer = ent.GetAttributeValue<EntityReference>("col_dealer").Id;
}
if (ent.Attributes.Contains("col_boat") && ent.GetAttributeValue<EntityReference>("col_boat") != null)
{
BoatID = ent.GetAttributeValue<EntityReference>("col_boat").Id;
}
if (ent.Attributes.Contains("col_name") && ent.GetAttributeValue<String>("col_name") != null)
{
ClaimName = ent.GetAttributeValue<String>("col_name").ToString();
}
}
// Claim Detail FetchXML
string queryFetchClamDetail = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='col_claimdetail'>
<attribute name='col_approvedlaboramount' />
<attribute name='col_invoicenumberid' />
<attribute name='col_claimdetailid' />
<attribute name='col_quantity' />
<attribute name='col_noninvoicedpartid' />
<attribute name='col_approvedamount' />
<attribute name='col_unitcost' />
<attribute name='col_extendedcost' />
<attribute name='col_approvedhours' />
<attribute name='col_subletlaborrate' />
<filter type='and'>
<condition attribute='col_claimid' operator='eq' value='{0}' />
<condition attribute='col_approved' operator='eq' value='1' />
</filter>
</entity>
</fetch>";
queryFetchClamDetail = string.Format(queryFetchClamDetail, ClaimID);
EntityCollection FetchClamDetailResults = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryFetchClamDetail));
List<ReturnQuoteClaim> tmpReturns = new List<ReturnQuoteClaim>();
sb.AppendFormat("Claim Detail Count : " + FetchClamDetailResults.Entities.Count());
if (FetchClamDetailResults.Entities.Count > 0)
{
bool emptyInvoice = false;
foreach (Entity enty in FetchClamDetailResults.Entities)
{
if (enty.Attributes.Contains("col_invoicenumberid") && enty.GetAttributeValue<EntityReference>("col_invoicenumberid") != null)
{
InvoiceID = enty.GetAttributeValue<EntityReference>("col_invoicenumberid").Id;
bool status = false;
foreach (ReturnQuoteClaim tmpReturn in tmpReturns)
{
if (tmpReturn.InvID.Equals(InvoiceID))
{
Decimal ApprLaboutAmt = 0;
status = true;
if (enty.Attributes.Contains("col_approvedlaboramount"))
{
ApprLaboutAmt = ((Money)enty.Attributes["col_approvedlaboramount"]).Value;
tmpReturn.sumOfInvoice = ApprLaboutAmt + tmpReturn.sumOfInvoice;
}
}
}
if (!status)
{
ReturnQuoteClaim tmpRet = new ReturnQuoteClaim();
tmpRet.InvID = InvoiceID;
// need to check
string coninvoiceid = InvoiceID.ToString().Substring(InvoiceID.ToString().Length - 4, 3).ToString();
tmpRet.CustomerID = Dealer != null ? Dealer : Guid.Empty;
tmpRet.quoteNumber = ClaimName + coninvoiceid;
tmpRet.PricelistID = Pricelevel;
tmpRet.ponumber = ClaimName != string.Empty ? ClaimName.ToString() : string.Empty;
tmpRet.quotetype = quoteType.ToString();
tmpRet.Name = ClaimName != string.Empty ? ClaimName.ToString() : string.Empty;
tmpRet.BoatID = BoatID != null ? BoatID : Guid.Empty;
tmpRet.CurrencyID = currencyID;
if (enty.Attributes.Contains("col_approvedlaboramount"))
{
ApprLaboutAmount = ((Money)enty.Attributes["col_approvedlaboramount"]).Value;
tmpRet.sumOfInvoice = ApprLaboutAmount + tmpRet.sumOfInvoice;
}
if (enty.Attributes.Contains("col_extendedcost"))
{
ApprAmt = ((Money)enty.Attributes["col_extendedcost"]).Value;
tmpRet.ApprovedAmt = ApprAmt + tmpRet.ApprovedAmt;
}
if (enty.Attributes.Contains("col_noninvoicedpartid"))
{
tmpRet.invoicedpartid = enty.GetAttributeValue<EntityReference>("col_noninvoicedpartid").Id;
}
else
{
tmpRet.invoicedpartid = Guid.Empty;
}
tmpReturns.Add(tmpRet);
}
}
else
{
if (!emptyInvoice)
{
ReturnQuoteClaim tmpRet = new ReturnQuoteClaim();
tmpRet.InvID = Guid.Empty;
InvoiceID = Guid.Empty;
string coninvoiceid = InvoiceID.ToString().Substring(InvoiceID.ToString().Length - 4, 3).ToString();
tmpRet.CustomerID = Dealer != null ? Dealer : Guid.Empty;
tmpRet.quoteNumber = ClaimName + coninvoiceid;
tmpRet.PricelistID = Pricelevel;
tmpRet.ponumber = ClaimName != string.Empty ? ClaimName.ToString() : string.Empty;
tmpRet.quotetype = quoteType.ToString();
tmpRet.Name = ClaimName != string.Empty ? ClaimName.ToString() : string.Empty;
tmpRet.BoatID = BoatID != null ? BoatID : Guid.Empty;
tmpRet.CurrencyID = currencyID;
if (enty.Attributes.Contains("col_approvedlaboramount"))
{
ApprLaboutAmount = ((Money)enty.Attributes["col_approvedlaboramount"]).Value;
tmpRet.sumOfInvoice = ApprLaboutAmount + tmpRet.sumOfInvoice;
}
if (enty.Attributes.Contains("col_extendedcost"))
{
ApprAmt = ((Money)enty.Attributes["col_extendedcost"]).Value;
tmpRet.ApprovedAmt = ApprAmt + tmpRet.ApprovedAmt;
}
if (enty.Attributes.Contains("col_noninvoicedpartid"))
{
tmpRet.invoicedpartid = enty.GetAttributeValue<EntityReference>("col_noninvoicedpartid").Id;
}
else
{
tmpRet.invoicedpartid = Guid.Empty;
}
tmpReturns.Add(tmpRet);
emptyInvoice = true;
}
}
}
foreach (ReturnQuoteClaim newReturn in tmpReturns)
{
Guid ReturnInvID = newReturn.InvID;
Guid returnQuoteID = CreateReturnRecord(newReturn.CustomerID, newReturn.quoteNumber,
newReturn.PricelistID, newReturn.ponumber.ToString(), newReturn.quotetype.ToString(),
newReturn.Name.ToString(), newReturn.BoatID, newReturn.CurrencyID,
ref service);
sb.AppendFormat("New Return ID : " + returnQuoteID.ToString());
if (returnQuoteID != Guid.Empty && ReturnInvID != Guid.Empty)
{
newReturn.reuturnQuoteID = returnQuoteID;
string queryFetchClamInvoiceartDetail = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='col_claimdetail'>
<attribute name='col_approvedlaboramount' />
<attribute name='col_quantity' />
<attribute name='col_noninvoicedpartid' />
<attribute name='col_approvedamount' />
<attribute name='col_unitcost' />
<attribute name='col_extendedcost' />
<attribute name='col_approvedhours' />
<attribute name='col_subletlaborrate' />
<filter type='and'>
<condition attribute='col_invoicenumberid' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>";
queryFetchClamInvoiceartDetail = string.Format(queryFetchClamInvoiceartDetail, ReturnInvID);
EntityCollection FetchClamInoivePartDetailResults = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryFetchClamInvoiceartDetail));
Decimal InvAprrAmt = 0;
foreach (Entity invoicepart in FetchClamInoivePartDetailResults.Entities)
{
Guid NonInvPartID = Guid.Empty;
Decimal QuoteDetailQuantity = 0;
Decimal ApproveLaboutAmt = 0;
Decimal InvExtendCostAmt = 0;
if (invoicepart.Attributes.Contains("col_quantity"))
{
QuoteDetailQuantity = Convert.ToDecimal(invoicepart.GetAttributeValue<Decimal>("col_quantity").ToString());
}
if (invoicepart.Attributes.Contains("col_noninvoicedpartid") && invoicepart.GetAttributeValue<EntityReference>("col_noninvoicedpartid") != null)
{
NonInvPartID = invoicepart.GetAttributeValue<EntityReference>("col_noninvoicedpartid").Id; // Product ID
}
if (invoicepart.Attributes.Contains("col_approvedlaboramount"))
{
ApproveLaboutAmt = ((Money)invoicepart.Attributes["col_approvedlaboramount"]).Value;
InvAprrAmt = InvAprrAmt + ApproveLaboutAmt;
}
if (invoicepart.Attributes.Contains("col_extendedcost"))
{
InvExtendCostAmt = ((Money)invoicepart.Attributes["col_extendedcost"]).Value;
}
// Uom fetchxml for labour return detail
string queryUOM = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='col_claimdetail'>
<attribute name='col_claimdetailid' />
<attribute name='col_name' />
<attribute name='createdon' />
<order attribute='col_name' descending='false' />
<filter type='and'>
<condition attribute='col_claimid' operator='eq' value='{0}' />
</filter>
<link-entity name='product' from='productid' to='col_noninvoicedpartid' alias='ae'>
<attribute name='defaultuomid' alias='UnitID'/>
<filter type='and'>
<condition attribute='productid' operator='eq' value='{1}' />
</filter>
</link-entity>
</entity>
</fetch>";
queryUOM = string.Format(queryUOM, ClaimID, NonInvPartID);
EntityCollection FetchUOMResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryUOM));
if (FetchUOMResult.Entities.Count > 0)
{
foreach (Entity UOMent in FetchUOMResult.Entities)
{
if (UOMent.Attributes.Contains("UnitID"))
{
uomid = ((EntityReference)UOMent.GetAttributeValue<AliasedValue>("UnitID").Value).Id;
}
}
}
sb.AppendFormat("Unit of Measure ID for Invoices : " + uomid.ToString());
Guid ReturnClaimDetailID = ReturnClaimDetail(returnQuoteID, NonInvPartID,
_ispriceoverridden, uomid, QuoteDetailQuantity, InvExtendCostAmt, InvExtendCostAmt, ref service);
sb.AppendFormat("New Return Detail ID : " + ReturnClaimDetailID.ToString());
}
if (InvAprrAmt > 0)
{
// Labor Reimbursement, Warranty Claim Fetch XML
string querylabour = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='product'>
<attribute name='name' />
<attribute name='productnumber' />
<attribute name='defaultuomid' />
<attribute name='statecode' />
<attribute name='productid' />
<order attribute='productnumber' descending='false' />
<filter type='and'>
<condition attribute='productnumber' operator='eq' value='005022' />
</filter>
</entity>
</fetch>";
querylabour = string.Format(querylabour);
EntityCollection FetchlabourResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(querylabour));
Guid LaboutProductID = Guid.Empty;
Guid LabourUOMID = Guid.Empty;
if (FetchlabourResult.Entities.Count > 0)
{
foreach (Entity Labourent in FetchlabourResult.Entities)
{
if (Labourent.Attributes.Contains("productid"))
{
LaboutProductID = Labourent.GetAttributeValue<EntityReference>("productid").Id;
}
if (Labourent.Attributes.Contains("defaultuomid"))
{
LabourUOMID = Labourent.GetAttributeValue<EntityReference>("defaultuomid").Id;
}
}
}
Guid ReturnClaimDetailID = ReturnClaimDetail(returnQuoteID, LaboutProductID,
_ispriceoverridden, LabourUOMID, _quantity, InvAprrAmt, InvAprrAmt, ref service);
sb.AppendFormat("New Return Detail ID : " + ReturnClaimDetailID.ToString());
}
}
else
{
string queryFetchNullClamInvoiceDetail = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
<entity name='col_claimdetail'>
<attribute name='col_approvedlaboramount' />
<attribute name='col_quantity' />
<attribute name='col_noninvoicedpartid' />
<attribute name='col_approvedamount' />
<attribute name='col_unitcost' />
<attribute name='col_extendedcost' />
<attribute name='col_approvedhours' />
<attribute name='col_subletlaborrate' />
<filter type='and'>
<condition attribute='col_claimid' operator='eq' value='{0}' />
<condition attribute='col_invoicenumberid' operator='null' />
</filter>
</entity>
</fetch>";
queryFetchNullClamInvoiceDetail = string.Format(queryFetchNullClamInvoiceDetail, ClaimID);
EntityCollection FetchClamInoivePartDetailResults = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryFetchNullClamInvoiceDetail));
foreach (Entity nullinvoicepart in FetchClamInoivePartDetailResults.Entities)
{
Guid NonInvPartID = Guid.Empty;
Decimal QuoteDetailQuantity = 0;
Decimal Extendedcost = 0;
if (nullinvoicepart.Attributes.Contains("col_quantity"))
{
QuoteDetailQuantity = Convert.ToDecimal(nullinvoicepart.GetAttributeValue<Decimal>("col_quantity").ToString());
}
if (nullinvoicepart.Attributes.Contains("col_noninvoicedpartid") && nullinvoicepart.GetAttributeValue<EntityReference>("col_noninvoicedpartid") != null)
{
NonInvPartID = nullinvoicepart.GetAttributeValue<EntityReference>("col_noninvoicedpartid").Id; // Product ID
}
if (nullinvoicepart.Attributes.Contains("col_extendedcost"))
{
Extendedcost = ((Money)nullinvoicepart.Attributes["col_extendedcost"]).Value;
}
// Uom fetchxml for labour return detail
string queryUOM = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='col_claimdetail'>
<attribute name='col_claimdetailid' />
<attribute name='col_name' />
<attribute name='createdon' />
<order attribute='col_name' descending='false' />
<filter type='and'>
<condition attribute='col_claimid' operator='eq' value='{0}' />
</filter>
<link-entity name='product' from='productid' to='col_noninvoicedpartid' alias='ae'>
<attribute name='defaultuomid' alias='UnitID' />
<filter type='and'>
<condition attribute='productid' operator='eq' value='{1}' />
</filter>
</link-entity>
</entity>
</fetch>";
queryUOM = string.Format(queryUOM, ClaimID, NonInvPartID);
EntityCollection FetchUOMResult = (EntityCollection)service.RetrieveMultiple(new FetchExpression(queryUOM));
if (FetchUOMResult.Entities.Count > 0)
{
foreach (Entity UOMent in FetchUOMResult.Entities)
{
if (UOMent.Attributes.Contains("UnitID"))
{
uomid = uomid = ((EntityReference)UOMent.GetAttributeValue<AliasedValue>("UnitID").Value).Id;
}
}
}
sb.AppendFormat("Unit of Measure ID for Non Invoices : " + uomid.ToString());
// Guid ReturnClaimDetailID = ReturnClaimDetail(returnQuoteID, NonInvPartID, _ispriceoverridden, uomid, _quantity, newReturn.sumOfInvoice, newReturn.sumOfInvoice, ref service);
Guid ReturnClaimDetailID = ReturnClaimDetail(returnQuoteID, NonInvPartID,
_ispriceoverridden, uomid, _quantity, Extendedcost, Extendedcost, ref service);
sb.AppendFormat("New Return Detail ID : " + ReturnClaimDetailID.ToString());
}
}
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString() + " " + sb.ToString());
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
}
}
/// <summary>
/// Create New Return Record
/// </summary>
/// <param name="CustomerID">Static Value Guid CustomerID</param>
/// <param name="quoteNumber">string quoteNumber</param>
/// <param name="PricelistID">Static Guid PricelistID</param>
/// <param name="ponumber">Guid ponumber</param>
/// <param name="quotetype">Static Value quote Type</param>
/// <param name="Name">Claim Name</param>
/// <param name="BoatID">Guid Boat ID</param>
/// <param name="CurrencyID">Static Value Currency ID</param>
/// <param name="ServiceObj">IOrganizationService Object</param>
/// <returns>new Return Guid</returns>
public Guid CreateReturnRecord(
Guid CustomerID,
string quoteNumber,
Guid PricelistID,
string ponumber,
string quotetype,
string Name,
Guid BoatID,
Guid CurrencyID, ref IOrganizationService ServiceObj)
{
Guid ClamQuoteID = Guid.Empty;
Entity ReturnObj = new Entity("quote");
if (CustomerID != Guid.Empty)
{
ReturnObj.Attributes.Add("customerid", new EntityReference("account", CustomerID));
}
if (quoteNumber != string.Empty)
{
ReturnObj.Attributes.Add("quotenumber", quoteNumber);
}
if (PricelistID != Guid.Empty)
{
ReturnObj.Attributes.Add("pricelevelid", new EntityReference("pricelevel", PricelistID));
}
if (quotetype != string.Empty)
{
ReturnObj.Attributes.Add("col_quotetype", new OptionSetValue(181400002));
}
if (ponumber != string.Empty)
{
ReturnObj.Attributes.Add("col_ponumber", ponumber);
}
if (Name != string.Empty)
{
ReturnObj.Attributes.Add("name", Name);
}
if (BoatID != Guid.Empty)
{
ReturnObj.Attributes.Add("col_boatid", new EntityReference("col_boat", BoatID));
}
if (CurrencyID != Guid.Empty)
{
ReturnObj.Attributes.Add("transactioncurrencyid", new EntityReference("transactioncurrency", CurrencyID));
}
ClamQuoteID = ServiceObj.Create(ReturnObj);
return ClamQuoteID;
}
/// <summary>
/// Create retun detail & move the record to quote detail
/// </summary>
/// <param name="productid">Guid productid</param>
/// <param name="ispriceoverridden">Boolean static value 1</param>
/// <param name="Uomid"> guid uoimd</param>
/// <param name="quantity">static value 1</param>
/// <param name="priceperunit">priceperunit</param>
/// <param name="extendedamt">extendedamt</param>
/// <param name="col_noninvoicedpartid_ProductID"></param>
/// <returns></returns>
public Guid ReturnClaimDetail(
Guid ReturnID,
Guid productid,
Boolean ispriceoverridden,
Guid Uomid,
decimal quantity,
decimal priceperunit,
decimal extendedamt,
ref IOrganizationService ServiceObj
)
{
Guid ReturnClaimDetail = Guid.Empty;
Entity ReturnDetailObj = new Entity("quotedetail");
if (ReturnID != Guid.Empty)
{
ReturnDetailObj.Attributes.Add("quoteid", new EntityReference("quote", ReturnID));
}
if (productid != Guid.Empty)
{
ReturnDetailObj.Attributes.Add("productid", new EntityReference("product", productid));
}
ReturnDetailObj.Attributes.Add("ispriceoverridden", ispriceoverridden);
ReturnDetailObj.Attributes.Add("uomid", new EntityReference("uom", Uomid));
ReturnDetailObj.Attributes.Add("quantity", quantity);
ReturnDetailObj.Attributes.Add("priceperunit", priceperunit);
ReturnDetailObj.Attributes.Add("extendedamount", extendedamt);
ReturnClaimDetail = ServiceObj.Create(ReturnDetailObj);
return ReturnClaimDetail;
}
}
/// <summary>
/// for each distinct Invoice ID for Claim Detail. create a return for each
/// </summary>
public class ReturnQuoteClaim
{
public Guid CustomerID;
public string quoteNumber;
public Guid PricelistID;
public Guid BoatID;
public Guid CurrencyID;
public string Name;
public string ponumber;
public string quotetype;
public Guid InvID;
public Guid reuturnQuoteID;
public Decimal sumOfInvoice = 0;
public Decimal ApprovedAmt = 0;
public Guid invoicedpartid;
public ReturnQuoteClaim() { }
}
}
No comments:
Post a Comment