Skip to content

Commit

Permalink
Merge pull request #398 from Z3rio/main
Browse files Browse the repository at this point in the history
feat: improvements of invoice system
  • Loading branch information
GhzGarage authored Nov 13, 2024
2 parents bd396e1 + 91db881 commit 007e5ee
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 99 deletions.
27 changes: 7 additions & 20 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ PhoneData = {
MentionedTweets = {},
Hashtags = {},
Chats = {},
Invoices = {},
CallData = {},
RecentCalls = {},
Garage = {},
Expand Down Expand Up @@ -227,13 +226,6 @@ local function LoadPhone()
PhoneData.Chats = Chats
end

if pData.Invoices ~= nil and next(pData.Invoices) ~= nil then
for _, invoice in pairs(pData.Invoices) do
invoice.name = IsNumberInContacts(invoice.number)
end
PhoneData.Invoices = pData.Invoices
end

if pData.Hashtags ~= nil and next(pData.Hashtags) ~= nil then
PhoneData.Hashtags = pData.Hashtags
end
Expand Down Expand Up @@ -598,11 +590,9 @@ RegisterNUICallback('GetBankContacts', function(_, cb)
end)

RegisterNUICallback('GetInvoices', function(_, cb)
if PhoneData.Invoices ~= nil and next(PhoneData.Invoices) ~= nil then
cb(PhoneData.Invoices)
else
cb(nil)
end
QBCore.Functions.TriggerCallback('qb-phone:server:GetInvoices', function(resp)
cb(resp)
end)
end)

RegisterNUICallback('SharedLocation', function(data, cb)
Expand Down Expand Up @@ -665,9 +655,8 @@ RegisterNUICallback('PayInvoice', function(data, cb)
local society = data.society
local amount = data.amount
local invoiceId = data.invoiceId
QBCore.Functions.TriggerCallback('qb-phone:server:PayInvoice', function(CanPay, Invoices)
if CanPay then PhoneData.Invoices = Invoices end
cb(CanPay)
QBCore.Functions.TriggerCallback('qb-phone:server:PayInvoice', function(resp)
cb(resp)
end, society, amount, invoiceId, senderCitizenId)
TriggerServerEvent('qb-phone:server:BillingEmail', data, true)
end)
Expand All @@ -676,9 +665,8 @@ RegisterNUICallback('DeclineInvoice', function(data, cb)
local society = data.society
local amount = data.amount
local invoiceId = data.invoiceId
QBCore.Functions.TriggerCallback('qb-phone:server:DeclineInvoice', function(_, Invoices)
PhoneData.Invoices = Invoices
cb('ok')
QBCore.Functions.TriggerCallback('qb-phone:server:DeclineInvoice', function(resp)
cb(resp)
end, society, amount, invoiceId)
TriggerServerEvent('qb-phone:server:BillingEmail', data, false)
end)
Expand Down Expand Up @@ -1456,7 +1444,6 @@ RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
MentionedTweets = {},
Hashtags = {},
Chats = {},
Invoices = {},
CallData = {},
RecentCalls = {},
Garage = {},
Expand Down
37 changes: 22 additions & 15 deletions html/css/bank.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,44 @@
background-color: #dc143c;
}
.bank-app-invoice {
position: relative;
height: 6vh;
height: fit-content;
display: flex;
flex-direction: column;
padding: 8px 16px;
width: 100%;
letter-spacing: .05vh;
border-bottom: .2vh solid #363d4b;
}
.bank-app-invoice-title {
text-transform : capitalize;
position: absolute;
top: 1vh;
left: 2vh;
width: 100%;
color: white;
font-family: 'Poppins', sans-serif;
font-size: 1.3vh;
}
.bank-app-invoice-info {
width: 100%;
display: flex;
flex-direction: row;
}
.bank-app-invoice-amount {
position: absolute;
bottom: 1vh;
left: 2vh;
color: rgba(255, 255, 255, 0.781);
font-family: 'Poppins', sans-serif;
font-size: 1.22vh;
}
.bank-app-invoice-buttons {
position: absolute;
right: 1.2vh;
bottom: .1vh;
height: 3vh;
width: 7vh;
text-align: center;
line-height: 3.5vh;
margin-left: auto;
}
.bank-app-invoice-reason {
color: rgba(255, 255, 255, 0.85);
font-family: 'Poppins', sans-serif;
font-size: 1.22vh;

width: 100%;

overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.bank-app-invoice-buttons > i {
margin-left: .5vh;
Expand Down
31 changes: 18 additions & 13 deletions html/js/bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ GetInvoiceLabel = function(type) {
$(document).on('click', '.pay-invoice', function(event){
event.preventDefault();

var InvoiceId = $(this).parent().parent().attr('id');
var InvoiceId = $(this).parent().parent().parent().attr('id');
var InvoiceData = $("#"+InvoiceId).data('invoicedata');
var BankBalance = $(".bank-app-account-balance").data('balance');

Expand Down Expand Up @@ -168,35 +168,40 @@ $(document).on('click', '.pay-invoice', function(event){
}
});

$(document).on('click', '.decline-invoice', function(event){
$(document).on('click', '.decline-invoice', async function(event) {
event.preventDefault();
var InvoiceId = $(this).parent().parent().attr('id');
var InvoiceId = $(this).parent().parent().parent().attr('id');
var InvoiceData = $("#"+InvoiceId).data('invoicedata');

$.post('https://qb-phone/DeclineInvoice', JSON.stringify({
const resp = await $.post('https://qb-phone/DeclineInvoice', JSON.stringify({
sender: InvoiceData.sender,
amount: InvoiceData.amount,
society: InvoiceData.society,
invoiceId: InvoiceData.id,
}));
$("#"+InvoiceId).animate({
left: 30+"vh",
}, 300, function(){
setTimeout(function(){
$("#"+InvoiceId).remove();
}, 100);
});
if(resp === true) {
QB.Phone.Notifications.Add("fas fa-university", "QBank", "You declined the invoice", "#8c7ae6")
$("#"+InvoiceId).animate({
left: 30+"vh",
}, 300, function(){
setTimeout(function(){
$("#"+InvoiceId).remove();
}, 100);
});
} else {
QB.Phone.Notifications.Add("fas fa-university", "QBank", "Couldnt decline this invoice...", "#8c7ae6")
}
});

QB.Phone.Functions.LoadBankInvoices = function(invoices) {
if (invoices !== null) {
$(".bank-app-invoices-list").html("");

$.each(invoices, function(i, invoice){
var Elem = '<div class="bank-app-invoice" id="invoiceid-'+i+'"> <div class="bank-app-invoice-title">'+invoice.society+' <span style="font-size: 1vh; color: gray;">(Sender: '+invoice.sender+')</span></div> <div class="bank-app-invoice-amount">&#36; '+invoice.amount+'</div> <div class="bank-app-invoice-buttons"> <i class="fas fa-check-circle pay-invoice"></i> <i class="fas fa-times-circle decline-invoice"></i> </div> </div>';
var Elem = '<div class="bank-app-invoice" id="invoiceid-'+invoice.id+'"> <div class="bank-app-invoice-title">'+invoice.society+' <span style="font-size: 1vh; color: gray;">(Sender: '+invoice.sender+')</span></div>' + (typeof invoice.reason === 'string' ? `<div class="bank-app-invoice-reason">${invoice.reason}</div>` : '') + '<div class="bank-app-invoice-info"><div class="bank-app-invoice-amount">&#36; '+invoice.amount+'</div> <div class="bank-app-invoice-buttons"> <i class="fas fa-check-circle pay-invoice"></i>'+ (invoice.candecline === 1 ? '<i class="fas fa-times-circle decline-invoice"></i>' : '') + '</div></div></div>';

$(".bank-app-invoices-list").append(Elem);
$("#invoiceid-"+i).data('invoicedata', invoice);
$("#invoiceid-"+invoice.id).data('invoicedata', invoice);
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions qb-phone.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ CREATE TABLE IF NOT EXISTS `phone_invoices` (
`society` tinytext DEFAULT NULL,
`sender` varchar(50) DEFAULT NULL,
`sendercitizenid` varchar(50) DEFAULT NULL,
`candecline` int(1) not null default 1,
`reason` varchar(256) default null,
PRIMARY KEY (`id`),
KEY `citizenid` (`citizenid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;
Expand Down
120 changes: 69 additions & 51 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,32 @@ end
exports('sendNewMailToOffline', sendNewMailToOffline)
-- Callbacks

QBCore.Functions.CreateCallback("qb-phone:server:GetInvoices", function(source, cb)
local Player = QBCore.Functions.GetPlayer(source)

if Player then
local invoices = MySQL.query.await('SELECT * FROM phone_invoices WHERE citizenid = ?', { Player.PlayerData.citizenid })
for _, v in pairs(invoices) do
local Ply = QBCore.Functions.GetPlayerByCitizenId(v.sender)
if Ply ~= nil then
v.number = Ply.PlayerData.charinfo.phone
else
local res = MySQL.query.await('SELECT * FROM players WHERE citizenid = ?', { v.sender })
if res[1] ~= nil then
res[1].charinfo = json.decode(res[1].charinfo)
v.number = res[1].charinfo.phone
else
v.number = nil
end
end
end
cb(invoices)
return
end

cb({})
end)

QBCore.Functions.CreateCallback('qb-phone:server:GetCallState', function(_, cb, ContactData)
local Target = QBCore.Functions.GetPlayerByPhone(ContactData.number)
if Target ~= nil then
Expand Down Expand Up @@ -187,7 +213,6 @@ QBCore.Functions.CreateCallback('qb-phone:server:GetPhoneData', function(source,
MentionedTweets = {},
Chats = {},
Hashtags = {},
Invoices = {},
Garage = {},
Mails = {},
Adverts = {},
Expand All @@ -207,25 +232,6 @@ QBCore.Functions.CreateCallback('qb-phone:server:GetPhoneData', function(source,
PhoneData.PlayerContacts = result
end

local invoices = MySQL.query.await('SELECT * FROM phone_invoices WHERE citizenid = ?', { Player.PlayerData.citizenid })
if invoices[1] ~= nil then
for _, v in pairs(invoices) do
local Ply = QBCore.Functions.GetPlayerByCitizenId(v.sender)
if Ply ~= nil then
v.number = Ply.PlayerData.charinfo.phone
else
local res = MySQL.query.await('SELECT * FROM players WHERE citizenid = ?', { v.sender })
if res[1] ~= nil then
res[1].charinfo = json.decode(res[1].charinfo)
v.number = res[1].charinfo.phone
else
v.number = nil
end
end
end
PhoneData.Invoices = invoices
end

local garageresult = MySQL.query.await('SELECT * FROM player_vehicles WHERE citizenid = ?', { Player.PlayerData.citizenid })
if garageresult[1] ~= nil then
PhoneData.Garage = garageresult
Expand Down Expand Up @@ -283,45 +289,57 @@ QBCore.Functions.CreateCallback('qb-phone:server:GetPhoneData', function(source,
end)

QBCore.Functions.CreateCallback('qb-phone:server:PayInvoice', function(source, cb, society, amount, invoiceId, sendercitizenid)
local Invoices = {}
local Ply = QBCore.Functions.GetPlayer(source)
local SenderPly = QBCore.Functions.GetPlayerByCitizenId(sendercitizenid)
local invoiceMailData = {}
if SenderPly and Config.BillingCommissions[society] then
local commission = round(amount * Config.BillingCommissions[society])
SenderPly.Functions.AddMoney('bank', commission)
invoiceMailData = {
sender = 'Billing Department',
subject = 'Commission Received',
message = string.format('You received a commission check of $%s when %s %s paid a bill of $%s.', commission, Ply.PlayerData.charinfo.firstname, Ply.PlayerData.charinfo.lastname, amount)
}
elseif not SenderPly and Config.BillingCommissions[society] then
invoiceMailData = {
sender = 'Billing Department',
subject = 'Bill Paid',
message = string.format('%s %s paid a bill of $%s', Ply.PlayerData.charinfo.firstname, Ply.PlayerData.charinfo.lastname, amount)
}
end
Ply.Functions.RemoveMoney('bank', amount, 'paid-invoice')
exports['qb-phone']:sendNewMailToOffline(sendercitizenid, invoiceMailData)
exports['qb-banking']:AddMoney(society, amount, 'Phone invoice')
MySQL.query('DELETE FROM phone_invoices WHERE id = ?', { invoiceId })
local invoices = MySQL.query.await('SELECT * FROM phone_invoices WHERE citizenid = ?', { Ply.PlayerData.citizenid })
if invoices[1] ~= nil then
Invoices = invoices
local invoiceMailData = nil
if Ply then
local exists = MySQL.query.await('select count(1) as count FROM phone_invoices WHERE id = ? and citizenid = ?', { invoiceId, Ply.PlayerData.citizenid })

if exists[1] and exists[1]["count"] == 1 then
if SenderPly and Config.BillingCommissions[society] then
local commission = round(amount * Config.BillingCommissions[society])
SenderPly.Functions.AddMoney('bank', commission)
invoiceMailData = {
sender = 'Billing Department',
subject = 'Commission Received',
message = string.format('You received a commission check of $%s when %s %s paid a bill of $%s.', commission, Ply.PlayerData.charinfo.firstname, Ply.PlayerData.charinfo.lastname, amount)
}
elseif not SenderPly and Config.BillingCommissions[society] then
invoiceMailData = {
sender = 'Billing Department',
subject = 'Bill Paid',
message = string.format('%s %s paid a bill of $%s', Ply.PlayerData.charinfo.firstname, Ply.PlayerData.charinfo.lastname, amount)
}
end
if Ply.Functions.RemoveMoney('bank', amount, 'paid-invoice') then
MySQL.query('DELETE FROM phone_invoices WHERE id = ? and citizenid = ?', { invoiceId, Ply.PlayerData.citizenid })
if invoiceMailData then
exports['qb-phone']:sendNewMailToOffline(sendercitizenid, invoiceMailData)
end
TriggerEvent("qb-phone:server:paidInvoice", source, invoiceId)
exports['qb-banking']:AddMoney(society, amount, 'Phone invoice')
cb(true)
return
end
end
end
cb(true, Invoices)
cb(false)
end)

QBCore.Functions.CreateCallback('qb-phone:server:DeclineInvoice', function(source, cb, _, _, invoiceId)
local Invoices = {}
local Ply = QBCore.Functions.GetPlayer(source)
MySQL.query('DELETE FROM phone_invoices WHERE id = ?', { invoiceId })
local invoices = MySQL.query.await('SELECT * FROM phone_invoices WHERE citizenid = ?', { Ply.PlayerData.citizenid })
if invoices[1] ~= nil then
Invoices = invoices
if Ply then
local exists = MySQL.query.await('select count(1) as count FROM phone_invoices WHERE id = ? and citizenid = ? and candecline = ?', { invoiceId, Ply.PlayerData.citizenid, 1 })

if exists[1] and exists[1]["count"] == 1 then
TriggerEvent("qb-phone:server:declinedInvoice", source, invoiceId)
MySQL.query('DELETE FROM phone_invoices WHERE id = ? and citizenid = ? and candecline = ?', { invoiceId, Ply.PlayerData.citizenid, 1 })
cb(true)
return
end
end
cb(true, Invoices)

cb(false)
end)

QBCore.Functions.CreateCallback('qb-phone:server:GetContactPictures', function(_, cb, Chats)
Expand Down

0 comments on commit 007e5ee

Please sign in to comment.