api.deferredsale.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Deferred sales basic implementation
  4. */
  5. class DeferredSale {
  6. /**
  7. * Minimum sale term in months
  8. *
  9. * @var int
  10. */
  11. protected $minMonth = 1;
  12. /**
  13. * Maximum sale term in months
  14. *
  15. * @var int
  16. */
  17. protected $maxMonth = 12;
  18. /**
  19. * Contains current instance user login
  20. *
  21. * @var string
  22. */
  23. protected $login = '';
  24. /**
  25. * Some other predefined stuff
  26. */
  27. const PROUTE_RUN = 'dsalerun';
  28. const PROUTE_SUMM = 'dsalesumm';
  29. const PROUTE_TERM = 'dsaleterm';
  30. const PROUTE_NOTE = 'dsalenote';
  31. const NOTE_PREFIX = 'DEFSALE:';
  32. public function __construct($userLogin) {
  33. $this->setLogin($userLogin);
  34. }
  35. /**
  36. * Sets current instance user login
  37. *
  38. * @param string $userLogin
  39. *
  40. * @return void
  41. */
  42. protected function setLogin($userLogin) {
  43. $this->login = $userLogin;
  44. }
  45. /**
  46. * Returns months term array for term selector
  47. *
  48. * @return array
  49. */
  50. protected function getTermsArr() {
  51. $result = array();
  52. for ($i = $this->minMonth; $i <= $this->maxMonth; $i++) {
  53. $result[$i] = $i;
  54. }
  55. return($result);
  56. }
  57. /**
  58. * Returns deferred sale form
  59. *
  60. * @return string
  61. */
  62. public function renderForm() {
  63. $result = '';
  64. //
  65. // m
  66. // / \
  67. // . \
  68. // ( O \ This pretty mushroom is for you
  69. // / _ O\ When it's spreading in the clouds
  70. // / (_) .
  71. // . O \
  72. // (__O__________)
  73. // \((((())))))/
  74. // / X
  75. // | /
  76. //_m.n.(___)nmm._.,n._
  77. //
  78. $inputs = wf_HiddenInput(self::PROUTE_RUN, 'true');
  79. $inputs .= wf_TextInput(self::PROUTE_SUMM, __('Withdraw from user account'), '', true, 5, 'finance');
  80. $inputs .= wf_Selector(self::PROUTE_TERM, $this->getTermsArr(), __('for so many months'), '', true);
  81. $inputs .= wf_TextInput(self::PROUTE_NOTE, __('Notes'), '', true, 30);
  82. $inputs .= wf_delimiter(0);
  83. $inputs .= wf_Submit(__('Charge'));
  84. $form = wf_Form('', 'POST', $inputs, 'glamour');
  85. $result .= ' ' . wf_modalAuto(wf_img_sized('skins/letter-r16.png', __('Deferred sale'), '10'), __('Deferred sale'), $form);
  86. return($result);
  87. }
  88. /**
  89. * Catches sale request and performs sale
  90. *
  91. * @return void/string on error
  92. */
  93. public function catchRequest() {
  94. $result = '';
  95. if (ubRouting::checkPost(array(self::PROUTE_RUN, self::PROUTE_SUMM, self::PROUTE_TERM))) {
  96. $chargeSumm = ubRouting::post(self::PROUTE_SUMM, 'mres');
  97. $chargeTerm = ubRouting::post(self::PROUTE_TERM, 'int');
  98. $chargeNoteRaw = ubRouting::post(self::PROUTE_NOTE, 'mres');
  99. if (zb_checkMoney($chargeSumm)) {
  100. if ($chargeTerm) {
  101. $dealWithIt = new DealWithIt();
  102. $chargeSumm = abs($chargeSumm); //always positive
  103. $chargeFee = $chargeSumm / $chargeTerm; //calculating monthly fee
  104. $chargeFee = round($chargeFee, 2); //rounded to cents
  105. $chargeFee = '-' . $chargeFee; //yes, its fee
  106. for ($monthCount = 1; $monthCount <= $chargeTerm; $monthCount++) {
  107. $chargeNote = self::NOTE_PREFIX . $chargeNoteRaw . ' #' . $monthCount;
  108. //calculating each next month start date
  109. $targetChargeDate = date('Y-m-d', mktime(0, 0, 0, date('m') + $monthCount, 1, date('Y')));
  110. //planning tasks
  111. $dealWithIt->createTask($targetChargeDate, $this->login, 'corrcash', $chargeFee, $chargeNote);
  112. }
  113. log_register('DEFSALE (' . $this->login . ') SCHEDULED `' . $chargeSumm . '` FOR `' . $chargeTerm . '` MONTHS');
  114. //preventing charge duplicates
  115. if (ubRouting::checkGet('module')) {
  116. $currentModule = ubRouting::get('module');
  117. $redirectUrl = '';
  118. if ($currentModule == 'userprofile') {
  119. $redirectUrl = '?module=userprofile&username=' . $this->login;
  120. }
  121. if ($currentModule == 'addcash') {
  122. $redirectUrl = '?module=addcash&username=' . $this->login . '#cashfield';
  123. }
  124. if (!empty($redirectUrl)) {
  125. //must be an header redirect to avoid fails with URLs that contains #anchor
  126. ubRouting::nav($redirectUrl, true);
  127. }
  128. }
  129. }
  130. } else {
  131. $result .= wf_modalOpened(__('Error'), __('Wrong format of a sum of money to pay'), '400', '200');
  132. log_register('DEFSALE (' . $this->login . ') WRONG SUMM `' . $chargeSumm . '`');
  133. }
  134. }
  135. return($result);
  136. }
  137. }