DealsSales.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. Lib::loadClass('ProcesHelper');
  4. Lib::loadClass('Typespecial');
  5. class Route_DealsSales extends RouteBase {
  6. public function handleAuth() {
  7. if (!User::logged()) {
  8. throw new HttpException('Unauthorized', 401);
  9. }
  10. }
  11. public function defaultAction() {
  12. SE_Layout::gora();
  13. SE_Layout::menu();
  14. $this->legacy_DEALS_SALES();
  15. SE_Layout::dol();
  16. }
  17. public function typespecialAction() {
  18. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  19. header("Content-type: application/json");
  20. $fld = V::get('fld', '', $_GET);
  21. switch ($fld) {
  22. case 'id_telboxes': {
  23. Lib::loadClass('TypespecialVariable');
  24. $typeSpecialTelboxes = TypespecialVariable::getInstance(-1, '__DEALS_SALES_TELBOXES_NAME');
  25. $query = V::get('q', '', $_REQUEST);
  26. $rawRows = null;
  27. $rows = $typeSpecialTelboxes->getValuesWithExports($query);
  28. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">rows('.$query.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'</pre>';}
  29. foreach ($rows as $kID => $vItem) {
  30. $itemJson = new stdClass();
  31. $itemJson->id = $vItem->id;
  32. $itemJson->name = $vItem->param_out;
  33. if (!empty($vItem->exports)) {
  34. $itemJson->exports = $vItem->exports;
  35. }
  36. $jsonData[] = $itemJson;
  37. }
  38. echo json_encode($jsonData);
  39. }
  40. break;
  41. case 'id_companies': {
  42. Lib::loadClass('TypespecialVariable');
  43. $typeSpecialCompanies = TypespecialVariable::getInstance(-1, '__COMPANIES');
  44. $query = V::get('q', '', $_REQUEST);
  45. $rawRows = null;
  46. $rows = $typeSpecialCompanies->getValuesWithExports($query);
  47. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">rows('.$query.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($rows);echo'</pre>';}
  48. foreach ($rows as $kID => $vItem) {
  49. $itemJson = new stdClass();
  50. $itemJson->id = $vItem->id;
  51. $itemJson->name = $vItem->param_out;
  52. if (!empty($vItem->exports)) {
  53. $itemJson->exports = $vItem->exports;
  54. }
  55. $jsonData[] = $itemJson;
  56. }
  57. echo json_encode($jsonData);
  58. }
  59. break;
  60. }
  61. }
  62. public function getDataAjaxAction() {
  63. header("Content-type: application/json");
  64. $response = new stdClass();
  65. $response->msg = 'TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__;
  66. echo json_encode($response);
  67. }
  68. public function inlineEditAjaxAction() {
  69. //header("Content-type: application/json");
  70. $response = new stdClass();
  71. $response->msg = 'TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__;// TODO: DBG
  72. $response->_request = $_REQUEST;// TODO: DBG
  73. $year = V::get('year', 0, $_REQUEST, 'int');
  74. $month = V::get('month', 0, $_REQUEST, 'int');
  75. $telbox = V::get('telbox', '', $_REQUEST, 'word');
  76. if ($year <= 0) throw new Exception('Error: wrong year');
  77. if ($month <= 0 || $month > 12) throw new Exception('Error: wrong month');
  78. if (empty($telbox)) throw new Exception('Error: wrong telbox');
  79. $record = $this->_fetchSaleByParams($telbox, $year, $month);
  80. if (empty($record)) {
  81. return $this->_renderInlineCreateForm($telbox, $year, $month);
  82. } else {
  83. return $this->_renderInlineEditForm($record);
  84. }
  85. }
  86. public function saveInlineAjaxAction() {
  87. header("Content-type: application/json");
  88. $response = new stdClass();
  89. $response->msg = 'TODO: F. ' . __FUNCTION__ . ' L.' . __LINE__;
  90. $response->_request = $_REQUEST;// TODO: DBG
  91. $id = V::get('ID', 0, $_REQUEST, 'int');
  92. if ($id > 0) {
  93. return $this->_saveInlineEdit($id, $_REQUEST);
  94. } else {
  95. return $this->_saveInlineCreate($_REQUEST);
  96. }
  97. }
  98. private function _saveInlineEdit($id, $args) {
  99. $record = $this->_fetchSaleById($id);
  100. if (empty($record)) throw new HttpException("Record {$id} not exists", 404);
  101. DBG::_('DBG', '>2', "record", $record, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
  102. DBG::_('DBG', '>2', "args", $args, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
  103. $tblAcl = $this->_getTableAcl();
  104. $primaryKeyFieldName = 'ID';
  105. $primaryKey = V::get($primaryKeyFieldName, 0, $record);
  106. $itemPatch = new stdClass();
  107. $itemPatch->{$primaryKeyFieldName} = $primaryKey;
  108. $visibleCols = array('SALES_VALUE', 'marka', 'A_STATUS');
  109. foreach ($visibleCols as $fieldName) {
  110. $fieldID = $tblAcl->getFieldIdByName($fieldName);
  111. if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
  112. if (!$tblAcl->isAllowed($fieldID, 'W', $record)) {
  113. throw new Exception("Brak uprawnień do zapisu ({$fieldName})");
  114. }
  115. $argsFieldName = "f{$fieldID}";
  116. if (!isset($args[$argsFieldName])) throw new Exception("Brak danych dla pola ({$fieldName})");
  117. $itemPatch->{$fieldName} = $args[$argsFieldName];
  118. }
  119. DBG::_('DBG', '>1', "TODO: save record", $itemPatch, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
  120. $response = new stdClass();
  121. try {
  122. $affected = $tblAcl->updateItem($itemPatch);
  123. if ($affected > 0) {
  124. $response->type = 'success';
  125. $response->msg = "Rekord zapisany pomyślnie";//"Record saved successfully";
  126. } else if ($affected == 0) {
  127. $response->type = 'info';
  128. $response->msg = "Nie wprowadzono żadnych zmian";
  129. }
  130. $response->record = $tblAcl->getItem($primaryKey);
  131. }
  132. catch (Exception $e) {
  133. $response->type = 'error';
  134. $response->msg = $e->getMessage();
  135. }
  136. echo json_encode($response);
  137. }
  138. private function _saveInlineCreate($args) {
  139. $year = V::get('SALES_YEAR', 0, $args, 'int');
  140. $month = V::get('SALES_MONTH', 0, $args, 'int');
  141. $telbox = V::get('T_TELBOX_NEIGHBOUR_IN', '', $args, 'word');
  142. if ($year <= 0) throw new Exception('Error: wrong year');
  143. if ($month <= 0 || $month > 12) throw new Exception('Error: wrong month');
  144. if (empty($telbox)) throw new Exception('Error: wrong telbox');
  145. $tblAcl = $this->_getTableAcl();
  146. $primaryKeyFieldName = 'ID';
  147. $primaryKey = V::get($primaryKeyFieldName, 0, $record);
  148. $item = new stdClass();//$tblAcl->convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f');
  149. $item->T_TELBOX_NEIGHBOUR_IN = $telbox;
  150. $item->SALES_YEAR = $year;
  151. $item->SALES_MONTH = $month;
  152. $visibleCols = array('SALES_VALUE', 'marka');
  153. foreach ($visibleCols as $fieldName) {
  154. $fieldID = $tblAcl->getFieldIdByName($fieldName);
  155. if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
  156. if (!$tblAcl->isAllowed($fieldID, 'C', $record)) {
  157. throw new Exception("Brak uprawnień do utworzenia ({$fieldName})");
  158. }
  159. $argsFieldName = "f{$fieldID}";
  160. if (!isset($args[$argsFieldName])) throw new Exception("Brak danych dla pola ({$fieldName})");
  161. $item->{$fieldName} = $args[$argsFieldName];
  162. }
  163. try {
  164. $createdId = $tblAcl->addItem($item);
  165. if ($createdId) {
  166. $response->type = 'success';
  167. $response->msg = "Utworzono pomyślnie rekord nr {$createdId}";
  168. $response->id = $createdId;
  169. $response->record = $tblAcl->getItem($createdId);
  170. }
  171. else {
  172. $response->type = 'error';
  173. $response->msg = "Nie udało się utworzyć nowego rekordu!";
  174. }
  175. }
  176. catch (Exception $e) {
  177. $response->type = 'error';
  178. $response->msg = "Wystąpiły błędy!";
  179. $response->msg = $e->getMessage();
  180. }
  181. echo json_encode($response);
  182. }
  183. private function _fetchSaleByParams($telbox, $year, $month) {
  184. $record = null;
  185. $db = DB::getDB();
  186. $sqlWhere = array();
  187. $sqlWhere[] = " o.`A_STATUS`!='DELETED' ";
  188. $sqlIdTelboxes = $db->_($telbox);
  189. $sqlWhere[] = " o.`T_TELBOX_NEIGHBOUR_IN`='{$sqlIdTelboxes}' ";
  190. $sqlWhere[] = " o.`SALES_YEAR`='{$year}' ";
  191. $sqlWhere[] = " o.`SALES_MONTH`='{$month}' ";
  192. $sqlWhere = (!empty($sqlWhere))? implode(" and ", $sqlWhere) : '';
  193. $sql = "select o.`ID`
  194. , o.`T_TELBOX_NEIGHBOUR_IN`
  195. , o.`ID_DEALS_TABLE`
  196. , o.`SALES_YEAR`
  197. , o.`SALES_MONTH`
  198. , o.`SALES_VALUE`
  199. , o.`marka`
  200. , o.`A_STATUS`
  201. from `DEALS_SALES` as o
  202. where
  203. {$sqlWhere}
  204. ";
  205. DBG::_('DBG_SQL', '>1', "sql:fetch({telbox:{$telbox},year:{$year},month:{$month}})", $sql, __CLASS__, __FUNCTION__, __LINE__);
  206. $res = $db->query($sql);
  207. while ($r = $db->fetch($res)) {
  208. $record = $r;
  209. }
  210. return $record;
  211. }
  212. private function _fetchSaleById($idSale) {
  213. $record = null;
  214. $db = DB::getDB();
  215. $sqlWhere = array();
  216. $sqlWhere[] = " o.`A_STATUS`!='DELETED' ";
  217. $sqlWhere[] = " o.`ID`='{$idSale}' ";
  218. $sqlWhere = (!empty($sqlWhere))? implode(" and ", $sqlWhere) : '';
  219. $sql = "select o.`ID`
  220. , o.`T_TELBOX_NEIGHBOUR_IN`
  221. , o.`ID_DEALS_TABLE`
  222. , o.`SALES_YEAR`
  223. , o.`SALES_MONTH`
  224. , o.`SALES_VALUE`
  225. , o.`marka`
  226. , o.`A_STATUS`
  227. from `DEALS_SALES` as o
  228. where
  229. {$sqlWhere}
  230. ";
  231. DBG::_('DBG_SQL', '>1', "sql:fetch({telbox:{$telbox},year:{$year},month:{$month}})", $sql, __CLASS__, __FUNCTION__, __LINE__);
  232. $res = $db->query($sql);
  233. while ($r = $db->fetch($res)) {
  234. $record = $r;
  235. }
  236. return $record;
  237. }
  238. private function _renderInlineCreateForm($telbox, $year, $month) {
  239. //DBG::_(true, true, "TODO: Edit Form for record", $record, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
  240. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  241. header("Content-type: text/plain");
  242. $row = $record;// TODO: refactor
  243. $tblAcl = $this->_getTableAcl();
  244. $visibleCols = array('SALES_VALUE', 'marka');
  245. $hasTypeSpecial = false;
  246. foreach ($visibleCols as $fieldName) {
  247. $fieldID = $tblAcl->getFieldIdByName($fieldName);
  248. if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
  249. $fieldVal = ($tblAcl->isAllowed($fieldID, 'R', $row))? V::get($fieldName, '', $row) : '*****';
  250. $fieldVal = V::get("f{$fieldID}", $fieldVal, $_POST);
  251. $vCol = $tblAcl->getField($fieldID);
  252. $vCol['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
  253. $tsValues = array();
  254. $typeSpecial = Typespecial::getInstance($fieldID, $vCol['name']);
  255. if ($typeSpecial) {
  256. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($typeSpecial);echo'</pre>';}
  257. $specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
  258. if($DBG){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">Typespecial('.$fieldID.') specialValues (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($specialValues);echo'</pre>';}
  259. if (!empty($specialValues)) {
  260. $tsValues[$row->ID] = implode('<br>', $specialValues);
  261. }
  262. }
  263. ?>
  264. <label for="<?php echo "f{$fieldID}"; ?>" class="AjaxTableEdit-label">
  265. <strong title="<?php echo "[{$fieldID}] {$fieldName}"; ?>"><?php echo "{$vCol['label']}"; ?></strong>
  266. <?php if (!empty($vCol['opis'])) : ?>
  267. <em><?php echo $vCol['opis']; ?></em>
  268. <?php $perms = $tblAcl->getFieldPerms($fieldID); SE_Layout::hotKeyDBG($perms); ?>
  269. <?php endif; ?>
  270. </label>
  271. <?php
  272. $fieldParams = array('widthClass'=>'inside-modal', 'maxGrid'=>6);
  273. if (!empty($tsValues[$row->ID])) {
  274. $fieldParams['typespecialValue'] = $tsValues[$row->ID];
  275. }
  276. $vDefault = $tblAcl->getColDefault($fieldName);
  277. if (!empty($vDefault)) {
  278. $fieldParams['default'] = $vDefault;
  279. }
  280. echo $tblAcl->showFormItem('W', $fieldID, "f{$fieldID}", $fieldVal, $fieldParams, $row);
  281. if ($typeSpecial) $hasTypeSpecial = true;
  282. }
  283. if ($hasTypeSpecial) {
  284. echo '<p style="padding:100px 0;"></p>';
  285. }
  286. ?>
  287. <input type="hidden" name="T_TELBOX_NEIGHBOUR_IN" value="<?php echo $telbox; ?>" />
  288. <input type="hidden" name="SALES_YEAR" value="<?php echo $year; ?>" />
  289. <input type="hidden" name="SALES_MONTH" value="<?php echo $month; ?>" />
  290. <?php
  291. exit;
  292. }
  293. private function _renderInlineEditForm($record) {
  294. //DBG::_(true, true, "TODO: Edit Form for record", $record, __CLASS__, __FUNCTION__, __LINE__);// TODO: DBG
  295. $DBG = ('1' == V::get('DBG', '', $_REQUEST));
  296. header("Content-type: text/plain");
  297. $row = $record;// TODO: refactor
  298. $tblAcl = $this->_getTableAcl();
  299. // TODO: $cols = array('T_TELBOX_NEIGHBOUR_IN','SALES_YEAR','SALES_MONTH','SALES_VALUE','marka');
  300. // TODO: form for only 'SALES_VALUE' and eventually 'marka'
  301. $visibleCols = array('SALES_VALUE', 'marka', 'A_STATUS');
  302. $hasTypeSpecial = false;
  303. foreach ($visibleCols as $fieldName) {
  304. $fieldID = $tblAcl->getFieldIdByName($fieldName);
  305. if (!$fieldID) throw new Exception("No field by name ({$fieldName})");
  306. $fieldVal = ($tblAcl->isAllowed($fieldID, 'R', $row))? V::get($fieldName, '', $row) : '*****';
  307. $fieldVal = V::get("f{$fieldID}", $fieldVal, $_POST);
  308. $vCol = $tblAcl->getField($fieldID);
  309. $vCol['label'] = (!empty($vCol['label']))? $vCol['label'] : $vCol['name'];
  310. $tsValues = array();
  311. $typeSpecial = Typespecial::getInstance($fieldID, $vCol['name']);
  312. if ($typeSpecial) {
  313. $specialValues = $typeSpecial->getEditSelectedValuesByIds($this->_zasobID, $row->ID, $fieldName, V::get($fieldName, $fieldVal, $row));
  314. if (!empty($specialValues)) {
  315. $tsValues[$row->ID] = implode('<br>', $specialValues);
  316. }
  317. }
  318. ?>
  319. <label for="<?php echo "f{$fieldID}"; ?>" class="AjaxTableEdit-label">
  320. <strong title="<?php echo "[{$fieldID}] {$fieldName}"; ?>"><?php echo "{$vCol['label']}"; ?></strong>
  321. <?php if (!empty($vCol['opis'])) : ?>
  322. <em><?php echo $vCol['opis']; ?></em>
  323. <?php $perms = $tblAcl->getFieldPerms($fieldID); SE_Layout::hotKeyDBG($perms); ?>
  324. <?php endif; ?>
  325. </label>
  326. <?php
  327. $fieldParams = array('widthClass'=>'inside-modal', 'maxGrid'=>6);
  328. if (!empty($tsValues[$row->ID])) {
  329. $fieldParams['typespecialValue'] = $tsValues[$row->ID];
  330. }
  331. $vDefault = $tblAcl->getColDefault($fieldName);
  332. if (!empty($vDefault)) {
  333. $fieldParams['default'] = $vDefault;
  334. }
  335. echo $tblAcl->showFormItem('W', $fieldID, "f{$fieldID}", $fieldVal, $fieldParams, $row);
  336. if ($typeSpecial) $hasTypeSpecial = true;
  337. }
  338. if ($hasTypeSpecial) {
  339. echo '<p style="padding:100px 0;"></p>';
  340. }
  341. ?>
  342. <input type="hidden" name="ID" value="<?php echo $record->ID; ?>" />
  343. <?php
  344. exit;
  345. }
  346. private function _getTableAcl() {
  347. $zasobObj = ProcesHelper::getZasobTableInfoByUri('default_db/deals_sales');
  348. if (!$zasobObj) throw new Exception("Zasob tabela deals_sales nie istnieje.");
  349. $userAcl = User::getAcl();
  350. $userAcl->fetchGroups();
  351. if (!$userAcl->hasTableAcl($zasobObj->ID)) throw new Exception("Brak uprawnień do tabeli ");
  352. $tblAcl = $userAcl->getTableAcl($zasobObj->ID);
  353. return $tblAcl;
  354. }
  355. public function legacy_DEALS_SALES() {
  356. $id_telboxes = V::get('id_telboxes', '', $_REQUEST);
  357. $id_companies = V::get('id_companies', '', $_REQUEST, 'int');
  358. $idTelboxes = $id_telboxes;
  359. if (!empty($idTelboxes)) {
  360. if (!empty($idTelboxes)) {
  361. if (false !== strpos($idTelboxes, ': ')) {
  362. $parts = explode(': ', $idTelboxes, 2);
  363. $idTelboxes = $parts[0];
  364. }
  365. }
  366. }
  367. SE_Layout::menu();
  368. Lib::loadClass('TypespecialVariable');
  369. $typeSpecialTelboxes = TypespecialVariable::getInstance(-1, '__DEALS_SALES_TELBOXES_NAME');
  370. $typeSpecialCompanies = null;// TypespecialVariable::getInstance(-1, '__COMPANIES');
  371. $obroty = array();
  372. $obrotyByYear = array();
  373. $marki = array();
  374. $obrotyToMarki = array();
  375. if (!empty($idTelboxes)) {
  376. $db = DB::getDB();
  377. $sqlWhere = array();
  378. $sqlWhere[] = " o.`A_STATUS`!='DELETED' ";
  379. $sqlIdTelboxes = $db->_($idTelboxes);
  380. $sqlWhere[] = " o.`T_TELBOX_NEIGHBOUR_IN`='{$sqlIdTelboxes}' ";
  381. $sqlWhere = (!empty($sqlWhere))? implode(" and ", $sqlWhere) : '';
  382. $sql = "select o.`ID`
  383. -- , d.`T_TELBOX_NEIGHBOUR_IN`
  384. , o.`T_TELBOX_NEIGHBOUR_IN`
  385. , o.`ID_DEALS_TABLE`
  386. -- , o.`SALES_DATE`
  387. , o.`SALES_YEAR`
  388. , o.`SALES_MONTH`
  389. , o.`SALES_VALUE`
  390. , o.`marka`
  391. from `DEALS_SALES` as o
  392. -- join `DEALS_TABLE` as d on (d.`ID`=o.`ID_DEALS_TABLE`)
  393. where
  394. {$sqlWhere}
  395. ";
  396. if(V::get('DBG','',$_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  397. $res = $db->query($sql);
  398. while ($r = $db->fetch($res)) {
  399. $year = $r->SALES_YEAR;
  400. $month = sprintf("%02d", $r->SALES_MONTH);
  401. $obroty[$r->ID_DEALS_TABLE][$year][$month] = $r->SALES_VALUE;
  402. $obrotyByYear[$year][$month] = floatval($r->SALES_VALUE);
  403. $marki[$r->marka] = true;
  404. $obrotyToMarki[$year][$month] = $r->marka;
  405. }
  406. }
  407. krsort($obrotyByYear);
  408. //DBG::_(true, true, "obrotyByYear", $obrotyByYear, __CLASS__, __FUNCTION__, __LINE__);
  409. //DBG::_('DBG', '>0', "marki", $marki, __CLASS__, __FUNCTION__, __LINE__);
  410. //DBG::_('DBG', '>0', "obrotyToMarki", $obrotyToMarki, __CLASS__, __FUNCTION__, __LINE__);
  411. $markiOut = array();
  412. {
  413. $markiLabels = array('primary', 'success', 'warning', 'danger', 'default');
  414. // <span class="label label-default">Default</span>
  415. reset($markiLabels);
  416. foreach ($marki as $marka => $vBool) {
  417. $markiOut[$marka] = current($markiLabels);
  418. if (!next($markiLabels)) reset($markiLabels);
  419. }
  420. }// Default Primary Success Info Warning Danger
  421. $monthsOut = array();
  422. for ($i = 0; $i < 12; $i++) $monthsOut[] = sprintf("%02d", $i + 1);
  423. $initData = new stdClass();
  424. $initData->obroty = $obrotyByYear;
  425. $initData->marki = $markiOut;
  426. $initData->obrotyToMarki = $obrotyToMarki;
  427. $initData->months = $monthsOut;
  428. $initData->years = array_keys($obrotyByYear);
  429. //DBG::_(true, true, "initData", $initData, __CLASS__, __FUNCTION__, __LINE__);
  430. ?>
  431. <style type="text/css">
  432. .cell_marka_primary { background-color: #d9edf7; }
  433. .cell_marka_success { background-color: #dff0d8; }
  434. .cell_marka_warning { background-color: #fcf8e3; }
  435. .cell_marka_danger { background-color: #f2dede; }
  436. .cell_marka_default { background-color: #f5f5f5; }
  437. </style>
  438. <div class="container-fluid">
  439. <p></p>
  440. <form action="" method="GET" class="form-inline">
  441. <input type="hidden" name="_route" value="DealsSales">
  442. <table>
  443. <!--
  444. <summary>Wybierz lokal i/lub firmę w celu przeglądania obrotów </summary>
  445. -->
  446. <thead>
  447. <tr>
  448. <th style="text-align:left">Wyszukaj lokal lub markę</th>
  449. <th style="text-align:left">
  450. <?php if ($typeSpecialCompanies) : ?>
  451. Firma
  452. <?php endif; ?>
  453. </th>
  454. </tr>
  455. </thead>
  456. <tbody>
  457. <tr>
  458. <td>
  459. <?php if ($typeSpecialTelboxes) : ?>
  460. <?php
  461. $fName = 'id_telboxes';
  462. $fldParams = array();
  463. $fldParams['allowCreate'] = false;
  464. $fldParams['ajaxDataUrlBase'] = "index.php?_route=DealsSales&_task=typespecial&fld={$fName}";
  465. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  466. echo $typeSpecialTelboxes->showFormItem($tblID = -1, $fName, $selValue = $id_telboxes, $fldParams);
  467. ?>
  468. <?php endif; ?>
  469. </td>
  470. <td>
  471. <?php if ($typeSpecialCompanies) : ?>
  472. <?php
  473. $fName = 'id_companies';
  474. $fldParams = array();
  475. $fldParams['allowCreate'] = false;
  476. $fldParams['ajaxDataUrlBase'] = "index.php?_route=DealsSales&_task=typespecial&fld={$fName}";
  477. //$fldParams['ajaxDataUrlBase'] .= "&DBG_TS=3";
  478. echo $typeSpecialCompanies->showFormItem($tblID = -1, $fName, $selValue = $id_companies, $fldParams);
  479. ?>
  480. <?php endif; ?>
  481. </td>
  482. </tr>
  483. <tr>
  484. <td clspan="2">
  485. <button class="btn">wybierz</button>
  486. </td>
  487. </tr>
  488. </tbody>
  489. </table>
  490. </form>
  491. <?php if (empty($obroty)) : ?>
  492. <?php else : ?>
  493. <hr>
  494. <div id="dealsSalesByMonth"></div>
  495. <script>
  496. (function($, undefined) {
  497. var DealsSalesTable = function () {
  498. var priv = {};// private api
  499. var publ = {};// public api
  500. priv.options = {};
  501. var defaults = {
  502. url: '',
  503. urlInlineEdit: '',
  504. urlSaveInlineEdit: '',
  505. initData: {},
  506. debug: false
  507. },
  508. _uiNodeCont = null,// container node
  509. _uiNode$Table = null,
  510. _inlineEditBox$Node = null,
  511. _state = {};// state - to replace variables below (date, ...)
  512. priv.init = function () {
  513. _uiNodeCont = priv.options.id;
  514. _state = {};// init state
  515. priv.initEvents();
  516. priv.initData();
  517. priv.initialRender();// set up _uiNode$...
  518. };
  519. priv.initEvents = function () {
  520. jQuery(_uiNodeCont).on('DealsSalesTable:render', priv.onRender);
  521. };
  522. priv.initData = function () {
  523. if (!priv.options.initData) return;
  524. if (priv.options.initData.obroty) _state.obroty = priv.options.initData.obroty;
  525. if (priv.options.initData.marki) _state.marki = priv.options.initData.marki;
  526. if (priv.options.initData.obrotyToMarki) _state.obrotyToMarki = priv.options.initData.obrotyToMarki;
  527. if (priv.options.initData.months) _state.months = priv.options.initData.months;
  528. if (priv.options.initData.years) _state.years = priv.options.initData.years;
  529. };
  530. priv.initialRender = function () {
  531. _uiNode$Marki = $('<table class="table table-bordered" style="width:auto"></table>');
  532. $('<tbody><tr><th>Marki:</th></tr></tbody>').appendTo(_uiNode$Marki);
  533. _uiNode$Marki.appendTo(_uiNodeCont);
  534. _uiNode$Table = $('<table class="table table-bordered table-hover"></table>');
  535. {// thead
  536. var thead$Node = $('<thead></thead>').appendTo(_uiNode$Table);
  537. var theadTr$Node = $('<tr></tr>').appendTo(thead$Node);
  538. $('<th></th>').appendTo(theadTr$Node);
  539. $.each(_state.months, function(index, value) {
  540. $('<th>' + value + '</th>').appendTo(theadTr$Node);
  541. });
  542. $('<th>suma</th>').appendTo(theadTr$Node);
  543. }
  544. $('<tbody></tbody>').appendTo(_uiNode$Table);
  545. _uiNode$Table.appendTo(_uiNodeCont);
  546. _uiNode$GraphByYear = $('<div style="width:100%; margin:10px 0; border:1px solid gray; overflow:auto"></div>');
  547. $('<canvas id="wykres03" width="1760" height="300">[No canvas support]</canvas>').appendTo(_uiNode$GraphByYear);
  548. _uiNode$GraphByYear.appendTo(_uiNodeCont);
  549. _uiNode$GraphByMonth = $('<div style="width:100%; margin:10px 0; border:1px solid gray; overflow:auto"></div>');
  550. $('<canvas id="wykres04" width="1760" height="300">[No canvas support]</canvas>').appendTo(_uiNode$GraphByMonth);
  551. _uiNode$GraphByMonth.appendTo(_uiNodeCont);
  552. priv.renderHeaderMarki();
  553. priv.renderTableBody();
  554. priv.renderGraphByYear();
  555. priv.renderGraphByMonth();
  556. priv.renderInitInlineEditBox();// .dealsSales__inlineEditBox
  557. };
  558. priv.renderGraphByYear = function(record) {
  559. var jsonData = [],
  560. jsonLabels = [],
  561. jsonTooltips = [],
  562. jsonColors = ['Gradient(#fe783e:#EC561B:#F59F7D)', 'Gradient(#99f:#27afe9:#058DC7:#058DC7)', 'Gradient(#94f776:#50B332:#B1E59F)', 'Gradient(#CC76F7:#8832B3:#CE9FE5)', 'Gradient(#768CF7:#3247B3:#9FABE5)', 'Gradient(#F77681:#B3323C:#E59FA5)'],
  563. jsonColorsAll = [],
  564. wykresWidth = 940,
  565. wykresMinItemWidth = 15,
  566. graphGutterLeft = 60,
  567. graphGutterRight = 25,
  568. itemsInLabelCount = 0,
  569. currentNode = _uiNode$GraphByYear.children('canvas'),
  570. node = $('<canvas id="wykres03" width="1760" height="300">[No canvas support]</canvas>')
  571. ;
  572. {// replace node
  573. currentNode.replaceWith(node);
  574. }
  575. $.each(_state.obroty, function(kYear, vSalesByMonth) {
  576. var jsonYearData = {},
  577. yearDataValues = []
  578. ;
  579. jsonLabels.push(parseInt(kYear));
  580. $.each(_state.months, function(monthIdx, vMonth) {
  581. if (undefined === vSalesByMonth[vMonth]) {
  582. jsonYearData['' + kYear + '-' + vMonth] = 0;
  583. } else {
  584. jsonYearData['' + kYear + '-' + vMonth] = vSalesByMonth[vMonth];
  585. }
  586. });
  587. $.each(jsonYearData, function(kLabel, vValue) {
  588. jsonTooltips.push('Obrót ' + numeral(vValue).format('0,0[.]00') + ' w ' + kLabel);
  589. });
  590. yearDataValues = Object.keys(jsonYearData).map(function (key) {
  591. return jsonYearData[key];
  592. });
  593. jsonData.push(yearDataValues);
  594. itemsInLabelCount = Math.max(itemsInLabelCount, yearDataValues.length);
  595. });
  596. {// colors
  597. for (var i = 0, mi = 0, mc = jsonColors.length; i < 12; i++) {
  598. jsonColorsAll.push(jsonColors[mi]);
  599. if (++mi >= mc) {
  600. mi = 0;
  601. }
  602. }
  603. jsonColors = jsonColorsAll;
  604. }
  605. {// wykres width
  606. wykresTestWidth = jsonLabels.length * (20 * 2 + itemsInLabelCount * wykresMinItemWidth) + graphGutterLeft + graphGutterRight;
  607. wykresWidth = Math.max(wykresWidth, wykresTestWidth);
  608. jQuery('#wykres03').css({width: wykresWidth});
  609. }
  610. var bar = new RGraph.Bar('wykres03', jsonData)
  611. .Set('labels', jsonLabels)
  612. .Set('colors', jsonColors)
  613. .Set('strokestyle', 'white')
  614. .Set('linewidth', 1)
  615. .Set('shadow', true)
  616. .Set('shadow.color', '#ccc')
  617. .Set('shadow.offsetx', 0)
  618. .Set('shadow.offsety', 0)
  619. .Set('shadow.blur', 10)
  620. .Set('gutter.bottom', 30)
  621. .Set('gutter.left', graphGutterLeft)
  622. .Set('gutter.right', graphGutterRight)
  623. .Set('tooltips', RGraph.ISOLD ? null : jsonTooltips)
  624. .Set('tooltips.event', 'mousemove')
  625. .Set('labels.above', true)
  626. .Set('hmargin', 20)
  627. .Draw();
  628. };
  629. priv.renderGraphByMonth = function(record) {
  630. var jsonData = [],
  631. jsonLabels = ['styczeń','luty','marzec','kwiecień','maj','czerwiec','lipiec','sierpień','wrzesień','październik','listopad','grudzień'],
  632. jsonTooltips = [],
  633. jsonColors = ['Gradient(#fe783e:#EC561B:#F59F7D)', 'Gradient(#99f:#27afe9:#058DC7:#058DC7)', 'Gradient(#94f776:#50B332:#B1E59F)', 'Gradient(#CC76F7:#8832B3:#CE9FE5)', 'Gradient(#768CF7:#3247B3:#9FABE5)', 'Gradient(#F77681:#B3323C:#E59FA5)'],
  634. jsonColorsAll = [],
  635. wykresWidth = 940,
  636. wykresMinItemWidth = 15,
  637. graphGutterLeft = 60,
  638. graphGutterRight = 25,
  639. itemsInLabelCount = 0,
  640. currentNode = _uiNode$GraphByMonth.children('canvas'),
  641. node = $('<canvas id="wykres04" width="1760" height="300">[No canvas support]</canvas>')
  642. ;
  643. {// replace node
  644. currentNode.replaceWith(node);
  645. }
  646. $.each(_state.months, function(monthIdx, vMonth) {
  647. var jsonByMonthData = {},
  648. byMonthDataValues = []
  649. ;
  650. $.each(_state.obroty, function(kYear, vSalesByMonth) {
  651. if (undefined === vSalesByMonth[vMonth]) {
  652. jsonByMonthData['' + kYear + '-' + vMonth] = 0;
  653. } else {
  654. jsonByMonthData['' + kYear + '-' + vMonth] = vSalesByMonth[vMonth];
  655. }
  656. });
  657. $.each(jsonByMonthData, function(kLabel, vValue) {
  658. jsonTooltips.push('Obrót ' + numeral(vValue).format('0,0[.]00') + ' w ' + kLabel);
  659. });
  660. byMonthDataValues = Object.keys(jsonByMonthData).map(function (key) {
  661. return jsonByMonthData[key];
  662. });
  663. jsonData.push(byMonthDataValues);
  664. itemsInLabelCount = Math.max(itemsInLabelCount, byMonthDataValues.length);
  665. });
  666. {// colors
  667. for (var i = 0, mi = 0, mc = jsonColors.length; i < 12; i++) {
  668. jsonColorsAll.push(jsonColors[mi]);
  669. if (++mi >= mc) {
  670. mi = 0;
  671. }
  672. }
  673. jsonColors = jsonColorsAll;
  674. }
  675. {// wykres width
  676. wykresTestWidth = jsonLabels.length * (20 * 2 + itemsInLabelCount * wykresMinItemWidth) + graphGutterLeft + graphGutterRight;
  677. wykresWidth = Math.max(wykresWidth, wykresTestWidth);
  678. jQuery('#wykres04').css({width: wykresWidth});
  679. }
  680. var bar = new RGraph.Bar('wykres04', jsonData)
  681. .Set('labels', jsonLabels)
  682. .Set('colors', jsonColors)
  683. .Set('strokestyle', 'white')
  684. .Set('linewidth', 1)
  685. .Set('shadow', true)
  686. .Set('shadow.color', '#ccc')
  687. .Set('shadow.offsetx', 0)
  688. .Set('shadow.offsety', 0)
  689. .Set('shadow.blur', 10)
  690. .Set('gutter.bottom', 30)
  691. .Set('gutter.left', graphGutterLeft)
  692. .Set('gutter.right', graphGutterRight)
  693. .Set('tooltips', RGraph.ISOLD ? null : jsonTooltips)
  694. .Set('tooltips.event', 'mousemove')
  695. .Set('labels.above', true)
  696. .Set('hmargin', 20)
  697. .Draw();
  698. };
  699. priv.renderHeaderMarki = function(record) {
  700. var currentNode = _uiNode$Marki.children('tbody').children('tr'),
  701. node = $('<tr></tr>'),
  702. marki = _state.marki
  703. ;
  704. $('<th>Marki:</th>').appendTo(node);
  705. $.each(_state.marki, function(markaLabel, markaClassName) {
  706. var td$Node = $('<td><b>' + markaLabel + '</b></td>').appendTo(node);
  707. td$Node.addClass('cell_marka_' + markaClassName);
  708. });
  709. currentNode.replaceWith(node);
  710. };
  711. priv.renderTableBody = function(record) {
  712. var currentNode = _uiNode$Table.children('tbody'),
  713. node = $('<tbody></tbody>'),
  714. vSalesByYear = _state.obroty
  715. ;
  716. $.each(_state.years, function(yearIdx, vYearId) {
  717. var vSalesByMonth = vSalesByYear[vYearId];
  718. var tr$Node = $('<tr></tr>').appendTo(node);
  719. $('<th>' + vYearId + '</th>').appendTo(tr$Node);
  720. var suma = 0;
  721. $.each(_state.months, function(monthsIdx, vMonth) {
  722. var td$Node = $('<td></td>').appendTo(tr$Node);
  723. td$Node.css({textAlign: 'right'});
  724. var saleValue = parseFloat(vSalesByMonth[vMonth]);
  725. //if (!vSalesByMonth.hasOwnProperty(vMonth)) {
  726. if (isNaN(saleValue)) {
  727. td$Node.text('...');
  728. td$Node.css({color: '#ddd'});
  729. } else {
  730. suma += saleValue;
  731. $('<nobr>' + numeral(saleValue).format('0,0[.]00') + '</nobr>').appendTo(td$Node);
  732. if (undefined !== _state.obrotyToMarki[vYearId][vMonth]) {
  733. var marka = _state.obrotyToMarki[vYearId][vMonth];
  734. td$Node.addClass('cell_marka_' + _state.marki[marka]);
  735. }
  736. }
  737. td$Node.on('dblclick', {year:vYearId, month:vMonth}, priv.cellDblClicked);
  738. });
  739. var suma$Node = $('<td>' + numeral(suma).format('0,0[.]00') + '</td>').appendTo(tr$Node);
  740. suma$Node.css({textAlign: 'right'});
  741. });
  742. currentNode.replaceWith(node);
  743. };
  744. priv.renderInitInlineEditBox = function() {
  745. var nodeClass = 'dealsSales__inlineEditBox',
  746. node;
  747. var node = $('<div class="' + nodeClass + ' modal fade" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>');
  748. var modalWrap = $('<div class="modal-dialog"></div>').appendTo(node);
  749. var modalWrap = $('<div class="modal-content"></div>').appendTo(modalWrap);
  750. var frmInlineEdit = $('<form style="margin:0;padding:0;"></form>').appendTo(modalWrap);
  751. var iebHead = $('<div class="modal-header">').appendTo(frmInlineEdit);
  752. $('<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="glyphicon glyphicon-remove"></i></button>').appendTo(iebHead);
  753. $('<h3 id="myModalLabel">Edytuj</h3>').appendTo(iebHead);
  754. var iebBodyWrap = $('<div class="modal-body" style="padding:0"></div>').appendTo(frmInlineEdit);
  755. var iebBody = $('<div style="padding:15px"></div>').appendTo(iebBodyWrap);
  756. //$('<input type="hidden" name="ID" value="">').appendTo(iebBody);
  757. //$('<input type="hidden" name="col" value="">').appendTo(iebBody);
  758. $('<div class="inlineEditBox-cnt"></div>').appendTo(iebBody);
  759. var iebFoot = $('<div class="modal-footer"></div>').appendTo(frmInlineEdit);
  760. $('<button class="btn" data-dismiss="modal" aria-hidden="true">Zamknij</button>').appendTo(iebFoot);
  761. var iebBtnSave = $('<input type="submit" value="Zapisz" class="btn btn-primary btn-save">').appendTo(iebFoot);
  762. frmInlineEdit.on('submit', function() {
  763. var data = _inlineEditBox$Node.find('form').serialize();
  764. _inlineEditBox$Node.find('.inlineEditBox-cnt').html('<span class="loading-info"> loading ...</span>');
  765. function notifyAjaxCallback(data) {
  766. var notify = {};
  767. notify.type = (data && data.type)? data.type : '';
  768. notify.msg = (data && data.msg)? data.msg : '';
  769. switch (notify.type) {
  770. case 'success':
  771. if (!notify.msg) notify.msg = 'Dane poprawnie zaktualizowane';
  772. break;
  773. case 'info':
  774. if (!notify.msg) notify.msg = 'Nie wprowadzono żadnych zmian';
  775. break;
  776. case 'error':
  777. if (!notify.msg) notify.msg = 'Wystąpiły błędy';
  778. break;
  779. case 'warning':
  780. notify.type = 'warn';
  781. if (!notify.msg) notify.msg = 'Wystąpiły błędy';
  782. break;
  783. default:
  784. notify.msg = 'Nieznany błąd';
  785. if (data && data.errorCode) notify.msg += ' ' + data.errorCode;
  786. notify.type = '';
  787. }
  788. jQuery.notify(notify.msg, notify.type);
  789. }
  790. $.ajax({
  791. data: data,
  792. dataType: 'json',
  793. type: "POST",
  794. url: priv.options.urlSaveInlineEdit
  795. })
  796. .done(function(data, textStatus, jqXHR){
  797. notifyAjaxCallback(data);
  798. if (data.record) {
  799. var year = data.record.SALES_YEAR,
  800. month = data.record.SALES_MONTH,
  801. status = data.record.A_STATUS,
  802. saleValue = parseFloat(data.record.SALES_VALUE),
  803. marka = data.record.marka,
  804. state = {}
  805. ;
  806. if (month < 10) month = '0' + month;
  807. // TODO: if marka changed - fix state
  808. if ('DELETED' === status) {
  809. state.obroty = {};
  810. state.obroty[year] = {};
  811. state.obroty[year][month] = null;
  812. state.obrotyToMarki = {};
  813. state.obrotyToMarki[year] = {};
  814. state.obrotyToMarki[year][month] = null;
  815. } else {
  816. state.obroty = {};
  817. state.obroty[year] = {};
  818. state.obroty[year][month] = saleValue;
  819. state.obrotyToMarki = {};
  820. state.obrotyToMarki[year] = {};
  821. state.obrotyToMarki[year][month] = marka;
  822. }
  823. priv.setState(state);
  824. }
  825. _inlineEditBox$Node.modal('hide');
  826. })
  827. .fail(function(jqXHR){// jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});
  828. if (jqXHR.responseJSON) {
  829. notifyAjaxCallback(jqXHR.responseJSON);
  830. }
  831. else {
  832. var txt = jqXHR.responseText || 'Wystąpiły błędy';
  833. if (jqXHR.status == 404) {
  834. jQuery.notify(jqXHR.responseText, 'error');
  835. } else {
  836. jQuery.notify(jqXHR.responseText, 'warn');
  837. }
  838. }
  839. _inlineEditBox$Node.modal('hide');
  840. });
  841. return false;
  842. });
  843. node.appendTo(_uiNodeCont);//currentNode.replaceWith(node);
  844. _inlineEditBox$Node = node;
  845. };
  846. priv.cellDblClicked = function(e) {
  847. if ('year' in e.data && 'month' in e.data && e.data.year > 0 && e.data.month > 0) {
  848. _inlineEditBox$Node.modal();
  849. _inlineEditBox$Node.show();
  850. _inlineEditBox$Node.on('shown.bs.modal', function(e) {
  851. var dialogBox = jQuery(this).find('.modal-dialog'),
  852. modalBody = dialogBox.find('.modal-body'),
  853. boudingRect = dialogBox.get(0).getBoundingClientRect();
  854. dialogBox.css({position: 'absolute', margin:0, top: boudingRect.top, left: boudingRect.left});
  855. modalBody.css({overflow: 'scroll'});
  856. dialogBox.resizable({minHeight: 300, minWidth: 300, alsoResize: modalBody});
  857. dialogBox.draggable();
  858. });
  859. _inlineEditBox$Node.on('hidden.bs.modal', function(e) {
  860. var dialogBox = jQuery(this).find('.modal-dialog'),
  861. modalBody = dialogBox.find('.modal-body');
  862. dialogBox.removeAttr('style');
  863. modalBody.removeAttr('style');
  864. modalBody.css({padding: '0'});
  865. dialogBox.resizable();
  866. dialogBox.draggable();
  867. dialogBox.resizable('destroy');
  868. dialogBox.draggable('destroy');
  869. });
  870. _inlineEditBox$Node.find('input[name=ID]').val(e.data.id);
  871. _inlineEditBox$Node.find('input[name=col]').val(e.data.col);
  872. _inlineEditBox$Node.find('.inlineEditBox-cnt').html('<span class="loading-info"> loading ...</span>');
  873. $.ajax({
  874. url: priv.options.urlInlineEdit,
  875. type: 'GET',
  876. dataType: 'text',
  877. data: e.data,
  878. async: true,
  879. success: function (data) {
  880. _inlineEditBox$Node.find('.inlineEditBox-cnt').html(data);
  881. _inlineEditBox$Node.find('.btn-save').show();
  882. initDateTimePicker(_inlineEditBox$Node);
  883. _inlineEditBox$Node.find('textarea').autosize();
  884. var fld = _inlineEditBox$Node.find('input[id^="f"]:first');
  885. if (fld && !fld.hasClass('se_type-date')) {
  886. fld.focus();
  887. fld.keydown(function(event) {
  888. if (event.which == 13) {
  889. event.preventDefault();
  890. _inlineEditBox$Node.find('form').submit();
  891. }
  892. });
  893. }
  894. },
  895. error: function (err) {
  896. if (priv.options.debug) console.log('err');
  897. }
  898. });
  899. } else {
  900. if (priv.options.debug) console.log('NO data');
  901. return false;
  902. }
  903. };
  904. priv.onRender = function(e) {
  905. if (priv.options.debug) console.log('onRender.arguments:', arguments.length, arguments, 'e:', e);
  906. if (arguments.length > 1) {
  907. for (var i=1; i<arguments.length; i++) {
  908. switch (arguments[i]) {
  909. case 'body': priv.renderTableBody(); break;
  910. case 'marki': priv.renderHeaderMarki(); break;
  911. case 'graphs': priv.renderGraphByYear(); priv.renderGraphByMonth(); break;
  912. }
  913. }
  914. }
  915. };
  916. priv.setState = function(state) {
  917. var oldState = _state,// TODO: use to check what really changed (use extend!)
  918. renderParts = {};
  919. if (state.obroty) {
  920. $.each(state.obroty, function(kYear, vSalesByMonth) {
  921. $.each(vSalesByMonth, function(kMonth, vSaleValue) {
  922. if (null === vSaleValue) {
  923. _state.obroty[kYear][kMonth] = undefined;
  924. } else {
  925. _state.obroty[kYear][kMonth] = vSaleValue;
  926. }
  927. });
  928. });
  929. renderParts['body'] = true;
  930. renderParts['graphs'] = true;
  931. }
  932. /*
  933. state.obrotyToMarki[year][month] = marka;
  934. */
  935. if (state.obrotyToMarki) {
  936. $.each(state.obrotyToMarki, function(kYear, vMarkiByMonth) {
  937. $.each(vMarkiByMonth, function(kMonth, vMarka) {
  938. if (null === vMarka) {
  939. _state.obrotyToMarki[kYear][kMonth] = undefined;
  940. } else {
  941. _state.obrotyToMarki[kYear][kMonth] = vMarka;
  942. if (undefined === _state.marki[vMarka]) {
  943. _state.marki[vMarka] = 'default';
  944. }
  945. }
  946. });
  947. });
  948. renderParts['marki'] = true;
  949. }
  950. renderParts = Object.keys(renderParts);
  951. if (priv.options.debug) console.log('setState::renderParts: ', renderParts);//TODO:DBG:RMME
  952. if (renderParts.length > 0) {
  953. jQuery(_uiNodeCont).trigger('DealsSalesTable:render', renderParts);
  954. }
  955. };
  956. publ.init = function (options) {
  957. if (priv.options.debug) console.log('DealsSalesTable initialization...');
  958. //merge supplied options with defaults
  959. $.extend(priv.options, defaults, options);
  960. priv.init();
  961. return publ;
  962. };
  963. return publ;
  964. };
  965. $.fn.DealsSalesTable = function (options) {
  966. options = options || {};
  967. return this.each(function () {
  968. options.id = this;
  969. $(this).data('DealsSalesTable', new DealsSalesTable().init(options));
  970. });
  971. };
  972. })(jQuery);
  973. jQuery(document).ready(function() {
  974. jQuery('#dealsSalesByMonth').DealsSalesTable({
  975. url: 'index.php?_route=DealsSales&_task=getDataAjax&telbox=<?php echo $idTelboxes; ?>',
  976. urlInlineEdit: 'index.php?_route=DealsSales&_task=inlineEditAjax&telbox=<?php echo $idTelboxes; ?>',
  977. urlSaveInlineEdit: 'index.php?_route=DealsSales&_task=saveInlineAjax&telbox=<?php echo $idTelboxes; ?>',
  978. initData: <?php echo json_encode($initData); ?>
  979. });
  980. });
  981. </script>
  982. <script src="stuff/RGraph/libraries/RGraph.common.core.js?_v=20140626"></script>
  983. <script src="stuff/RGraph/libraries/RGraph.common.dynamic.js?_v=20140626"></script>
  984. <script src="stuff/RGraph/libraries/RGraph.common.key.js?_v=20140626"></script>
  985. <script src="stuff/RGraph/libraries/RGraph.drawing.rect.js?_v=20140626"></script>
  986. <script src="stuff/RGraph/libraries/RGraph.common.tooltips.js?_v=20140626"></script>
  987. <script src="stuff/RGraph/libraries/RGraph.bar.js?_v=20140626"></script>
  988. <script src="stuff/RGraph/libraries/RGraph.line.js?_v=20140626"></script>
  989. <?php endif; ?>
  990. </div>
  991. <?php
  992. }
  993. }