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