PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/editar_facturas/controller/editar_factura_prov.php

https://gitlab.com/cosouth.battle/sartinofi
PHP | 434 lines | 375 code | 42 blank | 17 comment | 65 complexity | 5d13a8f87135d2ce5375bb2e60f9f66e MD5 | raw file
  1. <?php
  2. require_model('asiento.php');
  3. require_model('asiento_factura.php');
  4. require_model('divisa.php');
  5. require_model('fabricante.php');
  6. require_model('forma_pago.php');
  7. require_model('proveedor.php');
  8. require_model('recibo_proveedor.php');
  9. class editar_factura_prov extends fs_controller
  10. {
  11. public $divisa;
  12. public $factura;
  13. public $fabricante;
  14. public $familia;
  15. public $forma_pago;
  16. public $impuesto;
  17. public $nuevo_albaran_url;
  18. public $proveedor_s;
  19. public $serie;
  20. public function __construct()
  21. {
  22. parent::__construct(__CLASS__, 'editar factura', 'compras', FALSE, FALSE);
  23. }
  24. protected function private_core()
  25. {
  26. $this->divisa = new divisa();
  27. $this->fabricante = new fabricante();
  28. $this->familia = new familia();
  29. $this->forma_pago = new forma_pago();
  30. $this->impuesto = new impuesto();
  31. $this->serie = new serie();
  32. /// comprobamos si el usuario tiene acceso a nueva_compra
  33. $this->nuevo_albaran_url = FALSE;
  34. if( $this->user->have_access_to('nueva_compra', FALSE) )
  35. {
  36. $nuevoalbp = $this->page->get('nueva_compra');
  37. if($nuevoalbp)
  38. {
  39. $this->nuevo_albaran_url = $nuevoalbp->url();
  40. }
  41. }
  42. $this->factura = FALSE;
  43. if( isset($_REQUEST['id']) )
  44. {
  45. $fact0 = new factura_proveedor();
  46. $this->factura = $fact0->get($_REQUEST['id']);
  47. }
  48. if($this->factura)
  49. {
  50. $proveedor = new proveedor();
  51. $this->proveedor_s = $proveedor->get($this->factura->codproveedor);
  52. if( isset($_POST['numlineas']) )
  53. {
  54. $this->modificar_factura();
  55. }
  56. }
  57. else
  58. $this->new_error_msg('Factura no encontrada.');
  59. $this->share_extensions();
  60. }
  61. public function url()
  62. {
  63. if( !isset($this->factura) )
  64. {
  65. return parent::url();
  66. }
  67. else if($this->factura)
  68. {
  69. return $this->page->url().'&id='.$this->factura->idfactura;
  70. }
  71. else
  72. return $this->page->url();
  73. }
  74. private function share_extensions()
  75. {
  76. $extension = array(
  77. 'name' => 'editar_facturap',
  78. 'page_from' => __CLASS__,
  79. 'page_to' => 'compras_factura',
  80. 'type' => 'button',
  81. 'text' => '<span class="glyphicon glyphicon-edit" aria-hidden="true"></span> &nbsp; Editar',
  82. 'params' => ''
  83. );
  84. $fsext = new fs_extension($extension);
  85. $fsext->save();
  86. }
  87. private function modificar_factura()
  88. {
  89. $asient0 = new asiento();
  90. $articulo = new articulo();
  91. /// paso 1, eliminamos los asientos asociados
  92. if( !is_null($this->factura->idasiento) )
  93. {
  94. $asiento = $asient0->get($this->factura->idasiento);
  95. if($asiento)
  96. {
  97. if( $asiento->delete() )
  98. {
  99. $this->factura->idasiento = NULL;
  100. }
  101. }
  102. else
  103. $this->factura->idasiento = NULL;
  104. }
  105. /// asiento de pago
  106. if( !is_null($this->factura->idasientop) )
  107. {
  108. $asiento = $asient0->get($this->factura->idasientop);
  109. if($asiento)
  110. {
  111. if( $asiento->delete() )
  112. {
  113. $this->factura->idasientop = NULL;
  114. }
  115. }
  116. else
  117. $this->factura->idasientop = NULL;
  118. }
  119. /// paso 2, eliminar las líneas de IVA
  120. foreach($this->factura->get_lineas_iva() as $liva)
  121. {
  122. $liva->delete();
  123. }
  124. /// paso 3, eliminar los recibos asociados
  125. if( class_exists('recibo_proveedor') )
  126. {
  127. $borrar = TRUE;
  128. $recibo0 = new recibo_proveedor();
  129. foreach($recibo0->all_from_factura($this->factura->idfactura) as $rec)
  130. {
  131. if($rec->estado == 'Pagado')
  132. {
  133. $borrar = FALSE;
  134. break;
  135. }
  136. }
  137. if($borrar)
  138. {
  139. foreach($recibo0->all_from_factura($this->factura->idfactura) as $rec)
  140. {
  141. $rec->delete();
  142. }
  143. }
  144. else
  145. {
  146. $this->new_error_msg('Ya hay recibos pagados. No se puede modificar la factura.');
  147. return FALSE;
  148. }
  149. }
  150. /// ¿Cambiamos el proveedor?
  151. if($_POST['proveedor'] != $this->factura->codproveedor)
  152. {
  153. $this->proveedor_s = $this->proveedor_s->get($_POST['proveedor']);
  154. if($this->proveedor_s)
  155. {
  156. $this->factura->codproveedor = $this->proveedor_s->codproveedor;
  157. $this->factura->nombre = $this->proveedor_s->razonsocial;
  158. $this->factura->cifnif = $this->proveedor_s->cifnif;
  159. }
  160. else
  161. {
  162. $this->new_error_msg('No se ha encontrado el proveedor');
  163. return FALSE;
  164. }
  165. }
  166. else
  167. $this->factura->cifnif = $_POST['cifnif'];
  168. $this->factura->numproveedor = $_POST['numproveedor'];
  169. $this->factura->fecha = $_POST['fecha'];
  170. $this->factura->hora = $_POST['hora'];
  171. $this->factura->observaciones = $_POST['observaciones'];
  172. $this->factura->neto = 0;
  173. $this->factura->totaliva = 0;
  174. $this->factura->totalirpf = 0;
  175. $this->factura->totalrecargo = 0;
  176. $this->factura->irpf = 0;
  177. $this->factura->pagada = isset($_POST['pagada']);
  178. $this->factura->anulada = isset($_POST['anulada']);
  179. /// ¿Cambiamos la divisa?
  180. if($_POST['divisa'] != $this->factura->coddivisa)
  181. {
  182. $divisa = $this->divisa->get($_POST['divisa']);
  183. if($divisa)
  184. {
  185. $this->factura->coddivisa = $divisa->coddivisa;
  186. $this->factura->tasaconv = $divisa->tasaconv_compra;
  187. }
  188. }
  189. else if($_POST['tasaconv'] != '')
  190. {
  191. $this->factura->tasaconv = floatval($_POST['tasaconv']);
  192. }
  193. /// ¿Cambiamos la forma de pago?
  194. if($_POST['forma_pago'] != $this->factura->codpago)
  195. {
  196. $formap = $this->forma_pago->get($_POST['forma_pago']);
  197. if($formap)
  198. {
  199. $this->factura->codpago = $formap->codpago;
  200. }
  201. }
  202. /// ¿Cambiamos la serie?
  203. if($_POST['serie'] != $this->factura->codserie)
  204. {
  205. $serie2 = $this->serie->get($_POST['serie']);
  206. if($serie2)
  207. {
  208. $this->factura->codserie = $serie2->codserie;
  209. $this->factura->new_codigo();
  210. }
  211. }
  212. /// eliminamos las líneas que no encontremos en el $_POST
  213. $serie = $this->serie->get($this->factura->codserie);
  214. $numlineas = intval($_POST['numlineas']);
  215. $lineas = $this->factura->get_lineas();
  216. foreach($lineas as $l)
  217. {
  218. $encontrada = FALSE;
  219. for($num = 0; $num <= $numlineas; $num++)
  220. {
  221. if( isset($_POST['idlinea_'.$num]) )
  222. {
  223. if($l->idlinea == intval($_POST['idlinea_'.$num]))
  224. {
  225. $encontrada = TRUE;
  226. break;
  227. }
  228. }
  229. }
  230. if(!$encontrada)
  231. {
  232. if( $l->delete() )
  233. {
  234. /// actualizamos el stock
  235. $art0 = $articulo->get($l->referencia);
  236. if($art0)
  237. {
  238. $art0->sum_stock($this->factura->codalmacen, 0 - $l->cantidad);
  239. }
  240. }
  241. else
  242. $this->new_error_msg("¡Imposible eliminar la línea del artículo ".$l->referencia."!");
  243. }
  244. }
  245. /// modificamos y/o añadimos las demás líneas
  246. for($num = 0; $num <= $numlineas; $num++)
  247. {
  248. $encontrada = FALSE;
  249. if( isset($_POST['idlinea_'.$num]) )
  250. {
  251. foreach($lineas as $k => $value)
  252. {
  253. /// modificamos la línea
  254. if($value->idlinea == intval($_POST['idlinea_'.$num]))
  255. {
  256. $encontrada = TRUE;
  257. $cantidad_old = $value->cantidad;
  258. $lineas[$k]->cantidad = floatval($_POST['cantidad_'.$num]);
  259. $lineas[$k]->pvpunitario = floatval($_POST['pvp_'.$num]);
  260. $lineas[$k]->dtopor = floatval($_POST['dto_'.$num]);
  261. $lineas[$k]->dtolineal = 0;
  262. $lineas[$k]->pvpsindto = ($value->cantidad * $value->pvpunitario);
  263. $lineas[$k]->pvptotal = ($value->cantidad * $value->pvpunitario * (100 - $value->dtopor)/100);
  264. $lineas[$k]->descripcion = $_POST['desc_'.$num];
  265. $lineas[$k]->codimpuesto = NULL;
  266. $lineas[$k]->iva = 0;
  267. $lineas[$k]->recargo = 0;
  268. $lineas[$k]->irpf = floatval($_POST['irpf_'.$num]);
  269. if( !$serie->siniva AND $this->proveedor_s->regimeniva != 'Exento' )
  270. {
  271. $imp0 = $this->impuesto->get_by_iva($_POST['iva_'.$num]);
  272. if($imp0)
  273. {
  274. $lineas[$k]->codimpuesto = $imp0->codimpuesto;
  275. }
  276. $lineas[$k]->iva = floatval($_POST['iva_'.$num]);
  277. $lineas[$k]->recargo = floatval($_POST['recargo_'.$num]);
  278. }
  279. if( $lineas[$k]->save() )
  280. {
  281. $this->factura->neto += $value->pvptotal;
  282. $this->factura->totaliva += $value->pvptotal * $value->iva/100;
  283. $this->factura->totalirpf += $value->pvptotal * $value->irpf/100;
  284. $this->factura->totalrecargo += $value->pvptotal * $value->recargo/100;
  285. if($value->irpf > $this->factura->irpf)
  286. {
  287. $this->factura->irpf = $value->irpf;
  288. }
  289. if($lineas[$k]->cantidad != $cantidad_old)
  290. {
  291. /// actualizamos el stock
  292. $art0 = $articulo->get($value->referencia);
  293. if($art0)
  294. {
  295. $art0->sum_stock($this->factura->codalmacen, $lineas[$k]->cantidad - $cantidad_old);
  296. }
  297. }
  298. }
  299. else
  300. $this->new_error_msg("¡Imposible modificar la línea del artículo ".$value->referencia."!");
  301. break;
  302. }
  303. }
  304. /// añadimos la línea
  305. if(!$encontrada AND intval($_POST['idlinea_'.$num]) == -1 AND isset($_POST['referencia_'.$num]))
  306. {
  307. $linea = new linea_factura_proveedor();
  308. $linea->idfactura = $this->factura->idfactura;
  309. $linea->descripcion = $_POST['desc_'.$num];
  310. if( !$serie->siniva AND $this->proveedor_s->regimeniva != 'Exento' )
  311. {
  312. $imp0 = $this->impuesto->get_by_iva($_POST['iva_'.$num]);
  313. if($imp0)
  314. {
  315. $linea->codimpuesto = $imp0->codimpuesto;
  316. }
  317. $linea->iva = floatval($_POST['iva_'.$num]);
  318. $linea->recargo = floatval($_POST['recargo_'.$num]);
  319. }
  320. $linea->irpf = floatval($_POST['irpf_'.$num]);
  321. $linea->cantidad = floatval($_POST['cantidad_'.$num]);
  322. $linea->pvpunitario = floatval($_POST['pvp_'.$num]);
  323. $linea->dtopor = floatval($_POST['dto_'.$num]);
  324. $linea->pvpsindto = ($linea->cantidad * $linea->pvpunitario);
  325. $linea->pvptotal = ($linea->cantidad * $linea->pvpunitario * (100 - $linea->dtopor)/100);
  326. $art0 = $articulo->get( $_POST['referencia_'.$num] );
  327. if($art0)
  328. {
  329. $linea->referencia = $art0->referencia;
  330. }
  331. if( $linea->save() )
  332. {
  333. if($art0)
  334. {
  335. /// actualizamos el stock
  336. $art0->sum_stock($this->factura->codalmacen, $linea->cantidad);
  337. }
  338. $this->factura->neto += $linea->pvptotal;
  339. $this->factura->totaliva += $linea->pvptotal * $linea->iva/100;
  340. $this->factura->totalirpf += $linea->pvptotal * $linea->irpf/100;
  341. $this->factura->totalrecargo += $linea->pvptotal * $linea->recargo/100;
  342. if($linea->irpf > $this->factura->irpf)
  343. {
  344. $this->factura->irpf = $linea->irpf;
  345. }
  346. }
  347. else
  348. $this->new_error_msg("¡Imposible guardar la línea del artículo ".$linea->referencia."!");
  349. }
  350. }
  351. }
  352. /// redondeamos
  353. $this->factura->neto = round($this->factura->neto, FS_NF0);
  354. $this->factura->totaliva = round($this->factura->totaliva, FS_NF0);
  355. $this->factura->totalirpf = round($this->factura->totalirpf, FS_NF0);
  356. $this->factura->totalrecargo = round($this->factura->totalrecargo, FS_NF0);
  357. $this->factura->total = $this->factura->neto + $this->factura->totaliva - $this->factura->totalirpf + $this->factura->totalrecargo;
  358. if( abs(floatval($_POST['atotal']) - $this->factura->total) >= .02 )
  359. {
  360. $this->new_error_msg("El total difiere entre el controlador y la vista (".$this->factura->total.
  361. " frente a ".$_POST['atotal']."). Debes informar del error.");
  362. }
  363. else if( $this->factura->save() )
  364. {
  365. $this->new_message('Factura modificada correctamente.');
  366. $this->generar_asiento();
  367. }
  368. else
  369. $this->new_error_msg('Imposible modificar la factura.');
  370. }
  371. private function generar_asiento()
  372. {
  373. if( $this->factura->get_asiento() )
  374. {
  375. $this->new_error_msg('Ya hay un asiento asociado a esta factura.');
  376. }
  377. else
  378. {
  379. $asiento_factura = new asiento_factura();
  380. $asiento_factura->soloasiento = TRUE;
  381. $asiento_factura->generar_asiento_compra($this->factura);
  382. foreach($asiento_factura->errors as $err)
  383. {
  384. $this->new_error_msg($err);
  385. }
  386. foreach($asiento_factura->messages as $msg)
  387. {
  388. $this->new_message($msg);
  389. }
  390. }
  391. }
  392. }