Accounting.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ H2ZluzpPfZhz1mQiv795bx8ElEItbGgGVSGsjYQoYTU=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * accounting table service class
  14. *
  15. * theory of operation
  16. * - all listing related fees that have been debited from a user's account balance are saved in this table
  17. * -> listing setup fees grouped by listing id
  18. * -> sale transaction fees
  19. * -> other fees that have been debited against the user's balance (like automatic subscription renewals)
  20. * - credits made on the user's balance
  21. * -> admin balance adjustments
  22. * -> sale transactions refunds
  23. *
  24. * IMPORTANT: any payments are saved directly and only in the transactions table
  25. */
  26. namespace Ppb\Service;
  27. use Ppb\Db\Table\Accounting as AccountingTable,
  28. Cube\Db\Expr;
  29. class Accounting extends AbstractService
  30. {
  31. /**
  32. *
  33. * listing id
  34. * (for listing setup fees)
  35. *
  36. * @var integer
  37. */
  38. protected $_listingId = null;
  39. /**
  40. *
  41. * sale id
  42. * (for sale transaction fees)
  43. *
  44. * @var integer
  45. */
  46. protected $_saleId = null;
  47. /**
  48. *
  49. * user id
  50. *
  51. * @var integer
  52. */
  53. protected $_userId = null;
  54. /**
  55. *
  56. * gateway id
  57. *
  58. * @var integer
  59. */
  60. protected $_gatewayId = null;
  61. /**
  62. *
  63. * refund flag
  64. *
  65. * @var string
  66. */
  67. protected $_refundFlag = null;
  68. /**
  69. *
  70. * class constructor
  71. */
  72. public function __construct()
  73. {
  74. parent::__construct();
  75. $this->setTable(
  76. new AccountingTable());
  77. }
  78. /**
  79. *
  80. * get listing id
  81. *
  82. * @return integer
  83. */
  84. public function getListingId()
  85. {
  86. return $this->_listingId;
  87. }
  88. /**
  89. *
  90. * set listing id
  91. *
  92. * @param integer $listingId
  93. * @return $this
  94. */
  95. public function setListingId($listingId)
  96. {
  97. $this->_listingId = (int)$listingId;
  98. return $this;
  99. }
  100. /**
  101. *
  102. * set sale id
  103. *
  104. * @param int $saleId
  105. * @return $this
  106. */
  107. public function setSaleId($saleId)
  108. {
  109. $this->_saleId = $saleId;
  110. return $this;
  111. }
  112. /**
  113. *
  114. * get sale id
  115. *
  116. * @return int
  117. */
  118. public function getSaleId()
  119. {
  120. return $this->_saleId;
  121. }
  122. /**
  123. *
  124. * get user id
  125. *
  126. * @return integer
  127. */
  128. public function getUserId()
  129. {
  130. return $this->_userId;
  131. }
  132. /**
  133. *
  134. * set user id
  135. *
  136. * @param integer $userId
  137. * @return $this
  138. */
  139. public function setUserId($userId)
  140. {
  141. $this->_userId = (int)$userId;
  142. return $this;
  143. }
  144. /**
  145. *
  146. * get gateway id
  147. *
  148. * @return integer
  149. */
  150. public function getGatewayId()
  151. {
  152. return $this->_gatewayId;
  153. }
  154. /**
  155. *
  156. * set gateway id
  157. *
  158. * @param integer $gatewayId
  159. * @return $this
  160. */
  161. public function setGatewayId($gatewayId)
  162. {
  163. $this->_gatewayId = (int)$gatewayId;
  164. return $this;
  165. }
  166. /**
  167. *
  168. * set refund flag
  169. *
  170. * @param string $refundFlag
  171. * @return $this
  172. */
  173. public function setRefundFlag($refundFlag)
  174. {
  175. $this->_refundFlag = $refundFlag;
  176. return $this;
  177. }
  178. /**
  179. *
  180. * get refund flag
  181. *
  182. * @return string
  183. */
  184. public function getRefundFlag()
  185. {
  186. return $this->_refundFlag;
  187. }
  188. /**
  189. *
  190. * create or update a row in the accounting table
  191. * if amounts are positive, we have debit, and for negative amounts we have credit
  192. *
  193. * @param array $data
  194. * @return $this
  195. */
  196. public function save($data)
  197. {
  198. $row = null;
  199. $data = $this->_prepareSaveData($data);
  200. if ($data['amount'] != 0) {
  201. if (array_key_exists('id', $data)) {
  202. $select = $this->_table->select()
  203. ->where("id = ?", $data['id']);
  204. unset($data['id']);
  205. $row = $this->_table->fetchRow($select);
  206. }
  207. if (count($row) > 0) {
  208. $this->_table->update($data, "id='{$row['id']}'");
  209. }
  210. else {
  211. $data['created_at'] = new Expr('now()');
  212. $this->_table->insert($data);
  213. }
  214. }
  215. return $this;
  216. }
  217. /**
  218. *
  219. * save multiple table rows at once
  220. *
  221. * @param array $data
  222. * @return $this
  223. */
  224. public function saveMultiple(array $data)
  225. {
  226. foreach ($data as $row) {
  227. $this->save($row);
  228. }
  229. return $this;
  230. }
  231. /**
  232. *
  233. * prepare accounting data for when saving to the table
  234. *
  235. * @param array $data
  236. * @return array
  237. */
  238. protected function _prepareSaveData($data = array())
  239. {
  240. $data = parent::_prepareSaveData($data);
  241. if (!array_key_exists('user_id', $data) && $this->_userId !== null) {
  242. $data['user_id'] = $this->_userId;
  243. }
  244. if (!array_key_exists('refund_flag', $data) && $this->_refundFlag !== null) {
  245. $data['refund_flag'] = $this->_refundFlag;
  246. }
  247. if (!array_key_exists('listing_id', $data) && $this->_listingId !== null) {
  248. $data['listing_id'] = $this->_listingId;
  249. }
  250. if (!array_key_exists('sale_id', $data) && $this->_saleId !== null) {
  251. $data['sale_id'] = $this->_saleId;
  252. }
  253. if (!array_key_exists('gateway_id', $data) && $this->_gatewayId !== null) {
  254. $data['gateway_id'] = $this->_gatewayId;
  255. }
  256. return $data;
  257. }
  258. }