Advertisement
Nidhoggx

main.server billing

May 15th, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. ESX = nil
  2.  
  3. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  4.  
  5. RegisterServerEvent('esx_billing:sendBill')
  6. AddEventHandler('esx_billing:sendBill', function(playerId, sharedAccountName, label, amount)
  7.  
  8. local _source = source
  9. local xPlayer = ESX.GetPlayerFromId(_source)
  10. local xPlayers = ESX.GetPlayers()
  11.  
  12. TriggerEvent('esx_addonaccount:getSharedAccount', sharedAccountName, function(account)
  13.  
  14. if amount < 0 then
  15. print('esx_billing: ' .. GetPlayerName(_source) .. ' tried sending a negative bill!')
  16. TriggerClientEvent('esx:showNotification', _source, _U('negative_bill'))
  17. elseif account == nil then
  18.  
  19. for i=1, #xPlayers, 1 do
  20.  
  21. local xPlayer2 = ESX.GetPlayerFromId(xPlayers[i])
  22.  
  23. if xPlayer2.source == playerId then
  24.  
  25. MySQL.Async.execute(
  26. 'INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (@identifier, @sender, @target_type, @target, @label, @amount)',
  27. {
  28. ['@identifier'] = xPlayer2.identifier,
  29. ['@sender'] = xPlayer.identifier,
  30. ['@target_type'] = 'player',
  31. ['@target'] = xPlayer.identifier,
  32. ['@label'] = label,
  33. ['@amount'] = amount
  34. },
  35. function(rowsChanged)
  36. TriggerClientEvent('esx:showNotification', xPlayer2.source, _U('received_invoice'))
  37. end
  38. )
  39.  
  40. break
  41. end
  42. end
  43. else
  44. for i=1, #xPlayers, 1 do
  45.  
  46. local xPlayer2 = ESX.GetPlayerFromId(xPlayers[i])
  47.  
  48. if xPlayer2.source == playerId then
  49.  
  50. MySQL.Async.execute(
  51. 'INSERT INTO billing (identifier, sender, target_type, target, label, amount) VALUES (@identifier, @sender, @target_type, @target, @label, @amount)',
  52. {
  53. ['@identifier'] = xPlayer2.identifier,
  54. ['@sender'] = xPlayer.identifier,
  55. ['@target_type'] = 'society',
  56. ['@target'] = sharedAccountName,
  57. ['@label'] = label,
  58. ['@amount'] = amount
  59. },
  60. function(rowsChanged)
  61. TriggerClientEvent('esx:showNotification', xPlayer2.source, _U('received_invoice'))
  62. end
  63. )
  64.  
  65. break
  66. end
  67. end
  68. end
  69. end)
  70.  
  71. end)
  72.  
  73. ESX.RegisterServerCallback('esx_billing:getBills', function(source, cb)
  74.  
  75. local _source = source
  76. local xPlayer = ESX.GetPlayerFromId(_source)
  77.  
  78. MySQL.Async.fetchAll(
  79. 'SELECT * FROM billing WHERE identifier = @identifier',
  80. {
  81. ['@identifier'] = xPlayer.identifier
  82. },
  83. function(result)
  84.  
  85. local bills = {}
  86.  
  87. for i=1, #result, 1 do
  88. table.insert(bills, {
  89. id = result[i].id,
  90. identifier = result[i].identifier,
  91. sender = result[i].sender,
  92. targetType = result[i].target_type,
  93. target = result[i].target,
  94. label = result[i].label,
  95. amount = result[i].amount
  96. })
  97. end
  98.  
  99. cb(bills)
  100.  
  101. end
  102. )
  103.  
  104. end)
  105.  
  106.  
  107. ESX.RegisterServerCallback('esx_billing:payBill', function(source, cb, id)
  108.  
  109. local _source = source
  110. local xPlayer = ESX.GetPlayerFromId(_source)
  111.  
  112. MySQL.Async.fetchAll(
  113. 'SELECT * FROM billing WHERE id = @id',
  114. {
  115. ['@id'] = id
  116. },
  117. function(result)
  118.  
  119. local sender = result[1].sender
  120. local targetType = result[1].target_type
  121. local target = result[1].target
  122. local amount = result[1].amount
  123. local xPlayers = ESX.GetPlayers()
  124. local foundPlayer = nil
  125.  
  126. for i=1, #xPlayers, 1 do
  127.  
  128. local xPlayer2 = ESX.GetPlayerFromId(xPlayers[i])
  129.  
  130. if xPlayer2.identifier == sender then
  131. foundPlayer = xPlayer2
  132. break
  133. end
  134. end
  135.  
  136. if targetType == 'player' then
  137.  
  138. if foundPlayer ~= nil then
  139.  
  140. if xPlayer.get('money') >= amount then
  141.  
  142. MySQL.Async.execute(
  143. 'DELETE from billing WHERE id = @id',
  144. {
  145. ['@id'] = id
  146. },
  147. function(rowsChanged)
  148.  
  149. xPlayer.removeMoney(amount)
  150. foundPlayer.addMoney(amount)
  151.  
  152. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('paid_invoice') .. amount)
  153. TriggerClientEvent('esx:showNotification', foundPlayer.source, _U('received_payment') .. amount)
  154.  
  155. cb()
  156.  
  157. end
  158. )
  159.  
  160. else
  161. TriggerClientEvent('esx:showNotification', _source, _U('player_not_logged'))
  162. cb()
  163. end
  164.  
  165. end
  166.  
  167. else
  168. TriggerEvent('esx_addonaccount:getSharedAccount', target, function(account)
  169. if xPlayer.get('money') >= amount then
  170. MySQL.Async.execute(
  171. 'DELETE from billing WHERE id = @id',
  172. {
  173. ['@id'] = id
  174. },
  175. function(rowsChanged)
  176. xPlayer.removeMoney(amount)
  177. account.addMoney(amount)
  178. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('paid_invoice') .. amount)
  179. if foundPlayer ~= nil then
  180. TriggerClientEvent('esx:showNotification', foundPlayer.source, _U('received_payment') .. amount)
  181. end
  182. cb()
  183. end)
  184. else
  185. TriggerClientEvent('esx:showNotification', xPlayer.source, _U('no_money'))
  186. if foundPlayer ~= nil then
  187. TriggerClientEvent('esx:showNotification', foundPlayer.source, _U('target_no_money'))
  188. end
  189. end
  190. end)
  191.  
  192. end
  193.  
  194. end
  195. )
  196.  
  197. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement