PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/facturacion_base/model/linea_albaran_cliente.php

https://gitlab.com/cosouth.battle/sartinofi
PHP | 509 lines | 382 code | 62 blank | 65 comment | 32 complexity | 707951c15946f8de2211128252286a9a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Sartin
  4. * Miguel San Martin - cosouth.battle@gmail.com
  5. */
  6. require_model('articulo.php');
  7. require_model('albaran_cliente.php');
  8. /**
  9. * Línea de un albarán de cliente (boceto de factura).
  10. */
  11. class linea_albaran_cliente extends fs_model
  12. {
  13. /**
  14. * Clave primaria.
  15. * @var type
  16. */
  17. public $idlinea;
  18. /**
  19. * ID de la línea del pedido relacionado, si es que lo hay.
  20. * @var type
  21. */
  22. public $idlineapedido;
  23. /**
  24. * ID del albaran de esta línea.
  25. * @var type
  26. */
  27. public $idalbaran;
  28. /**
  29. * ID del pedido relacionado con el albarán relacionado.
  30. * @var type
  31. */
  32. public $idpedido;
  33. /**
  34. * Referencia del artículo.
  35. * @var type
  36. */
  37. public $referencia;
  38. public $descripcion;
  39. public $cantidad;
  40. /**
  41. * % de descuento.
  42. * @var type
  43. */
  44. public $dtopor;
  45. /**
  46. * Impuesto del artículo.
  47. * @var type
  48. */
  49. public $codimpuesto;
  50. /**
  51. * % de IVA del artículo (el que corresponde al impuesto.
  52. * @var type
  53. */
  54. public $iva;
  55. /**
  56. * Importe neto de la línea, sin impuestos.
  57. * @var type
  58. */
  59. public $pvptotal;
  60. /**
  61. * Importe neto sin descuento, es decir, pvpunitario * cantidad.
  62. * @var type
  63. */
  64. public $pvpsindto;
  65. /**
  66. * Precio del artículo, una sola unidad.
  67. * @var type
  68. */
  69. public $pvpunitario;
  70. /**
  71. * % de IRPF de la línea.
  72. * @var type
  73. */
  74. public $irpf;
  75. /**
  76. * % de recargo de equivalencia de la línea.
  77. * @var type
  78. */
  79. public $recargo;
  80. private $codigo;
  81. private $fecha;
  82. private static $albaranes;
  83. public function __construct($l=FALSE)
  84. {
  85. parent::__construct('lineasalbaranescli');
  86. if( !isset(self::$albaranes) )
  87. self::$albaranes = array();
  88. if($l)
  89. {
  90. $this->idlinea = $this->intval($l['idlinea']);
  91. $this->idlineapedido = $this->intval($l['idlineapedido']);
  92. $this->idalbaran = $this->intval($l['idalbaran']);
  93. $this->idpedido = $this->intval($l['idpedido']);
  94. $this->referencia = $l['referencia'];
  95. $this->descripcion = $l['descripcion'];
  96. $this->cantidad = floatval($l['cantidad']);
  97. $this->dtopor = floatval($l['dtopor']);
  98. $this->codimpuesto = $l['codimpuesto'];
  99. $this->iva = floatval($l['iva']);
  100. $this->pvptotal = floatval($l['pvptotal']);
  101. $this->pvpsindto = floatval($l['pvpsindto']);
  102. $this->pvpunitario = floatval($l['pvpunitario']);
  103. $this->irpf = floatval($l['irpf']);
  104. $this->recargo = floatval($l['recargo']);
  105. }
  106. else
  107. {
  108. $this->idlinea = NULL;
  109. $this->idlineapedido = NULL;
  110. $this->idalbaran = NULL;
  111. $this->idpedido = NULL;
  112. $this->referencia = NULL;
  113. $this->descripcion = '';
  114. $this->cantidad = 0;
  115. $this->dtopor = 0;
  116. $this->codimpuesto = NULL;
  117. $this->iva = 0;
  118. $this->pvptotal = 0;
  119. $this->pvpsindto = 0;
  120. $this->pvpunitario = 0;
  121. $this->irpf = 0;
  122. $this->recargo = 0;
  123. }
  124. }
  125. protected function install()
  126. {
  127. return '';
  128. }
  129. private function fill()
  130. {
  131. $encontrado = FALSE;
  132. foreach(self::$albaranes as $a)
  133. {
  134. if($a->idalbaran == $this->idalbaran)
  135. {
  136. $this->codigo = $a->codigo;
  137. $this->fecha = $a->fecha;
  138. $encontrado = TRUE;
  139. break;
  140. }
  141. }
  142. if( !$encontrado )
  143. {
  144. $alb = new albaran_cliente();
  145. $alb = $alb->get($this->idalbaran);
  146. if( $alb )
  147. {
  148. $this->codigo = $alb->codigo;
  149. $this->fecha = $alb->fecha;
  150. self::$albaranes[] = $alb;
  151. }
  152. }
  153. }
  154. public function pvp_iva()
  155. {
  156. return $this->pvpunitario*(100+$this->iva)/100;
  157. }
  158. public function total_iva()
  159. {
  160. return $this->pvptotal*(100+$this->iva-$this->irpf+$this->recargo)/100;
  161. }
  162. /// Devuelve el precio total por unidad (con descuento incluido e iva aplicado)
  163. public function total_iva2()
  164. {
  165. if($this->cantidad == 0)
  166. {
  167. return 0;
  168. }
  169. else
  170. return $this->pvptotal*(100+$this->iva)/100/$this->cantidad;
  171. }
  172. public function descripcion()
  173. {
  174. return nl2br($this->descripcion);
  175. }
  176. public function show_codigo()
  177. {
  178. if( !isset($this->codigo) )
  179. $this->fill();
  180. return $this->codigo;
  181. }
  182. public function show_fecha()
  183. {
  184. if( !isset($this->fecha) )
  185. $this->fill();
  186. return $this->fecha;
  187. }
  188. public function show_nombrecliente()
  189. {
  190. $nombre = 'desconocido';
  191. foreach(self::$albaranes as $a)
  192. {
  193. if($a->idalbaran == $this->idalbaran)
  194. {
  195. $nombre = $a->nombrecliente;
  196. break;
  197. }
  198. }
  199. return $nombre;
  200. }
  201. public function url()
  202. {
  203. return 'index.php?page=ventas_albaran&id='.$this->idalbaran;
  204. }
  205. public function articulo_url()
  206. {
  207. if( is_null($this->referencia) OR $this->referencia == '')
  208. {
  209. return "index.php?page=ventas_articulos";
  210. }
  211. else
  212. return "index.php?page=ventas_articulo&ref=".urlencode($this->referencia);
  213. }
  214. public function exists()
  215. {
  216. if( is_null($this->idlinea) )
  217. {
  218. return FALSE;
  219. }
  220. else
  221. return $this->db->select("SELECT * FROM ".$this->table_name." WHERE idlinea = ".$this->var2str($this->idlinea).";");
  222. }
  223. public function test()
  224. {
  225. $this->descripcion = $this->no_html($this->descripcion);
  226. $total = $this->pvpunitario * $this->cantidad * (100 - $this->dtopor) / 100;
  227. $totalsindto = $this->pvpunitario * $this->cantidad;
  228. if( !$this->floatcmp($this->pvptotal, $total, FS_NF0, TRUE) )
  229. {
  230. $this->new_error_msg("Error en el valor de pvptotal de la línea ".$this->referencia." del ".FS_ALBARAN.". Valor correcto: ".$total);
  231. return FALSE;
  232. }
  233. else if( !$this->floatcmp($this->pvpsindto, $totalsindto, FS_NF0, TRUE) )
  234. {
  235. $this->new_error_msg("Error en el valor de pvpsindto de la línea ".$this->referencia." del ".FS_ALBARAN.". Valor correcto: ".$totalsindto);
  236. return FALSE;
  237. }
  238. else
  239. return TRUE;
  240. }
  241. public function save()
  242. {
  243. if( $this->test() )
  244. {
  245. $this->clean_cache();
  246. if( $this->exists() )
  247. {
  248. $sql = "UPDATE ".$this->table_name." SET idalbaran = ".$this->var2str($this->idalbaran)
  249. .", idpedido = ".$this->var2str($this->idpedido)
  250. .", idlineapedido = ".$this->var2str($this->idlineapedido)
  251. .", referencia = ".$this->var2str($this->referencia)
  252. .", descripcion = ".$this->var2str($this->descripcion)
  253. .", cantidad = ".$this->var2str($this->cantidad)
  254. .", dtopor = ".$this->var2str($this->dtopor)
  255. .", codimpuesto = ".$this->var2str($this->codimpuesto)
  256. .", iva = ".$this->var2str($this->iva)
  257. .", pvptotal = ".$this->var2str($this->pvptotal)
  258. .", pvpsindto = ".$this->var2str($this->pvpsindto)
  259. .", pvpunitario = ".$this->var2str($this->pvpunitario)
  260. .", irpf = ".$this->var2str($this->irpf)
  261. .", recargo = ".$this->var2str($this->recargo)
  262. ." WHERE idlinea = ".$this->var2str($this->idlinea).";";
  263. return $this->db->exec($sql);
  264. }
  265. else
  266. {
  267. $sql = "INSERT INTO ".$this->table_name." (idlineapedido,idalbaran,idpedido,referencia,descripcion,
  268. cantidad,dtopor,codimpuesto,iva,pvptotal,pvpsindto,pvpunitario,irpf,recargo) VALUES
  269. (".$this->var2str($this->idlineapedido).
  270. ",".$this->var2str($this->idalbaran).
  271. ",".$this->var2str($this->idpedido).
  272. ",".$this->var2str($this->referencia).
  273. ",".$this->var2str($this->descripcion).
  274. ",".$this->var2str($this->cantidad).
  275. ",".$this->var2str($this->dtopor).
  276. ",".$this->var2str($this->codimpuesto).
  277. ",".$this->var2str($this->iva).
  278. ",".$this->var2str($this->pvptotal).
  279. ",".$this->var2str($this->pvpsindto).
  280. ",".$this->var2str($this->pvpunitario).
  281. ",".$this->var2str($this->irpf).
  282. ",".$this->var2str($this->recargo).");";
  283. if( $this->db->exec($sql) )
  284. {
  285. $this->idlinea = $this->db->lastval();
  286. return TRUE;
  287. }
  288. else
  289. return FALSE;
  290. }
  291. }
  292. else
  293. return FALSE;
  294. }
  295. public function delete()
  296. {
  297. $this->clean_cache();
  298. return $this->db->exec("DELETE FROM ".$this->table_name." WHERE idlinea = ".$this->var2str($this->idlinea).";");
  299. }
  300. public function clean_cache()
  301. {
  302. $this->cache->delete('albcli_top_articulos');
  303. }
  304. public function all_from_albaran($id)
  305. {
  306. $linealist = array();
  307. $lineas = $this->db->select("SELECT * FROM ".$this->table_name." WHERE idalbaran = ".$this->var2str($id)." ORDER BY idlinea ASC;");
  308. if($lineas)
  309. {
  310. foreach($lineas as $l)
  311. $linealist[] = new linea_albaran_cliente($l);
  312. }
  313. return $linealist;
  314. }
  315. public function all_from_articulo($ref, $offset=0, $limit=FS_ITEM_LIMIT)
  316. {
  317. $linealist = array();
  318. $lineas = $this->db->select_limit("SELECT * FROM ".$this->table_name.
  319. " WHERE referencia = ".$this->var2str($ref).
  320. " ORDER BY idalbaran DESC", $limit, $offset);
  321. if( $lineas )
  322. {
  323. foreach($lineas as $l)
  324. $linealist[] = new linea_albaran_cliente($l);
  325. }
  326. return $linealist;
  327. }
  328. public function search($query='', $offset=0)
  329. {
  330. $linealist = array();
  331. $query = mb_strtolower( $this->no_html($query), 'UTF8' );
  332. $sql = "SELECT * FROM ".$this->table_name." WHERE ";
  333. if( is_numeric($query) )
  334. {
  335. $sql .= "referencia LIKE '%".$query."%' OR descripcion LIKE '%".$query."%'";
  336. }
  337. else
  338. {
  339. $buscar = str_replace(' ', '%', $query);
  340. $sql .= "lower(referencia) LIKE '%".$buscar."%' OR lower(descripcion) LIKE '%".$buscar."%'";
  341. }
  342. $sql .= " ORDER BY idalbaran DESC, idlinea ASC";
  343. $data = $this->db->select_limit($sql, FS_ITEM_LIMIT, $offset);
  344. if($data)
  345. {
  346. foreach($data as $l)
  347. {
  348. $linealist[] = new linea_albaran_cliente($l);
  349. }
  350. }
  351. return $linealist;
  352. }
  353. public function search_from_cliente($codcliente, $query='', $offset=0)
  354. {
  355. $linealist = array();
  356. $query = mb_strtolower( $this->no_html($query), 'UTF8' );
  357. $sql = "SELECT * FROM ".$this->table_name." WHERE idalbaran IN
  358. (SELECT idalbaran FROM albaranescli WHERE codcliente = ".$this->var2str($codcliente).") AND ";
  359. if( is_numeric($query) )
  360. {
  361. $sql .= "(referencia LIKE '%".$query."%' OR descripcion LIKE '%".$query."%')";
  362. }
  363. else
  364. {
  365. $buscar = str_replace(' ', '%', $query);
  366. $sql .= "(lower(referencia) LIKE '%".$buscar."%' OR lower(descripcion) LIKE '%".$buscar."%')";
  367. }
  368. $sql .= " ORDER BY idalbaran DESC, idlinea ASC";
  369. $data = $this->db->select_limit($sql, FS_ITEM_LIMIT, $offset);
  370. if($data)
  371. {
  372. foreach($data as $l)
  373. {
  374. $linealist[] = new linea_albaran_cliente($l);
  375. }
  376. }
  377. return $linealist;
  378. }
  379. public function search_from_cliente2($codcliente, $ref='', $obs='', $offset=0)
  380. {
  381. $linealist = array();
  382. $ref = mb_strtolower( $this->no_html($ref), 'UTF8' );
  383. $sql = "SELECT * FROM ".$this->table_name." WHERE idalbaran IN
  384. (SELECT idalbaran FROM albaranescli WHERE codcliente = ".$this->var2str($codcliente)."
  385. AND lower(observaciones) LIKE '".mb_strtolower($obs, 'UTF8')."%') AND ";
  386. if( is_numeric($ref) )
  387. {
  388. $sql .= "(referencia LIKE '%".$ref."%' OR descripcion LIKE '%".$ref."%')";
  389. }
  390. else
  391. {
  392. $buscar = str_replace(' ', '%', $ref);
  393. $sql .= "(lower(referencia) LIKE '%".$ref."%' OR lower(descripcion) LIKE '%".$ref."%')";
  394. }
  395. $sql .= " ORDER BY idalbaran DESC, idlinea ASC";
  396. $data = $this->db->select_limit($sql, FS_ITEM_LIMIT, $offset);
  397. if($data)
  398. {
  399. foreach($data as $l)
  400. {
  401. $linealist[] = new linea_albaran_cliente($l);
  402. }
  403. }
  404. return $linealist;
  405. }
  406. public function last_from_cliente($codcliente, $offset=0)
  407. {
  408. $linealist = array();
  409. $sql = "SELECT * FROM ".$this->table_name." WHERE idalbaran IN
  410. (SELECT idalbaran FROM albaranescli WHERE codcliente = ".$this->var2str($codcliente).")
  411. ORDER BY idalbaran DESC, idlinea ASC";
  412. $lineas = $this->db->select_limit($sql, FS_ITEM_LIMIT, $offset);
  413. if( $lineas )
  414. {
  415. foreach($lineas as $l)
  416. $linealist[] = new linea_albaran_cliente($l);
  417. }
  418. return $linealist;
  419. }
  420. public function count_by_articulo()
  421. {
  422. $lineas = $this->db->select("SELECT COUNT(DISTINCT referencia) as total FROM ".$this->table_name.";");
  423. if($lineas)
  424. {
  425. return intval($lineas[0]['total']);
  426. }
  427. else
  428. return 0;
  429. }
  430. /**
  431. * Devuelve los datos de una linea
  432. * @param type $idlinea
  433. * @return new linea_albaran_cliente
  434. */
  435. public function get($idlinea)
  436. {
  437. $data = $this->db->select("SELECT * FROM ".$this->table_name." WHERE idlinea = ".$this->var2str($idlinea).";");
  438. if($data)
  439. {
  440. return new linea_albaran_cliente($data[0]);
  441. }
  442. else
  443. {
  444. return FALSE;
  445. }
  446. }
  447. }