-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
1080 lines (1011 loc) · 36 KB
/
Copy pathserver.lua
File metadata and controls
1080 lines (1011 loc) · 36 KB
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
local QBCore = exports['qb-core']:GetCoreObject()
local Accounts = {}
local Statements = {}
RegisterNetEvent('DP-Banking:server:cancelAllCards', function()
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then
return
end
-- Eliminar todas las tarjetas bancarias del jugador
while Player.Functions.GetItemByName('bank_card') do
Player.Functions.RemoveItem('bank_card', 1)
end
-- Registrar en el log
TriggerEvent('qb-log:server:CreateLog', 'banking', 'Tarjetas Canceladas', 'red',
string.format('**%s** ha cancelado todas sus tarjetas bancarias', GetPlayerName(src)))
end)
-- Functions
local function getPlayerAndCitizenId(playerId)
local Player = QBCore.Functions.GetPlayer(playerId)
if not Player then
return nil, nil
end
return Player, Player.PlayerData.citizenid
end
local function GetNumberOfAccounts(citizenid)
local numberOfAccounts = 0
for _, account in pairs(Accounts) do
if account.citizenid == citizenid then
numberOfAccounts = numberOfAccounts + 1
end
end
return numberOfAccounts
end
-- Exported Functions
local function CreatePlayerAccount(playerId, accountName, accountBalance, accountUsers)
local Player, citizenid = getPlayerAndCitizenId(playerId)
if not Player or not citizenid then
return false
end
if Accounts[accountName] then
return false
end
Accounts[accountName] = {
citizenid = citizenid,
account_name = accountName,
account_balance = accountBalance,
account_type = 'shared',
users = accountUsers
}
local insertSuccess = MySQL.insert.await(
'INSERT INTO bank_accounts (citizenid, account_name, account_balance, account_type, users) VALUES (?, ?, ?, ?, ?)',
{citizenid, accountName, accountBalance, 'shared', accountUsers})
return insertSuccess
end
exports('CreatePlayerAccount', CreatePlayerAccount)
local function CreateJobAccount(accountName, accountBalance)
Accounts[accountName] = {
account_name = accountName,
account_balance = accountBalance,
account_type = 'job'
}
local insertSuccess = MySQL.insert.await(
'INSERT INTO bank_accounts (account_name, account_balance, account_type) VALUES (?, ?, ?)',
{accountName, accountBalance, 'job'})
return insertSuccess
end
exports('CreateJobAccount', CreateJobAccount)
local function CreateGangAccount(accountName, accountBalance)
Accounts[accountName] = {
account_name = accountName,
account_balance = accountBalance,
account_type = 'gang'
}
local insertSuccess = MySQL.insert.await(
'INSERT INTO bank_accounts (account_name, account_balance, account_type) VALUES (?, ?, ?)',
{accountName, accountBalance, 'gang'})
return insertSuccess
end
exports('CreateGangAccount', CreateGangAccount)
local function CreateBankStatement(playerId, account, amount, reason, statementType, accountType)
local Player, citizenid = getPlayerAndCitizenId(playerId)
if not Player or not citizenid then
return false
end
local newStatement = {
citizenid = citizenid,
amount = amount,
reason = reason,
date = os.time() * 1000,
statement_type = statementType
}
if accountType == 'player' or accountType == 'shared' then
if accountType == 'player' then
account = 'Personal'
end
if not Statements[citizenid] then
Statements[citizenid] = {}
end
if not Statements[citizenid][account] then
Statements[citizenid][account] = {}
end
Statements[citizenid][account][#Statements[citizenid][account] + 1] = newStatement
else
if not Statements[account] then
Statements[account] = {}
end
Statements[account][#Statements[account] + 1] = newStatement
end
local insertSuccess = MySQL.insert.await(
'INSERT INTO bank_statements (citizenid, account_name, amount, reason, statement_type) VALUES (?, ?, ?, ?, ?)',
{citizenid, account, amount, reason, statementType})
if not insertSuccess then
return false
end
return true
end
exports('CreateBankStatement', CreateBankStatement)
local function AddMoney(accountName, amount, reason)
if not reason then
reason = 'External Deposit'
end
local newStatement = {
amount = amount,
reason = reason,
date = os.time() * 1000,
statement_type = 'deposit'
}
if Accounts[accountName] then
local accountToUpdate = Accounts[accountName]
accountToUpdate.account_balance = accountToUpdate.account_balance + amount
if not Statements[accountName] then
Statements[accountName] = {}
end
Statements[accountName][#Statements[accountName] + 1] = newStatement
MySQL.insert.await(
'INSERT INTO bank_statements (account_name, amount, reason, statement_type) VALUES (?, ?, ?, ?)',
{accountName, amount, reason, 'deposit'})
local updateSuccess = MySQL.update.await(
'UPDATE bank_accounts SET account_balance = account_balance + ? WHERE account_name = ?',
{amount, accountName})
return updateSuccess
end
return false
end
exports('AddMoney', AddMoney)
exports('AddGangMoney', AddMoney)
local function RemoveMoney(accountName, amount, reason)
if not reason then
reason = 'External Withdrawal'
end
local newStatement = {
amount = amount,
reason = reason,
date = os.time() * 1000,
statement_type = 'withdraw'
}
if Accounts[accountName] then
local accountToUpdate = Accounts[accountName]
accountToUpdate.account_balance = accountToUpdate.account_balance - amount
if not Statements[accountName] then
Statements[accountName] = {}
end
Statements[accountName][#Statements[accountName] + 1] = newStatement
MySQL.insert.await(
'INSERT INTO bank_statements (account_name, amount, reason, statement_type) VALUES (?, ?, ?, ?)',
{accountName, amount, reason, 'withdraw'})
local updateSuccess = MySQL.update.await(
'UPDATE bank_accounts SET account_balance = account_balance - ? WHERE account_name = ?',
{amount, accountName})
return updateSuccess
end
return false
end
exports('RemoveMoney', RemoveMoney)
exports('RemoveGangMoney', RemoveMoney)
local function GetAccount(accountName)
if Accounts[accountName] then
return Accounts[accountName]
end
return nil
end
exports('GetAccount', GetAccount)
exports('GetGangAccount', GetAccount)
local function GetAccountBalance(accountName)
local account = GetAccount(accountName)
return account and account.account_balance or 0
end
exports('GetAccountBalance', GetAccountBalance)
-- Callbacks
QBCore.Functions.CreateCallback('DP-Banking:server:getUserInfo', function(source, cb, data)
local citizenid = data.citizenid
local Player = QBCore.Functions.GetPlayerByCitizenId(citizenid)
if not Player then
-- Buscar en la base de datos si el jugador no está en línea
local result = MySQL.single.await('SELECT charinfo FROM players WHERE citizenid = ?', {citizenid})
if result and result.charinfo then
local charinfo = json.decode(result.charinfo)
cb({
name = charinfo.firstname .. ' ' .. charinfo.lastname,
citizenid = citizenid
})
return
end
end
if Player then
cb({
name = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname,
citizenid = citizenid
})
return
end
cb({
name = "Usuario no encontrado",
citizenid = citizenid
})
end)
QBCore.Functions.CreateCallback('DP-Banking:server:openBank', function(source, cb)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return
end
local job = Player.PlayerData.job
local gang = Player.PlayerData.gang
local accounts = {}
local statements = {}
if job.name ~= 'unemployed' and not Accounts[job.name] then
CreateJobAccount(job.name, 0)
end
if gang.name ~= 'none' and not Accounts[gang.name] then
CreateGangAccount(gang.name, 0)
end
accounts[#accounts + 1] = {
account_name = 'Personal',
account_type = 'Personal',
account_balance = Player.PlayerData.money.bank
}
statements['Personal'] = Statements[citizenid] and Statements[citizenid]['Personal'] or {}
for accountName, accountInfo in pairs(Accounts) do
if accountInfo.citizenid == citizenid then
accounts[#accounts + 1] = accountInfo
if Statements[accountName] then
statements[accountName] = Statements[accountName]
end
end
if accountInfo.users and string.find(accountInfo.users, citizenid) then
accounts[#accounts + 1] = accountInfo
if Statements[accountName] then
statements[accountName] = Statements[accountName]
end
end
if (accountName == job.name and job.isboss) or (accountName == gang.name and gang.isboss) then
accounts[#accounts + 1] = accountInfo
if Statements[accountName] then
statements[accountName] = Statements[accountName]
end
end
end
cb(accounts, statements, Player.PlayerData)
end)
QBCore.Functions.CreateCallback('DP-Banking:server:openATM', function(source, cb)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return
end
local bankCards = Player.Functions.GetItemsByName('bank_card')
if not bankCards then
return QBCore.Functions.Notify(src, Lang:t('error.card'), 'error', 5000)
end
local acceptablePins = {}
for _, bankCard in ipairs(bankCards) do
acceptablePins[#acceptablePins + 1] = bankCard.info.cardPin
end
local job = Player.PlayerData.job
local gang = Player.PlayerData.gang
local accounts = {}
accounts[#accounts + 1] = {
account_name = 'Personal',
account_type = 'Personal',
account_balance = Player.PlayerData.money.bank
}
for accountName, accountInfo in pairs(Accounts) do
if accountInfo.citizenid == citizenid then
accounts[#accounts + 1] = accountInfo
end
if accountInfo.users and string.find(accountInfo.users, citizenid) then
accounts[#accounts + 1] = accountInfo
end
if (accountName == job.name and job.isboss) or (accountName == gang.name and gang.isboss) then
accounts[#accounts + 1] = accountInfo
end
end
cb(accounts, Player.PlayerData, acceptablePins)
end)
QBCore.Functions.CreateCallback('DP-Banking:server:withdraw', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local accountName = data.accountName
local withdrawAmount = tonumber(data.amount)
local reason = (data.reason ~= '' and data.reason) or 'Bank Withdrawal'
if accountName == 'Personal' then
local accountBalance = Player.PlayerData.money.bank
if accountBalance < withdrawAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
Player.Functions.RemoveMoney('bank', withdrawAmount, 'bank withdrawal')
Player.Functions.AddMoney('cash', withdrawAmount, 'bank withdrawal')
if not CreateBankStatement(src, 'Personal', withdrawAmount, reason, 'withdraw', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.withdraw')
})
end
if Accounts[accountName] then
local job = Player.PlayerData.job
local gang = Player.PlayerData.gang
if Accounts[accountName].account_type == 'job' and job.name ~= accountName and not job.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
if Accounts[accountName].account_type == 'gang' and gang.name ~= accountName and not gang.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local accountBalance = GetAccountBalance(accountName)
if accountBalance < withdrawAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
if not RemoveMoney(accountName, withdrawAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
Player.Functions.AddMoney('cash', withdrawAmount, 'bank account: ' .. accountName .. ' withdrawal')
if not CreateBankStatement(src, accountName, withdrawAmount, reason, 'withdraw',
Accounts[accountName].account_type) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.withdraw')
})
end
end)
QBCore.Functions.CreateCallback('DP-Banking:server:deposit', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local accountName = data.accountName
local depositAmount = tonumber(data.amount)
local reason = (data.reason ~= '' and data.reason) or 'Bank Deposit'
if accountName == 'Personal' then
local accountBalance = Player.PlayerData.money.cash
if accountBalance < depositAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
Player.Functions.RemoveMoney('cash', depositAmount, 'bank deposit')
Player.Functions.AddMoney('bank', depositAmount, 'bank deposit')
if not CreateBankStatement(src, 'Personal', depositAmount, reason, 'deposit', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.deposit')
})
end
if Accounts[accountName] then
local job = Player.PlayerData.job
local gang = Player.PlayerData.gang
if Accounts[accountName].account_type == 'job' and job.name ~= accountName and not job.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
if Accounts[accountName].account_type == 'gang' and gang.name ~= accountName and not gang.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
if Player.PlayerData.money.cash < depositAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
Player.Functions.RemoveMoney('cash', depositAmount, 'bank account: ' .. accountName .. ' deposit')
if not AddMoney(accountName, depositAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.deposit')
})
end
end)
QBCore.Functions.CreateCallback('DP-Banking:server:internalTransfer', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local job = Player.PlayerData.job
local gang = Player.PlayerData.gang
local fromAccountName = data.fromAccountName
local toAccountName = data.toAccountName
local transferAmount = tonumber(data.amount)
local reason = (data.reason ~= '' and data.reason) or 'Internal transfer'
if fromAccountName == 'Personal' then
if Player.PlayerData.money.bank < transferAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
Player.Functions.RemoveMoney('bank', transferAmount, reason)
if toAccountName == 'Personal' then
Player.Functions.AddMoney('bank', transferAmount, reason)
else
if not AddMoney(toAccountName, transferAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
end
if not CreateBankStatement(src, 'Personal', transferAmount, reason, 'withdraw', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.transfer')
})
elseif toAccountName == 'Personal' then
if Accounts[fromAccountName].account_type == 'job' and job.name ~= fromAccountName and not job.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
if Accounts[fromAccountName].account_type == 'gang' and gang.name ~= fromAccountName and not gang.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local fromAccountBalance = GetAccountBalance(fromAccountName)
if fromAccountBalance < transferAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
if not RemoveMoney(fromAccountName, transferAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
Player.Functions.AddMoney('bank', transferAmount, reason)
if not CreateBankStatement(src, 'Personal', transferAmount, reason, 'deposit', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.transfer')
})
else
if Accounts[fromAccountName].account_type == 'job' and job.name ~= fromAccountName and not job.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
if Accounts[fromAccountName].account_type == 'gang' and gang.name ~= fromAccountName and not gang.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local fromAccountBalance = GetAccountBalance(fromAccountName)
if fromAccountBalance < transferAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
if not RemoveMoney(fromAccountName, transferAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
if not AddMoney(toAccountName, transferAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.transfer')
})
end
end)
QBCore.Functions.CreateCallback('DP-Banking:server:externalTransfer', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local job = Player.PlayerData.job
local gang = Player.PlayerData.gang
local toAccountName = data.toAccountNumber
local toPlayer = QBCore.Functions.GetPlayerByCitizenId(toAccountName)
if not toPlayer then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local fromAccountName = data.fromAccountName
local transferAmount = tonumber(data.amount)
local reason = (data.reason ~= '' and data.reason) or 'External transfer'
if fromAccountName == 'Personal' then
if Player.PlayerData.money.bank < transferAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
Player.Functions.RemoveMoney('bank', transferAmount, reason)
toPlayer.Functions.AddMoney('bank', transferAmount, reason)
if not CreateBankStatement(src, 'Personal', transferAmount, reason, 'withdraw', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
if not CreateBankStatement(toPlayer.PlayerData.source, 'Personal', transferAmount, reason, 'deposit', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.transfer')
})
else
if Accounts[fromAccountName].account_type == 'job' and job.name ~= fromAccountName and not job.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
if Accounts[fromAccountName].account_type == 'gang' and gang.name ~= fromAccountName and not gang.isboss then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local fromAccountBalance = GetAccountBalance(fromAccountName)
if fromAccountBalance < transferAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
if not RemoveMoney(fromAccountName, transferAmount) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
toPlayer.Functions.AddMoney('bank', transferAmount, reason)
if not CreateBankStatement(toPlayer.PlayerData.source, 'Personal', transferAmount, reason, 'deposit', 'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
cb({
success = true,
message = Lang:t('success.transfer')
})
end
end)
QBCore.Functions.CreateCallback('DP-Banking:server:orderCard', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = "Jugador no encontrado"
})
end
local pinNumber = tonumber(data.pin)
if not pinNumber or string.len(tostring(pinNumber)) ~= 4 then
return cb({
success = false,
message = "PIN inválido. Debe tener 4 dígitos"
})
end
local cardNumber = math.random(1000000000000000, 9999999999999999)
local info = {
citizenid = citizenid,
name = Player.PlayerData.charinfo.firstname .. ' ' .. Player.PlayerData.charinfo.lastname,
cardNumber = cardNumber,
cardPin = pinNumber
}
exports['qb-inventory']:AddItem(src, 'bank_card', 1, false, info)
TriggerEvent('qb-log:server:CreateLog', 'banking', 'Tarjeta Ordenada', 'green',
string.format('**%s** ordenó una nueva tarjeta bancaria', GetPlayerName(src)))
cb({
success = true,
message = "Tarjeta de débito ordenada correctamente"
})
end)
QBCore.Functions.CreateCallback('DP-Banking:server:openAccount', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local accountName = data.accountName
local initialAmount = tonumber(data.amount)
if GetNumberOfAccounts(citizenid) >= Config.maxAccounts then
return cb({
success = false,
message = Lang:t('error.accounts')
})
end
if Player.PlayerData.money.bank < initialAmount then
return cb({
success = false,
message = Lang:t('error.money')
})
end
Player.Functions.RemoveMoney('bank', initialAmount, 'Opened account ' .. accountName)
if not CreatePlayerAccount(src, accountName, initialAmount, json.encode({})) then
return cb({
success = false,
message = Lang:t('error.error')
})
end
if not CreateBankStatement(src, accountName, initialAmount, 'Initial deposit', 'deposit', 'shared') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
if not CreateBankStatement(src, 'Personal', initialAmount, 'Initial deposit for ' .. accountName, 'withdraw',
'player') then
return cb({
success = false,
message = Lang:t('error.error')
})
end
TriggerEvent('qb-log:server:CreateLog', 'banking', 'Account Opened', 'green',
string.format('**%s** opened account **%s** with an initial deposit of **$%s**', GetPlayerName(src),
accountName, initialAmount))
cb({
success = true,
message = Lang:t('success.account')
})
end)
QBCore.Functions.CreateCallback('DP-Banking:server:renameAccount', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local oldName = data.oldName
local newName = data.newName
-- Verificar si el nuevo nombre ya existe
local existingAccount = MySQL.scalar.await('SELECT account_name FROM bank_accounts WHERE account_name = ?',
{newName})
if existingAccount then
return cb({
success = false,
message = 'Ya existe una cuenta con ese nombre'
})
end
if not Accounts[oldName] then
return cb({
success = false,
message = Lang:t('error.error')
})
end
if Accounts[oldName].citizenid ~= citizenid then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local result = MySQL.update.await(
'UPDATE bank_accounts SET account_name = ? WHERE account_name = ? AND citizenid = ?',
{newName, oldName, citizenid})
if not result then
return cb({
success = false,
message = Lang:t('error.error')
})
end
-- Actualizar localmente
Accounts[newName] = Accounts[oldName]
Accounts[newName].account_name = newName
Accounts[oldName] = nil
TriggerEvent('qb-log:server:CreateLog', 'banking', 'Account Renamed', 'red',
string.format('**%s** renamed **%s** to **%s**', GetPlayerName(src), oldName, newName))
cb({
success = true,
message = Lang:t('success.rename')
})
end)
QBCore.Functions.CreateCallback('DP-Banking:server:deleteAccount', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local accountName = data.accountName
if not Accounts[accountName] then
return cb({
success = false,
message = Lang:t('error.account')
})
end
if Accounts[accountName].citizenid ~= citizenid then
return cb({
success = false,
message = Lang:t('error.access')
})
end
-- Primero eliminamos los statements
local result = MySQL.rawExecute.await('DELETE FROM bank_statements WHERE account_name = ?', {accountName})
if not result then
return cb({
success = false,
message = 'Error al eliminar el historial de la cuenta'
})
end
-- Luego la cuenta
result = MySQL.rawExecute.await('DELETE FROM bank_accounts WHERE account_name = ? AND citizenid = ?',
{accountName, citizenid})
if not result then
return cb({
success = false,
message = Lang:t('error.error')
})
end
Accounts[accountName] = nil
TriggerEvent('qb-log:server:CreateLog', 'banking', 'Account Deleted', 'red',
string.format('**%s** deleted account **%s**', GetPlayerName(src), accountName))
cb({
success = true,
message = Lang:t('success.delete')
})
end)
QBCore.Functions.CreateCallback('DP-Banking:server:addUser', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local userToAdd = data.userName
local accountName = data.accountName
if not Accounts[accountName] then
return cb({
success = false,
message = Lang:t('error.account')
})
end
if Accounts[accountName].citizenid ~= citizenid then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local account = Accounts[accountName]
local users = json.decode(account.users)
for _, cid in ipairs(users) do
if cid == userToAdd then
return cb({
success = false,
message = Lang:t('error.user')
})
end
end
users[#users + 1] = userToAdd
local usersData = json.encode(users)
Accounts[accountName].users = usersData
local result = MySQL.update.await('UPDATE bank_accounts SET users = ? WHERE account_name = ? AND citizenid = ?',
{usersData, accountName, citizenid})
if not result then
cb({
success = false,
message = Lang:t('error.error')
})
end
TriggerEvent('qb-log:server:CreateLog', 'banking', 'User Added', 'green',
string.format('**%s** added **%s** to **%s**', GetPlayerName(src), userToAdd, accountName))
cb({
success = true,
message = Lang:t('success.userAdd')
})
end)
QBCore.Functions.CreateCallback('DP-Banking:server:removeUser', function(source, cb, data)
local src = source
local Player, citizenid = getPlayerAndCitizenId(src)
if not Player or not citizenid then
return cb({
success = false,
message = Lang:t('error.error')
})
end
local userToRemove = data.userName
local accountName = data.accountName
if not Accounts[accountName] then
return cb({
success = false,
message = Lang:t('error.account')
})
end
if Accounts[accountName].citizenid ~= citizenid then
return cb({
success = false,
message = Lang:t('error.access')
})
end
local account = Accounts[accountName]
local users = json.decode(account.users)
local userFound = false
for i = #users, 1, -1 do
if users[i] == userToRemove then
table.remove(users, i)
userFound = true
break
end
end
if not userFound then
return cb({
success = false,
message = Lang:t('error.noUser')
})
end
local usersData = json.encode(users)
Accounts[accountName].users = usersData
local result = MySQL.update.await('UPDATE bank_accounts SET users = ? WHERE account_name = ? AND citizenid = ?',
{usersData, accountName, citizenid})
if not result then
cb({
success = false,
message = Lang:t('error.error')
})
end
TriggerEvent('qb-log:server:CreateLog', 'banking', 'User Removed', 'red',
string.format('**%s** removed **%s** from **%s**', GetPlayerName(src), userToRemove, accountName))
cb({
success = true,
message = Lang:t('success.userRemove')
})
end)
-- Items
QBCore.Functions.CreateUseableItem('bank_card', function(source, item)
local Player = QBCore.Functions.GetPlayer(source)
if not Player then
return
end
if Player.Functions.GetItemByName(item.name) then
TriggerClientEvent('DP-Banking:client:useCard', source)