PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/caja.c

#
C | 847 lines | 476 code | 140 blank | 231 comment | 63 complexity | b9dd486fae621de57fea52b1b1e57610 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4;
  2. c-indentation-style: gnu -*- */
  3. /*caja.c
  4. *
  5. * Copyright (C) 2004,2008 Rizoma Tecnologia Limitada <info@rizoma.cl>
  6. *
  7. * This file is part of rizoma.
  8. *
  9. * Rizoma is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include<gtk/gtk.h>
  24. #include<stdlib.h>
  25. #include<string.h>
  26. #include"tipos.h"
  27. #include"postgres-functions.h"
  28. #include"config_file.h"
  29. #include"errors.h"
  30. #include"caja.h"
  31. #include"utils.h"
  32. #include"vale.h"
  33. GtkWidget *calendar_win;
  34. guint day, month, year;
  35. GtkWidget *inicio_caja;
  36. GtkWidget *ventas_efect;
  37. GtkWidget *ventas_doc;
  38. GtkWidget *pago_ventas;
  39. GtkWidget *otros_ingresos;
  40. GtkWidget *total_haberes;
  41. GtkWidget *pagos;
  42. GtkWidget *retiros;
  43. GtkWidget *gastos_corrientes;
  44. GtkWidget *otros_egresos;
  45. GtkWidget *total_debitos;
  46. GtkWidget *total_caja;
  47. GtkWidget *combo_egreso;
  48. GtkWidget *combo_ingreso;
  49. /* FLAGS */
  50. gboolean con_cierre; //Indica si se realiza un ingreso o egreso de caja con cierre
  51. /**
  52. * Es llamada por la funcion EgresarDinero.
  53. *
  54. * Esta funcion retorna el valor de inicio de la caja
  55. *
  56. * @return caja: entero que contiene el resultado de la consulta a la caja
  57. *
  58. */
  59. gint
  60. ReturnSaldoCaja (void)
  61. {
  62. PGresult *res;
  63. gint caja;
  64. res = EjecutarSQL ("select * from get_arqueo_caja(-1)");
  65. if (res != NULL && PQntuples (res) > 0)
  66. caja = atoi (PQgetvalue(res, 0, 0));
  67. else
  68. caja = 0;
  69. return caja;
  70. }
  71. /**
  72. * Es llamada por la funcion IngresarDinero.
  73. *
  74. * Esta funcion cierra la ventana "wnd_caja_ingreso" y limpia la caja de
  75. * texto "entry_caja_in_amount"
  76. *
  77. */
  78. void
  79. CloseVentanaIngreso(void)
  80. {
  81. GtkWidget *wid;
  82. wid = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_in_amount"));
  83. gtk_entry_set_text(GTK_ENTRY(wid), "");
  84. wid = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_ingreso"));
  85. gtk_widget_hide(wid);
  86. if (con_cierre == TRUE)
  87. {
  88. con_cierre = FALSE;
  89. }
  90. }
  91. /**
  92. * Es llamada cuando el boton "btn_ingresarDinero" es presionado (signal click).
  93. *
  94. * Esta funcion carga el valor desde la caja de texto "entry_caja_in_amount"
  95. * en monto y luego llama a la funcion Ingreso, que realiza el ingreso de
  96. * dinero y su motivo, finalemente llama a la funcion CloseVentanaIngreso
  97. * para cerrar la ventana.
  98. *
  99. * @param button the button
  100. * @param user_data the user data
  101. *
  102. */
  103. void
  104. IngresarDinero (GtkWidget *widget, gpointer data)
  105. {
  106. GtkWidget *aux_widget;
  107. GtkTreeModel *model;
  108. GtkTreeIter iter;
  109. gint monto;
  110. gint motivo;
  111. gchar *motivo_texto;
  112. /*De estar habilitada caja, se asegura que ésta se encuentre
  113. abierta al momento de vender*/
  114. if (rizoma_get_value_boolean ("CAJA"))
  115. if (check_caja()) // Se abre la caja en caso de que está cerrada
  116. open_caja (TRUE);
  117. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "cmb_caja_in_motiv"));
  118. model = gtk_combo_box_get_model(GTK_COMBO_BOX(aux_widget));
  119. if (!(gtk_combo_box_get_active_iter(GTK_COMBO_BOX(aux_widget), &iter)))
  120. {
  121. ErrorMSG(aux_widget, "Debe seleccionar un tipo de ingreso");
  122. return;
  123. }
  124. gtk_tree_model_get (model, &iter,
  125. 0, &motivo,
  126. 1, &motivo_texto,
  127. -1);
  128. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_in_amount"));
  129. monto = atoi(gtk_entry_get_text(GTK_ENTRY(aux_widget)));
  130. if (monto == 0)
  131. {
  132. ErrorMSG (data, "No pueden haber ingresos de $0");
  133. return;
  134. }
  135. if (Ingreso (monto, motivo, user_data->user_id))
  136. {
  137. print_cash_box_info (get_last_cash_box_id (), monto, 0, motivo_texto);
  138. if (con_cierre == TRUE)
  139. {
  140. if (CerrarCaja(ReturnSaldoCaja()))
  141. {
  142. con_cierre = FALSE;
  143. gtk_widget_show_all (GTK_WIDGET (builder_get (builder, "dialog_cash_box_closed")));
  144. }
  145. else
  146. ErrorMSG (aux_widget, "No se pudo cerrar la caja apropiadamente\nPor favor intente nuevamente");
  147. }
  148. CloseVentanaIngreso ();
  149. }
  150. else
  151. {
  152. ErrorMSG(aux_widget, "No fue posible registrar el ingreso de dinero en la caja");
  153. return;
  154. }
  155. }
  156. /**
  157. * Es llamada por la funcion "IniciarLaCaja".
  158. *
  159. * Esta funcion visualiza la ventana "wnd_caja_ingreso" y carga en la lista
  160. * desplegable(combobox) los motivos del ingreso de dinero a la caja,
  161. *
  162. * @param monto: entero que contiene el monto de inicio de la caja
  163. *
  164. */
  165. void
  166. VentanaIngreso (gint monto)
  167. {
  168. GtkWidget *aux_widget;
  169. PGresult *res;
  170. gint tuples, i;
  171. GtkListStore *store;
  172. GtkTreeIter iter;
  173. res = EjecutarSQL ("SELECT id, descrip FROM tipo_ingreso");
  174. tuples = PQntuples (res);
  175. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "cmb_caja_in_motiv"));
  176. store = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(aux_widget)));
  177. if (store == NULL)
  178. {
  179. GtkCellRenderer *cell;
  180. store = gtk_list_store_new (2,
  181. G_TYPE_INT,
  182. G_TYPE_STRING);
  183. gtk_combo_box_set_model(GTK_COMBO_BOX(aux_widget), GTK_TREE_MODEL(store));
  184. cell = gtk_cell_renderer_text_new();
  185. gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(aux_widget), cell, TRUE);
  186. gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(aux_widget), cell,
  187. "text", 1,
  188. NULL);
  189. }
  190. gtk_list_store_clear(store);
  191. for (i=0 ; i < tuples ; i++)
  192. {
  193. gtk_list_store_append(store, &iter);
  194. gtk_list_store_set(store, &iter,
  195. 0, atoi(PQvaluebycol(res, i, "id")),
  196. 1, PQvaluebycol(res, i, "descrip"),
  197. -1);
  198. }
  199. gtk_combo_box_set_active (GTK_COMBO_BOX(aux_widget), 0);
  200. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_in_amount"));
  201. gtk_entry_set_text(GTK_ENTRY(aux_widget), g_strdup_printf("%d", monto));
  202. gtk_widget_grab_focus(aux_widget);
  203. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_ingreso"));
  204. gtk_widget_show_all(aux_widget);
  205. }
  206. /**
  207. * Es llamada cuando el boton "btn_egresar_dinero" es presionado
  208. * (signal click).
  209. *
  210. * Esta funcion carga el monto que de egreso y luego a llama a la funcion
  211. * Egreso que realiza el egreso y el motivo
  212. *
  213. * @param button the button
  214. * @param user_data the user data
  215. *
  216. */
  217. void
  218. EgresarDinero (GtkWidget *widget, gpointer data)
  219. {
  220. GtkWidget *aux_widget;
  221. gint active;
  222. gint monto;
  223. gint motivo;
  224. gint saldo_en_caja;
  225. gchar *motivo_texto;
  226. GtkTreeModel *model;
  227. GtkTreeIter iter;
  228. /*Saldo en caja*/
  229. saldo_en_caja = ReturnSaldoCaja ();
  230. /*De estar habilitada caja, se asegura que ésta se encuentre
  231. abierta al momento de vender*/
  232. if (rizoma_get_value_boolean ("CAJA"))
  233. if (check_caja()) // Se abre la caja en caso de que está cerrada
  234. open_caja (TRUE);
  235. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_out_amount"));
  236. monto = atoi (gtk_entry_get_text (GTK_ENTRY (aux_widget)));
  237. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "cmb_caja_out_motiv"));
  238. active = gtk_combo_box_get_active (GTK_COMBO_BOX (aux_widget));
  239. if (active == -1)
  240. ErrorMSG (aux_widget, "Debe seleccionar un tipo de egreso");
  241. else if (monto <= 0)
  242. ErrorMSG (aux_widget, "No pueden haber egresos de monto $0 o menor");
  243. else if (monto > saldo_en_caja)
  244. ErrorMSG (aux_widget, "No se puede retirar más dinero del que hay en caja");
  245. else
  246. {
  247. model = gtk_combo_box_get_model (GTK_COMBO_BOX (aux_widget));
  248. gtk_combo_box_get_active_iter (GTK_COMBO_BOX (aux_widget), &iter);
  249. gtk_tree_model_get (model, &iter,
  250. 0, &motivo,
  251. 1, &motivo_texto,
  252. -1);
  253. if (Egresar (monto, motivo, user_data->user_id))
  254. {
  255. print_cash_box_info (get_last_cash_box_id (), 0, monto, motivo_texto);
  256. if (con_cierre == TRUE)
  257. {
  258. if (CerrarCaja(ReturnSaldoCaja()))
  259. {
  260. con_cierre = FALSE;
  261. gtk_widget_show_all (GTK_WIDGET (builder_get (builder, "dialog_cash_box_closed")));
  262. }
  263. else
  264. ErrorMSG (aux_widget, "No se pudo cerrar la caja apropiadamente\nPor favor intente nuevamente");
  265. }
  266. CloseVentanaEgreso();
  267. }
  268. else
  269. ErrorMSG(aux_widget, "No fue posible ingresar el egreso de dinero de la caja");
  270. }
  271. }
  272. /**
  273. * Es llamada por la funcion EgresarDinero.
  274. *
  275. * Esta funcion cierra la ventana "wnd_caja_egreso" y limpia la caja de
  276. * texto "entry_caja_out_amount"
  277. *
  278. */
  279. void
  280. CloseVentanaEgreso (void)
  281. {
  282. GtkWidget *wid;
  283. wid = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_out_amount"));
  284. gtk_entry_set_text(GTK_ENTRY(wid), "");
  285. wid = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_egreso"));
  286. gtk_widget_hide(wid);
  287. if (con_cierre == TRUE)
  288. {
  289. con_cierre = FALSE;
  290. }
  291. }
  292. /**
  293. * Es llamada por la funcion "IniciarLaCaja".
  294. *
  295. * Esta funcion visualiza la ventana "wnd_caja_egreso" y carga en la lista
  296. * desplegable(combobox) los motivos del ingreso de dinero a la caja,
  297. *
  298. * @param monto: entero que contiene el monto de inicio de la caja
  299. *
  300. */
  301. void
  302. VentanaEgreso (gint monto)
  303. {
  304. GtkWidget *combo;
  305. GtkWidget *aux_widget;
  306. GtkListStore *store;
  307. GtkTreeIter iter;
  308. PGresult *res, *resId;
  309. gint tuples, i;
  310. gint nulVenId; // El Id de "Nulidad de venta"
  311. res = EjecutarSQL ("SELECT id, descrip FROM tipo_egreso");
  312. tuples = PQntuples (res);
  313. combo = GTK_WIDGET (gtk_builder_get_object(builder, "cmb_caja_out_motiv"));
  314. store = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(combo)));
  315. if (store == NULL)
  316. {
  317. GtkCellRenderer *cell;
  318. store = gtk_list_store_new(2,
  319. G_TYPE_INT,
  320. G_TYPE_STRING);
  321. gtk_combo_box_set_model(GTK_COMBO_BOX(combo),GTK_TREE_MODEL(store));
  322. cell = gtk_cell_renderer_text_new();
  323. gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(combo), cell, TRUE);
  324. gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo), cell,
  325. "text", 1,
  326. NULL);
  327. }
  328. gtk_list_store_clear(store);
  329. /*Obtención del Id de 'nulidad de venta' */
  330. resId = EjecutarSQL ("SELECT id FROM tipo_egreso WHERE descrip='Nulidad de Venta'");
  331. nulVenId = atoi (PUT(PQvaluebycol(resId, 0, "id")));
  332. /*Poblamiento del combobox*/
  333. for (i = 0; i < tuples; i++)
  334. {
  335. if (atoi (PUT(PQvaluebycol(res, i, "id"))) != nulVenId) /*No queremos que el motivo "Nulidad de Venta" aparezca como opción*/
  336. {
  337. gtk_list_store_append(store, &iter);
  338. gtk_list_store_set(store, &iter,
  339. 0, atoi(PQvaluebycol(res, i, "id")),
  340. 1, PQvaluebycol(res, i, "descrip"),
  341. -1);
  342. }
  343. }
  344. gtk_combo_box_set_active (GTK_COMBO_BOX(combo), 0);
  345. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_out_amount"));
  346. gtk_entry_set_text(GTK_ENTRY(aux_widget), g_strdup_printf("%d", monto));
  347. gtk_widget_grab_focus(aux_widget);
  348. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_egreso"));
  349. gtk_widget_show_all(aux_widget);
  350. }
  351. /**
  352. * Es llamada por las funciones "IniciarLaCaja" y "open_caja"
  353. *
  354. * Esta funcion insertar los valores iniciales de la caja
  355. * (id_vendedor,fecha_inicio, inicio(monto))
  356. *
  357. * @return TRUE si se realizo correctamented, FALSE si fallo.
  358. *
  359. */
  360. gboolean
  361. InicializarCaja (gint monto)
  362. {
  363. PGresult *res;
  364. gchar *q;
  365. q = g_strdup_printf ("INSERT INTO caja (id_vendedor, fecha_inicio, inicio) "
  366. "VALUES(%d, NOW(), %d)", user_data->user_id, monto);
  367. res = EjecutarSQL (q);
  368. g_free (q);
  369. if (res != NULL)
  370. return TRUE;
  371. else
  372. return FALSE;
  373. }
  374. /**
  375. * Es llamada por la funcion "CerrarCajaWin"
  376. *
  377. * Esta funcion retorna el valor de inicio de la caja
  378. *
  379. * @return monto de la consulta, -1 si fallo
  380. *
  381. */
  382. gint
  383. ArqueoCaja (void)
  384. {
  385. PGresult *res;
  386. res = EjecutarSQL("select * from get_arqueo_caja (-1)");
  387. if ((res != NULL) && (PQntuples(res)>0))
  388. return atoi (PQgetvalue (res, 0, 0));
  389. else
  390. return -1;
  391. }
  392. /**
  393. * Es llamada por la funcion "CerrarLaCaja"
  394. *
  395. * Esta funcion actualiza la caja para insertar los valores de cierre de la caja
  396. * (fecha_termino, termino(monto))
  397. *
  398. * @param monto entero que contiene el dinero de cierre la caja
  399. * @return TRUE si se realizo correctamented, FALSE si fallo.
  400. *
  401. */
  402. gboolean
  403. CerrarCaja (gint monto)
  404. {
  405. PGresult *res;
  406. gchar *q;
  407. if (monto == -1)
  408. monto = ArqueoCaja ();
  409. q = g_strdup_printf ("UPDATE caja SET fecha_termino=NOW(), termino=%d "
  410. "WHERE id=(SELECT last_value FROM caja_id_seq)",
  411. monto);
  412. res = EjecutarSQL (q);
  413. g_free (q);
  414. if (res != NULL || PQntuples (res) == 0)
  415. return TRUE;
  416. else
  417. return FALSE;
  418. }
  419. /**
  420. * Esta funcion es llamda por "prepare_caja"
  421. *
  422. * Retornamos TRUE si la caja fue cerrada anteriormente y debemos
  423. * inicilizarla otra vez de lo contrario FALSE lo cual significa que
  424. * la caja no se a cerrado
  425. *
  426. * @return TRUE when the caja was closed previously
  427. */
  428. gboolean
  429. check_caja (void)
  430. {
  431. PGresult *res;
  432. res = EjecutarSQL ("select is_caja_abierta()");
  433. if (PQntuples (res) == 0)
  434. {
  435. g_printerr("%s: could not retrieve the result of the sql query\n",
  436. G_STRFUNC);
  437. return FALSE;
  438. }
  439. if (g_str_equal(PQgetvalue(res, 0, 0), "t"))
  440. return FALSE;
  441. else
  442. return TRUE;
  443. }
  444. /**
  445. * Closes the dialog that close the caja
  446. */
  447. void
  448. CloseCajaWin (void)
  449. {
  450. GtkWidget *widget;
  451. widget = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_close"));
  452. gtk_widget_hide (widget);
  453. }
  454. /**
  455. * Esta funcion es llamada por "open_caja
  456. * Raise the initialization caja dialog
  457. *
  458. * @param proposed_amount the amount that must be entered in the entry
  459. */
  460. void
  461. InicializarCajaWin (gint proposed_amount)
  462. {
  463. GtkWidget *widget;
  464. widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_init_amount"));
  465. gtk_entry_set_text(GTK_ENTRY(widget), g_strdup_printf("%d", proposed_amount));
  466. gtk_widget_grab_focus(widget);
  467. widget = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_init"));
  468. gtk_widget_show_all (widget);
  469. }
  470. /**
  471. * Esta Funcion es llamda por "open_caja"
  472. *
  473. * Callback connected to accept button of the initialize caja dialog
  474. *
  475. * @param widget the widget that emited the signal
  476. * @param data the user data
  477. */
  478. void
  479. IniciarLaCaja (GtkWidget *widget, gpointer data)
  480. {
  481. GtkWidget *aux_widget;
  482. gint inicio;
  483. gint monto;
  484. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_init_amount"));
  485. monto = atoi (gtk_entry_get_text (GTK_ENTRY (aux_widget)));
  486. inicio = caja_get_last_amount();
  487. CloseCajaWin ();
  488. InicializarCaja (inicio);
  489. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_init"));
  490. gtk_widget_hide (aux_widget);
  491. if (inicio < monto)
  492. VentanaIngreso (monto - inicio);
  493. else if (inicio > monto)
  494. VentanaEgreso (inicio - monto);
  495. }
  496. /**
  497. * Es llamada por la funcion "on_btn_cash_box_close_clicked"
  498. *
  499. * Raise the close caja dialog
  500. *
  501. */
  502. void
  503. CerrarCajaWin (void)
  504. {
  505. GtkWidget *widget;
  506. gint amount_must_have;
  507. gint monto_base_caja;
  508. gint egreso_cierre;
  509. gint proxima_apertura;
  510. monto_base_caja = rizoma_get_value_int ("MONTO_BASE_CAJA");
  511. amount_must_have = ArqueoCaja();
  512. if (amount_must_have > monto_base_caja)
  513. egreso_cierre = amount_must_have - monto_base_caja;
  514. else
  515. egreso_cierre = 0;
  516. proxima_apertura = (egreso_cierre == 0) ? amount_must_have : monto_base_caja;
  517. /*Se setean los labels con la información correspondiente*/
  518. /*Monto que debería haber en caja*/
  519. widget = GTK_WIDGET (builder_get (builder, "lbl_caja_close_must_have"));
  520. gtk_label_set_markup (GTK_LABEL (widget),
  521. g_strdup_printf ("<b>$ %s</b>", PutPoints (g_strdup_printf ("%d", amount_must_have))));
  522. g_object_set_data(G_OBJECT (widget), "must-have", (gpointer)amount_must_have);
  523. /*Monto que se retirará de caja*/
  524. widget = GTK_WIDGET (builder_get (builder, "lbl_cash_out"));
  525. gtk_label_set_markup (GTK_LABEL (widget),
  526. g_strdup_printf ("<b>$ %s</b>", PutPoints (g_strdup_printf ("%d", egreso_cierre))));
  527. /*Monto con el cual se iniciará la siguiente caja*/
  528. widget = GTK_WIDGET (builder_get (builder, "lbl_initial_cash"));
  529. gtk_label_set_markup (GTK_LABEL (widget),
  530. g_strdup_printf ("<b>$ %s</b>", PutPoints (g_strdup_printf ("%d", proxima_apertura))));
  531. /*Diferencia de caja - se calcula dependiendo de lo que se ingrese en el siguiente widget*/
  532. widget = GTK_WIDGET (gtk_builder_get_object(builder, "lbl_caja_close_lost"));
  533. gtk_label_set_text(GTK_LABEL(widget), "");
  534. gtk_widget_hide (widget);
  535. /*Monto con el que se cierra caja*/
  536. widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_close_have"));
  537. gtk_entry_set_max_length (GTK_ENTRY(widget), 9);
  538. gtk_entry_set_text(GTK_ENTRY(widget), g_strdup_printf("%d", amount_must_have));
  539. gtk_editable_select_region (GTK_EDITABLE(widget), 0, -1);
  540. gtk_widget_grab_focus(widget);
  541. gtk_widget_hide (widget);
  542. widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_close_amount"));
  543. gtk_entry_set_text(GTK_ENTRY(widget), g_strdup_printf("%d", amount_must_have));
  544. gtk_widget_hide (widget);
  545. /*Se ocultas los widgets restantes*/
  546. widget = GTK_WIDGET (builder_get (builder, "label59"));
  547. gtk_widget_hide (widget);
  548. widget = GTK_WIDGET (builder_get (builder, "label60"));
  549. gtk_widget_hide (widget);
  550. widget = GTK_WIDGET (builder_get (builder, "label62"));
  551. gtk_widget_hide (widget);
  552. widget = GTK_WIDGET (gtk_builder_get_object(builder, "wnd_caja_close"));
  553. gtk_widget_show (widget);
  554. }
  555. /**
  556. * Callback associated to the accept button of close caja dialog
  557. *
  558. * @param widget the widget that emited the signal
  559. * @param data the user data
  560. */
  561. void
  562. CerrarLaCaja (GtkWidget *widget, gpointer data)
  563. {
  564. GtkWidget *aux_widget;
  565. gint must_have;
  566. //gint real_have;
  567. //gint end_amount;
  568. gint monto_base_caja=rizoma_get_value_int ("MONTO_BASE_CAJA");
  569. gint motivo;
  570. //Lo que deberia tener
  571. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "lbl_caja_close_must_have"));
  572. must_have = (gint)g_object_get_data(G_OBJECT(aux_widget), "must-have");
  573. //Lo que realmente tiene
  574. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_close_have"));
  575. //real_have = atoi(gtk_entry_get_text(GTK_ENTRY(aux_widget)));
  576. //El monto de cierre (Lo que realmente tiene pero filtrado)
  577. aux_widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_close_amount"));
  578. //end_amount = atoi(gtk_entry_get_text(GTK_ENTRY(aux_widget)));
  579. /*(En los casos donde hay más o menos dinero, se debe preguntar el motivo)*/
  580. /* if (end_amount < must_have) //Si hay menos dinero del que debería */
  581. /* { */
  582. /* con_cierre = TRUE; //FLAG */
  583. /* CloseCajaWin (); */
  584. /* VentanaEgreso(must_have - end_amount); */
  585. /* } */
  586. /* else if (end_amount > must_have) //Si hay más dinero del que debería */
  587. /* { */
  588. /* con_cierre = TRUE; //FLAG */
  589. /* CloseCajaWin (); */
  590. /* VentanaIngreso (end_amount - must_have); */
  591. /* } */
  592. /* else if (end_amount == must_have) //Si está el dinero que debería */
  593. /* { */
  594. motivo = atoi (PQvaluebycol (EjecutarSQL ("SELECT id FROM tipo_egreso WHERE descrip='Retiro por cierre'"), 0, "id"));
  595. if (must_have > monto_base_caja)
  596. //Se deja en caja el monto base
  597. Egresar (must_have-monto_base_caja, motivo, user_data->user_id);
  598. if (CerrarCaja(ReturnSaldoCaja()))
  599. {
  600. CloseCajaWin ();
  601. /*aux_widget = GTK_WIDGET (gtk_builder_get_object (builder, "quit_message"));
  602. gtk_dialog_response (GTK_DIALOG(aux_widget), GTK_RESPONSE_YES);*/
  603. print_cash_box_info (get_last_cash_box_id (), 0, 0, NULL);
  604. gtk_widget_show_all (GTK_WIDGET (builder_get (builder, "dialog_cash_box_closed")));
  605. }
  606. else
  607. ErrorMSG (aux_widget, "No se pudo cerrar la caja apropiadamente\nPor favor intente nuevamente");
  608. //}
  609. }
  610. /**
  611. * prepares the software to open the caja based in the in the user
  612. * that is running the software.
  613. *
  614. */
  615. void
  616. prepare_caja (void)
  617. {
  618. if (check_caja())
  619. open_caja (FALSE);
  620. }
  621. /**
  622. * Es llamada por la funcion "prepare_caja"
  623. *
  624. * initializes a new caja
  625. *
  626. * @param automatic_mode TRUE if does NOT must prompt a dialog
  627. * interaction with the user
  628. */
  629. void
  630. open_caja (gboolean automatic_mode)
  631. {
  632. if (automatic_mode)
  633. InicializarCaja (caja_get_last_amount ());
  634. else
  635. InicializarCajaWin (caja_get_last_amount ());
  636. }
  637. /**
  638. * Es llamada por las funciones "open_caja" y "IniciarLaCaja"
  639. *
  640. * Retrieves the last amount of money that has the current caja
  641. * (opened or closed)
  642. *
  643. * @return the amount of money
  644. */
  645. gint
  646. caja_get_last_amount (void)
  647. {
  648. PGresult *res;
  649. gchar *q;
  650. gint last_amount = 0;
  651. gint last_caja;
  652. res = EjecutarSQL("select max(id) from caja");
  653. last_caja = atoi (PQgetvalue (res, 0, 0));
  654. q = g_strdup_printf ("select termino from caja where id=%d", last_caja);
  655. res = EjecutarSQL (q);
  656. g_free (q);
  657. if (PQntuples (res) != 0)
  658. {
  659. last_amount = atoi (PQgetvalue (res, 0, 0));
  660. }
  661. return last_amount;
  662. }
  663. /**
  664. * Es llamada cuando se preiona enter en la caja de texto "entry_caja_close_have"
  665. * (signal actived)
  666. *
  667. * Uodates the entry of amount to close and the label of lost money in
  668. * the close 'caja' dialog.
  669. *
  670. * @param editable the entry that emits the signal
  671. * @param data the user data
  672. */
  673. void
  674. on_entry_caja_close_have_changed (GtkEditable *editable, gpointer data)
  675. {
  676. GtkWidget *widget;
  677. gint monto;
  678. gint must_have;
  679. if (!HaveCharacters (g_strdup (gtk_entry_get_text(GTK_ENTRY(editable)))))
  680. {
  681. monto = atoi(gtk_entry_get_text(GTK_ENTRY(editable)));
  682. widget = GTK_WIDGET (gtk_builder_get_object(builder, "lbl_caja_close_must_have"));
  683. must_have = (gint)g_object_get_data(G_OBJECT(widget), "must-have");
  684. widget = GTK_WIDGET (gtk_builder_get_object(builder, "lbl_caja_close_lost"));
  685. gtk_label_set_text(GTK_LABEL(widget), g_strdup_printf("%d", must_have - monto));
  686. if (must_have > monto)
  687. gtk_label_set_markup (GTK_LABEL(widget), g_strdup_printf("<span color=\"red\">%d</span>", monto - must_have));
  688. else
  689. gtk_label_set_markup (GTK_LABEL(widget), g_strdup_printf("%d", monto - must_have));
  690. widget = GTK_WIDGET (gtk_builder_get_object(builder, "entry_caja_close_amount"));
  691. gtk_entry_set_text (GTK_ENTRY(widget), g_strdup_printf("%d", monto));
  692. }
  693. }
  694. /**
  695. * Es llamada cuando el boton "btn_cash_box_close" es presionado
  696. * (signal clicked)
  697. *
  698. * Llama a la funcion "CerrarCajaWin"
  699. *
  700. * @param editable the entry that emits the signal
  701. * @param data the user data
  702. */
  703. void
  704. on_btn_cash_box_close_clicked (void)
  705. {
  706. CerrarCajaWin ();
  707. }