Produced by Araxis Merge on 2/1/2017 2:56:25 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\Araxis_Merge_Comprasion\Pub_un\BTSSS_CIF_122016.zip\BTSSS_CIF_12_20_16\clean\CRM\trunk\SDK\SampleCode\CS\BusinessDataModel\ProductCatalog\CustomPricePlug-In | CalculatePricePlugin.cs | Tue Dec 20 19:51:44 2016 UTC |
| 2 | Wed Feb 1 19:56:25 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 464 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 2 | // This f ile is par t of the M icrosoft C RM SDK Cod e Samples. | |||||
| 3 | // | |||||
| 4 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 5 | // | |||||
| 6 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 7 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 8 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 9 | // | |||||
| 10 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 11 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 12 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 13 | // PARTIC ULAR PURPO SE. | |||||
| 14 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 15 | //<snippet CalculateP ricePlugin > | |||||
| 16 | using Syst em; | |||||
| 17 | using Syst em.Service Model; | |||||
| 18 | ||||||
| 19 | // Microso ft Dynamic s CRM name space(s) | |||||
| 20 | using Micr osoft.Xrm. Sdk; | |||||
| 21 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 22 | ||||||
| 23 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 24 | { | |||||
| 25 | public class Cal culatePric ePlugin : IPlugin | |||||
| 26 | { | |||||
| 27 | // / <summary > | |||||
| 28 | // / A plugin that calc ulates cus tom pricin g for | |||||
| 29 | // / opportun ities, quo tes, order s, and inv oices. | |||||
| 30 | // / </summar y> | |||||
| 31 | // / <remarks >Register this plug- in on the CalculateP rice messa ge, | |||||
| 32 | // / Post Ope ration exe cution sta ge, and Sy nchronous execution mode. | |||||
| 33 | // / </remark s> | |||||
| 34 | pu blic void Execute(IS erviceProv ider servi ceProvider ) | |||||
| 35 | { | |||||
| 36 | //Extrac t the trac ing servic e for use in debuggi ng sandbox ed plug-in s. | |||||
| 37 | ITracing Service tr acingServi ce = | |||||
| 38 | (ITr acingServi ce)service Provider.G etService( typeof(ITr acingServi ce)); | |||||
| 39 | ||||||
| 40 | // Obtai n the exec ution cont ext from t he service provider. | |||||
| 41 | IPluginE xecutionCo ntext cont ext = (IPl uginExecut ionContext ) | |||||
| 42 | serv iceProvide r.GetServi ce(typeof( IPluginExe cutionCont ext)); | |||||
| 43 | ||||||
| 44 | if (cont ext.Parent Context != null | |||||
| 45 | && c ontext.Par entContext .ParentCon text != nu ll | |||||
| 46 | && c ontext.Par entContext .ParentCon text != nu ll | |||||
| 47 | && c ontext.Par entContext .ParentCon text.Paren tContext.S haredVaria bles.Conta insKey("Cu stomPrice" ) | |||||
| 48 | && ( bool)conte xt.ParentC ontext.Par entContext .ParentCon text.Share dVariables ["CustomPr ice"]) | |||||
| 49 | retu rn; | |||||
| 50 | ||||||
| 51 | // The I nputParame ters colle ction cont ains all t he data pa ssed in th e message request. | |||||
| 52 | if (cont ext.InputP arameters. Contains(" Target") & & | |||||
| 53 | cont ext.InputP arameters[ "Target"] is EntityR eference) | |||||
| 54 | { | |||||
| 55 | // O btain the target ent ity from t he input p armameters . | |||||
| 56 | Enti tyReferenc e entity = (EntityRe ference)co ntext.Inpu tParameter s["Target" ]; | |||||
| 57 | ||||||
| 58 | // V erify that the targe t entity r epresents an appropr iate entit y. | |||||
| 59 | if ( CheckIfNot ValidEntit y(entity)) | |||||
| 60 | return; | |||||
| 61 | ||||||
| 62 | try | |||||
| 63 | { | |||||
| 64 | context.Sh aredVariab les.Add("C ustomPrice ", true); | |||||
| 65 | context.Pa rentContex t.SharedVa riables.Ad d("CustomP rice", tru e); | |||||
| 66 | IOrganizat ionService Factory se rviceFacto ry = (IOrg anizationS erviceFact ory)servic eProvider. GetService (typeof(IO rganizatio nServiceFa ctory)); | |||||
| 67 | IOrganizat ionService service = serviceFa ctory.Crea teOrganiza tionServic e(context. UserId); | |||||
| 68 | ||||||
| 69 | // Calcula te pricing depending on the ta rget entit y | |||||
| 70 | switch (en tity.Logic alName) | |||||
| 71 | { | |||||
| 72 | case " opportunit y": | |||||
| 73 | Ca lculateOpp ortunity(e ntity, ser vice); | |||||
| 74 | re turn; | |||||
| 75 | ||||||
| 76 | case " quote": | |||||
| 77 | Ca lculateQuo te(entity, service); | |||||
| 78 | re turn; | |||||
| 79 | ||||||
| 80 | case " salesorder ": | |||||
| 81 | Ca lculateOrd er(entity, service); | |||||
| 82 | re turn; | |||||
| 83 | ||||||
| 84 | case " invoice": | |||||
| 85 | Ca lculateInv oice(entit y, service ); | |||||
| 86 | re turn; | |||||
| 87 | ||||||
| 88 | case " opportunit yproduct": | |||||
| 89 | Ca lculateOpp ortunityPr oduct(enti ty, servic e); | |||||
| 90 | re turn; | |||||
| 91 | ||||||
| 92 | case " quotedetai l": | |||||
| 93 | Ca lculateQuo teProduct( entity, se rvice); | |||||
| 94 | re turn; | |||||
| 95 | ||||||
| 96 | case " salesorder detail": | |||||
| 97 | Ca lculateOrd erProduct( entity, se rvice); | |||||
| 98 | re turn; | |||||
| 99 | ||||||
| 100 | case " invoicedet ail": | |||||
| 101 | Ca lculateInv oiceProduc t(entity, service); | |||||
| 102 | re turn; | |||||
| 103 | ||||||
| 104 | defaul t: | |||||
| 105 | re turn; | |||||
| 106 | } | |||||
| 107 | } | |||||
| 108 | ||||||
| 109 | catc h (FaultEx ception<Or ganization ServiceFau lt> ex) | |||||
| 110 | { | |||||
| 111 | tracingSer vice.Trace ("Calculat ePrice: {0 }", ex.ToS tring()); | |||||
| 112 | throw new InvalidPlu ginExecuti onExceptio n("An erro r occurred in the Ca lculate Pr ice plug-i n.", ex); | |||||
| 113 | } | |||||
| 114 | ||||||
| 115 | catc h (Excepti on ex) | |||||
| 116 | { | |||||
| 117 | tracingSer vice.Trace ("Calculat ePrice: {0 }", ex.ToS tring()); | |||||
| 118 | throw; | |||||
| 119 | } | |||||
| 120 | } | |||||
| 121 | } | |||||
| 122 | ||||||
| 123 | pr ivate stat ic bool Ch eckIfNotVa lidEntity( EntityRefe rence enti ty) | |||||
| 124 | { | |||||
| 125 | switch ( entity.Log icalName) | |||||
| 126 | { | |||||
| 127 | case "op portunity" : | |||||
| 128 | case "quote": | |||||
| 129 | case "sa lesorder": | |||||
| 130 | case "in voice": | |||||
| 131 | case "op portunityp roduct": | |||||
| 132 | case "in voicedetai l": | |||||
| 133 | case "qu otedetail" : | |||||
| 134 | case "sa lesorderde tail": | |||||
| 135 | return fal se; | |||||
| 136 | ||||||
| 137 | default: | |||||
| 138 | return tru e; | |||||
| 139 | } | |||||
| 140 | } | |||||
| 141 | ||||||
| 142 | #r egion Calc ulate Oppo rtunity Pr ice | |||||
| 143 | // Method to calculate price in an opportu nity | |||||
| 144 | pr ivate stat ic void Ca lculateOpp ortunity(E ntityRefer ence entit y, IOrgani zationServ ice servic e) | |||||
| 145 | { | |||||
| 146 | Entity e = service .Retrieve( entity.Log icalName, entity.Id, new Colum nSet("stat ecode")); | |||||
| 147 | OptionSe tValue sta tecode = ( OptionSetV alue)e["st atecode"]; | |||||
| 148 | if (stat ecode.Valu e == 0) | |||||
| 149 | { | |||||
| 150 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 151 | colu mns.AddCol umns("tota ltax", "to tallineite mamount", "totalamou ntlessfrei ght", "dis countamoun t"); | |||||
| 152 | Enti ty opp = s ervice.Ret rieve(enti ty.Logical Name, enti ty.Id, col umns); | |||||
| 153 | ||||||
| 154 | Quer yExpressio n query = new QueryE xpression( "opportuni typroduct" ); | |||||
| 155 | quer y.ColumnSe t.AddColum ns("quanti ty", "pric eperunit") ; | |||||
| 156 | quer y.Criteria .AddCondit ion("oppor tunityid", Condition Operator.E qual, enti ty.Id); | |||||
| 157 | Enti tyCollecti on ec = se rvice.Retr ieveMultip le(query); | |||||
| 158 | opp[ "totalline itemamount "] = 0; | |||||
| 159 | ||||||
| 160 | deci mal total = 0; | |||||
| 161 | deci mal discou nt = 0; | |||||
| 162 | deci mal tax = 0; | |||||
| 163 | ||||||
| 164 | for (int i = 0 ; i < ec.E ntities.Co unt; i++) | |||||
| 165 | { | |||||
| 166 | total = to tal + ((de cimal)ec.E ntities[i] ["quantity "] * ((Mon ey)ec.Enti ties[i]["p riceperuni t"]).Value ); | |||||
| 167 | (ec.Entiti es[i])["ex tendedamou nt"] = new Money(((d ecimal)ec. Entities[i ]["quantit y"] * ((Mo ney)ec.Ent ities[i][" priceperun it"]).Valu e)); | |||||
| 168 | service.Up date(ec.En tities[i]) ; | |||||
| 169 | } | |||||
| 170 | ||||||
| 171 | opp[ "totalline itemamount "] = new M oney(total ); | |||||
| 172 | ||||||
| 173 | // C alculate d iscount ba sed on the total amo unt | |||||
| 174 | disc ount = Cal culateDisc ount(total ); | |||||
| 175 | tota l = total - discount ; | |||||
| 176 | opp[ "discounta mount"] = new Money( discount); | |||||
| 177 | opp[ "totalamou ntlessfrei ght"] = ne w Money(to tal); | |||||
| 178 | serv ice.Update (opp); | |||||
| 179 | ||||||
| 180 | // C alculate t ax after t he discoun t is appli ed | |||||
| 181 | tax = Calculat eTax(total ); | |||||
| 182 | tota l = total + tax; | |||||
| 183 | opp[ "totaltax" ] = new Mo ney(tax); | |||||
| 184 | opp[ "totalamou nt"] = new Money(tot al); | |||||
| 185 | opp[ "estimated value"] = new Money( total); | |||||
| 186 | serv ice.Update (opp); | |||||
| 187 | } | |||||
| 188 | return; | |||||
| 189 | } | |||||
| 190 | ||||||
| 191 | // Method to calculate extended amount in the produc t line ite ms in an o pportunity | |||||
| 192 | pr ivate stat ic void Ca lculateOpp ortunityPr oduct(Enti tyReferenc e entity, IOrganizat ionService service) | |||||
| 193 | { | |||||
| 194 | try | |||||
| 195 | { | |||||
| 196 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 197 | Enti ty e = ser vice.Retri eve(entity .LogicalNa me, entity .Id, new C olumnSet(" quantity", "priceper unit")); | |||||
| 198 | deci mal total = 0; | |||||
| 199 | tota l = total + ((decima l)e["quant ity"] * (( Money)e["p riceperuni t"]).Value ); | |||||
| 200 | e["e xtendedamo unt"] = ne w Money(to tal); | |||||
| 201 | serv ice.Update (e); | |||||
| 202 | } | |||||
| 203 | catch (F aultExcept ion<Organi zationServ iceFault> ex) | |||||
| 204 | { | |||||
| 205 | Syst em.Diagnos tics.Debug .Write(ex. Message); | |||||
| 206 | } | |||||
| 207 | } | |||||
| 208 | ||||||
| 209 | #e ndregion | |||||
| 210 | ||||||
| 211 | #r egion Calc ulate Quot e Price | |||||
| 212 | // Method to calculate price in a quote | |||||
| 213 | pr ivate stat ic void Ca lculateQuo te(EntityR eference e ntity, IOr ganization Service se rvice) | |||||
| 214 | { | |||||
| 215 | Entity e = service .Retrieve( entity.Log icalName, entity.Id, new Colum nSet("stat ecode")); | |||||
| 216 | OptionSe tValue sta tecode = ( OptionSetV alue)e["st atecode"]; | |||||
| 217 | if (stat ecode.Valu e == 0) | |||||
| 218 | { | |||||
| 219 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 220 | colu mns.AddCol umns("tota ltax", "to tallineite mamount", "totalamou ntlessfrei ght", "dis countamoun t"); | |||||
| 221 | Enti ty quote = service.R etrieve(en tity.Logic alName, en tity.Id, c olumns); | |||||
| 222 | ||||||
| 223 | Quer yExpressio n query = new QueryE xpression( "quotedeta il"); | |||||
| 224 | quer y.ColumnSe t.AddColum ns("quanti ty", "pric eperunit") ; | |||||
| 225 | quer y.Criteria .AddCondit ion("quote id", Condi tionOperat or.Equal, entity.Id) ; | |||||
| 226 | Enti tyCollecti on ec = se rvice.Retr ieveMultip le(query); | |||||
| 227 | quot e["totalli neitemamou nt"] = 0; | |||||
| 228 | ||||||
| 229 | deci mal total = 0; | |||||
| 230 | deci mal discou nt = 0; | |||||
| 231 | deci mal tax = 0; | |||||
| 232 | ||||||
| 233 | for (int i = 0 ; i < ec.E ntities.Co unt; i++) | |||||
| 234 | { | |||||
| 235 | total = to tal + ((de cimal)ec.E ntities[i] ["quantity "] * ((Mon ey)ec.Enti ties[i]["p riceperuni t"]).Value ); | |||||
| 236 | (ec.Entiti es[i])["ex tendedamou nt"] = new Money(((d ecimal)ec. Entities[i ]["quantit y"] * ((Mo ney)ec.Ent ities[i][" priceperun it"]).Valu e)); | |||||
| 237 | service.Up date(ec.En tities[i]) ; | |||||
| 238 | } | |||||
| 239 | ||||||
| 240 | quot e["totalli neitemamou nt"] = new Money(tot al); | |||||
| 241 | ||||||
| 242 | // C alculate d iscount ba sed on the total amo unt | |||||
| 243 | disc ount = Cal culateDisc ount(total ); | |||||
| 244 | tota l = total - discount ; | |||||
| 245 | quot e["discoun tamount"] = new Mone y(discount ); | |||||
| 246 | quot e["totalam ountlessfr eight"] = new Money( total); | |||||
| 247 | serv ice.Update (quote); | |||||
| 248 | ||||||
| 249 | // C alculate t ax after t he discoun t is appli ed | |||||
| 250 | tax = Calculat eTax(total ); | |||||
| 251 | tota l = total + tax; | |||||
| 252 | quot e["totalta x"] = new Money(tax) ; | |||||
| 253 | quot e["totalam ount"] = n ew Money(t otal); | |||||
| 254 | serv ice.Update (quote); | |||||
| 255 | } | |||||
| 256 | return; | |||||
| 257 | } | |||||
| 258 | ||||||
| 259 | // Method to calculate extended amount in the produc t line ite ms in a qu ote | |||||
| 260 | pr ivate stat ic void Ca lculateQuo teProduct( EntityRefe rence enti ty, IOrgan izationSer vice servi ce) | |||||
| 261 | { | |||||
| 262 | try | |||||
| 263 | { | |||||
| 264 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 265 | Enti ty e = ser vice.Retri eve(entity .LogicalNa me, entity .Id, new C olumnSet(" quantity", "priceper unit")); | |||||
| 266 | deci mal total = 0; | |||||
| 267 | tota l = total + ((decima l)e["quant ity"] * (( Money)e["p riceperuni t"]).Value ); | |||||
| 268 | e["e xtendedamo unt"] = ne w Money(to tal); | |||||
| 269 | serv ice.Update (e); | |||||
| 270 | } | |||||
| 271 | catch (F aultExcept ion<Organi zationServ iceFault> ex) | |||||
| 272 | { | |||||
| 273 | Syst em.Diagnos tics.Debug .Write(ex. Message); | |||||
| 274 | } | |||||
| 275 | } | |||||
| 276 | ||||||
| 277 | #e ndregion | |||||
| 278 | ||||||
| 279 | #r egion Calc ulate Orde r Price | |||||
| 280 | // Method to calculate price in an order | |||||
| 281 | pr ivate stat ic void Ca lculateOrd er(EntityR eference e ntity, IOr ganization Service se rvice) | |||||
| 282 | { | |||||
| 283 | Entity e = service .Retrieve( entity.Log icalName, entity.Id, new Colum nSet("stat ecode")); | |||||
| 284 | OptionSe tValue sta tecode = ( OptionSetV alue)e["st atecode"]; | |||||
| 285 | if (stat ecode.Valu e == 0) | |||||
| 286 | { | |||||
| 287 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 288 | colu mns.AddCol umns("tota ltax", "to tallineite mamount", "totalamou ntlessfrei ght", "dis countamoun t"); | |||||
| 289 | Enti ty order = service.R etrieve(en tity.Logic alName, en tity.Id, c olumns); | |||||
| 290 | ||||||
| 291 | Quer yExpressio n query = new QueryE xpression( "salesorde rdetail"); | |||||
| 292 | quer y.ColumnSe t.AddColum ns("quanti ty", "sale sorderispr icelocked" , "pricepe runit"); | |||||
| 293 | quer y.Criteria .AddCondit ion("sales orderid", ConditionO perator.Eq ual, entit y.Id); | |||||
| 294 | ||||||
| 295 | Quer yExpressio n query1 = new Query Expression ("salesord erdetail") ; | |||||
| 296 | quer y1.ColumnS et.AddColu mns("sales orderispri celocked") ; | |||||
| 297 | quer y1.Criteri a.AddCondi tion("sale sorderid", Condition Operator.E qual, enti ty.Id); | |||||
| 298 | ||||||
| 299 | Enti tyCollecti on ec = se rvice.Retr ieveMultip le(query); | |||||
| 300 | Enti tyCollecti on ec1 = s ervice.Ret rieveMulti ple(query1 ); | |||||
| 301 | orde r["totalli neitemamou nt"] = 0; | |||||
| 302 | ||||||
| 303 | deci mal total = 0; | |||||
| 304 | deci mal discou nt = 0; | |||||
| 305 | deci mal tax = 0; | |||||
| 306 | ||||||
| 307 | for (int i = 0 ; i < ec.E ntities.Co unt; i++) | |||||
| 308 | { | |||||
| 309 | total = to tal + ((de cimal)ec.E ntities[i] ["quantity "] * ((Mon ey)ec.Enti ties[i]["p riceperuni t"]).Value ); | |||||
| 310 | (ec1.Entit ies[i])["e xtendedamo unt"] = ne w Money((( decimal)ec .Entities[ i]["quanti ty"] * ((M oney)ec.En tities[i][ "priceperu nit"]).Val ue)); | |||||
| 311 | service.Up date(ec1.E ntities[i] ); | |||||
| 312 | } | |||||
| 313 | ||||||
| 314 | orde r["totalli neitemamou nt"] = new Money(tot al); | |||||
| 315 | ||||||
| 316 | // C alculate d iscount ba sed on the total amo unt | |||||
| 317 | disc ount = Cal culateDisc ount(total ); | |||||
| 318 | tota l = total - discount ; | |||||
| 319 | orde r["discoun tamount"] = new Mone y(discount ); | |||||
| 320 | orde r["totalam ountlessfr eight"] = new Money( total); | |||||
| 321 | serv ice.Update (order); | |||||
| 322 | ||||||
| 323 | // C alculate t ax after t he discoun t is appli ed | |||||
| 324 | tax = Calculat eTax(total ); | |||||
| 325 | tota l = total + tax; | |||||
| 326 | orde r["totalta x"] = new Money(tax) ; | |||||
| 327 | orde r["totalam ount"] = n ew Money(t otal); | |||||
| 328 | serv ice.Update (order); | |||||
| 329 | } | |||||
| 330 | return; | |||||
| 331 | } | |||||
| 332 | ||||||
| 333 | // Method to calculate extended amount in the produc t line ite ms in a or der | |||||
| 334 | pr ivate stat ic void Ca lculateOrd erProduct( EntityRefe rence enti ty, IOrgan izationSer vice servi ce) | |||||
| 335 | { | |||||
| 336 | try | |||||
| 337 | { | |||||
| 338 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 339 | Enti ty e = ser vice.Retri eve(entity .LogicalNa me, entity .Id, new C olumnSet(" quantity", "priceper unit", "sa lesorderis pricelocke d")); | |||||
| 340 | Enti ty e1 = se rvice.Retr ieve(entit y.LogicalN ame, entit y.Id, new ColumnSet( "quantity" , "salesor derisprice locked")); | |||||
| 341 | deci mal total = 0; | |||||
| 342 | tota l = total + ((decima l)e["quant ity"] * (( Money)e["p riceperuni t"]).Value ); | |||||
| 343 | e1[" extendedam ount"] = n ew Money(t otal); | |||||
| 344 | serv ice.Update (e1); | |||||
| 345 | } | |||||
| 346 | catch (F aultExcept ion<Organi zationServ iceFault> ex) | |||||
| 347 | { | |||||
| 348 | Syst em.Diagnos tics.Debug .Write(ex. Message); | |||||
| 349 | } | |||||
| 350 | } | |||||
| 351 | ||||||
| 352 | #e ndregion | |||||
| 353 | ||||||
| 354 | #r egion Calc ulate Invo ice Price | |||||
| 355 | // Method to calculate price in an invoice | |||||
| 356 | pr ivate stat ic void Ca lculateInv oice(Entit yReference entity, I Organizati onService service) | |||||
| 357 | { | |||||
| 358 | Entity e = service .Retrieve( entity.Log icalName, entity.Id, new Colum nSet("stat ecode")); | |||||
| 359 | OptionSe tValue sta tecode = ( OptionSetV alue)e["st atecode"]; | |||||
| 360 | if (stat ecode.Valu e == 0) | |||||
| 361 | { | |||||
| 362 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 363 | colu mns.AddCol umns("tota ltax", "to tallineite mamount", "totalamou ntlessfrei ght", "dis countamoun t"); | |||||
| 364 | ||||||
| 365 | Enti ty invoice = service .Retrieve( entity.Log icalName, entity.Id, columns); | |||||
| 366 | ||||||
| 367 | Quer yExpressio n query = new QueryE xpression( "invoicede tail"); | |||||
| 368 | quer y.ColumnSe t.AddColum ns("quanti ty", "invo iceisprice locked", " priceperun it"); | |||||
| 369 | quer y.Criteria .AddCondit ion("invoi ceid", Con ditionOper ator.Equal , entity.I d); | |||||
| 370 | ||||||
| 371 | Quer yExpressio n query1 = new Query Expression ("invoiced etail"); | |||||
| 372 | quer y1.ColumnS et.AddColu mns("quant ity", "inv oiceispric elocked"); | |||||
| 373 | quer y1.Criteri a.AddCondi tion("invo iceid", Co nditionOpe rator.Equa l, entity. Id); | |||||
| 374 | ||||||
| 375 | Enti tyCollecti on ec = se rvice.Retr ieveMultip le(query); | |||||
| 376 | Enti tyCollecti on ec1 = s ervice.Ret rieveMulti ple(query1 ); | |||||
| 377 | ||||||
| 378 | invo ice["total lineitemam ount"] = 0 ; | |||||
| 379 | ||||||
| 380 | deci mal total = 0; | |||||
| 381 | deci mal discou nt = 0; | |||||
| 382 | deci mal tax = 0; | |||||
| 383 | ||||||
| 384 | for (int i = 0 ; i < ec.E ntities.Co unt; i++) | |||||
| 385 | { | |||||
| 386 | total = to tal + ((de cimal)ec.E ntities[i] ["quantity "] * ((Mon ey)ec.Enti ties[i]["p riceperuni t"]).Value ); | |||||
| 387 | (ec1.Entit ies[i])["e xtendedamo unt"] = ne w Money((( decimal)ec .Entities[ i]["quanti ty"] * ((M oney)ec.En tities[i][ "priceperu nit"]).Val ue)); | |||||
| 388 | service.Up date(ec1.E ntities[i] ); | |||||
| 389 | } | |||||
| 390 | ||||||
| 391 | invo ice["total lineitemam ount"] = n ew Money(t otal); | |||||
| 392 | ||||||
| 393 | // C alculate d iscount ba sed on the total amo unt | |||||
| 394 | disc ount = Cal culateDisc ount(total ); | |||||
| 395 | tota l = total - discount ; | |||||
| 396 | invo ice["disco untamount" ] = new Mo ney(discou nt); | |||||
| 397 | invo ice["total amountless freight"] = new Mone y(total); | |||||
| 398 | serv ice.Update (invoice); | |||||
| 399 | ||||||
| 400 | // C alculate t ax after t he discoun t is appli ed | |||||
| 401 | tax = Calculat eTax(total ); | |||||
| 402 | tota l = total + tax; | |||||
| 403 | invo ice["total tax"] = ne w Money(ta x); | |||||
| 404 | invo ice["total amount"] = new Money (total); | |||||
| 405 | serv ice.Update (invoice); | |||||
| 406 | } | |||||
| 407 | return; | |||||
| 408 | } | |||||
| 409 | ||||||
| 410 | // Method to calculate extended amount in the produc t line ite ms in an i nvoice | |||||
| 411 | pr ivate stat ic void Ca lculateInv oiceProduc t(EntityRe ference en tity, IOrg anizationS ervice ser vice) | |||||
| 412 | { | |||||
| 413 | try | |||||
| 414 | { | |||||
| 415 | Colu mnSet colu mns = new ColumnSet( ); | |||||
| 416 | Enti ty e = ser vice.Retri eve(entity .LogicalNa me, entity .Id, new C olumnSet(" quantity", "priceper unit", "in voiceispri celocked") ); | |||||
| 417 | Enti ty e1 = se rvice.Retr ieve(entit y.LogicalN ame, entit y.Id, new ColumnSet( "quantity" , "invoice ispriceloc ked")); | |||||
| 418 | deci mal total = 0; | |||||
| 419 | tota l = total + ((decima l)e["quant ity"] * (( Money)e["p riceperuni t"]).Value ); | |||||
| 420 | e1[" extendedam ount"] = n ew Money(t otal); | |||||
| 421 | serv ice.Update (e1); | |||||
| 422 | } | |||||
| 423 | catch (F aultExcept ion<Organi zationServ iceFault> ex) | |||||
| 424 | { | |||||
| 425 | Syst em.Diagnos tics.Debug .Write(ex. Message); | |||||
| 426 | } | |||||
| 427 | } | |||||
| 428 | ||||||
| 429 | #e ndregion | |||||
| 430 | ||||||
| 431 | ||||||
| 432 | // Method to calculate discount. | |||||
| 433 | pr ivate stat ic decimal Calculate Discount(d ecimal amo unt) | |||||
| 434 | { | |||||
| 435 | decimal discount = 0; | |||||
| 436 | ||||||
| 437 | if (amou nt > (deci mal)1000.0 0 && amoun t < (decim al)5000.00 ) | |||||
| 438 | { | |||||
| 439 | disc ount = amo unt * (dec imal)0.05; | |||||
| 440 | } | |||||
| 441 | else if (amount >= (decimal) 5000.00) | |||||
| 442 | { | |||||
| 443 | disc ount = amo unt * (dec imal)0.10; | |||||
| 444 | } | |||||
| 445 | return d iscount; | |||||
| 446 | } | |||||
| 447 | ||||||
| 448 | // Method to calculate tax. | |||||
| 449 | pr ivate stat ic decimal Calculate Tax(decima l amount) | |||||
| 450 | { | |||||
| 451 | decimal tax = 0; | |||||
| 452 | if (amou nt < (deci mal)5000.0 0) | |||||
| 453 | { | |||||
| 454 | tax = amount * (decimal) 0.10; | |||||
| 455 | } | |||||
| 456 | else | |||||
| 457 | { | |||||
| 458 | tax = amount * (decimal) 0.08; | |||||
| 459 | } | |||||
| 460 | return t ax; | |||||
| 461 | } | |||||
| 462 | } | |||||
| 463 | } | |||||
| 464 | //</snippe tCalculate PricePlugi n> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.