-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPikaCRM.cpp
3339 lines (3044 loc) · 106 KB
/
PikaCRM.cpp
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
#include "PikaCRM.h"
#define TFILE <PikaCRM/PikaCRM.t>
#include <Core/t.h>
#define IMAGECLASS SrcImages // Adding Graphic
#define IMAGEFILE <PikaCRM/SrcImages.iml> //
#include <Draw/iml.h> //
#define TOPICFILE <PikaCRM/srcdoc.tpp/all.i> // Adding QTF for splash (and for other aims)
#include <Core/topic_group.h> //
#include <PikaCRM/sql/sql.ids> //for convenient use tables/columns name
//#include <string>
//#include <vector>
//#include "boost/smart_ptr.hpp"
#include "boost/tokenizer.hpp"
#include "DBCharset/DBCharset.h"
struct ConvContactNames : Convert
{
Value Format(const Value &q) const
{
const VectorMap<int, String> & contact_map= ValueTo< VectorMap<int, String> >(q);
String all_name;
for(int i = 0; i < contact_map.GetCount(); i++)//add already select contact to costomer
{
int contact_id=contact_map.GetKey(i);
String one_name(contact_map.Get(contact_id));
all_name+=one_name+"\n";
}
if(all_name.GetLength()>0)
{
all_name.Remove(all_name.GetLength()-1,1);//remove last "\n"
}
return all_name;
}
};
class DisplayColorNotNull : public Display
{
public:
virtual void PaintBackground(Draw& w, const Rect& r, const Value& q,
Color ink, Color paper, dword style) const
{
if( IsNull(q) ) paper = Color(255, 223, 223);
Display::PaintBackground(w, r, q, ink, paper, style);
};
};
class GDisplayNullRedBack : public GridDisplay
{
public:
void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style,
Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
//Color new_bg = bg;
if( IsNull(val) ) bg = Color(255, 223, 223);
GridDisplay::Paint(w, x, y, cx, cy, val, style, fg, bg, fnt, found, fs, fe);
};
};
class GDisplayNewUnsaved : public GridDisplay
{
public:
void Paint(Draw &w, int x, int y, int cx, int cy, const Value &val, dword style,
Color &fg, Color &bg, Font &fnt, bool found, int fs, int fe)
{
Value show=val;
if( -1==val ) show=t_("Unsaved");
GridDisplay::Paint(w, x, y, cx, cy, show, style, fg, bg, fnt, found, fs, fe);
};
};
PikaCRM::PikaCRM()
{
}
PikaCRM::~PikaCRM()
{
}
void PikaCRM::Initial()
{
String config_file_path = getConfigDirPath()+FILE_CONFIG;
String database_file_path = getConfigDirPath()+FILE_DATABASE;
SysLog.Info("Loading Settings...\n");
LoadConfig(config_file_path);
SetLanguage( SetLNGCharset( mConfig.Language, CHARSET_UTF8 ) );
int QtfHigh=20;
SplashSV splash;
splash.SplashInit("PikaCRM/srcdoc/Splash",QtfHigh,getLangLogo(mConfig.Language),SrcImages::Logo(),mConfig.Language);
splash.ShowSplash();
SetupUI();
splash.HideSplash();
if(mConfig.IsDBEncrypt)
{
if(mConfig.IsRememberPW)
{
SysLog.Info("config: Remeber the PW\n");
String syskey=GetSystemKey();
if( syskey.IsEmpty() )
{
String msg = t_("The function of \"Remeber the password\" is not work.");
#ifdef PLATFORM_POSIX
PromptOK( msg + "&" +
t_("To make it active, please excute \"hdsn`_permit.sh\" for PikaCRM to get reading hard disk serial number permission."));
#elif defined(PLATFORM_WIN32)
PromptOK( msg + "&" +
t_("To make it active, please give PikaCRM the permission to reading hard disk serial number."));
#endif
}
String key=CombineKey(syskey, mConfig.Password);
SysLog.Debug("systemPWKey:"+key+"\n");
if(mConfig.SystemPWKey.IsEmpty() || key!=mConfig.SystemPWKey)//use different PC
{
SysLog.Info("config: application is running on different PC\n");
if(!IsInputPWCheck()) throw ApExc("user cancel Input PW").SetHandle(ApExc::NONE);
}
//else
// ;//just using mConfig.Password;
}
else//not Remember PW
{
SysLog.Info("config: Not Remeber the PW\n");
if(!IsInputPWCheck()) throw ApExc("user cancel Input PW").SetHandle(ApExc::NONE);
}
}
else if(!mConfig.Password.IsEqual(PW_EMPTY)) //avoid hack set Encrypted value="0"
{
SysLog.Info("config: Encrypted value is false but password is not empty\n");
if(!IsInputPWCheck()) throw ApExc("user cancel Input PW").SetHandle(ApExc::NONE);
}
splash.ShowSplash();
splash.ShowSplashStatus(t_("Checking Database..."));
SysLog.Info(t_("Checking Database..."))<<"\n";
if(IsHaveDBFile(database_file_path))
{
splash.ShowSplashStatus(t_("Loading Database..."));
SysLog.Info(t_("Loading Database..."))<<"\n";
OpenMainDB(database_file_path);//OpenDB
}
else
{
SysLog.Info("setup the database file\n");
splash.HideSplash();
FirstWelcome();
if(!IsSetupDB(config_file_path)) throw ApExc("user cancel").SetHandle(ApExc::NONE);
splash.ShowSplash();
splash.ShowSplashStatus(t_("Creating the database..."));
SysLog.Info(t_("Creating the database..."))<<"\n";;
CreateMainDB(database_file_path);//CreateDB
}
//test if database OK-----------------------------------------------------
if(IsDBWork(mSqlite3Session))
;//donothing
else //pw error or not the file?
{
String msg = t_("Failed to load database! Maybe file is encrypted.\n"
"Last error: ") + SQL.GetLastError();
throw ApExc(msg).SetHandle(ApExc::SYS_FAIL);
///@remark setkey(password), wrong password may cause this
///if make multi database, must do reset pw and forget pw
}
//end test if database OK-----------------------------------------------------
if(0==GetDBVersion()) InitialDB();
/*
int past_db_ver=GetDBVersion();
if(past_db_ver<DATABASE_VERSION)
{
for(int i=past_db_ver;i<DATABASE_VERSION;++i)
{
UpdateToDB(i+1);
}
}
else if(past_db_ver>DATABASE_VERSION)
{
show can not up compatibility, please use the Latest version
}
*/
splash.ShowSplashStatus(t_("Normal Running..."));
SysLog.Info(t_("Normal Running..."))<<"\n";
splash.SetSplashTimer(500);
LoadAllData();
}
void PikaCRM::SetupUI()
{
CtrlLayout(MainFrom);
MainFrom.WhenClose=THISBACK(CloseMainFrom);
MainFrom.Sizeable().Zoomable();
MainFrom.Title(t_("Pika Customer Relationship Management"));
MainFrom.Icon(SrcImages::Icon16());
MainFrom.LargeIcon(SrcImages::Icon32());
//TabCtrl----------------------------------------------------------------------------
//MainFrom.tabMain.WhenSet=THISBACK1(TabChange,MainFrom.tabMain.Get());
CtrlLayout(Customer);
MainFrom.tabMain.Add(Customer.SizePos(), t_("Customers"));
CtrlLayout(Contact);
MainFrom.tabMain.Add(Contact.SizePos(), t_("Contacts"));
CtrlLayout(Merchandise);
MainFrom.tabMain.Add(Merchandise.SizePos(), t_("Merchandises"));
CtrlLayout(Order);
MainFrom.tabMain.Add(Order.SizePos(), t_("Orders"));
CtrlLayout(Event);
MainFrom.tabMain.Add(Event.SizePos(), t_("Events"));
CtrlLayout(Preference);
MainFrom.tabMain.Add(Preference.SizePos(), t_("Preferences"));
CtrlLayout(Help);
MainFrom.tabMain.Add(Help.SizePos(), t_("Help"));
//end TabCtrl------------------------------------------------------------------------
//set icon---------------------------------------------------------------------------
int imageh=SrcImages::CustomerAdd().GetHeight();
int fonth= GetStdFont().GetHeight();
int btnRectH=Customer.btnCreate.GetRect().Height()-Ctrl::VertLayoutZoom(6);//6 for 2 x sapce(3), define in Button::Paint
int scale=imageh;
if(imageh+fonth>btnRectH) //scale image
scale=btnRectH-fonth;
//fonth should be txtsz.cy
//Size txtsz = *text ? GetSmartTextSize(text, font, txtcx) : paintrect.GetStdSize();
//in LabelBase.cpp, but we can't, so whatever let it go.
Customer.btnCreate.SetImage(fitScale(SrcImages::CustomerAdd(),scale)).SetFont(StdFontS(-1));
Customer.btnModify.SetImage(fitScale(SrcImages::CustomerEdit(),scale)).SetFont(StdFontS(-1));
Customer.btnDelete.SetImage(fitScale(SrcImages::CustomerRemove(),scale)).SetFont(StdFontS(-1));
Customer.btnCancel.SetImage(fitScale(SrcImages::CustomerCancel(),scale)).SetFont(StdFontS(-1)).Hide();
Customer.btnCreateF.SetImage(fitScale(SrcImages::CustomAdd(),scale)).SetFont(StdFontS(-1));
Customer.btnModifyF.SetImage(fitScale(SrcImages::CustomEdit(),scale)).SetFont(StdFontS(-1));
Customer.btnDeleteF.SetImage(fitScale(SrcImages::CustomSetup(),scale)).SetFont(StdFontS(-1));
Customer.btnImport.SetImage(fitScale(SrcImages::Import(),scale)).SetFont(StdFontS(-1));
Customer.btnExport.SetImage(fitScale(SrcImages::Export(),scale)).SetFont(StdFontS(-1));
Customer.btnPrint.SetImage(fitScale(SrcImages::Print(),scale)).SetFont(StdFontS(-1));
Contact.btnCreate.SetImage(fitScale(SrcImages::ContactAdd(),scale)).SetFont(StdFontS(-1));
Contact.btnModify.SetImage(fitScale(SrcImages::ContactEdit(),scale)).SetFont(StdFontS(-1));
Contact.btnDelete.SetImage(fitScale(SrcImages::ContactRemove(),scale)).SetFont(StdFontS(-1));
Contact.btnCancel.SetImage(fitScale(SrcImages::ContactCancel(),scale)).SetFont(StdFontS(-1)).Hide();
Contact.btnCreateF.SetImage(fitScale(SrcImages::CustomAdd(),scale)).SetFont(StdFontS(-1));
Contact.btnModifyF.SetImage(fitScale(SrcImages::CustomEdit(),scale)).SetFont(StdFontS(-1));
Contact.btnDeleteF.SetImage(fitScale(SrcImages::CustomSetup(),scale)).SetFont(StdFontS(-1));
Contact.btnImport.SetImage(fitScale(SrcImages::Import(),scale)).SetFont(StdFontS(-1));
Contact.btnExport.SetImage(fitScale(SrcImages::Export(),scale)).SetFont(StdFontS(-1));
Contact.btnPrint.SetImage(fitScale(SrcImages::Print(),scale)).SetFont(StdFontS(-1));
Event.btnCreate.SetImage(fitScale(SrcImages::EventAdd(),scale)).SetFont(StdFontS(-1));
Event.btnModify.SetImage(fitScale(SrcImages::EventEdit(),scale)).SetFont(StdFontS(-1));
Event.btnDelete.SetImage(fitScale(SrcImages::EventRemove(),scale)).SetFont(StdFontS(-1));
Event.btnCancel.SetImage(fitScale(SrcImages::EventCancel(),scale)).SetFont(StdFontS(-1)).Hide();
Event.btnExport.SetImage(fitScale(SrcImages::Export(),scale)).SetFont(StdFontS(-1));
Event.btnPrint.SetImage(fitScale(SrcImages::Print(),scale)).SetFont(StdFontS(-1));
Order.btnCreate.SetImage(fitScale(SrcImages::OrderAdd(),scale)).SetFont(StdFontS(-1));
Order.btnModify.SetImage(fitScale(SrcImages::OrderEdit(),scale)).SetFont(StdFontS(-1));
Order.btnDelete.SetImage(fitScale(SrcImages::OrderRemove(),scale)).SetFont(StdFontS(-1));
Order.btnCancel.SetImage(fitScale(SrcImages::OrderCancel(),scale)).SetFont(StdFontS(-1)).Hide();
Order.btnExport.SetImage(fitScale(SrcImages::Export(),scale)).SetFont(StdFontS(-1));
Order.btnPrint.SetImage(fitScale(SrcImages::Print(),scale)).SetFont(StdFontS(-1));
Merchandise.btnCreate.SetImage(fitScale(SrcImages::MerchandiseAdd(),scale)).SetFont(StdFontS(-1));
Merchandise.btnModify.SetImage(fitScale(SrcImages::MerchandiseEdit(),scale)).SetFont(StdFontS(-1));
Merchandise.btnDelete.SetImage(fitScale(SrcImages::MerchandiseRemove(),scale)).SetFont(StdFontS(-1));
Merchandise.btnCancel.SetImage(fitScale(SrcImages::MerchandiseCancel(),scale)).SetFont(StdFontS(-1)).Hide();
Merchandise.btnCreateF.SetImage(fitScale(SrcImages::CustomAdd(),scale)).SetFont(StdFontS(-1));
Merchandise.btnModifyF.SetImage(fitScale(SrcImages::CustomEdit(),scale)).SetFont(StdFontS(-1));
Merchandise.btnDeleteF.SetImage(fitScale(SrcImages::CustomRemove(),scale)).SetFont(StdFontS(-1));
Merchandise.btnImport.SetImage(fitScale(SrcImages::Import(),scale)).SetFont(StdFontS(-1));
Merchandise.btnExport.SetImage(fitScale(SrcImages::Export(),scale)).SetFont(StdFontS(-1));
Merchandise.btnPrint.SetImage(fitScale(SrcImages::Print(),scale)).SetFont(StdFontS(-1));
//Customer Tab-----------------------------------------------------------------------
Customer.btnCreate <<= callback(&(Customer.Grid),&GridCtrl::DoAppend);
Customer.btnModify <<= callback(&(Customer.Grid),&GridCtrl::DoEdit);
Customer.btnDelete <<= callback(&(Customer.Grid),&GridCtrl::DoRemove);
Customer.btnCancel <<= callback(&(Customer.Grid),&GridCtrl::DoCancelEdit);
Customer.btnCreateF <<= THISBACK2(CreateField, &(Customer.Grid), "c");
Customer.btnModifyF <<= THISBACK2(ModifyField, &(Customer.Grid), "c");
Customer.btnDeleteF.Disable();
//Customer.btnDeleteF <<= callback(&(Customer.Grid),&GridCtrl::DoRemove);
Customer.btnImport <<= THISBACK2(ImportFile, &(Customer.Grid), "Customers");
Customer.btnExport <<= THISBACK2(ExportFile, &(Customer.Grid), "Customers");
Customer.btnPrint <<= THISBACK2(Print, &(Customer.Grid), "Customers");
Customer.Grid.Absolute();
Customer.Grid.AddIndex(C_ID).Default(-1);//for when create row before insert row
Customer.Grid.AddColumn(C_TITLE,t_("Title")).Edit(cesn).Width(mConfig.CWidth.Get(~C_TITLE));
Customer.Grid.AddColumn(C_PHONE,t_("Phone")).Edit(ces1).Width(mConfig.CWidth.Get(~C_PHONE));
Customer.Grid.AddColumn(C_ADDRESS,t_("Address")).Edit(ces2).Width(mConfig.CWidth.Get(~C_ADDRESS));
Customer.Grid.AddColumn(C_EMAIL,t_("Email")).Edit(ces3).Width(mConfig.CWidth.Get(~C_EMAIL));
Customer.Grid.AddColumn(C_WEBSITE,t_("Web site")).Edit(ces4).Width(mConfig.CWidth.Get(~C_WEBSITE));
///@important when SetConvert(), it will Convert when you add, so must add the type like RawToValue(temp) in LoadCustomer()
Customer.Grid.AddColumn(CO_NAME,t_("Contact")).Edit(mCustomerGridContactBtn).Width(mConfig.CWidth.Get(~CO_NAME));//.SetConvert(Single<ConvContactNames>());
mCustomerGridContactBtn.AddButton().SetLabel("...").WhenPush=THISBACK(CustomerGridContactBtnClick);
Customer.Grid.AddIndex(CONTACTS_MAP);
Customer.Grid.AddColumn(C_0).Hidden().Width(mConfig.CWidth.Get(~C_0));
Customer.Grid.AddColumn(C_1).Hidden().Width(mConfig.CWidth.Get(~C_1));
Customer.Grid.AddColumn(C_2).Hidden().Width(mConfig.CWidth.Get(~C_2));
Customer.Grid.AddColumn(C_3).Hidden().Width(mConfig.CWidth.Get(~C_3));
Customer.Grid.AddColumn(C_4).Hidden().Width(mConfig.CWidth.Get(~C_4));
Customer.Grid.AddColumn(C_5).Hidden().Width(mConfig.CWidth.Get(~C_5));
Customer.Grid.AddColumn(C_6).Hidden().Width(mConfig.CWidth.Get(~C_6));
Customer.Grid.AddColumn(C_7).Hidden().Width(mConfig.CWidth.Get(~C_7));
Customer.Grid.AddColumn(C_8).Hidden().Width(mConfig.CWidth.Get(~C_8));
Customer.Grid.AddColumn(C_9).Hidden().Width(mConfig.CWidth.Get(~C_9));
Customer.Grid.Appending().Removing().AskRemove().Editing().Canceling().ColorRows();//.Searching();
//Customer.Grid.RejectNullRow();.Duplicating().Accepting().Clipboard()//.Absolute() for horizontal scroll
//Customer.Grid.GetDisplay().SetTheme(2);
//Customer.Grid.WhenCreateRow = THISBACK(test);
Customer.Grid.WhenInsertRow = THISBACK(InsertCustomer);
Customer.Grid.WhenNewRow = THISBACK(NewCustomer);
Customer.Grid.WhenDuplicateRow=THISBACK(DuplicateCustomer);
Customer.Grid.WhenUpdateRow = THISBACK(UpdateCustomer);
Customer.Grid.WhenRemoveRow = THISBACK(RemoveCustomer);
Customer.Grid.WhenStartEdit = THISBACK(StartEditCustomer);
Customer.Grid.WhenEndEdit = THISBACK(EndEditCustomer);
//Customer Search------------------------------------------
Customer.Add(customer_search_bar.LeftPosZ(286, 84).TopPosZ(4, 20));
Customer.Grid.FindBar(customer_search_bar, Ctrl::HorzLayoutZoom(80));
Customer.btnSearchClear <<= THISBACK(BtnSearchClearClick);
Customer.btnSearchGo <<= THISBACK(BtnSearchGoClick);
//Contact Tab-----------------------------------------------------------------------
Contact.btnCreate <<= callback(&(Contact.Grid),&GridCtrl::DoAppend);
Contact.btnModify <<= callback(&(Contact.Grid),&GridCtrl::DoEdit);
Contact.btnDelete <<= callback(&(Contact.Grid),&GridCtrl::DoRemove);
Contact.btnCancel <<= callback(&(Contact.Grid),&GridCtrl::DoCancelEdit);
Contact.btnCreateF <<= THISBACK2(CreateField, &(Contact.Grid), "co");
Contact.btnModifyF <<= THISBACK2(ModifyField, &(Contact.Grid), "co");
Contact.btnDeleteF.Disable();
Contact.btnImport <<= THISBACK2(ImportFile, &(Contact.Grid), "Contacts");
Contact.btnExport <<= THISBACK2(ExportFile, &(Contact.Grid), "Contacts");
Contact.btnPrint <<= THISBACK2(Print, &(Contact.Grid), "Contacts");
Contact.Grid.Absolute();
Contact.Grid.AddIndex(CO_ID).Default(-1);
Contact.Grid.AddColumn(CO_NAME,t_("Name_")).Edit(coesn).Width(mConfig.COWidth.Get(~CO_NAME));
Contact.Grid.AddIndex(C_ID).Default(-1);
Contact.Grid.AddColumn(C_TITLE,t_("Customer")).Edit(mContactGridCustomerBtn).Width(mConfig.COWidth.Get(~C_TITLE));
mContactGridCustomerBtn.AddButton().SetLabel("...").WhenPush=THISBACK(ContactGridCustomerBtnClick);
Contact.Grid.AddColumn(CO_PHONE,t_("Phone")).Edit(coes1).Width(mConfig.COWidth.Get(~CO_PHONE));
Contact.Grid.AddColumn(CO_ADDRESS,t_("Address")).Edit(coes2).Width(mConfig.COWidth.Get(~CO_ADDRESS));
Contact.Grid.AddColumn(CO_EMAIL,t_("Email")).Edit(coes3).Width(mConfig.COWidth.Get(~CO_EMAIL));
Contact.Grid.AddColumn(CO_0).Hidden().Width(mConfig.COWidth.Get(~CO_0));
Contact.Grid.AddColumn(CO_1).Hidden().Width(mConfig.COWidth.Get(~CO_1));
Contact.Grid.AddColumn(CO_2).Hidden().Width(mConfig.COWidth.Get(~CO_2));
Contact.Grid.AddColumn(CO_3).Hidden().Width(mConfig.COWidth.Get(~CO_3));
Contact.Grid.AddColumn(CO_4).Hidden().Width(mConfig.COWidth.Get(~CO_4));
Contact.Grid.AddColumn(CO_5).Hidden().Width(mConfig.COWidth.Get(~CO_5));
Contact.Grid.AddColumn(CO_6).Hidden().Width(mConfig.COWidth.Get(~CO_6));
Contact.Grid.AddColumn(CO_7).Hidden().Width(mConfig.COWidth.Get(~CO_7));
Contact.Grid.AddColumn(CO_8).Hidden().Width(mConfig.COWidth.Get(~CO_8));
Contact.Grid.AddColumn(CO_9).Hidden().Width(mConfig.COWidth.Get(~CO_9));
Contact.Grid.Appending().Removing().AskRemove().Editing().Canceling().ColorRows();
//.Searching() will take the partent of Grid.FindBar then take away GridFind, so don't use
Contact.Grid.WhenInsertRow = THISBACK(InsertContact);
Contact.Grid.WhenUpdateRow = THISBACK(UpdateContact);
Contact.Grid.WhenRemoveRow = THISBACK(RemoveContact);
Contact.Grid.WhenChangeRow = THISBACK(ChangeContactRow);
Contact.Grid.WhenStartEdit = THISBACK(StartEditContact);
Contact.Grid.WhenEndEdit = THISBACK(EndEditContact);
//Contact Search------------------------------------------
Contact.Add(contact_search_bar.LeftPosZ(286, 84).TopPosZ(4, 20));
Contact.Grid.FindBar(contact_search_bar, Ctrl::HorzLayoutZoom(80));
Contact.btnSearchClear <<= callback2(&(Contact.Grid),&GridCtrl::ClearFound,true,true);
Contact.btnSearchGo <<= callback(&(Contact.Grid),&GridCtrl::DoFind);
//ContactInfo, giProfile, giCard
CtrlLayout(ContactInfo);
Contact.Add(ContactInfo);
ContactInfo.Indent(&(Contact.Grid));
ContactInfo.SetPos(HidePanel::BOTTOM);
ContactInfo.SetLength(730);
ContactInfo.giCard.SetCutSize(400,200);
ContactInfo.giProfile.WhenGrabed= THISBACK1(UpdateContactImage, true);
ContactInfo.giCard.WhenGrabed = THISBACK1(UpdateContactImage, false);
ContactInfo.giProfile.WhenClick = THISBACK(NewContactImage);
ContactInfo.giCard.WhenClick = THISBACK(NewContactImage);
//Event Tab-----------------------------------------------------------------------
Event.btnCreate <<= callback(&(Event.Grid),&GridCtrl::DoAppend);
Event.btnModify <<= callback(&(Event.Grid),&GridCtrl::DoEdit);
Event.btnDelete <<= callback(&(Event.Grid),&GridCtrl::DoRemove);
Event.btnCancel <<= callback(&(Event.Grid),&GridCtrl::DoCancelEdit);
Event.btnExport <<= THISBACK2(ExportFile, &(Event.Grid), "Events");
Event.btnPrint <<= THISBACK2(Print, &(Event.Grid), "Events");
Event.Grid.AddIndex(E_ID).Default(-1);//for when create row before insert row;
Event.Grid.AddIndex(C_ID);
Event.Grid.AddColumn(C_TITLE,t_("Customer")).Edit(mEventGridCustomerBtn);
mEventGridCustomerBtn.SetDisplay(Single<DisplayColorNotNull>());
mEventGridCustomerBtn.AddButton().SetLabel("...").WhenPush=THISBACK(EventGridCustomerBtnClick);
Event.Grid.AddColumn(E_ASK,t_("Request")).Edit(eesn);
//content
Event.Grid.AddColumn(E_STATUS,t_("Status")).Edit(mEventDropStatus);
mEventDropStatus.AddPlus(THISBACK(EventNewStatusClick));
Event.Grid.AddColumn(E_RTIME,t_("Request Time")).Edit(edt).Default(GetSysTime());
Event.Grid.AddColumn(E_CTIME,t_("Create Time"));
Event.Grid.AddColumn(E_NOTE,t_("Note")).Edit(ees1);
Event.Grid.Appending().Removing().AskRemove().Editing().Canceling().ColorRows();
Event.Grid.WhenInsertRow = THISBACK(InsertEvent);
Event.Grid.WhenUpdateRow = THISBACK(UpdateEvent);
Event.Grid.WhenRemoveRow = THISBACK(RemoveEvent);
Event.Grid.WhenStartEdit = THISBACK(StartEditEvent);
Event.Grid.WhenEndEdit = THISBACK(EndEditEvent);
//Event Search------------------------------------------
Event.Add(event_search_bar.LeftPosZ(147, 84).TopPosZ(4, 20));
Event.Grid.FindBar(event_search_bar, Ctrl::HorzLayoutZoom(80));
Event.btnSearchClear <<= callback2(&(Event.Grid),&GridCtrl::ClearFound,true,true);
Event.btnSearchGo <<= callback(&(Event.Grid),&GridCtrl::DoFind);
//Merchandise Tab-----------------------------------------------------------------------
Merchandise.btnCreate <<= callback(&(Merchandise.Grid),&GridCtrl::DoAppend);
Merchandise.btnModify <<= callback(&(Merchandise.Grid),&GridCtrl::DoEdit);
Merchandise.btnDelete <<= callback(&(Merchandise.Grid),&GridCtrl::DoRemove);
Merchandise.btnCancel <<= callback(&(Merchandise.Grid),&GridCtrl::DoCancelEdit);
Merchandise.btnCreateF <<= THISBACK2(CreateField, &(Merchandise.Grid), "m");
Merchandise.btnModifyF <<= THISBACK2(ModifyField, &(Merchandise.Grid), "m");
Merchandise.btnDeleteF.Disable();
Merchandise.btnImport <<= THISBACK2(ImportFile, &(Merchandise.Grid), "Merchandises");
Merchandise.btnExport <<= THISBACK2(ExportFile, &(Merchandise.Grid), "Merchandises");
Merchandise.btnPrint <<= THISBACK2(Print, &(Merchandise.Grid), "Merchandises");
Merchandise.Grid.Absolute();
Merchandise.Grid.AddIndex(M_ID).Default(-1);//for when create row before insert row;
Merchandise.Grid.AddColumn(M_NAME,t_("Product Name")).Edit(mesn).Width(mConfig.MWidth.Get(~M_NAME));
Merchandise.Grid.AddColumn(M_MODEL,t_("Product Model")).Edit(mes1).Width(mConfig.MWidth.Get(~M_MODEL));
Merchandise.Grid.AddColumn(M_PRICE,t_("Price")).Edit(med).Width(mConfig.MWidth.Get(~M_PRICE));
Merchandise.Grid.AddColumn(M_0).Hidden().Width(mConfig.MWidth.Get(~M_0));
Merchandise.Grid.AddColumn(M_1).Hidden().Width(mConfig.MWidth.Get(~M_1));
Merchandise.Grid.AddColumn(M_2).Hidden().Width(mConfig.MWidth.Get(~M_2));
Merchandise.Grid.AddColumn(M_3).Hidden().Width(mConfig.MWidth.Get(~M_3));
Merchandise.Grid.AddColumn(M_4).Hidden().Width(mConfig.MWidth.Get(~M_4));
Merchandise.Grid.AddColumn(M_5).Hidden().Width(mConfig.MWidth.Get(~M_5));
Merchandise.Grid.AddColumn(M_6).Hidden().Width(mConfig.MWidth.Get(~M_6));
Merchandise.Grid.AddColumn(M_7).Hidden().Width(mConfig.MWidth.Get(~M_7));
Merchandise.Grid.AddColumn(M_8).Hidden().Width(mConfig.MWidth.Get(~M_8));
Merchandise.Grid.AddColumn(M_9).Hidden().Width(mConfig.MWidth.Get(~M_9));
Merchandise.Grid.Appending().Removing().AskRemove().Editing().Canceling().ColorRows();
Merchandise.Grid.WhenInsertRow = THISBACK(InsertMerchandise);
Merchandise.Grid.WhenUpdateRow = THISBACK(UpdateMerchandise);
Merchandise.Grid.WhenRemoveRow = THISBACK(RemoveMerchandise);
Merchandise.Grid.WhenStartEdit = THISBACK(StartEditMerchandise);
Merchandise.Grid.WhenEndEdit = THISBACK(EndEditMerchandise);
//Merchandise Search------------------------------------------
Merchandise.Add(merchandise_search_bar.LeftPosZ(286, 84).TopPosZ(4, 20));
Merchandise.Grid.FindBar(merchandise_search_bar, Ctrl::HorzLayoutZoom(80));
Merchandise.btnSearchClear <<= callback2(&(Merchandise.Grid),&GridCtrl::ClearFound,true,true);
Merchandise.btnSearchGo <<= callback(&(Merchandise.Grid),&GridCtrl::DoFind);
//Order Tab-----------------------------------------------------------------------
Order.btnCreate <<= callback(&(Order.Grid),&GridCtrl::DoAppend);
Order.btnModify <<= callback(&(Order.Grid),&GridCtrl::DoEdit);
Order.btnDelete <<= callback(&(Order.Grid),&GridCtrl::DoRemove);
Order.btnCancel <<= callback(&(Order.Grid),&GridCtrl::DoCancelEdit);
Order.btnExport <<= THISBACK2(ExportFile, &(Order.Grid), "Orders");
Order.btnPrint <<= THISBACK2(Print, &(Order.Grid), "Orders");
Order.Grid.AddColumn(O_ID,t_("Order ID")).Default(-1).SetDisplay(Single<GDisplayNewUnsaved>());//-1 for when create row before insert row;
Order.Grid.AddIndex(C_ID);
Order.Grid.AddColumn(C_TITLE,t_("Customer")).Edit(mOrderGridCustomerBtn);
mOrderGridCustomerBtn.SetDisplay(Single<DisplayColorNotNull>());
mOrderGridCustomerBtn.AddButton().SetLabel("...").WhenPush=THISBACK(OrderGridCustomerBtnClick);
Order.Grid.AddColumn(O_SHIP_ADD,t_("Ship Add.")).Edit(oes1);
Order.Grid.AddColumn(O_BILL_ADD,t_("Bill Add.")).Edit(oes2);
Order.Grid.AddColumn(O_ORDER_DATE,t_("Order Date")).Edit(odd1).Default(GetSysDate());
Order.Grid.AddColumn(O_SHIP_DATE,t_("Ship Date")).Edit(odd2);
Order.Grid.AddColumn(O_STATUS,t_("Status")).Edit(oes3);
Order.Grid.AddColumn(O_NOTE,t_("Note")).Edit(oes4);
Order.Grid.Appending().Removing().AskRemove().Editing().Canceling().ColorRows();
Order.Grid.WhenInsertRow = THISBACK(InsertOrder);
Order.Grid.WhenUpdateRow = THISBACK(UpdateOrder);
Order.Grid.WhenRemoveRow = THISBACK(RemoveOrder);
Order.Grid.WhenChangeRow = THISBACK(ChangeOrder);
Order.Grid.WhenStartEdit = THISBACK(StartEditOrder);
Order.Grid.WhenEndEdit = THISBACK(EndEditOrder);
//Order Filter------------------------------------------
Order.dlFilter.Add(t_("All"));//0
Order.dlFilter.Add(t_("Past year"));//1
Order.dlFilter.Add(t_("Past half year"));//2
Order.dlFilter.Add(t_("Past 2 months"));//3
Order.dlFilter.Add(t_("Past month"));//4
Order.dlFilter.SetIndex(mConfig.OrderFilter);
Order.btnFilterSet <<= THISBACK(OrderFilterSet);
//Order Search------------------------------------------
Order.Add(order_search_bar.LeftPosZ(239, 84).TopPosZ(4, 20));
Order.Grid.FindBar(order_search_bar, Ctrl::HorzLayoutZoom(80));
Order.btnSearchClear <<= callback2(&(Order.Grid),&GridCtrl::ClearFound,true,true);
Order.btnSearchGo <<= callback(&(Order.Grid),&GridCtrl::DoFind);
//Order.ContactDrop-------------------------------------
Order.ContactDrop.AddColumn(t_("Name"));
Order.ContactDrop.AddColumn(t_("Phone"));
Order.ContactDrop.AddColumn(t_("Email"));
Order.ContactDrop.Width(200);
//Order.ContactDrop.SetValueColumn(0);
Order.ContactDrop.AddValueColumn(0).AddValueColumn(1);
Order.ContactDrop.Tip(t_("Contact"));
//Order.BuyItemGrid-------------------------------------
Order.BuyItemGrid.AddIndex(O_ID);
Order.BuyItemGrid.AddIndex(B_ID).Default(-1);//for when create row before insert row;
Order.BuyItemGrid.AddIndex(M_ID);
Order.BuyItemGrid.AddColumn(M_NAME,t_("Product Name / Model")).Edit(mBuyItemGridMerchBtn);
mBuyItemGridMerchBtn.SetDisplay(Single<DisplayColorNotNull>());
mBuyItemGridMerchBtn.AddButton().SetLabel("...").WhenPush=THISBACK(BuyItemGridMerchBtnClick);
//Order.BuyItemGrid.AddColumn(M_MODEL,t_("Product Model"));
Order.BuyItemGrid.AddColumn(M_PRICE,t_("Price"));
Order.BuyItemGrid.AddColumn(B_PRICE,t_("Purchase price")).Edit(bed);
Order.BuyItemGrid.AddColumn(B_NUMBER,t_("Quantity_")).Edit(beis).Default(0);
beis.NotNull();
Order.BuyItemGrid.Appending().Removing().AskRemove().Editing().Canceling().ColorRows();
Order.BuyItemGrid.SetToolBar();
Order.BuyItemGrid.WhenNewRow = THISBACK(NewBuyItem);
Order.BuyItemGrid.WhenInsertRow = THISBACK(InsertBuyItem);
Order.BuyItemGrid.WhenUpdateRow = THISBACK(UpdateBuyItem);
Order.BuyItemGrid.WhenRemoveRow = THISBACK(RemoveBuyItem);
//Preference Tab-----------------------------------------------------------------------
Preference.dlLang.Add( SetLNGCharset(LNG_('E','N','U','S'),CHARSET_UTF8) , "English" );
Preference.dlLang.Add( SetLNGCharset(LNG_('Z','H','T','W'),CHARSET_UTF8) , "繁體中文" );
Preference.dlLang.Add( SetLNGCharset(LNG_('Z','H','C','N'),CHARSET_UTF8) , "简体中文" );
//Preference.dlLang.Add( SetLNGCharset(LNG_('J','A','J','P'),CHARSET_UTF8) , "日本語" );
int index=Preference.dlLang.FindKey(mConfig.Language);
if(-1==index) Preference.dlLang.SetIndex(0);
else Preference.dlLang.SetIndex(index);
Preference.btnSave <<= THISBACK(SavePreference);
Preference.btnDBConfig <<= THISBACK(ConfigDB);
Preference.btnDBBackup <<= THISBACK(BackupDB);
Preference.btnDBRestore <<= THISBACK(RestoreDB);
//Help Tab-----------------------------------------------------------------------
Help.btnLicense << THISBACK(ShowLicense);
String lan = ToLower(LNGAsText(mConfig.Language & 0xfffff));
Topic about = GetTopic("PikaCRM/srcdoc/About$"+lan);
if (about.text.IsEmpty()) {
about = GetTopic("PikaCRM/srcdoc/About$en-us");
}
about.text=Replace(about.text,"##SoftwareVersion",SOFTWARE_VERSION);
about.text=Replace(about.text,"##DatabaseVersion",DATABASE_VERSION);
about.text=Replace(about.text,"##BuildDate",Format(BUILD_DATE));
about.text=Replace(about.text,"##RegisterState",t_("Unregistered"));//Unregistered
//about.text=Replace(about.text,"##RegisterState",t_("Registered"));
about.text=Replace(about.text,"##Limit",t_("First release version with full functions"));
Help.About.SetQTF(about);
Topic link = GetTopic("PikaCRM/srcdoc/Link$"+lan);
if (link.text.IsEmpty()) {
link = GetTopic("PikaCRM/srcdoc/Link$en-us");
}
Help.Link.SetQTF(link);
//WithImportLayout<TopWindow> Import;------------------------------------------------------------
CtrlLayoutOKCancel(Import,t_("Import File"));
Import.swFormat <<= 0;
Import.swFormat.DisableCase(1);
Import.swFormat.DisableCase(2);
Import.dlEncode.Add("Utf8");
Import.dlEncode.Add("Big5");
AddCodePage("Big5","CodePage/CP950.TXT");
Import.dlEncode.SetIndex(0);
}
//database control------------------------------------------------------------
bool PikaCRM::OpenDB(Sqlite3Session & sqlsession, const String & database_file_path, const String & password, bool log)
{
sqlsession.Close();
if(!sqlsession.Open(database_file_path))
{
SysLog.Error("can't open database file: "+database_file_path+"\n");
return false;
}
SysLog.Debug("opened database file: "+database_file_path+"\n");
if(log)
sqlsession.SetTrace();
if( !(password.IsEmpty() || password.IsEqual(PW_EMPTY)) )
{
SysLog.Info("set database encrypted key.\n");
if(!sqlsession.SetKey(getSwap1st2ndChar(password)))
{
SysLog.Error("sqlite3 set key error\n");
///@note we dont know how to deal this error, undefine
///so unknow return true or false
}
}
return true;
}
void PikaCRM::LoadAllData()
{
try
{
//Load and set customer field(UI+data)
LoadSetAllField();
//Load all tab data
LoadCustomer();
LoadContact();
LoadEvent();
LoadMerchandise();
LoadOrder();
}
catch(SqlExc &e)
{
//splash.HideSplash();
SysLog.Error(e+"\n");
Exclamation( t_("There is a database operation error.&"
"If data is not correct, please report to [^http://pika.sevenjay.tw/node/add/forum/2^ Bugs Report] with the log and last error: &")
+ DeQtfLf(SQL.GetLastError()));
}
}
void PikaCRM::LoadSetAllField()
{
SysLog.Info("Load and Set All Fields\n");
SQL.ExecuteX("select * from Field;");
mFieldMap.Clear();
SetAllFieldMap();
while(SQL.Fetch())
{
mFieldEditList.Add(new EditString());
if(SQL[F_TABLE]=="c")
{
FieldId & field=mFieldMap.Get("c")[SQL[F_ROWID]];//"c" [0] is FieldId with C_0
int c_index=Customer.Grid.FindCol(field.Id);
if(-1!=c_index)
{
Customer.Grid.GetColumn(c_index).Edit(mFieldEditList.Top()).Name(SQL[F_NAME].ToString()).Hidden(false);
field.IsUsed=true;
}
}
else if(SQL[F_TABLE]=="co")
{
FieldId & field=mFieldMap.Get("co")[SQL[F_ROWID]];
int c_index=Contact.Grid.FindCol(field.Id);
if(-1!=c_index)
{
Contact.Grid.GetColumn(c_index).Edit(mFieldEditList.Top()).Name(SQL[F_NAME].ToString()).Hidden(false);
field.IsUsed=true;
}
}
else if(SQL[F_TABLE]=="m")
{
FieldId & field=mFieldMap.Get("m")[SQL[F_ROWID]];
int c_index=Merchandise.Grid.FindCol(field.Id);
if(-1!=c_index)
{
Merchandise.Grid.GetColumn(c_index).Edit(mFieldEditList.Top()).Name(SQL[F_NAME].ToString()).Hidden(false);
field.IsUsed=true;
}
}
}
}
void PikaCRM::CreateField(GridCtrl * grid, String f_table)
{
SysLog.Info("Create a custom field\n");
//UI--------------------------------------------
TopWindow d;
Button ok, cancel;
d.Title(t_("Create a custom field")).SetRect(0, 0, Ctrl::HorzLayoutZoom(180), Ctrl::VertLayoutZoom(80));
d.Add(ok.SetLabel(t_("OK")).LeftPosZ(20, 45).TopPosZ(50, 16));
d.Add(cancel.SetLabel(t_("Cancel")).LeftPosZ(100, 45).TopPosZ(50, 16));
ok.Ok() <<= d.Acceptor(IDOK);
cancel.Cancel() <<= d.Rejector(IDCANCEL);
EditStringNotNull edit;
Label title;
title.SetLabel(t_("Field title: "));
d.Add(title.LeftPosZ(15, 75).TopPosZ(20, 16));
d.Add(edit.LeftPosZ(70, 75).TopPosZ(20, 16));
//end UI--------------------------------------------
bool is_no_field;
if(d.Run()==IDOK) {
grid->Ready(false);
is_no_field=true;
for(int i=0; i<mFieldMap.Get(f_table).GetCount(); ++i){
FieldId & field=mFieldMap.Get(f_table)[i];//"c" [0] is FieldId with C_0
if(false==field.IsUsed){
is_no_field=false;
mFieldEditList.Add(new EditString());
int c_index=grid->FindCol(field.Id);
if(-1!=c_index)
{
grid->GetColumn(c_index).Edit(mFieldEditList.Top()).Name(edit.GetData().ToString()).Hidden(false).Width(50);
field.IsUsed=true;
//INSERT INTO "main"."Field" ("f_table","f_rowid","f_name") VALUES ('c','3','asdf')
try
{
SQL & Insert(FIELD)
(F_TABLE, f_table)
(F_ROWID, i)
(F_NAME, edit.GetData().ToString());
}
catch(SqlExc &e)
{
SysLog.Error(e+"\n");
Exclamation("[* " + DeQtfLf(e) + "]");
}
break;
}
}
}
grid->Ready(true);
if(is_no_field) Exclamation(t_("There is no more column for custom field."));
}
}
void PikaCRM::ModifyField(GridCtrl * grid, String f_table)
{
SysLog.Info("Modify Fields\n");
//UI--------------------------------------------
WithModifyFieldsLayout<TopWindow> d;
CtrlLayoutOKCancel(d,t_("Modify Fields"));
EditString edit;
Label title;
title.SetLabel(t_("Field title: "));
ArrayMap<int,StaticText> stList;
ArrayMap<int,EditString> esList;
int y_level=24;
int y_start=32;
//end UI--------------------------------------------
try
{
SQL.ExecuteX("select * from Field where f_table==?;",f_table);
while(SQL.Fetch())
{
stList.Add(SQL[F_ROWID],new StaticText());
esList.Add(SQL[F_ROWID],new EditString());
d.Add(stList.Top());
d.Add(esList.Top());
stList.Top().SetText(SQL[F_NAME].ToString()).LeftPosZ(16, 60).TopPosZ(y_start, 16);
esList.Top().LeftPosZ(104, 72).TopPosZ(y_start, 16);
y_start+=y_level;
}
if(d.Run()==IDOK) {
for(int i=0;i<esList.GetCount();++i)
{
if(""==(esList[i].GetData().ToString())) continue;
SQL & ::Update(FIELD) (F_NAME, ~(esList[i]))
.Where(F_TABLE == f_table && F_ROWID==esList.GetKey(i));
//update grid
FieldId & field=mFieldMap.Get(f_table)[esList.GetKey(i)];//"c" [0] is FieldId with C_0
int c_index=grid->FindCol(field.Id);
if(-1!=c_index)
{
grid->GetColumn(c_index).Name(esList[i].GetData().ToString());
}
}
}
}
catch(SqlExc &e)
{
SysLog.Error(e+"\n");
Exclamation("[* " + DeQtfLf(e) + "]");
}
}
void PikaCRM::LoadCustomer()
{
SysLog.Info("Load Customers\n");
Customer.Grid.Clear();
SQL.ExecuteX("select * from Customer;");
while(SQL.Fetch())
{
VectorMap<int, String> temp_contact_map;
Sql sql2;
sql2 & Select(CO_ID, CO_NAME).From(CONTACT).Where(C_ID == SQL[C_ID]);
while(sql2.Fetch())
{
temp_contact_map.Add(sql2[CO_ID], sql2[CO_NAME]);
}
const Value & raw_map = RawToValue(temp_contact_map);
Customer.Grid.Add();
Customer.Grid(C_ID) = SQL[C_ID];
Customer.Grid(C_TITLE) = SQL[C_TITLE];
Customer.Grid(C_PHONE) = SQL[C_PHONE];
Customer.Grid(C_ADDRESS) = SQL[C_ADDRESS];
Customer.Grid(C_EMAIL) = SQL[C_EMAIL];
Customer.Grid(C_WEBSITE) = SQL[C_WEBSITE];
Customer.Grid(CONTACTS_MAP) = raw_map;//this is must, "=" will set the same typeid for Value of GridCtrl with RawDeepToValue
Customer.Grid(CO_NAME) = ConvContactNames().Format(Customer.Grid(CONTACTS_MAP));
Customer.Grid(C_0) = SQL[C_0];
Customer.Grid(C_1) = SQL[C_1];
Customer.Grid(C_2) = SQL[C_2];
Customer.Grid(C_3) = SQL[C_3];
Customer.Grid(C_4) = SQL[C_4];
Customer.Grid(C_5) = SQL[C_5];
Customer.Grid(C_6) = SQL[C_6];
Customer.Grid(C_7) = SQL[C_7];
Customer.Grid(C_8) = SQL[C_8];
Customer.Grid(C_9) = SQL[C_9];
}
}
void PikaCRM::NewCustomer()
{
int costomer_id = Customer.Grid.Get(C_ID);//get C_ID value of the current row
if(-1==costomer_id)//no use in Customer.Grid.AddIndex(CONTACTS_MAP).Default(RawDeepToValue(temp_contact_map));
{ //so this is set the same typeid for Value of GridCtrl with RawDeepToValue
//to avoid "Invalid value conversion: "
VectorMap<int, String> temp_contact_map;
Customer.Grid(CONTACTS_MAP)=RawToValue(temp_contact_map);
}
}
void PikaCRM::InsertCustomer()
{
SysLog.Debug("Insert Customer\n");
try
{
SQL & Insert(CUSTOMER)
(C_TITLE, Customer.Grid(C_TITLE))
(C_PHONE, Customer.Grid(C_PHONE))
(C_ADDRESS,Customer.Grid(C_ADDRESS))
(C_EMAIL, Customer.Grid(C_EMAIL))
(C_WEBSITE,Customer.Grid(C_WEBSITE))
(C_0, Customer.Grid(C_0))
(C_1, Customer.Grid(C_1))
(C_2, Customer.Grid(C_2))
(C_3, Customer.Grid(C_3))
(C_4, Customer.Grid(C_4))
(C_5, Customer.Grid(C_5))
(C_6, Customer.Grid(C_6))
(C_7, Customer.Grid(C_7))
(C_8, Customer.Grid(C_8))
(C_9, Customer.Grid(C_9));
Customer.Grid(C_ID) = SQL.GetInsertedId();//it will return only one int primary key
//database set C_ID of CONTACTS_MAP's Contact to now
const VectorMap<int, String> & contact_map= ValueTo< VectorMap<int, String> >(Customer.Grid(CONTACTS_MAP));
for(int i = 0; i < contact_map.GetCount(); i++)//add already select contact to customer
{
int contact_id=contact_map.GetKey(i);
SQL & Update(CONTACT) (C_ID, Customer.Grid(C_ID)).Where(CO_ID == contact_id);
//update Contact.Grid(C_TITLE);
int contact_row=Contact.Grid.Find(contact_id,CO_ID);
Contact.Grid.Set(contact_row,C_TITLE,Customer.Grid(C_TITLE));
Contact.Grid.Set(contact_row,C_ID,Customer.Grid(C_ID));
}
}
catch(SqlExc &e)
{
Customer.Grid.CancelInsert();
SysLog.Error(e+"\n");
Exclamation("[* " + DeQtfLf(e) + "]");
}
}
void PikaCRM::DuplicateCustomer()///@note not use, because not support multiselect duplicate
{
Customer.Grid(CO_NAME)="";
InsertCustomer();
}
void PikaCRM::UpdateCustomer()
{
SysLog.Debug("Update Customer\n");
try
{
SQL & ::Update(CUSTOMER)
(C_TITLE, Customer.Grid(C_TITLE))
(C_PHONE, Customer.Grid(C_PHONE))
(C_ADDRESS,Customer.Grid(C_ADDRESS))
(C_EMAIL, Customer.Grid(C_EMAIL))
(C_WEBSITE,Customer.Grid(C_WEBSITE))
(C_0, Customer.Grid(C_0))
(C_1, Customer.Grid(C_1))
(C_2, Customer.Grid(C_2))
(C_3, Customer.Grid(C_3))
(C_4, Customer.Grid(C_4))
(C_5, Customer.Grid(C_5))
(C_6, Customer.Grid(C_6))
(C_7, Customer.Grid(C_7))
(C_8, Customer.Grid(C_8))
(C_9, Customer.Grid(C_9))
.Where(C_ID == Customer.Grid(C_ID));
//update Contact.Grid(C_TITLE)
const VectorMap<int, String> & contact_map= ValueTo< VectorMap<int, String> >(Customer.Grid(CONTACTS_MAP));
for(int i = 0; i < contact_map.GetCount(); i++)
{
int contact_id=contact_map.GetKey(i);
int contact_row=Contact.Grid.Find(contact_id,CO_ID);
Contact.Grid.Set(contact_row,C_TITLE,Customer.Grid(C_TITLE));
}
}
catch(SqlExc &e)
{
Customer.Grid.CancelUpdate();
SysLog.Error(e+"\n");
Exclamation("[* " + DeQtfLf(e) + "]");
}
}
void PikaCRM::RemoveCustomer()
{
SysLog.Debug("Remove Customer\n");
const VectorMap<int, String> & contact_map= ValueTo< VectorMap<int, String> >(Customer.Grid(CONTACTS_MAP));
try
{
SQL & Delete(CUSTOMER).Where(C_ID == Customer.Grid(C_ID));
///@remark just clear customer in contact, this will be a performance issue
for(int i = 0; i < contact_map.GetCount(); i++)
{
int contact_id=contact_map.GetKey(i);
SQL.ExecuteX("UPDATE main.Contact SET c_id = -1 WHERE co_id = ?;", contact_id);
//clear Contact.Grid(C_TITLE);
int contact_row=Contact.Grid.Find(contact_id,CO_ID);
Contact.Grid.Set(contact_row,C_TITLE,"");
Contact.Grid.Set(contact_row,C_ID,-1);//there is no use for Contact.Grid.Set(C_ID,NULL) with ever set some data
}
}
catch(SqlExc &e)
{
Customer.Grid.CancelRemove();
SysLog.Error(e+"\n");
Exclamation("[* " + DeQtfLf(e) + "]");
}
}
void PikaCRM::LoadContact()
{
SysLog.Info("Load Contacts\n");
Contact.Grid.Clear();
SQL.ExecuteX("select co_id, Contact.c_id, c_title, co_name, co_phone, co_address, co_email from Contact left outer join Customer on Contact.c_id = Customer.c_id;");
while(SQL.Fetch())
{
Contact.Grid.Add(SQL[CO_ID],SQL[CO_NAME],SQL[C_ID],SQL[C_TITLE],SQL[CO_PHONE],SQL[CO_ADDRESS],SQL[CO_EMAIL],SQL[CO_0],SQL[CO_1],SQL[CO_2],SQL[CO_3],SQL[CO_4],SQL[CO_5],SQL[CO_6],SQL[CO_7],SQL[CO_8],SQL[CO_9]);
}
}
void PikaCRM::InsertContact()
{
SysLog.Debug("Insert Contact\n");
try
{
SQL & Insert(CONTACT)
(CO_NAME, Contact.Grid(CO_NAME))
(C_ID, Contact.Grid(C_ID))
(CO_PHONE, Contact.Grid(CO_PHONE))
(CO_ADDRESS,Contact.Grid(CO_ADDRESS))
(CO_EMAIL, Contact.Grid(CO_EMAIL))
(CO_0, Contact.Grid(CO_0))
(CO_1, Contact.Grid(CO_1))
(CO_2, Contact.Grid(CO_2))
(CO_3, Contact.Grid(CO_3))
(CO_4, Contact.Grid(CO_4))
(CO_5, Contact.Grid(CO_5))
(CO_6, Contact.Grid(CO_6))
(CO_7, Contact.Grid(CO_7))
(CO_8, Contact.Grid(CO_8))
(CO_9, Contact.Grid(CO_9));
Contact.Grid(CO_ID) = SQL.GetInsertedId();//it will return only one int primary key
int customer_row=Customer.Grid.Find(Contact.Grid(C_ID), C_ID);
if(-1!=customer_row) //update for customer.grid contact list, add
{
const VectorMap<int, String> & contact_map = ValueTo< VectorMap<int, String> >(Customer.Grid.Get(customer_row, CONTACTS_MAP));
VectorMap<int, String> new_contact_map = contact_map;
new_contact_map.Add(Contact.Grid(CO_ID),Contact.Grid(CO_NAME));