How Can We Help?
Ustawianie cechy Enova przy wystawianiu dokumentu z Unicore Produkcja
Założenia
- W Enova mamy zdefiniowany słownik globalny “MPK”
- W Enova, w towarach mamy cechę referencyjną do słownika “MPK” o nazwie “Ref1”
- W Enova, w dokumentach magazynowych mamy cechę referencyjną “Ref2” do tego samego słownika.
Oczekiwania
Chcemy, aby przy rozliczaniu ZP na dany produkt dokumenty RW miały ustawianą cechę “Ref2” na podstawie cechy “Ref1”
Rozwiązanie
W Produkcji należy zdefiniować zdarzenie dla obiektu HMDocument: Obiekty biznesowe => Dokument (HMDocument) => zakładka “Zdarzenia” , wklejając poniższy kod.
using System;
using System.Linq;
using System.Windows;
using Worm;
using Worm.Authentication;
using Worm.Ui.Wpf;
using Worm.Ui.Core;
using Worm.Ui.Wpf.Forms;
using Worm.Ui.Wpf.Views;
using Unicore.Manufacture.Business;
using Hydra.Enova;
using Soneta.Business;
using Soneta.Handel;
using Soneta.Types;
using Soneta.Ksiega;
using Log = Worm.Ui.Wpf.Log;
public override void OnObjectUpdated (object sender, ObjectChangedEventArgs args) {
	if (args.IsRemote) return;
	var hmDoc = args.DbObject as HMDocument; //dokument Produkcja
	var storeDoc = StoreDocument.Get(hmDoc.ForeignId); //dokument Enova obiekt Produkcji
	if (hmDoc == null || storeDoc == null) return;
	if (storeDoc.NativeFeatures["Ref2"].Value != null) return;
	var issue = WorkOrderIssue.Get(iss => iss.StoreDocumentId == hmDoc.Id && iss.Input); //rozl powiązane z dokumentem
	if (issue == null) return;
	var wol = issue.WorkOrderLine; //pozycja zlecenia
	var kierunekFeat = wol.Product.Article.NativeFeatures["Ref1"].Value as EnovaDictionaryItem; 
	if (kierunekFeat == null) return;
	try {
		using (var session = EnovaLoader.CreateSession()) {
			var handelMod = HandelModule.GetInstance(session);
			var ksiegaMod = KsiegaModule.GetInstance(session);
			var elSlow = ksiegaMod.ElemSlownikow[kierunekFeat.Id];
			var enovaRw = handelMod.DokHandlowe[storeDoc.Id];
			if (enovaRw == null || elSlow == null) return;
			using (var trans = session.Logout(true)) {
				enovaRw.Features["Ref2"] = elSlow;
				trans.Commit();
			}
			session.Save();		
		}
	}
	catch(Exception e) {
		Log.Add(new Result().AddError(e));
	}	
}
