api.selling.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Selling management API
  4. */
  5. /**
  6. * Returns available selling lister with some controls
  7. *
  8. * @return string
  9. */
  10. function web_SellingLister() {
  11. $selling = zb_GetAllSellingData();
  12. $cells = wf_TableCell(__('ID'));
  13. $cells.= wf_TableCell(__('Selling name'));
  14. $cells.= wf_TableCell(__('Selling address'));
  15. $cells.= wf_TableCell(__('Selling geo data'));
  16. $cells.= wf_TableCell(__('Selling contact'));
  17. $cells.= wf_TableCell(__('Selling count cards'));
  18. $cells.= wf_TableCell(__('Selling comment'));
  19. $cells.= wf_TableCell(__('Actions'));
  20. $rows = wf_TableRow($cells, 'row1');
  21. if (!empty($selling)) {
  22. foreach ($selling as $row) {
  23. $cells = wf_TableCell($row['id']);
  24. $cells.= wf_TableCell($row['name']);
  25. $cells.= wf_TableCell($row['address']);
  26. $cells.= wf_TableCell($row['geo']);
  27. $cells.= wf_TableCell($row['contact']);
  28. $cells.= wf_TableCell($row['count_cards']);
  29. $cells.= wf_TableCell($row['comment']);
  30. $acts = wf_JSAlert('?module=selling&action=delete&id=' . $row['id'], web_delete_icon(), 'Removing this may lead to irreparable results') . ' ';
  31. $acts.= wf_JSAlert('?module=selling&action=edit&id=' . $row['id'], web_edit_icon(), 'Are you serious') . ' ';
  32. $cells.= wf_TableCell($acts);
  33. $rows.= wf_TableRow($cells, 'row3');
  34. }
  35. }
  36. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  37. return $result;
  38. }
  39. /**
  40. * Returns selling creation form
  41. *
  42. * @return string
  43. */
  44. function web_SellingCreateForm() {
  45. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  46. $inputs = wf_TextInput('new_selling[name]', __('Selling name') . $sup, '', true);
  47. $inputs.= wf_TextInput('new_selling[address]', __('Selling address'), '', true);
  48. $inputs.= wf_TextInput('new_selling[geo]', __('Selling geo data'), '', true, 20, 'geo');
  49. $inputs.= wf_TextInput('new_selling[contact]', __('Selling contact'), '', true);
  50. $inputs.= wf_TextArea('new_selling[comment]', __('Selling comment'), '', true);
  51. $inputs.= wf_Submit(__('Create'));
  52. $form = wf_Form('', 'POST', $inputs, 'glamour');
  53. return $form;
  54. }
  55. /**
  56. * Returns existing selling editing form
  57. *
  58. * @param int $sellingId
  59. *
  60. * @return string
  61. */
  62. function web_SellingEditForm($sellingId) {
  63. $data = zb_GetSellingData($sellingId);
  64. $sup = wf_tag('sup') . '*' . wf_tag('sup', true);
  65. $inputs = wf_TextInput('edit_selling[name]', __('Selling name') . $sup, $data['name'], true);
  66. $inputs.= wf_TextInput('edit_selling[address]', __('Selling address'), $data['address'], true);
  67. $inputs.= wf_TextInput('edit_selling[geo]', __('Selling geo data'), $data['geo'], true, 20 , 'geo');
  68. $inputs.= wf_TextInput('edit_selling[contact]', __('Selling contact'), $data['contact'], true);
  69. $inputs.= wf_TextArea('edit_selling[comment]', __('Selling comment'), $data['comment'], true);
  70. $inputs.= wf_Submit(__('Save'));
  71. $form = wf_Form('', 'POST', $inputs, 'glamour');
  72. $form.= wf_BackLink('?module=selling');
  73. return $form;
  74. }
  75. /**
  76. * Returns selling data from DB by its ID
  77. *
  78. * @param int $sellingId
  79. *
  80. * @return array
  81. */
  82. function zb_GetSellingData($sellingId) {
  83. $sellingId = vf($sellingId, 3);
  84. $query = sprintf("SELECT * from `selling` WHERE `id`='%s'", $sellingId);
  85. $city_data = simple_query($query);
  86. return $city_data;
  87. }
  88. /**
  89. * Creates new selling in database
  90. *
  91. * @param string $name
  92. * @param array $newSelling
  93. *
  94. * @return void
  95. */
  96. function zb_CreateSellingData($name, $newSelling) {
  97. foreach ($newSelling as $key => $field) {
  98. $newSelling[$key] = isset($field) ? mysql_real_escape_string($field) : null;
  99. }
  100. $address = '';
  101. $geo = '';
  102. $contact = '';
  103. $comment = '';
  104. extract($newSelling, EXTR_OVERWRITE);
  105. $query = sprintf(
  106. "INSERT INTO `selling` (`id`, `name`, `address`, `geo`, `contact`, `comment`) VALUES (NULL, '%s', '%s', '%s', '%s', '%s'); ", $name, $address, $geo, $contact, $comment
  107. );
  108. nr_query($query);
  109. log_register(sprintf('CREATE Selling `%s` `%s` `%s` `%s` `%s`', $name, $address, $geo, $contact, $comment));
  110. }
  111. /**
  112. * Returns all available selling full data
  113. *
  114. * @return array
  115. */
  116. function zb_GetAllSellingData() {
  117. $query = 'SELECT `sel`.`id` AS `id`, `sel`.`name` AS `name`, `sel`.`address` AS `address`, `sel`.`geo` AS `geo`, `sel`.`contact` AS `contact`, `sel`.`comment` AS `comment`, COUNT(`ca`.`id`) AS `count_cards`
  118. FROM `selling` AS `sel`
  119. LEFT JOIN `cardbank` AS `ca` ON `ca`.`selling_id` = `sel`.`id` AND `ca`.`active` = 1 AND `ca`.`used` = 0
  120. GROUP BY `sel`.`id` ORDER by `sel`.`id` ASC ;';
  121. $all_data = simple_queryall($query);
  122. return $all_data;
  123. }
  124. /**
  125. * Changes selling alias by its ID
  126. *
  127. * @param int $sellingId
  128. * @param array $editSelling
  129. *
  130. * @return void
  131. */
  132. function zb_UpdateSellingData($sellingId, $editSelling) {
  133. $sellingId = vf($sellingId, 3);
  134. foreach ($editSelling as $key => $field) {
  135. $editSelling[$key] = isset($field) ? mysql_real_escape_string($field) : null;
  136. }
  137. $name = '';
  138. $address = '';
  139. $geo = '';
  140. $contact = '';
  141. $comment = '';
  142. extract($editSelling, EXTR_OVERWRITE);
  143. $query = sprintf(
  144. "UPDATE `selling` SET `name` = '%s', `address` = '%s', `geo` = '%s', `contact` = '%s', `comment` = '%s' WHERE `id` = '%u'; ", $name, $address, $geo, $contact, $comment, $sellingId
  145. );
  146. nr_query($query);
  147. log_register(sprintf('UPDATE Selling [%u] `%s` `%s` `%s` `%s` `%s`', $sellingId, $name, $address, $geo, $contact, $comment));
  148. }
  149. /**
  150. * Deletes selling from database by its ID
  151. *
  152. * @param int $sellingId
  153. *
  154. * @return void
  155. */
  156. function zb_DeleteSellingData($sellingId) {
  157. $sellingId = vf($sellingId, 3);
  158. $query = sprintf("DELETE from `selling` WHERE `id` = '%u';", $sellingId);
  159. nr_query($query);
  160. log_register(sprintf('DELETE Selling [%u]', $sellingId));
  161. }
  162. /**
  163. * @return array|string
  164. */
  165. function zb_SelectAllSellingData() {
  166. $query = "SELECT * FROM `selling` ORDER BY `name` ASC";
  167. $allData = simple_queryall($query);
  168. $allData = !empty($allData) ? $allData : array();
  169. return $allData;
  170. }
  171. /**
  172. * @return array|string
  173. */
  174. function zb_BuilderSelectSellingData() {
  175. $select = zb_SelectAllSellingData();
  176. $allData[] = '';
  177. foreach ($select as $row) {
  178. $allData[$row['id']] = $row['name'];
  179. }
  180. return $allData;
  181. }
  182. /**
  183. * @param array $params
  184. *
  185. * @return array
  186. */
  187. function zb_SellingReport(array $params) {
  188. $queryCardId = '';
  189. if ($params['idfrom'] || $params['idto']) {
  190. if (empty($params['idfrom'])) {
  191. $params['idfrom'] = $params['idto'];
  192. }
  193. if (empty($params['idto'])) {
  194. $params['idto'] = $params['idfrom'];
  195. }
  196. $idFrom = mysql_real_escape_string($params['idfrom']);
  197. $idTo = mysql_real_escape_string($params['idto']);
  198. $queryCardId = sprintf("AND `ca`.`id` BETWEEN %s AND %s", $idFrom, $idTo);
  199. }
  200. $queryCardDate = '';
  201. if ($params['datefrom'] || $params['dateto']) {
  202. if (empty($params['datefrom'])) {
  203. $params['datefrom'] = $params['dateto'];
  204. }
  205. if (empty($params['dateto'])) {
  206. $params['dateto'] = $params['datefrom'];
  207. }
  208. $dateFrom = mysql_real_escape_string($params['datefrom']);
  209. $dateTo = mysql_real_escape_string($params['dateto']);
  210. $queryCardDate = sprintf("AND DATE(`ca`.`receipt_date`) BETWEEN STR_TO_DATE('%s', '%s') AND STR_TO_DATE('%s', '%s')", $dateFrom, '%Y-%m-%d %H:%i:%s', $dateTo, '%Y-%m-%d %H:%i:%s');
  211. }
  212. $querySellingIdWhere = '';
  213. if ($params['selling']) {
  214. $id = mysql_real_escape_string($params['selling']);
  215. $querySellingIdWhere = sprintf('WHERE `sel`.`id` = 1', $id);
  216. }
  217. $select = ' SELECT `sel`.`id` AS `id`, `sel`.`name` AS `name`,
  218. SUM(case when `ca`.`active`= 1 then `ca`.`cash` end) as `cash_total`,
  219. COUNT(case when `ca`.`active`= 1 then `ca`.`id` end) as `count_total`,
  220. SUM(case when `ca`.`active`= 1 AND `ca`.`used`= 1 then `ca`.`cash` end) as `cash_sel`,
  221. COUNT(case when `ca`.`active`= 1 AND `ca`.`used`= 1 then `ca`.`id` end) as `count_sel`,
  222. SUM(case when `ca`.`active`= 1 AND `ca`.`used`= 0 then `ca`.`cash` end) as `cash_balabce`,
  223. COUNT(case when `ca`.`active`= 1 AND `ca`.`used`= 0 then `ca`.`id` end) as `count_balance`
  224. FROM `selling` AS `sel`';
  225. $leftJoin = sprintf('LEFT JOIN `cardbank` AS `ca` ON `ca`.`selling_id` = `sel`.`id` %s %s', $queryCardId, $queryCardDate);
  226. $query = sprintf('%s %s %s GROUP BY `sel`.`id` ORDER by `sel`.`id` ASC ;', $select, $leftJoin, $querySellingIdWhere);
  227. $data = simple_queryall($query);
  228. return $data;
  229. }
  230. ?>