/
MainForm.cs
1925 lines (1710 loc) · 81 KB
/
MainForm.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
/*-----------------------------------------------------------
* QBMigrationTool : Tool for migrating data and ad-hoc
* querying of QB file
*
* Description: Tool for migrating data from QB to custom DB
* and for ad-hoc querying of the QB file.
*
* Created: 5/27/2014
*
* Copyright © 2014 Unified Communications Inc. All rights reserved.
*
*----------------------------------------------------------
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using Interop.QBXMLRP2;
using System.Collections.Generic;
using rototrack_data_access;
using rototrack_model;
using System.Linq;
using System.Timers;
using zochnet_utils;
using System.Reflection;
using System.Text;
using System.IO;
using System.Threading;
namespace QBMigrationTool
{
/// <summary>
/// CustomerAddForm shows how to invoke QuickBooks qbXMLRP COM object
/// It uses .NET to create qbXML request and parse qbXML response
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
#region Member Data
private bool stopUpdateAmountThread;
private Thread updateAmountThread = null;
private bool updateAmountThreadBusy = false;
private System.Timers.Timer aTimer;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button Exit;
private TextBox tbResults;
private Button btnGetQBListID;
private TextBox tbWorkOrderNumber;
private Label label2;
private Button btnRunXml;
private TextBox tbXML;
private Label label4;
private Button btnAutoSync;
private Label labelBuildType;
private NumericUpDown numericUpDownSyncDuration;
private Label label5;
private Button btnSyncNow;
private Label label1;
private Button buttonSyncWorkOrders;
private Button buttonSyncSalesReps;
private Button buttonSyncVacSick;
private Button buttonSyncSales;
private Button button1;
private Button button2;
private Button buttonSyncItems;
private Button buttonSyncEstimates;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
#endregion
#region Main Form Code
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
numericUpDownSyncDuration.Value = 10;
numericUpDownSyncDuration.Maximum = 30;
numericUpDownSyncDuration.Minimum = 1;
labelBuildType.Text = RototrackConfig.GetBuildType();
//aTimer = new System.Timers.Timer(10000);
aTimer = new System.Timers.Timer((double)numericUpDownSyncDuration.Value * 60.0 * 1000.0);
aTimer.Elapsed += aTimer_Elapsed;
this.stopUpdateAmountThread = false;
// Initialize web security for creating users
try
{
RotoTrackDbUtils.InitializeWebSecurityIfNotAlready();
}
catch (Exception e)
{
MessageBox.Show("Unable to access database. Application will now exit. Please make sure the database is accessible and try again." + e.Message);
throw(e);
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
static void Main()
{
MainForm mf = null;
try
{
mf = new MainForm();
Application.Run(mf);
}
catch (Exception)
{
Application.ExitThread();
}
}
#endregion
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label3 = new System.Windows.Forms.Label();
this.Exit = new System.Windows.Forms.Button();
this.tbResults = new System.Windows.Forms.TextBox();
this.btnGetQBListID = new System.Windows.Forms.Button();
this.tbWorkOrderNumber = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.btnRunXml = new System.Windows.Forms.Button();
this.tbXML = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.btnAutoSync = new System.Windows.Forms.Button();
this.labelBuildType = new System.Windows.Forms.Label();
this.numericUpDownSyncDuration = new System.Windows.Forms.NumericUpDown();
this.label5 = new System.Windows.Forms.Label();
this.btnSyncNow = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.buttonSyncWorkOrders = new System.Windows.Forms.Button();
this.buttonSyncSalesReps = new System.Windows.Forms.Button();
this.buttonSyncVacSick = new System.Windows.Forms.Button();
this.buttonSyncSales = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.buttonSyncItems = new System.Windows.Forms.Button();
this.buttonSyncEstimates = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownSyncDuration)).BeginInit();
this.SuspendLayout();
//
// label3
//
this.label3.Location = new System.Drawing.Point(12, 132);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(368, 14);
this.label3.TabIndex = 13;
this.label3.Text = "Note: You need to have QuickBooks with a company file opened.";
//
// Exit
//
this.Exit.Location = new System.Drawing.Point(669, 12);
this.Exit.Name = "Exit";
this.Exit.Size = new System.Drawing.Size(120, 24);
this.Exit.TabIndex = 12;
this.Exit.Text = "Exit";
this.Exit.Click += new System.EventHandler(this.Exit_Click);
//
// tbResults
//
this.tbResults.Location = new System.Drawing.Point(12, 149);
this.tbResults.Multiline = true;
this.tbResults.Name = "tbResults";
this.tbResults.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.tbResults.Size = new System.Drawing.Size(774, 633);
this.tbResults.TabIndex = 15;
//
// btnGetQBListID
//
this.btnGetQBListID.Location = new System.Drawing.Point(302, 65);
this.btnGetQBListID.Name = "btnGetQBListID";
this.btnGetQBListID.Size = new System.Drawing.Size(186, 23);
this.btnGetQBListID.TabIndex = 19;
this.btnGetQBListID.Text = "Get QBListID And EditSeq";
this.btnGetQBListID.UseVisualStyleBackColor = true;
this.btnGetQBListID.Click += new System.EventHandler(this.btnGetQBListID_Click);
//
// tbWorkOrderNumber
//
this.tbWorkOrderNumber.Location = new System.Drawing.Point(58, 67);
this.tbWorkOrderNumber.Name = "tbWorkOrderNumber";
this.tbWorkOrderNumber.Size = new System.Drawing.Size(238, 20);
this.tbWorkOrderNumber.TabIndex = 20;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(13, 70);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(36, 13);
this.label2.TabIndex = 21;
this.label2.Text = "WO #";
//
// btnRunXml
//
this.btnRunXml.Location = new System.Drawing.Point(310, 95);
this.btnRunXml.Name = "btnRunXml";
this.btnRunXml.Size = new System.Drawing.Size(75, 23);
this.btnRunXml.TabIndex = 22;
this.btnRunXml.Text = "Run XML";
this.btnRunXml.UseVisualStyleBackColor = true;
this.btnRunXml.Click += new System.EventHandler(this.btnRunXml_Click);
//
// tbXML
//
this.tbXML.Location = new System.Drawing.Point(66, 97);
this.tbXML.Name = "tbXML";
this.tbXML.Size = new System.Drawing.Size(238, 20);
this.tbXML.TabIndex = 23;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(13, 100);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(44, 13);
this.label4.TabIndex = 24;
this.label4.Text = "QBXML";
//
// btnAutoSync
//
this.btnAutoSync.Location = new System.Drawing.Point(12, 9);
this.btnAutoSync.Name = "btnAutoSync";
this.btnAutoSync.Size = new System.Drawing.Size(102, 23);
this.btnAutoSync.TabIndex = 28;
this.btnAutoSync.Text = "Enable Auto Sync";
this.btnAutoSync.UseVisualStyleBackColor = true;
this.btnAutoSync.Click += new System.EventHandler(this.btnAutoSync_Click);
//
// labelBuildType
//
this.labelBuildType.AutoSize = true;
this.labelBuildType.Location = new System.Drawing.Point(717, 132);
this.labelBuildType.Name = "labelBuildType";
this.labelBuildType.Size = new System.Drawing.Size(69, 13);
this.labelBuildType.TabIndex = 29;
this.labelBuildType.Text = "<Build Type>";
//
// numericUpDownSyncDuration
//
this.numericUpDownSyncDuration.Location = new System.Drawing.Point(120, 12);
this.numericUpDownSyncDuration.Name = "numericUpDownSyncDuration";
this.numericUpDownSyncDuration.Size = new System.Drawing.Size(120, 20);
this.numericUpDownSyncDuration.TabIndex = 30;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(246, 19);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(144, 13);
this.label5.TabIndex = 31;
this.label5.Text = "Auto Sync Duration (minutes)";
//
// btnSyncNow
//
this.btnSyncNow.Location = new System.Drawing.Point(12, 38);
this.btnSyncNow.Name = "btnSyncNow";
this.btnSyncNow.Size = new System.Drawing.Size(75, 23);
this.btnSyncNow.TabIndex = 32;
this.btnSyncNow.Text = "Sync Now";
this.btnSyncNow.UseVisualStyleBackColor = true;
this.btnSyncNow.Click += new System.EventHandler(this.btn_SyncNow_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(647, 132);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 14);
this.label1.TabIndex = 33;
this.label1.Text = "Build Type: ";
//
// buttonSyncWorkOrders
//
this.buttonSyncWorkOrders.Location = new System.Drawing.Point(93, 37);
this.buttonSyncWorkOrders.Name = "buttonSyncWorkOrders";
this.buttonSyncWorkOrders.Size = new System.Drawing.Size(75, 23);
this.buttonSyncWorkOrders.TabIndex = 34;
this.buttonSyncWorkOrders.Text = "Sync WOs";
this.buttonSyncWorkOrders.UseVisualStyleBackColor = true;
this.buttonSyncWorkOrders.Click += new System.EventHandler(this.buttonSyncWorkOrders_Click);
//
// buttonSyncSalesReps
//
this.buttonSyncSalesReps.Location = new System.Drawing.Point(174, 37);
this.buttonSyncSalesReps.Name = "buttonSyncSalesReps";
this.buttonSyncSalesReps.Size = new System.Drawing.Size(108, 23);
this.buttonSyncSalesReps.TabIndex = 35;
this.buttonSyncSalesReps.Text = "Sync Sales Reps";
this.buttonSyncSalesReps.UseVisualStyleBackColor = true;
this.buttonSyncSalesReps.Click += new System.EventHandler(this.buttonSyncSalesReps_Click);
//
// buttonSyncVacSick
//
this.buttonSyncVacSick.Location = new System.Drawing.Point(288, 38);
this.buttonSyncVacSick.Name = "buttonSyncVacSick";
this.buttonSyncVacSick.Size = new System.Drawing.Size(108, 23);
this.buttonSyncVacSick.TabIndex = 36;
this.buttonSyncVacSick.Text = "Sync Vac/Sick";
this.buttonSyncVacSick.UseVisualStyleBackColor = true;
this.buttonSyncVacSick.Click += new System.EventHandler(this.buttonSyncVacSick_Click);
//
// buttonSyncSales
//
this.buttonSyncSales.Location = new System.Drawing.Point(402, 38);
this.buttonSyncSales.Name = "buttonSyncSales";
this.buttonSyncSales.Size = new System.Drawing.Size(108, 23);
this.buttonSyncSales.TabIndex = 37;
this.buttonSyncSales.Text = "Sync Sales";
this.buttonSyncSales.UseVisualStyleBackColor = true;
this.buttonSyncSales.Click += new System.EventHandler(this.buttonSyncSales_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(516, 38);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(108, 23);
this.button1.TabIndex = 38;
this.button1.Text = "Sync Mileage";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.buttonSyncMileage_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(630, 38);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(108, 23);
this.button2.TabIndex = 39;
this.button2.Text = "Del Invoices";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.buttonRemoveDeletedInvoices_Click);
//
// buttonSyncItems
//
this.buttonSyncItems.Location = new System.Drawing.Point(744, 38);
this.buttonSyncItems.Name = "buttonSyncItems";
this.buttonSyncItems.Size = new System.Drawing.Size(108, 23);
this.buttonSyncItems.TabIndex = 40;
this.buttonSyncItems.Text = "Sync Items";
this.buttonSyncItems.UseVisualStyleBackColor = true;
this.buttonSyncItems.Click += new System.EventHandler(this.buttonSyncItems_Click);
//
// buttonSyncEstimates
//
this.buttonSyncEstimates.Location = new System.Drawing.Point(744, 67);
this.buttonSyncEstimates.Name = "buttonSyncEstimates";
this.buttonSyncEstimates.Size = new System.Drawing.Size(108, 23);
this.buttonSyncEstimates.TabIndex = 41;
this.buttonSyncEstimates.Text = "Sync Estimates";
this.buttonSyncEstimates.UseVisualStyleBackColor = true;
this.buttonSyncEstimates.Click += new System.EventHandler(this.buttonSyncEstimates_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(880, 794);
this.Controls.Add(this.buttonSyncEstimates);
this.Controls.Add(this.buttonSyncItems);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.buttonSyncSales);
this.Controls.Add(this.buttonSyncVacSick);
this.Controls.Add(this.buttonSyncSalesReps);
this.Controls.Add(this.buttonSyncWorkOrders);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnSyncNow);
this.Controls.Add(this.label5);
this.Controls.Add(this.numericUpDownSyncDuration);
this.Controls.Add(this.labelBuildType);
this.Controls.Add(this.btnAutoSync);
this.Controls.Add(this.label4);
this.Controls.Add(this.tbXML);
this.Controls.Add(this.btnRunXml);
this.Controls.Add(this.label2);
this.Controls.Add(this.tbWorkOrderNumber);
this.Controls.Add(this.btnGetQBListID);
this.Controls.Add(this.tbResults);
this.Controls.Add(this.label3);
this.Controls.Add(this.Exit);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "QuickBooks Migration Tool";
((System.ComponentModel.ISupportInitialize)(this.numericUpDownSyncDuration)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
#region Helper Methods for updating GUI
private void ClearStatus()
{
this.Invoke((MethodInvoker)delegate
{
tbResults.Clear();
});
this.Invoke((MethodInvoker)delegate
{
tbResults.Refresh();
});
}
private void SetStatus(string value)
{
this.Invoke((MethodInvoker)delegate
{
tbResults.Text = value;
});
}
private void AppendStatus(string value)
{
this.Invoke((MethodInvoker)delegate
{
tbResults.AppendText(value);
});
}
#endregion
#region Sync Helper Methods
private void AddCustomerType(int biID)
{
RotoTrackDb db = new RotoTrackDb();
BillingInstruction bi = db.BillingInstructions.Find(biID);
XmlDocument doc = CustomerTypeDAL.BuildAddRq(bi);
string response = QBUtils.DoRequest(doc);
CustomerTypeDAL.HandleAddResponse(response);
}
private void AddWorkOrder(int woID)
{
RotoTrackDb db = new RotoTrackDb();
WorkOrder wo = db.WorkOrders.Find(woID);
XmlDocument doc = WorkOrderDAL.BuildAddRq(wo);
string response = QBUtils.DoRequest(doc);
WorkOrderDAL.HandleAddResponse(response);
}
private void UpdateWorkOrderEditSequence(int woID)
{
RotoTrackDb db = new RotoTrackDb();
WorkOrder wo = db.WorkOrders.Find(woID);
string ownerID = "0";
XmlDocument doc = WorkOrderDAL.BuildQueryRequestForUpdate(wo.QBListId, ownerID);
string response = QBUtils.DoRequest(doc);
WorkOrderDAL.HandleResponseForUpdate(response);
}
private bool UpdateWorkOrder(int woID)
{
RotoTrackDb db = new RotoTrackDb();
WorkOrder wo = db.WorkOrders.Find(woID);
XmlDocument doc = WorkOrderDAL.BuildModRq(wo);
string response = QBUtils.DoRequest(doc);
bool status = WorkOrderDAL.HandleModResponse(response);
if (!status)
{
//Logging.RototrackErrorLog("QBMigrationTool: " + RototrackConfig.GetBuildType() + ": " + "Failed to update work order for WO#: " + wo.WorkOrderNumber + " ListID: " + wo.QBListId + ". Setting NeedToUpdateQB flag to true to try again.");
wo.NeedToUpdateQB = true;
db.Entry(wo).State = EntityState.Modified;
db.SaveChanges();
return false;
}
else
{
return true;
}
}
private void UpdateSalesRep(int woID)
{
RotoTrackDb db = new RotoTrackDb();
WorkOrder wo = db.WorkOrders.Find(woID);
XmlDocument doc = SiteAndAdditionalInfoDAL.BuildUpdateSalesRepRq(wo);
string response = QBUtils.DoRequest(doc);
}
private void UpdateSiteAndAdditionalInfo(int woID)
{
RotoTrackDb db = new RotoTrackDb();
WorkOrder wo = db.WorkOrders.Find(woID);
XmlDocument doc = SiteAndAdditionalInfoDAL.BuildUpdateRq(wo);
string response = QBUtils.DoRequest(doc);
bool status = SiteAndAdditionalInfoDAL.HandleResponse(response);
if (!status)
{
//Logging.RototrackErrorLog("QBMigrationTool: " + RototrackConfig.GetBuildType() + ": " + "Failed to update site and additional info for WO#: " + wo.WorkOrderNumber + " ListID: " + wo.QBListId + ". Setting NeedToUpdateQB flag to true to try again.");
wo.NeedToUpdateQB = true;
db.Entry(wo).State = EntityState.Modified;
db.SaveChanges();
}
}
private void AddVehicleMileage(int seID, int sdID)
{
RotoTrackDb db = new RotoTrackDb();
ServiceEntry se = db.ServiceEntries.Find(seID);
ServiceDetail sd = db.ServiceDetails.Find(sdID);
XmlDocument doc = VehicleMileageDAL.BuildAddRq(se, sd);
if (doc != null)
{
string response = QBUtils.DoRequest(doc);
VehicleMileageDAL.HandleAddResponse(response);
}
}
private void AddTimeTracking(int seID, int sdID, int serviceId, decimal hours)
{
RotoTrackDb db = new RotoTrackDb();
ServiceEntry se = db.ServiceEntries.Find(seID);
ServiceDetail sd = db.ServiceDetails.Find(sdID);
XmlDocument doc = TimeTrackingDAL.BuildAddRq(se, sd, serviceId, hours);
if (doc != null)
{
string response = QBUtils.DoRequest(doc);
TimeTrackingDAL.HandleAddResponse(response);
}
}
private bool RemoveTimeTracking(int ttID)
{
RotoTrackDb db = new RotoTrackDb();
DeletedTimeTracking tt = db.DeletedTimeTrackings.Find(ttID);
XmlDocument doc = TimeTrackingDAL.BuildRemoveRq(tt.QBTxnId);
string response = QBUtils.DoRequest(doc);
return TimeTrackingDAL.HandleRemoveResponse(response);
}
private bool RemoveVehicleMileage(int mtID)
{
RotoTrackDb db = new RotoTrackDb();
DeletedMileageTracking mt = db.DeletedMileageTrackings.Find(mtID);
XmlDocument doc = VehicleMileageDAL.BuildRemoveRq(mt.QBTxnId);
string response = QBUtils.DoRequest(doc);
return VehicleMileageDAL.HandleRemoveResponse(response);
}
private string SyncDataHelper(XmlDocument doc)
{
string response = QBUtils.DoRequest(doc);
return response;
}
private int GetNumUnsyncedWorkOrders()
{
RotoTrackDb db = new RotoTrackDb();
int count = 0;
var workorders = db.Database.SqlQuery<int>("select ID from WorkOrders where NeedToUpdateQB=1").ToList();
count += workorders.Count();
workorders = db.Database.SqlQuery<int>("select ID from WorkOrders where QBListId is null").ToList();
count += workorders.Count();
return count;
}
private int GetNumUnsyncedDSRs()
{
RotoTrackDb db = new RotoTrackDb();
int count = 0;
var dsrs = db.Database.SqlQuery<int>("select Id from DSRs where Status = 3 and IsSynchronizedWithQB = 0").ToList();
count += dsrs.Count();
dsrs = db.Database.SqlQuery<int>("select d.Id from DSRs d inner join ServiceEntries se on se.DSRId = d.Id where d.IsSynchronizedWithQB = 1 and (se.QBListIdForMileage is null or se.QBListIdForOTHours is null or se.QBListIdForRegularHours is null)").ToList();
count += dsrs.Count();
return count;
}
private void SetMileageToNA(int seID)
{
RotoTrackDb db = new RotoTrackDb();
ServiceEntry se = db.ServiceEntries.Find(seID);
se.QBListIdForMileage = "N/A";
db.Entry(se).State = EntityState.Modified;
db.SaveChanges();
}
private void SetRegularHoursToNA(int seID)
{
RotoTrackDb db = new RotoTrackDb();
ServiceEntry se = db.ServiceEntries.Find(seID);
se.QBListIdForRegularHours = "N/A";
db.Entry(se).State = EntityState.Modified;
db.SaveChanges();
}
private void SetOTHoursToNA(int seID)
{
RotoTrackDb db = new RotoTrackDb();
ServiceEntry se = db.ServiceEntries.Find(seID);
se.QBListIdForOTHours = "N/A";
db.Entry(se).State = EntityState.Modified;
db.SaveChanges();
}
#endregion
#region Main Sync Functions
private void DoSync()
{
DateTime nowTime = DateTime.Now;
string dayOfWeek = nowTime.DayOfWeek.ToString();
DateTime startTime = DateTime.Today.AddHours(18).AddMinutes(45);
DateTime endTime = DateTime.Today.AddHours(22).AddMinutes(15);
DateTime startTime2 = DateTime.Parse("2015-03-07 10:55:00.000");
DateTime endTime2 = DateTime.Parse("2015-03-07 15:05:00.000");
ClearStatus();
SetStatus("");
/*
//AppendStatus(dayOfWeek);
AppendStatus("NOTE: Sync is disabled on Thursdays from " + startTime.ToString() + " to " + endTime.ToString());
AppendStatus(Environment.NewLine);
if (dayOfWeek == "Thursday" && nowTime > startTime && nowTime < endTime)
{
AppendStatus("Sync is disabled on Thursdays from " + startTime.ToString() + " to " + endTime.ToString());
AppendStatus(Environment.NewLine);
return;
}
if (nowTime > startTime2 && nowTime < endTime2)
{
AppendStatus("Sync is disabled on Saturday March 7 2015 from " + startTime2.ToString() + " to " + endTime2.ToString());
AppendStatus(Environment.NewLine);
return;
}
*/
SyncWorkOrders();
SyncDSRs();
RemoveDSRTimeAndMileage();
SyncVacationAndSickTime();
UpdateUtilization();
int numUnsyncedWorkOrders = GetNumUnsyncedWorkOrders();
int numUnsyncedDSRs = GetNumUnsyncedDSRs();
if (numUnsyncedWorkOrders > 0)
{
Logging.RototrackErrorLog("QBMigrationTool: " + RototrackConfig.GetBuildType() + ": " + "There are " + numUnsyncedWorkOrders.ToString() + " Work Orders that failed to sync with QB!");
}
if (numUnsyncedDSRs > 0)
{
Logging.RototrackErrorLog("QBMigrationTool: " + RototrackConfig.GetBuildType() + ": " + "There are " + numUnsyncedDSRs.ToString() + " DSRs that failed to sync with QB!");
}
SyncQBData();
AppendStatus("Sync Completed: " + DateTime.Now.ToString());
AppendStatus(Environment.NewLine);
}
private void SyncSalesReps()
{
RotoTrackDb db = new RotoTrackDb();
List<WorkOrder> woList = null;
AppendStatus("Syncing Work Order Sales Reps...");
woList = db.WorkOrders.Where(wo => wo.statusValue != (int)WorkOrderStatus.Inactive).ToList();
foreach (WorkOrder wo in woList)
{
UpdateSalesRep(wo.Id);
}
AppendStatus("Done");
AppendStatus(Environment.NewLine);
}
private void SyncWorkOrders()
{
RotoTrackDb db = new RotoTrackDb();
List<WorkOrder> woList = null;
AppendStatus("Syncing Work Orders...");
// First, all new CustomerTypes (Billing Instructions)
List<BillingInstruction> biList = db.BillingInstructions.Where(bi => bi.QBListId == null).ToList();
foreach (BillingInstruction bi in biList)
{
AppendStatus("Add customer type " + bi.Id.ToString());
AddCustomerType(bi.Id);
// Find and add workorders that need to be added that referred to this
woList = db.WorkOrders.Where(wo => wo.QBListId == null && wo.BillingInstructionsId == bi.Id).ToList();
foreach (WorkOrder wo in woList)
{
AppendStatus("Add wo " + wo.Id.ToString());
AddWorkOrder(wo.Id);
// Evertime we add a work order, we have to immediately follow up with an update to the work order and the site--QB quirkiness
// But first, get the latest copy from the database
AppendStatus("Update wo " + wo.Id.ToString());
if (UpdateWorkOrder(wo.Id))
{
UpdateSiteAndAdditionalInfo(wo.Id);
}
}
}
// Next, all the new work orders that need to be added to Quickbooks.
woList = db.WorkOrders.Where(wo => wo.QBListId == null).ToList();
foreach (WorkOrder wo in woList)
{
// Include any where the BillingInstructions QBListID is not null
// since we will take care of adding such workorders after the new BillingInstruction
// is successfully added to QB as a result of a request above
BillingInstruction bi = db.BillingInstructions.Find(wo.BillingInstructionsId);
if (bi.QBListId != null)
{
AppendStatus("Add wo " + wo.Id.ToString());
AddWorkOrder(wo.Id);
// Evertime we add a work order, we have to immediately follow up with an update to the work order and the site--QB quirkiness
// But first, get the latest copy from the database
AppendStatus("Update wo " + wo.Id.ToString());
if (UpdateWorkOrder(wo.Id))
{
UpdateSiteAndAdditionalInfo(wo.Id);
}
}
}
// Next, all the work orders that need to be updated in Quickbooks where QBListId is not null
woList = db.WorkOrders.Where(wo => wo.NeedToUpdateQB == true && wo.QBListId != null).ToList();
foreach (WorkOrder wo in woList)
{
// Update EditSequence for the workorder and then update the work order and site.
UpdateWorkOrderEditSequence(wo.Id);
if (UpdateWorkOrder(wo.Id))
{
UpdateSiteAndAdditionalInfo(wo.Id);
}
}
// Finally, in case we encounter the quirk where we try to add a work order, but it's already in Quickbooks, let's get the ListID and EditSequence
// from QuickBooks and save it
woList = db.WorkOrders.Where(wo => wo.QBListId == null && wo.NeedToUpdateQB == true).ToList();
foreach (WorkOrder wo in woList)
{
CustomerDAL.UpdateAlreadyExistingWorkOrder(wo.Id);
if (UpdateWorkOrder(wo.Id))
{
UpdateSiteAndAdditionalInfo(wo.Id);
}
}
AppendStatus("Done");
AppendStatus(Environment.NewLine);
}
private void SyncDSRs()
{
AppendStatus("Syncing DSRs...");
RotoTrackDb db = new RotoTrackDb();
int approvedVal = (int)DSRStatus.Approved;
List<DSRLite> dsrList = db.DSRs.Where(f => f.IsSynchronizedWithQB == false && f.statusValue == approvedVal).Select(f => new DSRLite { Id = f.Id, WorkOrderId = f.WorkOrderId, WorkOrderGUID = f.WorkOrderGUID, Created = f.Created, Modified = f.Modified, DateWorked = f.DateWorked, TechnicianId = f.TechnicianId, IsSynchronizedWithQB = f.IsSynchronizedWithQB, statusValue = f.statusValue }).ToList();
foreach (DSRLite dsr in dsrList)
{
List<ServiceEntry> serviceEntryList = db.ServiceEntries.Where(f => f.DSRId == dsr.Id).ToList();
foreach (ServiceEntry se in serviceEntryList.ToList())
{
ServiceDetail sd = db.ServiceDetails.Find(se.ServiceDetailId);
if (se.QBListIdForMileage == null)
{
if (se.Mileage == 0)
{
SetMileageToNA(se.Id);
}
else
{
AddVehicleMileage(se.Id, se.ServiceDetailId);
}
}
if (se.QBListIdForRegularHours == null)
{
if (se.RegularHours == 0)
{
SetRegularHoursToNA(se.Id);
}
else
{
AddTimeTracking(se.Id, se.ServiceDetailId, sd.ServiceTypeId, se.RegularHours);
}
}
if (se.QBListIdForOTHours == null)
{
if (se.OTHours == 0)
{
SetOTHoursToNA(se.Id);
}
else
{
AddTimeTracking(se.Id, se.ServiceDetailId, sd.OTServiceTypeId, se.OTHours);
}
}
}
// Update status of DSR
DSR dsrToUpdate = db.DSRs.Find(dsr.Id);
dsrToUpdate.IsSynchronizedWithQB = true;
db.Entry(dsrToUpdate).State = EntityState.Modified;
db.SaveChanges();
}
AppendStatus("Done");
AppendStatus(Environment.NewLine);
}
private void SyncEstimates()
{
RotoTrackDb db = new RotoTrackDb();
DateTime fromModifiedDate = DateTime.Parse("2019-05-31 00:00:00.000");
//DateTime fromModifiedDate = DateTime.Parse("2023-8-1 00:00:00.000");
DateTime toModifiedDate = DateTime.Parse("2023-9-1 00:00:00.000");
toModifiedDate = DateTime.Now;
XmlDocument doc = null;
string response = "";
string fromDateTime = XmlUtils.GetAdjustedDateAsQBString(fromModifiedDate.ToShortDateString(), -1, false);
string toDateTime = XmlUtils.GetAdjustedDateAsQBString(toModifiedDate.ToShortDateString(), 1, false);
MessageBox.Show(fromDateTime);
MessageBox.Show(toDateTime);
try
{
AppendStatus("Sync Estimates...");
doc = EstimateDAL.BuildQueryRequest(fromDateTime, toDateTime);
response = SyncDataHelper(doc);
//MessageBox.Show(response);
AppendStatus(response);
AppendStatus("Done" + Environment.NewLine + "Processing...");
EstimateDAL.HandleResponse(response);
//AppendStatus("Done" + Environment.NewLine);
}
catch (Exception e)
{
MessageBox.Show("Error!");
MessageBox.Show("Error." + e.Message + e.StackTrace);
throw(e);
}
finally
{
AppendStatus("Done" + Environment.NewLine);
}
/*
AppendStatus("Done" + Environment.NewLine + "Processing...");
SalesOrderDAL.HandleResponse(response);
AppendStatus("Done" + Environment.NewLine);
*/
/*
AppendStatus("Removing deleted Sales Orders...");
SalesOrderDAL.RemoveDeleted();
AppendStatus("Done" + Environment.NewLine);
*/
}
private void SyncSalesOrders()
{
RotoTrackDb db = new RotoTrackDb();
DateTime fromModifiedDate = DateTime.Parse("2019-05-31 00:00:00.000"); //5-31-2019
//DateTime toModifiedDate = DateTime.Parse("2020-1-1 00:00:00.000");
DateTime toModifiedDate = DateTime.Parse("2023-9-1 00:00:00.000");
toModifiedDate = DateTime.Now;
XmlDocument doc = null;
string response = "";
string fromDateTime = XmlUtils.GetAdjustedDateAsQBString(fromModifiedDate.ToShortDateString(), -1, false);
string toDateTime = XmlUtils.GetAdjustedDateAsQBString(toModifiedDate.ToShortDateString(), 1, false);
MessageBox.Show(fromDateTime);
MessageBox.Show(toDateTime);
try
{
AppendStatus("Sync Sales Orders...");
doc = SalesOrderDAL.BuildQueryRequest(fromDateTime, toDateTime);
response = SyncDataHelper(doc);
//MessageBox.Show(response);
AppendStatus(response);
AppendStatus("Done" + Environment.NewLine + "Processing...");
SalesOrderDAL.HandleResponse(response);
//AppendStatus("Done" + Environment.NewLine);
}
catch (Exception e)
{
MessageBox.Show("Error!");
MessageBox.Show("Error." + e.Message + e.StackTrace);
throw(e);
}
finally
{
AppendStatus("Done" + Environment.NewLine);
}
/*
AppendStatus("Done" + Environment.NewLine + "Processing...");
SalesOrderDAL.HandleResponse(response);
AppendStatus("Done" + Environment.NewLine);
*/
/*
AppendStatus("Removing deleted Sales Orders...");
SalesOrderDAL.RemoveDeleted();
AppendStatus("Done" + Environment.NewLine);
*/
}
private void SyncItems()
{
RotoTrackDb db = new RotoTrackDb();
string activeStatus = "All";
AppendStatus("Sync Items...");
XmlDocument doc = ItemDAL.BuildQueryRequest(activeStatus);
string response = SyncDataHelper(doc);
AppendStatus("Done" + Environment.NewLine + "Processing...");
ItemDAL.HandleResponse(response);
AppendStatus("Done" + Environment.NewLine);
}
private void RemoveDSRTimeAndMileage()
{
AppendStatus("Removing Deleted DSR Time and Mileage...");
RotoTrackDb db = new RotoTrackDb();
List<DeletedTimeTracking> deletedTimeList = db.DeletedTimeTrackings.Where(f => f.IsSynchronizedWithQB == false).ToList();
foreach (DeletedTimeTracking deletedTime in deletedTimeList)
{
if (RemoveTimeTracking(deletedTime.Id))
{
deletedTime.IsSynchronizedWithQB = true;
db.Entry(deletedTime).State = EntityState.Modified;
db.SaveChanges();
}
}
List<DeletedMileageTracking> deletedMileageList = db.DeletedMileageTrackings.Where(f => f.IsSynchronizedWithQB == false).ToList();
foreach (DeletedMileageTracking deletedMileage in deletedMileageList)
{
if (RemoveVehicleMileage(deletedMileage.Id))