--[[ d0305fc985f4811f ]] --[[ local code, err = business.Mail('angry@consultant.com', 'free@post.com', nil, nil, 'subject', 'Body', false) if code ~= 0 then MessageBox('Posielanie emailu sa nezdarilo (' .. code .. ': ' .. err .. ').', caption, 0x30) return false else return false end ]] local year = Settings.Year local caption = 'Hromadne do PDF' local proc = { ['najomca'] = { ['oznacene'] = 'nájomcov', ['tlacovka'] = 'Ročné vyúčtovanie za rok ' .. year .. ' (ak bolo vypočítané)', ['filename'] = 'Vyuctovanie ' .. year, ['report'] = 'Vyuctovanie', ['main_id'] = 'main.ID_najomcu', ['select'] = [[ SELECT DISTINCT ID_najomcu, n.Meno, p.Mail FROM vyucto_vystup INNER JOIN najomcaX AS n ON ID_najomcu = n.RowID LEFT JOIN partner AS p ON ID_partnera = p.RowID WHERE ID_najomcu %s AND Rok = %s ]], ['mail_subject'] = 'Ročné Vyúčtovanie ' .. year, }, ['obrat'] = { ['oznacene'] = 'obratov', ['tlacovka'] = 'Evidenčný list', ['filename'] = 'Evidencny ' .. year, ['report'] = 'EvidencnyList2', ['main_id'] = 'main.ID_obratu', ['select'] = [[ SELECT DISTINCT obrat.RowID, n.Meno, p.Mail FROM obrat INNER JOIN najomcaX AS n ON ID_najomcu = n.RowID LEFT JOIN partner AS p ON n.ID_partnera = p.RowID WHERE obrat.RowID %s ]], ['mail_subject'] = 'Evidenčný list na rok ' .. year, }, } if not db.Opened then return false end local e = FrmMain.ActiveEvid.TableName local g = FrmMain.ActiveEvid.Table if not proc[e] then MessageBox('Označte nájomcov (v evidencii nájomcov) alebo evidenčné listy (v evidencii obratov) a spustite tento skript znovu.', caption, 0x30) else local t = {} g:First() while not g.Eof do if g.Selected then table.insert(t, g:Field'RowID') end g:Next() end caption = proc[e].mail_subject .. ': ' .. caption if MessageBox(string.format('Označili ste %i %s. ' .. ' \r\nSkript teraz vygeneruje do súboru zostavu %s, samostatne pre každého nájomcu. Operácia môže trvať dlho.' .. '\r\nŽeláte si pokračovať?', #t, proc[e].oznacene, proc[e].tlacovka), caption, 0x30 + 0x4) ~= 6 then return false end local path = Settings:Read('DefaultPrintFileName', 'C:\\file.pdf'):gsub ('\\([^\\]-)(%.%a-)$', '\\' .. proc[e].filename .. '%2') print(path) local ext = path:match('%a-$'):upper() path = business.OpenDialog(path, 'Zvoľte umiestnenie súborov ' .. ext, ext .. ' súbory|*.' .. ext) if path then local peciatka = Settings:Read('Peciatka') -- check if file exists local f = io.open(peciatka, 'r') if f then io.close(f) else peciatka = nil end -- ak user vymazal priponu if path:sub(-#ext):upper() ~= ext then path = path .. '.' .. ext end -- pridaj %s, aby sme mohli vlozit meno najomcu path = path:gsub('%.%a-$', ' %%s%1') print(path) local printer_name = Settings:Read('DefaultPrinterName', 'PDF') local fnames = {} local fnames_count = 0 local cond = ' IN (' .. table.concat(t, ',') .. ')' local s = sql(string.format(proc[e].select, cond, year)) progress.init(s.RowCount) for najomca, meno in s:each() do -- odstran vsetky znaky (control, punctuation), ktore by mohli vadit menu suboru local file_name = string.format(path, meno:gsub('[%c%p]', '')) if not progress.step(meno) or not business.Print( proc[e].report, printer_name, file_name, function(R) local formula = prn.FormulaByName(R, '_Rok') if formula then formula.Text = year end formula = prn.FormulaByName(R, '_DatumOD') if formula then formula.Text = date.encode(year, 01) end formula = prn.FormulaByName(R, '_DatumDO') if formula then formula.Text = date.encode(year, 12) end local id = proc[e].main_id prn.SetDataSourceCondition(R, id:match('^%w+'), id .. ' = ' .. najomca) if peciatka and R.Pictures.Count > 0 then R.Pictures:Items(0).FileName = peciatka end end ) then return false else fnames[najomca] = file_name fnames_count = fnames_count + 1 end end progress.done() if MessageBox('Bolo vygenerovaných ' .. fnames_count .. ' súborov. ' .. ' \r\nSkript teraz pošle tieto súbory na emailové adresy nájomcov (ak sú zadané). ' .. '\r\nŽeláte si pokračovať?', caption, 0x30 + 0x4) ~= 6 then return false end local err = '' for najomca, meno, mail in s:each() do if fnames[najomca] then if not mail or #mail < 5 then err = err .. '\r\n' .. meno fnames[najomca] = false end end end if #err > 0 then MessageBox('Nasledujúci nájomcovia nemajú zadanú emailovú adresu:' .. err, caption, 0x30) end for najomca, meno, mail in s:each() do if fnames[najomca] then local code, err = business.Mail( nil, mail, nil, fnames[najomca], proc[e].mail_subject, 'Vážený/á ' .. meno .. '\r\nV prílohe Vám zasielame...', false ) if code ~= 0 then MessageBox('Posielanie emailu sa nezdarilo (' .. code .. ': ' .. err .. ').', caption, 0x30) return false end end end end return false end