FacturisSync.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 checkLatestVersion()
  57. {
  58. try{
  59. $formController = new \Controller\FormController();
  60. $result = $formController->checkLatestVersion($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. public function sync()
  69. {
  70. try{
  71. $syncController = new \Controller\SyncController($this->db, $this->language);
  72. $result = $syncController->sync();
  73. if(!empty($result))
  74. {
  75. \Core\Log\FileLog::write($result, \Core\Log\FileLog::INFO);
  76. }
  77. return array('success' => $result);
  78. }
  79. catch(\Exception $e) {
  80. $message = $this->language->get($e->getMessage());
  81. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  82. return array('error' => $message);
  83. }
  84. }
  85. private function getForm($db, $result)
  86. {
  87. try{
  88. extract($result);
  89. $formController = new \Controller\FormController();
  90. $data = $formController->getForm($db, $this->language);
  91. extract($data);
  92. include('View/main.php');
  93. }
  94. catch(\Exception $e) {
  95. $message = $e->getMessage();
  96. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  97. echo $message;
  98. }
  99. }
  100. function autoSync() {
  101. try{
  102. $shops = $this->db->getAll();
  103. if(!empty($shops))
  104. {
  105. foreach($shops as $shop)
  106. {
  107. if(isset($shop['market_code']))
  108. {
  109. try{
  110. $controller = new \Controller\SyncController($this->db, $this->language, $shop['market_code']);
  111. $result = $controller->autoSync();
  112. $message = 'Shop OK: ' . $shop['market_code'];
  113. echo $message . PHP_EOL;
  114. \Core\Log\FileLog::write($message, \Core\Log\FileLog::INFO);
  115. }
  116. catch(\Exception $e) {
  117. $messsage = 'Err shop: ' . $shop['market_code'] . ': ' . $this->language->get($e->getMessage());
  118. \Core\Log\FileLog::write($messsage, \Core\Log\FileLog::ERROR);
  119. echo $messsage . PHP_EOL;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. catch(\Exception $e) {
  126. $messsage = 'Err cron: ' . $this->language->get($e->getMessage());
  127. \Core\Log\FileLog::write($messsage, \Core\Log\FileLog::ERROR);
  128. echo $messsage . PHP_EOL;
  129. }
  130. }
  131. public function downloadLog()
  132. {
  133. try{
  134. if(isset($_REQUEST['action']) && ($_REQUEST['action'] == 'downloadLog'))
  135. {
  136. $header = array(
  137. $this->language->get('log_header_date'),
  138. $this->language->get('log_header_type'),
  139. $this->language->get('log_header_description')
  140. );
  141. $logController = new \Controller\LogController();
  142. $logController->downloadFileLog(
  143. isset($_REQUEST['start']) ? $_REQUEST['start'] : date('d.m.Y H:i:s'),
  144. isset($_REQUEST['end']) ? $_REQUEST['end'] : date('d.m.Y H:i:s'),
  145. $header
  146. );
  147. return array('success' => $this->language->get('success_download_log'));
  148. }
  149. else
  150. {
  151. return array();
  152. }
  153. }
  154. catch(\Exception $e) {
  155. $message = $this->language->get('error_download_log');
  156. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  157. return array('error' => $message);
  158. }
  159. }
  160. public function clearLog()
  161. {
  162. try{
  163. if(isset($_REQUEST['action']) && ($_REQUEST['action'] == 'clearLog'))
  164. {
  165. $result = null;
  166. $logController = new \Controller\LogController();
  167. $logController->clearFileLog();
  168. return array('success' => $this->language->get('success_clear_log'));
  169. }
  170. else
  171. {
  172. return array();
  173. }
  174. }
  175. catch(\Exception $e) {
  176. $message = $this->language->get('error_clear_log');
  177. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  178. return array('error' => $message);
  179. }
  180. }
  181. public function saveOptions()
  182. {
  183. try{
  184. if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'save')
  185. {
  186. $this->db->saveOptions();
  187. $message = $this->language->get('success_save_data');
  188. \Core\Log\FileLog::write($message, \Core\Log\FileLog::INFO);
  189. return array('success' => $message);
  190. }
  191. else
  192. {
  193. return array();
  194. }
  195. }
  196. catch(\Exception $e) {
  197. $message = $this->language->get($e->getMessage());
  198. \Core\Log\FileLog::write($message, \Core\Log\FileLog::ERROR);
  199. return array('error' => $message);
  200. }
  201. }
  202. }