Advertisement
febripratama

IsPublished

Jun 1st, 2020
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.48 KB | None | 0 0
  1.  
  2.                         if (model.IsPublished)
  3.                         {
  4.                             if (model.PromoQuota.ToJSON() != promo.ResetQuota)//cek bagan resetquota, berubah atau tidak
  5.                             {
  6.                                 if (!string.IsNullOrEmpty(promo.HistoryResetQuota))
  7.                                 {
  8.                                     List<PromoQuotaModel> HistoryResetQuota = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PromoQuotaModel>>(promo.HistoryResetQuota);
  9.                                     int TransMax = 0;
  10.                                     int.TryParse(promo.TRANSAKSIMAX, out TransMax);
  11.                                     var ListUsed = ProductTransactionQuota.GetListNonExpiredByPromoCode(promo.KodePromo);
  12.                                     if (model.PromoQuota.ResetQuotaType == Constant.ResetQuotaPromoType.Manual && TransMax > 0)
  13.                                     {
  14.                                         int QuotaUsed = ListUsed.Sum(x => x.Quantity);
  15.                                         int TotalPrev = 0;
  16.                                         if (model.PromoQuota.Accumulation == true)
  17.                                         {
  18.                                             int prev = HistoryResetQuota.Where(x => x.ResetQuotaType == Constant.ResetQuotaPromoType.Auto).Sum(x => x.AutoResetPromoQuotaDetail.Sum(y => y.Quota));
  19.                                             prev += HistoryResetQuota.Where(x => x.ResetQuotaType != Constant.ResetQuotaPromoType.Auto).Sum(x => x.TotalResetQuota);
  20.                                             TotalPrev = prev - QuotaUsed;
  21.                                         }
  22.                                         else
  23.                                         {
  24.                                             int prev = HistoryResetQuota.Where(x => x.ResetQuotaType == Constant.ResetQuotaPromoType.Auto).Sum(x => x.AutoResetPromoQuotaDetail.Sum(y => y.Quota));
  25.                                             prev += HistoryResetQuota.Where(x => x.ResetQuotaType != Constant.ResetQuotaPromoType.Auto).Sum(x => x.TotalResetQuota);
  26.                                             TotalPrev = prev;
  27.                                         }
  28.  
  29.                                         if (!String.IsNullOrEmpty(promo.ResetQuota))
  30.                                         {
  31.                                             PromoQuotaModel RecentResetQuota = Newtonsoft.Json.JsonConvert.DeserializeObject<PromoQuotaModel>(promo.ResetQuota);
  32.                                             if (RecentResetQuota.StartDatePromo <= DateTime.Now && RecentResetQuota.EndDatePromo >= DateTime.Now)
  33.                                             {
  34.                                                 CookieManager.Set(CookieManager.CMS_GLOBAL_ERROR, "Reset promo sebelumnya sedang aktif", false);
  35.                                                 return RedirectToAction(MVC.Promo.Edit(promo.ID));
  36.                                             }
  37.                                         }
  38.  
  39.                                         if (TotalPrev + model.PromoQuota.TotalResetQuota > TransMax)
  40.                                         {
  41.                                             CookieManager.Set(CookieManager.CMS_GLOBAL_ERROR, "Total quota reset dengan reset sebelumnya tidak boleh melebihi transaksi max promo utama", false);
  42.                                             return RedirectToAction(MVC.Promo.Edit(promo.ID));
  43.                                         }
  44.                                         else
  45.                                         {
  46.                                             //if (model.PromoQuota.Accumulation == true)
  47.                                             //{
  48.                                             //    promo.TotalQuota = model.PromoQuota.TotalResetQuota + QuotaUsed;
  49.                                             //}
  50.                                             //else
  51.                                             //{
  52.                                             //    promo.TotalQuota = model.PromoQuota.TotalResetQuota;
  53.                                             //}
  54.                                         }
  55.                                     }
  56.                                     else
  57.                                     {
  58.                                         ////auto
  59.                                         //promo.TotalQuota = model.PromoQuota.TotalResetQuota;
  60.  
  61.                                         int QuotaUsed = ListUsed.Sum(x => x.Quantity);
  62.                                         int TotalPrev = 0;
  63.                                         if (model.PromoQuota.Accumulation == true)
  64.                                         {
  65.                                             int prev = HistoryResetQuota.Where(x => x.ResetQuotaType == Constant.ResetQuotaPromoType.Auto).Sum(x => x.AutoResetPromoQuotaDetail.Sum(y => y.Quota));
  66.                                             prev += HistoryResetQuota.Where(x => x.ResetQuotaType != Constant.ResetQuotaPromoType.Auto).Sum(x => x.TotalResetQuota);
  67.                                             TotalPrev = prev - QuotaUsed;
  68.                                         }
  69.                                         else
  70.                                         {
  71.                                             int prev = HistoryResetQuota.Where(x => x.ResetQuotaType == Constant.ResetQuotaPromoType.Auto).Sum(x => x.AutoResetPromoQuotaDetail.Sum(y => y.Quota));
  72.                                             prev += HistoryResetQuota.Where(x => x.ResetQuotaType != Constant.ResetQuotaPromoType.Auto).Sum(x => x.TotalResetQuota);
  73.                                             TotalPrev = prev;
  74.                                         }
  75.  
  76.  
  77.                                         if (!String.IsNullOrEmpty(promo.ResetQuota))
  78.                                         {
  79.                                             PromoQuotaModel RecentResetQuota = Newtonsoft.Json.JsonConvert.DeserializeObject<PromoQuotaModel>(promo.ResetQuota);
  80.                                             if (RecentResetQuota.AutoResetPromoQuotaDetail != null)
  81.                                             {
  82.                                                 foreach (var item in RecentResetQuota.AutoResetPromoQuotaDetail)
  83.                                                     if (item.StartDate <= DateTime.Now && item.EndDate >= DateTime.Now)
  84.                                                     {
  85.                                                         CookieManager.Set(CookieManager.CMS_GLOBAL_ERROR, "Reset promo sebelumnya sedang aktif", false);
  86.                                                         return RedirectToAction(MVC.Promo.Edit(promo.ID));
  87.                                                     }
  88.                                             }
  89.                                         }
  90.  
  91.                                         int QuotaWillBeReset = model.PromoQuota.AutoResetPromoQuotaDetail.Sum(x => x.Quota);
  92.                                         if (TotalPrev + QuotaWillBeReset > TransMax)
  93.                                         {
  94.                                             CookieManager.Set(CookieManager.CMS_GLOBAL_ERROR, "Total quota reset dengan reset sebelumnya tidak boleh melebihi transaksi max promo utama", false);
  95.                                             return RedirectToAction(MVC.Promo.Edit(promo.ID));
  96.                                         }
  97.                                     }
  98.                                     model.PromoQuota.IsExecuted = false;
  99.                                     promo.ResetQuota = model.PromoQuota.ToJSON().ToString();
  100.  
  101.                                     ////delete all used quota transaction
  102.                                     //foreach (var itemList in ListUsed)
  103.                                     //{
  104.                                     //    itemList.IsDeleted = true;                                    
  105.                                     //    itemList.Update(CookieManager.Get(CookieManager.USERID).ToString());
  106.                                     //}
  107.  
  108.                                     HistoryResetQuota.Add(model.PromoQuota);
  109.                                     promo.HistoryResetQuota = HistoryResetQuota.ToJSON();
  110.                                 }
  111.                                 else
  112.                                 {
  113.                                     if ((model.PromoQuota.ResetQuotaType == Constant.ResetQuotaPromoType.Manual || (model.PromoQuota.ResetQuotaType == Constant.ResetQuotaPromoType.Auto && model.PromoQuota.CategoryReset == Constant.ResetQuotaPromoAutoType.Perhari)) && (model.PromoQuota.StartDatePromo == DateTime.MinValue || model.PromoQuota.EndDatePromo == DateTime.MinValue))
  114.                                     {
  115.                                         CookieManager.Set(CookieManager.CMS_GLOBAL_ERROR, "Tanggal awal atau tanggal akhir reset promo harap diisi terlebih dahulu", false);
  116.                                         return RedirectToAction(MVC.Promo.Edit(promo.ID));
  117.                                     }
  118.  
  119.                                     List<PromoQuotaModel> HistoryResetQuota = new List<PromoQuotaModel>();
  120.                                     promo.ResetQuota = model.PromoQuota.ToJSON().ToString();
  121.                                     HistoryResetQuota.Add(model.PromoQuota);
  122.                                     promo.HistoryResetQuota = HistoryResetQuota.ToJSON().ToString();
  123.                                 }
  124.                             }
  125.                         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement