Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Linq;
- using BST.LionMarrodentWorkers.Extensions;
- using BST.LionMarrodentWorkers.Triggery;
- using Soneta.Business;
- using Soneta.Business.Db;
- using Soneta.Handel;
- using Soneta.Towary;
- using Soneta.Types;
- [assembly: ModuleType("DiscountTriggerModule", typeof(DiscountTriggerModule), 4, "", 0)]
- namespace BST.LionMarrodentWorkers.Triggery
- {
- public sealed class DiscountTriggerModule : Module
- {
- //CTOR
- public DiscountTriggerModule(Session session) : base(session)
- {
- DiscountTrigger trigger = new DiscountTrigger();
- }
- }
- public sealed class DiscountTrigger
- {
- //CTOR
- static DiscountTrigger()
- {
- HandelModule.PozycjaDokHandlowegoSchema.AddTowarAfterEdit(ProductEdit);
- }
- //Private
- private static void ProductEdit(HandelModule.PozycjaDokHandlowegoRow row)
- {
- if (row.Session.Login.Operator.Features.GetBool("CZ_User") && row.Dokument.Bufor && row.Towar != null && row.Dokument.Kontrahent != null)
- {
- if (row.Towar.HlavniSkupina() && row.Dokument.IsCzechSaleOrder())
- {
- row.Rabat = Percent.Zero;
- }
- else
- {
- //Variables
- BusinessModule bm = BusinessModule.GetInstance(row.Session);
- TowaryModule tm = TowaryModule.GetInstance(row.Session);
- Percent max = Percent.Zero;
- //Check product group
- DictionaryItem item = bm.Dictionary.CreateView()
- ?.OriginalSubTable[new FieldCondition.Equal("Category", "F.CZ_GroupOfGood")]
- ?.Cast<DictionaryItem>()
- ?.FirstOrDefault(x=> x.Value == row.Towar.Features.GetString("CZ_GroupOfGoods").Replace(@"\", string.Empty));
- if (item != null)
- {
- SubTable<CenaGrupowa> discounts = tm.CenyGrupowe.WgGrupaTowarowa[item, row.Dokument.Kontrahent];
- if (discounts != null && discounts.Any())
- {
- Percent tempMax = discounts.Max(x => x.Rabat);
- if (tempMax > max)
- {
- max = tempMax;
- }
- }
- }
- //Check manufacturer
- DictionaryItem item2 = bm.Dictionary.CreateView()
- ?.OriginalSubTable[new FieldCondition.Equal("Category", "F.CZ_Manufacture")]
- ?.Cast<DictionaryItem>()
- ?.FirstOrDefault(x=> x.Value == row.Towar.Features.GetString("CZ_Manufacturer").Replace(@"\", string.Empty));
- if (item2 != null)
- {
- SubTable<CenaGrupowa> discounts = tm.CenyGrupowe.WgGrupaTowarowa[item2, row.Dokument.Kontrahent];
- if (discounts != null && discounts.Any())
- {
- Percent tempMax = discounts.Max(x => x.Rabat);
- if (tempMax > max)
- {
- max = tempMax;
- }
- }
- }
- //Check period general
- RowCondition rc = new FieldCondition.GreaterEqual("PrzecenaOkresowa.Okres.To", row.Dokument.Data);
- rc &= new FieldCondition.LessEqual("PrzecenaOkresowa.Okres.From", row.Dokument.Data);
- rc &= new FieldCondition.Null("PrzecenaOkresowa.Kontrahent", true);
- SubTable<PrzecenaOkresowaTowaru> discounts2 = tm.PrzecenyOkresTwr.WgTowar[row.Towar][rc];
- if (discounts2 != null && discounts2.Any)
- {
- foreach (PrzecenaOkresowaTowaru discount in discounts2)
- {
- if (discount.Rabat > max)
- {
- max = discount.Rabat;
- }
- }
- }
- //Check manufacturer and period individual
- RowCondition rc2 = new FieldCondition.GreaterEqual("Okres.To", row.Dokument.Data);
- rc2 &= new FieldCondition.LessEqual("Okres.From", row.Dokument.Data);
- SubTable<PrzecenaOkresowa> discounts3 = tm.PrzecenyOkres.WgKontrahent[row.Dokument.Kontrahent][rc2];
- if (discounts3 != null && discounts3.Any)
- {
- foreach (PrzecenaOkresowa discount in discounts3)
- {
- SubTable<PrzecenaOkresowaTowaru> productDiscounts = discount.PrzecenyTowarow[new FieldCondition.Equal("Towar.Guid", row.Towar.Guid)];
- if (!productDiscounts.Any) continue;
- Percent tempMax = productDiscounts.Max(x => x.Rabat);
- if (tempMax > max)
- {
- max = tempMax;
- }
- }
- }
- if (row.Dokument.Kontrahent != null && row.Dokument.Kontrahent.Rabat > max)
- {
- max = row.Dokument.Kontrahent.Rabat;
- }
- if (max != Percent.Zero && row.Rabat != max)
- {
- row.Rabat = max;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement