api.pseudocrm.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. <?php
  2. /**
  3. * It is definitely not CRM and does not even look like it
  4. */
  5. class PseudoCRM {
  6. /**
  7. *
  8. * Contains system alter config as key=>value
  9. *
  10. * @var array
  11. */
  12. protected $altCfg = array();
  13. /**
  14. * Leads database abstraction layer
  15. *
  16. * @var object
  17. */
  18. protected $leadsDb = '';
  19. /**
  20. * Activities database abstraction layer
  21. *
  22. * @var object
  23. */
  24. protected $activitiesDb = '';
  25. /**
  26. * Messages system helper placeholder
  27. *
  28. * @var object
  29. */
  30. protected $messages = '';
  31. /**
  32. * Contains all leads data as id=>leadData
  33. *
  34. * @var array
  35. */
  36. protected $allLeads = array();
  37. /**
  38. * Contains all activities data as id=>activityData
  39. *
  40. * @var array
  41. */
  42. protected $allActivities = array();
  43. /**
  44. * Contains all employee data as id=>name
  45. *
  46. * @var array
  47. */
  48. protected $allEmployee = array();
  49. /**
  50. * Contains all employee Telegram chatId as id=>chatid
  51. *
  52. * @var array
  53. */
  54. protected $allEmployeeChatIds = array();
  55. /**
  56. * Contains all active employee data as id=>name
  57. *
  58. * @var array
  59. */
  60. protected $allActiveEmployee = array();
  61. /**
  62. * Contains all employee administator logins as login=>employeeId
  63. *
  64. * @var array
  65. */
  66. protected $allEmployeeLogins = array();
  67. /**
  68. * Contains branches data as id=>name
  69. *
  70. * @var array
  71. */
  72. protected $allBranches = array();
  73. /**
  74. * Is branches enabled flag?
  75. *
  76. * @var bool
  77. */
  78. protected $branchesFlag = false;
  79. /**
  80. * Contains available states stigma scopes for activities as SCOPE=>__(name)
  81. *
  82. * @var array
  83. */
  84. protected $activitiesStatesList = array();
  85. /**
  86. * Contains all available tariff names as tariffName=>__(tariffName)
  87. *
  88. * @var array
  89. */
  90. protected $allTariffs = array();
  91. /**
  92. * Contains all available users data as login=>userData
  93. *
  94. * @var array
  95. */
  96. protected $allUserData = array();
  97. /**
  98. * Is senddog enabled flag?
  99. *
  100. * @var bool
  101. */
  102. protected $sendDogEnabled = false;
  103. /**
  104. * Activities protection mechanics flag
  105. *
  106. * @var bool
  107. */
  108. protected $activityProtectedFlag = false;
  109. /**
  110. * Contains current administrator login
  111. *
  112. * @var string
  113. */
  114. protected $myLogin = '';
  115. /**
  116. * Some other predefined stuff
  117. */
  118. const RIGHT_VIEW = 'PSEUDOCRM';
  119. const RIGHT_LEADS = 'PSEUDOCRMLEADS';
  120. const RIGHT_ACTIVITIES = 'PSEUDOCRMACTS';
  121. const RIGHT_ACT_MANAGER = 'PSEUDOCRMACTMGR';
  122. const RIGHT_TASKS = 'TASKMAN';
  123. /**
  124. * database shortcuts
  125. */
  126. const TABLE_LEADS = 'crm_leads';
  127. const TABLE_ACTIVITIES = 'crm_activities';
  128. const TABLE_STATES_LOG = 'crm_stateslog';
  129. const OPT_ACT_CUSTSTATES = 'PSEUDOCRM_ACT_CUSTSTATES';
  130. const OPT_ACT_PROTECTED = 'PSEUDOCRM_ACT_PROTECTED';
  131. /**
  132. * routes here
  133. */
  134. const URL_ME = '?module=pseudocrm';
  135. const ROUTE_LEADS_LIST = 'leadslist';
  136. const ROUTE_LEADS_LIST_AJ = 'ajaxleadslist';
  137. const ROUTE_LEAD_PROFILE = 'showlead';
  138. const ROUTE_ACTIVITY_PROFILE = 'showactivity';
  139. const ROUTE_ACTIVITY_CREATE = 'createnewactivity';
  140. const ROUTE_LEAD_DETECT = 'username';
  141. const ROUTE_ACTIVITY_DONE = 'setactivitydone';
  142. const ROUTE_ACTIVITY_UNDONE = 'setactivityundone';
  143. const ROUTE_REPORT_SOURCES = 'reportleadsources';
  144. const ROUTE_REPORT_STATESLOG = 'reportstates';
  145. const ROUTE_REPORT_STATESLOG_AJ = 'ajaxtstatesreport';
  146. /**
  147. * post-routes
  148. */
  149. const PROUTE_LEAD_CREATE = 'leadcreatenew';
  150. const PROUTE_LEAD_SAVE = 'leadeditexisting';
  151. const PROUTE_LEAD_ASSIGN = 'assignlogintolead';
  152. const PROUTE_LEAD_ASSIGN_ID = 'leadidtoassign';
  153. const PROUTE_LEAD_ADDR = 'leadaddress';
  154. const PROUTE_LEAD_NAME = 'leadname';
  155. const PROUTE_LEAD_PHONE = 'leadphone';
  156. const PROUTE_LEAD_MOBILE = 'leadmobile';
  157. const PROUTE_LEAD_EXTMOBILE = 'leadextmobile';
  158. const PROUTE_LEAD_EMAIL = 'leademail';
  159. const PROUTE_LEAD_BRANCH = 'leadbranchid';
  160. const PROUTE_LEAD_TARIFF = 'leadtariff';
  161. const PROUTE_LEAD_LOGIN = 'leadlogin';
  162. const PROUTE_LEAD_EMPLOYEE = 'leademployee';
  163. const PROUTE_LEAD_NOTES = 'leadnotes';
  164. const PROUTE_ACTIVITY_EDIT = 'editactivityid';
  165. const PROUTE_ACTIVITY_NOTE = 'newactivitynote';
  166. /**
  167. * stigma lead/activity scopes here
  168. */
  169. const PHOTO_ACT_SCOPE = 'CRMACTIVITY';
  170. const STIGMA_LEAD_SOURCE = 'CRMSOURCE';
  171. const STIGMA_ACT_TYPE = 'CRMACTTYPE';
  172. const STIGMA_ACT_RESULT = 'CRMACTRESULT';
  173. const STIGMA_ACT_TARGET = 'CRMACTTARGET';
  174. const ADCOMM_ACT_SCOPE = 'ADCRMACTIVITY';
  175. /**
  176. * Creates new PseudoCRM instance
  177. */
  178. public function __construct() {
  179. $this->initMessages();
  180. $this->setMyLogin();
  181. $this->loadAlter();
  182. $this->setActivitiesStatesList();
  183. $this->setActivitiesCustomStates();
  184. $this->initLeadsDb();
  185. $this->initActivitiesDb();
  186. $this->loadEmployeeData();
  187. $this->loadUserData();
  188. $this->loadTariffs();
  189. $this->loadBranches();
  190. $this->loadLeads();
  191. $this->loadActivities();
  192. }
  193. /**
  194. * Inits system messages helper
  195. *
  196. * @return void
  197. */
  198. protected function initMessages() {
  199. $this->messages = new UbillingMessageHelper();
  200. }
  201. /**
  202. * Loads alter.ini config into protected property
  203. *
  204. * @global object $ubillingConfig
  205. *
  206. * @return void
  207. */
  208. protected function loadAlter() {
  209. global $ubillingConfig;
  210. $this->altCfg = $ubillingConfig->getAlter();
  211. $this->sendDogEnabled = $this->altCfg['SENDDOG_ENABLED'];
  212. $this->activityProtectedFlag = $this->altCfg['PSEUDOCRM_ACT_PROTECTED'];
  213. }
  214. /**
  215. * Sets current administrator username property
  216. *
  217. * @return void
  218. */
  219. protected function setMyLogin() {
  220. $this->myLogin = whoami();
  221. }
  222. /**
  223. * Sets available activities states list. May be configurable in future.
  224. *
  225. * @return void
  226. */
  227. protected function setActivitiesStatesList() {
  228. $this->activitiesStatesList = array(
  229. self::STIGMA_ACT_TYPE => __('Marketing type'),
  230. self::STIGMA_ACT_TARGET => __('Marketing target'),
  231. self::STIGMA_ACT_RESULT => __('Post-marketing status'),
  232. );
  233. }
  234. /**
  235. * Sets or overrides custom activities states list depends on config option
  236. *
  237. * @return void
  238. */
  239. protected function setActivitiesCustomStates() {
  240. if (isset($this->altCfg[self::OPT_ACT_CUSTSTATES])) {
  241. if (!empty($this->altCfg[self::OPT_ACT_CUSTSTATES])) {
  242. $rawList = explode(',', $this->altCfg[self::OPT_ACT_CUSTSTATES]);
  243. if (!empty($rawList)) {
  244. foreach ($rawList as $io => $each) {
  245. if (ispos($each, ':')) {
  246. $actStatesCustom = explode(':', $each);
  247. //at least two required sections available
  248. if (isset($actStatesCustom[0]) and isset($actStatesCustom[1])) {
  249. $customScope = strtoupper($actStatesCustom[0]);
  250. $customStateName = __($actStatesCustom[1]);
  251. $this->activitiesStatesList[$customScope] = $customStateName;
  252. }
  253. } else {
  254. show_error(__('Wrong element format') . ' `' . $each . '` IN ' . self::OPT_ACT_CUSTSTATES);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }
  261. /**
  262. * Loads all existing tariffs from database
  263. *
  264. * @return void
  265. */
  266. protected function loadTariffs() {
  267. $allTariffsTmp = zb_TariffsGetAll();
  268. if (!empty($allTariffsTmp)) {
  269. foreach ($allTariffsTmp as $io => $each) {
  270. $this->allTariffs[$each['name']] = __($each['name']);
  271. }
  272. }
  273. }
  274. /**
  275. * Preloads branches data, if its enabled
  276. *
  277. * @return void
  278. */
  279. protected function loadBranches() {
  280. if ($this->altCfg['BRANCHES_ENABLED']) {
  281. $this->branchesFlag = true;
  282. $branchesDb = new NyanORM('branches');
  283. $branchesDb->orderBy('id', 'DESC');
  284. $allBranchesTmp = $branchesDb->getAll();
  285. if (!empty($allBranchesTmp)) {
  286. foreach ($allBranchesTmp as $io => $each) {
  287. $this->allBranches[$each['id']] = $each['name'];
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * Preloads all existing employee data
  294. *
  295. * @return void
  296. */
  297. protected function loadEmployeeData() {
  298. $allEmployeeTmp = ts_GetAllEmployeeData();
  299. if (!empty($allEmployeeTmp)) {
  300. foreach ($allEmployeeTmp as $io => $each) {
  301. $this->allEmployee[$each['id']] = $each['name'];
  302. if (!empty($each['admlogin'])) {
  303. $this->allEmployeeLogins[$each['admlogin']] = $each['id'];
  304. }
  305. if ($each['active']) {
  306. $this->allActiveEmployee[$each['id']] = $each['name'];
  307. }
  308. if ($each['telegram']) {
  309. $this->allEmployeeChatIds[$each['id']] = $each['telegram'];
  310. }
  311. }
  312. }
  313. }
  314. /**
  315. * Loads all existing users data
  316. *
  317. * @return void
  318. */
  319. protected function loadUserData() {
  320. $this->allUserData = zb_UserGetAllDataCache();
  321. }
  322. /**
  323. * Inits leads database abstraction layer
  324. *
  325. * @return void
  326. */
  327. protected function initLeadsDb() {
  328. $this->leadsDb = new NyanORM(self::TABLE_LEADS);
  329. }
  330. /**
  331. * Loads existing leads into protected property
  332. *
  333. * @return void
  334. */
  335. protected function loadLeads() {
  336. $this->allLeads = $this->leadsDb->getAll('id');
  337. }
  338. /**
  339. * Inits activities database abstraction layer
  340. *
  341. * @return void
  342. */
  343. protected function initActivitiesDb() {
  344. $this->activitiesDb = new NyanORM(self::TABLE_ACTIVITIES);
  345. }
  346. /**
  347. * Loads existing leads into protected property
  348. *
  349. * @return void
  350. */
  351. protected function loadActivities() {
  352. $this->activitiesDb->orderBy('id', 'DESC');
  353. $this->allActivities = $this->activitiesDb->getAll('id');
  354. }
  355. /**
  356. * Renders existing leads list
  357. *
  358. * @return string
  359. */
  360. public function renderLeadsList() {
  361. $result = '';
  362. $columns = array('ID', 'Type', 'Full address', 'Real Name', 'Mobile', 'Worker', 'Notes', 'Actions');
  363. $url = self::URL_ME . '&' . self::ROUTE_LEADS_LIST_AJ . '=true';
  364. $customStyling = wf_tag('style');
  365. $customStyling .= file_get_contents('skins/pseudocrm.css');
  366. $customStyling .= wf_tag('style', true);
  367. $result .= $customStyling;
  368. $result .= wf_JqDtLoader($columns, $url, false, __('Leads'), 50, '"order": [[ 0, "desc" ]]');
  369. return ($result);
  370. }
  371. /**
  372. * Returns ajax data for existing leads list
  373. *
  374. * @return void
  375. */
  376. public function ajLeadsList() {
  377. $json = new wf_JqDtHelper();
  378. if (!empty($this->allLeads)) {
  379. foreach ($this->allLeads as $io => $each) {
  380. $leadType = (empty($each['login'])) ? __('Potential') : __('Existing');
  381. $leadProfileUrl = self::URL_ME . '&' . self::ROUTE_LEAD_PROFILE . '=' . $each['id'];
  382. $employeeName = (isset($this->allEmployee[$each['employeeid']])) ? $this->allEmployee[$each['employeeid']] : '';
  383. $data[] = $each['id'];
  384. $data[] = $leadType;
  385. $data[] = $each['address'];
  386. $data[] = $each['realname'];
  387. $data[] = $each['mobile'];
  388. $data[] = $employeeName;
  389. $data[] = $each['notes'];
  390. $actLinks = wf_Link($leadProfileUrl, web_edit_icon());
  391. $data[] = $actLinks;
  392. $json->addRow($data);
  393. unset($data);
  394. }
  395. }
  396. $json->getJson();
  397. }
  398. /**
  399. * Returns new lead creation form
  400. *
  401. * @return string
  402. */
  403. protected function renderLeadCreateForm() {
  404. $result = '';
  405. //previous, may be failed form submitted data
  406. $prevAddress = ubRouting::post(self::PROUTE_LEAD_ADDR);
  407. $prevName = ubRouting::post(self::PROUTE_LEAD_NAME);
  408. $prevMobile = ubRouting::post(self::PROUTE_LEAD_MOBILE);
  409. $prevExtMobile = ubRouting::post(self::PROUTE_LEAD_EXTMOBILE);
  410. $prevPhone = ubRouting::post(self::PROUTE_LEAD_PHONE);
  411. $prevEmail = ubRouting::post(self::PROUTE_LEAD_EMAIL);
  412. $prevBranch = ubRouting::post(self::PROUTE_LEAD_BRANCH);
  413. $prevTariff = ubRouting::post(self::PROUTE_LEAD_TARIFF);
  414. $prevLogin = ubRouting::post(self::PROUTE_LEAD_LOGIN);
  415. $prevEmployee = ubRouting::post(self::PROUTE_LEAD_EMPLOYEE);
  416. $prevNotes = ubRouting::post(self::PROUTE_LEAD_NOTES);
  417. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  418. $inputs = '';
  419. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_CREATE, 'true');
  420. $inputs .= wf_TextInput(self::PROUTE_LEAD_ADDR, __('Full address') . $sup, $prevAddress, true, '40', '');
  421. $inputs .= wf_TextInput(self::PROUTE_LEAD_NAME, __('Real Name') . $sup, $prevName, true, '40', '');
  422. $inputs .= wf_TextInput(self::PROUTE_LEAD_MOBILE, __('Mobile') . $sup, $prevMobile, true, '15', 'mobile');
  423. $inputs .= wf_TextInput(self::PROUTE_LEAD_EXTMOBILE, __('Additional mobile'), $prevExtMobile, true, '15', 'mobile');
  424. $inputs .= wf_TextInput(self::PROUTE_LEAD_PHONE, __('Phone'), $prevPhone, true, '15', 'mobile');
  425. $inputs .= wf_TextInput(self::PROUTE_LEAD_EMAIL, __('Email'), $prevEmail, true, '15', 'email');
  426. if ($this->branchesFlag) {
  427. $branchesParams = array('' => '-');
  428. $branchesParams += $this->allBranches;
  429. $inputs .= wf_Selector(self::PROUTE_LEAD_BRANCH, $branchesParams, __('Branch'), $prevBranch, true);
  430. } else {
  431. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_BRANCH, '0');
  432. }
  433. $tariffsParams = array('' => '-');
  434. $tariffsParams += $this->allTariffs;
  435. $inputs .= wf_Selector(self::PROUTE_LEAD_TARIFF, $tariffsParams, __('Tariff'), $prevTariff, true);
  436. $inputs .= wf_TextInput(self::PROUTE_LEAD_LOGIN, __('Login'), $prevLogin, true, '15', 'login');
  437. $employeeParams = array('' => '-');
  438. $employeeParams += $this->allActiveEmployee;
  439. $inputs .= wf_Selector(self::PROUTE_LEAD_EMPLOYEE, $employeeParams, __('Worker'), $prevEmployee, true);
  440. $inputs .= wf_TextInput(self::PROUTE_LEAD_NOTES, __('Notes') . $sup, $prevNotes, true, '40', '');
  441. $inputs .= wf_Submit(__('Create'));
  442. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  443. return ($result);
  444. }
  445. /**
  446. * Creates new lead in database
  447. *
  448. * @param string $address
  449. * @param string $realname
  450. * @param string $phone
  451. * @param string $mobile
  452. * @param string $extmobile
  453. * @param string $email
  454. * @param int $branch
  455. * @param string $tariff
  456. * @param string $login
  457. * @param int $employeeid
  458. * @param string $notes
  459. *
  460. * @return int
  461. */
  462. public function createLead($address, $realname, $phone, $mobile, $extmobile, $email, $branch, $tariff, $login, $employeeid, $notes) {
  463. $addressF = ubRouting::filters($address, 'mres');
  464. $realnameF = ubRouting::filters($realname, 'mres');
  465. $phoneF = ubRouting::filters($phone, 'mres');
  466. $mobileF = ubRouting::filters($mobile, 'mres');
  467. $extmobileF = ubRouting::filters($extmobile, 'mres');
  468. $emailF = ubRouting::filters($email, 'mres');
  469. $branchF = ubRouting::filters($branch, 'int');
  470. $tariffF = ubRouting::filters($tariff, 'mres');
  471. $loginF = ubRouting::filters($login, 'mres');
  472. $employeeidF = ubRouting::filters($employeeid, 'int');
  473. $notesF = ubRouting::filters($notes, 'mres');
  474. $this->leadsDb->data('address', $addressF);
  475. $this->leadsDb->data('realname', $realnameF);
  476. $this->leadsDb->data('phone', $phoneF);
  477. $this->leadsDb->data('mobile', $mobileF);
  478. $this->leadsDb->data('extmobile', $extmobileF);
  479. $this->leadsDb->data('email', $emailF);
  480. $this->leadsDb->data('branch', $branchF);
  481. $this->leadsDb->data('tariff', $tariffF);
  482. $this->leadsDb->data('login', $loginF);
  483. $this->leadsDb->data('employeeid', $employeeidF);
  484. $this->leadsDb->data('notes', $notesF);
  485. $this->leadsDb->create();
  486. $newId = $this->leadsDb->getLastId();
  487. log_register('CRM CREATE LEAD [' . $newId . ']');
  488. return ($newId);
  489. }
  490. /**
  491. * Returns existing lead data
  492. *
  493. * @param int $leadId
  494. *
  495. * @return array
  496. */
  497. public function getLeadData($leadId) {
  498. $result = array();
  499. if (isset($this->allLeads[$leadId])) {
  500. $result = $this->allLeads[$leadId];
  501. }
  502. return ($result);
  503. }
  504. /**
  505. * Checks is lead exist or not by its ID
  506. *
  507. * @param int $leadId
  508. *
  509. * @return bool
  510. */
  511. public function isLeadExists($leadId) {
  512. $result = false;
  513. if (isset($this->allLeads[$leadId])) {
  514. $result = true;
  515. }
  516. return ($result);
  517. }
  518. /**
  519. * Returns existing lead profile title
  520. *
  521. * @param int $leadId
  522. *
  523. * @return string
  524. */
  525. public function getLeadLabel($leadId) {
  526. $result = '';
  527. $leadData = $this->getLeadData($leadId);
  528. if (!empty($leadData)) {
  529. $result .= $leadData['address'] . ', ' . $leadData['realname'];
  530. }
  531. return ($result);
  532. }
  533. /**
  534. * Renders existing lead source controls
  535. *
  536. * @param int $leadId
  537. *
  538. * @return string
  539. */
  540. public function renderLeadSource($leadId) {
  541. $result = '';
  542. if ($this->isLeadExists($leadId)) {
  543. $leadSource = new Stigma(self::STIGMA_LEAD_SOURCE, $leadId);
  544. $readOnly = true;
  545. if (cfr(self::RIGHT_LEADS)) {
  546. $readOnly = false;
  547. $leadSource->stigmaController('CUSTOM:' . self::TABLE_STATES_LOG);
  548. }
  549. $result .= $leadSource->render($leadId, '54', $readOnly);
  550. }
  551. return ($result);
  552. }
  553. /**
  554. * Renders existing lead profile
  555. *
  556. * @param int $leadId
  557. *
  558. * @return string
  559. */
  560. public function renderLeadProfile($leadId) {
  561. $result = '';
  562. $leadId = ubRouting::filters($leadId, 'int');
  563. if ($this->isLeadExists($leadId)) {
  564. $leadData = $this->getLeadData($leadId);
  565. $rows = '';
  566. $cells = wf_TableCell(__('Type'), '30%', 'row2');
  567. $leadType = (empty($leadData['login'])) ? __('Potential') : __('Existing');
  568. $cells .= wf_TableCell($leadType);
  569. $rows .= wf_TableRow($cells, 'row3');
  570. $cells = wf_TableCell(__('Full address'), '30%', 'row2');
  571. $cells .= wf_TableCell($leadData['address']);
  572. $rows .= wf_TableRow($cells, 'row3');
  573. $cells = wf_TableCell(__('Real Name'), '30%', 'row2');
  574. $cells .= wf_TableCell($leadData['realname']);
  575. $rows .= wf_TableRow($cells, 'row3');
  576. $cells = wf_TableCell(__('Phone'), '30%', 'row2');
  577. $cells .= wf_TableCell($leadData['phone']);
  578. $rows .= wf_TableRow($cells, 'row3');
  579. $cells = wf_TableCell(__('Mobile'), '30%', 'row2');
  580. $cells .= wf_TableCell($leadData['mobile']);
  581. $rows .= wf_TableRow($cells, 'row3');
  582. $cells = wf_TableCell(__('Additional mobile'), '30%', 'row2');
  583. $cells .= wf_TableCell($leadData['extmobile']);
  584. $rows .= wf_TableRow($cells, 'row3');
  585. $cells = wf_TableCell(__('Email'), '30%', 'row2');
  586. $cells .= wf_TableCell($leadData['email']);
  587. $rows .= wf_TableRow($cells, 'row3');
  588. if ($this->branchesFlag) {
  589. $cells = wf_TableCell(__('Branch'), '30%', 'row2');
  590. $cells .= wf_TableCell(@$this->allBranches[$leadData['branch']]);
  591. $rows .= wf_TableRow($cells, 'row3');
  592. }
  593. $cells = wf_TableCell(__('Tariff'), '30%', 'row2');
  594. $cells .= wf_TableCell($leadData['tariff']);
  595. $rows .= wf_TableRow($cells, 'row3');
  596. $userLabel = '';
  597. if (!empty($leadData['login'])) {
  598. if (isset($this->allUserData[$leadData['login']])) {
  599. $userData = $this->allUserData[$leadData['login']];
  600. $userUrl = UserProfile::URL_PROFILE . $leadData['login'];
  601. $userLabel = wf_Link($userUrl, wf_img_sized('skins/icon_user.gif', '', 10) . ' ' . $userData['fulladress'] . ', ' . $userData['realname']);
  602. }
  603. }
  604. $cells = wf_TableCell(__('Login'), '30%', 'row2');
  605. $cells .= wf_TableCell($userLabel);
  606. $rows .= wf_TableRow($cells, 'row3');
  607. $cells = wf_TableCell(__('Worker'), '30%', 'row2');
  608. $cells .= wf_TableCell(@$this->allEmployee[$leadData['employeeid']]);
  609. $rows .= wf_TableRow($cells, 'row3');
  610. $cells = wf_TableCell(__('Notes'), '30%', 'row2');
  611. $cells .= wf_TableCell($leadData['notes']);
  612. $rows .= wf_TableRow($cells, 'row3');
  613. $result .= wf_TableBody($rows, '100%', 0);
  614. } else {
  615. $result .= $this->messages->getStyledMessage(__('Strange exception') . ': ' . __('Lead') . ' [' . $leadId . '] ' . __('Not exists'), 'error');
  616. $result .= wf_delimiter();
  617. $result .= wf_BackLink(self::URL_ME . '&' . self::ROUTE_LEADS_LIST . '=true');
  618. }
  619. return ($result);
  620. }
  621. /**
  622. * Returns existing lead editing form
  623. *
  624. * @param int $leadId
  625. *
  626. * @return string
  627. */
  628. protected function renderLeadEditForm($leadId) {
  629. $leadId = ubRouting::filters($leadId, 'int');
  630. $result = '';
  631. $leadData = $this->getLeadData($leadId);
  632. if (!empty($leadData)) {
  633. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  634. $inputs = '';
  635. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_SAVE, $leadId);
  636. $inputs .= wf_TextInput(self::PROUTE_LEAD_ADDR, __('Full address') . $sup, $leadData['address'], true, '40', '');
  637. $inputs .= wf_TextInput(self::PROUTE_LEAD_NAME, __('Real Name') . $sup, $leadData['realname'], true, '40', '');
  638. $inputs .= wf_TextInput(self::PROUTE_LEAD_MOBILE, __('Mobile') . $sup, $leadData['mobile'], true, '15', 'mobile');
  639. $inputs .= wf_TextInput(self::PROUTE_LEAD_EXTMOBILE, __('Additional mobile'), $leadData['extmobile'], true, '15', 'mobile');
  640. $inputs .= wf_TextInput(self::PROUTE_LEAD_PHONE, __('Phone'), $leadData['phone'], true, '15', 'mobile');
  641. $inputs .= wf_TextInput(self::PROUTE_LEAD_EMAIL, __('Email'), $leadData['email'], true, '15', 'email');
  642. if ($this->branchesFlag) {
  643. $branchesParams = array('' => '-');
  644. $branchesParams += $this->allBranches;
  645. $inputs .= wf_Selector(self::PROUTE_LEAD_BRANCH, $branchesParams, __('Branch'), $leadData['branch'], true);
  646. } else {
  647. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_BRANCH, $leadData['branch']);
  648. }
  649. $tariffsParams = array('' => '-');
  650. $tariffsParams += $this->allTariffs;
  651. $inputs .= wf_Selector(self::PROUTE_LEAD_TARIFF, $tariffsParams, __('Tariff'), $leadData['tariff'], true);
  652. $inputs .= wf_TextInput(self::PROUTE_LEAD_LOGIN, __('Login'), $leadData['login'], true, '15', 'login');
  653. $employeeParams = array('' => '-');
  654. $employeeParams += $this->allActiveEmployee;
  655. $inputs .= wf_Selector(self::PROUTE_LEAD_EMPLOYEE, $employeeParams, __('Worker'), $leadData['employeeid'], true);
  656. $inputs .= wf_TextInput(self::PROUTE_LEAD_NOTES, __('Notes') . $sup, $leadData['notes'], true, '40', '');
  657. $inputs .= wf_Submit(__('Save'));
  658. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  659. } else {
  660. $result .= $this->messages->getStyledMessage(__('Strange exception') . ': ' . __('Lead') . ' [' . $leadId . '] ' . __('Not exists'), 'error');
  661. }
  662. return ($result);
  663. }
  664. /**
  665. * Changes existing lead database record
  666. *
  667. * @param int $leadId
  668. * @param string $address
  669. * @param string $realname
  670. * @param string $phone
  671. * @param string $mobile
  672. * @param string $extmobile
  673. * @param string $email
  674. * @param int $branch
  675. * @param string $tariff
  676. * @param string $login
  677. * @param int $employeeid
  678. * @param string $notes
  679. *
  680. * @return int
  681. */
  682. public function saveLead($leadId, $address, $realname, $phone, $mobile, $extmobile, $email, $branch, $tariff, $login, $employeeid, $notes) {
  683. $leadId = ubRouting::filters($leadId, 'int');
  684. $addressF = ubRouting::filters($address, 'mres');
  685. $realnameF = ubRouting::filters($realname, 'mres');
  686. $phoneF = ubRouting::filters($phone, 'mres');
  687. $mobileF = ubRouting::filters($mobile, 'mres');
  688. $extmobileF = ubRouting::filters($extmobile, 'mres');
  689. $emailF = ubRouting::filters($email, 'mres');
  690. $branchF = ubRouting::filters($branch, 'int');
  691. $tariffF = ubRouting::filters($tariff, 'mres');
  692. $loginF = ubRouting::filters($login, 'mres');
  693. $employeeidF = ubRouting::filters($employeeid, 'int');
  694. $notesF = ubRouting::filters($notes, 'mres');
  695. $this->leadsDb->data('address', $addressF);
  696. $this->leadsDb->data('realname', $realnameF);
  697. $this->leadsDb->data('phone', $phoneF);
  698. $this->leadsDb->data('mobile', $mobileF);
  699. $this->leadsDb->data('extmobile', $extmobileF);
  700. $this->leadsDb->data('email', $emailF);
  701. $this->leadsDb->data('branch', $branchF);
  702. $this->leadsDb->data('tariff', $tariffF);
  703. $this->leadsDb->data('login', $loginF);
  704. $this->leadsDb->data('employeeid', $employeeidF);
  705. $this->leadsDb->data('notes', $notesF);
  706. $this->leadsDb->where('id', '=', $leadId);
  707. $this->leadsDb->save();
  708. log_register('CRM EDIT LEAD [' . $leadId . ']');
  709. return ($leadId);
  710. }
  711. /**
  712. * Renders new lead activity record creation dialog
  713. *
  714. * @param int $leadId
  715. *
  716. * @return string
  717. */
  718. protected function renderActivityCreateForm($leadId) {
  719. $result = '';
  720. if ($this->isLeadExists($leadId)) {
  721. $urlCreate = self::URL_ME . '&' . self::ROUTE_ACTIVITY_CREATE . '=' . $leadId;
  722. $urlCancel = self::URL_ME . '&' . self::ROUTE_LEAD_PROFILE . '=' . $leadId;
  723. $label = __('Are you realy want to create record for this lead') . '?';
  724. $result .= wf_ConfirmDialog($urlCreate, web_icon_create() . ' ' . __('Create new record'), $label, 'ubButton', $urlCancel, __('Create new record'));
  725. }
  726. return ($result);
  727. }
  728. /**
  729. * Creates new activity database record for existing lead
  730. *
  731. * @param int $leadId
  732. *
  733. * @return int/zero on error
  734. */
  735. public function createActivity($leadId) {
  736. $result = 0;
  737. $leadId = ubRouting::filters($leadId, 'int');
  738. if ($this->isLeadExists($leadId)) {
  739. $adminLogin = whoami();
  740. $currentEmployeeId = 0;
  741. if (isset($this->allEmployeeLogins[$adminLogin])) {
  742. $currentEmployeeId = $this->allEmployeeLogins[$adminLogin];
  743. }
  744. $this->activitiesDb->data('leadid', $leadId);
  745. $this->activitiesDb->data('date', curdatetime());
  746. $this->activitiesDb->data('admin', $adminLogin);
  747. $this->activitiesDb->data('employeeid', $currentEmployeeId);
  748. $this->activitiesDb->data('state', 0);
  749. $this->activitiesDb->data('notes', '');
  750. $this->activitiesDb->create();
  751. $newId = $this->activitiesDb->getLastId();
  752. $result = $newId;
  753. log_register('CRM CREATE ACTIVITY [' . $newId . '] FOR LEAD [' . $leadId . ']');
  754. }
  755. return ($result);
  756. }
  757. /**
  758. * Checks existence of activity by its ID
  759. *
  760. * @param int $activityId
  761. *
  762. * @return bool
  763. */
  764. public function isActivityExists($activityId) {
  765. $result = false;
  766. if (isset($this->allActivities[$activityId])) {
  767. $result = true;
  768. }
  769. return ($result);
  770. }
  771. /**
  772. * Returns existing activity record data
  773. *
  774. * @param int $activityId
  775. *
  776. * @return array
  777. */
  778. public function getActivityData($activityId) {
  779. $result = array();
  780. if (isset($this->allActivities[$activityId])) {
  781. $result = $this->allActivities[$activityId];
  782. }
  783. return ($result);
  784. }
  785. /**
  786. * Render existing activity states controllers
  787. *
  788. * @param int $activityId
  789. * @param int $size
  790. *
  791. * @return string
  792. */
  793. protected function renderActivityStatesController($activityId, $size = 128) {
  794. $activityId = ubRouting::filters($activityId, 'int');
  795. $result = '';
  796. $readOnly = cfr(self::RIGHT_ACTIVITIES) ? false : true;
  797. $activityData = $this->getActivityData($activityId);
  798. //preventing state changes on closed activities
  799. if ($activityData['state']) {
  800. $readOnly = true;
  801. }
  802. $stigmaInstances = array();
  803. if (!empty($this->activitiesStatesList)) {
  804. foreach ($this->activitiesStatesList as $eachScope => $eachTitle) {
  805. //creating some instances
  806. $stigmaInstances[$eachScope] = new Stigma($eachScope, $activityId);
  807. //render state here
  808. $result .= wf_tag('strong', false) . __($eachTitle) . wf_tag('strong', true) . wf_delimiter(0);
  809. if (cfr(self::RIGHT_ACTIVITIES)) {
  810. $stigmaInstances[$eachScope]->stigmaController('CUSTOM:' . self::TABLE_STATES_LOG);
  811. }
  812. $result .= $stigmaInstances[$eachScope]->render($activityId, $size, $readOnly);
  813. }
  814. }
  815. return ($result);
  816. }
  817. /**
  818. * Checks have user activity access rights to manage it open/closed states or not.
  819. *
  820. * @param int $activityId
  821. *
  822. * @return bool
  823. */
  824. protected function checkActivityAccess($activityId) {
  825. $result = false;
  826. //only if activity protection option enabled
  827. if ($this->activityProtectedFlag) {
  828. $activityId = ubRouting::filters($activityId, 'int');
  829. if (cfr(self::RIGHT_ACT_MANAGER)) {
  830. //user have total rights to manage all activities
  831. $result = true;
  832. } else {
  833. //checking some activity access
  834. if ($this->isActivityExists($activityId)) {
  835. $activityData = $this->getActivityData($activityId);
  836. $activityOwner = $activityData['admin'];
  837. if ($activityOwner == $this->myLogin) {
  838. //yep, thats is our activity!
  839. $result = true;
  840. }
  841. }
  842. }
  843. } else {
  844. $result = true;
  845. }
  846. return ($result);
  847. }
  848. /**
  849. * Sets existing activity database record as processed
  850. *
  851. * @param int $activityId
  852. *
  853. * @return void
  854. */
  855. public function setActivityDone($activityId) {
  856. $activityId = ubRouting::filters($activityId, 'int');
  857. if ($this->isActivityExists($activityId)) {
  858. if ($this->checkActivityAccess($activityId)) {
  859. $activityData = $this->getActivityData($activityId);
  860. $leadId = $activityData['leadid'];
  861. $this->activitiesDb->data('state', 1);
  862. $this->activitiesDb->where('id', '=', $activityId);
  863. $this->activitiesDb->save();
  864. log_register('CRM CLOSE ACTIVITY [' . $activityId . '] FOR LEAD [' . $leadId . ']');
  865. }
  866. }
  867. }
  868. /**
  869. * Sets existing activity database record as not processed
  870. *
  871. * @param int $activityId
  872. *
  873. * @return void
  874. */
  875. public function setActivityUndone($activityId) {
  876. $activityId = ubRouting::filters($activityId, 'int');
  877. if ($this->isActivityExists($activityId)) {
  878. if ($this->checkActivityAccess($activityId)) {
  879. $activityData = $this->getActivityData($activityId);
  880. $leadId = $activityData['leadid'];
  881. $this->activitiesDb->data('state', 0);
  882. $this->activitiesDb->where('id', '=', $activityId);
  883. $this->activitiesDb->save();
  884. log_register('CRM OPEN ACTIVITY [' . $activityId . '] FOR LEAD [' . $leadId . ']');
  885. }
  886. }
  887. }
  888. /**
  889. * Renders existing activity notes aka result editing form
  890. *
  891. * @param int $activityId
  892. *
  893. * @return string
  894. */
  895. protected function renderActivityResultEditForm($activityId) {
  896. $result = '';
  897. $activityId = ubRouting::filters($activityId, 'int');
  898. if ($this->isActivityExists($activityId)) {
  899. $activityData = $this->getActivityData($activityId);
  900. $currentNote = $activityData['notes'];
  901. $inputs = wf_HiddenInput(self::PROUTE_ACTIVITY_EDIT, $activityId);
  902. $inputs .= wf_TextInput(self::PROUTE_ACTIVITY_NOTE, __('Result'), $currentNote, false, 40) . ' ';
  903. $inputs .= wf_Submit(__('Save'));
  904. $result .= wf_Form('', 'POST', $inputs, '');
  905. }
  906. return ($result);
  907. }
  908. /**
  909. * Changes activity record notes aka result
  910. *
  911. * @param int $activityId
  912. * @param string $notes
  913. *
  914. * @return void
  915. */
  916. public function setActivityResult($activityId, $notes = '') {
  917. $activityId = ubRouting::filters($activityId, 'int');
  918. $notes = ubRouting::filters($notes, 'mres');
  919. if ($this->isActivityExists($activityId)) {
  920. $activityData = $this->getActivityData($activityId);
  921. $leadId = $activityData['leadid'];
  922. $this->activitiesDb->data('notes', $notes);
  923. $this->activitiesDb->where('id', '=', $activityId);
  924. $this->activitiesDb->save();
  925. log_register('CRM CHANGE ACTIVITY [' . $activityId . '] RESULT FOR LEAD [' . $leadId . ']');
  926. }
  927. }
  928. /**
  929. * Renders existing activity record profile with state controllers
  930. *
  931. * @param int $activityId
  932. *
  933. * @return string
  934. */
  935. public function renderActivityProfile($activityId) {
  936. $result = '';
  937. $activityId = ubRouting::filters($activityId, 'int');
  938. if ($this->isActivityExists($activityId)) {
  939. $activityData = $this->getActivityData($activityId);
  940. $leadId = $activityData['leadid'];
  941. $readOnly = cfr(self::RIGHT_ACTIVITIES) ? false : true;
  942. //preventing state changes on closed activities
  943. if ($activityData['state']) {
  944. $readOnly = true;
  945. }
  946. //appending lead profile here
  947. $result .= $this->renderLeadProfile($leadId);
  948. //and some controls
  949. $leadBackLink = wf_BackLink(self::URL_ME . '&' . self::ROUTE_LEAD_PROFILE . '=' . $leadId) . ' ';
  950. $activityControls = $leadBackLink;
  951. if (cfr(self::RIGHT_ACTIVITIES)) {
  952. if ($this->checkActivityAccess($activityId)) {
  953. if ($activityData['state']) {
  954. $actOpenUrl = self::URL_ME . '&' . self::ROUTE_ACTIVITY_PROFILE . '=' . $activityId . '&' . self::ROUTE_ACTIVITY_UNDONE . '=' . $activityId;
  955. $activityControls .= wf_Link($actOpenUrl, wf_img('skins/icon_unlock.png') . ' ' . __('Open'), false, 'ubButton') . ' ';
  956. } else {
  957. $actCloseUrl = self::URL_ME . '&' . self::ROUTE_ACTIVITY_PROFILE . '=' . $activityId . '&' . self::ROUTE_ACTIVITY_DONE . '=' . $activityId;
  958. $activityControls .= wf_Link($actCloseUrl, wf_img('skins/icon_lock.png') . ' ' . __('Close'), false, 'ubButton') . ' ';
  959. }
  960. }
  961. }
  962. $result .= $activityControls;
  963. //activity basic data
  964. $result .= wf_delimiter(0);
  965. $result .= wf_tag('div', false, 'dashtask');
  966. $result .= __('Date') . ': ' . $activityData['date'];
  967. $result .= wf_tag('div', true);
  968. $result .= wf_tag('div', false, 'dashtask');
  969. $result .= __('Worker') . ': ' . @$this->allEmployee[$activityData['employeeid']];
  970. $result .= wf_tag('div', true);
  971. $stateLabel = ($activityData['state']) ? __('Closed') : __('New');
  972. $result .= wf_tag('div', false, 'dashtask');
  973. $result .= __('Status') . ': ' . $stateLabel;
  974. $result .= wf_tag('div', true);
  975. //here result editing/display
  976. $result .= wf_tag('div', false, 'dashtask');
  977. if ($readOnly) {
  978. $result .= __('Result') . ': ' . $activityData['notes'];
  979. } else {
  980. $result .= $this->renderActivityResultEditForm($activityId);
  981. }
  982. $result .= wf_tag('div', true);
  983. $result .= wf_CleanDiv();
  984. //some state controllers here
  985. $result .= $this->renderActivityStatesController($activityId, 64);
  986. $result .= wf_delimiter(0);
  987. //photostorage here
  988. if ($this->altCfg['PHOTOSTORAGE_ENABLED']) {
  989. $photostorage = new PhotoStorage(self::PHOTO_ACT_SCOPE, $activityId);
  990. $photostorageUrl = $photostorage::MODULE_URL . '&scope=' . self::PHOTO_ACT_SCOPE . '&itemid=' . $activityId . '&mode=list';
  991. $result .= wf_Link($photostorageUrl, wf_img('skins/photostorage.png') . ' ' . __('Upload images'), false, 'ubButton');
  992. $result .= wf_delimiter();
  993. $result .= $photostorage->renderImagesRaw();
  994. }
  995. //few additional comments here
  996. if ($this->altCfg['ADCOMMENTS_ENABLED']) {
  997. //catching notification for lead assidned employee
  998. $this->catchADcommentNotification($leadId, $activityData);
  999. //rendering adcomments
  1000. $adComments = new ADcomments(self::ADCOMM_ACT_SCOPE);
  1001. $result .= wf_tag('strong', false) . __('Additional comments') . wf_tag('strong', true) . wf_delimiter(0);
  1002. $result .= $adComments->renderComments($activityId);
  1003. }
  1004. } else {
  1005. $result .= $this->messages->getStyledMessage(__('Strange exception') . ': ' . __('Activity record') . ' [' . $activityId . '] ' . __('Not exists'), 'error');
  1006. }
  1007. return ($result);
  1008. }
  1009. /**
  1010. * Catches and sends notification to telegram, on new additional comment creation to lead assigned employee
  1011. *
  1012. * @param int $leadId
  1013. * @param array $activityData
  1014. *
  1015. * @return void
  1016. */
  1017. protected function catchADcommentNotification($leadId = 0, $activityData = array()) {
  1018. if ($this->sendDogEnabled) {
  1019. //someone posting new additional comment
  1020. if (ubRouting::checkPost(ADcomments::PROUTE_NEW_TEXT)) {
  1021. $leadData = $this->getLeadData($leadId);
  1022. if ($leadData) {
  1023. $leadEmployeeId = $leadData['employeeid'];
  1024. if (isset($this->allEmployeeChatIds[$leadEmployeeId])) {
  1025. $employeeChatId = $this->allEmployeeChatIds[$leadEmployeeId];
  1026. if ($employeeChatId) {
  1027. $telegram = new UbillingTelegram();
  1028. $billingUrl = ($this->altCfg['FULL_BILLING_URL']) ? $this->altCfg['FULL_BILLING_URL'] : '';
  1029. $message = __('New comment on lead') . ' ' . $this->getLeadLabel($leadId) . ' ';
  1030. $message .= __('for activity') . ' #' . $activityData['id'] . ' ' . __('from') . ' ' . $activityData['date'] . '. ';
  1031. $commentTextPreview = zb_cutString(ubRouting::post(ADcomments::PROUTE_NEW_TEXT), 40);
  1032. $message .= __('Comment') . ': "' . $commentTextPreview . '". ';
  1033. if ($billingUrl) {
  1034. $activityUrl = $billingUrl . self::URL_ME . '&' . self::ROUTE_ACTIVITY_PROFILE . '=' . $activityData['id'];
  1035. $message .= wf_Link($activityUrl, __('Show'));
  1036. }
  1037. $message .= 'parseMode:{html}';
  1038. $telegram->sendMessage($employeeChatId, $message, false, 'PSEUDOCRM');
  1039. }
  1040. }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. /**
  1046. * Sends Telegram notification about open activities to activity employee
  1047. *
  1048. * @return void
  1049. */
  1050. public function notifyOpenActivities() {
  1051. if ($this->sendDogEnabled) {
  1052. $telegram = new UbillingTelegram();
  1053. $billingUrl = ($this->altCfg['FULL_BILLING_URL']) ? $this->altCfg['FULL_BILLING_URL'] : '';
  1054. $activityBaseUrl = $billingUrl . self::URL_ME . '&' . self::ROUTE_ACTIVITY_PROFILE . '=';
  1055. $sendingQueue = array(); //employeeId=>activitiesList
  1056. $eol = '\r\n';
  1057. if (!empty($this->allActivities)) {
  1058. foreach ($this->allActivities as $io => $each) {
  1059. //activity open?
  1060. if ($each['state'] == 0) {
  1061. $activityEmployeeId = $each['employeeid'];
  1062. $activityLink = '';
  1063. $activityLink = ' #' . $each['id'] . ' ' . __('from') . ' ' . $each['date'] . '. ';
  1064. if ($billingUrl) {
  1065. $activityLink .= wf_Link($activityBaseUrl . $each['id'], __('Show'));
  1066. }
  1067. if (isset($sendingQueue[$activityEmployeeId])) {
  1068. $sendingQueue[$activityEmployeeId] .= $activityLink . $eol;
  1069. } else {
  1070. $sendingQueue[$activityEmployeeId] = $activityLink . $eol;
  1071. }
  1072. }
  1073. }
  1074. if (!empty($sendingQueue)) {
  1075. foreach ($sendingQueue as $eachEmployeeId => $eachMessages) {
  1076. if (isset($this->allEmployeeChatIds[$eachEmployeeId])) {
  1077. $employeeChatId = $this->allEmployeeChatIds[$eachEmployeeId];
  1078. if (!empty($eachMessages)) {
  1079. $message = __('The following activities are open for you') . ':' . $eol;
  1080. $message .= $eachMessages;
  1081. $message .= ' parseMode:{html}';
  1082. $telegram->sendMessage($employeeChatId, $message, false, 'PSEUDOCRM');
  1083. }
  1084. }
  1085. }
  1086. }
  1087. }
  1088. }
  1089. }
  1090. /**
  1091. * Returns array of all lead previous activity records
  1092. *
  1093. * @param int $leadId
  1094. *
  1095. * @return array
  1096. */
  1097. protected function getLeadActivities($leadId) {
  1098. $result = array();
  1099. if (!empty($this->allActivities)) {
  1100. foreach ($this->allActivities as $activityId => $eachActivityData) {
  1101. if ($eachActivityData['leadid'] == $leadId) {
  1102. $result[$activityId] = $eachActivityData;
  1103. }
  1104. }
  1105. }
  1106. return ($result);
  1107. }
  1108. /**
  1109. * Renders previous lead activities list
  1110. *
  1111. * @param int $leadId
  1112. * @param bool $onlyLast
  1113. *
  1114. * @return string
  1115. */
  1116. public function renderLeadActivitiesList($leadId, $onlyLast = false) {
  1117. $result = '';
  1118. $previousActivities = $this->getLeadActivities($leadId);
  1119. if (!empty($previousActivities)) {
  1120. if ($onlyLast) {
  1121. $previousActivities = array_slice($previousActivities, 0, 1, true);
  1122. }
  1123. //performing stigma instances creation
  1124. $stigmaInstances = array();
  1125. if (!empty($this->activitiesStatesList)) {
  1126. foreach ($this->activitiesStatesList as $eachScope => $eachTitle) {
  1127. //creating some instances
  1128. $stigmaInstances[$eachScope] = new Stigma($eachScope);
  1129. }
  1130. }
  1131. $result .= wf_CleanDiv();
  1132. foreach ($previousActivities as $activityId => $activityData) {
  1133. $activityUrl = self::URL_ME . '&' . self::ROUTE_ACTIVITY_PROFILE . '=' . $activityId;
  1134. $activityClass = ($activityData['state']) ? 'donetask' : 'undone';
  1135. $employeeLabel = $activityData['admin'];
  1136. if (isset($this->allEmployeeLogins[$activityData['admin']])) {
  1137. $employeeId = $this->allEmployeeLogins[$activityData['admin']];
  1138. $employeeLabel = $this->allEmployee[$employeeId];
  1139. }
  1140. $activityLabel = web_edit_icon() . ' ' . $activityData['date'] . ' - ' . $employeeLabel;
  1141. //getting and appending each activity states
  1142. if (!empty($stigmaInstances)) {
  1143. foreach ($stigmaInstances as $eachScope => $eachStigma) {
  1144. $activityLabel .= ' ' . $stigmaInstances[$eachScope]->textRender($activityId, ' ', 16);
  1145. }
  1146. }
  1147. //appending comment as result if not empty
  1148. if (!empty($activityData['notes'])) {
  1149. $activityLabel .= ', ' . $activityData['notes'];
  1150. } else {
  1151. $activityLabel .= ', ' . __('No result');
  1152. }
  1153. $result .= wf_tag('div', false, $activityClass, 'style="padding: 10px; margin: 10px;"');
  1154. $result .= wf_Link($activityUrl, $activityLabel, false, '', 'style="color: #FFFFFF;"');
  1155. $result .= wf_tag('div', true);
  1156. }
  1157. } else {
  1158. if (!$onlyLast) {
  1159. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  1160. }
  1161. }
  1162. return ($result);
  1163. }
  1164. /**
  1165. * Returns lead task creation form
  1166. *
  1167. * @param int $leadId
  1168. *
  1169. * @return string
  1170. */
  1171. public function renderLeadTaskCreateForm($leadId) {
  1172. $result = '';
  1173. $leadId = ubRouting::filters($leadId, 'int');
  1174. if ($this->isLeadExists($leadId)) {
  1175. $leadData = $this->getLeadData($leadId);
  1176. $taskAddress = $leadData['address'];
  1177. $taskMobile = $leadData['mobile'];
  1178. $taskPhone = $leadData['phone'];
  1179. $taskExtMobile = $leadData['extmobile'];
  1180. if (!empty($taskExtMobile)) {
  1181. $taskMobile .= ' ' . $taskExtMobile;
  1182. }
  1183. $taskLogin = $leadData['login'];
  1184. $taskForm = ts_TaskCreateFormUnified($taskAddress, $taskMobile, $taskPhone, $taskLogin);
  1185. $result .= wf_modal(wf_img('skins/createtask.gif') . ' ' . __('Create task'), __('Create task'), $taskForm, 'ubButton', '450', '540');
  1186. }
  1187. return ($result);
  1188. }
  1189. /**
  1190. * Returns sticky note creation form
  1191. *
  1192. * @return string
  1193. */
  1194. protected function renderStickyCreateForm($textPreset) {
  1195. $inputs = wf_tag('label') . __('Text') . ': ' . wf_tag('br') . wf_tag('label', true);
  1196. $inputs .= wf_TextArea('newtext', '', $textPreset, true, '50x15');
  1197. $inputs .= wf_CheckInput('newactive', __('Create note as active'), true, true);
  1198. $inputs .= wf_DatePickerPreset('newreminddate', '');
  1199. $inputs .= wf_tag('label') . __('Remind only after this date') . wf_tag('label', true);
  1200. $inputs .= wf_tag('br');
  1201. $inputs .= wf_TimePickerPreset('newremindtime', '', __('Remind time'), false);
  1202. $inputs .= wf_tag('br');
  1203. $inputs .= wf_tag('br');
  1204. $inputs .= wf_Submit(__('Create'));
  1205. $result = wf_Form(StickyNotes::URL_ME, 'POST', $inputs, 'glamour');
  1206. return ($result);
  1207. }
  1208. /**
  1209. * Returns lead sticky note creation form control
  1210. *
  1211. * @param int $leadId
  1212. *
  1213. * @return string
  1214. */
  1215. protected function renderLeadStickyControl($leadId) {
  1216. $result = '';
  1217. $leadId = ubRouting::filters($leadId, 'int');
  1218. if ($this->isLeadExists($leadId)) {
  1219. $leadData = $this->getLeadData($leadId);
  1220. $textPreset = '';
  1221. $textPreset .= __('Lead') . ': ' . $leadData['address'] . PHP_EOL;
  1222. $textPreset .= __('Real Name') . ': ' . $leadData['realname'] . PHP_EOL;
  1223. if (!empty($leadData['mobile'])) {
  1224. $textPreset .= __('Mobile') . ': ' . $leadData['mobile'] . PHP_EOL;
  1225. }
  1226. if (!empty($leadData['phone'])) {
  1227. $textPreset .= __('Phone') . ': ' . $leadData['phone'] . PHP_EOL;
  1228. }
  1229. if (!empty($leadData['extmobile'])) {
  1230. $textPreset .= __('Additional mobile') . ': ' . $leadData['extmobile'] . PHP_EOL;
  1231. }
  1232. $textPreset .= '======' . PHP_EOL;
  1233. $stickyForm = $this->renderStickyCreateForm($textPreset);
  1234. $result .= wf_modalAuto(wf_img('skins/pushpin.png') . ' ' . __('Create new personal note'), __('Create new personal note'), $stickyForm, 'ubButton');
  1235. }
  1236. return ($result);
  1237. }
  1238. /**
  1239. * Searches lead Id by assigned login, returns 0 if not found.
  1240. *
  1241. * @param string $userLogin
  1242. *
  1243. * @return int
  1244. */
  1245. public function searchLeadByLogin($userLogin) {
  1246. $result = 0;
  1247. if (!empty($this->allLeads)) {
  1248. foreach ($this->allLeads as $io => $eachLead) {
  1249. if ($eachLead['login'] == $userLogin) {
  1250. $result = $eachLead['id'];
  1251. break;
  1252. }
  1253. }
  1254. }
  1255. return ($result);
  1256. }
  1257. /**
  1258. * Renders leads sources basic report
  1259. *
  1260. * @return string
  1261. */
  1262. public function renderReportLeadSources() {
  1263. $result = '';
  1264. $sources = new Stigma(self::STIGMA_LEAD_SOURCE);
  1265. $result .= $sources->renderBasicReport();
  1266. return ($result);
  1267. }
  1268. /**
  1269. * Renders states report
  1270. *
  1271. * @return string
  1272. */
  1273. public function renderReportStatesLog() {
  1274. $result = '';
  1275. $columns = array('Date', 'Worker', 'Status', 'Activity record', 'Event', 'Value');
  1276. $opts = ' "order": [[ 0, "desc" ]]';
  1277. $ajaxUrl = self::URL_ME . '&' . self::ROUTE_REPORT_STATESLOG_AJ . '=true';
  1278. $result .= wf_JqDtLoader($columns, $ajaxUrl, false, __('Events'), 100, $opts);
  1279. return ($result);
  1280. }
  1281. /**
  1282. * Renders states log report ajax data
  1283. *
  1284. * @return void
  1285. */
  1286. public function ajStatesLog() {
  1287. $json = new wf_JqDtHelper();
  1288. $statesLogDb = new NyanORM(self::TABLE_STATES_LOG);
  1289. $statesLogDb->where('scope', '!=', self::STIGMA_LEAD_SOURCE);
  1290. $statesLogDb->orderBy('id', 'DESC');
  1291. $allStatesLog = $statesLogDb->getAll();
  1292. if (!empty($allStatesLog)) {
  1293. //preloading stigma isnstances
  1294. $stigmaInstances = array();
  1295. $allStatesNames = array();
  1296. $allStatesIcons = array();
  1297. if (!empty($this->activitiesStatesList)) {
  1298. foreach ($this->activitiesStatesList as $eachScope => $scopeName) {
  1299. $stigmaInstances[$eachScope] = new Stigma($eachScope);
  1300. $allStatesNames[$eachScope] = $stigmaInstances[$eachScope]->getAllStates();
  1301. if (!empty($allStatesNames[$eachScope])) {
  1302. foreach ($allStatesNames[$eachScope] as $eachStateId => $eachStateName) {
  1303. $allStatesIcons[$eachScope][$eachStateId] = $stigmaInstances[$eachScope]->getStateIcon($eachStateId);
  1304. }
  1305. }
  1306. }
  1307. }
  1308. foreach ($allStatesLog as $io => $each) {
  1309. $data[] = $each['date'];
  1310. $adminLabel = (isset($this->allEmployeeLogins[$each['admin']])) ? $this->allEmployee[$this->allEmployeeLogins[$each['admin']]] : $each['admin'];
  1311. $data[] = $adminLabel;
  1312. $scopeName = (isset($this->activitiesStatesList[$each['scope']])) ? __($this->activitiesStatesList[$each['scope']]) : __('Unknown');
  1313. $data[] = $scopeName;
  1314. $actityLink = wf_Link(self::URL_ME . '&' . self::ROUTE_ACTIVITY_PROFILE . '=' . $each['itemid'], $each['itemid']);
  1315. $data[] = $actityLink;
  1316. $data[] = __($each['action']);
  1317. $stateName = $each['state'];
  1318. $stateIcon = '';
  1319. $stateIconCode = '';
  1320. if (isset($allStatesNames[$each['scope']])) {
  1321. if (isset($allStatesNames[$each['scope']][$stateName])) {
  1322. $stateName = $allStatesNames[$each['scope']][$stateName];
  1323. $stateIcon = $allStatesIcons[$each['scope']][$each['state']];
  1324. if (!empty($stateIcon)) {
  1325. $stateIconCode = wf_img_sized($stateIcon, '', 10) . ' ';
  1326. }
  1327. }
  1328. }
  1329. $data[] = $stateIconCode . __($stateName);
  1330. $json->addRow($data);
  1331. unset($data);
  1332. }
  1333. }
  1334. $json->getJson();
  1335. }
  1336. /**
  1337. * Renders lead assign form
  1338. *
  1339. * @param string $login
  1340. *
  1341. * @return string
  1342. */
  1343. public function renderLeadAssignForm($login) {
  1344. $result = '';
  1345. if (isset($this->allUserData[$login])) {
  1346. $inputs = wf_HiddenInput(self::PROUTE_LEAD_ASSIGN, $login);
  1347. $availableLeadsParams = array('' => '-');
  1348. if (!empty($this->allLeads)) {
  1349. foreach ($this->allLeads as $io => $each) {
  1350. //lead have no user assigned yet
  1351. if (empty($each['login'])) {
  1352. $availableLeadsParams[$each['id']] = $each['address'] . ' ' . $each['realname'];
  1353. }
  1354. }
  1355. }
  1356. $inputs .= wf_SelectorSearchable(self::PROUTE_LEAD_ASSIGN_ID, $availableLeadsParams, __('Lead'), '', false);
  1357. $inputs .= wf_Submit(__('Assign'));
  1358. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1359. } else {
  1360. $result .= $this->messages->getStyledMessage(__('Strange exception') . ': ' . __('User not exists'), 'error');
  1361. }
  1362. return ($result);
  1363. }
  1364. /**
  1365. * Renders lead creation form
  1366. *
  1367. * @param string $login
  1368. *
  1369. * @return string
  1370. */
  1371. public function renderUserLeadCreationForm($login) {
  1372. $result = '';
  1373. if (isset($this->allUserData[$login])) {
  1374. $userData = $this->allUserData[$login];
  1375. //some prefilled user data here
  1376. $prevAddress = $userData['fulladress'];
  1377. $prevName = $userData['realname'];
  1378. $prevMobile = $userData['mobile'];
  1379. $prevExtMobile = '';
  1380. if ($this->altCfg['MOBILES_EXT']) {
  1381. $extMobiles = new MobilesExt();
  1382. $userAdditionalMobiles = $extMobiles->getUserMobiles($login);
  1383. if (!empty($userAdditionalMobiles)) {
  1384. foreach ($userAdditionalMobiles as $io => $each) {
  1385. $prevExtMobile = $each['mobile'];
  1386. }
  1387. }
  1388. }
  1389. $prevPhone = $userData['phone'];
  1390. $prevEmail = $userData['email'];
  1391. $prevBranch = 0;
  1392. if ($this->altCfg['BRANCHES_ENABLED']) {
  1393. global $branchControl;
  1394. $prevBranch = $branchControl->userGetBranch($login);
  1395. }
  1396. $prevTariff = $userData['Tariff'];
  1397. $prevLogin = $login;
  1398. $prevEmployee = '';
  1399. $curAdmLogin = whoami();
  1400. if (isset($this->allEmployeeLogins[$curAdmLogin])) {
  1401. $prevEmployee = $this->allEmployeeLogins[$curAdmLogin];
  1402. }
  1403. $prevNotes = '';
  1404. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  1405. $inputs = '';
  1406. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_CREATE, 'true');
  1407. $inputs .= wf_TextInput(self::PROUTE_LEAD_ADDR, __('Full address') . $sup, $prevAddress, true, '40', '');
  1408. $inputs .= wf_TextInput(self::PROUTE_LEAD_NAME, __('Real Name') . $sup, $prevName, true, '40', '');
  1409. $inputs .= wf_TextInput(self::PROUTE_LEAD_MOBILE, __('Mobile') . $sup, $prevMobile, true, '15', 'mobile');
  1410. $inputs .= wf_TextInput(self::PROUTE_LEAD_EXTMOBILE, __('Additional mobile'), $prevExtMobile, true, '15', 'mobile');
  1411. $inputs .= wf_TextInput(self::PROUTE_LEAD_PHONE, __('Phone'), $prevPhone, true, '15', 'mobile');
  1412. $inputs .= wf_TextInput(self::PROUTE_LEAD_EMAIL, __('Email'), $prevEmail, true, '15', 'email');
  1413. if ($this->branchesFlag) {
  1414. $branchesParams = array('' => '-');
  1415. $branchesParams += $this->allBranches;
  1416. $inputs .= wf_Selector(self::PROUTE_LEAD_BRANCH, $branchesParams, __('Branch'), $prevBranch, true);
  1417. } else {
  1418. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_BRANCH, '0');
  1419. }
  1420. $tariffsParams = array('' => '-');
  1421. $tariffsParams += $this->allTariffs;
  1422. $inputs .= wf_Selector(self::PROUTE_LEAD_TARIFF, $tariffsParams, __('Tariff'), $prevTariff, true);
  1423. $inputs .= wf_HiddenInput(self::PROUTE_LEAD_LOGIN, $prevLogin);
  1424. $employeeParams = array('' => '-');
  1425. $employeeParams += $this->allActiveEmployee;
  1426. $inputs .= wf_Selector(self::PROUTE_LEAD_EMPLOYEE, $employeeParams, __('Worker'), $prevEmployee, true);
  1427. $inputs .= wf_TextInput(self::PROUTE_LEAD_NOTES, __('Notes') . $sup, $prevNotes, true, '40', '');
  1428. $inputs .= wf_delimiter(0);
  1429. $inputs .= wf_Submit(__('Create new lead'));
  1430. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  1431. } else {
  1432. $result .= $this->messages->getStyledMessage(__('Strange exception') . ': ' . __('User not exists'), 'error');
  1433. }
  1434. return ($result);
  1435. }
  1436. /**
  1437. * Assigns some login to existing lead
  1438. *
  1439. * @param int $leadId
  1440. * @param string $login
  1441. *
  1442. * @return void/string on error
  1443. */
  1444. public function setLeadLogin($leadId, $login) {
  1445. $result = '';
  1446. $leadId = ubRouting::filters($leadId, 'int');
  1447. $loginF = ubRouting::filters($login, 'mres');
  1448. if ($this->isLeadExists($leadId)) {
  1449. if (isset($this->allUserData[$loginF])) {
  1450. $this->leadsDb->data('login', $loginF);
  1451. $this->leadsDb->where('id', '=', $leadId);
  1452. $this->leadsDb->save();
  1453. log_register('CRM LEAD [' . $leadId . '] ASSIGN (' . $login . ')');
  1454. } else {
  1455. $result .= __('Strange exception') . ': ' . __('User not exists') . ' (' . $login . ')';
  1456. }
  1457. } else {
  1458. $result .= __('Strange exception') . ': ' . __('Lead') . ' [' . $leadId . '] ' . __('Not exists');
  1459. }
  1460. return ($result);
  1461. }
  1462. /**
  1463. * Renders primary module controls
  1464. *
  1465. * @return string
  1466. */
  1467. public function renderPanel() {
  1468. $result = '';
  1469. if (ubRouting::checkGet(self::ROUTE_LEAD_PROFILE)) {
  1470. $leadId = ubRouting::get(self::ROUTE_LEAD_PROFILE, 'int');
  1471. $leadData = $this->getLeadData($leadId);
  1472. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_LEADS_LIST . '=true', wf_img('skins/ukv/users.png') . ' ' . __('Existing leads'), false, 'ubButton') . ' ';
  1473. if (cfr(self::RIGHT_LEADS)) {
  1474. $result .= wf_modalAuto(web_edit_icon() . ' ' . __('Edit lead'), __('Edit lead'), $this->renderLeadEditForm($leadId), 'ubButton');
  1475. }
  1476. if (cfr(self::RIGHT_ACTIVITIES)) {
  1477. $result .= $this->renderActivityCreateForm($leadId);
  1478. }
  1479. if (!empty($leadData)) {
  1480. if ($leadData['login']) {
  1481. $result .= wf_Link(UserProfile::URL_PROFILE . $leadData['login'], web_profile_icon() . ' ' . __('User profile'), false, 'ubButton') . ' ';
  1482. }
  1483. }
  1484. if (cfr(self::RIGHT_TASKS)) {
  1485. $result .= $this->renderLeadTaskCreateForm($leadId);
  1486. }
  1487. if (cfr('STICKYNOTES')) {
  1488. if ($this->altCfg['STICKY_NOTES_ENABLED']) {
  1489. $result .= $this->renderLeadStickyControl($leadId);
  1490. }
  1491. }
  1492. }
  1493. if (ubRouting::checkGet(self::ROUTE_LEADS_LIST)) {
  1494. if (cfr(self::RIGHT_LEADS)) {
  1495. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Create new lead'), __('Create new lead'), $this->renderLeadCreateForm(), 'ubButton') . ' ';
  1496. }
  1497. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_REPORT_SOURCES . '=true', wf_img('skins/icon_funnel16.png') . ' ' . __('Leads sources'), false, 'ubButton') . ' ';
  1498. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_REPORT_STATESLOG . '=true', wf_img('skins/icon_note.gif') . ' ' . __('States log'), false, 'ubButton') . ' ';
  1499. }
  1500. if (ubRouting::checkGet(self::ROUTE_REPORT_SOURCES)) {
  1501. $result .= wf_BackLink(self::URL_ME . '&' . self::ROUTE_LEADS_LIST . '=true') . ' ';
  1502. }
  1503. if (ubRouting::checkGet(self::ROUTE_REPORT_STATESLOG)) {
  1504. $result .= wf_BackLink(self::URL_ME . '&' . self::ROUTE_LEADS_LIST . '=true') . ' ';
  1505. }
  1506. if (ubRouting::checkGet(self::ROUTE_ACTIVITY_PROFILE)) {
  1507. // ????
  1508. }
  1509. return ($result);
  1510. }
  1511. }