diff --git a/data/clientes.data b/data/clientes.data index 2c497e8..30419ba 100644 --- a/data/clientes.data +++ b/data/clientes.data @@ -1,4 +1,4 @@ -40 +41 0 93 991885880 @@ -359,3 +359,12 @@ Rua Acnhieta 123 123 Acre +Vinicius Costa Castro +98 +985166409 +Rua +Anchieta +1677 +Centro +Altamira +Pará diff --git a/data/vendas.data b/data/vendas.data new file mode 100644 index 0000000..5540b45 --- /dev/null +++ b/data/vendas.data @@ -0,0 +1,25 @@ +2 +1542343839 +Vinicius Costa Castro +Rua Anchieta - 1677, Centro, Altamira - Pará +(98) 985166409 +1 +Monitor Dell 0 +699,990000 +50 +0,000000 +0 +34999,500000 +34999,500000 +1542344175 +Vinicius Costa Castro +Rua Anchieta - 1677, Centro, Altamira - Pará +(98) 985166409 +1 +Monitor Dell 0 +699,990000 +5 +0,000000 +0 +3499,950000 +3499,950000 diff --git a/headers/carrinho.h b/headers/carrinho.h index a1e88f4..0f527df 100644 --- a/headers/carrinho.h +++ b/headers/carrinho.h @@ -18,17 +18,19 @@ typedef struct { unsigned int tamanho; struct produto_vendido *produtos; double desconto; + int descontopercento; double preco; double preco_final; } Carrinho; Carrinho *criar_carrinho(); void apagar_carrinho(Carrinho *); -void redefinir_carrinho(Carrinho *); -void adicionar_produto_carrinho(Carrinho *, struct produto, int quantidade); +void adicionar_produto_vendido_carrinho(Carrinho *, struct produto_vendido); + +void adicionar_produto_carrinho(Carrinho *, struct produto, int); void remover_produto_carrinho(Carrinho *, int); -void definir_desconto(Carrinho *, double); +void definir_desconto(Carrinho *, int); #endif \ No newline at end of file diff --git a/headers/vendas.h b/headers/vendas.h index 67a6150..9cafc83 100644 --- a/headers/vendas.h +++ b/headers/vendas.h @@ -8,10 +8,16 @@ #include #include +struct comprador { + char nome[100]; + char endereco[210]; + char telefone[15]; +}; + struct venda { unsigned int index; time_t horario; - struct cliente cliente; + struct comprador cliente; Carrinho carrinho; }; @@ -24,6 +30,11 @@ Vendas *reutilizar_vendas(); Vendas *criar_vendas(); void apagar_vendas(Vendas *); +void adicionar_venda(Vendas *, struct comprador, Carrinho *, time_t); + +void popular_vendas(Vendas *lista); +void salvar_vendas(Vendas *); + void nova_venda(Vendas *, struct cliente, Carrinho *); void remover_venda(Vendas *, int); diff --git a/interfaces/carrinho.xml b/interfaces/carrinho.xml new file mode 100644 index 0000000..8bfff75 --- /dev/null +++ b/interfaces/carrinho.xml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + 1 + 2147483647 + 1 + 1 + + + + False + 5 + False + center + 450 + + + + False + vertical + 2 + + + + True + False + Carrinho atual + + + + + + 100 + True + False + True + True + vertical + + + + True + False + True + True + list_store_carrinho + + + + + + + + Nome + + + + 0 + + + + + + + + Quantidade + + + + 1 + + + + + + + + Preço Unitário + + + + 2 + + + + + + + + + + + + True + False + Adicionar ao carrinho + + + + + + True + horizontal + + + + True + True + Digite o nome do produto + + + True + True + + + + + + True + quantidade_adjustment + + + + + + + + True + True + Adicionar ao carrinho + + + + + + \ No newline at end of file diff --git a/interfaces/main.xml b/interfaces/main.xml index c405c5a..e46bec4 100644 --- a/interfaces/main.xml +++ b/interfaces/main.xml @@ -28,6 +28,8 @@ + + @@ -307,7 +309,7 @@ - Horário da Venda + Produtos @@ -316,6 +318,28 @@ + + + Preço + + + + 2 + + + + + + + Horário + + + + 3 + + + + True diff --git a/interfaces/novavenda.xml b/interfaces/novavenda.xml index 0b7d600..f1b496e 100644 --- a/interfaces/novavenda.xml +++ b/interfaces/novavenda.xml @@ -1,62 +1,221 @@ + - - - - 11 - 99 - 11 - 1 - - - - 0 - 100000 - 1 - 1 - - - - False - 5 - center - False - 300 - - - - 2 - vertical + + + 100000 + 1 + 1 + + + 11 + 99 + 11 + 1 + + + 0 + 100 + 0 + 1 + + + + + + + + + + False + 5 + False + center + 450 + + + + False + vertical + 2 + + + + True + False + Cliente + + - - - True - Cliente - + + + True + True + Digite o nome do cliente + + + False + True + 2 + + + + + + True + False + Carrinho + + + False + True + 3 + + + + + + 100 + True + False + True + True + vertical + + + True + False + True + True + list_store_carrinho + + + - - True - True - Bairro - + + Nome + + + + 0 + + + - - True - - + + Quantidade + + + + 1 + + + - - True - Cadastrar - + + Preço Unitário + + + + 2 + + + - + + + False + True + 0 + + + + + True + True + 4 + + + + + + True + False + Desconto (%) + + + False + True + 5 + + + + + + True + adjustment_desconto + + + False + True + 6 + + + + + + True + False + + + False + True + 7 + + + + + True + False + Preço Final: + + + False + True + 8 + + + + + True + False + + + False + True + 9 + + + + + Cadastrar + True + False + False + + + False + True + 10 + - - \ No newline at end of file + + + + diff --git a/main.c b/main.c index 7058dcf..b1f1771 100644 --- a/main.c +++ b/main.c @@ -34,9 +34,10 @@ int main(int argc, char **argv){ janela = (GtkWidget *) gtk_builder_get_object(interface, "window"); - void **argumentos = (void **) malloc(2 * sizeof(void *)); + void **argumentos = (void **) malloc(3 * sizeof(void *)); argumentos[0] = clientes; argumentos[1] = produtos; + argumentos[2] = vendas; g_signal_connect(janela, "destroy", G_CALLBACK(salvar_ao_sair), argumentos); g_signal_connect(janela, "destroy", G_CALLBACK(gtk_main_quit), NULL); @@ -51,10 +52,14 @@ int main(int argc, char **argv){ void salvar_ao_sair(GtkWidget *janela, void **argumentos){ Clientes *clientes = (Clientes *) argumentos[0]; Produtos *produtos = (Produtos *) argumentos[1]; + Vendas *vendas = (Vendas *) argumentos[2]; salvar_clientes(clientes); apagar_clientes(clientes); salvar_produtos(produtos); apagar_produtos(produtos); + + salvar_vendas(vendas); + apagar_vendas(vendas); } \ No newline at end of file diff --git a/sources/carrinho.c b/sources/carrinho.c index e3a5245..f59018d 100644 --- a/sources/carrinho.c +++ b/sources/carrinho.c @@ -5,6 +5,7 @@ Carrinho *criar_carrinho(){ lista->tamanho = 0; lista->produtos = NULL; lista->desconto = 0.00; + lista->descontopercento = 0; lista->preco_final = 0.00; } @@ -18,9 +19,20 @@ void apagar_carrinho(Carrinho *lista){ free(lista); } -void redefinir_carrinho(Carrinho *lista){ - apagar_carrinho(lista); - lista = criar_carrinho(); +void adicionar_produto_vendido_carrinho(Carrinho *lista, struct produto_vendido produto){ + if (!lista){ + fprintf(stderr, "O carrinho precisa ser inicializado primeiro.\n"); + return; + } + + lista->produtos = (struct produto_vendido *) realloc(lista->produtos, (lista->tamanho + 1) * sizeof(struct produto_vendido)); + lista->produtos[lista->tamanho].index = lista->tamanho; + strcpy(lista->produtos[lista->tamanho].nome, produto.nome); + lista->produtos[lista->tamanho].preco = produto.preco; + lista->produtos[lista->tamanho].quantidade = produto.quantidade; + lista->preco += produto.preco * produto.quantidade; + lista->preco_final = lista->preco - ((lista->preco * lista->descontopercento) / 100); + lista->tamanho++; } void adicionar_produto_carrinho(Carrinho *lista, struct produto produto, int quantidade){ @@ -33,8 +45,9 @@ void adicionar_produto_carrinho(Carrinho *lista, struct produto produto, int qua lista->produtos[lista->tamanho].index = lista->tamanho; strcpy(lista->produtos[lista->tamanho].nome, produto.nome); lista->produtos[lista->tamanho].preco = produto.preco; + lista->produtos[lista->tamanho].quantidade = quantidade; lista->preco += produto.preco * quantidade; - lista->preco_final = lista->preco - ((lista->preco * lista->desconto) / 100); + lista->preco_final = lista->preco - ((lista->preco * lista->descontopercento) / 100); lista->tamanho++; } @@ -45,7 +58,7 @@ void remover_produto_carrinho(Carrinho *lista, int index){ } lista->preco -= lista->produtos[index].preco; - lista->preco_final = lista->preco - ((lista->preco * lista->desconto) / 100); + lista->preco_final = lista->preco - ((lista->preco * lista->descontopercento) / 100); for (; index < lista->tamanho - 1; index++) lista->produtos[index] = lista->produtos[index + 1]; @@ -53,12 +66,14 @@ void remover_produto_carrinho(Carrinho *lista, int index){ lista->tamanho--; } -void definir_desconto(Carrinho *lista, double desconto){ +void definir_desconto(Carrinho *lista, int descontopercento){ if (!lista){ fprintf(stderr, "O carrinho precisa ser inicializado primeiro.\n"); return; } + double desconto = (double) (lista->preco * descontopercento) / 100; + lista->descontopercento = descontopercento; lista->desconto = desconto; - lista->preco_final = lista->preco - ((lista->preco * lista->desconto) / 100); + lista->preco_final = lista->preco - desconto; } \ No newline at end of file diff --git a/sources/produtos.c b/sources/produtos.c index f2ca97f..c20e184 100644 --- a/sources/produtos.c +++ b/sources/produtos.c @@ -55,6 +55,7 @@ void salvar_produtos(Produtos *lista){ return; fprintf(dados, "%d\n", lista->tamanho); + int i; for (i = 0; i < lista->tamanho; i++){ fprintf(dados, "%s\n", lista->produtos[i].nome); diff --git a/sources/vendas.c b/sources/vendas.c index 5077b34..0190a25 100644 --- a/sources/vendas.c +++ b/sources/vendas.c @@ -5,6 +5,7 @@ Vendas *reutilizar_vendas(){ if (lista) return lista; lista = criar_vendas(); + popular_vendas(lista); return lista; } @@ -25,18 +26,113 @@ void apagar_vendas(Vendas *lista){ free(lista); } -void nova_venda(Vendas *lista, struct cliente cliente, Carrinho *carrinho){ +void popular_vendas(Vendas *lista){ + FILE *arquivo; + Carrinho *carrinho; + struct comprador cliente; + struct produto_vendido produto; + long int horario; + int i, j, quant; + double desconto; + + carrinho = criar_carrinho(); + arquivo = fopen("data/vendas.data", "r"); + + if (!arquivo){ + fprintf(stderr, "Ocorreu um erro desconhecido ao abrir o arquivo.\n"); + exit(1); + } + + fscanf(arquivo, "%d ", &quant); + + for (i = 0; i < quant; i++){ + fscanf(arquivo, "%ld\n", &horario); + fscanf(arquivo, "%[^\n]%*c\n", &cliente.nome); + fscanf(arquivo, "%[^\n]%*c\n", &cliente.endereco); + fscanf(arquivo, "%[^\n]%*c\n", &cliente.telefone); + + fscanf(arquivo, "%d\n", &j); + for (; j > 0; j--){ + fscanf(arquivo, "%[^\n]%*c\n", &produto.nome); + fscanf(arquivo, "%lf\n", &produto.preco); + fscanf(arquivo, "%d\n", &produto.quantidade); + adicionar_produto_vendido_carrinho(carrinho, produto); + } + + fscanf(arquivo, "%lf\n", &carrinho->desconto); + fscanf(arquivo, "%d\n", &carrinho->descontopercento); + fscanf(arquivo, "%lf\n", &carrinho->preco); + fscanf(arquivo, "%lf\n", &carrinho->preco_final); + + adicionar_venda(lista, cliente, carrinho, (time_t) horario); + carrinho = criar_carrinho(); + } + + fclose(arquivo); +} + +void salvar_vendas(Vendas *lista){ + int i, j; + char horario[21]; + struct tm *shorario; + FILE *arquivo; + arquivo = fopen("data/vendas.data", "w"); + + if (!arquivo){ + fprintf(stderr, "Ocorreu um erro desconhecido ao abrir o arquivo.\n"); + exit(1); + } + + fprintf(arquivo, "%d\n", lista->tamanho); + for (i = 0; i < lista->tamanho; i++){ + fprintf(arquivo, "%ld\n", lista->vendas[i].horario); + fprintf(arquivo, "%s\n", lista->vendas[i].cliente.nome); + fprintf(arquivo, "%s\n", lista->vendas[i].cliente.endereco); + fprintf(arquivo, "%s\n", lista->vendas[i].cliente.telefone); + fprintf(arquivo, "%d\n", lista->vendas[i].carrinho.tamanho); + + for (j = 0; j < lista->vendas[i].carrinho.tamanho; j++){ + fprintf(arquivo, "%s\n", lista->vendas[i].carrinho.produtos[j].nome); + fprintf(arquivo, "%lf\n", lista->vendas[i].carrinho.produtos[j].preco); + fprintf(arquivo, "%d\n", lista->vendas[i].carrinho.produtos[j].quantidade); + } + + fprintf(arquivo, "%lf\n", lista->vendas[i].carrinho.desconto); + fprintf(arquivo, "%d\n", lista->vendas[i].carrinho.descontopercento); + fprintf(arquivo, "%lf\n", lista->vendas[i].carrinho.preco); + fprintf(arquivo, "%lf\n", lista->vendas[i].carrinho.preco_final); + } + + fclose(arquivo); +} + +void adicionar_venda(Vendas *lista, struct comprador cliente, Carrinho *carrinho, time_t horario){ if (!lista){ fprintf(stderr, "As vendas precisam ser inicializadas primeiro.\n"); return; } - lista->vendas = (struct venda *) realloc(lista->vendas, (lista->tamanho - 1) * sizeof(struct venda)); + lista->vendas = (struct venda *) realloc(lista->vendas, (lista->tamanho + 1) * sizeof(struct venda)); lista->vendas[lista->tamanho].index = lista->tamanho; lista->vendas[lista->tamanho].cliente = cliente; + lista->vendas[lista->tamanho].horario = (time_t) horario; + lista->vendas[lista->tamanho].carrinho = *carrinho; + lista->tamanho++; +} + +void nova_venda(Vendas *lista, struct cliente cliente, Carrinho *carrinho){ + if (!lista){ + fprintf(stderr, "As vendas precisam ser inicializadas primeiro.\n"); + return; + } + + lista->vendas = (struct venda *) realloc(lista->vendas, (lista->tamanho + 1) * sizeof(struct venda)); + lista->vendas[lista->tamanho].index = lista->tamanho; + sprintf(lista->vendas[lista->tamanho].cliente.nome, "%s", cliente.nome); + sprintf(lista->vendas[lista->tamanho].cliente.endereco, "%s %s - %u, %s, %s - %s", cliente.endereco.logradouro, cliente.endereco.endereco, cliente.endereco.casa, cliente.endereco.bairro, cliente.endereco.cidade, cliente.endereco.estado); + sprintf(lista->vendas[lista->tamanho].cliente.telefone, "(%u) %s", cliente.telefone.ddd, cliente.telefone.telefone); time(&(lista->vendas[lista->tamanho].horario)); lista->vendas[lista->tamanho].carrinho = *carrinho; - redefinir_carrinho(carrinho); lista->tamanho++; } diff --git a/vendas.c b/vendas.c index 3d36b80..ef01401 100644 --- a/vendas.c +++ b/vendas.c @@ -5,34 +5,102 @@ // ;===================================== #include + +#include #include #include "headers/vendas.h" #include "headers/clientes.h" +#include "headers/produtos.h" static Vendas *VENDAS_LISTA = NULL; +static Carrinho *CARRINHO = NULL; void vendas_gui(GtkBuilder *, Vendas *); void vendas_aba(GtkBuilder *, Vendas *); +void recarregar_aba_vendas(GtkBuilder *, GtkBuilder *); void vendas_botao(GtkBuilder *); -void vendas_cliente_completion(GtkBuilder *); +void vendas_cliente_completion(GtkWidget *); +void vendas_produto_completion(GtkWidget *); +void mensagem_simples_vendas(GtkWindow *, gchar *, gchar *); +void atualizar_valor_final(GtkWidget *, GtkBuilder *); void nova_venda_dialog(GtkWidget *, GtkBuilder *); +void nova_venda_callback(GtkWidget *, GtkBuilder *); + +void carrinho_dialog(GtkWidget *, GtkBuilder *); +void recarregar_lista_carrinho(GtkTreeView *); +void carrinho_callback(GtkWidget *, GtkBuilder *); void vendas_gui(GtkBuilder *interface, Vendas *vendas){ VENDAS_LISTA = vendas; + CARRINHO = criar_carrinho(); vendas_aba(interface, vendas); } +char *asctime(const struct tm *timeptr){ + static const char wday_name[][4] = { "Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sab" }; + static const char mon_name[][4] = { "Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez" }; + static char result[27]; + sprintf(result, "%.3s,%3d %.3s %d %.2d:%.2d:%.2d", + wday_name[timeptr->tm_wday], + timeptr->tm_mday, + mon_name[timeptr->tm_mon], + 1900 + timeptr->tm_year, + timeptr->tm_hour, + timeptr->tm_min, + timeptr->tm_sec + ); + return result; +} + void vendas_aba(GtkBuilder *interface, Vendas * vendas){ + GtkTreeView *lista; + GtkListStore *lista_estrutura; + GtkTreeIter lista_estrutura_primaria; + + lista = (GtkTreeView *) gtk_builder_get_object(interface, "tree_view_vendas"); + lista_estrutura = (GtkListStore *) gtk_tree_view_get_model(lista); + + gtk_list_store_clear(lista_estrutura); + + char produtos[203]; + char preco[17]; + char *horario; + struct tm *horario_info; + int i; + + for (i = 0; i < vendas->tamanho; i++){ + horario_info = localtime(&vendas->vendas[i].horario); + horario = asctime(horario_info); + + if (vendas->vendas[i].carrinho.tamanho > 2) + sprintf(produtos, "%s, %s...", vendas->vendas[i].carrinho.produtos[0].nome, vendas->vendas[i].carrinho.produtos[1].nome); + else if (vendas->vendas[i].carrinho.tamanho > 1) + sprintf(produtos, "%s, %s", vendas->vendas[i].carrinho.produtos[0].nome, vendas->vendas[i].carrinho.produtos[1].nome); + else + sprintf(produtos, "%s", vendas->vendas[i].carrinho.produtos[0].nome); + sprintf(preco, "R$ %.2lf", vendas->vendas[i].carrinho.preco_final); + + gtk_list_store_append(lista_estrutura, &lista_estrutura_primaria); + gtk_list_store_set(lista_estrutura, &lista_estrutura_primaria, 0, vendas->vendas[i].cliente.nome, 1, produtos, 2, preco, 3, horario, -1); + } + vendas_botao(interface); } +void recarregar_aba_vendas(GtkBuilder *dialog, GtkBuilder *interface){ + dialogOpened = false; + if (interface) + vendas_aba(interface, VENDAS_LISTA); +} + void nova_venda_dialog(GtkWidget *botao, GtkBuilder *interface){ GtkBuilder *dialog_interface; const gchar dialog_interface_nome[] = "interfaces/novavenda.xml"; - GtkWidget *window, *dialog; + GtkWidget *window, *dialog, *inputCliente, *inputSubmit, *inputPrecoFinal, *inputDesconto; + GtkTreeView *lista; GError *erro = NULL; if (dialogOpened) @@ -48,34 +116,193 @@ void nova_venda_dialog(GtkWidget *botao, GtkBuilder *interface){ window = (GtkWidget *) gtk_builder_get_object(interface, "window"); dialog = (GtkWidget *) gtk_builder_get_object(dialog_interface, "dialog"); - - if (!window || !dialog){ + inputCliente = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputCliente"); + lista = (GtkTreeView *) gtk_builder_get_object(dialog_interface, "tree_view_carrinho"); + inputSubmit = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputSubmit"); + inputPrecoFinal = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputPrecoFinal"); + inputDesconto = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputDesconto"); + + if (!window || !dialog || !inputCliente || !lista || !inputCliente){ fprintf(stderr, "Nao ha nenhum objeto com este id.\n"); return; } - vendas_cliente_completion(dialog_interface); + gtk_spin_button_set_value((GtkSpinButton *) inputDesconto, CARRINHO->descontopercento); + + char preco[40]; + sprintf(preco, "Preço Final: R$ %.2lf", CARRINHO->preco_final); + gtk_label_set_text((GtkLabel *) inputPrecoFinal, preco); + recarregar_lista_carrinho(lista); + + vendas_cliente_completion(inputCliente); gtk_window_set_title((GtkWindow *) dialog, "Nova Venda"); gtk_window_set_transient_for((GtkWindow *) dialog, (GtkWindow *) window); gtk_widget_show(dialog); + + g_signal_connect(inputDesconto, "value-changed", G_CALLBACK(atualizar_valor_final), dialog_interface); + g_signal_connect(inputSubmit, "clicked", G_CALLBACK(nova_venda_callback), dialog_interface); + g_signal_connect(inputSubmit, "clicked", G_CALLBACK(recarregar_aba_vendas), interface); + g_signal_connect(dialog, "destroy", G_CALLBACK(recarregar_aba_vendas), interface); +} + +void atualizar_valor_final(GtkWidget *inputDesconto, GtkBuilder *dialog_interface){ + GtkWidget *inputPrecoFinal; + inputDesconto = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputDesconto"); + inputPrecoFinal = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputPrecoFinal"); + char preco[40]; + + definir_desconto(CARRINHO, gtk_spin_button_get_value((GtkSpinButton *) inputDesconto)); + + sprintf(preco, "Preço Final: R$ %.2lf", CARRINHO->preco_final); + gtk_label_set_text((GtkLabel *) inputPrecoFinal, preco); +} + +void nova_venda_callback(GtkWidget *botao, GtkBuilder *dialog_interface){ + GtkWidget *dialog, *inputCliente, *inputDesconto, *inputPrecoFinal; + GtkTreeView *lista; + + lista = (GtkTreeView *) gtk_builder_get_object(dialog_interface, "tree_view_carrinho"); + dialog = (GtkWidget *) gtk_builder_get_object(dialog_interface, "dialog"); + inputCliente = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputCliente"); + inputDesconto = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputDesconto"); + + if (!strlen((char *) gtk_entry_get_text((GtkEntry *) inputCliente)) > 0){ + mensagem_simples_vendas((GtkWindow *) dialog, "Atenção", "Digite o nome de algum cliente."); + return; + } + + int ocorrencias; + struct cliente **clientes = buscar_cliente(CLIENTES_LISTA, (char *) gtk_entry_get_text((GtkEntry *) inputCliente), &ocorrencias); + + if (ocorrencias > 0) + if (!CARRINHO->tamanho > 0) + mensagem_simples_vendas((GtkWindow *) dialog, "Erro", "O carrinho está vazio."); + else if (strcmp((char *) gtk_entry_get_text((GtkEntry *) inputCliente), clientes[0]->nome) == 0){ + nova_venda(VENDAS_LISTA, *clientes[0], CARRINHO); + CARRINHO = criar_carrinho(); + gtk_widget_destroy(dialog); + } else + mensagem_simples_vendas((GtkWindow *) dialog, "Erro", "Não foi possível encontrar este cliente."); + else + mensagem_simples_vendas((GtkWindow *) dialog, "Erro", "Não foi possível encontrar este cliente."); +} + +void mensagem_simples_vendas(GtkWindow *janela, gchar *titulo, gchar *mensagem){ + GtkWidget *dialog; + GtkDialogFlags flags; + + flags = GTK_DIALOG_DESTROY_WITH_PARENT; + dialog = gtk_message_dialog_new(janela, flags, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, mensagem); + gtk_window_set_title((GtkWindow *) dialog, titulo); + + g_signal_connect(dialog, "response", G_CALLBACK(gtk_widget_destroy), dialog); + g_signal_connect(dialog, "destroy", G_CALLBACK(recarregar_aba_produtos), NULL); + gtk_widget_show_all(dialog); +} + +void carrinho_dialog(GtkWidget *botao, GtkBuilder *interface){ + GtkBuilder *dialog_interface; + const gchar dialog_interface_nome[] = "interfaces/carrinho.xml"; + GtkWidget *window, *dialog, *inputProduto, *submit; + GtkTreeView *lista; + + GError *erro = NULL; + + if (dialogOpened) + return; + else + dialogOpened = true; + + dialog_interface = gtk_builder_new(); + if (!gtk_builder_add_from_file(dialog_interface, dialog_interface_nome, &erro)){ + fprintf(stderr, "Nao foi possivel inicializar a GUI: %s\n", erro->message); + return ; + } + + window = (GtkWidget *) gtk_builder_get_object(interface, "window"); + dialog = (GtkWidget *) gtk_builder_get_object(dialog_interface, "dialog"); + inputProduto = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputProduto"); + submit = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputSubmit"); + lista = (GtkTreeView *) gtk_builder_get_object(dialog_interface, "tree_view_carrinho"); + + if (!window || !dialog || !inputProduto || !submit || !lista){ + fprintf(stderr, "Nao ha nenhum objeto com este id.\n"); + return; + } + + recarregar_lista_carrinho(lista); + + g_signal_connect(submit, "clicked", G_CALLBACK(carrinho_callback), dialog_interface); + g_signal_connect(dialog, "destroy", G_CALLBACK(recarregar_aba_vendas), interface); + + vendas_produto_completion(inputProduto); + + gtk_window_set_title((GtkWindow *) dialog, "Carrinho"); + gtk_window_set_transient_for((GtkWindow *) dialog, (GtkWindow *) window); + gtk_widget_show(dialog); +} + +void recarregar_lista_carrinho(GtkTreeView *lista){ + GtkListStore *lista_estrutura; + GtkTreeIter lista_estrutura_primaria; + + lista_estrutura = (GtkListStore *) gtk_tree_view_get_model(lista); + gtk_list_store_clear(lista_estrutura); + + char preco[17]; + int i; + for (i = 0; i < CARRINHO->tamanho; i++){ + sprintf(preco, "R$ %.2lf", CARRINHO->produtos[i].preco); + + gtk_list_store_append(lista_estrutura, &lista_estrutura_primaria); + gtk_list_store_set(lista_estrutura, &lista_estrutura_primaria, 0, CARRINHO->produtos[i].nome, 1, CARRINHO->produtos[i].quantidade, 2, preco, -1); + } +} + +void carrinho_callback(GtkWidget *botao, GtkBuilder *dialog_interface){ + GtkWidget *dialog, *inputProduto, *inputQuantidade; + GtkTreeView *lista; + dialog = (GtkWidget *) gtk_builder_get_object(dialog_interface, "dialog"); + inputProduto = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputProduto"); + inputQuantidade = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputQuantidade"); + lista = (GtkTreeView *) gtk_builder_get_object(dialog_interface, "tree_view_carrinho"); + + if (!dialog || !inputProduto || !inputQuantidade){ + fprintf(stderr, "Nao ha nenhum objeto com este id.\n"); + return; + } + + int ocorrencias; + struct produto **produtos = buscar_produto(PRODUTOS_LISTA, (char *) gtk_entry_get_text((GtkEntry *) inputProduto), &ocorrencias); + + if (ocorrencias > 0) + if (strcmp((char *) gtk_entry_get_text((GtkEntry *) inputProduto), produtos[0]->nome) == 0){ + adicionar_produto_carrinho(CARRINHO, *produtos[0], gtk_spin_button_get_value((GtkSpinButton *) inputQuantidade)); + recarregar_lista_carrinho(lista); + } else + mensagem_simples_vendas((GtkWindow *) dialog, "Erro", "Nao foi possivel encontrar este produto."); + else + mensagem_simples_vendas((GtkWindow *) dialog, "Erro", "Nao foi possivel encontrar este produto."); } void vendas_botao(GtkBuilder *interface){ - GtkWidget *botao_nova_venda; + GtkWidget *botao_nova_venda, *botao_carrinho; botao_nova_venda = (GtkWidget *) gtk_builder_get_object(interface, "botao_nova_venda"); gtk_tool_item_set_tooltip_text((GtkToolItem *) botao_nova_venda, "Nova Venda"); g_signal_connect(botao_nova_venda, "clicked", G_CALLBACK(nova_venda_dialog), interface); + + botao_carrinho = (GtkWidget *) gtk_builder_get_object(interface, "botao_carrinho"); + gtk_tool_item_set_tooltip_text((GtkToolItem *) botao_carrinho, "Carrinho"); + g_signal_connect(botao_carrinho, "clicked", G_CALLBACK(carrinho_dialog), interface); } -void vendas_cliente_completion(GtkBuilder *dialog_interface){ - GtkWidget *inputCliente; +void vendas_cliente_completion(GtkWidget *inputCliente){ GtkTreeModel *lista; GtkTreeIter lista_estrutura_primaria; - inputCliente = (GtkWidget *) gtk_builder_get_object(dialog_interface, "inputCliente"); lista = (GtkTreeModel *) gtk_list_store_new(1, G_TYPE_STRING); int i; @@ -91,4 +318,25 @@ void vendas_cliente_completion(GtkBuilder *dialog_interface){ gtk_entry_completion_set_popup_completion(inputClienteCompletion, TRUE); gtk_entry_set_completion((GtkEntry *) inputCliente, inputClienteCompletion); +} + +void vendas_produto_completion(GtkWidget *inputProduto){ + GtkTreeModel *lista; + GtkTreeIter lista_estrutura_primaria; + + lista = (GtkTreeModel *) gtk_list_store_new(1, G_TYPE_STRING); + + int i; + for (i = 0; i < PRODUTOS_LISTA->tamanho; i++){ + gtk_list_store_append((GtkListStore *) lista, &lista_estrutura_primaria); + gtk_list_store_set((GtkListStore *) lista, &lista_estrutura_primaria, 0, PRODUTOS_LISTA->produtos[i].nome, -1); + } + + GtkEntryCompletion *inputProdutoCompletion = gtk_entry_completion_new(); + gtk_entry_completion_set_model(inputProdutoCompletion, lista); + gtk_entry_completion_set_text_column(inputProdutoCompletion, 0); + gtk_entry_completion_set_minimum_key_length(inputProdutoCompletion, 1); + gtk_entry_completion_set_popup_completion(inputProdutoCompletion, TRUE); + + gtk_entry_set_completion((GtkEntry *) inputProduto, inputProdutoCompletion); } \ No newline at end of file