SalesListings.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ v+Yq3FVLV7w/XjjKm9CxccWTfneLD6nC9ap970xoQEo=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * sales listings table service class
  14. * creates/edits/removes sales listings
  15. * when editing or removing a sale listing, check if the corresponding row in the
  16. * sales table is empty, and if it is, remove that as well
  17. */
  18. namespace Ppb\Service\Table;
  19. use Ppb\Db\Table,
  20. Ppb\Service\Sales,
  21. Cube\Db\Expr;
  22. class SalesListings extends AbstractServiceTable
  23. {
  24. /**
  25. * download encryption key separator
  26. */
  27. const KEY_SEPARATOR = '|';
  28. /**
  29. *
  30. * sales table service class
  31. *
  32. * @var \Ppb\Service\Sales
  33. */
  34. protected $_sales = null;
  35. /**
  36. *
  37. * class constructor
  38. */
  39. public function __construct()
  40. {
  41. parent::__construct();
  42. $this->setTable(
  43. new Table\SalesListings());
  44. }
  45. /**
  46. *
  47. * get all table columns needed to generate the
  48. * sale invoices edit/combine form
  49. *
  50. * @return array
  51. */
  52. public function getColumns()
  53. {
  54. return array(
  55. array(
  56. 'label' => '',
  57. 'class' => 'size-tiny',
  58. 'element_id' => array(
  59. 'id', 'listing_id',
  60. ),
  61. ),
  62. array(
  63. 'label' => $this->_('Name'),
  64. 'element_id' => 'name',
  65. ),
  66. array(
  67. 'label' => $this->_('Price'),
  68. 'element_id' => 'price',
  69. 'class' => 'size-small',
  70. ),
  71. array(
  72. 'label' => $this->_('Quantity'),
  73. 'class' => 'size-small',
  74. 'element_id' => 'quantity',
  75. ),
  76. );
  77. }
  78. /**
  79. *
  80. * get all form elements that are needed to generate the
  81. * sale invoices edit/combine form
  82. *
  83. * @return array
  84. */
  85. public function getElements()
  86. {
  87. return array(
  88. array(
  89. 'id' => 'id', // sale listing id
  90. 'element' => 'hidden',
  91. ),
  92. array(
  93. 'id' => 'listing_id',
  94. 'element' => 'hidden',
  95. ),
  96. array(
  97. 'id' => 'name',
  98. 'element' => 'description',
  99. ),
  100. array(
  101. 'id' => 'price',
  102. 'element' => 'text',
  103. 'attributes' => array(
  104. 'class' => 'form-control input-mini',
  105. ),
  106. ),
  107. array(
  108. 'id' => 'quantity',
  109. 'element' => 'text',
  110. 'attributes' => array(
  111. 'class' => 'form-control input-mini',
  112. ),
  113. ),
  114. );
  115. }
  116. /**
  117. *
  118. * get sales table service class
  119. *
  120. * @return \Ppb\Service\Sales
  121. */
  122. public function getSales()
  123. {
  124. if (!$this->_sales instanceof Sales) {
  125. $this->setSales();
  126. }
  127. return $this->_sales;
  128. }
  129. /**
  130. *
  131. * set sales table service class
  132. *
  133. * @param \Ppb\Service\Sales $sales
  134. * @return \Ppb\Service\Table\SalesListings
  135. */
  136. public function setSales(Sales $sales = null)
  137. {
  138. if (!$sales instanceof Sales) {
  139. $sales = new Sales();
  140. }
  141. $this->_sales = $sales;
  142. return $this;
  143. }
  144. /**
  145. *
  146. * create or edit a sale listing.
  147. * - when editing, if changing "sale_id", check if there are other listings
  148. * corresponding to that "sale_id", and if not, remove the row in the "sales" table
  149. *
  150. * @param array $data
  151. * @return \Ppb\Service\Table\SalesListings
  152. * @throws \InvalidArgumentException
  153. */
  154. public function save(array $data)
  155. {
  156. $saleListing = null;
  157. $data = $this->_prepareSaveData($data);
  158. if (empty($data['id']) && (empty($data['sale_id']) || empty($data['listing_id']))) {
  159. throw new \InvalidArgumentException("A sale id and a listing id are required
  160. when adding/editing a row from the sales listings table.");
  161. }
  162. if (array_key_exists('id', $data)) {
  163. $saleListing = $this->findBy('id', $data['id']);
  164. unset($data['id']);
  165. }
  166. if (count($saleListing) > 0) {
  167. $saleId = $saleListing['sale_id'];
  168. $saleListing->save($data);
  169. $this->_deleteEmptySale($saleId);
  170. }
  171. else {
  172. $data['created_at'] = new Expr('now()');
  173. $this->_table->insert($data);
  174. }
  175. return $this;
  176. }
  177. /**
  178. *
  179. * fetches all matched rows
  180. *
  181. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  182. * @param string|array $order
  183. * @param int $count
  184. * @param int $offset
  185. * @return \Ppb\Db\Table\Rowset\SalesListings
  186. */
  187. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  188. {
  189. if ($order === null) {
  190. $order = 'sale_id ASC, created_at ASC';
  191. }
  192. return parent::fetchAll($where, $order, $count, $offset);
  193. }
  194. /**
  195. *
  196. * delete data from the sales listings table
  197. *
  198. * @param array $id
  199. * @param string|null $userToken
  200. * @return integer|false returns the number of affected rows or false if the deletion was not completed
  201. */
  202. public function deleteOne($id, $userToken = null)
  203. {
  204. if ($userToken !== null) {
  205. $saleListing = $this->findBy('id', $id);
  206. if (count($saleListing) > 0) {
  207. $select = $this->_table->select()
  208. ->where('pending = ?', 1)
  209. ->where('user_token = ?', $userToken);
  210. $sale = $saleListing->findParentRow('\Ppb\Db\Table\Sales', null, $select);
  211. if (!$sale) {
  212. return false;
  213. }
  214. }
  215. else {
  216. return false;
  217. }
  218. }
  219. return $this->delete(array($id));
  220. }
  221. /**
  222. *
  223. * delete one or more rows from the sale listings table
  224. *
  225. * if the parent sale has no more sales listings, delete the sale as well.
  226. *
  227. * @param array $data
  228. * @return int
  229. */
  230. public function delete(array $data)
  231. {
  232. $result = 0;
  233. $data = array_filter(
  234. array_values($data));
  235. $salesListings = $this->_table->fetchAll(
  236. $this->_table->select()
  237. ->where('id IN (?)', implode(',', $data))
  238. );
  239. /** @var \Ppb\Db\Table\Row\SaleListing $saleListing */
  240. foreach ($salesListings as $saleListing) {
  241. $result += $saleListing->delete();
  242. }
  243. return $result;
  244. }
  245. /**
  246. *
  247. * check if the sale having the id $saleId contains listings, and if not, delete it
  248. *
  249. * @param int $saleId
  250. * @return $this
  251. */
  252. protected function _deleteEmptySale($saleId)
  253. {
  254. /** @var \Ppb\Db\Table\Row\Sale $sale */
  255. $sale = $this->getSales()
  256. ->findBy('id', $saleId);
  257. if (!$sale->countDependentRowset('\Ppb\Db\Table\SalesListings')) {
  258. $sale->delete(true);
  259. }
  260. return $this;
  261. }
  262. }