garbagecollect
This commit is contained in:
commit
dae8491c9f
1434
bitrix/crm.lua
Normal file
1434
bitrix/crm.lua
Normal file
File diff suppressed because it is too large
Load Diff
5
config/bitrix.lua
Executable file
5
config/bitrix.lua
Executable file
@ -0,0 +1,5 @@
|
||||
local config = require('config.config')
|
||||
local _M = config
|
||||
_M.file = 'bitrix.conf' -- файл конфигурации
|
||||
_M.read()
|
||||
return _M.data
|
||||
52
config/config.lua
Executable file
52
config/config.lua
Executable file
@ -0,0 +1,52 @@
|
||||
local json = require('cjson.safe')
|
||||
local _M = {}
|
||||
_M.data = {}
|
||||
_M.comments = {}
|
||||
_M.file = '' -- файл конфигурации
|
||||
local key
|
||||
|
||||
function _M.read()
|
||||
for line in io.lines(_M.file) do
|
||||
key = string.match(line, '([%w_]+)::')
|
||||
if (key) then
|
||||
_M.data[key] = string.match(line, '::(.*) #')
|
||||
_M.comments[key] = string.match(line, '#(.*)')
|
||||
if string.find(_M.data[key],'%{%"') or string.find(_M.data[key],'%[%"') then
|
||||
_M.data[key] = json.decode(_M.data[key])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function reprint(k,v)
|
||||
if type(v) == 'table' then
|
||||
for i,j in pairs(v) do
|
||||
reprint(i,j)
|
||||
end
|
||||
else
|
||||
print(k..': '..v)
|
||||
end
|
||||
end
|
||||
|
||||
function _M.data:write()
|
||||
local config_file = io.open(_M.file, 'w')
|
||||
for k,v in pairs(self) do
|
||||
if type(v) ~= 'function' then
|
||||
if type(v) == 'table' then v = json.encode(v) end
|
||||
config_file:write(k..'::'..v..' #'.._M.comments[key]..'\n')
|
||||
end
|
||||
end
|
||||
config_file:close()
|
||||
end
|
||||
|
||||
function _M.data:print()
|
||||
for k,v in pairs(self) do
|
||||
if type(v) ~= 'function' then
|
||||
if _M.comments[k] then print('\n'.._M.comments[k]:gsub("^%s*(.-)%s*$", "%1")..': \n') end
|
||||
reprint(k,v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return _M
|
||||
|
||||
46
test.lua
Normal file
46
test.lua
Normal file
@ -0,0 +1,46 @@
|
||||
local json = require('cjson')
|
||||
local crm = require('bitrix.crm')
|
||||
local log = require('utils.log')
|
||||
log.outfile = 'logs/tests_'..os.date('%Y-%m-%d')..'.log'
|
||||
log.level = 'trace'
|
||||
|
||||
local deal_id = 158811
|
||||
local test_date = '2025-01-05'
|
||||
|
||||
local function get_statuses()
|
||||
log.info('Поля статусов CRM: '..json.encode(crm.status.fields()))
|
||||
log.info('Статусы сделок : '..json.encode(crm.status.list(nil,{['ENTITY_ID']='DEAL_TYPE'})))
|
||||
end
|
||||
|
||||
local function test_deal_get(id)
|
||||
local result = crm.deal.get(id)
|
||||
if result.BEGINDATE then log.info('Тест получения сделок: успешно')
|
||||
else log.error('Тест получения сделок: провален '..json.encode(result)) end
|
||||
result = crm.deal.productrows.get(id)
|
||||
if result[1].PRODUCT_ID then log.info('Тест получения продукта сделки: успешно')
|
||||
else log.error('Тест получения продукта сделки: провален '..json.encode(result)) end
|
||||
end
|
||||
|
||||
local function test_deal_add_update_delete()
|
||||
local result = crm.deal.add(id)
|
||||
if result.BEGINDATE then log.info('Тест получения сделок: успешно')
|
||||
else log.error('Тест получения сделок: провален '..json.encode(result)) end
|
||||
result = crm.deal.productrows.get(id)
|
||||
if result[1].PRODUCT_ID then log.info('Тест получения продукта сделки: успешно')
|
||||
else log.error('Тест получения продукта сделки: провален '..json.encode(result)) end
|
||||
end
|
||||
|
||||
local function test_deal_list()
|
||||
local result = crm.deal.list({'*'},{['STAGE_ID']='NEW',['>DATE_CREATE']=test_date})
|
||||
if #result ~= tonumber(crm.res.total) then
|
||||
log.error('Ошибка обработки листинга сделок '..json.encode(crm.res))
|
||||
else
|
||||
log.info('Тест листинга сделок: успешно')
|
||||
end
|
||||
end
|
||||
|
||||
get_statuses()
|
||||
test_deal_get(deal_id)
|
||||
test_deal_list()
|
||||
|
||||
|
||||
43
utils/array.lua
Executable file
43
utils/array.lua
Executable file
@ -0,0 +1,43 @@
|
||||
local _M = {}
|
||||
|
||||
function _M.keys(t)
|
||||
local keys = {}
|
||||
for key,_ in pairs(t) do
|
||||
table.insert(keys, key)
|
||||
end
|
||||
return keys
|
||||
end
|
||||
|
||||
function _M.merge(t1,t2)
|
||||
for k,v in pairs(t2) do
|
||||
table.insert(t1,v)
|
||||
end
|
||||
return t1
|
||||
end
|
||||
|
||||
function _M.count(t)
|
||||
local count = 0
|
||||
for key,_ in pairs(t) do
|
||||
count = count + 1
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
function _M.minkey(t)
|
||||
local a = {}
|
||||
for n in pairs(t) do table.insert(a, n) end
|
||||
table.sort(a)
|
||||
return a[1]
|
||||
end
|
||||
|
||||
function _M.implode(t,before,after,sep)
|
||||
local result = ''
|
||||
local count = _M.count(t)
|
||||
for key,val in pairs(t) do
|
||||
result = result..before..val..after
|
||||
if key < count then result = result..sep end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
return _M
|
||||
90
utils/log.lua
Normal file
90
utils/log.lua
Normal file
@ -0,0 +1,90 @@
|
||||
--
|
||||
-- log.lua
|
||||
--
|
||||
-- Copyright (c) 2016 rxi
|
||||
--
|
||||
-- This library is free software; you can redistribute it and/or modify it
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local log = { _version = "0.1.0" }
|
||||
|
||||
log.usecolor = true
|
||||
log.outfile = nil
|
||||
log.level = "trace"
|
||||
|
||||
|
||||
local modes = {
|
||||
{ name = "trace", color = "\27[34m", },
|
||||
{ name = "debug", color = "\27[36m", },
|
||||
{ name = "info", color = "\27[32m", },
|
||||
{ name = "warn", color = "\27[33m", },
|
||||
{ name = "error", color = "\27[31m", },
|
||||
{ name = "fatal", color = "\27[35m", },
|
||||
}
|
||||
|
||||
|
||||
local levels = {}
|
||||
for i, v in ipairs(modes) do
|
||||
levels[v.name] = i
|
||||
end
|
||||
|
||||
|
||||
local round = function(x, increment)
|
||||
increment = increment or 1
|
||||
x = x / increment
|
||||
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
|
||||
end
|
||||
|
||||
|
||||
local _tostring = tostring
|
||||
|
||||
local tostring = function(...)
|
||||
local t = {}
|
||||
for i = 1, select('#', ...) do
|
||||
local x = select(i, ...)
|
||||
if type(x) == "number" then
|
||||
x = round(x, .01)
|
||||
end
|
||||
t[#t + 1] = _tostring(x)
|
||||
end
|
||||
return table.concat(t, " ")
|
||||
end
|
||||
|
||||
|
||||
for i, x in ipairs(modes) do
|
||||
local nameupper = x.name:upper()
|
||||
log[x.name] = function(...)
|
||||
|
||||
-- Return early if we're below the log level
|
||||
if i < levels[log.level] then
|
||||
return
|
||||
end
|
||||
|
||||
local msg = tostring(...)
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
local lineinfo = info.short_src .. ":" .. info.currentline
|
||||
|
||||
-- Output to console
|
||||
print(string.format("%s[%-6s%s]%s %s: %s",
|
||||
log.usecolor and x.color or "",
|
||||
nameupper,
|
||||
os.date("%H:%M:%S"),
|
||||
log.usecolor and "\27[0m" or "",
|
||||
lineinfo,
|
||||
msg))
|
||||
|
||||
-- Output to log file
|
||||
if log.outfile then
|
||||
local fp = io.open(log.outfile, "a")
|
||||
local str = string.format("[%-6s%s] %s: %s\n",
|
||||
nameupper, os.date(), lineinfo, msg)
|
||||
fp:write(str)
|
||||
fp:close()
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
return log
|
||||
30
utils/url.lua
Normal file
30
utils/url.lua
Normal file
@ -0,0 +1,30 @@
|
||||
local _M = {}
|
||||
|
||||
local char_to_hex = function(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end
|
||||
|
||||
local hex_to_char = function(x)
|
||||
return string.char(tonumber(x, 16))
|
||||
end
|
||||
|
||||
function _M.encode(url)
|
||||
if url == nil then
|
||||
return
|
||||
end
|
||||
url = url:gsub("\n", "\r\n")
|
||||
url = url:gsub("([^%w ])", char_to_hex)
|
||||
url = url:gsub(" ", "+")
|
||||
return url
|
||||
end
|
||||
|
||||
_M.decode = function(url)
|
||||
if url == nil then
|
||||
return
|
||||
end
|
||||
url = url:gsub("+", " ")
|
||||
url = url:gsub("%%(%x%x)", hex_to_char)
|
||||
return url
|
||||
end
|
||||
|
||||
return _M
|
||||
Loading…
x
Reference in New Issue
Block a user