Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using GuzikZablokujTowar;
- using Soneta.Business;
- using Soneta.Handel;
- using Soneta.Towary;
- using Soneta.Types;
- [assembly: Worker(typeof(GuzikZablokujTowarWorker), typeof(Towar))]
- namespace GuzikZablokujTowar
- {
- public sealed class GuzikZablokujTowarWorker
- {
- [Context]
- //Otwarcie okienka proszącego o dane
- public GuzikZablokujTowarPars Pars { get; set; }
- [Action("Zablokuj towary",Priority = 10,Mode = ActionMode.SingleSession,Target = ActionTarget.ToolbarWithText | ActionTarget.Menu,Icon = ActionIcon.Cancel)]
- public void BlockProducts(Context cx)
- {
- //Inicjalizacja potrzebnego modułu z context
- var hm = HandelModule.GetInstance(cx.Session);
- //Tworzenie sesji edycyjnej
- using (var transaction = cx.Session.Logout(true))
- {
- //Pobieranie zaznaczonych elementów oraz iterowanie po nich
- foreach (var towar in (Towar[])cx[typeof(Towar[])])
- {
- if (Pars.Quantity)
- {
- //Jeśli zaznaczono opcje checkbox blokuj automatycznie blokuje i przechodzi do nastepnego towaru
- towar.Blokada = true;
- continue;
- }
- //Jeżeli towar jest już zablokowany to kod się nie wykona
- if (Pars.Buy > 0 && !towar.Blokada)
- {
- var historyList = new List<DateTime>();
- //Tworzenie widoku faktur zakup
- var buyInvoices = hm.DokHandlowe.CreateView();
- buyInvoices.Condition &= new FieldCondition.GreaterEqual("Kategoria", "Zakup");
- buyInvoices.Condition &= new FieldCondition.LessEqual("Kategoria", "KorektaZakupu");
- foreach (var row in buyInvoices)
- {
- var dokument = (DokumentHandlowy)row;
- //Jeśli znajdzie na jakiejkolwiek fakturze aktualny towar dodaje date tej faktury do historii
- historyList.AddRange((from pozycja in dokument.Pozycje where pozycja.Towar == towar select dokument.Data).Select(dummy => (DateTime) dummy));
- historyList.Sort();
- //Jeżeli data ostatniej faktury jest starsza niż data ustawiona ustawiamy blokade aktualnego towaru
- if (Date.Now.AddDays(-Pars.Buy) < historyList.Max()) continue;
- towar.Blokada = true;
- }
- }
- //Jeżeli towar jest już zablokowany to kod się nie wykona
- if (Pars.Sell <= 0 || towar.Blokada) continue;
- {
- var historyList = new List<DateTime>();
- //Tworzenie widoku faktur sprzedaż
- var sellInvoices = hm.DokHandlowe.CreateView();
- sellInvoices.Condition &= new FieldCondition.GreaterEqual("Kategoria", "Sprzedaż");
- sellInvoices.Condition &= new FieldCondition.LessEqual("Kategoria", "KorektaSprzedaży");
- foreach (var row in sellInvoices)
- {
- var dokument = (DokumentHandlowy)row;
- //Jeśli znajdzie na jakiejkolwiek fakturze aktualny towar dodaje date tej faktury do historii
- historyList.AddRange((from pozycja in dokument.Pozycje where pozycja.Towar == towar select dokument.Data).Select(dummy => (DateTime) dummy));
- //Jeżeli data ostatniej faktury jest starsza niż data ustawiona ustawiamy blokade aktualnego towaru
- if (Date.Now.AddDays(-Pars.Buy) < historyList.Max()) continue;
- towar.Blokada = true;
- }
- }
- }
- //Zakończenie pracy workera i zapis zmian
- transaction.CommitUI();
- }
- }
- public static bool IsEnabledBlockProducts() => true;
- public static bool IsVisibleBlockProducts() => true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement