This repository has been archived by the owner on Oct 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample.cs
804 lines (680 loc) · 33.1 KB
/
sample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
#region namespace HQ.CSRH.SimpleDocument.Services.Sap
namespace HQ.CSRH.SimpleDocument.Services.Sap
{
using ReactiveUI;
using sapfewse;
using saprotwr.net;
using Splat;
using HQ.CSRH.SimpleDocument.Models;
using Xy.Logging;
using Xy.Sap;
public static class SapServices
{
public static IPA20Service PA20 { get { return Locator.Current.GetService<IPA20Service>(Contract); } }
public static void Initialize()
{
Locator.CurrentMutable.RegisterLazySingleton(() => new PA20Service(), typeof(IPA20Service), RealContract);
Locator.CurrentMutable.RegisterLazySingleton(() => new FakePA20Service(), typeof(IPA20Service), FakeContract);
}
public static TService GetService<TService, TRealService, TFakeService>()
where TRealService : TService, new()
where TFakeService : TService, new()
{
return !App.Config.TestMode ? (TService)new TRealService() : new TFakeService();
}
private const string RealContract = "Sap", FakeContract = "Faker";
private static string Contract { get { return !App.Config.TestMode ? RealContract : FakeContract; } }
}
public interface IPA20Service
{
Person GetIdentity(int employeeID);
Gender GetGender(int employeeID);
Address GetAddress(int employeeID);
Dictionary<EmailType, string> GetEmailAddresses(int employeeID);
}
public partial class PA20Service : IPA20Service
{
public Person GetIdentity(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
var detail = GetHrDetail(employeeID, "Identité (0002)");
var result = new Person();
using (result.Changed.Log(Logger.Instance.Info))
{
var prefix = detail.FindByName<GuiComboBox>("Q0002-ANREX").Key;
result.Gender = new Dictionary<string, Gender?>
{
{ "M.", Gender.Male },
{ "Mme", Gender.Female },
}
.FirstOrDefault(x => x.Key == prefix).Value;
result.Name = Regex.Replace(detail.FindByName<GuiTextField>("P0001-ENAME").Text, @"^(M\.|Mme) ", "");
}
return result;
}
public Gender GetGender(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
var detail = GetHrDetail(employeeID, "Identité (0002)");
var result = new Person();
using (result.Changed.Log(Logger.Instance.Info))
{
var prefix = detail.FindByName<GuiComboBox>("Q0002-ANREX").Key;
result.Gender = new Dictionary<string, Gender?>
{
{ "M.", Gender.Male },
{ "Mme", Gender.Female },
}
.FirstOrDefault(x => x.Key == prefix).Value;
}
return result.Gender.Value;
}
public Address GetAddress(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
var detail = GetHrDetail(employeeID, "Adresses (0006)");
var result = new Address();
using (result.Changed.Log(Logger.Instance.Info))
{
result.StreetNumberName = string.Join(" ", new string[]
{
detail.FindByName<GuiTextField>("P0006-STRAS").Text.Trim(), //Address
detail.FindByName<GuiTextField>("P0006-LOCAT").Text.Trim(), //Additional Address
}
.Where(x => !string.IsNullOrWhiteSpace(x)));
result.City = detail.FindByName<GuiTextField>("P0006-ORT01").Text.Trim();
result.Province = detail.FindByName<GuiTextField>("T005U-BEZEI").Text.Trim();
result.PostalCode = detail.FindByName<GuiTextField>("P0006-PSTLZ").Text.Trim().ToUpper();
}
return result;
}
public Dictionary<EmailType, string> GetEmailAddresses(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
var entries = GetHrEntries(employeeID, "Communication (0105)");
/* Index Type Title Tooltip
0 GuiTextField Début Date de début
1 GuiTextField Fin Date de fin
2 GuiCTextField Mode communicat. Mode communicat.
3 GuiTextField Désignation Désignation
4 GuiTextField ID du système ID/N° long
5 GuiTextField CB Code de blocage */
const int DescriptionIndex = 3, ValueIndex = 4;
var emailTypes = new Dictionary<string, EmailType>
{
{ "Courrier électronique HQ", EmailType.Work },
{ "Adresse courriel personnelle", EmailType.Personal },
};
var result = new ReactiveList<KeyValuePair<EmailType, string>>();
using (result.ItemsAdded.Subscribe(x => Logger.Instance.Info(x.Key + " => " + x.Value)))
{
foreach (var entry in entries)
{
var description = entry[DescriptionIndex];
var type = default(EmailType);
if (emailTypes.TryGetValue(description, out type) && !result.Any(x => x.Key == type))
{
result.Add(KVPair.Create(type, entry[ValueIndex]));
// early exit if all found
if (result.Count == emailTypes.Count)
break;
}
}
}
return result.ToDictionary(x => x.Key, x => x.Value);
}
}
public partial class PA20Service
{
public static IEnumerable<HrEntry> GetHrEntries(int employeeID, int it, string sty = null)
{
return GetHrEntries(employeeID, it.ToString("D4"), sty);
}
public static IEnumerable<HrEntry> GetHrEntries(int employeeID, string it, string sty = null)
{
Logger.Instance.LogCurrentMethod(LogLevel.Debug);
var window = QueryPA20(employeeID, it, sty, true);
var usr = window.FindByName<GuiUserArea>("usr");
var tableName = usr.Children.OfType<GuiTableControl>().Single().Name;
// Scrolling, or navigating to another page and navigate back
// all these resets the reference
Func<GuiTableControl> getTable = () => window.FindByName<GuiTableControl>(tableName);
// todo: optimize this to scroll only when item go out of page
var rows = getTable().VerticalScrollbar.Maximum;
for (int i = 0; i <= rows; i++)
{
/* zero-indexed position (screen value = this+1) */
getTable().VerticalScrollbar.Position = i;
yield return new HrEntry(window, (GuiTableRow)getTable().Rows.Item(0));
}
}
public static HrDetail GetHrDetail(int employeeID, int it, string sty = null)
{
return GetHrDetail(employeeID, it.ToString("D4"), sty);
}
public static HrDetail GetHrDetail(int employeeID, string it, string sty = null)
{
Logger.Instance.LogCurrentMethod(LogLevel.Debug);
var window = QueryPA20(employeeID, it, sty, false);
return new HrDetail(window);
}
/// <summary>Performs a PA20 query</summary>
/// <param name="it">Infotype ID</param>
/// <param name="sty">Subtype ID, optional</param>
/// <param name="asListView">True for list view. False for detail view. The default is to use list view</param>
public static GuiFrameWindow QueryPA20(int employeeID, string it, string sty = null, bool asListView = false)
{
const string TransactionTitle = "Afficher données de base personnel";
var session = SapHelper.GetActiveSession();
var window = session.BeginTransaction("PA20", TransactionTitle);
Logger.Instance.Debug("Accessing personal data: emp={0}, it={1}, sty={2}, mode={3}", employeeID, it, sty ?? "null", asListView ? "list" : "detail");
window.FindByName<GuiCTextField>("RP50G-PERNR").Text = employeeID.ToString();
window.FindByName<GuiCTextField>("RP50G-CHOIC").Text = it;
window.FindByName<GuiCTextField>("RP50G-SUBTY").Text = sty;
window.SendVKey(asListView ? 20 : 7); // Ctrl+F8 vs F7
// if the title remains the same, that means we have failed to access the data
if (window.Text == TransactionTitle)
{
const string Message = "The employee's personal data can not be accessed. The employee's ID may be incorrect, or there is no record for the requested infotype. Or. you do not have the correct permission to access this infotype.";
var context = new Dictionary<string, object>
{
{ "EmployeeID", employeeID },
{ "InfoType", it },
{ "SubType", sty },
{ "AsListView", asListView },
{ "StatusMessage", window.FindByName<GuiStatusbar>("sbar").Text }
};
Logger.Instance.Error("Unable to access infotype: " + context.Prettify());
throw new InvalidOperationException(Message + Environment.NewLine + context.Prettify())
.BindContext(context);
}
return window;
}
#region nested types
public class HrEntry
{
protected GuiFrameWindow window;
protected GuiTableRow row;
public HrEntry(GuiFrameWindow window, GuiTableRow row)
{
this.window = window;
this.row = row;
}
public string this[int column, bool isCText = false]
{
get
{
return isCText ? row.GetCText(column) : row.GetText(column);
}
}
public HrDetail InspectDetail()
{
row.Selected = true;
window.SendVKey(2); // F2
return new HrDetail(window);
}
}
public class HrDetail
{
protected GuiFrameWindow window;
public HrDetail(GuiFrameWindow window)
{
this.window = window;
}
public TSapControl FindByName<TSapControl>(string name)
{
return window.FindByName<TSapControl>(name);
}
public void Dispose()
{
window.SendVKey(3); // F3
}
}
#endregion
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
public class FakePA20Service : IPA20Service
{
public Person GetIdentity(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = new Person();
using (result.Changed.Log(Logger.Instance.Warn))
{
result.Gender = Faker.EnumFaker.SelectFrom<Gender>();
result.Name = result.Gender == Gender.Male ? Faker.NameFaker.MaleName() : Faker.NameFaker.FemaleName();
}
return result;
}
public Gender GetGender(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = new Person();
using (result.Changed.Log(Logger.Instance.Warn))
{
result.Gender = Faker.EnumFaker.SelectFrom<Gender>();
}
return result.Gender.Value;
}
public Address GetAddress(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = new Address();
using (result.Changed.Log(Logger.Instance.Warn))
{
result.StreetNumberName = Faker.LocationFaker.Street();
result.City = Faker.LocationFaker.City();
result.Province = Faker.LocationFaker.Country();
result.PostalCode = Faker.StringFaker.Randomize("?#? #?#").ToUpper();
}
return result;
}
public Dictionary<EmailType, string> GetEmailAddresses(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = new ReactiveList<KeyValuePair<EmailType, string>>();
using (result.ItemsAdded.Subscribe(x => Logger.Instance.Warn(x.Key + " => " + x.Value)))
{
result.Add(KVPair.Create(EmailType.Work, Faker.StringFaker.Randomize("????????.###@fake.email.com")));
result.Add(KVPair.Create(EmailType.Personal, Faker.StringFaker.Randomize("????????.###@fake.email.biz")));
}
return result.ToDictionary(x => x.Key, x => x.Value);
}
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
public interface ICustomSapService
{
KeyValuePair<EmploymentType, DateTime>? GetModificationEvent(int employeeID);
bool? HasHealthInsurance(int employeeID);
bool? HasDentalInsurance(int employeeID);
DisabilityCoverage? GetDisabilityCoverage(int employeeID);
}
public class CustomSapService : ICustomSapService
{
public KeyValuePair<EmploymentType, DateTime>? GetModificationEvent(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
var entries = PA20Service.GetHrEntries(employeeID, "Mesures (0000)");
/* Index Type Title Tooltip
0 GuiTextField Début Date de début
1 GuiTextField Fin Date de fin
2 GuiCTextField Mes. Catégorie de mesure
3 GuiTextField Dés. cat. mesure Dés. cat. mesure
4 GuiCTextField MotMe Motif mesure
5 GuiTextField Dés. motif mesure Dés. motif mesure
6 GuiCTextField Client Statut propre client
7 GuiCTextField Activité Statut d'activité
8 GuiCTextField Paiement Statut paiement part */
var actionToEmploymentType = new Dictionary<string, EmploymentType>
{
{ "A5", EmploymentType.Temporary }, // Cess. réemb. sans bris /CCE
{ "A6", EmploymentType.Permanent }, // Mouvement de personnel /CCE
};
Logger.Instance.Info("Searching for a record with action of [A5 or A6] and reason of [90]");
var match = entries
.Select(x => new
{
StartDate = x[0],
EndDate = x[1],
Action = x[2, true],
ActionText = x[3],
Reason = x[4, true],
ReasonText = x[5],
})
.FirstOrDefault(x => actionToEmploymentType.Keys.Contains(x.Action));
if (match == null)
{
Logger.Instance.Warn("-> There is no record matching the criteria.");
Logger.Instance.Info("=> null");
return null;
}
Logger.Instance.Info("-> " + match);
var result = KVPair.Create(
actionToEmploymentType[match.Action],
DateTime.ParseExact(match.StartDate, "yyyy/MM/dd", CultureInfo.InvariantCulture));
Logger.Instance.Info("=> " + result);
return result;
}
public bool? HasHealthInsurance(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
try
{
var detail = PA20Service.GetHrDetail(employeeID, "Régimes de santé (0167)", "MEDI");
// retirement health plan is not considered valid
if (detail.FindByName<GuiTextField>("Q0167-BPLAN").Text == "MALH")
{
Logger.Instance.Warn("-> The employee doesn't have a valid health plan: [{0}]{1}",
detail.FindByName<GuiTextField>("Q0167-BPLAN").Text,
detail.FindByName<GuiTextField>("T5UCA-LTEXT").Text);
Logger.Instance.Info("=> null");
return null;
}
// an expired plan will have an end date other than 9999-12-31
if (!detail.FindByName<GuiCTextField>("P0167-ENDDA").Text.StartsWith("9999"))
{
Logger.Instance.Info("-> Employee's last health plan has expired on " +
detail.FindByName<GuiCTextField>("P0167-ENDDA").Text);
Logger.Instance.Warn("=> false");
return false;
}
var options = new Dictionary<string, bool>
{
{ "EXEM", false }, // Exempté de participation
{ "BASE", true }, // Option de base
{ "MOD1", true }, // Module de base
{ "MOD2", true }, // Module bonifié
{ "MOD3", true }, // Module enrichi
{ "NONP", false }, // Non participant
};
var option = detail.FindByName<GuiCTextField>("P0167-BOPTI").Text;
var optionText = detail.FindByName<GuiTextField>("T5UCE-LTEXT").Text;
bool result;
if (!options.TryGetValue(option, out result))
{
const string Message = "The application doesn't know to how to process this information.";
var context = new Dictionary<string, string>
{
{ "Option", option },
{ "Description", optionText },
};
Logger.Instance.Error("Invalid health plan option:" + context.Prettify());
throw new ArgumentOutOfRangeException(Message + Environment.NewLine + context.Prettify())
.BindContext(context);
}
Logger.Instance.Info("-> [{0}]{1}", option, optionText);
Logger.Instance.Info("=> " + result);
return result;
}
catch (InvalidOperationException e)
{
const string NoRecordStatusMessage = "Aucune donnée existe pour Régimes de santé (0167) (dans période sélectionnée)";
if (e.Data["StatusMessage"] as string == NoRecordStatusMessage)
{
Logger.Instance.Warn("-> " + NoRecordStatusMessage);
Logger.Instance.Info("=> false");
return false;
}
throw;
}
}
public bool? HasDentalInsurance(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
try
{
var detail = PA20Service.GetHrDetail(employeeID, "Régimes de santé (0167)", "DENT");
// only '[DEN0]Dentaire Bureau/Métier/Techn.' is taken into consideration
if (detail.FindByName<GuiCTextField>("P0167-BPLAN").Text != "DEN0")
{
// TODO: ask for the reason why this is invalid
const string Message = "The dental plan type is invalid.";
var context = new Dictionary<string, string>
{
{ "Type", detail.FindByName<GuiTextField>("Q0167-BPLAN").Text },
{ "Description", detail.FindByName<GuiTextField>("T5UCA-LTEXT").Text },
};
Logger.Instance.Error("The dental plan type is invalid: " + context.Prettify());
throw new ArgumentOutOfRangeException(Message + Environment.NewLine + context.Prettify())
.BindContext(context);
}
var options = new Dictionary<string, bool>
{
{ "EXEM", false }, // Exempté de participation
{ "BASE", true }, // Option de base
{ "MOD1", true }, // Module de base
{ "MOD2", true }, // Module bonifié
{ "MOD3", true }, // Module enrichi
{ "NONP", false }, // Non participant
};
var option = detail.FindByName<GuiCTextField>("P0167-BOPTI").Text;
var optionText = detail.FindByName<GuiTextField>("T5UCE-LTEXT").Text;
bool result;
if (!options.TryGetValue(option, out result))
{
const string Message = "The dental plan option could not be processed.";
var context = new Dictionary<string, string>
{
{ "Option", option },
{ "Description", optionText },
};
Logger.Instance.Error("Invalid dental plan option:" + context.Prettify());
throw new ArgumentOutOfRangeException(Message + Environment.NewLine + context.Prettify())
.BindContext(context);
}
Logger.Instance.Info("-> [{0}]{1}", option, optionText);
Logger.Instance.Info("=> " + result);
return result;
}
catch (Exception e)
{
const string NoRecordStatusMessage = "Aucune donnée existe pour Régimes de santé (0167) (dans période sélectionnée)";
if (e.Data["StatusMessage"] as string == NoRecordStatusMessage)
{
Logger.Instance.Warn("-> " + NoRecordStatusMessage);
Logger.Instance.Info("=> false");
return false;
}
throw;
}
}
public DisabilityCoverage? GetDisabilityCoverage(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Info);
try
{
var detail = PA20Service.GetHrDetail(employeeID, "Contingents d'absences (2006)", "15");
var valueText = detail.FindByName<GuiTextField>("P2006-ANZHL").Text.Trim();
Logger.Instance.Info("-> {0} < 99999", valueText);
var result = double.Parse(valueText) < 99999 ? DisabilityCoverage.Weeks52 : DisabilityCoverage.Weeks26;
Logger.Instance.Info("=> " + result);
return result;
}
catch (Exception e)
{
const string NoRecordStatusMessage = "Aucune donnée existe pour Contingents d'absences (2006) (dans période sélectionnée)";
if (e.Data["StatusMessage"] as string == NoRecordStatusMessage)
{
Logger.Instance.Warn("-> " + NoRecordStatusMessage);
Logger.Instance.Info("=> null");
return null;
}
throw;
}
}
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
public class FakeCustomSapService : ICustomSapService
{
public KeyValuePair<EmploymentType, DateTime>? GetModificationEvent(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = KVPair.Create(
Faker.EnumFaker.SelectFrom<EmploymentType>(),
Faker.DateTimeFaker.DateTime().Date);
Logger.Instance.Warn("=> " + result);
return result;
}
public bool? HasHealthInsurance(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = Faker.BooleanFaker.Boolean();
Logger.Instance.Warn("=> " + result);
return result;
}
public bool? HasDentalInsurance(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = Faker.BooleanFaker.Boolean();
Logger.Instance.Warn("=> " + result);
return result;
}
public DisabilityCoverage? GetDisabilityCoverage(int employeeID)
{
Logger.Instance.LogCurrentMethod(LogLevel.Warn);
var result = Faker.EnumFaker.SelectFrom<DisabilityCoverage>();
Logger.Instance.Warn("=> " + result);
return result;
}
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
public static class KVPair
{
public static KeyValuePair<TKey, TValue> Create<TKey, TValue>(TKey key, TValue value)
{
return new KeyValuePair<TKey, TValue>(key, value);
}
}
}
#endregion
#region namespace Xy.Sap
namespace Xy.Sap
{
using System.Runtime.InteropServices;
using System.Reflection;
using sapfewse;
using saprotwr.net;
using Xy.Logging;
using COMException = System.Runtime.InteropServices.COMException;
public static class SapHelper
{
/// <summary>Return an already opened sap session</summary>
public static GuiSession GetActiveSession()
{
var rot = new CSapROTWrapper().GetROTEntry("SAPGUI");
if (rot == null)
{
const string Message = "The application can no connect to SAP. Make sure SAP is up and running, and then try again.";
Logger.Instance.Fatal("The application can no connect to SAP");
throw new InvalidComObjectException(Message);
}
var app = (GuiApplication)rot.GetType().InvokeMember("GetScriptingEngine", BindingFlags.InvokeMethod, null, rot, null);
var connectedSession = app.Connections.Cast<GuiConnection>()
.SelectMany(x => x.Children.Cast<GuiSession>())
.Where(x => !string.IsNullOrEmpty(x.Info.User))
.FirstOrDefault();
if (connectedSession == null)
{
const string Message = "Could not find an opened SAP session. Make sure you are logged in, and then try again.";
Logger.Instance.Fatal("Could not find an opened SAP session");
throw new InvalidComObjectException(Message);
}
return connectedSession;
}
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
public static class SapExtensions
{
#region GuiFrameWindow
public static TSapControl FindByName<TSapControl>(this GuiFrameWindow window, string name)
{
try
{
return (TSapControl)window.FindByName(name, typeof(TSapControl).Name);
}
catch (COMException e)
{
const string Message = "The control could not be found by name and type.";
var context = new Dictionary<string, object>
{
{ "Name", name },
{ "Type", typeof(TSapControl).Name },
}.Prettify();
Logger.Instance.ErrorException("The control could not be found by name and type: " + context, e);
throw new InvalidOperationException(Message + Environment.NewLine + context, e);
}
}
#endregion
#region GuiTableControl
/// <summary>Note: Do not iterate through this ienumerable more than once</summary>
public static IEnumerable<GuiTableRow> AsEnumerable(this GuiTableControl table)
{
var container = table.Parent as dynamic;
string name = table.Name, type = table.Type;
int rowCount = table.VerticalScrollbar.Maximum;
Func<GuiTableControl> getTable = () => container.FindByName(name, type) as GuiTableControl;
for (int i = 0; i <= rowCount; i++)
{
getTable().VerticalScrollbar.Position = i;
yield return getTable().Rows.Item(0) as GuiTableRow;
}
}
public static TSapControl GetCell<TSapControl>(this GuiTableRow row, int column)
{
return (TSapControl)row.Item(column);
}
public static string GetCText(this GuiTableRow row, int column)
{
return row.GetCell<GuiCTextField>(column).Text;
}
public static string GetText(this GuiTableRow row, int column)
{
return row.GetCell<GuiTextField>(column).Text;
}
#endregion
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
public static class CommonSapService
{
/// <summary>Start a new transaction</summary>
/// <param name="transactionID">Transaction ID</param>
/// <param name="expectedTitle">Title of the transaction. This is used for validation.</param>
public static GuiFrameWindow BeginTransaction(this GuiSession session, string transactionID, string expectedTitle)
{
// force current transaction to end, preventing any blocking(eg: model dialog)
session.EndTransaction();
Logger.Instance.Debug("Starting transaction : " + transactionID);
session.StartTransaction(transactionID);
var window = session.ActiveWindow;
if (window.Text != expectedTitle)
{
const string Message = "You do not have the correct permission to access this transaction. Or, such transaction does not exist.";
var context = new Dictionary<string, object>
{
{ "TransactionID", transactionID },
{ "Expected Title", expectedTitle },
{ "WindowTitle", window.Text },
{ "StatusMessage", window.FindByName<GuiStatusbar>("sbar").Text },
};
Logger.Instance.Error("Unable to access personal data");
throw new InvalidOperationException(Message + Environment.NewLine + context.Prettify())
.BindContext(context);
}
return window;
}
/// <summary>Performs a PA20 query</summary>
/// <param name="it">Infotype ID</param>
/// <param name="sty">Subtype ID, optional</param>
/// <param name="asListView">True for list view. False for detail view. The default is to use list view</param>
[Obsolete]
public static GuiFrameWindow QueryPA20(this GuiSession session, int employeeID, string it, string sty = null, bool asListView = false)
{
const string TransactionTitle = "Afficher données de base personnel";
var window = session.BeginTransaction("PA20", TransactionTitle);
Logger.Instance.Debug("Accessing personal data: emp={0}, it={1}, sty={2}, mode={3}", employeeID, it, sty ?? "null", asListView ? "list" : "detail");
window.FindByName<GuiCTextField>("RP50G-PERNR").Text = employeeID.ToString();
window.FindByName<GuiCTextField>("RP50G-CHOIC").Text = it;
window.FindByName<GuiCTextField>("RP50G-SUBTY").Text = sty;
window.SendVKey(asListView ? 20 : 7); // Ctrl+F8 vs F7
// if the title remains the same, that means we have failed to access the data
if (window.Text == TransactionTitle)
{
const string Message = "The employee's personal data can not be accessed. The employee's ID may be incorrect, or there is no record for the requested infotype. Or. you do not have the correct permission to access this infotype.";
var context = new Dictionary<string, object>
{
{ "EmployeeID", employeeID },
{ "InfoType", it },
{ "SubType", sty },
{ "AsListView", asListView },
{ "StatusMessage", window.FindByName<GuiStatusbar>("sbar").Text }
};
Logger.Instance.Error("Unable to access infotype." + context.Prettify());
throw new InvalidOperationException(Message + Environment.NewLine + context.Prettify())
.BindContext(context);
}
return window;
}
private static readonly TypeNamedLogger Logger = new TypeNamedLogger();
}
}
#endregion