api.printreceipt.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. <?php
  2. /**
  3. * Receipts/Bills printing
  4. */
  5. class PrintReceipt {
  6. /**
  7. * Contains array user statuses. Possible values: "debt" / "debtasbalance" / "undebt" / "all"
  8. *
  9. * @var array
  10. */
  11. protected $receiptAllUserStatuses = array();
  12. /**
  13. * Contains array user freeze statuses. Possible values: "frozen" / "unfrozen" / "all"
  14. *
  15. * @var array
  16. */
  17. protected $receiptAllFrozenStatuses = array();
  18. /**
  19. * Contains array of all cities represented as cityname => cityname
  20. *
  21. * @var array
  22. */
  23. protected $receiptAllCities = array('-' => '-');
  24. /**
  25. * Contains array of all streets represented as cityname + streetname => streetname
  26. *
  27. * @var array
  28. */
  29. protected $receiptAllStreets = array();
  30. /**
  31. * Contains array of all builds represented as cityname + streetname + buildnum => buildnum
  32. *
  33. * @var array
  34. */
  35. protected $receiptAllBuilds = array();
  36. /**
  37. * Placeholder for PRINT_RECEIPTS_HISTORY_ENABLED alter.ini option
  38. *
  39. * @var bool
  40. */
  41. public $receiptsHistoryOn = false;
  42. /**
  43. * Placeholder for ADDRESS_EXTENDED_ENABLED alter.ini option
  44. *
  45. * @var bool
  46. */
  47. public $extenAddressOn = false;
  48. /**
  49. * Placeholder for UbillingConfig object
  50. *
  51. * @var null
  52. */
  53. protected $ubConfig = null;
  54. /**
  55. * Contains receipt folders to use different templates
  56. *
  57. * @var array
  58. */
  59. protected $receiptTemplateFolders = array();
  60. const TEMPLATE_PATH = 'content/documents/receipt_template/';
  61. const URL_ME = '?module=printreceipts';
  62. public function __construct() {
  63. global $ubillingConfig;
  64. $this->ubConfig = $ubillingConfig;
  65. $this->receiptsHistoryOn = $this->ubConfig->getAlterParam('PRINT_RECEIPTS_HISTORY_ENABLED');
  66. $this->extenAddressOn = $this->ubConfig->getAlterParam('ADDRESS_EXTENDED_ENABLED');
  67. $this->receiptAllUserStatuses = array(
  68. 'debt' => __('Debtors'),
  69. 'debtasbalance' => __('Debtors (as of current balance)'),
  70. 'undebt' => __('AntiDebtors'),
  71. 'all' => __('All')
  72. );
  73. $this->receiptAllFrozenStatuses = array(
  74. 'all' => __('All'),
  75. 'unfrozen' => __('Not frozen'),
  76. 'frozen' => __('Frozen')
  77. );
  78. $this->reloadCitiesStreetsBuilds();
  79. $tmpFolders = rcms_scandir(self::TEMPLATE_PATH, '', 'dir');
  80. if (!empty($tmpFolders)) {
  81. foreach ($tmpFolders as $tmpFolder) {
  82. $this->receiptTemplateFolders[$tmpFolder] = $tmpFolder;
  83. }
  84. }
  85. $this->receiptTemplateFolders = array('-' => __('Default')) + $this->receiptTemplateFolders;
  86. }
  87. /**
  88. * Fills $receiptAllStreets placeholder
  89. *
  90. * @return void
  91. */
  92. protected function getAllCities() {
  93. $query = "SELECT DISTINCT `cityname` FROM `city` ORDER BY `cityname` ASC;";
  94. $allStreets = simple_queryall($query);
  95. if (!empty($allStreets)) {
  96. foreach ($allStreets as $io => $each) {
  97. $this->receiptAllCities[trim($each['cityname'])] = trim($each['cityname']);
  98. }
  99. }
  100. }
  101. /**
  102. * Fills $receiptAllStreets placeholder
  103. *
  104. * @return void
  105. */
  106. protected function getAllStreets() {
  107. $query = "SELECT `city`.`cityname`, `street`.`streetname`
  108. FROM `city`
  109. RIGHT JOIN `street` ON `street`.`cityid` = `city`.`id`
  110. ORDER BY `streetname`;";
  111. $allStreets = simple_queryall($query);
  112. if (!empty($allStreets)) {
  113. foreach ($allStreets as $io => $each) {
  114. $this->receiptAllStreets[trim($each['cityname']) . trim($each['streetname'])] = trim($each['streetname']);
  115. }
  116. }
  117. }
  118. /**
  119. * Fills $receiptAllBuilds placeholder
  120. *
  121. * @return void
  122. */
  123. protected function getAllBuilds() {
  124. $query = "SELECT `city`.`cityname`, `street`.`streetname`, `build`.`buildnum`
  125. FROM `city`
  126. RIGHT JOIN `street` ON `street`.`cityid` = `city`.`id`
  127. RIGHT JOIN `build` ON `build`.`streetid` = `street`.`id`
  128. ORDER BY `buildnum`;";
  129. $allBuilds = simple_queryall($query);
  130. if (!empty($allBuilds)) {
  131. foreach ($allBuilds as $io => $each) {
  132. $this->receiptAllBuilds[trim($each['cityname']) . trim($each['streetname']) . trim($each['buildnum'])] = trim($each['buildnum']);
  133. }
  134. }
  135. }
  136. /**
  137. * Saves receipt to DB
  138. *
  139. * @param $login
  140. * @param $rcptNum
  141. * @param $rcptDate
  142. * @param $rcptSum
  143. * @param $rcptBody
  144. */
  145. protected function saveToDB($login, $rcptNum, $rcptDate, $rcptSum, $rcptBody) {
  146. $tabInvoices = new nya_invoices();
  147. $tabInvoices->dataArr(
  148. array(
  149. 'login' => $login,
  150. 'invoice_num' => $rcptNum,
  151. 'invoice_date' => $rcptDate,
  152. 'invoice_sum' => $rcptSum,
  153. 'invoice_body' => $rcptBody,
  154. )
  155. );
  156. $tabInvoices->create();
  157. }
  158. /**
  159. * Returns all tags list as:
  160. * Inet service: tariffname => tariffname . tarifffee . tariffperiod
  161. * UKV service: tariffid => tariffname . tarifffee
  162. *
  163. * @return array
  164. */
  165. public function getAllTariffs($isInetSrv = true) {
  166. $tmpArr = array('-' => __('-'));
  167. if ($isInetSrv) {
  168. $query = "SELECT name, fee, period FROM `tariffs`";
  169. $alltypes = simple_queryall($query);
  170. if (!empty($alltypes)) {
  171. foreach ($alltypes as $io => $eachtype) {
  172. $tmpArr[$eachtype['name']] = $eachtype['name'] . ' - ' . $eachtype['fee'] . ' - ' . $eachtype['period'];
  173. }
  174. }
  175. } else {
  176. $query = "SELECT id, tariffname, price FROM `ukv_tariffs`";
  177. $alltypes = simple_queryall($query);
  178. if (!empty($alltypes)) {
  179. foreach ($alltypes as $io => $eachtype) {
  180. $tmpArr[$eachtype['id']] = $eachtype['tariffname'] . ' - ' . $eachtype['price'];
  181. }
  182. }
  183. }
  184. return ($tmpArr);
  185. }
  186. /**
  187. * Returns all tags list as tagid => tagname
  188. *
  189. * @return array
  190. */
  191. public function getAllTags() {
  192. $tmpArr = array('-' => __('-'));
  193. $query = "SELECT * from `tagtypes`";
  194. $alltypes = simple_queryall($query);
  195. if (!empty($alltypes)) {
  196. foreach ($alltypes as $io => $eachtype) {
  197. $tmpArr[$eachtype['id']] = $eachtype['tagname'];
  198. }
  199. }
  200. return ($tmpArr);
  201. }
  202. /**
  203. * Returns receipts data
  204. *
  205. * @param string $whereString
  206. *
  207. * @return mixed
  208. */
  209. public function getInvoicesData($whereString = '') {
  210. $tabInvoices = new nya_invoices();
  211. $tabInvoices->setDebug(true, true);
  212. $tabInvoices->whereRaw($whereString);
  213. $tabInvoices->selectable('*');
  214. $allInvoices = $tabInvoices->getAll();
  215. return ($allInvoices);
  216. }
  217. /**
  218. * Fires updating of $receiptAllStreets and $receiptAllBuilds placeholder
  219. *
  220. * @return void
  221. */
  222. public function reloadCitiesStreetsBuilds() {
  223. $this->getAllCities();
  224. $this->getAllStreets();
  225. $this->getAllBuilds();
  226. }
  227. /**
  228. * Returns users print data considering filters values
  229. *
  230. * @param string $receiptServiceType
  231. * @param string $receiptUserStatus
  232. * @param string $receiptUserLogin
  233. * @param string $receiptDebtCash
  234. * @param string $receiptStreet
  235. * @param string $receiptBuild
  236. * @param string $receiptCity
  237. * @param string $receiptTagID
  238. * @param string $receiptTariff
  239. * @param string $receiptFrozenStatus
  240. *
  241. * @return array
  242. */
  243. public function getUsersPrintData($receiptServiceType, $receiptUserStatus, $receiptUserLogin = '', $receiptDebtCash = '', $receiptCity = '', $receiptStreet = '', $receiptBuild = '', $receiptTagID = '', $receiptTariff = '', $receiptFrozenStatus = '') {
  244. $whereClause = '';
  245. $debtAsBalance = 0;
  246. switch ($receiptUserStatus) {
  247. case 'debt':
  248. $receiptDebtCash = (empty($receiptDebtCash)) ? 0 : ('-' . vf($receiptDebtCash, 3));
  249. $whereClause = ' WHERE `cash` < ' . $receiptDebtCash . ' ';
  250. break;
  251. case 'undebt':
  252. $receiptDebtCash = (empty($receiptDebtCash)) ? 0 : (vf($receiptDebtCash, 3));
  253. $whereClause = ' WHERE `cash` > ' . $receiptDebtCash . ' ';
  254. break;
  255. case 'debtasbalance':
  256. $whereClause = ' WHERE `cash` < \'0\' ';
  257. $debtAsBalance = 1;
  258. break;
  259. }
  260. if ($receiptServiceType == 'inetsrv' and !empty($receiptFrozenStatus) and $receiptFrozenStatus != 'all') {
  261. if (empty($whereClause)) {
  262. $whereClause = ' WHERE ';
  263. } else {
  264. $whereClause .= ' AND ';
  265. }
  266. switch ($receiptFrozenStatus) {
  267. case 'frozen':
  268. $whereClause .= ' `Passive` = 1 ';
  269. break;
  270. case 'unfrozen':
  271. $whereClause .= ' `Passive` = 0 ';
  272. break;
  273. }
  274. }
  275. if (!empty($receiptCity)) {
  276. if (empty($whereClause)) {
  277. $whereClause = ' WHERE ';
  278. } else {
  279. $whereClause .= ' AND ';
  280. }
  281. $whereClause .= " `city` = '" . $receiptCity . "' ";
  282. if (!empty($receiptStreet)) {
  283. $whereClause .= " AND `street` = '" . str_ireplace($receiptCity, '', $receiptStreet) . "' ";
  284. }
  285. if (!empty($receiptBuild)) {
  286. $whereClause .= " AND `build` = '" . str_ireplace($receiptCity . $receiptStreet, '', $receiptBuild) . "' ";
  287. }
  288. }
  289. if (!empty($receiptTagID)) {
  290. if ($receiptServiceType == 'inetsrv') {
  291. $tag_query_str = " `tags`.`tagid`, ";
  292. $tag_join_str = " INNER JOIN `tags` ON `users`.`login` = `tags`.`login` and `tags`.`tagid` = " . $receiptTagID . " ";
  293. } else {
  294. $tag_query_str = " `ukv_tags`.`tagtypeid`, ";
  295. $tag_join_str = " INNER JOIN `ukv_tags` ON `ukv_users`.`id` = `ukv_tags`.`userid` and `ukv_tags`.`tagtypeid` = " . $receiptTagID . " ";
  296. }
  297. } else {
  298. $tag_query_str = '';
  299. $tag_join_str = '';
  300. }
  301. if (!empty($receiptTariff)) {
  302. if (empty($whereClause)) {
  303. $whereClause = ' WHERE ';
  304. } else {
  305. $whereClause .= ' AND ';
  306. }
  307. if ($receiptServiceType == 'inetsrv') {
  308. $whereClause .= " `tariffname` = '" . $receiptTariff . "' ";
  309. } else {
  310. $whereClause .= " `tariffid` = '" . $receiptTariff . "' ";
  311. }
  312. }
  313. if (!empty($receiptUserLogin)) {
  314. if (empty($whereClause)) {
  315. $whereClause = ' WHERE ';
  316. } else {
  317. $whereClause .= ' AND ';
  318. }
  319. $whereClause .= ($receiptServiceType == 'inetsrv') ? " `login` = '" . $receiptUserLogin . "' " : " `login` = " . $receiptUserLogin . " ";
  320. }
  321. if ($this->extenAddressOn) {
  322. $addrexten_query = " `postal_code`, `town_district`, address_exten, ";
  323. $addrexten_join = " LEFT JOIN `address_extended` ON `users`.`login` = `address_extended`.`login` ";
  324. } else {
  325. $addrexten_query = " '' AS `postal_code`, '' AS `town_district`, '' AS address_exten, ";
  326. $addrexten_join = '';
  327. }
  328. if ($receiptServiceType == 'inetsrv') {
  329. $query = "SELECT * FROM
  330. (SELECT `users`.`login`, `users`.`cash`, `realname`.`realname`, `users`.`Passive`, `tariffs`.`name` AS `tariffname`, `tariffs`.`fee` AS `tariffprice`,
  331. `contracts`.`contract`, `contractdates`.`date` AS `contractdate`, `phones`.`phone`, `phones`.`mobile`, `emails`.`email`,
  332. `tmp_addr`.`cityname` AS `city`, `tmp_addr`.`streetname` AS `street`, `tmp_addr`.`buildnum` AS `build`, `tmp_addr`.`apt`, `passportdata`.`pinn` AS `inn`,
  333. " . $tag_query_str . "
  334. " . $addrexten_query . "
  335. " . $debtAsBalance . " AS `debtasbalance`
  336. FROM `users`
  337. LEFT JOIN `tariffs` ON `users`.`tariff` = `tariffs`.`name`
  338. LEFT JOIN `contracts` USING(`login`)
  339. LEFT JOIN `contractdates` USING(`contract`)
  340. LEFT JOIN `realname` USING(`login`)
  341. LEFT JOIN `phones` USING(`login`)
  342. LEFT JOIN `emails` USING(`login`)
  343. LEFT JOIN `passportdata` USING(`login`)
  344. LEFT JOIN (SELECT `address`.`login`,`city`.`id`,`city`.`cityname`,`street`.`streetname`,`build`.`buildnum`,`apt`.`apt`
  345. FROM `address`
  346. INNER JOIN `apt` ON `address`.`aptid`= `apt`.`id`
  347. INNER JOIN `build` ON `apt`.`buildid`=`build`.`id`
  348. INNER JOIN `street` ON `build`.`streetid`=`street`.`id`
  349. INNER JOIN `city` ON `street`.`cityid`=`city`.`id`
  350. ) AS `tmp_addr` USING(`login`) "
  351. . $addrexten_join
  352. . $tag_join_str . " ) AS tmpQ " .
  353. $whereClause . " ORDER BY `street` ASC, `build` ASC";
  354. } else {
  355. $query = "SELECT * FROM
  356. ( SELECT `ukv_users`.`id` AS login, `ukv_users`.*, `ukv_users`.`regdate` AS `contractdate`, `ukv_tariffs`.`tariffname`, `ukv_tariffs`.`price` AS `tariffprice`,
  357. " . $tag_query_str . "
  358. " . $addrexten_query . "
  359. " . $debtAsBalance . " AS `debtasbalance`
  360. FROM `ukv_users`
  361. LEFT JOIN `ukv_tariffs` ON `ukv_users`.`tariffid` = `ukv_tariffs`.`id` "
  362. . $addrexten_join
  363. . $tag_join_str . ") AS tmpQ " .
  364. $whereClause . " ORDER BY `street` ASC, `build` ASC";
  365. }
  366. $usersDataToPrint = simple_queryall($query);
  367. return ($usersDataToPrint);
  368. }
  369. /**
  370. * Returns macro substituted, ready to print template filled with data from $usersDataToPrint
  371. *
  372. * @param array $usersDataToPrint
  373. * @param string $rcptServiceName
  374. * @param string $rcptPayTillDate
  375. * @param int $rcptMonthsCnt
  376. * @param string $rcptPayForPeriod
  377. * @param bool $rcptSaveToDB
  378. * @param string $rcptTemplateFolder
  379. *
  380. * @return string
  381. */
  382. public function printReceipts($usersDataToPrint, $rcptServiceName = '', $rcptPayTillDate = '', $rcptMonthsCnt = 1, $rcptPayForPeriod = '', $rcptSaveToDB = false, $rcptTemplateFolder = '') {
  383. $rcptTemplateFolder = (empty($rcptTemplateFolder) or $rcptTemplateFolder == '-') ? '' : $rcptTemplateFolder . '/';
  384. $rawTemplate = file_get_contents(self::TEMPLATE_PATH . $rcptTemplateFolder . "payment_receipt.tpl");
  385. $rawTemplateHeader = file_get_contents(self::TEMPLATE_PATH . $rcptTemplateFolder . "payment_receipt_head.tpl");
  386. $rawTemplateFooter = file_get_contents(self::TEMPLATE_PATH . $rcptTemplateFolder . "payment_receipt_footer.tpl");
  387. $printableTemplate = '';
  388. $qrCodeExtInfo = '';
  389. $formatDates = 'd.m.Y';
  390. $formatTime = 'H:i:s';
  391. $formatMonthYear = 'm.Y';
  392. $i = 0;
  393. //whether to embed QR-codes ot not
  394. $qrEmbed = (strpos($rawTemplateHeader, '{QR_EMBED}') !== false);
  395. //whether to use current date and time as an invoice number
  396. $invNumCurDateTime = (strpos($rawTemplateHeader, '{INV_NUM_CURDATETIME}') !== false);
  397. preg_match('/{QR_EXT_START}(.*?){QR_EXT_END}/ms', $rawTemplateHeader, $matchResult);
  398. if (isset($matchResult[1])) {
  399. $qrCodeExtInfo = trim(str_ireplace('"', "'", $matchResult[1]));
  400. }
  401. preg_match('/{DATES_FORMAT_START}(.*?){DATES_FORMAT_END}/ms', $rawTemplateHeader, $matchResult);
  402. if (isset($matchResult[1])) {
  403. $tmpStr = trim($matchResult[1]);
  404. $formatDates = (!empty($tmpStr)) ? $tmpStr : $formatDates;
  405. }
  406. preg_match('/{MONTHYEAR_FORMAT_START}(.*?){MONTHYEAR_FORMAT_END}/ms', $rawTemplateHeader, $matchResult);
  407. if (isset($matchResult[1])) {
  408. $tmpStr = trim($matchResult[1]);
  409. $formatMonthYear = (!empty($tmpStr)) ? $tmpStr : $formatMonthYear;
  410. }
  411. $formatTimeNoDelim = str_ireplace(array('.', '/', '-', ':'), '', $formatTime);
  412. $formatDatesNoDelim = str_ireplace(array('.', '/', '-', ':'), '', $formatDates);
  413. if (!empty($rcptPayTillDate)) {
  414. $tmpDate = new DateTime($rcptPayTillDate);
  415. $rcptPayTillDate = $tmpDate->format($formatDates);
  416. }
  417. if ($rcptSaveToDB) {
  418. $tabInvoices = new nya_invoices();
  419. $tabInvoices->selectable('id');
  420. $tabInvoices->limit(1);
  421. $tabInvoices->orderBy('id', 'DESC');
  422. $lastIDRec = $tabInvoices->getAll();
  423. } else {
  424. $lastIDRec = array();
  425. }
  426. $lastID = (empty($lastIDRec) ? 0 : $lastIDRec[0]['id']);
  427. $curArrIdx = 0;
  428. log_register('PRINT RECEIPTS: number of users to proceed [' . count($usersDataToPrint) . ']');
  429. //
  430. // main template processing
  431. //
  432. foreach ($usersDataToPrint as $item => $eachUser) {
  433. if (empty($eachUser) or empty($eachUser['login'])) {
  434. continue;
  435. }
  436. $curArrIdx++;
  437. $curUsrContractDate = date($formatDates, strtotime($eachUser['contractdate']));
  438. if (!$rcptSaveToDB or $invNumCurDateTime) {
  439. if ($invNumCurDateTime) {
  440. $receiptNextNum = date($formatDatesNoDelim . $formatTimeNoDelim);
  441. } else {
  442. $receiptNextNum = date($formatDatesNoDelim . $formatTimeNoDelim) . '-' . $curArrIdx;
  443. }
  444. } else {
  445. $receiptNextNum = ++$lastID;
  446. }
  447. if ($eachUser['debtasbalance']) {
  448. $receiptPaySum = abs(round($eachUser['cash'], 2));
  449. } else {
  450. $receiptPaySum = $eachUser['tariffprice'] * $rcptMonthsCnt;
  451. }
  452. /* // replacing macro values for qr-code info in template
  453. $tmpQRCode = str_ireplace('{CURDATE}', date($formatDates), $qrCodeExtInfo);
  454. $tmpQRCode = str_ireplace('{PAYFORPERIODSTR}', $rcptPayForPeriod, $qrCodeExtInfo);
  455. $tmpQRCode = str_ireplace('{PAYTILLMONTHYEAR}', date($formatMonthYear, strtotime("+1 month")), $qrCodeExtInfo);
  456. $tmpQRCode = str_ireplace('{PAYTILLDATE}', $rcptPayTillDate, $qrCodeExtInfo);
  457. $tmpQRCode = str_ireplace('{SERVICENAME}', $rcptServiceName, $qrCodeExtInfo);
  458. $tmpQRCode = str_ireplace('{CONTRACT}', $eachUser['contract'], $qrCodeExtInfo);
  459. $tmpQRCode = str_ireplace('{REALNAME}', $eachUser['realname'], $qrCodeExtInfo);
  460. $tmpQRCode = str_ireplace('{CITY}', $eachUser['city'], $qrCodeExtInfo);
  461. $tmpQRCode = str_ireplace('{STREET}', $eachUser['street'], $qrCodeExtInfo);
  462. $tmpQRCode = str_ireplace('{BUILD}', $eachUser['build'], $qrCodeExtInfo);
  463. $tmpQRCode = str_ireplace('{APT}', (!empty($eachUser['apt'])) ? '/' . $eachUser['apt'] : '', $qrCodeExtInfo);
  464. $tmpQRCode = str_ireplace('{PHONE}', $eachUser['phone'], $qrCodeExtInfo);
  465. $tmpQRCode = str_ireplace('{MOBILE}', $eachUser['mobile'], $qrCodeExtInfo);
  466. $tmpQRCode = str_ireplace('{TARIFF}', $eachUser['tariffname'], $qrCodeExtInfo);
  467. $tmpQRCode = str_ireplace('{TARIFFPRICE}', $eachUser['tariffprice'], $qrCodeExtInfo);
  468. $tmpQRCode = str_ireplace('{TARIFFPRICECOINS}', $eachUser['tariffprice'] * 100, $qrCodeExtInfo);
  469. $tmpQRCode = str_ireplace('{TARIFFPRICEDECIMALS}', number_format((float)$eachUser['tariffprice'], 2, '.', ''), $qrCodeExtInfo);
  470. $tmpQRCode = str_ireplace('{SUMM}', $receiptPaySum, $qrCodeExtInfo);
  471. $tmpQRCode = str_ireplace('{SUMMCOINS}', $receiptPaySum * 100, $qrCodeExtInfo);
  472. $tmpQRCode = str_ireplace('{SUMMDECIMALS}', number_format((float)($receiptPaySum),2, '.', ''), $qrCodeExtInfo);
  473. // replacing macro values in template
  474. $rowtemplate = $rawTemplate;
  475. $rowtemplate = str_ireplace('{QR_INDEX}', ++$i, $rowtemplate);
  476. $rowtemplate = str_ireplace('{QR_CODE_CONTENT}', $tmpQRCode, $rowtemplate);
  477. $rowtemplate = str_ireplace('{CURDATE}', date($formatDates), $rowtemplate);
  478. $rowtemplate = str_ireplace('{PAYFORPERIODSTR}', $rcptPayForPeriod, $rowtemplate);
  479. $rowtemplate = str_ireplace('{PAYTILLMONTHYEAR}', date($formatMonthYear, strtotime("+1 month")), $rowtemplate);
  480. $rowtemplate = str_ireplace('{PAYTILLDATE}', $rcptPayTillDate, $rowtemplate);
  481. $rowtemplate = str_ireplace('{SERVICENAME}', $rcptServiceName, $rowtemplate);
  482. $rowtemplate = str_ireplace('{CONTRACT}', $eachUser['contract'], $rowtemplate);
  483. $rowtemplate = str_ireplace('{REALNAME}', $eachUser['realname'], $rowtemplate);
  484. $rowtemplate = str_ireplace('{CITY}', $eachUser['city'], $rowtemplate);
  485. $rowtemplate = str_ireplace('{STREET}', $eachUser['street'], $rowtemplate);
  486. $rowtemplate = str_ireplace('{BUILD}', $eachUser['build'], $rowtemplate);
  487. $rowtemplate = str_ireplace('{APT}', (!empty($eachUser['apt'])) ? '/' . $eachUser['apt'] : '', $rowtemplate);
  488. $rowtemplate = str_ireplace('{PHONE}', $eachUser['phone'], $rowtemplate);
  489. $rowtemplate = str_ireplace('{MOBILE}', $eachUser['mobile'], $rowtemplate);
  490. $rowtemplate = str_ireplace('{TARIFF}', $eachUser['tariffname'], $rowtemplate);
  491. $rowtemplate = str_ireplace('{TARIFFPRICE}', $eachUser['tariffprice'], $rowtemplate);
  492. $rowtemplate = str_ireplace('{TARIFFPRICECOINS}', $eachUser['tariffprice'] * 100, $rowtemplate);
  493. $rowtemplate = str_ireplace('{TARIFFPRICEDECIMALS}', number_format((float)$eachUser['tariffprice'], 2, '.', ''), $rowtemplate);
  494. $rowtemplate = str_ireplace('{SUMM}', $receiptPaySum, $rowtemplate);
  495. $rowtemplate = str_ireplace('{SUMMCOINS}', $receiptPaySum * 100, $rowtemplate);
  496. $rowtemplate = str_ireplace('{SUMMDECIMALS}', number_format((float)($receiptPaySum),2, '.', ''), $rowtemplate);
  497. $printableTemplate.= $rowtemplate;
  498. } */
  499. // replacing macro values for qr-code info in template
  500. $tmpQRCode = $qrCodeExtInfo;
  501. $tmpQRCode = $this->replaceMainTemplateMacro(
  502. $tmpQRCode,
  503. date($formatDates),
  504. date($formatTime),
  505. date($formatDatesNoDelim),
  506. date($formatTimeNoDelim),
  507. $receiptNextNum,
  508. $rcptMonthsCnt,
  509. $rcptPayForPeriod,
  510. date($formatMonthYear, strtotime("+1 month")),
  511. $rcptPayTillDate,
  512. $rcptServiceName,
  513. $eachUser['contract'],
  514. $curUsrContractDate,
  515. $eachUser['realname'],
  516. $eachUser['city'],
  517. $eachUser['street'],
  518. $eachUser['build'],
  519. (!empty($eachUser['apt'])) ? '/' . $eachUser['apt'] : '',
  520. $eachUser['postal_code'],
  521. $eachUser['town_district'],
  522. $eachUser['address_exten'],
  523. $eachUser['phone'],
  524. $eachUser['mobile'],
  525. @$eachUser['inn'],
  526. $eachUser['tariffname'],
  527. $eachUser['tariffprice'],
  528. $eachUser['tariffprice'] * 100,
  529. number_format((float) $eachUser['tariffprice'], 2, '.', ''),
  530. $receiptPaySum,
  531. $receiptPaySum * 100,
  532. number_format((float) ($receiptPaySum), 2, '.', '')
  533. );
  534. // replacing macro values in template
  535. $rowtemplate = $rawTemplate;
  536. $rowtemplate = str_ireplace('{QR_INDEX}', ++$i, $rowtemplate);
  537. //embed the qr-code image or just put filled data
  538. if ($qrEmbed) {
  539. $rowtemplate = str_ireplace('{QR_CODE_CONTENT}', $tmpQRCode, $rowtemplate);
  540. $qrGen = new QRCode($tmpQRCode, array('w' => 150, 'h' => 150));
  541. ob_start();
  542. imagepng($qrGen->render_image());
  543. $imageBody = ob_get_contents();
  544. ob_end_clean();
  545. $qrImg = base64_encode($imageBody);
  546. $qrImg = '<img src="data:image/png;base64,' . $qrImg . '" alt="QR-CODE" />';
  547. $rowtemplate = str_ireplace('{QR_CODE_EMBEDDED}', $qrImg, $rowtemplate);
  548. } else {
  549. $rowtemplate = str_ireplace('{QR_CODE_CONTENT}', $tmpQRCode, $rowtemplate);
  550. $rowtemplate = str_ireplace('{QR_CODE_EMBEDDED}', '', $rowtemplate);
  551. }
  552. $rowtemplate = $this->replaceMainTemplateMacro(
  553. $rowtemplate,
  554. date($formatDates),
  555. date($formatTime),
  556. date($formatDatesNoDelim),
  557. date($formatTimeNoDelim),
  558. $receiptNextNum,
  559. $rcptMonthsCnt,
  560. $rcptPayForPeriod,
  561. date($formatMonthYear, strtotime("+1 month")),
  562. $rcptPayTillDate,
  563. $rcptServiceName,
  564. $eachUser['contract'],
  565. $curUsrContractDate,
  566. $eachUser['realname'],
  567. $eachUser['city'],
  568. $eachUser['street'],
  569. $eachUser['build'],
  570. (!empty($eachUser['apt'])) ? '/' . $eachUser['apt'] : '',
  571. $eachUser['apt'],
  572. $eachUser['postal_code'],
  573. $eachUser['town_district'],
  574. $eachUser['address_exten'],
  575. $eachUser['phone'],
  576. $eachUser['mobile'],
  577. @$eachUser['inn'],
  578. zb_OschadCSgen(@$eachUser['inn']),
  579. $eachUser['tariffname'],
  580. $eachUser['tariffprice'],
  581. $eachUser['tariffprice'] * 100,
  582. number_format((float) $eachUser['tariffprice'], 2, '.', ''),
  583. $receiptPaySum,
  584. $receiptPaySum * 100,
  585. number_format((float) ($receiptPaySum), 2, '.', '')
  586. );
  587. $printableTemplate .= $rowtemplate;
  588. if ($rcptSaveToDB) {
  589. $singleTemplateHeader = $rawTemplateHeader;
  590. $singleTemplateHeader = str_ireplace('{QR_CODES_CNT}', '1', $singleTemplateHeader);
  591. // getting one single receipt with header and footer as a separate html document
  592. $singleReceipt = $singleTemplateHeader . $rowtemplate . $rawTemplateFooter;
  593. $this->saveToDB($eachUser['login'], $receiptNextNum, curdatetime(), $receiptPaySum, base64_encode($singleReceipt));
  594. }
  595. }
  596. log_register('PRINT RECEIPTS: number of invoices created [' . $curArrIdx . ']');
  597. $rawTemplateHeader = str_ireplace('{QR_CODES_CNT}', $i, $rawTemplateHeader);
  598. return ($rawTemplateHeader . $printableTemplate . $rawTemplateFooter);
  599. }
  600. /**
  601. * Replaces macro in given main receipt template
  602. *
  603. * @param $rcptTemplate
  604. * @param string $tplCurDate
  605. * @param string $tplCurTime
  606. * @param string $tplCurDateNoDelims
  607. * @param string $tplCurTimeNoDelims
  608. * @param string $tplCrhgPeriodDStart
  609. * @param string $tplCrhgPeriodDEnd
  610. * @param string $tplInvoiceNum
  611. * @param string $tplMonthCnt
  612. * @param string $tplPayForPeriodStr
  613. * @param string $tplPayTillMnthYr
  614. * @param string $tplPayTillDate
  615. * @param string $tplSrvName
  616. * @param string $tplContract
  617. * @param string $tplContractDate
  618. * @param string $tplRealName
  619. * @param string $tplCity
  620. * @param string $tplStreet
  621. * @param string $tplBuild
  622. * @param string $tplApt
  623. * @param string $tplEAddrPostCode
  624. * @param string $tplEAddrTwnDstr
  625. * @param string $tplEAddrExt
  626. * @param string $tplPhone
  627. * @param string $tplMobile
  628. * @param string $tplTotalCoins
  629. * @param string $tplTotalDecimals
  630. * @param string $tplTotalVATCoins
  631. * @param string $tplTotalVATDecimals
  632. * @param string $tplTotalWithVATCoins
  633. * @param string $tplTotalWithVATDecimals
  634. * @param string $tplServicesRows
  635. *
  636. * @return mixed
  637. */
  638. public function replaceMainTemplateMacro(
  639. $rcptTemplate,
  640. $tplCurDate = '',
  641. $tplCurTime = '',
  642. $tplCurDateNoDelims = '',
  643. $tplCurTimeNoDelims = '',
  644. $tplInvoiceNum = '',
  645. $tplMonthCnt = '',
  646. $tplPayForPeriodStr = '',
  647. $tplPayTillMnthYr = '',
  648. $tplPayTillDate = '',
  649. $tplSrvName = '',
  650. $tplContract = '',
  651. $tplContractDate = '',
  652. $tplRealName = '',
  653. $tplCity = '',
  654. $tplStreet = '',
  655. $tplBuild = '',
  656. $tplApt = '',
  657. $tplAPT2 = '',
  658. $tplEAddrPostCode = '',
  659. $tplEAddrTwnDstr = '',
  660. $tplEAddrExt = '',
  661. $tplPhone = '',
  662. $tplMobile = '',
  663. $tplInn = '',
  664. $tploshadCS = '',
  665. $tplTariff = '',
  666. $tplTrfPrice = 0,
  667. $tplTrfPriceCoins = 0,
  668. $tplTrfPriceDecimals = 0,
  669. $tplSumm = 0,
  670. $tplSummCoins = 0,
  671. $tplSummDecimals = 0
  672. ) {
  673. $rcptTemplate = str_ireplace('{CURDATE}', $tplCurDate, $rcptTemplate);
  674. $rcptTemplate = str_ireplace('{CURTIME}', $tplCurTime, $rcptTemplate);
  675. $rcptTemplate = str_ireplace('{CURDATENODELIMS}', $tplCurDateNoDelims, $rcptTemplate);
  676. $rcptTemplate = str_ireplace('{CURDATETIMENODELIMS}', $tplCurTimeNoDelims, $rcptTemplate);
  677. $rcptTemplate = str_ireplace('{INVOICE_NUM}', $tplInvoiceNum, $rcptTemplate);
  678. $rcptTemplate = str_ireplace('{MONTH_COUNT}', $tplMonthCnt, $rcptTemplate);
  679. $rcptTemplate = str_ireplace('{PAYFORPERIODSTR}', $tplPayForPeriodStr, $rcptTemplate);
  680. $rcptTemplate = str_ireplace('{PAYTILLMONTHYEAR}', $tplPayTillMnthYr, $rcptTemplate);
  681. $rcptTemplate = str_ireplace('{PAYTILLDATE}', $tplPayTillDate, $rcptTemplate);
  682. $rcptTemplate = str_ireplace('{SERVICENAME}', $tplSrvName, $rcptTemplate);
  683. $rcptTemplate = str_ireplace('{CONTRACT}', $tplContract, $rcptTemplate);
  684. $rcptTemplate = str_ireplace('{CONTRACTDATE}', $tplContractDate, $rcptTemplate);
  685. $rcptTemplate = str_ireplace('{REALNAME}', $tplRealName, $rcptTemplate);
  686. $rcptTemplate = str_ireplace('{CITY}', $tplCity, $rcptTemplate);
  687. $rcptTemplate = str_ireplace('{STREET}', $tplStreet, $rcptTemplate);
  688. $rcptTemplate = str_ireplace('{BUILD}', $tplBuild, $rcptTemplate);
  689. $rcptTemplate = str_ireplace('{APT}', $tplApt, $rcptTemplate);
  690. $rcptTemplate = str_ireplace('{APT2}', $tplAPT2, $rcptTemplate);
  691. $rcptTemplate = str_ireplace('{EXTADDR_POSTALCODE}', $tplEAddrPostCode, $rcptTemplate);
  692. $rcptTemplate = str_ireplace('{EXTADDR_TOWNDISTR}', $tplEAddrTwnDstr, $rcptTemplate);
  693. $rcptTemplate = str_ireplace('{EXTADDR_ADDREXT}', $tplEAddrExt, $rcptTemplate);
  694. $rcptTemplate = str_ireplace('{PHONE}', $tplPhone, $rcptTemplate);
  695. $rcptTemplate = str_ireplace('{INN}', $tplInn, $rcptTemplate);
  696. $rcptTemplate = str_ireplace('{oshadCS}', $tploshadCS, $rcptTemplate);
  697. $rcptTemplate = str_ireplace('{MOBILE}', $tplMobile, $rcptTemplate);
  698. $rcptTemplate = str_ireplace('{TARIFF}', $tplTariff, $rcptTemplate);
  699. $rcptTemplate = str_ireplace('{TARIFFPRICE}', $tplTrfPrice, $rcptTemplate);
  700. $rcptTemplate = str_ireplace('{TARIFFPRICECOINS}', $tplTrfPriceCoins, $rcptTemplate);
  701. $rcptTemplate = str_ireplace('{TARIFFPRICEDECIMALS}', $tplTrfPriceDecimals, $rcptTemplate);
  702. $rcptTemplate = str_ireplace('{SUMM}', $tplSumm, $rcptTemplate);
  703. $rcptTemplate = str_ireplace('{SUMMCOINS}', $tplSummCoins, $rcptTemplate);
  704. $rcptTemplate = str_ireplace('{SUMMDECIMALS}', $tplSummDecimals, $rcptTemplate);
  705. return ($rcptTemplate);
  706. }
  707. /**
  708. * Returns receipts print web form
  709. *
  710. * @return string
  711. */
  712. public function renderWebForm() {
  713. $inputs = '';
  714. if ($this->receiptsHistoryOn) {
  715. $inputs .= wf_Link(self::URL_ME . '&showhistory=true', __('Issued receipts'), true, 'ubButton', 'style="width: 90%; text-align: center;"');
  716. $inputs .= wf_delimiter(0);
  717. }
  718. $inputs .= wf_tag('div', false, '', 'style="line-height: 0.8em"');
  719. $inputs .= wf_RadioInput('receiptsrv', __('Internet'), 'inetsrv', false, true, 'ReceiptSrvInet');
  720. $inputs .= wf_RadioInput('receiptsrv', __('UKV'), 'ctvsrv', true, false, 'ReceiptSrvCTV');
  721. $inputs .= wf_delimiter(0);
  722. $inputs .= wf_TextInput('receiptsrvtxt', __('Service'), __('Internet'), true, '28', '', '', 'ReceiptSrvName');
  723. $inputs .= wf_delimiter(0);
  724. $inputs .= wf_Selector('receipttemplate', $this->receiptTemplateFolders, __('Choose template'), '', true, false, 'ReceiptTemplate');
  725. $inputs .= wf_delimiter(0);
  726. $inputs .= wf_Selector('receiptsubscrstatus', $this->receiptAllUserStatuses, __('Subscriber\'s account status'), '', true, false, 'ReceiptDirSel');
  727. $inputs .= wf_Selector('receiptfrozenstatus', $this->receiptAllFrozenStatuses, __('Subscriber\'s frozen status'), '', true, false, 'ReceiptFrozenSel');
  728. $inputs .= wf_TextInput('receiptdebtcash', __('The threshold at which the money considered user debtor'), '0', true, '4', '', '', 'ReceiptDebtSumm');
  729. $inputs .= wf_TextInput('receiptmonthscnt', __('Amount of months to be payed(will be multiplied on tariff cost)'), '1', true, '4', '', '', 'ReceiptMonthsCnt');
  730. $inputs .= wf_delimiter(0);
  731. $inputs .= wf_Selector('receiptscities', $this->receiptAllCities, __('City'), '', true, true, 'ReceiptCities');
  732. $inputs .= wf_Selector('receiptstreets', array('-' => '-'), __('Street'), '', true, true, 'ReceiptStreets');
  733. $inputs .= wf_Selector('receiptbuilds', array('-' => '-'), __('Build'), '', true, true, 'ReceiptBuilds');
  734. $inputs .= wf_delimiter(0);
  735. $inputs .= wf_Selector('receipttariffs', $this->getAllTariffs(), __('User has tariff assigned'), '', true, true, 'ReceiptTariffs');
  736. $inputs .= wf_delimiter(0);
  737. $inputs .= wf_Selector('receipttags', $this->getAllTags(), __('User have tag assigned'), '', true, true, 'ReceiptTags');
  738. $inputs .= wf_delimiter(0);
  739. $inputs .= wf_TextInput('receiptpayperiod', __('Pay for period(months), e.g.: March 2019, April 2019'), '', true, '40', '', '', 'ReceiptPayPeriod');
  740. $inputs .= wf_delimiter(0);
  741. $inputs .= wf_tag('span', false);
  742. $inputs .= wf_DatePickerPreset('receiptpaytill', date("Y-m-d", strtotime("+5 days")), true);
  743. $inputs .= wf_nbsp(2) . __('Pay till date');
  744. $inputs .= wf_tag('span', true);
  745. if ($this->receiptsHistoryOn) {
  746. $inputs .= wf_delimiter(1);
  747. $inputs .= wf_CheckInput('receiptsaveindb', __('Save receipt(s) to DB'), true, false, 'ReceiptSaveInDB');
  748. }
  749. $inputs .= wf_delimiter(1);
  750. $inputs .= wf_HiddenInput('tmpstreetsall', base64_encode(json_encode($this->receiptAllStreets)), 'TmpStreetsAll');
  751. $inputs .= wf_HiddenInput('tmpbuildsall', base64_encode(json_encode($this->receiptAllBuilds)), 'TmpBuildsAll');
  752. $inputs .= wf_HiddenInput('tmpinettariffs', base64_encode(json_encode($this->getAllTariffs())), 'TmpInetTariffs');
  753. $inputs .= wf_HiddenInput('tmpukvtariffs', base64_encode(json_encode($this->getAllTariffs(false))), 'TmpUkvTariffs');
  754. $inputs .= wf_HiddenInput('printthemall', 'true', 'PrintThemAll');
  755. $inputs .= wf_Submit(__('Print'), '', 'class="ubButton" style="width: 100%"');
  756. $inputs .= wf_tag('script', false, '', 'type="text/javascript"');
  757. $inputs .= '$(document).ready(function() {
  758. $("[name=receiptsrv]").change(function(evt) {
  759. var tmpStr;
  760. if ($(this).val() == \'inetsrv\') {
  761. tmpStr = \'' . __('Internet') . '\';
  762. exchangeSrvsTariffs(true);
  763. } else {
  764. tmpStr = \'' . __('Cable television') . '\';
  765. exchangeSrvsTariffs(false);
  766. }
  767. $(\'#ReceiptSrvName\').val(tmpStr);
  768. });
  769. $(\'#ReceiptDirSel\').change(function(evt) {
  770. switch ($(this).val()) {
  771. case \'all\':
  772. $(\'#ReceiptDebtSumm\').val(\'\');
  773. $(\'#ReceiptDebtSumm\').hide();
  774. $("label[for=\'ReceiptDebtSumm\']").text(\'\');
  775. break;
  776. case \'debtasbalance\':
  777. $(\'#ReceiptDebtSumm\').val(\'\');
  778. $(\'#ReceiptDebtSumm\').hide();
  779. $("label[for=\'ReceiptDebtSumm\']").text(\'\');
  780. $(\'#ReceiptMonthsCnt\').val(\'\');
  781. $(\'#ReceiptMonthsCnt\').hide();
  782. $("label[for=\'ReceiptMonthsCnt\']").text(\'\');
  783. break;
  784. default:
  785. $(\'#ReceiptDebtSumm\').val(\'0\');
  786. $(\'#ReceiptDebtSumm\').show();
  787. $("label[for=\'ReceiptDebtSumm\']").text(\'' . __('The threshold at which the money considered user debtor') . '\');
  788. $(\'#ReceiptMonthsCnt\').val(\'0\');
  789. $(\'#ReceiptMonthsCnt\').show();
  790. $("label[for=\'ReceiptMonthsCnt\']").text(\'' . __('Amount of months to be payed(will be multiplied on tariff cost)') . '\');
  791. }
  792. });
  793. $(\'#ReceiptCities\').change(function(evt) {
  794. var keyword = $(this).val();
  795. var source = JSON.parse(atob($(\'#TmpStreetsAll\').val()));
  796. filterStreetsSelect(keyword, source);
  797. $(\'#ReceiptStreets\').change();
  798. });
  799. $(\'#ReceiptStreets\').change(function(evt) {
  800. var keyword = $(this).val();
  801. var source = JSON.parse(atob($(\'#TmpBuildsAll\').val()));
  802. filterBuildsSelect(keyword, source);
  803. });
  804. function filterStreetsSelect(search_keyword, search_array) {
  805. var newselect = \'<option value>-</option>\';
  806. if (search_keyword.length > 0 && search_keyword.trim() !== "-") {
  807. for (var key in search_array) {
  808. if ( key.trim() !== "" && key.toLowerCase() == search_keyword.toLowerCase() + search_array[key].toLowerCase() ) {
  809. newselect = newselect + \'<option value="\' + key + \'">\' + search_array[key] + \'</option>\';
  810. }
  811. }
  812. }
  813. $(\'#ReceiptStreets\').html(newselect);
  814. }
  815. function filterBuildsSelect(search_keyword, search_array) {
  816. var newselect = $("<select id=\"ReceiptBuilds\" name=\"receiptbuilds\" />");
  817. $("<option />", {value: \'\', text: \'-\'}).appendTo(newselect);
  818. if (search_keyword.length > 0 && search_keyword.trim() !== "-") {
  819. for (var key in search_array) {
  820. if ( key.trim() !== "" && key.toLowerCase() == search_keyword.toLowerCase() + search_array[key].toLowerCase() ) {
  821. $("<option />", {value: key, text: search_array[key]}).appendTo(newselect);
  822. }
  823. }
  824. }
  825. $(\'#ReceiptBuilds\').replaceWith(newselect);
  826. }
  827. function exchangeSrvsTariffs(isInetSrv) {
  828. if (isInetSrv) {
  829. var source = JSON.parse(atob($(\'#TmpInetTariffs\').val()));
  830. } else {
  831. var source = JSON.parse(atob($(\'#TmpUkvTariffs\').val()));
  832. }
  833. var newselect = $("<select id=\"ReceiptTariffs\" name=\"receipttariffs\" />");
  834. for (var key in source) {
  835. $("<option />", {value: key, text: source[key]}).appendTo(newselect);
  836. }
  837. $(\'#ReceiptTariffs\').replaceWith(newselect);
  838. }
  839. var keyword = $(\'#ReceiptCities\').val();
  840. var source = JSON.parse(atob($(\'#TmpStreetsAll\').val()));
  841. filterStreetsSelect(keyword, source);
  842. var keyword = $(\'#ReceiptStreets\').val();
  843. var source = JSON.parse(atob($(\'#TmpBuildsAll\').val()));
  844. filterBuildsSelect(keyword, source);
  845. });
  846. ';
  847. $inputs .= wf_tag('script', true);
  848. $inputs .= wf_tag('div', true);
  849. $form = wf_Form('?module=printreceipts', 'POST', $inputs, 'glamour', '', 'ReceiptPrintForm', "_blank");
  850. return ($form);
  851. }
  852. /**
  853. * Returns button with modal form attached for user profile
  854. *
  855. * @param mixed $receiptLogin
  856. * @param $receiptServiceType
  857. * @param string $receiptServiceName
  858. * @param mixed $userBalance
  859. * @param string $receiptStreet
  860. * @param string $receiptBuild
  861. *
  862. * @return string
  863. */
  864. public function renderWebFormForProfile($receiptLogin, $receiptServiceType, $receiptServiceName = '', $userBalance = 0, $receiptStreet = '', $receiptBuild = '') {
  865. $receiptSumSources = array(
  866. 'debtasbalance' => __('Get current balance debt sum'),
  867. '' => __('Specify number of months')
  868. );
  869. $inputs = wf_Selector('receipttemplate', $this->receiptTemplateFolders, __('Choose template'), '', true, false, 'ReceiptTemplate');
  870. $inputs .= wf_delimiter(0);
  871. $inputs .= wf_TextInput('receiptsrvtxt', __('Service'), __($receiptServiceName), true, '28', '', '', 'ReceiptSrvName');
  872. $inputs .= wf_delimiter(0);
  873. if ($userBalance < 0) {
  874. $inputs .= wf_Selector('receiptsumsource', $receiptSumSources, __('Specify receipt sum source'), '', true, false, 'ReceiptSumSource');
  875. $inputs .= wf_TextInput('receiptbalancesum', __('Current user\'s balance debt sum'), abs(round($userBalance, 2)), true, '4', '', '', 'ReceiptBalanceSum', 'readonly="readonly"');
  876. }
  877. $inputs .= wf_TextInput('receiptmonthscnt', __('Amount of months to be payed(will be multiplied on tariff cost)'), '1', true, '4', '', '', 'ReceiptMonthsCnt');
  878. $inputs .= wf_TextInput('receiptpayperiod', __('Pay for period(months), e.g.: March 2019, April 2019'), '', true, '40', '', '', 'ReceiptPayPeriod');
  879. $inputs .= wf_delimiter(0);
  880. $inputs .= wf_tag('span', false);
  881. $inputs .= wf_DatePickerPreset('receiptpaytill', date("Y-m-d", strtotime("+5 days")), true);
  882. $inputs .= wf_nbsp(2) . __('Pay till date');
  883. $inputs .= wf_tag('span', true);
  884. if ($this->receiptsHistoryOn) {
  885. $inputs .= wf_delimiter(1);
  886. $inputs .= wf_CheckInput('receiptsaveindb', __('Save receipt(s) to DB'), true, false, 'ReceiptSaveInDB');
  887. }
  888. $inputs .= wf_HiddenInput('receiptsubscrstatus', '', 'ReceiptSubscrStatus');
  889. $inputs .= wf_HiddenInput('receiptdebtcash', '');
  890. $inputs .= wf_HiddenInput('receiptsrv', $receiptServiceType);
  891. $inputs .= wf_HiddenInput('receiptslogin', $receiptLogin);
  892. $inputs .= wf_HiddenInput('receiptstreets', $receiptStreet);
  893. $inputs .= wf_HiddenInput('receiptbuilds', $receiptBuild);
  894. $inputs .= wf_HiddenInput('printthemall', 'true');
  895. $inputs .= wf_delimiter(0);
  896. $inputs .= wf_Submit(__('Print'), '', 'class="ubButton" style="width: 100%"');
  897. $form = wf_Form('?module=printreceipts', 'POST', $inputs, 'glamour', '', 'ReceiptPrintForm', "_blank");
  898. if ($userBalance < 0) {
  899. $form .= wf_tag('script', false, '', 'type="text/javascript"');
  900. $form .= '
  901. $(document).ready(function() {
  902. endisControls();
  903. });
  904. $(\'#ReceiptSumSource\').change(function() {
  905. endisControls();
  906. });
  907. function endisControls() {
  908. if ( $(\'#ReceiptSumSource\').val() == \'debtasbalance\' ) {
  909. $(\'#ReceiptBalanceSum\').prop("disabled", false);
  910. $(\'#ReceiptMonthsCnt\').prop("disabled", true);
  911. } else {
  912. $(\'#ReceiptBalanceSum\').prop("disabled", true);
  913. $(\'#ReceiptMonthsCnt\').prop("disabled", false);
  914. }
  915. $(\'#ReceiptSubscrStatus\').val($(\'#ReceiptSumSource\').val());
  916. }
  917. ';
  918. $form .= wf_tag('script', true);
  919. }
  920. $form = wf_modalAuto(wf_img_sized('skins/taskbar/receipt_big.png', __('Print receipt'), '', '64'), __('Print receipt'), $form);
  921. return ($form);
  922. }
  923. /**
  924. * Renders JQDT and returns it
  925. *
  926. * @return string
  927. */
  928. public function renderJQDT($userLogin = '') {
  929. $ajaxUrlStr = (empty($userLogin)) ? self::URL_ME . '&ajax=true' : self::URL_ME . '&ajax=true&usrlogin=' . $userLogin;
  930. $columns = array(__('ID'), __('Login'), __('Number'), __('Date'), __('Sum'), __('Actions'));
  931. $formID = wf_InputId();
  932. $jqdtId = 'jqdt_' . md5($ajaxUrlStr);
  933. // filter controls for dates
  934. $inputs = wf_DatePicker('invdatefrom');
  935. $inputs .= __('Creation date from') . wf_nbsp(3);
  936. $inputs .= wf_DatePicker('invdateto');
  937. $inputs .= __('Creation date to') . wf_nbsp(4);
  938. $inputs .= wf_SubmitClassed(true, 'ubButton', '', __('Show'));
  939. $inputs .= wf_tag('script', false, '', 'type="text/javascript"');
  940. $inputs .= '
  941. $(\'#' . $formID . '\').submit(function(evt) {
  942. evt.preventDefault();
  943. var FrmData = $(\'#' . $formID . '\').serialize();
  944. $(\'#' . $jqdtId . '\').DataTable().ajax.url(\'' . $ajaxUrlStr . '\' + \'&\' + FrmData).load();
  945. //$(\'#' . $jqdtId . '\').DataTable().ajax.url(\'' . $ajaxUrlStr . '\');
  946. });
  947. ';
  948. $inputs .= wf_tag('script', true);
  949. $form = wf_Form('', 'POST', $inputs, 'glamour', '', $formID) . wf_delimiter(0);
  950. return ($form . wf_JqDtLoader($columns, $ajaxUrlStr, false, __('results'), 100));
  951. }
  952. /**
  953. * Renders JSON for JQDT
  954. */
  955. public function renderJSON($queryData) {
  956. $json = new wf_JqDtHelper();
  957. if (!empty($queryData)) {
  958. $data = array();
  959. foreach ($queryData as $eachRec) {
  960. $data[] = $eachRec['id'];
  961. $data[] = $eachRec['login'];
  962. $data[] = $eachRec['invoice_num'];
  963. $data[] = $eachRec['invoice_date'];
  964. $data[] = $eachRec['invoice_sum'];
  965. $data[] = wf_Link(self::URL_ME . '&printid=' . $eachRec['id'], __('Print'), false, 'ubButton', 'target="_blank"');
  966. $json->addRow($data);
  967. unset($data);
  968. }
  969. }
  970. $json->getJson();
  971. }
  972. }