-
Notifications
You must be signed in to change notification settings - Fork 0
/
yearly.c
150 lines (141 loc) · 6.45 KB
/
yearly.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "headertoko.h"
int yearly() {
printf(" ========================= LAPORAN TAHUNAN ========================\n");
int year;
int modal = 0, masuk = 0, laba = 0, brgterjual = 0;
printf("Laporan tahun : ");
scanf("%d", &year);
FILE *report;
report = fopen("laporan.dat", "wb+");
FILE *master;
master = fopen("masterbarang.dat", "rb");
//allocate memory and input data from file masterbarang.dat
int max = 10;
int count = 1;
struct masterbarang* mstr = calloc(max, sizeof(struct masterbarang));
while (!feof(master)) {
fread(&mstr[count], sizeof(struct masterbarang), 1, master);
count++;
if (count == max) {
max = max + 10;
mstr = realloc(mstr, max*sizeof(struct masterbarang));
}
}
//get transaction according to inputs
int arr = count;
int sold[arr];
int i;
for (i=1; i < arr; i++) {
sold[i] = 0;
}
max = 10;
count = 1;
FILE *history;
history = fopen("histori.dat", "rb");
struct history histori = {"", 0, 0, 0, 0, 0, 0, 0, 0};
struct history* histdata = calloc(max, sizeof(struct history));
while (!feof(history)) {
fread(&histori, sizeof(struct history), 1, history);
if (histori.year == year) {
histdata[count] = histori;
count++;
}
if (count >= max) {
max = max + 10;
histdata = realloc(histdata, max*sizeof(struct history));
}
}
//display history
printf(" ========================== RIWAYAT %d ==========================\n", year);
printf("| %-2s-%-2s-%-4s | %-10s | %-5s | %-23s | %-5s|\n", "T", "B", "Thn", "Customer", "Kode", "Barang", "Qty");
printf("|------------------------------------------------------------------|\n");
int index = 0;
for (i=1; i < count; i++) {
if (histdata[i].kode != index) {
printf("| %-2d-%-2d-%-4d | %-10s | %-5d | %-23s | %-5d|\n", histdata[i].day, histdata[i].month, histdata[i].year, histdata[i].nama,
histdata[i].kode, mstr[histdata[i].kode].nama, histdata[i].qty);
modal = modal + (histdata[i].hargabeli*histdata[i].qty);
masuk = masuk + (histdata[i].hargajual*histdata[i].qty);
brgterjual = brgterjual + histdata[i].qty;
index = histdata[i].kode;
sold[histdata[i].kode] = sold[histdata[i].kode] + histdata[i].qty;
}
}
printf(" ------------------------------------------------------------------\n\n");
printf("============================ INSIGHT ==============================\n");
laba = masuk - modal;
printf("Jumlah barang terjual : %d\n", brgterjual);
printf("Modal : Rp%d\n", modal);
printf("Pemasukan : Rp%d\n", masuk);
if (laba >= 0) {
printf("Keuntungan/Laba : Rp%d\n", laba);
} else {
printf("Rugi : Rp%d\n", abs(laba));
}
printf("===================================================================\n\n");
printf(" =========================== RINCIAN =============================\n");
printf("| %-13s | %-32s | %13s|\n", "Kode", "Barang", "Terjual");
printf(" -----------------------------------------------------------------\n");
for (i=1; i < arr; i++) {
if (sold[i] != 0) {
printf("| %-13d | %-32s | %13d|\n", i, mstr[i].nama, sold[i]);
}
}
printf(" -----------------------------------------------------------------\n\n");
int chs;
printf("Cetak laporan ?\n(1 untuk ya/0 untuk kembali) ");
scanf("%d", &chs);
if (chs == 1) {
fprintf(report, " ==================================================================\n");
fprintf(report, "| TRIAL TOKO |\n");
fprintf(report, "|------------------------------------------------------------------|\n");
fprintf(report, "| Final Project Daspro B |\n");
fprintf(report, "| 051_Hanun Shaka P |\n");
fprintf(report, " ==================================================================\n\n");
fprintf(report, " =========================== RIWAYAT %d ===========================\n", year);
fprintf(report, "| %-2s-%-2s-%-4s | %-10s | %-5s | %-23s | %-5s|\n", "T", "B", "Thn", "Customer", "Kode", "Barang", "Qty");
fprintf(report, "|------------------------------------------------------------------|\n");
int index = 0;
int i;
for (i=1; i < count; i++) {
if (histdata[i].kode != index) {
fprintf(report, "| %-2d-%-2d-%-4d | %-10s | %-5d | %-23s | %-5d|\n", histdata[i].day,
histdata[i].month, histdata[i].year, histdata[i].nama,
histdata[i].kode, mstr[histdata[i].kode].nama, histdata[i].qty);
}
}
fprintf(report, " ------------------------------------------------------------------\n\n");
fprintf(report, "============================ INSIGHT ===============================\n");
fprintf(report, "Jumlah barang terjual : %d\n", brgterjual);
fprintf(report, "Modal : Rp%d\n", modal);
fprintf(report, "Pemasukan : Rp%d\n", masuk);
if (laba >= 0) {
fprintf(report, "Keuntungan/Laba : Rp%d\n", laba);
} else {
fprintf(report, "Rugi : Rp%d\n", abs(laba));
}
fprintf(report, "====================================================================\n");
fprintf(report, " =========================== RINCIAN =============================\n");
fprintf(report, "| %-13s | %-32s | %13s|\n", "Kode", "Barang", "Terjual");
fprintf(report, " -----------------------------------------------------------------\n");
for (i=1; i < arr; i++) {
if (sold[i] != 0) {
fprintf(report, "| %-13d | %-32s | %13d|\n", i, mstr[i].nama, sold[i]);
}
}
fprintf(report, " -----------------------------------------------------------------\n\n");
printf("Berhasil mencetak laporan.\n");
}
printf("Selesai menampilkan laporan, tekan enter untuk kembali ...\n");
_getch();
fclose(report);
fclose(history);
fclose(master);
}