+t_Factura *fact_buscar(t_LstFacturas *lst, int numero, EMUFS_REG_ID *id, EMUFS_REG_ID *id_texto)
+{
+ t_Factura *fact;
+ t_Reg_Factura *reg;
+ char *leo;
+ EMUFS_REG_SIZE size;
+ int error;
+ if (lst == NULL) return NULL;
+
+ fact = NULL;
+ reg = lst->primero;
+ while (reg) {
+ if (reg->numero == numero) {
+ size = 0;
+ error = 0;
+ leo = lst->fp->leer_registro(lst->fp, reg->num_reg, &size, &error);
+ if (leo != NULL) {
+ fact = (t_Factura *)malloc(sizeof(t_Factura));
+ if (fact == NULL) {
+ free(leo);
+ return NULL;
+ }
+ procesar_leer_factura(fact, leo, size, lst);
+ (*id) = reg->num_reg;
+ (*id_texto) = reg->texto_reg;
+ free(leo);
+ }
+ break;
+ }
+ reg = reg->sig;
+ }
+ return fact;
+}
+
+t_Factura *fact_form_buscar(WINDOW *win, EMUFS_REG_ID *id, EMUFS_REG_ID *id_texto)
+{
+ t_Form *form;
+ t_Factura *fact;
+
+ form = form_crear(win);
+ form_agregar_widget(form, INPUT, "Numero de Factura", 8, "");
+ form_ejecutar(form, 1,1);
+ fact = fact_buscar(lst_facturas, form_obtener_valor_int(form, "Numero de Factura"), id, id_texto);
+ form_destruir(form);
+
+ return fact;
+}
+
+void fact_eliminar(char *s)
+{
+ WINDOW *win;
+ t_Factura *fact;
+ t_Reg_Factura *nodo;
+ EMUFS_REG_ID id;
+
+ win = newwin(LINES-4, COLS-2, 2, 1);
+ box(win, 0, 0);
+
+ fact = fact_form_buscar(win, &id, &id);
+
+ if (fact == NULL) {
+ wattron(win, COLOR_PAIR(COLOR_YELLOW));
+ mvwaddstr(win, 2, 1, "No existe artículo con ese código. Abortando!");
+ wattroff(win, COLOR_PAIR(COLOR_YELLOW));
+ wrefresh(win);
+ getch();
+ werase(win);
+ wrefresh(win);
+ delwin(win);
+ return;
+ }
+
+ nodo = lst_facturas->primero;
+ while (nodo) {
+ if (nodo->numero == fact->numero) {
+ lst_facturas->fp->borrar_registro(lst_facturas->fp, nodo->num_reg);
+ lst_facturas->fp_texto->borrar_registro(lst_facturas->fp_texto, nodo->texto_reg);
+ eliminar_nodo_factura(lst_facturas, nodo);
+ break;
+ }
+ nodo = nodo->sig;
+ }
+
+ free(fact->items);
+ free(fact);
+}
+
+void fact_modificar(char *s)
+{
+ WINDOW *win, *items;
+ t_Form *form;
+ t_Factura *fact;
+ /*EMUFS_REG_SIZE size;*/
+ EMUFS_REG_ID id, id_texto;
+/* int y_actual, cant, error;*/
+ char tmp_str[10];
+
+
+ win = newwin(LINES-4, COLS-2, 2, 1);
+ box(win, 0, 0);
+
+ fact = fact_form_buscar(win, &id, &id_texto);
+
+ if (fact == NULL) {
+ wattron(win, COLOR_PAIR(COLOR_YELLOW));
+ mvwaddstr(win, 2, 1, "No existe artículo con ese código. Abortando!");
+ wattroff(win, COLOR_PAIR(COLOR_YELLOW));
+ wrefresh(win);
+ getch();
+ werase(win);
+ wrefresh(win);
+ delwin(win);
+ return;
+ }
+
+ mvwaddch(win, 10, 0, ACS_LTEE);
+ mvwhline(win, 10, 1, ACS_HLINE, COLS-3);
+ mvwaddch(win, 10, COLS-3, ACS_RTEE);
+ wrefresh(win);
+
+ items = derwin(win, LINES-20, COLS-4, 15, 1);
+ wrefresh(items);
+
+ form = form_crear(win);
+ sprintf(tmp_str, "%08d", fact->numero);
+ form_agregar_widget(form, INPUT, "Numero de Factura", 8, tmp_str);
+ form_agregar_widget(form, INPUT, "Fecha Emision", 8, fact->emision);
+ form_agregar_widget(form, INPUT, "Fecha Vto", 8, fact->vencimiento);
+ sprintf(tmp_str, "%08d", fact->numero_remito);
+ form_agregar_widget(form, INPUT, "Nro Remito", 8, tmp_str);
+ form_agregar_widget(form, RADIO, "Estado", 6, "PN,CD,CM,SF,PM,NC");
+ form_agregar_widget(form, RADIO, "Forma de pago", 3, "CO,CR,CH");
+ sprintf(tmp_str, "%02.2f", fact->procdoi);
+ form_agregar_widget(form, INPUT, "%% Descuento", 5, tmp_str);
+ form_agregar_widget(form, INPUT, "Cuenta Cte", 5, fact->ctacte);
+ form_agregar_widget(form, INPUT, "Cheque Nro", 18, fact->cheque);
+
+ form_ejecutar(form, 1,1);
+
+ fact->numero = form_obtener_valor_int(form, "Numero de Factura");
+ strcpy(fact->emision, form_obtener_valor_char(form, "Fecha Emision"));
+ strcpy(fact->vencimiento, form_obtener_valor_char(form, "Fecha Vto"));
+ fact->numero_remito = form_obtener_valor_int(form, "Nro Remito");
+ strcpy(fact->estado, form_obtener_valor_char(form, "Estado"));
+ strcpy(fact->fp, form_obtener_valor_char(form, "Forma de pago"));
+ fact->procdoi = form_obtener_valor_float(form, "%% Descuento");
+ strcpy(fact->ctacte, form_obtener_valor_char(form, "Cuenta Cte"));
+ strcpy(fact->cheque, form_obtener_valor_char(form, "Cheque Nro"));
+
+ form_destruir(form);
+
+ /* TODO MODIFICAR */
+
+/* entrada = procesar_guardar_factura(&fact,lst_facturas, &size);
+ if (entrada) {
+ id = lst_facturas->fp->grabar_registro(lst_facturas->fp, entrada, size, &error);*/
+ /*id_texto = tmp->fp_texto->grabar_registro(tmp->fp_texto, fact.nota, 400, &error);*/
+ /* TODO : -1 == id_texto !!!!!!!! XXX XXX XXX XXX XXX XXX XXX */
+ /*agregar_nodo_factura(lst_facturas, crear_nodo_factura(id, -1, fact.numero));
+ free(entrada);
+ }
+ */
+/* form_destruir(form); */
+
+ free(fact->items);
+ free(fact);
+
+ werase(win);
+ wrefresh(win);
+ delwin(items);
+ delwin(win);
+}
+
+void fact_agregar(char *s)
+{
+ WINDOW *win, *items;
+ t_Form *form;
+ t_Item *its = NULL;
+ t_Factura fact;
+ EMUFS_REG_SIZE size;
+ EMUFS_REG_ID id; /*, id_texto;*/
+ int y_actual, cant, error;
+ char *entrada;
+
+ win = newwin(LINES-4, COLS-2, 2, 1);
+ box(win, 0, 0);
+ mvwaddch(win, 10, 0, ACS_LTEE);
+ mvwhline(win, 10, 1, ACS_HLINE, COLS-3);
+ mvwaddch(win, 10, COLS-3, ACS_RTEE);
+ wrefresh(win);
+
+ items = derwin(win, LINES-20, COLS-4, 15, 1);
+ wrefresh(items);
+
+ form = form_crear(win);
+ form_agregar_widget(form, INPUT, "Numero de Factura", 8, "");
+ form_agregar_widget(form, INPUT, "Fecha Emision", 8, "");
+ form_agregar_widget(form, INPUT, "Fecha Vto", 8, "");
+ form_agregar_widget(form, INPUT, "Nro Remito", 8, "");
+ form_agregar_widget(form, RADIO, "Estado", 6, "PN,CD,CM,SF,PM,NC");
+ form_agregar_widget(form, RADIO, "Forma de pago", 3, "CO,CR,CH");
+ form_agregar_widget(form, INPUT, "%% Descuento", 5, "");
+ form_agregar_widget(form, INPUT, "Cuenta Cte", 5, "");
+ form_agregar_widget(form, INPUT, "Cheque Nro", 18, "");
+
+ form_ejecutar(form, 1,1);
+
+ fact.numero = form_obtener_valor_int(form, "Numero de Factura");
+ fprintf(stderr, "Agregando numero %d\n", fact.numero);
+ strcpy(fact.emision, form_obtener_valor_char(form, "Fecha Emision"));
+ strcpy(fact.vencimiento, form_obtener_valor_char(form, "Fecha Vto"));
+ fact.numero_remito = form_obtener_valor_int(form, "Nro Remito");
+ strcpy(fact.estado, form_obtener_valor_char(form, "Estado"));
+ strcpy(fact.fp, form_obtener_valor_char(form, "Forma de pago"));
+ fact.procdoi = form_obtener_valor_float(form, "%% Descuento");
+ strcpy(fact.ctacte, form_obtener_valor_char(form, "Cuenta Cte"));
+ strcpy(fact.cheque, form_obtener_valor_char(form, "Cheque Nro"));
+
+ form_destruir(form);
+
+ form = form_crear(win);
+ form_agregar_widget(form, INPUT, "Nro de Articulo (* == fin)", 8, "");
+ form_agregar_widget(form, INPUT, "CV", 8, "");
+ form_agregar_widget(form, INPUT, "PVU", 8, "");
+ y_actual = 0;
+ scrollok(items, 1);
+ mvwaddstr(win, 15, 2, "Numero");
+ mvwaddstr(win, 15, 11, "CV");
+ mvwaddstr(win, 15, 21, "PVU");
+ do {
+ form_set_valor(form, "Nro de Articulo (* == fin)", "");
+ form_set_valor(form, "CV", "");
+ form_set_valor(form, "PVU", "");
+ form_ejecutar(form, 2, 11);
+
+ entrada = form_obtener_valor_char(form, "Nro de Articulo (* == fin)");
+
+ if (entrada[0] != '\0') {
+ y_actual++;
+ if (y_actual > LINES-22) {
+ y_actual = LINES-22;
+ wscrl(items, 1);
+ }
+ mvwaddstr(items, y_actual, 1, entrada);
+ mvwaddstr(items, y_actual, 10, form_obtener_valor_char(form, "CV"));
+ mvwaddstr(items, y_actual, 20, form_obtener_valor_char(form, "PVU"));
+ wrefresh(items);
+ /* Agrego el Item */
+ cant++;
+ its = (t_Item *)realloc(its, cant*sizeof(t_Item));
+ its[cant-1].numero = form_obtener_valor_int(form, entrada);
+ strcpy(its[cant-1].cv, form_obtener_valor_char(form, "CV"));
+ strcpy(its[cant-1].pvu, form_obtener_valor_char(form, "PVU"));
+ }
+ } while (strcmp(entrada, "*") != 0);
+
+ if (lst_facturas->fp->tipo == 3) {
+ if (cant != 10) {
+ /* TODO Limitar en la GUI en lugar de truncar! */
+ its = (t_Item *)realloc(its, 10*sizeof(t_Item));
+ cant = 10;
+ }
+ }
+ fact.items = its;
+ fact.cant_items = cant;
+
+ entrada = procesar_guardar_factura(&fact,lst_facturas, &size);
+ if (entrada) {
+ error = 0;
+ id = lst_facturas->fp->grabar_registro(lst_facturas->fp, entrada, size, &error);
+ /*id_texto = tmp->fp_texto->grabar_registro(tmp->fp_texto, fact.nota, 400, &error);*/
+ /* TODO : -1 == id_texto !!!!!!!! XXX XXX XXX XXX XXX XXX XXX */
+ agregar_nodo_factura(lst_facturas, crear_nodo_factura(id, -1, fact.numero));
+ free(entrada);
+ }
+
+ if (its) free(its);
+ form_destruir(form);
+
+ werase(win);
+ wrefresh(win);
+ delwin(items);
+ delwin(win);
+}