PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/factura_more.c

#
C | 292 lines | 187 code | 51 blank | 54 comment | 24 complexity | 46b67c99a1c8a17cf4d3f3baa3181a6c 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. /*facutra_more.c
  4. *
  5. * Copyright (C) 2005,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. #define _XOPEN_SOURCE 600
  24. #include<features.h>
  25. #include<gtk/gtk.h>
  26. #include<libps/pslib.h>
  27. #include<stdlib.h>
  28. #include<locale.h>
  29. #include<string.h>
  30. #include<math.h>
  31. #include"tipos.h"
  32. #include"config_file.h"
  33. #include"factura_more.h"
  34. #include"postgres-functions.h"
  35. #include"utils.h"
  36. #include"boleta.h"
  37. #define COEF_PULGADA 0.3528
  38. /**
  39. * Generate the neccesary invoice postscript files to be printed.
  40. *
  41. * @param sell_type the type of sell, at the momment must be always FACTURA
  42. * @param rut the rut of the client
  43. * @param total the total amount of the sale (not neccesary the total
  44. * amount of the invoice
  45. * @param num the num of the invoice (currently not being used)
  46. * @param products the list of products
  47. *
  48. * @return 0 if the invoices could be generated and printed
  49. */
  50. gint
  51. PrintDocument (gint sell_type, gchar *rut, gint total, gint num, Productos *products)
  52. {
  53. // GnomePrintJob *job;
  54. gchar *client, *address, *giro, *comuna, *fono;
  55. gchar *file_to_print = NULL;
  56. gchar *aux_rut;
  57. PGresult *res;
  58. gchar *q;
  59. Productos *header;
  60. GList *aux_list;
  61. gint max_lines = rizoma_get_value_int ("FACTURA_LINEAS");
  62. gint fact_num = num;
  63. gint i;
  64. gchar *print_command;
  65. aux_rut = g_strdup(rut);
  66. print_command = rizoma_get_value ("PRINT_COMMAND");
  67. q = g_strdup_printf("SELECT nombre || ' ' || apell_p || ' ' || apell_m AS name, "
  68. "direccion, giro, comuna, telefono FROM cliente where rut=%s",
  69. strtok(g_strdup(aux_rut),"-"));
  70. res = EjecutarSQL(q);
  71. g_free (q);
  72. client = PQvaluebycol(res, 0, "name");
  73. address = PQvaluebycol(res, 0, "direccion");
  74. giro = PQvaluebycol(res, 0, "giro");
  75. comuna = PQvaluebycol(res, 0, "comuna");
  76. fono = PQvaluebycol(res, 0, "telefono");
  77. header = products;
  78. do
  79. {
  80. aux_list = NULL;
  81. i = 0;
  82. do {
  83. aux_list = g_list_append(aux_list, products->product);
  84. products = products->next;
  85. i++;
  86. } while ((i < max_lines) && (products != header));
  87. if (sell_type == FACTURA)
  88. {
  89. fact_num = get_ticket_number (FACTURA);
  90. file_to_print = PrintFactura(client, rut, address, giro, comuna, fono, aux_list, fact_num);
  91. set_ticket_number (fact_num, FACTURA);
  92. system (g_strdup_printf ("%s %s", print_command, file_to_print));
  93. }
  94. else
  95. g_printerr ("%s: calling without the proper sell_type\n", G_STRFUNC);
  96. } while (products != header);
  97. return 0;
  98. }
  99. /**
  100. * Generate the information of a factura
  101. *
  102. * @param client the name of the client
  103. * @param rut the rut of the client
  104. * @param address the address of the client
  105. * @param giro the 'giro' of the client
  106. * @param comuna the comuna of the client
  107. * @param fono the contact phone of the client
  108. * @param products the list of products sold
  109. * @param num the number of the factura
  110. *
  111. * @return the path of the postscrip file generated to be printed
  112. */
  113. gchar *
  114. PrintFactura (gchar *client, gchar *rut, gchar *address, gchar *giro, gchar *comuna, gchar *fono,
  115. GList *products, gint num)
  116. {
  117. gchar *filename;
  118. PSDoc *psdoc;
  119. gint psfont;
  120. gfloat initial;
  121. gdouble precio, subtotal = 0, iva = 0, total;
  122. gint pepe;
  123. gchar *temp_directory = rizoma_get_value ("TEMP_FILES");
  124. gchar *factura_prefix = rizoma_get_value ("FACTURA_FILE_PREFIX");
  125. gchar *str_aux;
  126. gchar *font;
  127. gdouble *fact_address = NULL;
  128. gdouble *fact_giro = NULL;
  129. gdouble *fact_comuna = NULL;
  130. gdouble *fact_phone = NULL;
  131. gdouble *fact_detail = NULL;
  132. gdouble *fact_code = NULL;
  133. gdouble *fact_desc = NULL;
  134. gdouble *fact_cant = NULL;
  135. gdouble *fact_uni = NULL;
  136. gdouble *fact_total = NULL;
  137. gdouble *fact_iva = NULL;
  138. gdouble *fact_total_final = NULL;
  139. gdouble *fact_size = NULL;
  140. gdouble *fact_client = NULL;
  141. gdouble *fact_rut = NULL;
  142. gdouble *fact_subtotal = NULL;
  143. gdouble current_iva;
  144. Producto *product;
  145. gint i;
  146. current_iva = strtod (PUT(GetDataByOne("select monto from impuesto where id = 1")), (char **)NULL)/100 + 1.0;
  147. //Productos *header = products;
  148. filename = g_strdup_printf ("%s/%s%d.ps", temp_directory, factura_prefix, num);
  149. setlocale (LC_NUMERIC, "C");
  150. PS_boot ();
  151. psdoc = PS_new ();
  152. PS_open_file (psdoc, filename);
  153. /* rizoma_extract_xy (rizoma_get_value ("FACTURA_SIZE"), &x, &y); */
  154. fact_size = rizoma_get_double_list("FACTURA_SIZE", 2);
  155. g_assert(fact_size != NULL);
  156. PS_set_info (psdoc, "Creator", "POS Rizoma Comercio");
  157. PS_set_info (psdoc, "Author", "POS Rizoma Comercio");
  158. PS_set_info (psdoc, "Title", "Invoice");
  159. PS_set_info (psdoc, "Orientation", "Portraint");
  160. str_aux = g_strdup_printf ("0 0 %f %f", fact_size[0]/COEF_PULGADA, fact_size[1]/COEF_PULGADA);
  161. PS_set_info (psdoc, "BoundingBox", str_aux);
  162. g_free(str_aux);
  163. font = g_strconcat(rizoma_get_value("FONT_METRICS"), rizoma_get_value("FONT"), NULL);
  164. psfont = PS_findfont (psdoc, font, "", 0);
  165. if (psfont == 0)
  166. {
  167. g_printerr("%s: the font could not be found", G_STRFUNC);
  168. return NULL;
  169. }
  170. PS_begin_page (psdoc, fact_size[0]/COEF_PULGADA, fact_size[1]/COEF_PULGADA);
  171. PS_setfont (psdoc, psfont, 10);
  172. fact_client = rizoma_get_double_list ("FACTURA_CLIENTE", 2);
  173. g_assert (fact_client != NULL);
  174. PS_show_xy (psdoc, client, fact_client[0]/COEF_PULGADA, fact_client[1]/COEF_PULGADA);
  175. fact_rut = rizoma_get_double_list ("FACTURA_RUT", 2);
  176. g_assert (fact_rut != NULL);
  177. PS_show_xy (psdoc, rut, fact_rut[0]/COEF_PULGADA, fact_rut[1]/COEF_PULGADA);
  178. fact_address = rizoma_get_double_list ("FACTURA_ADDRESS", 2);
  179. g_assert (fact_address != NULL);
  180. PS_show_xy (psdoc, address, fact_address[0]/COEF_PULGADA, fact_address[1]/COEF_PULGADA);
  181. fact_giro = rizoma_get_double_list ("FACTURA_GIRO", 2);
  182. g_assert (fact_giro != NULL);
  183. PS_show_xy (psdoc, giro, fact_giro[0]/COEF_PULGADA, fact_giro[1]/COEF_PULGADA);
  184. fact_comuna = rizoma_get_double_list("FACTURA_COMUNA", 2);
  185. g_assert (fact_comuna != NULL);
  186. PS_show_xy (psdoc, comuna, fact_comuna[0]/COEF_PULGADA, fact_comuna[1]/COEF_PULGADA);
  187. fact_phone = rizoma_get_double_list("FACTURA_PHONE", 2);
  188. if (fact_phone != NULL)
  189. PS_show_xy (psdoc, fono, fact_phone[0]/COEF_PULGADA, fact_phone[1]/COEF_PULGADA);
  190. /* We draw the products detail */
  191. fact_detail = rizoma_get_double_list ("FACTURA_DETAIL", 2);
  192. g_assert (fact_detail != NULL);
  193. initial = fact_detail[1]/COEF_PULGADA;
  194. fact_code = rizoma_get_double_list ("FACTURA_CODIGO", 2);
  195. fact_desc = rizoma_get_double_list ("FACTURA_DESCRIPCION", 2);
  196. fact_cant = rizoma_get_double_list ("FACTURA_CANT", 2);
  197. fact_uni = rizoma_get_double_list ("FACTURA_UNI", 2);
  198. fact_total = rizoma_get_double_list ("FACTURA_TOTAL", 2);
  199. for (i = 0 ; i < g_list_length(products) ; i++)
  200. {
  201. product = g_list_nth_data (products, i);
  202. PS_show_xy (psdoc, product->codigo, fact_code[0]/COEF_PULGADA, initial);
  203. PS_show_xy (psdoc, product->producto, fact_desc[0]/COEF_PULGADA, initial);
  204. str_aux = g_strdup_printf ("%.3f", product->cantidad);
  205. PS_show_xy (psdoc, str_aux, fact_cant[0]/COEF_PULGADA, initial);
  206. g_free (str_aux);
  207. //check if must be used the mayorist price or not
  208. if ((product->cantidad < product->cantidad_mayorista) || (product->mayorista == FALSE))
  209. precio = product->precio; ///(((gdouble)products->product->iva/100)+1);
  210. else
  211. precio = product->precio_mayor; ///(((gdouble)products->product->iva/100)+1);
  212. str_aux = g_strdup_printf ("%.0f", precio);
  213. PS_show_xy (psdoc, str_aux, fact_uni[0]/COEF_PULGADA, initial);
  214. g_free (str_aux);
  215. str_aux = g_strdup_printf ("%.0f", product->cantidad * precio);
  216. PS_show_xy (psdoc, str_aux, fact_total[0]/COEF_PULGADA, initial);
  217. g_free (str_aux);
  218. pepe = lround ((gdouble)precio/current_iva);
  219. subtotal += pepe * product->cantidad;
  220. iva += (precio - pepe) * product->cantidad;
  221. initial -= 10;
  222. }
  223. /* Finished the products detail*/
  224. fact_subtotal = rizoma_get_double_list ("FACTURA_SUBTOTAL", 2);
  225. g_assert (fact_subtotal != NULL);
  226. str_aux = g_strdup_printf ("%.0f", subtotal);
  227. PS_show_xy (psdoc, str_aux, fact_subtotal[0]/COEF_PULGADA, fact_subtotal[1]/COEF_PULGADA);
  228. total = subtotal + iva;
  229. fact_iva = rizoma_get_double_list ("FACTURA_IVA", 2);
  230. PS_show_xy (psdoc, g_strdup_printf ("%.0f", iva), fact_iva[0]/COEF_PULGADA, fact_iva[1]/COEF_PULGADA);
  231. fact_total_final = rizoma_get_double_list ("FACTURA_TOTAL_FINAL", 2);
  232. g_assert (fact_total_final != NULL);
  233. str_aux = g_strdup_printf ("%.0f", total);
  234. PS_show_xy (psdoc, str_aux, fact_total_final[0]/COEF_PULGADA, fact_total_final[1]/COEF_PULGADA);
  235. g_free (str_aux);
  236. PS_end_page (psdoc);
  237. PS_delete (psdoc);
  238. PS_shutdown ();
  239. g_printerr("factura: %s\n", filename);
  240. return filename;
  241. }