Advertisement
isotonicq

Untitled

Jul 23rd, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using GuzikZablokujTowar;
  5. using Soneta.Business;
  6. using Soneta.Handel;
  7. using Soneta.Towary;
  8. using Soneta.Types;
  9.  
  10. [assembly: Worker(typeof(GuzikZablokujTowarWorker), typeof(Towar))]
  11.  
  12. namespace GuzikZablokujTowar
  13. {
  14.     public sealed class GuzikZablokujTowarWorker
  15.     {
  16.         [Context]
  17.         //Otwarcie okienka proszącego o dane
  18.         public GuzikZablokujTowarPars Pars { get; set; }
  19.  
  20.         [Action("Zablokuj towary",Priority = 10,Mode = ActionMode.SingleSession,Target = ActionTarget.ToolbarWithText | ActionTarget.Menu,Icon = ActionIcon.Cancel)]
  21.         public void BlockProducts(Context cx)
  22.         {
  23.             //Inicjalizacja potrzebnego modułu z context
  24.             var hm = HandelModule.GetInstance(cx.Session);
  25.  
  26.             //Tworzenie sesji edycyjnej
  27.             using (var transaction = cx.Session.Logout(true))
  28.             {
  29.                 //Pobieranie zaznaczonych elementów oraz iterowanie po nich
  30.                 foreach (var towar in (Towar[])cx[typeof(Towar[])])
  31.                 {
  32.                     if (Pars.Quantity)
  33.                     {
  34.                         //Jeśli zaznaczono opcje checkbox blokuj automatycznie blokuje i przechodzi do nastepnego towaru
  35.                         towar.Blokada = true;
  36.                         continue;
  37.                     }
  38.  
  39.                     //Jeżeli towar jest już zablokowany to kod się nie wykona
  40.                     if (Pars.Buy > 0 && !towar.Blokada)
  41.                     {
  42.                         var historyList = new List<DateTime>();
  43.  
  44.                         //Tworzenie widoku faktur zakup
  45.                         var buyInvoices = hm.DokHandlowe.CreateView();
  46.                         buyInvoices.Condition &= new FieldCondition.GreaterEqual("Kategoria", "Zakup");
  47.                         buyInvoices.Condition &= new FieldCondition.LessEqual("Kategoria", "KorektaZakupu");
  48.  
  49.                         foreach (var row in buyInvoices)
  50.                         {
  51.                             var dokument = (DokumentHandlowy)row;
  52.  
  53.                             //Jeśli znajdzie na jakiejkolwiek fakturze aktualny towar dodaje date tej faktury do historii
  54.                             historyList.AddRange((from pozycja in dokument.Pozycje where pozycja.Towar == towar select dokument.Data).Select(dummy => (DateTime) dummy));
  55.                             historyList.Sort();
  56.  
  57.                             //Jeżeli data ostatniej faktury jest starsza niż data ustawiona ustawiamy blokade aktualnego towaru
  58.                             if (Date.Now.AddDays(-Pars.Buy) < historyList.Max()) continue;
  59.                             towar.Blokada = true;
  60.                         }
  61.                     }
  62.  
  63.                     //Jeżeli towar jest już zablokowany to kod się nie wykona
  64.                     if (Pars.Sell <= 0 || towar.Blokada) continue;
  65.                     {
  66.                         var historyList = new List<DateTime>();
  67.  
  68.                         //Tworzenie widoku faktur sprzedaż
  69.                         var sellInvoices = hm.DokHandlowe.CreateView();
  70.                         sellInvoices.Condition &= new FieldCondition.GreaterEqual("Kategoria", "Sprzedaż");
  71.                         sellInvoices.Condition &= new FieldCondition.LessEqual("Kategoria", "KorektaSprzedaży");
  72.  
  73.                         foreach (var row in sellInvoices)
  74.                         {
  75.                             var dokument = (DokumentHandlowy)row;
  76.  
  77.                             //Jeśli znajdzie na jakiejkolwiek fakturze aktualny towar dodaje date tej faktury do historii
  78.                             historyList.AddRange((from pozycja in dokument.Pozycje where pozycja.Towar == towar select dokument.Data).Select(dummy => (DateTime) dummy));
  79.  
  80.                             //Jeżeli data ostatniej faktury jest starsza niż data ustawiona ustawiamy blokade aktualnego towaru
  81.                             if (Date.Now.AddDays(-Pars.Buy) < historyList.Max()) continue;
  82.                             towar.Blokada = true;
  83.                         }
  84.                     }
  85.                 }
  86.                 //Zakończenie pracy workera i zapis zmian
  87.                 transaction.CommitUI();
  88.             }
  89.         }
  90.         public static bool IsEnabledBlockProducts() => true;
  91.  
  92.         public static bool IsVisibleBlockProducts() => true;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement