PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/fannie/mem/correction_pages/MemArEquityDumpTool.php

https://github.com/CORE-POS/IS4C
PHP | 333 lines | 270 code | 38 blank | 25 comment | 30 complexity | 09d05487e9d21334a74d2cd5be475ee8 MD5 | raw file
  1. <?php
  2. /*******************************************************************************
  3. Copyright 2010,2013 Whole Foods Co-op, Duluth, MN
  4. This file is part of CORE-POS.
  5. IT CORE is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. IT CORE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. in the file license.txt along with IT CORE; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. *********************************************************************************/
  17. include(dirname(__FILE__) . '/../../config.php');
  18. if (!class_exists('FannieAPI')) {
  19. include_once(__DIR__ . '/../../classlib2.0/FannieAPI.php');
  20. }
  21. class MemArEquityDumpTool extends FanniePage
  22. {
  23. protected $title='Fannie - Member Management Module';
  24. protected $header='Dump Member Equity/AR';
  25. public $description = '[Remove Equity/AR] simply removes amounts from a member\'s account.';
  26. public $themed = true;
  27. protected $must_authenticate = true;
  28. protected $auth_classes = array('editmembers');
  29. private $errors = '';
  30. private $mode = 'init';
  31. private $depts = array();
  32. private $CORRECTION_CASHIER = 1001;
  33. private $CORRECTION_LANE = 30;
  34. private $DEFAULT_DEPT = 703;
  35. private $dept1;
  36. private $dept2;
  37. private $amount;
  38. private $cn;
  39. private $name1;
  40. function preprocess(){
  41. global $FANNIE_AR_DEPARTMENTS;
  42. global $FANNIE_EQUITY_DEPARTMENTS;
  43. global $FANNIE_OP_DB;
  44. global $FANNIE_EMP_NO, $FANNIE_REGISTER_NO;
  45. global $FANNIE_MISC_DEPT;
  46. /**
  47. Use fannie settings if properly configured
  48. */
  49. if (is_numeric($FANNIE_EMP_NO)) {
  50. $this->CORRECTION_CASHIER = $FANNIE_EMP_NO;
  51. }
  52. if (is_numeric($FANNIE_REGISTER_NO)) {
  53. $this->CORRECTION_LANE = $FANNIE_REGISTER_NO;
  54. }
  55. if (is_numeric($FANNIE_MISC_DEPT)) {
  56. $this->DEFAULT_DEPT = $FANNIE_MISC_DEPT;
  57. }
  58. if (empty($FANNIE_AR_DEPARTMENTS)){
  59. $this->errors .= '<div class="alert alert-danger">Error: no AR departments found</div>';
  60. return True;
  61. }
  62. if (empty($FANNIE_EQUITY_DEPARTMENTS)){
  63. $this->errors .= '<div class="alert alert-danger">Error: no Equity departments found</div>';
  64. return True;
  65. }
  66. $ret = preg_match_all("/[0-9]+/",$FANNIE_AR_DEPARTMENTS,$depts);
  67. if ($ret == 0){
  68. $this->errors .= '<div class="alert alert-danger">Error: can\'t read AR department definitions</div>';
  69. return True;
  70. }
  71. $temp_depts = array_pop($depts);
  72. $ret = preg_match_all("/[0-9]+/",$FANNIE_EQUITY_DEPARTMENTS,$depts);
  73. if ($ret == 0){
  74. $this->errors .= '<div class="alert alert-danger">Error: can\'t read Equity department definitions</div>';
  75. return True;
  76. }
  77. $temp_depts2 = array_pop($depts);
  78. foreach($temp_depts2 as $num)
  79. $temp_depts[] = $num;
  80. $dlist = "(";
  81. $dArgs = array();
  82. foreach ($temp_depts as $d){
  83. $dlist .= "?,";
  84. $dArgs[] = $d;
  85. }
  86. $dlist = substr($dlist,0,strlen($dlist)-1).")";
  87. $dbc = FannieDB::get($FANNIE_OP_DB);
  88. $q = $dbc->prepare("SELECT dept_no,dept_name FROM departments WHERE dept_no IN $dlist");
  89. $r = $dbc->execute($q,$dArgs);
  90. if ($dbc->num_rows($r) == 0){
  91. $this->errors .= '<div class="alert alert-danger">Error: department(s) don\'t exist.</div>';
  92. return true;
  93. }
  94. $this->depts = array();
  95. while($row = $dbc->fetch_row($r)){
  96. $this->depts[$row[0]] = $row[1];
  97. }
  98. if (FormLib::get_form_value('submit1',False) !== False)
  99. $this->mode = 'confirm';
  100. elseif (FormLib::get_form_value('submit2',False) !== False)
  101. $this->mode = 'finish';
  102. // error check inputs
  103. if ($this->mode != 'init'){
  104. $this->dept1 = FormLib::get_form_value('deptFrom');
  105. $this->dept2 = FormLib::get_form_value('deptTo');
  106. $this->amount = FormLib::get_form_value('amount');
  107. $this->cn = FormLib::get_form_value('card_no');
  108. if (!is_numeric($this->amount)){
  109. $this->errors .= "<div class=\"alert alert-danger\">Error: amount given (".$this->amount.") isn't a number</div>"
  110. ."<br /><br />"
  111. ."<a href=\"\" onclick=\"back(); return false;\">Back</a>";
  112. return True;
  113. }
  114. if (!is_numeric($this->cn)){
  115. $this->errors .= "<div class=\"alert alert-danger\">Error: member given (".$this->cn1.") isn't a number</div>"
  116. ."<br /><br />"
  117. ."<a href=\"\" onclick=\"back(); return false;\">Back</a>";
  118. return True;
  119. }
  120. if ($this->dept1 == $this->dept2){
  121. $this->errors .= "<div class=\"alert alert-danger\">Error: departments are the same; nothing to convert</div>"
  122. ."<br /><br />"
  123. ."<a href=\"\" onclick=\"back(); return false;\">Back</a>";
  124. return True;
  125. }
  126. $account = \COREPOS\Fannie\API\member\MemberREST::get($this->cn);
  127. if ($account == false) {
  128. $this->errors .= "<div class=\"alert alert-success\">Error: no such member: ".$this->cn."</div>"
  129. ."<br /><br />"
  130. ."<a href=\"\" onclick=\"back(); return false;\">Back</a>";
  131. return True;
  132. }
  133. foreach ($account['customers'] as $c) {
  134. if ($c['accountHolder']) {
  135. $this->name1 = $c['firstName'] . ' ' . $c['lastName'];
  136. break;
  137. }
  138. }
  139. }
  140. return True;
  141. }
  142. function body_content(){
  143. if ($this->mode == 'init')
  144. return $this->form_content();
  145. elseif($this->mode == 'confirm')
  146. return $this->confirm_content();
  147. elseif($this->mode == 'finish')
  148. return $this->finish_content();
  149. }
  150. function confirm_content()
  151. {
  152. if (!empty($this->errors)) return $this->errors;
  153. $ret = "<form action=\"MemArEquityDumpTool.php\" method=\"post\">";
  154. $ret .= "<b>Confirm transactions</b>";
  155. $ret .= "<div class=\"alert alert-info\">";
  156. $ret .= sprintf("\$%.2f will be moved from %s to %s for Member #%d (%s)",
  157. $this->amount,$this->depts[$this->dept1],
  158. $this->dept2,$this->cn,$this->name1);
  159. $ret .= "</div><p>";
  160. $ret .= "<input type=\"hidden\" name=\"deptFrom\" value=\"{$this->dept1}\" />";
  161. $ret .= "<input type=\"hidden\" name=\"deptTo\" value=\"{$this->dept2}\" />";
  162. $ret .= "<input type=\"hidden\" name=\"amount\" value=\"{$this->amount}\" />";
  163. $ret .= "<input type=\"hidden\" name=\"card_no\" value=\"{$this->cn}\" />";
  164. $ret .= "<input type=\"hidden\" name=\"comment\" value=\"".FormLib::get_form_value('comment')."\" />";
  165. $ret .= "<button type=\"submit\" name=\"submit2\" value=\"Confirm\"
  166. class=\"btn btn-default\">Confirm</button>";
  167. $ret .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  168. $ret .= "<button type=\"buton\" class=\"btn btn-default\" onclick=\"back(); return false;\">Back</button>";
  169. $ret .= "</form>";
  170. return $ret;
  171. }
  172. function finish_content()
  173. {
  174. if (!empty($this->errors)) {
  175. return $this->errors;
  176. }
  177. $ret = '';
  178. $trans_no = DTrans::getTransNo($this->connection, $this->CORRECTION_CASHIER, $this->CORRECTION_LANE);
  179. $params = array(
  180. 'card_no' => $this->cn,
  181. 'register_no' => $this->CORRECTION_LANE,
  182. 'emp_no' => $this->CORRECTION_CASHIER,
  183. );
  184. DTrans::addOpenRing($this->connection, $this->dept1, -1*$this->amount, $trans_no, $params);
  185. DTrans::addOpenRing($this->connection, $this->dept2, $this->amount, $trans_no, $params);
  186. $comment = FormLib::get_form_value('comment');
  187. if (!empty($comment)) {
  188. $params = array(
  189. 'description' => $comment,
  190. 'trans_type' => 'C',
  191. 'trans_subtype' => 'CM',
  192. 'card_no' => $this->cn,
  193. 'register_no' => $this->CORRECTION_LANE,
  194. 'emp_no' => $this->CORRECTION_CASHIER,
  195. );
  196. DTrans::addItem($this->connection, $trans_no, $params);
  197. }
  198. $ret .= sprintf("Receipt #1: %s",$this->CORRECTION_CASHIER.'-'.$this->CORRECTION_LANE.'-'.$trans_no);
  199. return $ret;
  200. }
  201. function form_content()
  202. {
  203. if (!empty($this->errors)) return $this->errors;
  204. ob_start();
  205. ?>
  206. <form action="MemArEquityDumpTool.php" method="post">
  207. <div class="container">
  208. <div class="row form-group form-inline">
  209. <label>Remove</label>
  210. <div class="input-group">
  211. <span class="input-group-addon">$</span>
  212. <input type="text" name="amount" class="form-control"
  213. required />
  214. </div>
  215. <label>From</label>
  216. <select name="deptFrom" class="form-control">
  217. <?php
  218. foreach($this->depts as $k=>$v)
  219. echo "<option value=\"$k\">$v</option>";
  220. ?>
  221. </select>
  222. <label>And add to department #</label>
  223. <input type="number" name="deptTo" class="form-control"
  224. value="<?php echo $this->DEFAULT_DEPT; ?>" required />
  225. </div>
  226. <div class="row form-group form-inline">
  227. <label>Member #</label>
  228. <input type="number" name="card_no" class="form-control" required />
  229. </div>
  230. <div class="row form-group form-inline">
  231. <label>Comment</label>
  232. <?php $memNum = FormLib::get_form_value('memIN'); ?>
  233. <input type="text" name="comment" class="form-control" required
  234. value="<?php echo $memNum; ?>" />
  235. </div>
  236. <p>
  237. <button type="submit" name="submit1" value="Submit"
  238. class="btn btn-default">Submit</button>
  239. </p>
  240. </div>
  241. </form>
  242. <?php
  243. return ob_get_clean();
  244. }
  245. public function helpContent()
  246. {
  247. return '<p>
  248. Reduce a member account\'s equity or AR balance by
  249. the specified amount. Because the primary purpose is
  250. paying out equity, entering a positive $20 will
  251. reduce the member equity by $20.
  252. </p>
  253. <p>
  254. However, AR essentially runs backwards from equity.
  255. While an equity payment increases a member\'s equity
  256. balance, an AR payment reduces a member\'s AR
  257. balance. Entering a positive $20 for AR will actually
  258. increase the member balance by $20. Negative numbers
  259. are permitted and $-20 AR will reduce the member
  260. balance by $20.
  261. <p>
  262. The <em>add to department</em> is the second line of
  263. the transaction. The payment to equity or AR must
  264. be offset by an additional, opposite payment somewhere
  265. else or the books won\'t balance. A generic corrections
  266. or miscellaneous department may be a good fit.
  267. </p>';
  268. }
  269. public function unitTest($phpunit)
  270. {
  271. $this->errors = 'foo';
  272. $this->mode = 'init';
  273. $phpunit->assertEquals('foo', $this->body_content());
  274. $this->errors = '';
  275. $this->depts = array(1 => 'Dept');
  276. $phpunit->assertNotEquals(0, strlen($this->body_content()));
  277. $this->errors = 'foo';
  278. $this->mode = 'confirm';
  279. $phpunit->assertEquals('foo', $this->body_content());
  280. $this->errors = '';
  281. $this->amount = 1;
  282. $this->dept1 = 1;
  283. $this->dept2 = 2;
  284. $this->cn = 1;
  285. $this->name = 'JoeyJoeJoe';
  286. $phpunit->assertNotEquals(0, strlen($this->body_content()));
  287. }
  288. }
  289. FannieDispatch::conditionalExec();