PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/tipo_cambios_controller.php

http://scp-soft.googlecode.com/
PHP | 358 lines | 326 code | 30 blank | 2 comment | 73 complexity | c9eaf7a56c1a3077cf1a33a8e55c9fe1 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. class TipoCambiosController extends AppController{
  3. var $name = 'TipoCambios';
  4. var $appModule = 'Contabilidad';
  5. var $helpers = array('Html', 'Form', 'Javascript', 'Codigo', 'GetList');
  6. var $components = array('RequestHandler','Formating');
  7. var $uses = array('TipoCambio', 'Moneda');
  8. var $paginate=array('order'=>array('TipoCambio.fecha'=>'desc'));
  9. function index(){
  10. $conditions['TipoCambio.activo'] = 1;
  11. $conditions['date_part(\'month\',TipoCambio.fecha)'] = $this->periodoId();
  12. $conditions['date_part(\'year\',TipoCambio.fecha)'] = $this->_ejercicio['codigo'];
  13. if (!empty($this->params['url']['limite']))
  14. $this->limite = $this->params['url']['limite'];
  15. $this->data['TipoCambio']['pagesize'] = $this->paginate['limit']=$this->limite;
  16. $data = $this->paginate($conditions);
  17. $this->set('cambios', $data);
  18. $this->set('limite', $this->limite);
  19. }
  20. function add(){
  21. if (!empty($this->data)){
  22. $cantidad = $this->TipoCambio->find('count',array(
  23. 'conditions'=>array(
  24. 'moneda_fuente_id'=>$this->data['TipoCambio']['moneda_fuente_id'],
  25. 'moneda_destino_id'=>$this->data['TipoCambio']['moneda_destino_id'],
  26. 'fecha'=>$this->data['TipoCambio']['fecha'],
  27. 'activo'=>true
  28. ),
  29. 'recursive'=>-1
  30. ));
  31. if ($cantidad > 0){
  32. $tipoCambio = $this->TipoCambio->find('first',array(
  33. 'conditions'=>array(
  34. 'moneda_fuente_id'=>$this->data['TipoCambio']['moneda_fuente_id'],
  35. 'moneda_destino_id'=>$this->data['TipoCambio']['moneda_destino_id'],
  36. 'fecha'=>$this->data['TipoCambio']['fecha'],
  37. 'activo'=>true
  38. ),
  39. 'recursive'=>-1
  40. ));
  41. $this->data['TipoCambio']['id'] = $tipoCambio['TipoCambio']['id'];
  42. $this->data['TipoCambio']['usuario_modificacion_id'] = $this->_usuario['id'];
  43. $this->data['TipoCambio']['fecha_modificacion'] = 'now()';
  44. } else {
  45. $this->data['TipoCambio']['usuario_creacion_id'] = $this->_usuario['id'];
  46. $this->data['TipoCambio']['fecha_creacion'] = 'now()';
  47. $this->TipoCambio->create();
  48. }
  49. if ($this->TipoCambio->save($this->data)){
  50. $this->Session->setFlash('El tipo de cambio se ha adicionado satisfactoriamente');
  51. $this->redirect(array('action'=>'index'));
  52. } else {
  53. $this->Session->setFlash('Se ha producido un error al adicionado el tipo de cambio');
  54. }
  55. }
  56. $this->set('monedas', $this->Moneda->find('list', array(
  57. 'fields'=>array('id', 'descripcion'),
  58. 'conditions'=>array('activo'=>true),
  59. 'order'=>'id'
  60. )));
  61. $this->set('fecha', $this->_ejercicio['Actual']['fecha']);
  62. }
  63. function edit($id = null){
  64. if (empty($id)){
  65. $this->Session->setFlash('El identificador de tipo de cambio es incorrecto');
  66. $this->redirect(array('action'=>'index'));
  67. }
  68. if (!empty($this->data)){
  69. $this->data['TipoCambio']['usuario_modificacion_id'] = $this->_usuario['id'];
  70. $this->data['TipoCambio']['fecha_modificacion'] = 'now()';
  71. if ($this->TipoCambio->save($this->data)){
  72. $this->Session->setFlash('El tipo de cambio se ha adicionado satisfactoriamente');
  73. $this->redirect(array('action'=>'index'));
  74. } else {
  75. $this->Session->setFlash('Se ha producido un error al adicionado el tipo de cambio');
  76. }
  77. } else {
  78. $this->TipoCambio->recursive = -1;
  79. $this->data = $this->TipoCambio->read(null, $id);
  80. }
  81. $this->set('monedas', $this->Moneda->find('list', array(
  82. 'fields'=>array('id', 'descripcion'),
  83. 'conditions'=>array('activo'=>true),
  84. 'order'=>'id'
  85. )));
  86. $this->set('fecha', $this->_ejercicio['Actual']['fecha']);
  87. }
  88. function delete($id = null, $verbose = true){
  89. if (empty($id)){
  90. if ($verbose){
  91. $this->Session->setFlash('El identificador de tipo de cambio es incorrecto');
  92. $this->redirect(array('action'=>'index'));
  93. } else return false;
  94. }
  95. $this->TipoCambio->recursive = -1;
  96. $data = $this->TipoCambio->read(null, $id);
  97. $data['TipoCambio']['activo'] = false;
  98. $data['TipoCambio']['usuario_borrado_id'] = $this->_usuario['id'];
  99. $data['TipoCambio']['fecha_borrado'] = 'now()';
  100. if ($this->TipoCambio->save($data)){
  101. if ($verbose){
  102. $this->Session->setFlash('El tipo de cambio se ha adicionado satisfactoriamente');
  103. $this->redirect(array('action'=>'index'));
  104. } else return true;
  105. } else {
  106. if ($verbose){
  107. $this->Session->setFlash('Se ha producido un error al adicionado el tipo de cambio');
  108. } else return false;
  109. }
  110. }
  111. function process(){// procesa anulaciones y eliminaciones en bloque
  112. //$this->autoRender = false;
  113. $op = isset($this->params['form']['delete'])?'eliminar':'';
  114. $flag = true;
  115. $i = 0;
  116. foreach($this->data as $item){
  117. if ($item['id']!='0'){
  118. if(isset($this->params['form']['delete'])){ // anular
  119. $flag = $this->delete($item['id'], false);
  120. }
  121. if (!$flag){
  122. $data = $this->TipoCambio->read(null, $item['TipoCambio']['id']);
  123. $this->Session->setFlash(__("Se ha producido un error al $op el Tipo de Cambio #".$data['TipoCambio']['id'], true));
  124. break;
  125. }
  126. }
  127. $i++;
  128. }
  129. if ($flag && $i++){
  130. $this->Session->setFlash(__("El proceso de $op Tipos de Cambio se ha realizado con exito.", true));
  131. }
  132. $this->redirect(array('action'=>'index'));
  133. }
  134. function proxyUpdateChange($from = 2, $to = 1, $date = null){
  135. if (empty($date)) $date = date('d-m-Y');
  136. $date = str_replace('-','/',$date);
  137. $dateParts = explode('/',$date);
  138. $datetime = date('d/m/Y H:i:s');// para log
  139. $sunat = $this->downloadParseSunat($this->URL_SUNAT,$dateParts[2],$dateParts[1],$dateParts[0]);
  140. if (count($sunat) > 0){
  141. $sunat = $sunat[count($sunat)-1];
  142. $sunat['dia'] = date('d/m/Y', mktime(0,0,0,$dateParts[1],$sunat['dia'],$dateParts[2]));
  143. $result['SUNAT']=array(
  144. 'dia'=>$sunat['dia'],
  145. 'compra'=>$sunat['compra'],
  146. 'venta'=>$sunat['venta'],
  147. 'mensaje'=>''
  148. );
  149. } else {
  150. echo "$datetime: No se encontro tipo de cambio el $date de $from a $to\n";
  151. die;
  152. }
  153. if ($sunat['dia'] != $date){
  154. echo "$datetime: No se encontro tipo de cambio el $date de $from a $to\n";
  155. die;
  156. }
  157. $sbs = $this->downloadParseSbs($this->URL_SBS,$dateParts[2],$dateParts[1],$dateParts[0]);
  158. if (count($sbs) > 0){
  159. $sbs = $sbs[count($sbs)-1];
  160. $result['SBS']=array(
  161. 'dia'=>$sbs['dia'],
  162. 'compra'=>$sbs['compra'],
  163. 'venta'=>$sbs['venta'],
  164. 'mensaje'=>''
  165. );
  166. } else {
  167. echo "$datetime: No se encontro tipo de cambio el $date de $from a $to\n";
  168. die;
  169. }
  170. $date = $sunat['dia'];
  171. $cantidad = $this->TipoCambio->find('count',array(
  172. 'conditions'=>array(
  173. 'moneda_fuente_id'=>$from,
  174. 'moneda_destino_id'=>$to,
  175. 'fecha'=>$date,
  176. 'activo'=>true
  177. ),
  178. 'recursive'=>-1
  179. ));
  180. if ($cantidad > 0){
  181. $tipoCambio = $this->TipoCambio->find('first',array(
  182. 'conditions'=>array(
  183. 'moneda_fuente_id'=>$from,
  184. 'moneda_destino_id'=>$to,
  185. 'fecha'=>$date,
  186. 'activo'=>true
  187. ),
  188. 'recursive'=>-1
  189. ));
  190. $tipoCambio['TipoCambio']['moneda_fuente_id'] = $from;
  191. $tipoCambio['TipoCambio']['moneda_destino_id'] = $to;
  192. $tipoCambio['TipoCambio']['fecha'] = $date;
  193. $tipoCambio['TipoCambio']['bancario_compra'] = $result['SUNAT']['compra'];
  194. $tipoCambio['TipoCambio']['bancario_venta'] = $result['SUNAT']['venta'];
  195. $tipoCambio['TipoCambio']['promedio_compra'] = $result['SBS']['compra'];
  196. $tipoCambio['TipoCambio']['promedio_venta'] = $result['SBS']['venta'];
  197. $tipoCambio['TipoCambio']['usuario_modificacion_id'] = -1;
  198. $tipoCambio['TipoCambio']['fecha_modificacion'] = 'now()';
  199. } else {
  200. $tipoCambio['TipoCambio']['moneda_fuente_id'] = $from;
  201. $tipoCambio['TipoCambio']['moneda_destino_id'] = $to;
  202. $tipoCambio['TipoCambio']['fecha'] = $date;
  203. $tipoCambio['TipoCambio']['bancario_compra'] = $result['SUNAT']['compra'];
  204. $tipoCambio['TipoCambio']['bancario_venta'] = $result['SUNAT']['venta'];
  205. $tipoCambio['TipoCambio']['promedio_compra'] = $result['SBS']['compra'];
  206. $tipoCambio['TipoCambio']['promedio_venta'] = $result['SBS']['venta'];
  207. $tipoCambio['TipoCambio']['usuario_creacion_id'] = -1;
  208. $tipoCambio['TipoCambio']['fecha_creacion'] = 'now()';
  209. $this->TipoCambio->create();
  210. }
  211. if ($this->TipoCambio->save($tipoCambio)){
  212. echo "$datetime: El tipo de cambio se ha adicionado satisfactoriamente el $date de $from a $to\n";
  213. die;
  214. } else {
  215. echo "$datetime: Se ha producido un error al adicionado el tipo de cambio el $date de $from a $to\n";
  216. die;
  217. }
  218. }
  219. private function downloadParseSunat($url = null, $year = null, $month = null, $day = null){
  220. set_time_limit(0);
  221. $now = $this->fechaTrabajo();
  222. if (!empty($now)) list($d,$m,$y) = explode('/',$now);
  223. if (empty($day)) $day = $d;
  224. if (empty($month)) $month = $m;
  225. if (empty($year)) $year = $y;
  226. $res = App::import('Vendor', 'simple_html_dom', array('file' =>'simple_html_dom'.DS.'simple_html_dom.php'));
  227. if (empty($url)) $url = $this->URL_SUNAT;
  228. $url = sprintf($url, $year, $month);
  229. $html = file_get_dom($url);
  230. $tr = $html->find('table tr td');
  231. $row = array();
  232. $lista = array();
  233. foreach ($tr as $element){
  234. $data = trim($element->plaintext);
  235. $data += 0;
  236. if (!is_numeric($data) || $data == 0) continue;
  237. if (!isset($row['dia']))
  238. $row['dia'] = $data;
  239. else if (!isset($row['compra']))
  240. $row['compra'] = $data;
  241. else if (!isset($row['venta']))
  242. $row['venta'] = $data;
  243. else {
  244. if ($row['dia'] >= $day) break; // para que se detenga en el dia
  245. array_push($lista, $row);
  246. $row = array();
  247. $row['dia'] = $data;
  248. }
  249. }
  250. if (count($row)>0) array_push($lista, $row);
  251. return $lista;
  252. }
  253. private function downloadParseSbs($url = null, $year = null, $month = null, $day = null){
  254. set_time_limit(0);
  255. $now = $this->fechaTrabajo();
  256. if (!empty($now)) list($d,$m,$y) = explode('/',$now);
  257. if (empty($day)) $day = $d;
  258. if (empty($month)) $month = $m;
  259. if (empty($year)) $year = $y;
  260. $from = date('d/m/Y', mktime(0, 0, 0, $month, 1, $year));
  261. $to = date('d/m/Y', mktime(0, 0, 0, $month, $day+1, $year));
  262. $res = App::import('Vendor', 'simple_html_dom', array('file' =>'simple_html_dom'.DS.'simple_html_dom.php'));
  263. if (empty($url)) $url = $this->URL_SBS;
  264. $url = sprintf($url, $from, $to);
  265. $html = file_get_dom($url);
  266. $tr = $html->find('table tr td');
  267. $row = array();
  268. $lista = array();
  269. foreach ($tr as $element){
  270. $data = trim($element->plaintext);
  271. $data = explode(' ',$data);
  272. foreach ($data as $d){
  273. $d = trim($d);
  274. if (!is_numeric($d) && !$this->is_date($d)) continue;
  275. if (!isset($row['dia']))
  276. $row['dia'] = $d;
  277. else if (!isset($row['compra']))
  278. $row['compra'] = $d+0;
  279. else if (!isset($row['venta']))
  280. $row['venta'] = $d+0;
  281. else {
  282. if ($row['dia'] > $day) break; // para que se detenga en el dia
  283. // echo $row['dia'];
  284. array_push($lista, $row);
  285. $row = array();
  286. $row['dia'] = $d;
  287. }
  288. }
  289. }
  290. if (count($row)>0) array_push($lista, $row);
  291. return $lista;
  292. }
  293. function download(){
  294. $sunat = $this->downloadParseSunat();
  295. if (count($sunat) > 0){
  296. $sunat = $sunat[count($sunat)-1];
  297. $sunat['dia'] = date('d/m/Y', mktime(0,0,0,$this->periodoId(),$sunat['dia'],$this->_ejercicio['codigo']));
  298. $result['SUNAT']=array(
  299. 'dia'=>$sunat['dia'],
  300. 'compra'=>$sunat['compra'],
  301. 'venta'=>$sunat['venta'],
  302. 'mensaje'=>''
  303. );
  304. } else {
  305. $result['SUNAT']['mensaje'] = 'No se encontro tipo de cambio';
  306. }
  307. $sbs = $this->downloadParseSbs();
  308. if (count($sbs) > 0){
  309. $sbs = $sbs[count($sbs)-1];
  310. $result['SBS']=array(
  311. 'dia'=>$sbs['dia'],
  312. 'compra'=>$sbs['compra'],
  313. 'venta'=>$sbs['venta'],
  314. 'mensaje'=>''
  315. );
  316. } else {
  317. $result['SBS']['mensaje'] = 'No se encontro tipo de cambio';
  318. }
  319. if ($this->RequestHandler->isAjax()) {
  320. Configure::write('debug', 0); //quito el debugger
  321. $this->autoRender = false;//desabilito el auto dibujado
  322. $this->autoLayout = false;//desabilito el auto layout
  323. $this->header('Content-Type: application/json');
  324. echo json_encode($result);
  325. } else $this->set(compact('result'));
  326. }
  327. }
  328. ?>