private TotalPromotion ComparePromotions(TotalPromotion tp1, TotalPromotion tp2, decimal promotedTotal) { decimal discountPromo1 = Math.Max(tp1.Discount, tp1.TotalAmount() * tp1.PercentDiscount / 100m); decimal discountPromo2 = Math.Max(tp2.Discount, tp2.TotalAmount() * tp2.PercentDiscount / 100m); if (discountPromo2 == discountPromo1) { TotalPromotion ep = tp2.Points + tp2.ExtraPoints > tp1.Points + tp1.ExtraPoints ? tp2 : tp1; return(ep); } TotalPromotion bp = discountPromo2 > discountPromo1 ? tp2 : tp1; if (bp.RequiredAmount <= promotedTotal) { return(bp); } TotalPromotion sp = discountPromo2 < discountPromo1 ? tp2 : tp1; if (sp.RequiredAmount > promotedTotal) { return(bp); } return(Math.Max(sp.Discount, sp.TotalAmount() * sp.PercentDiscount / 100m) + totalDiscount > Math.Max(bp.Discount, bp.TotalAmount() * bp.PercentDiscount / 100m) ? sp : bp); }
private TotalPromotion BestPointPromotion(decimal promotedTotal) { TotalPromotion bestPointPromotion = null; foreach (IPromotion pr in Settings.TotalPromotions) { if (!pr.PromotionCoverAll(this.promotionRange)) { continue; } if (!pr.CheckPaymentType(paymentType)) { continue; } if ((((TotalPromotion)pr).PercentDiscount == 0) && (((TotalPromotion)pr).Discount == 0)) { if (pr is TotalPromotion && ((TotalPromotion)pr).RequiredAmount <= promotedTotal) { bestPointPromotion = bestPointPromotion == null ? (TotalPromotion)pr : ComparePromotionsPoints(bestPointPromotion, (TotalPromotion)pr, promotedTotal); } } } return(bestPointPromotion); }
private TotalPromotion ComparePromotionsPoints(TotalPromotion tp1, TotalPromotion tp2, decimal promotedTotal) { int ratio1 = (int)(promotedTotal / tp1.RequiredAmount); int ratio2 = (int)(promotedTotal / tp2.RequiredAmount); return(tp1.Points * ratio1 > tp2.Points * ratio2 ? tp1 : tp2); }
internal static void Load() { if (!Directory.Exists(ErrorPath)) { Directory.CreateDirectory(ErrorPath); } SalePromotions = new List <IPromotion>(); TotalPromotions = new List <IPromotion>(); try { TotalPromotion.Load(); } catch (Exception ex) { Settings.Log(String.Format("Promo.dat dosyasi yuklenemedi!\r{0}", ex.Message)); } #if !WindowsCE try { ProductPromotion.Load(); } catch (Exception ex) { Settings.Log(String.Format("Prourun.dat dosyasi yuklenemedi!\r{0}", ex.Message)); } try { CategoryPromotion.Load(); } catch (Exception ex) { Settings.Log(String.Format("Proreyon.dat dosyasi yuklenemedi!\r{0}", ex.Message)); } try { SpecialPromotion.Load(); } catch (Exception ex) { Settings.Log(String.Format("Ozelpromo.dat dosyasi yuklenemedi!\r{0}", ex.Message)); } #endif try { FileWatcher.Start(); } catch (Exception ex) { Settings.Log(String.Format("Promosyon dosyasý bulunamadý.\r{0}", ex.Message)); } }
static void LoadChangedFile(string filePath) { string fileName = filePath.Substring(filePath.LastIndexOfAny(new char[] { '\\', '/' }) + 1); int fileContentHashCode = String.Empty.GetHashCode(); StreamReader sr = null; string context; using (sr = new StreamReader(filePath, Settings.DefaultEncoding)) { context = sr.ReadToEnd(); } fileContentHashCode = context.GetHashCode(); if (contentHashCodes.ContainsKey(fileName)) { if (contentHashCodes[fileName] == fileContentHashCode) { return; } contentHashCodes.Remove(fileName); } Settings.Log("File is changed : " + fileName); try { switch (fileName) { case Settings.PRODUCT_PROMOTION_FILE_NAME: case Settings.CATEGORY_PROMOTION_FILE_NAME: Settings.SalePromotions = new List <IPromotion>(); CategoryPromotion.Load(); ProductPromotion.Load(); break; case Settings.TOTAL_PROMOTION_FILE_NAME: case Settings.SPECIAL_PROMOTION_FILE_NAME: Settings.TotalPromotions = new List <IPromotion>(); TotalPromotion.Load(); SpecialPromotion.Load(); break; } contentHashCodes.Add(fileName, fileContentHashCode); } catch (Exception ex) { Settings.Log(ex.Message); } }
private TotalPromotion BestGiftPromotion(List <TotalPromotion> giftPromotions, decimal promotedAmount) { TotalPromotion bestPromotion = null; foreach (TotalPromotion tp in giftPromotions) { IProduct giftProduct = FindProductByLabel(tp.GiftProductLabelNo); Decimal lineQuantity = soldItems.LineTotal(tp.GiftProductLabelNo); if (giftProduct != null && lineQuantity > 0 && tp.RequiredAmount <= promotedAmount) { bestPromotion = bestPromotion == null ? tp : ComparePromotions(bestPromotion, tp, promotedAmount); } } return(bestPromotion); }
internal String[] CreatePromotionList() { if (documentTotalAmount == 0) { return new String[] { } } ; List <String> promoRemarks = new List <string>(); long pointsEarned = 0; ICustomer customer = null; if (SecurityConnector.CurrentCustomer != null && customerCode == SecurityConnector.CurrentCustomer.Code) { customer = SecurityConnector.CurrentCustomer; } if (customer != null) { remarks.Add("M", customer.Name); } foreach (IPromotion pr in Settings.SalePromotions) { if (!pr.PromotionCoverAll(this.promotionRange)) { continue; } if (this.promotionRange == PromotionRange.CUSTOMER) { if (customer == null || !((BasePromotion)pr).CheckCustomerGroup(customer)) { continue; } } BasePromotion basePRM = (BasePromotion)pr; IProduct giftProduct = null; if (basePRM.GiftProductLabelNo > 0) { giftProduct = FindProductByLabel(basePRM.GiftProductLabelNo); } if (basePRM.LimitType == LimitType.NoLimit && giftProduct == null && basePRM.Points == 0) { continue; } //compare promotions with requirement totals if (CheckLimit(basePRM)) { soldItems.SetPromotion(basePRM, false); if (giftProduct != null && soldItems.LineTotal(basePRM.GiftProductLabelNo) > 0m) { if (basePRM.LimitType == LimitType.Amount) { soldItems.GiveAsPromotion(basePRM); } else if (soldItems.LineTotal(basePRM) >= basePRM.RequiredQuantity) { //calculates how many times promotion will applied int giftRatio = (int)(soldItems.LineTotal(basePRM) / basePRM.RequiredQuantity); for (int i = 0; i < giftRatio; i++) { soldItems.GiveAsPromotion(basePRM); } } } } } //Apply product and category promotion to document BasePromotion[] tempPromotions = soldItems.Promotions; foreach (BasePromotion prm in tempPromotions) { ((IPromotion)prm).ApplyPromotion(); pointsEarned += ((IPromotion)prm).PointEarned; } //Gets total discount amount totalDiscount = soldItems.TotalDiscount(); List <String> promoLines = soldItems.PromotedLines; String promocodeApplied = ""; List <TotalPromotion> giftPromotions = new List <TotalPromotion>(); //define best total promotion TotalPromotion bestTotalPromotion = null; //Decimal promotedTotal = paymentAmount - totalDiscount; Decimal promotedTotal = documentTotalAmount - totalDiscount; foreach (IPromotion pr in Settings.TotalPromotions) { if (!pr.PromotionCoverAll(this.promotionRange)) { continue; } if (!pr.CheckPaymentType(paymentType)) { continue; } if ((this.promotionRange == PromotionRange.CUSTOMER) && (customer == null || !((TotalPromotion)pr).CheckCustomerGroup(customer))) { continue; } TotalPromotion totalPromo = (TotalPromotion)pr; if (totalPromo.GiftProductLabelNo > 0) { giftPromotions.Add((TotalPromotion)pr); #if ISKULTUR IProduct product = FindProductByLabel(totalPromo.GiftProductLabelNo); Decimal quantity = soldItems.LineTotal(totalPromo.GiftProductLabelNo); Decimal amountWithoutProduct = promotedTotal - product.UnitPrice * quantity; if (amountWithoutProduct >= totalPromo.RequiredAmount) { promotedTotal = amountWithoutProduct; } #endif } else { Decimal requiredAmount = totalPromo.HavePayment ? promotedTotal : (documentTotalAmount - totalDiscount); //select best total promotion as discount amount if (totalPromo.RequiredAmount <= requiredAmount) { bestTotalPromotion = bestTotalPromotion == null ? totalPromo : ComparePromotions(bestTotalPromotion, totalPromo, documentTotalAmount); } } } Decimal discountTotalPromotion = 0, discountCustomerPromotion = 0; if (bestTotalPromotion != null) { // Promosyonun ödeme tipine baðlý deðilse promosyon ödeme parçalý olsa da aratoplama uygulanýyor. //if (!bestTotalPromotion.HavePayment) //{ // paymentAmount = documentTotalAmount; // promotedTotal = paymentAmount - totalDiscount; //} } //Qunatity to be promotion applied int ratio = 0; if (bestTotalPromotion != null) { ratio = (int)((promotedTotal) / bestTotalPromotion.RequiredAmount); discountTotalPromotion = Math.Max(bestTotalPromotion.Discount, promotedTotal * bestTotalPromotion.PercentDiscount / 100m); if (bestTotalPromotion.RequiredAmount <= promotedTotal) { discountTotalPromotion = Math.Max(bestTotalPromotion.Discount * ratio, promotedTotal * bestTotalPromotion.PercentDiscount / 100m); } else { discountTotalPromotion = discountTotalPromotion > totalDiscount ? discountTotalPromotion : 0; } //pointsEarned += (bestTotalPromotion.Points + bestTotalPromotion.ExtraPoints) * ratio; } //Apply gift product promotions Decimal promoReqAmount = promotedTotal; #if ISKULTUR promoReqAmount -= discountTotalPromotion; #endif while (true) { TotalPromotion tp = BestGiftPromotion(giftPromotions, promoReqAmount); if (tp == null) { break; } ratio = (int)((promoReqAmount) / tp.RequiredAmount); IProduct giftProduct = FindProductByLabel(tp.GiftProductLabelNo); Decimal lineQuantity = soldItems.LineTotal(tp.GiftProductLabelNo); for (int i = 0; i < ratio && lineQuantity > 0; i++) { promoReqAmount -= tp.RequiredAmount; decimal lineDiscount = Math.Max(tp.Discount, giftProduct.UnitPrice * tp.PercentDiscount / 100m); long linePoint = (tp.Points + tp.ExtraPoints); if (lineQuantity < 1) { lineDiscount = lineDiscount * lineQuantity; linePoint = (long)(linePoint * lineQuantity); } #if ISKULTUR promoLines.Add( String.Format("06,IND,SAT {0:D4} %{1:D2}, {2:D10}", soldItems.GetSalesID((BasePromotion)tp)[i], (int)0, (int)Math.Round(lineDiscount * 100, 0))); if (promoLines.Count > 0) { promoLines.Add(String.Format("00,KOD,{0}", Settings.PromoKey)); } for (int cntr = 0; cntr < promoLines.Count; cntr++) { promoLines[cntr] = String.Format("1,{0:D5},{1}", cntr + 1, promoLines[cntr]); } pointsEarned += linePoint; lineQuantity = soldItems.LineTotal(tp.GiftProductLabelNo); #else discountTotalPromotion += lineDiscount; pointsEarned += linePoint; soldItems.GiveAsPromotion(tp); lineQuantity = soldItems.LineTotal(tp.GiftProductLabelNo); #endif } } #if ISKULTUR promotedTotal = documentTotalAmount; #endif promotedTotal -= discountTotalPromotion; String[] appliedCodes = soldItems.AppliedCodes; foreach (String appliedCode in appliedCodes) { if (appliedCode.Length > 0 && !Str.Contains(promocodeApplied, (appliedCode.Trim() + ","))) { promocodeApplied = promocodeApplied + appliedCode.Trim(); } } TotalPromotion bestPointPromo = null; if (customer != null) { discountCustomerPromotion = documentTotalAmount * customer.PromotionLimit / 100m; } promotedTotal -= discountCustomerPromotion; if (discountCustomerPromotion > 0) { promocodeApplied += "M"; } if (bestTotalPromotion != null) { promocodeApplied += bestTotalPromotion.PromotionCode; } //decimal ttlDiscount = paymentAmount - promotedTotal - totalDiscount; decimal ttlDiscount = 0; if (isFirstPayment) { ttlDiscount = documentTotalAmount - promotedTotal - totalDiscount; } if (paymentAmount > promotedTotal && isFirstPayment) { paymentAmount = promotedTotal; } bestPointPromo = BestPointPromotion(paymentAmount); if (bestPointPromo != null) { int pointMultiple = (int)((paymentAmount) / bestPointPromo.RequiredAmount); pointsEarned += (bestPointPromo.Points + bestPointPromo.ExtraPoints) * pointMultiple; if (!bestPointPromo.Equals(bestTotalPromotion)) { promocodeApplied += bestPointPromo.PromotionCode; } } #if ISKULTUR int startIndex = promoLines.Count; #else int startIndex = 0; #endif if (ttlDiscount > 0) { promoLines.Add(String.Format("06,IND,TOP {0:D4} %, {1:D10}", 0, (Int64)(ttlDiscount * 100)));//%00 } if (promocodeApplied.Length > 0) { promoLines.Add(String.Format("22,MSG,{0} {1}", PosMessage.PROMOTION_CODE, promocodeApplied)); } if (promoRemarks.Count > 0) { foreach (string pr in promoRemarks) { promoLines.Add("29,NOT," + pr); } } if (pointsEarned > 0 && customer != null && !(Str.Contains(customer.Code, "*"))) { promoLines.Add(String.Format("22,PRM,{0:D9}", pointsEarned)); } if (customer != null && int.Parse(customer.Number) > 0) { promoLines.Add(String.Format("23,PNT,{0:D9}", customer.Number)); } if (promoLines.Count > 0) { promoLines.Add(String.Format("00,KOD,{0}", Settings.PromoKey)); } for (int cntr = startIndex; cntr < promoLines.Count; cntr++) { promoLines[cntr] = String.Format("1,{0:D5},{1}", cntr + 1, promoLines[cntr]); } return(promoLines.ToArray()); }
internal static void Load() { StreamReader sr = null; Exception lastException = null; TotalPromotion pro; try { sr = new StreamReader(Settings.DataPath + Settings.TOTAL_PROMOTION_FILE_NAME, Settings.DefaultEncoding); } catch (FileNotFoundException fnfe) { if (sr != null) { sr.Close(); } throw new InvalidDataException(fnfe.Message); } using (sr) { String line = String.Empty; String[] lineArray; int lineNo = 0; while ((line = sr.ReadLine()) != null) { try { if (line.Trim().Length == 0) { break; } lineArray = line.Split(','); pro = new TotalPromotion(); lineNo++; switch (lineArray[0]) { case "H": pro.range = PromotionRange.ALL; break; case "M": pro.range = PromotionRange.CUSTOMER; break; case "I": pro.range = PromotionRange.ALL; pro.limitType = LimitType.Amount; break; case "N": pro.range = PromotionRange.CUSTOMER; pro.limitType = LimitType.Amount; break; } pro.SetDateRange(lineArray[1], lineArray[2], lineArray[3], lineArray[4]); pro.requiredAmount = Decimal.Parse(lineArray[5]) / 100m; pro.percentDiscount = Int32.Parse(lineArray[6]) == -1 ? 100 : Int32.Parse(lineArray[6]); pro.code = "T" + (lineNo).ToString(); pro.discount = decimal.Parse(lineArray[7]) / 100m; pro.paymentTypes = new List <string>(); pro.customerGroups = new List <string>(); pro.point = Int32.Parse(lineArray[8]); pro.extraPoint = Int32.Parse(lineArray[9]); long groupId; for (int counter = 10; counter < lineArray.Length; counter++) { if (lineArray[counter] == String.Empty) { continue; } if (lineArray[counter].StartsWith("M")) { if (Parser.TryLong(lineArray[counter].Substring(1), out groupId)) { pro.customerGroups.Add(lineArray[counter].Trim().Substring(1)); } } else if (lineArray[counter].Length == 7 && lineArray[counter][0] == 'P') { Parser.TryInt(lineArray[counter].Substring(1), out pro.giftProductLabelNo); pro.giftProductQuantity = 1; } else { pro.paymentTypes.Add(lineArray[counter]); } } Settings.TotalPromotions.Add(pro); } catch (ParameterRelationException) { //Date range is invalid } catch (Exception e) { lastException = e; Settings.Log("Promo.dat okuma hatasi! " + e.Message); continue; } } } if (lastException != null) { throw new InvalidDataException(lastException.Message); } }