FacturisSync.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. class FacturisSync
  3. {
  4. public $language;
  5. public $db;
  6. public function __construct($db) {
  7. $this->language = $this->getLanguage();
  8. $this->db = $db;
  9. }
  10. private function getLanguage()
  11. {
  12. $language = new Core\Language\English();
  13. return $language;
  14. }
  15. public function showMenu()
  16. {
  17. $result = $this->downloadLog();
  18. if(empty($result))
  19. {
  20. $result = $this->clearLog();
  21. }
  22. if(empty($result))
  23. {
  24. $result = $this->saveOptions();
  25. }
  26. if(empty($result))
  27. {
  28. $result = $this->sync();
  29. }
  30. $this->getForm($this->db, $result);
  31. }
  32. function testAuth(){
  33. try{
  34. $formController = new \Controller\FormController();
  35. $formController->testAuth($this->db);
  36. \Core\Log\FileLog::write($this->language->get('success_test_auth'), \Core\Log\FileLog::INFO);
  37. echo '';exit;
  38. }
  39. catch(\Exception $e) {
  40. \Core\Log\FileLog::write($e->getMessage(), \Core\Log\FileLog::ERROR);
  41. echo $e->getMessage();exit;
  42. }
  43. }
  44. function getPdlGestiuni()
  45. {
  46. try{
  47. $formController = new \Controller\FormController();
  48. $result = $formController->getPdlGestiuni($this->db, $this->language);
  49. echo $result;exit;
  50. }
  51. catch(\Exception $e) {
  52. \Core\Log\FileLog::write($e->getMessage(), \Core\Log\FileLog::ERROR);
  53. echo '';exit;
  54. }
  55. }
  56. function getPdls()
  57. {
  58. try{
  59. $formController = new \Controller\FormController();
  60. $result = $formController->getPdls($this->db, $this->language);
  61. echo $result;exit;
  62. }
  63. catch(\Exception $e) {
  64. \Core\Log\FileLog::write($e->getMessage(), \Core\Log\FileLog::ERROR);
  65. echo '';exit;
  66. }
  67. }
  68. function checkLatestVersion()
  69. {
  70. try{
  71. $formController = new \Controller\FormController();
  72. $result = $formController->checkLatestVersion($this->db, $this->language);
  73. echo $result;exit;
  74. }
  75. catch(\Exception $e) {
  76. \Core\Log\FileLog::write($e->getMessage(), \Core\Log\FileLog::ERROR);
  77. echo '';exit;
  78. }
  79. }
  80. public function sync()
  81. {
  82. try{
  83. $syncController = new \Controller\SyncController($this->db, $this->language);
  84. $result = $syncController->sync();
  85. if(!empty($result))
  86. {
  87. \Core\Log\FileLog::write($result, \Core\Log\FileLog::INFO);
  88. }
  89. return array('success' => $result);
  90. }
  91. catch(\Exception $e) {
  92. $message = $this->language->get($e->getMessage());
  93. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  94. return array('error' => $message);
  95. }
  96. }
  97. private function getForm($db, $result)
  98. {
  99. try{
  100. extract($result);
  101. $formController = new \Controller\FormController();
  102. $data = $formController->getForm($db, $this->language);
  103. extract($data);
  104. include('View/main.php');
  105. }
  106. catch(\Exception $e) {
  107. $message = $e->getMessage();
  108. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  109. echo $message;
  110. }
  111. }
  112. function autoSync() {
  113. try{
  114. $shops = $this->db->getAll();
  115. if(!empty($shops))
  116. {
  117. foreach($shops as $shop)
  118. {
  119. if(isset($shop['market_code']))
  120. {
  121. try{
  122. $controller = new \Controller\SyncController($this->db, $this->language, $shop['market_code']);
  123. $result = $controller->autoSync();
  124. $message = 'Shop OK: ' . $shop['market_code'];
  125. echo $message . PHP_EOL;
  126. \Core\Log\FileLog::write($message, \Core\Log\FileLog::INFO);
  127. }
  128. catch(\Exception $e) {
  129. $messsage = 'Err shop: ' . $shop['market_code'] . ': ' . $this->language->get($e->getMessage());
  130. \Core\Log\FileLog::write($messsage, \Core\Log\FileLog::ERROR);
  131. echo $messsage . PHP_EOL;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. catch(\Exception $e) {
  138. $messsage = 'Err cron: ' . $this->language->get($e->getMessage());
  139. \Core\Log\FileLog::write($messsage, \Core\Log\FileLog::ERROR);
  140. echo $messsage . PHP_EOL;
  141. }
  142. }
  143. public function downloadLog()
  144. {
  145. try{
  146. if(isset($_REQUEST['action']) && ($_REQUEST['action'] == 'downloadLog'))
  147. {
  148. $header = array(
  149. $this->language->get('log_header_date'),
  150. $this->language->get('log_header_type'),
  151. $this->language->get('log_header_description')
  152. );
  153. $logController = new \Controller\LogController();
  154. $logController->downloadFileLog(
  155. isset($_REQUEST['start']) ? $_REQUEST['start'] : date('d.m.Y H:i:s'),
  156. isset($_REQUEST['end']) ? $_REQUEST['end'] : date('d.m.Y H:i:s'),
  157. $header
  158. );
  159. return array('success' => $this->language->get('success_download_log'));
  160. }
  161. else
  162. {
  163. return array();
  164. }
  165. }
  166. catch(\Exception $e) {
  167. $message = $this->language->get('error_download_log');
  168. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  169. return array('error' => $message);
  170. }
  171. }
  172. public function clearLog()
  173. {
  174. try{
  175. if(isset($_REQUEST['action']) && ($_REQUEST['action'] == 'clearLog'))
  176. {
  177. $result = null;
  178. $logController = new \Controller\LogController();
  179. $logController->clearFileLog();
  180. return array('success' => $this->language->get('success_clear_log'));
  181. }
  182. else
  183. {
  184. return array();
  185. }
  186. }
  187. catch(\Exception $e) {
  188. $message = $this->language->get('error_clear_log');
  189. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  190. return array('error' => $message);
  191. }
  192. }
  193. public function saveOptions()
  194. {
  195. try{
  196. if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'save')
  197. {
  198. $this->db->saveOptions();
  199. $message = $this->language->get('success_save_data');
  200. \Core\Log\FileLog::write($message, \Core\Log\FileLog::INFO);
  201. return array('success' => $message);
  202. }
  203. else
  204. {
  205. return array();
  206. }
  207. }
  208. catch(\Exception $e) {
  209. $message = $this->language->get($e->getMessage());
  210. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  211. return array('error' => $message);
  212. }
  213. }
  214. }