/
Operazioni.cs
1110 lines (1088 loc) · 29.7 KB
/
Operazioni.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel; // Per Observable collection
using System.Collections.Specialized;
using System.Windows.Controls; // Per DataGrid
using System.IO; // Per operazioni su stream
using System.Linq; // Per Collection<>.OrderBy
namespace WPF02
{
public delegate void StatusChangedHandler();
public class Operazioni
{
static string intestazioneFile = "@INTESTAZIONE";
enum readStat { Indefinito, Operazioni, Conti, OpStandard, Intestazione };
enum checkType { Conti_Unique, OpStandard_Unique, Op_ContiValid, Op_OpStdValid, OpStd_ContiValid, Op_Consuntivo_old };
ObservableCollection<Operazione> opVisibili // Operazioni visibili
{ get; set; }
ObservableCollection<Operazione> opTotali // Tutte le operazioni
{ get; set; }
ObservableCollection<Conto> cntTotali // Tutti i conti
{ get; set; }
ObservableCollection<OpStandard> opStdTotali // Tutte le operazioni standard
{ get; set; }
Encryption encriptor;
Dictionary<int, List<Consuntivo>> dicConsuntivi // Tutti i consuntivi
{ get; set; }
List<Consuntivo> consTotali // Solo i consuntivi del conto scelto
{ get; set; }
List<Consuntivo> empty;
bool comboContiUpdated = false;
StatusChangedHandler statHandler = null;
bool bStatusOk = true;
string filename; // Per operazioni su file
bool readwriteCripted;
string readwritePassword;
List<string> lstLog;
List<string> lstErr;
public Filtro filtro;
#region PROPRIETA
public bool LogAbilitato { get; set; }
public ObservableCollection<Operazione> operazioni
{
get { return opVisibili; }
set { opVisibili = value; }
}
public ObservableCollection<Conto> conti
{
get { return cntTotali; }
set { cntTotali = value; }
}
public ObservableCollection<OpStandard> opStandard
{
get { return opStdTotali; }
set { opStdTotali = value; }
}
public bool status
{
get { return bStatusOk; }
set
{
bStatusOk = value;
if (statHandler != null)
statHandler();
}
}
public StatusChangedHandler azioneStatus
{
get { return statHandler; }
set { statHandler = value; }
}
public string Filename
{
get { return filename; }
set { filename = value; }
}
public bool contiUpdated
{
get { return comboContiUpdated; }
set { comboContiUpdated = value; }
}
public string NomeSecondario {get; set;}
public bool AttivoSecondario { get; set; }
public bool Cripto1 { get; set; }
public bool Cripto2 { get; set; }
public string Passphrase1 { get; set; }
public string Passphrase2 { get; set; }
public string Salt { get; set; }
public bool StorePassphrase1 { get; set; }
public bool StorePassphrase2 { get; set; }
#endregion
void opChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if(opVisibiliChangedEventEnabled)
{
StringBuilder strb = new StringBuilder();
if (e.NewItems != null)
{
if (LogAbilitato)
strb.Append("Aggiunti " + e.NewItems.Count + " elementi");
foreach (Operazione op in e.NewItems)
{
opTotali.Add(op);
}
}
if (e.OldItems != null)
{
if (LogAbilitato)
strb.Append("Rimossi " + e.OldItems.Count + " elementi");
foreach (Operazione op in e.OldItems)
{
opTotali.Remove(op);
}
}
if (LogAbilitato)
lstLog.Add(strb.ToString());
}
}
void cntChanged(object sender, NotifyCollectionChangedEventArgs e)
{
comboContiUpdated = false;
}
void opsChanged(object sender, NotifyCollectionChangedEventArgs e)
{
#warning DA SCRIVERE...
}
bool opVisibiliChangedEventEnabled;
public Operazioni()
{
LeggiConfigurazione();
opTotali = new ObservableCollection<Operazione>();
opVisibili = new ObservableCollection<Operazione>();
opVisibiliChangedEventEnabled = true;
opVisibili.CollectionChanged += opChanged;
cntTotali = new ObservableCollection<Conto>();
cntTotali.CollectionChanged += cntChanged;
opStdTotali = new ObservableCollection<OpStandard>();
opStdTotali.CollectionChanged += opsChanged;
empty = new List<Consuntivo>();
dicConsuntivi = new Dictionary<int, List<Consuntivo>>();
dicConsuntivi.Add(0, empty);
consTotali = dicConsuntivi[0];
filename = "";
readwriteCripted = false;
readwritePassword = "";
lstLog = new List<string>();
lstErr = new List<string>();
LogAbilitato = false;
filtro = new Filtro();
encriptor = new Encryption();
}
/// <summary>
/// Legge i parametri di configurazione dalle impostazioni utente del programma
/// </summary>
public void LeggiConfigurazione()
{
NomeSecondario = Properties.Settings.Default.SecondSavePath + Path.DirectorySeparatorChar + Properties.Settings.Default.SecondSaveFilename;
AttivoSecondario = Properties.Settings.Default.SecondSaveActive;
Cripto1 = Properties.Settings.Default.FirstSaveEncryptActive;
Cripto2 = Properties.Settings.Default.SecondSaveEncryptActive;
Passphrase1 = Properties.Settings.Default.FirstSavePassphrase;
Passphrase2 = Properties.Settings.Default.SecondSavePassphrase;
Salt = Properties.Settings.Default.Salt;
StorePassphrase1 = Properties.Settings.Default.FirstSaveStorePassphrase;
StorePassphrase2 = Properties.Settings.Default.SecondSaveStorePassphrase;
}
/// <summary>
/// Scrive i parametri di configurazione nelle impostazioni utente del programma
/// </summary>
public void ScriviConfigurazione()
{
Properties.Settings.Default.SecondSavePath = Path.GetDirectoryName(NomeSecondario);
Properties.Settings.Default.SecondSaveFilename = Path.GetFileName(NomeSecondario);
Properties.Settings.Default.SecondSaveActive = AttivoSecondario;
Properties.Settings.Default.FirstSaveEncryptActive = Cripto1;
Properties.Settings.Default.SecondSaveEncryptActive = Cripto2;
Properties.Settings.Default.Salt = Salt;
Properties.Settings.Default.FirstSaveStorePassphrase = StorePassphrase1;
Properties.Settings.Default.SecondSaveStorePassphrase = StorePassphrase2;
if(StorePassphrase1)
Properties.Settings.Default.FirstSavePassphrase = Passphrase1;
if(StorePassphrase2)
Properties.Settings.Default.SecondSavePassphrase = Passphrase2;
Properties.Settings.Default.Save();
}
/// <summary>
/// Aggiorna le griglie dati con le collezioni contenute
/// </summary>
/// <param name="dgOperazioni"></param>
/// <param name="dgConti"></param>
/// <param name="dgOpStandard"></param>
/// <param name="dgConsuntivi"></param>
public void setDatiGrid(DataGrid dgOperazioni, DataGrid dgConti, DataGrid dgOpStandard, DataGrid dgConsuntivi)
{
dgOperazioni.AutoGenerateColumns = true;
dgOperazioni.ItemsSource = this.opVisibili;
dgConti.AutoGenerateColumns = true;
dgConti.ItemsSource = this.cntTotali;
dgOpStandard.AutoGenerateColumns = true;
dgOpStandard.ItemsSource = this.opStdTotali;
dgConsuntivi.AutoGenerateColumns = true;
dgConsuntivi.ItemsSource = this.consTotali;
dgConsuntivi.Items.Refresh();
dgConsuntivi.IsReadOnly = true;
}
/// <summary>
/// Aggiorna la griglie dei consuntivi con quello con il numero selezionato
/// </summary>
/// <param name="dgConsuntivi"></param>
/// <param name="nc"></param>
public void setConsuntivoGrid(DataGrid dgConsuntivi, int nc)
{
if (dicConsuntivi.ContainsKey(nc))
this.consTotali = dicConsuntivi[nc];
else
this.consTotali = empty;
dgConsuntivi.AutoGenerateColumns = true;
dgConsuntivi.ItemsSource = this.consTotali;
dgConsuntivi.IsReadOnly = true;
dgConsuntivi.Items.Refresh();
}
/// <summary>
/// Restituisce una stringa con la lista dei messaggi
/// </summary>
/// <returns></returns>
public string MsgList()
{
StringBuilder strb = new StringBuilder();
strb.Append("Azioni eseguite:\n");
foreach (string str in lstLog)
{
strb.Append(str + '\n');
}
return strb.ToString();
}
/// <summary>
/// Enumeratore dei messaggi
/// </summary>
/// <returns></returns>
public IEnumerable<string> Messaggi()
{
foreach (string str in lstLog)
yield return str;
yield break;
}
/// <summary>
/// Enumeratore degli errori
/// </summary>
/// <returns></returns>
public IEnumerable<string> Errori()
{
foreach (string str in lstErr)
yield return str;
yield break;
}
/// <summary>
/// Numero degli errori
/// </summary>
/// <returns></returns>
public int ErroriCount()
{
return lstErr.Count;
}
/// <summary>
/// Enumeratore delle liste dei consuntivi
/// </summary>
/// <returns></returns>
public IEnumerable<List<Consuntivo>> ListeConsuntivi()
{
foreach (List<Consuntivo> lc in dicConsuntivi.Values)
yield return lc;
yield break;
}
/// <summary>
/// Enumeratore delle operazioni standard
/// </summary>
/// <returns></returns>
public IEnumerable<OpStandard> OpStandard()
{
foreach (OpStandard ops in opStandard)
yield return ops;
yield break;
}
/// <summary>
/// Enumeratore dei conti
/// </summary>
/// <returns></returns>
public IEnumerable<Conto> Conti()
{
foreach (Conto cnt in cntTotali)
yield return cnt;
yield break;
}
/// <summary>
/// Calcella la lista dei log
/// </summary>
public void CancellaLog()
{
lstLog.Clear();
}
/// <summary>
/// Restituisce una stringa con le operazioni
/// </summary>
/// <returns></returns>
public string ListaOperazioni()
{
StringBuilder strb = new StringBuilder();
int n = opTotali.Count;
if (n > 0)
{
strb.Append("OPERAZIONI[" + n.ToString() + "]\n");
}
else
strb.Append("NESSUNA OPERAZIONE\n");
foreach (Operazione op in opTotali)
{
strb.Append(op.ToString() + Riga.Separatore.line);
}
return strb.ToString();
}
/// <summary>
/// Restituisce una stringa con i conti
/// </summary>
/// <returns></returns>
public string ListaConti()
{
StringBuilder strb = new StringBuilder();
int n = cntTotali.Count;
if (n > 0)
{
strb.Append("CONTI[" + n.ToString() + "]\n");
}
else
strb.Append("NESSUN CONTO\n");
foreach (Conto cn in cntTotali)
{
strb.Append(cn.ToString() + Riga.Separatore.line);
}
return strb.ToString();
}
/// <summary>
/// Restituisce una stringa con le operazioni standard
/// </summary>
/// <returns></returns>
public string ListaOpStandard()
{
StringBuilder strb = new StringBuilder();
int n = opStdTotali.Count;
if (n > 0)
{
strb.Append("OPERAZIONI STANDARD[" + n.ToString() + "]\n");
}
else
strb.Append("NESSUNA OPERAZIONE STANDARD\n");
foreach (OpStandard ops in opStdTotali)
{
strb.Append(ops.ToString() + Riga.Separatore.line);
}
return strb.ToString();
}
/// <summary>
/// Restituisce una stringa con i consuntivi attualmente elaborati
/// </summary>
/// <returns></returns>
public string ListaConsuntivi()
{
StringBuilder strb = new StringBuilder();
int n = consTotali.Count;
if (n > 0)
{
strb.Append("CONSUNTIVI [" + n.ToString() + "]\n");
}
else
strb.Append("NESSUN CONSUNTIVO\n");
foreach (Consuntivo cns in consTotali)
{
strb.Append(cns.ToString() + Riga.Separatore.line);
}
return strb.ToString();
}
/// <summary>
/// Inserisce una operazione alla riga indicata
/// </summary>
/// <param name="indx"></param>
/// <returns></returns>
public bool OpInserisciAt(int indx)
{
bool ok = false;
Operazione op = new Operazione();
if ((indx >= 0) && (indx < opVisibili.Count))
{
opVisibili.Insert(indx, op);
ok = true;
}
return ok;
}
/// <summary>
/// Inserisce una operazione prima dell'operazione indicata
/// </summary>
/// <param name="opi"></param>
/// <returns></returns>
public bool OpInserisciAt(Operazione opi)
{
bool ok = false;
Operazione op = new Operazione();
int indx = opVisibili.IndexOf(opi);
if(indx!=-1)
{
opVisibili.Insert(indx, op);
ok = true;
}
return ok;
}
/// <summary>
/// Salva il file primario e, se impostato, anche il secondario
/// </summary>
/// <param name="fullfilename"></param>
/// <returns></returns>
public bool SaveFiles(string fullfilename)
{
bool ok = true;
if(AttivoSecondario) // Salva prima il secondario di backup
{
if (!Save(fullfilename, false))
{
lstLog.Add("Errore salvataggio file secondario");
ok = false;
}
}
if (!Save(fullfilename)) // Poi salva il primario (per mantenere il riferimento al file giusto)
{
lstLog.Add("Errore salvataggio file primario");
ok = false;
}
return ok;
}
/// <summary>
/// Salva il file
/// </summary>
/// <param name="fullfilename">Nome completo di path</param>
/// <param name="primario">Se false, usa il nome secondario</param>
/// <returns></returns>
private bool Save(string fullfilename, bool primario = true)
{
bool ret = true;
readwriteCripted = false;
if(primario)
{
if(Cripto1)
{
readwriteCripted = true;
readwritePassword = Passphrase1;
encriptor.ClearErrors();
encriptor.Salt = Salt;
}
}
else
{
fullfilename = this.NomeSecondario;
if(Cripto2)
{
readwriteCripted = true;
readwritePassword = Passphrase2;
encriptor.ClearErrors();
encriptor.Salt = Salt;
}
}
try
{
using (StreamWriter sw = new StreamWriter(fullfilename, false, Encoding.UTF8))
{
WriteLine(sw, this.Intestazione());
WriteLine(sw, new Operazione().Intestazione());
foreach (Operazione op in opTotali)
{
WriteLine(sw, op.ToString());
}
WriteLine(sw, new Conto().Intestazione());
foreach (Conto op in cntTotali)
{
WriteLine(sw, op.ToString());
}
WriteLine(sw, new OpStandard().Intestazione());
foreach (OpStandard op in opStdTotali)
{
WriteLine(sw, op.ToString());
}
this.filename = fullfilename;
}
}
catch (Exception ex)
{
if(LogAbilitato)
lstLog.Add("Errore salvataggio file:\n"+ex.ToString());
ret = false;
}
return ret;
}
/// <summary>
/// Azzera i dati in memoria
/// </summary>
public void New()
{
opTotali.Clear();
opVisibili.Clear();
cntTotali.Clear();
opStdTotali.Clear();
consTotali.Clear();
filename = "";
status = true;
}
/// <summary>
/// Apre il file indicato
/// </summary>
/// <param name="fullfilename">Nome completo di path</param>
/// <returns></returns>
public bool Open(string fullfilename)
{
bool ret = true;
readwriteCripted = false;
readwritePassword = "";
readStat rstat = readStat.Indefinito;
status = true;
try
{
using (StreamReader sr = new StreamReader(fullfilename, Encoding.UTF8))
{
if (ReadFirstLineAndSetCripted(sr, ref rstat, ref readwritePassword))
{
while (sr.Peek() >= 0)
{
string rline = ReadLine(sr);
try
{
if (rline == new Operazione().Intestazione())
rstat = readStat.Operazioni;
else if (rline == new Conto().Intestazione())
rstat = readStat.Conti;
else if (rline == new OpStandard().Intestazione())
rstat = readStat.OpStandard;
//else if (rline == Operazioni.intestazioneFile) // ELIMINARE
// rstat = readStat.Intestazione; // ELIMINARE
else
{
switch (rstat)
{
case readStat.Operazioni:
{
Operazione op = new Operazione();
if (op.FromString(rline))
{
opVisibili.Add(op);
}
else
{
throw new Exception("Errore analisi linea: " + rline, new Exception("Errore analisi linea"));
}
}
break;
case readStat.Conti:
{
Conto op = new Conto();
if (op.FromString(rline))
{
cntTotali.Add(op);
}
else
{
throw new Exception("Errore analisi linea: " + rline, new Exception("Errore analisi linea"));
}
}
break;
case readStat.OpStandard:
{
OpStandard op = new OpStandard();
if (op.FromString(rline))
{
opStdTotali.Add(op);
}
else
{
throw new Exception("Errore analisi linea: " + rline, new Exception("Errore analisi linea"));
}
}
break;
case readStat.Intestazione:
break;
case readStat.Indefinito:
throw new Exception("Errore sintattico nel file" + rline, new Exception("Errore sintattico nel file"));
}
}
ret = true;
}
catch (Exception ex)
{
if (LogAbilitato)
lstLog.Add("Errore in importazione dati:\n" + ex.ToString());
ret = false;
status = false;
}
}
}
}
}
catch (Exception ex)
{
if (LogAbilitato)
lstLog.Add("Errore in lettura file:\n" + ex.ToString());
ret = false;
status = false;
}
if(ret)
this.filename = fullfilename;
return ret;
}
/// <summary>
/// Restituisce la stringa di intestazione del file
/// </summary>
/// <returns></returns>
public string Intestazione()
{
return Operazioni.intestazioneFile;
}
/// <summary>
/// Legge una linea da file
/// </summary>
/// <param name="sr">Streamreader</param>
/// <returns></returns>
private string ReadLine(StreamReader sr)
{
string str = "";
str = sr.ReadLine();
if (readwriteCripted == true)
str = encriptor.Decrypt(str, readwritePassword);
return str;
}
/// <summary>
/// Scrive la linea su file
/// </summary>
/// <param name="sw">Streamwriter</param>
/// <param name="txt">linea da scrivere (senza newline finale)</param>
private void WriteLine(StreamWriter sw, string txt)
{
if(readwriteCripted)
{
txt = encriptor.Encrypt(txt, readwritePassword);
}
sw.WriteLine(txt);
}
/// <summary>
/// Legge la prima riga del file e determina se criptato e se con password principale o secondaria
/// </summary>
/// <param name="sr"></param>
/// <param name="rstat"></param>
/// <param name="password"></param>
private bool ReadFirstLineAndSetCripted(StreamReader sr, ref readStat rstat, ref string password)
{
bool ok = false;
password = "";
readwriteCripted = false;
string str;
rstat = readStat.Indefinito;
try
{
str = sr.ReadLine(); // Legge la prima riga
if(str.Length > 0)
{
if(str.StartsWith(Operazioni.intestazioneFile)) // La verifica in chiaro
{
password = "";
readwriteCripted = false;
ok = true;
throw new Exception("Ok");
}
else // Prova con le password
{
string dec;
encriptor.ClearErrors();
encriptor.Salt = Salt;
dec = encriptor.Decrypt(str, Passphrase1);
if(dec.StartsWith(Operazioni.intestazioneFile))
{
password = Passphrase1;
readwriteCripted = true;
ok = true;
throw new Exception("Ok");
}
encriptor.ClearErrors();
dec = encriptor.Decrypt(str, Passphrase2);
if (dec.StartsWith(Operazioni.intestazioneFile))
{
password = Passphrase2;
readwriteCripted = true;
ok = true;
throw new Exception("Ok");
}
}
}
}
catch(Exception ex)
{
if (ex.Message != "Ok")
ok = false;
else
rstat = readStat.Intestazione;
}
return ok;
}
public bool CheckSaveOptions()
{
bool ok = true;
if ((Cripto1 == true) && !(Passphrase1.Length > 0)) // Crittografa primario con password nulla
ok = false;
if (AttivoSecondario == true) // Se salvataggio secondario...
{
if (!(NomeSecondario.Length > 0)) // ...ma salva secondario senza nome file
ok = false;
if ((Cripto2 == true) && !(Passphrase2.Length > 0)) // ...ma crittografa secondario con password nulla
ok = false;
}
if((Cripto1==true)||((Cripto2==true)&&(AttivoSecondario))) // Primario o secondario crittografati...
{
if (!(Salt.Length > 0)) // ...ma Salt nullo
ok = false;
}
return ok;
}
#region RICERCA e FILTRO
/// <summary>
/// Cerca OpStandard, null se non trovata
/// </summary>
/// <param name="numero"></param>
/// <returns></returns>
public OpStandard FindOpStandard(int numero)
{
OpStandard found = null;
foreach(OpStandard ops in opStdTotali)
{
if(ops.numero == numero)
{
found = ops;
break;
}
}
return found;
}
/// <summary>
/// Cerca Conto , null se non trovato
/// </summary>
/// <param name="numero"></param>
/// <returns></returns>
public Conto FindConto(int numero)
{
Conto found = null;
foreach (Conto ops in cntTotali)
{
if (ops.numero == numero)
{
found = ops;
break;
}
}
return found;
}
/// <summary>
/// Cerca la lista dei consuntivi per un numero di conto
/// </summary>
/// <param name="numero_conto"></param>
/// <returns></returns>
public List<Consuntivo> FindListaConsuntivi(int numero_conto)
{
List<Consuntivo> found = null;
if (dicConsuntivi.ContainsKey(numero_conto))
found = dicConsuntivi[numero_conto];
return found;
}
/// <summary>
/// Applica il filtro alle operazioni visualizzate
/// </summary>
public void ApplicaFiltro()
{
opVisibiliChangedEventEnabled = false;
opVisibili.Clear();
foreach(Operazione op in opTotali)
{
if (filtro.Check(op))
opVisibili.Add(op);
}
opVisibiliChangedEventEnabled = true;
filtro.IsAttivo = true;
}
/// <summary>
/// Rimuove il filtro dalle operazioni visualizzate
/// </summary>
public void CancellaFiltro()
{
opVisibiliChangedEventEnabled = false;
opVisibili.Clear();
foreach (Operazione op in opTotali)
opVisibili.Add(op);
opVisibiliChangedEventEnabled = true;
filtro.IsAttivo = false;
}
#endregion
#region CHECK e CALCOLO
/// <summary>
/// Verifica ed imposta lo stato
/// </summary>
/// <returns></returns>
public bool Check()
{
bool ok = true;
lstErr.Clear();
List<bool> oki = new List<bool>();
oki.Add(Check(checkType.Conti_Unique));
oki.Add(Check(checkType.OpStandard_Unique));
oki.Add(Check(checkType.OpStd_ContiValid));
oki.Add(Check(checkType.Op_ContiValid));
oki.Add(Check(checkType.Op_OpStdValid));
oki.Add(Check(checkType.Op_Consuntivo_old));
foreach (bool x in oki)
ok = ok && x;
this.status = ok; // Usa la proprietà, per attivare l'handler
return ok;
}
/// <summary>
/// Esegue il controllo specifico del tipo scelto
/// </summary>
/// <param name="typ"></param>
/// <returns></returns>
bool Check(checkType typ)
{
bool ok = false;
switch(typ)
{
case checkType.Conti_Unique:
ok = Riga.CheckUnique(this.conti); // verifica unicità dei numeri dei conti
if (!ok) lstErr.Add("Conti duplicati.");
break;
case checkType.OpStandard_Unique: // Verifica unicità dei numeri dei conti
ok = Riga.CheckUnique(this.opStandard);
if (!ok) lstErr.Add("Operazioni standard duplicate.");
break;
case checkType.OpStd_ContiValid: // Verifica che i conti delle opStandard esistano
ok = true;
foreach(OpStandard os in this.opStandard)
{
foreach(int nc in os.Conti())
{
bool found = false;
foreach(Conto c in this.cntTotali)
{
if (c.numero == Math.Abs(nc))
found = true;
}
if(!found)
{
ok = false;
lstErr.Add("Operazione standard <" + os.numero + "> con numero di conto non valido.");
}
}
}
break;
case checkType.Op_ContiValid: // Verifica che i conti delle Operazioni esistano
ok = true;
foreach (Operazione op in this.opTotali)
{
foreach (int nc in op.Conti())
{
bool found = false;
foreach (Conto c in this.cntTotali)
{
if (c.numero == Math.Abs(nc))
found = true;
}
if (!found)
{
ok = false;
lstErr.Add("Operazione <" + op.descrizione + "> del <" + op.data + "> con numero di conto non valido.");
}
}
}
break;
case checkType.Op_OpStdValid: // Verifica che le opStandard delle Operazioni esistano
ok = true;
foreach(Operazione op in this.opTotali)
{
bool found = false;
if(op.tipostd == 0)
{
found = true;
}
else
{
foreach (OpStandard os in this.opStandard)
{
if (os.numero == op.tipostd)
found = true;
}
}
if (!found)
{
ok = false;
lstErr.Add("Operazione <" + op.descrizione + "> del <" + op.data + "> con numero di operazione standard non valido.");
}
}
break;
case checkType.Op_Consuntivo_old:
// DateTime olderDate = DateTime.Now;
DateTime olderDate = DateTime.Now.Date.AddDays(-1);
ok = true;
foreach (Operazione op in this.opTotali)
{
DateTime dt = Riga.String2DateTime(op.data);
if((olderDate >= dt)&&(!op.consuntivo))
{
ok = false;
lstErr.Add("Operazione <" + op.descrizione + "> del <" + op.data + "> non impostata come consuntivo.");
}
}
break;
default:
throw new Exception("Caso CheckType non gestito.");
}
return ok;
}
/// <summary>
/// Inserisce i conti delle OpStandard nelle Operazioni
/// </summary>
public void EspandeOpStandard()
{
if (Check())
{
foreach (Operazione op in opTotali)
{
if (op.tipostd != 0)
{
OpStandard ops = FindOpStandard(op.tipostd);
if (ops != null)
{
op.conti = ops.conti;
}
else
{
lstErr.Add("L'operazione " + op.descrizione + " del " + op.data + " contiene l'operazione standard " + op.tipostd.ToString() + " inesistente.");
this.status = false;
}
}
}
}
}
/// <summary>
/// Ordina le operazioni
/// </summary>
/// <returns></returns>
public List<Operazione> OrdinaOperazioni()
{
List<Operazione> tmp = this.opTotali.ToList<Operazione>();
List<Operazione> tmpOrd = new List<Operazione>(tmp.OrderBy(x => x));
return tmpOrd;
}
/// <summary>
/// Genera le liste dei consuntivi
/// </summary>
/// <returns></returns>
public bool GeneraConsuntivi()
{
bool ok = Check();
try
{
if (ok)
{
// Crea il dizionario dei conti
dicConsuntivi.Clear();
try
{
foreach (Conto conto in conti)
{
int nc = conto.numero;
if (nc > 0)