api.gres.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. <?php
  2. /**
  3. * Some goose doin some resistance
  4. */
  5. class GRes {
  6. /**
  7. * Contains system alter.ini config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains all available userdata as login=>userData
  14. *
  15. * @var array
  16. */
  17. protected $allUserData = array();
  18. /**
  19. * Contains all available agents data as id=>agentData
  20. *
  21. * @var array
  22. */
  23. protected $allAgents = array();
  24. /**
  25. * Contains all available agents extended data as agentId=>[extDataArr]
  26. *
  27. * @var array
  28. */
  29. protected $allAgentsExtInfo = array();
  30. /**
  31. * Contains agents extended data for strategy as agentExtId=>[extDataArr]
  32. *
  33. * @var array
  34. */
  35. protected $stategyAgentsExtInfo = array();
  36. /**
  37. * Contains preprocessed all agents data as agentId=>[data]+[exinfo]+[split]
  38. *
  39. * @var array
  40. */
  41. protected $fullAgentData = array();
  42. /**
  43. * Contains all existing agents names as id=>name
  44. *
  45. * @var array
  46. */
  47. protected $allAgentNames = array();
  48. /**
  49. * Contains all address based assigns as agentId=>street
  50. *
  51. * @var array
  52. */
  53. protected $allAssigns = array();
  54. /**
  55. * Contains all available strict assigns as login=>agentId
  56. *
  57. * @var array
  58. */
  59. protected $allAssignsStrict = array();
  60. /**
  61. * System messages helper object placeholder
  62. *
  63. * @var object
  64. */
  65. protected $messages = '';
  66. /**
  67. * Avarice instance placeholder
  68. *
  69. * @var object
  70. */
  71. protected $avarice = '';
  72. /**
  73. * Strategy database abstraction layer
  74. *
  75. * @var object
  76. */
  77. protected $strategyDb = '';
  78. /**
  79. * Strategy specs database abstraction layer
  80. *
  81. * @var object
  82. */
  83. protected $specsDb = '';
  84. /**
  85. * Contains existing strategies full data as stratId=>data/specs
  86. *
  87. * @var array
  88. */
  89. protected $allStrategies = array();
  90. /**
  91. * Contains all existing strat specs as id=>data
  92. *
  93. * @var array
  94. */
  95. protected $allSpecs = array();
  96. /**
  97. * Contains all strategies with enabled use assigns flag and theis primary agents as stratId=>agentId
  98. *
  99. * @var array
  100. */
  101. protected $allPrimaryAgents = array();
  102. /**
  103. * Contains available strategy spec types
  104. *
  105. * @var array
  106. */
  107. protected $specTypes = array();
  108. /**
  109. * Contains money ammount to apply strategy
  110. *
  111. * @var float
  112. */
  113. protected $amount = 0;
  114. /**
  115. * Contains current instance user login
  116. *
  117. * @var string
  118. */
  119. protected $userLogin = '';
  120. /**
  121. * Contains current instance user paymentId
  122. *
  123. * @var string
  124. */
  125. protected $userPaymentId = '';
  126. /**
  127. * OpenPayz object placeholder
  128. *
  129. * @var object
  130. */
  131. protected $openPayz = '';
  132. /**
  133. * Contains all PaymentIds as paymentId=>userLogin
  134. *
  135. * @var array
  136. */
  137. protected $allPaymentIds = array();
  138. /**
  139. * Contains all users paymentIds as userLogin=>paymentId
  140. *
  141. * @var array
  142. */
  143. protected $allCustomerPaymentIds = array();
  144. /**
  145. * Contains current instance runtime which will be returned with every reply
  146. *
  147. * @var string
  148. */
  149. protected $runtime = '';
  150. /**
  151. * Contains all available tariffs data
  152. *
  153. * @var array
  154. */
  155. protected $allTariffs = array();
  156. /**
  157. * Contains all tariff names as tariffname=>tariffname
  158. *
  159. * @var array
  160. */
  161. protected $allTariffNames = array();
  162. /**
  163. * Contains maximum valid amount for payment depend on selected strategy
  164. *
  165. * @var float
  166. */
  167. protected $maxAmount = 0;
  168. /**
  169. * Contains minimum valid amount for payment depend on selected strategy
  170. *
  171. * @var float
  172. */
  173. protected $minAmount = 0;
  174. /**
  175. * Contains payment validity flag depends if current amount>=minAmount
  176. *
  177. * @var bool
  178. */
  179. protected $paymentIsValid = false;
  180. /**
  181. * Contains splitted values percent precision to avoid 1 cent diff
  182. *
  183. * @var int
  184. */
  185. protected $splitPercentPrecision = 0;
  186. /**
  187. * some predefined stuff here
  188. */
  189. const TABLE_STRATEGY = 'gr_strat';
  190. const TABLE_SPECS = 'gr_spec';
  191. const URL_ME = '?module=gooseresistance';
  192. const ROUTE_ST_DELETE = 'deletestrategyid';
  193. const PROUTE_ST_CREATE = 'createnewstrategy';
  194. const PROUTE_ST_EDIT = 'editstrategyid';
  195. const PROUTE_ST_NAME = 'strategyname';
  196. const PROUTE_ST_ASSIGNS = 'strategyassignsflag';
  197. const PROUTE_ST_AGENTID = 'strategyprimaryagentid';
  198. const PROUTE_ST_TARIFF = 'strategytariffname';
  199. const ROUTE_SP_DELETE = 'deletespecid';
  200. const ROUTE_SP_EDIT = 'editstrateryspecs';
  201. const ROUTE_SP_CUSTDATA = 'speccustdataedit';
  202. const PROUTE_SP_CREATE = 'createnewspec';
  203. const PROUTE_SP_EDIT = 'editspecid';
  204. const PROUTE_SP_STRAT = 'specstratid';
  205. const PROUTE_SP_AGENT = 'specagentid';
  206. const PROUTE_SP_TYPE = 'spectype';
  207. const PROUTE_SP_VALUE = 'specvalue';
  208. const ROUTE_CD_DELKEY = 'delcustdatakey';
  209. const PROUTE_CD_SPEC = 'newcustomdataspecid';
  210. const PROUTE_CD_KEY = 'newcustomdatakey';
  211. const PROUTE_CD_VAL = 'newcustomdatavalue';
  212. const PROUTE_CH_USER = 'runcheckuser';
  213. const PROUTE_CH_AMOUNT = 'runcheckamount';
  214. const PROUTE_CH_STRAT = 'runcheckstratid';
  215. // ⠸⣿⣦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⡠⠔⠒⠒⠒⢤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  216. // ⠀⠙⠻⣿⣷⣦⣀⠀⠀⠀⢀⣾⣷⠀⠘⠀⠀⠀⠙⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  217. // ⠀⠀⠀⠀⠉⠛⠙⢏⢩⣶⣿⣿⠿⠖⠒⠤⣄⠀⠀⠈⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  218. // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠋⢅⡈⠐⠠⢀⠈⢆⠀⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  219. // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠐⠠⢀⠩⠀⢸⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  220. // ⣿⣹⠆⣿⣉⢀⡟⡄⣰⠉⠂⢸⣏⠁⠀⠀⠀⡌⠀⠀⠸⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  221. // ⠛⠀⠀⠓⠒⠘⠉⠛⠘⠒⠃⠘⠒⠂⠀⠀⢰⠁⠀⠀⠀⠑⢤⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
  222. // ⠀⠀⠀⢦⢠⡄⡄⢠⣦⠀⣔⠢⠀⠀⠀⠀⡠⠃⠀⠀⠀⠀⠀⠀⠀⠈⠉⠉⠙⠒⠒⠤⢄⣀⠤⠔⠒⡄
  223. // ⠀⠀⠀⠸⠏⠳⠃⠟⠺⠆⠬⠽⠀⠀⠀⢰⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇
  224. // ⠀⣄⢀⡀⣠⠀⢠⡀⣠⢠⡀⠀⣠⢀⡀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠀
  225. // ⠀⡏⢿⡇⣿⣒⠈⣧⡇⢸⣒⡂⣿⢺⡁⠀⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡤⠊⠀⠀
  226. // ⠀⠀⠈⠀⠀⠀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡸⠀⠀⠀⠀
  227. // ⠀⠀⠀⠀⠀⣼⣳⠀⡟⣼⠀⠀⠀⠀⠀⠀⠀⠈⢆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⠇⠀⠀⠀⠀
  228. // ⠀⠀⠀⠀⠀⠃⠈⠃⠃⠘⠀⠀⠀⠀⠀⠀⠀⠀⠈⢆⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⢀⠎⠀⠀⠀⠀⠀
  229. // ⡖⢲⡄⣶⣲⡆⢲⠒⣶⢀⡖⢲⠀⡶⡄⡆⠀⠀⠀⠀⣿⠁⠀⠈⠑⠢⣄⠀⠀⠀⢠⠎⠀⠀⠀⠀⠀⠀
  230. // ⠳⠼⠃⠿⠀⠀⠸⠀⠿⠈⠣⠞⠀⠇⠹⠇⠀⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⠙⣢⡴⠁⠀⠀⠀⠀⠀⠀⠀
  231. // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⣶⣶⣶⣾⣿⡿⠀⠀⠀⠀⠀⠀⠀⣿⠇⠀⠀⠀⠀⠀⠀⠀⠀
  232. // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⠛⠿⠛⠉⠀⠀⠀⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀
  233. // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⣴⣶⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀
  234. // ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠻⠛⠉⠻⠀⠀⠀⠀⠀⠀⠀⠀⠀
  235. public function __construct() {
  236. $this->initMessages();
  237. $this->loadConfigs();
  238. $this->setSpecTypes();
  239. $this->initDb();
  240. $this->loadStrategies();
  241. $this->loadTariffs();
  242. $this->loadUserData();
  243. $this->initOpayz();
  244. $this->loadAgents();
  245. $this->loadAssigns();
  246. $this->loadAgentsExtInfo();
  247. $this->preprocessAgentData();
  248. }
  249. /**
  250. * Inits system message helper for further usage
  251. *
  252. * @return void
  253. */
  254. protected function initMessages() {
  255. $this->messages = new UbillingMessageHelper();
  256. }
  257. /**
  258. * Preloads some required configs and sets few options
  259. *
  260. * @return void
  261. */
  262. protected function loadConfigs() {
  263. global $ubillingConfig;
  264. $this->altCfg = $ubillingConfig->getAlter();
  265. if (isset($this->altCfg['GOOSE_PERCENT_PRECISION'])) {
  266. $this->splitPercentPrecision = ubRouting::filters($this->altCfg['GOOSE_PERCENT_PRECISION'], 'int');
  267. }
  268. }
  269. /**
  270. * Sets default strategy spec types
  271. *
  272. * @return void
  273. */
  274. protected function setSpecTypes() {
  275. $this->specTypes = array(
  276. 'none' => __('None'),
  277. 'percent' => __('Percent of sum'),
  278. 'percentleft' => __('Percent of remains'),
  279. 'absolute' => __('Absolute'),
  280. 'leftovers' => __('Leftovers')
  281. );
  282. }
  283. /**
  284. * Inits required database layers
  285. *
  286. * @return void
  287. */
  288. protected function initDb() {
  289. $this->strategyDb = new NyanORM(self::TABLE_STRATEGY);
  290. $this->specsDb = new NyanORM(self::TABLE_SPECS);
  291. }
  292. /**
  293. * Loads existing tariffs data and names into protected properties
  294. *
  295. * @return void
  296. */
  297. protected function loadTariffs() {
  298. $this->allTariffs = zb_TariffGetAllData();
  299. if (!empty($this->allTariffs)) {
  300. foreach ($this->allTariffs as $io => $each) {
  301. $this->allTariffNames[$each['name']] = $each['name'];
  302. }
  303. }
  304. }
  305. /**
  306. * Preloads all existing strategies from database
  307. *
  308. * @return void
  309. */
  310. protected function loadStrategies() {
  311. $this->allStrategies = $this->strategyDb->getAll('id');
  312. if (!empty($this->allStrategies)) {
  313. foreach ($this->allStrategies as $io => $each) {
  314. $this->allStrategies[$each['id']]['specs'] = array();
  315. if ($each['useassigns'] and $each['primaryagentid']) {
  316. $this->allPrimaryAgents[$each['id']] = $each['primaryagentid'];
  317. }
  318. }
  319. }
  320. $this->allSpecs = $this->specsDb->getAll('id');
  321. if (!empty($this->allSpecs)) {
  322. foreach ($this->allSpecs as $io => $each) {
  323. if (isset($this->allStrategies[$each['stratid']])) {
  324. $customData = array();
  325. if (!empty($each['customdata'])) {
  326. $customData = json_decode($each['customdata'], true);
  327. }
  328. $each['customdata'] = $customData;
  329. $this->allSpecs[$each['id']]['customdata'] = $customData;
  330. $this->allStrategies[$each['stratid']]['specs'][$each['id']] = $each;
  331. }
  332. }
  333. }
  334. }
  335. /**
  336. * Loads existing agents data from database
  337. *
  338. * @return void
  339. */
  340. protected function loadAgents() {
  341. $this->allAgents = zb_ContrAhentGetAllDataAssoc();
  342. }
  343. /**
  344. * Loads all existing users data
  345. *
  346. * @return void
  347. */
  348. protected function loadUserData() {
  349. $this->allUserData = zb_UserGetAllDataCache();
  350. }
  351. /**
  352. * Preloads all address based and strict assigns into priavate props
  353. *
  354. * @return void
  355. */
  356. protected function loadAssigns() {
  357. $this->allAssigns = zb_AgentAssignGetAllData();
  358. $this->allAssignsStrict = zb_AgentAssignStrictGetAllData();
  359. }
  360. /**
  361. * Loads extended agents infor from database
  362. *
  363. * @return void
  364. */
  365. protected function loadAgentsExtInfo() {
  366. if (@$this->altCfg['AGENTS_EXTINFO_ON']) {
  367. $extInfTmp = zb_GetAgentExtInfo('', '', '', '', '');
  368. if (!empty($extInfTmp)) {
  369. foreach ($extInfTmp as $io => $each) {
  370. $this->allAgentsExtInfo[$each['agentid']][$each['id']] = $each;
  371. }
  372. }
  373. }
  374. }
  375. /**
  376. * Inits OpenPayz instance and preloads some paymentId=>login mappings
  377. *
  378. * @return void
  379. */
  380. protected function initOpayz() {
  381. $this->openPayz = new OpenPayz(false, true);
  382. $this->allPaymentIds = $this->openPayz->getCustomers();
  383. $this->allCustomerPaymentIds = $this->openPayz->getCustomersPaymentIds();
  384. }
  385. /**
  386. * Preprocesses all existing agents and extinfo data in some protected prop
  387. *
  388. * @return void
  389. */
  390. protected function preprocessAgentData() {
  391. if (!empty($this->allAgents)) {
  392. foreach ($this->allAgents as $eachAgentId => $eachAgentData) {
  393. $this->allAgentNames[$eachAgentId] = $eachAgentData['contrname'];
  394. $this->fullAgentData[$eachAgentId]['data'] = $eachAgentData;
  395. $this->fullAgentData[$eachAgentId]['split'] = array();
  396. if (isset($this->allAgentsExtInfo[$eachAgentId])) {
  397. $this->fullAgentData[$eachAgentId]['extinfo'] = $this->allAgentsExtInfo[$eachAgentId];
  398. } else {
  399. $this->fullAgentData[$eachAgentId]['extinfo'] = array();
  400. }
  401. }
  402. }
  403. }
  404. /**
  405. * Sets current instance amount property
  406. *
  407. * @param float $amount
  408. *
  409. * @return void
  410. */
  411. public function setAmount($amount = 0) {
  412. $this->amount = $amount;
  413. }
  414. /**
  415. * Sets the user login after applying necessary filters.
  416. *
  417. * @param string $userLogin The user login to be set. Default is an empty string.
  418. *
  419. * @return void
  420. */
  421. public function setUserLogin($userLogin = '') {
  422. $this->userLogin = ubRouting::filters($userLogin, 'login');
  423. }
  424. /**
  425. * Sets the user payment Id property and tries to set valid userLogin based on it
  426. *
  427. * @param string $paymentId
  428. *
  429. * @return void
  430. */
  431. public function setPaymentId($paymentId = '') {
  432. $this->userPaymentId = ubRouting::filters($paymentId, 'mres');
  433. if (isset($this->allPaymentIds[$this->userPaymentId])) {
  434. $this->userLogin = $this->allPaymentIds[$this->userPaymentId];
  435. }
  436. }
  437. /**
  438. * Sets current instance runtime ID
  439. *
  440. * @param string $runtime
  441. *
  442. * @return void
  443. */
  444. public function setRuntime($runtime = '') {
  445. $this->runtime = ubRouting::filters($runtime, 'vf');
  446. }
  447. /**
  448. * Returns some runtime array
  449. *
  450. * @param string $name
  451. *
  452. * @return array
  453. */
  454. protected function getRuntime($name) {
  455. if (empty($this->avarice)) {
  456. $this->avarice = new Avarice();
  457. }
  458. $result = $this->avarice->runtime($name);
  459. return ($result);
  460. }
  461. /**
  462. * Returns some existing user assigned agent data
  463. *
  464. * @param string $userLogin
  465. *
  466. * @return array
  467. */
  468. public function getUserAssignedAgentData($userLogin) {
  469. $result = array();
  470. if (isset($this->allUserData[$userLogin])) {
  471. $userData = $this->allUserData[$userLogin];
  472. $userAddress = $userData['cityname'] . ' ' . $userData['streetname'] . ' ' . $userData['buildnum'] . '/' . $userData['apt'];
  473. } else {
  474. $userAddress = '';
  475. }
  476. $assignedAgentId = zb_AgentAssignCheckLoginFast($userLogin, $this->allAssigns, $userAddress, $this->allAssignsStrict);
  477. if (isset($this->allAgents[$assignedAgentId])) {
  478. $result = $this->allAgents[$assignedAgentId];
  479. }
  480. return ($result);
  481. }
  482. /**
  483. * Renders the form for creating a new strategy.
  484. *
  485. * @return string
  486. */
  487. public function renderStrategyCreateForm() {
  488. $result = '';
  489. $agentParams = array(0 => __('No'));
  490. $agentParams += $this->allAgentNames;
  491. $tariffParams = array(0 => __('No'));
  492. $tariffParams += $this->allTariffNames;
  493. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  494. $inputs = wf_HiddenInput(self::PROUTE_ST_CREATE, 'true');
  495. $inputs .= wf_TextInput(self::PROUTE_ST_NAME, __('Name') . $sup, '', true, 20);
  496. $inputs .= wf_CheckInput(self::PROUTE_ST_ASSIGNS, __('Use address based assigns'), true, false);
  497. $inputs .= wf_Selector(self::PROUTE_ST_AGENTID, $agentParams, __('Primary agent'), '', true);
  498. $inputs .= wf_Selector(self::PROUTE_ST_TARIFF, $tariffParams, __('Tariff'), '', true);
  499. $inputs .= wf_delimiter(0);
  500. $inputs .= wf_Submit(__('Create'));
  501. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  502. return ($result);
  503. }
  504. /**
  505. * Creates new strategy database record
  506. *
  507. * @param string $name
  508. * @param bool $assigns
  509. * @param int $primaryAgentId
  510. * @param string $tariffName
  511. *
  512. * @return void
  513. */
  514. public function createStrategy($name, $assigns = false, $primaryAgentId = 0, $tariffName = 0) {
  515. $nameF = ubRouting::filters($name, 'safe');
  516. $assigns = ($assigns) ? 1 : 0;
  517. $primaryAgentId = ubRouting::filters($primaryAgentId, 'int');
  518. $tariffName = ubRouting::filters($tariffName, 'mres');
  519. $this->strategyDb->data('name', $nameF);
  520. $this->strategyDb->data('useassigns', $assigns);
  521. $this->strategyDb->data('primaryagentid', $primaryAgentId);
  522. $this->strategyDb->data('tariff', $tariffName);
  523. $this->strategyDb->create();
  524. $newId = $this->strategyDb->getLastId();
  525. log_register('GOOSE STRAT CREATE [' . $newId . '] `' . $name . '`');
  526. }
  527. /**
  528. * Save or update an existing strategy in the database.
  529. *
  530. * @param int $stratId The ID of the strategy to save or update.
  531. * @param string $name The name of the strategy.
  532. * @param bool $assigns Whether the strategy uses address assignments. Defaults to false.
  533. * @param int $primaryAgentId The ID of the primary agent. Defaults to 0.
  534. * @param string $tariffName
  535. * @return void
  536. */
  537. public function saveStrategy($stratId, $name, $assigns = false, $primaryAgentId = 0, $tariffName = 0) {
  538. $stratId = ubRouting::filters($stratId, 'int');
  539. $nameF = ubRouting::filters($name, 'safe');
  540. $assigns = ($assigns) ? 1 : 0;
  541. $primaryAgentId = ubRouting::filters($primaryAgentId, 'int');
  542. $tariffName = ubRouting::filters($tariffName, 'mres');
  543. if (isset($this->allStrategies[$stratId])) {
  544. $this->strategyDb->where('id', '=', $stratId);
  545. $this->strategyDb->data('name', $nameF);
  546. $this->strategyDb->data('useassigns', $assigns);
  547. $this->strategyDb->data('primaryagentid', $primaryAgentId);
  548. $this->strategyDb->data('tariff', $tariffName);
  549. $this->strategyDb->save();
  550. log_register('GOOSE STRAT EDIT [' . $stratId . '] `' . $name . '`');
  551. }
  552. }
  553. /**
  554. * Deletes a strategy from the database.
  555. *
  556. * @param int $stratId The ID of the strategy to be deleted.
  557. *
  558. * @return void
  559. */
  560. public function deleteStrategy($stratId) {
  561. $stratId = ubRouting::filters($stratId, 'int');
  562. if (isset($this->allStrategies[$stratId])) {
  563. $this->strategyDb->where('id', '=', $stratId);
  564. $this->strategyDb->delete();
  565. log_register('GOOSE STRAT DELETE [' . $stratId . ']');
  566. $this->flushStrategySpecs($stratId);
  567. }
  568. }
  569. /**
  570. * Flushed all strategy specs on strategy deletion
  571. *
  572. * @param int $stratId
  573. *
  574. * @return void
  575. */
  576. protected function flushStrategySpecs($stratId) {
  577. $stratId = ubRouting::filters($stratId, 'int');
  578. $this->specsDb->where('stratid', '=', $stratId);
  579. $this->specsDb->delete();
  580. log_register('GOOSE STRAT [' . $stratId . '] FLUSH SPECS');
  581. }
  582. /**
  583. * Renders the form for editing existing strategy.
  584. *
  585. * @return string
  586. */
  587. public function renderStrategyEditForm($stratId) {
  588. $result = '';
  589. $stratId = ubRouting::filters($stratId, 'int');
  590. $agentParams = array(0 => __('No'));
  591. $agentParams += $this->allAgentNames;
  592. $tariffParams = array(0 => __('No'));
  593. $tariffParams += $this->allTariffNames;
  594. if (isset($this->allStrategies[$stratId])) {
  595. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  596. $stratData = $this->allStrategies[$stratId];
  597. $assignsFlag = ($stratData['useassigns']) ? true : false;
  598. $inputs = wf_HiddenInput(self::PROUTE_ST_EDIT, $stratId);
  599. $inputs .= wf_TextInput(self::PROUTE_ST_NAME, __('Name') . $sup, $stratData['name'], true, 20);
  600. $inputs .= wf_CheckInput(self::PROUTE_ST_ASSIGNS, __('Use address based assigns'), true, $assignsFlag);
  601. $inputs .= wf_Selector(self::PROUTE_ST_AGENTID, $agentParams, __('Primary agent'), $stratData['primaryagentid'], true);
  602. $inputs .= wf_Selector(self::PROUTE_ST_TARIFF, $tariffParams, __('Tariff'), $stratData['tariff'], true);
  603. $inputs .= wf_delimiter(0);
  604. $inputs .= wf_Submit(__('Save'));
  605. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  606. } else {
  607. $result .= $this->messages->getStyledMessage(__('Strategy') . ' [' . $stratId . '] ' . __('Not exists'), 'error');
  608. }
  609. return ($result);
  610. }
  611. /**
  612. * Renders strategies dry-run testing form
  613. *
  614. * @return string
  615. */
  616. protected function renderStratTestingForm() {
  617. $result = '';
  618. $inputs = wf_TextInput(self::PROUTE_CH_USER, __('Login'), ubRouting::post(self::PROUTE_CH_USER), false, 10, 'login') . ' ';
  619. $inputs .= wf_TextInput(self::PROUTE_CH_AMOUNT, __('Payment sum'), ubRouting::post(self::PROUTE_CH_AMOUNT), false, 5, 'finance') . ' ';
  620. $inputs .= wf_TextInput(self::PROUTE_CH_STRAT, __('Strategy'), ubRouting::post(self::PROUTE_CH_STRAT), false, 2, 'digits') . ' ';
  621. $inputs .= wf_Submit(__('Testing'));
  622. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  623. return ($result);
  624. }
  625. /**
  626. * Renders a list of available strategies in a table format.
  627. *
  628. * @return string
  629. */
  630. public function renderStrategiesList() {
  631. $result = '';
  632. if (!empty($this->allStrategies)) {
  633. $cells = wf_TableCell(__('ID'));
  634. $cells .= wf_TableCell(__('Name'));
  635. $cells .= wf_TableCell(__('Use assigns'));
  636. $cells .= wf_TableCell(__('Primary agent'));
  637. $cells .= wf_TableCell(__('Tariff'));
  638. $cells .= wf_TableCell(__('Actions'));
  639. $rows = wf_TableRow($cells, 'row1');
  640. foreach ($this->allStrategies as $io => $each) {
  641. $cells = wf_TableCell($each['id']);
  642. $cells .= wf_TableCell($each['name']);
  643. $cells .= wf_TableCell(web_bool_led($each['useassigns']));
  644. $agentName = (isset($this->allAgentNames[$each['primaryagentid']])) ? $this->allAgentNames[$each['primaryagentid']] : __('No');
  645. $tariffName = (isset($this->allTariffNames[$each['tariff']])) ? $this->allTariffNames[$each['tariff']] : __('No');
  646. $cells .= wf_TableCell($agentName);
  647. $cells .= wf_TableCell($tariffName);
  648. $actControls = '';
  649. $deletionUrl = self::URL_ME . '&' . self::ROUTE_ST_DELETE . '=' . $each['id'];
  650. $delTitle = __('Delete') . ' ' . $each['name'] . '?';
  651. $actControls .= wf_ConfirmDialog($deletionUrl, web_delete_icon(), $this->messages->getDeleteAlert(), '', self::URL_ME, $delTitle);
  652. $actControls .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderStrategyEditForm($each['id'])) . ' ';
  653. $actControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_SP_EDIT . '=' . $each['id'], web_icon_extended(__('Config')));
  654. $cells .= wf_TableCell($actControls);
  655. $rows .= wf_TableRow($cells, 'row5');
  656. }
  657. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  658. } else {
  659. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  660. }
  661. $result .= wf_delimiter();
  662. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Create'), __('Create'), $this->renderStrategyCreateForm(), 'ubButton') . ' ';
  663. if (!empty($this->allStrategies)) {
  664. $result .= wf_modalAuto(wf_img('skins/icon_testing.png') . ' ' . __('Testing'), __('Testing'), $this->renderStratTestingForm(), 'ubButton');
  665. }
  666. return ($result);
  667. }
  668. /**
  669. * Retrieves the name of a strategy based on its ID.
  670. *
  671. * @param int $stratId The ID of the strategy.
  672. *
  673. * @return string The name of the strategy if found, otherwise an empty string.
  674. */
  675. public function getStrategyName($stratId) {
  676. $result = '';
  677. if (isset($this->allStrategies[$stratId])) {
  678. $result .= $this->allStrategies[$stratId]['name'];
  679. }
  680. return ($result);
  681. }
  682. /**
  683. * Creates a new specification entry in the database.
  684. *
  685. * @param int $stratId The strategy ID to associate with the specification.
  686. * @param int $agentId The agent ID to associate with the specification.
  687. * @param string $type The type of the specification.
  688. * @param int $value The value of the specification.
  689. *
  690. * @return void
  691. */
  692. public function createSpec($stratId, $agentId, $type, $value) {
  693. $stratId = ubRouting::filters($stratId, 'int');
  694. $agentId = ubRouting::filters($agentId, 'int');
  695. $type = ubRouting::filters($type, 'mres');
  696. $value = ubRouting::filters($value, 'int');
  697. $customData = json_encode(array());
  698. $this->specsDb->data('stratid', $stratId);
  699. $this->specsDb->data('agentid', $agentId);
  700. $this->specsDb->data('type', $type);
  701. $this->specsDb->data('value', $value);
  702. $this->specsDb->data('customdata', $customData);
  703. $this->specsDb->create();
  704. $newId = $this->specsDb->getLastId();
  705. log_register('GOOSE STRAT [' . $stratId . '] CREATE SPEC [' . $newId . '] AGENT [' . $agentId . '] `' . $type . '` VALUE `' . $value . '`');
  706. }
  707. /**
  708. * Save or update a specification in the database.
  709. *
  710. * @param int $specId The ID of the specification.
  711. * @param int $agentId The ID of the agent.
  712. * @param string $type The type of the specification.
  713. * @param int $value The value of the specification.
  714. *
  715. * @return void
  716. */
  717. public function saveSpec($specId, $agentId, $type, $value) {
  718. $specId = ubRouting::filters($specId, 'int');
  719. $agentId = ubRouting::filters($agentId, 'int');
  720. $type = ubRouting::filters($type, 'mres');
  721. $value = ubRouting::filters($value, 'int');
  722. if (isset($this->allSpecs[$specId])) {
  723. $specData = $this->allSpecs[$specId];
  724. $stratId = $specData['stratid'];
  725. $this->specsDb->where('id', '=', $specId);
  726. $this->specsDb->data('agentid', $agentId);
  727. $this->specsDb->data('type', $type);
  728. $this->specsDb->data('value', $value);
  729. $this->specsDb->save();
  730. $newId = $this->specsDb->getLastId();
  731. log_register('GOOSE STRAT [' . $stratId . '] EDIT SPEC [' . $specId . '] AGENT [' . $agentId . '] `' . $type . '` VALUE `' . $value . '`');
  732. }
  733. }
  734. /**
  735. * Deletes a specific specification by its ID.
  736. *
  737. * @param int $specId The ID of the specification to delete.
  738. *
  739. * @return void
  740. */
  741. public function deleteSpec($specId) {
  742. $specId = ubRouting::filters($specId, 'int');
  743. if (isset($this->allSpecs[$specId])) {
  744. $specData = $this->allSpecs[$specId];
  745. $stratId = $specData['stratid'];
  746. $this->specsDb->where('id', '=', $specId);
  747. $this->specsDb->delete();
  748. log_register('GOOSE STRAT [' . $stratId . '] DELETE SPEC [' . $specId . ']');
  749. }
  750. }
  751. /**
  752. * Renders a form for creating a new specification.
  753. *
  754. * @param int $stratId The ID of the strategy for which the form is being created.
  755. * @return string
  756. */
  757. public function renderSpecCreateForm($stratId) {
  758. $stratId = ubRouting::filters($stratId, 'int');
  759. $result = '';
  760. if (isset($this->allStrategies[$stratId])) {
  761. $inputs = wf_HiddenInput(self::PROUTE_SP_CREATE, 'true');
  762. $inputs .= wf_HiddenInput(self::PROUTE_SP_STRAT, $stratId);
  763. $inputs .= wf_Selector(self::PROUTE_SP_AGENT, $this->allAgentNames, __('Agent'), '', false);
  764. $inputs .= wf_Selector(self::PROUTE_SP_TYPE, $this->specTypes, __('Distribution'), '', false);
  765. $inputs .= wf_TextInput(self::PROUTE_SP_VALUE, __('Value'), '', true, 5, 'digits');
  766. $inputs .= wf_delimiter(0);
  767. $inputs .= wf_Submit(__('Create'));
  768. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  769. } else {
  770. $result .= $this->messages->getStyledMessage(__('Strategy') . ' [' . $stratId . '] ' . __('Not exists'), 'error');
  771. }
  772. return ($result);
  773. }
  774. /**
  775. * Renders a form for editing a existing specification.
  776. *
  777. * @param int $specId The ID of the strategy spec database record
  778. * @return string
  779. */
  780. public function renderSpecEditForm($specId) {
  781. $specId = ubRouting::filters($specId, 'int');
  782. $result = '';
  783. if (isset($this->allSpecs[$specId])) {
  784. $specData = $this->allSpecs[$specId];
  785. $inputs = wf_HiddenInput(self::PROUTE_SP_EDIT, $specId);
  786. $inputs .= wf_HiddenInput(self::PROUTE_SP_STRAT, $specData['stratid']);
  787. $inputs .= wf_Selector(self::PROUTE_SP_AGENT, $this->allAgentNames, __('Agent'), $specData['agentid'], false);
  788. $inputs .= wf_Selector(self::PROUTE_SP_TYPE, $this->specTypes, __('Distribution'), $specData['type'], false);
  789. $inputs .= wf_TextInput(self::PROUTE_SP_VALUE, __('Value'), $specData['value'], true, 5, 'digits');
  790. $inputs .= wf_delimiter(0);
  791. $inputs .= wf_Submit(__('Save'));
  792. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  793. } else {
  794. $result .= $this->messages->getStyledMessage(__('Spec') . ' [' . $specId . '] ' . __('Not exists'), 'error');
  795. }
  796. return ($result);
  797. }
  798. /**
  799. * Renders the strategy specifications list for a given strategy ID.
  800. *
  801. * @param int $stratId The ID of the strategy to render specifications for.
  802. *
  803. * @return string
  804. */
  805. public function renderStratSpecsList($stratId) {
  806. $stratId = ubRouting::filters($stratId, 'int');
  807. $result = '';
  808. if (isset($this->allStrategies[$stratId])) {
  809. $stratSpecs = $this->allStrategies[$stratId]['specs'];
  810. if (!empty($stratSpecs)) {
  811. $cells = wf_TableCell(__('Agent'));
  812. $cells .= wf_TableCell(__('Type'));
  813. $cells .= wf_TableCell(__('Value'));
  814. $cells .= wf_TableCell(__('Custom data'));
  815. $cells .= wf_TableCell(__('Actions'));
  816. $rows = wf_TableRow($cells, 'row1');
  817. foreach ($stratSpecs as $io => $each) {
  818. $agentName = (isset($this->allAgentNames[$each['agentid']])) ? $this->allAgentNames[$each['agentid']] : __('No');
  819. $cells = wf_TableCell($agentName);
  820. $cells .= wf_TableCell($this->specTypes[$each['type']]);
  821. $cells .= wf_TableCell($each['value']);
  822. $cells .= wf_TableCell(web_bool_led($each['customdata']));
  823. $actControls = '';
  824. $deletionUrl = self::URL_ME . '&' . self::ROUTE_SP_DELETE . '=' . $each['id'] . '&' . self::ROUTE_SP_EDIT . '=' . $stratId;
  825. $cancelUrl = self::URL_ME . '&' . self::ROUTE_SP_EDIT . '=' . $stratId;
  826. $agentName = (isset($this->allAgentNames[$each['agentid']])) ? $this->allAgentNames[$each['agentid']] : __('Deleted');
  827. $delTitle = __('Delete') . ' ' . $agentName . '?';
  828. $actControls .= wf_ConfirmDialog($deletionUrl, web_delete_icon(), $this->messages->getDeleteAlert(), '', $cancelUrl, $delTitle) . ' ';
  829. $actControls .= wf_modalAuto(web_edit_icon(), __('Edit'), $this->renderSpecEditForm($each['id']), '');
  830. $actControls .= wf_Link(self::URL_ME . '&' . self::ROUTE_SP_CUSTDATA . '=' . $each['id'], wf_img('skins/grcustdata.png', __('Custom data')));
  831. $cells .= wf_TableCell($actControls);
  832. $rows .= wf_TableRow($cells, 'row5');
  833. }
  834. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  835. } else {
  836. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  837. }
  838. } else {
  839. $result .= $this->messages->getStyledMessage(__('Strategy') . ' [' . $stratId . '] ' . __('Not exists'), 'error');
  840. }
  841. $result .= wf_delimiter();
  842. $result .= wf_BackLink(self::URL_ME) . ' ';
  843. $result .= wf_modalAuto(web_icon_create() . ' ' . __('Append'), __('Append'), $this->renderSpecCreateForm($stratId), 'ubButton');
  844. return ($result);
  845. }
  846. /**
  847. * Creates new or replaces custom data field record for some spec
  848. *
  849. * @param int $specId
  850. * @param string $key
  851. * @param string $value
  852. *
  853. * @return void
  854. */
  855. public function setCustDataField($specId, $key, $value = '') {
  856. $specId = ubRouting::filters($specId, 'int');
  857. $keyF = ubRouting::filters($key, 'mres');
  858. $valueF = ubRouting::filters($value, 'mres');
  859. if (isset($this->allSpecs[$specId])) {
  860. $currentCustomData = $this->allSpecs[$specId]['customdata'];
  861. $dataToSave = $currentCustomData;
  862. $dataToSave[$keyF] = $valueF;
  863. $dataToSave = json_encode($dataToSave, JSON_UNESCAPED_UNICODE);
  864. $this->specsDb->where('id', '=', $specId);
  865. $this->specsDb->data('customdata', $dataToSave);
  866. $this->specsDb->save();
  867. log_register('GOOSE SPEC [' . $specId . '] CUSTDATA SET `' . $key . '` ON `' . $value . '`');
  868. }
  869. }
  870. /**
  871. * Deletes custom data field record for some spec
  872. *
  873. * @param int $specId
  874. * @param string $key
  875. *
  876. * @return void
  877. */
  878. public function deleteCustDataField($specId, $key) {
  879. $specId = ubRouting::filters($specId, 'int');
  880. $keyF = ubRouting::filters($key, 'mres');
  881. if (isset($this->allSpecs[$specId])) {
  882. $currentCustomData = $this->allSpecs[$specId]['customdata'];
  883. $dataToSave = $currentCustomData;
  884. unset($dataToSave[$key]);
  885. $dataToSave = json_encode($dataToSave, JSON_UNESCAPED_UNICODE);
  886. $this->specsDb->where('id', '=', $specId);
  887. $this->specsDb->data('customdata', $dataToSave);
  888. $this->specsDb->save();
  889. log_register('GOOSE SPEC [' . $specId . '] CUSTDATA DELETE `' . $key . '`');
  890. }
  891. }
  892. /**
  893. * Renders a custom data fields creation form based on the provided specification ID.
  894. *
  895. * @param int $specId
  896. *
  897. * @return string
  898. */
  899. protected function renderCustomDataCreateForm($specId) {
  900. $result = '';
  901. $specId = ubRouting::filters($specId, 'int');
  902. if (isset($this->allSpecs[$specId])) {
  903. $inputs = wf_HiddenInput(self::PROUTE_CD_SPEC, $specId);
  904. $inputs .= wf_TextInput(self::PROUTE_CD_KEY, __('Key'), '', false, 10);
  905. $inputs .= wf_TextInput(self::PROUTE_CD_VAL, __('Value'), '', false, 20);
  906. $inputs .= wf_Submit(__('Set'));
  907. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  908. }
  909. return ($result);
  910. }
  911. /**
  912. * Renders spec custom data list and editors
  913. *
  914. * @param int $specId
  915. *
  916. * @return string
  917. */
  918. public function renderCustomDataEditor($specId) {
  919. $specId = ubRouting::filters($specId, 'int');
  920. $result = '';
  921. if (isset($this->allSpecs[$specId])) {
  922. $specData = $this->allSpecs[$specId];
  923. if (!empty($specData['customdata'])) {
  924. $cells = wf_TableCell(__('Key'));
  925. $cells .= wf_TableCell(__('Value'));
  926. $cells .= wf_TableCell(__('Actions'));
  927. $rows = wf_TableRow($cells, 'row1');
  928. foreach ($specData['customdata'] as $key => $value) {
  929. $cells = wf_TableCell($key);
  930. $cells .= wf_TableCell($value);
  931. $delUrl = self::URL_ME . '&' . self::ROUTE_SP_CUSTDATA . '=' . $specId . '&' . self::ROUTE_CD_DELKEY . '=' . $key;
  932. $cancelUrl = self::URL_ME . '&' . self::ROUTE_SP_CUSTDATA . '=' . $specId;
  933. $actLinks = wf_ConfirmDialog($delUrl, web_delete_icon(), $this->messages->getDeleteAlert(), '', $cancelUrl, __('Delete') . '?');
  934. $cells .= wf_TableCell($actLinks);
  935. $rows .= wf_TableRow($cells, 'row5');
  936. }
  937. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  938. } else {
  939. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'info');
  940. }
  941. $result .= wf_delimiter();
  942. $result .= $this->renderCustomDataCreateForm($specId);
  943. $result .= wf_delimiter();
  944. $result .= wf_BackLink(self::URL_ME . '&' . self::ROUTE_SP_EDIT . '=' . $specData['stratid']);
  945. } else {
  946. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': [' . $specId . '] ' . __('Not exists'), 'error');
  947. }
  948. return ($result);
  949. }
  950. /**
  951. * Renders strategy-run testing results
  952. *
  953. * @param array $stratData
  954. *
  955. * @return string
  956. */
  957. public function renderStratTestingResults($stratData) {
  958. $result = '';
  959. if (!empty($stratData)) {
  960. if ($stratData['user']) {
  961. $result .= $this->messages->getStyledMessage(__('User') . ' `' . $stratData['user']['login'] . '` ' . __('exists'), 'success');
  962. $result .= $this->messages->getStyledMessage(__('Address') . ': ' . $stratData['user']['fulladress'], 'success');
  963. $result .= $this->messages->getStyledMessage(__('Tariff') . ': ' . $stratData['user']['Tariff'], 'success');
  964. } else {
  965. $result .= $this->messages->getStyledMessage(__('User') . ' `' . $stratData['userlogin'] . '` ' . __('not exists'), 'error');
  966. }
  967. if ($stratData['amount'] > 0) {
  968. $result .= $this->messages->getStyledMessage(__('Payment sum') . ': ' . $stratData['amount'], 'success');
  969. } else {
  970. $result .= $this->messages->getStyledMessage(__('Payment sum') . ': ' . $stratData['amount'], 'warning');
  971. }
  972. $agentLookupData = @$this->getUserAssignedAgentData($stratData['user']['login']);
  973. $agentLookupName = (isset($this->allAgentNames[$agentLookupData['id']])) ? $this->allAgentNames[$agentLookupData['id']] : __('No');
  974. $result .= $this->messages->getStyledMessage(__('Contrahent name') . ': ' . '[' . $agentLookupData['id'] . '] ' . $agentLookupName, 'info');
  975. $result .= $this->messages->getStyledMessage(__('Strategy used') . ': ' . '[' . $stratData['id'] . '] ' . $stratData['name'], 'info');
  976. if (!empty($stratData['agents'])) {
  977. $cells = wf_TableCell(__('ID'));
  978. $cells .= wf_TableCell(__('Contrahent name'));
  979. $cells .= wf_TableCell(__('Bank account'));
  980. $cells .= wf_TableCell(__('EDRPOU'));
  981. $cells .= wf_TableCell(__('Distribution'));
  982. $cells .= wf_TableCell(__('Value'));
  983. $cells .= wf_TableCell(__('Cash'));
  984. $rows = wf_TableRow($cells, 'row1');
  985. foreach ($stratData['agents'] as $eachAgentId => $eachAgentData) {
  986. $cells = wf_TableCell($eachAgentData['id']);
  987. $cells .= wf_TableCell($eachAgentData['contrname']);
  988. $cells .= wf_TableCell($eachAgentData['bankacc']);
  989. $cells .= wf_TableCell($eachAgentData['edrpo']);
  990. $cells .= wf_TableCell($this->specTypes[$eachAgentData['splittype']]);
  991. $cells .= wf_TableCell($eachAgentData['splitvalue']);
  992. $cells .= wf_TableCell($eachAgentData['splitamount']);
  993. $rows .= wf_TableRow($cells, 'row5');
  994. }
  995. $result .= wf_delimiter(0);
  996. $result .= wf_tag('b') . __('Money distribution') . ':' . wf_tag('b', true);
  997. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  998. } else {
  999. $result .= $this->messages->getStyledMessage(__('Contrahens') . ' ' . __('is empty') . '!', 'error');
  1000. }
  1001. //raw json preview
  1002. $inputs = wf_tag('textarea', false, 'fileeditorarea', 'name="editfilecontent" cols="145" rows="30" spellcheck="false"');
  1003. $inputs .= print_r($stratData, true);
  1004. $inputs .= wf_tag('textarea', true);
  1005. $inputs .= wf_tag('br');
  1006. $rawData = wf_Form('', 'POST', $inputs, 'glamour');
  1007. $result .= wf_Spoiler($rawData, __('Preview') . ' ' . __('JSON'), true);
  1008. } else {
  1009. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Data') . ' ' . __('is empty'), 'error');
  1010. }
  1011. $result .= wf_delimiter();
  1012. $result .= wf_BackLink(self::URL_ME);
  1013. return ($result);
  1014. }
  1015. /**
  1016. * Preprocess some agents data depend on strategy specs
  1017. *
  1018. * @param array $specs
  1019. * @param float $amount
  1020. *
  1021. * @return array
  1022. */
  1023. protected function calcAgents($specs, $amount = 0) {
  1024. $result = array();
  1025. $origAmount = $amount;
  1026. $specAmount = $amount;
  1027. $leftoversCount = 0;
  1028. if (!empty($specs)) {
  1029. //specs processing
  1030. foreach ($specs as $io => $each) {
  1031. $agentId = $each['agentid'];
  1032. if (isset($this->allAgents[$agentId])) {
  1033. $result[$agentId] = $this->allAgents[$agentId];
  1034. $result[$agentId]['splitamount'] = 0;
  1035. $result[$agentId]['splittype'] = $each['type'];
  1036. $result[$agentId]['splitvalue'] = $each['value'];
  1037. if ($each['type'] == 'leftovers') {
  1038. $leftoversCount++;
  1039. }
  1040. //appending legacy extinfo
  1041. if (isset($this->allAgentsExtInfo[$agentId])) {
  1042. $result[$agentId]['extinfo'] = $this->allAgentsExtInfo[$agentId];
  1043. $this->stategyAgentsExtInfo += $this->allAgentsExtInfo[$agentId];
  1044. } else {
  1045. $result[$agentId]['extinfo'] = array();
  1046. }
  1047. //appending custom data section
  1048. $result[$agentId]['customdata'] = $each['customdata'];
  1049. }
  1050. }
  1051. if ($specAmount > 0) {
  1052. //agents post-processing
  1053. if (!empty($result)) {
  1054. //absolute values
  1055. foreach ($result as $io => $each) {
  1056. if ($each['splittype'] == 'absolute') {
  1057. if ($each['splitvalue'] > 0) {
  1058. $this->minAmount += $each['splitvalue'];
  1059. $splitAmount = $each['splitvalue'];
  1060. $specAmount = $specAmount - $splitAmount;
  1061. $result[$each['id']]['splitamount'] = $splitAmount;
  1062. }
  1063. }
  1064. }
  1065. //percent of sum values
  1066. foreach ($result as $io => $each) {
  1067. if ($each['splittype'] == 'percent') {
  1068. if ($each['splitvalue'] > 0) {
  1069. $splitAmount = zb_Percent($origAmount, $each['splitvalue']);
  1070. if ($this->splitPercentPrecision) {
  1071. $splitAmount = round($splitAmount, 2);
  1072. }
  1073. $specAmount = $specAmount - $splitAmount;
  1074. $result[$each['id']]['splitamount'] = $splitAmount;
  1075. }
  1076. }
  1077. }
  1078. //percent of remains values
  1079. foreach ($result as $io => $each) {
  1080. if ($each['splittype'] == 'percentleft') {
  1081. if ($each['splitvalue'] > 0) {
  1082. $splitAmount = zb_Percent($specAmount, $each['splitvalue']);
  1083. if ($this->splitPercentPrecision) {
  1084. $splitAmount = round($splitAmount, 2);
  1085. }
  1086. $specAmount = $specAmount - $splitAmount;
  1087. $result[$each['id']]['splitamount'] = $splitAmount;
  1088. }
  1089. }
  1090. }
  1091. //leftovers at end
  1092. foreach ($result as $io => $each) {
  1093. if ($each['splittype'] == 'leftovers') {
  1094. if ($leftoversCount > 1) {
  1095. $splitAmount = $specAmount / $leftoversCount;
  1096. } else {
  1097. $splitAmount = $specAmount;
  1098. }
  1099. $result[$each['id']]['splitamount'] = $splitAmount;
  1100. }
  1101. }
  1102. }
  1103. }
  1104. }
  1105. return ($result);
  1106. }
  1107. /**
  1108. * Detects the strategy ID assigned to a user based on their login.
  1109. *
  1110. * This method checks if the user has an assigned agent and then matches that agent
  1111. * to a strategy ID from the list of all primary agents.
  1112. *
  1113. * @param string $userLogin The login identifier of the user.
  1114. *
  1115. * @return int
  1116. **/
  1117. protected function detectUserStrategyId($userLogin) {
  1118. $result = 0;
  1119. if (!empty($userLogin)) {
  1120. if (!empty($this->allStrategies)) {
  1121. $assignedAgentData = $this->getUserAssignedAgentData($userLogin);
  1122. if (!empty($assignedAgentData)) {
  1123. $assignedAgentId = $assignedAgentData['id'];
  1124. if ($assignedAgentId) {
  1125. //based on default address based assigns
  1126. if (!empty($this->allPrimaryAgents)) {
  1127. foreach ($this->allPrimaryAgents as $eachStratId => $eachAgentId) {
  1128. if ($eachAgentId == $assignedAgentId) {
  1129. $result = $eachStratId;
  1130. break;
  1131. }
  1132. }
  1133. }
  1134. //tariff based lookup
  1135. if (isset($this->allUserData[$userLogin])) {
  1136. $userTariff = $this->allUserData[$userLogin]['Tariff'];
  1137. foreach ($this->allStrategies as $io => $each) {
  1138. if ($each['tariff']) {
  1139. if ($each['tariff'] == $userTariff) {
  1140. $result = $each['id'];
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. }
  1146. //use first available strategy if still not detected
  1147. if ($result == 0) {
  1148. $firstStratData = reset($this->allStrategies);
  1149. $result = $firstStratData['id'];
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. return ($result);
  1156. }
  1157. /**
  1158. * Returns strategy data by its ID
  1159. *
  1160. * @param int $stratId implict strategy Id or 0 for auto detect
  1161. *
  1162. * @return array
  1163. */
  1164. public function getStrategyData($stratId = 0) {
  1165. $stratId = ubRouting::filters($stratId, 'int');
  1166. $result = array();
  1167. //automatic strategy detection
  1168. if ($stratId == 0) {
  1169. $stratId = $this->detectUserStrategyId($this->userLogin);
  1170. }
  1171. if (!empty($stratId)) {
  1172. if (isset($this->allStrategies[$stratId])) {
  1173. $stratData = $this->allStrategies[$stratId];
  1174. $result['amount'] = $this->amount;
  1175. $result['userlogin'] = $this->userLogin;
  1176. $result['paymentid'] = $this->userPaymentId;
  1177. $result['payopts'] = array();
  1178. $result['payopts']['minamount'] = $this->minAmount;
  1179. $result['payopts']['maxamount'] = $this->maxAmount;
  1180. $result['payopts']['isvalid'] = $this->paymentIsValid;
  1181. $result['payopts']['amount'] = $this->amount;
  1182. $result += $stratData;
  1183. $result['agents'] = array();
  1184. $result['user'] = array();
  1185. $result['runtime'] = array();
  1186. if (!empty($stratData['specs'])) {
  1187. $result['agents'] = $this->calcAgents($stratData['specs'], $this->amount);
  1188. $result['agentsextinfo'] = $this->stategyAgentsExtInfo;
  1189. //cleanup spec raw data
  1190. unset($result['specs']);
  1191. //set payment opts
  1192. if ($this->amount >= $this->minAmount) {
  1193. $this->paymentIsValid = true;
  1194. } else {
  1195. $this->paymentIsValid = false;
  1196. }
  1197. $result['payopts']['isvalid'] = $this->paymentIsValid;
  1198. $result['payopts']['minamount'] = $this->minAmount;
  1199. $result['payopts']['maxamount'] = $this->maxAmount;
  1200. //reset flags
  1201. $this->minAmount = 0;
  1202. $this->maxAmount = 0;
  1203. $this->paymentIsValid = false;
  1204. }
  1205. //appending user data
  1206. if (!empty($this->userLogin)) {
  1207. if (isset($this->allUserData[$this->userLogin])) {
  1208. $result['user'] = $this->allUserData[$this->userLogin];
  1209. if (isset($this->allCustomerPaymentIds[$this->userLogin])) {
  1210. $result['user']['paymentid'] = $this->allCustomerPaymentIds[$this->userLogin];
  1211. $result['paymentid'] = $result['user']['paymentid'];
  1212. }
  1213. }
  1214. }
  1215. //appending runtime if required
  1216. if (!empty($this->runtime)) {
  1217. $result['runtime'] = $this->getRuntime($this->runtime);
  1218. }
  1219. }
  1220. }
  1221. return ($result);
  1222. }
  1223. }