Skip to content

Commit bb44192

Browse files
committed
Add project files.
1 parent 5c2415d commit bb44192

17 files changed

+1959
-0
lines changed

App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>

Billie.cs

Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Windows.Forms;
4+
using ClosedXML.Excel;
5+
6+
namespace Billie
7+
{
8+
public class Billie
9+
{
10+
private readonly BillieData Parameters;
11+
public Billie(BillieData Parameters)
12+
{
13+
this.Parameters = Parameters;
14+
}
15+
16+
private string ToLetters(int num)
17+
{
18+
var mod = num % 26;
19+
var pow = num / 26 | 0;
20+
string rem;
21+
if (mod != 0)
22+
rem = ((char)(64 + mod)).ToString();
23+
else
24+
{
25+
pow--;
26+
rem = "Z";
27+
}
28+
return pow != 0? ToLetters(pow) + rem : rem;
29+
}
30+
31+
public string GenerateBreakdown()
32+
{
33+
using (var workbook = new XLWorkbook())
34+
{
35+
int i;
36+
int columnAlphaCount = 2;
37+
IXLCell ActiveCell;
38+
var worksheet = workbook.Worksheets.Add("Bill Breakdown");
39+
40+
worksheet.Cell("A1").Value = Parameters.PurchaseDate.ToShortDateString().Replace("-", "/");
41+
MakeCell(worksheet.Cell("A1"), false, true, XLColor.FromArgb(200, 218, 247));
42+
worksheet.Cell("A2").Value = Parameters.ShopName;
43+
MakeCell(worksheet.Cell("A2"), true, true, XLColor.FromArgb(200, 218, 247));
44+
worksheet.Range("A2:A3").Merge();
45+
46+
for (i = 0; i < Parameters.Items.Count; i++, columnAlphaCount++, columnAlphaCount++)
47+
{
48+
worksheet.Range(ToLetters(columnAlphaCount) + "1:" + ToLetters(columnAlphaCount + 1) + "1").Merge();
49+
worksheet.Cell(ToLetters(columnAlphaCount) + "1").Value = Parameters.Items[i].Name;
50+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "1"), true, true, XLColor.FromArgb(200, 218, 247));
51+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "1").CellRight(), true, true, XLColor.FromArgb(200, 218, 247));
52+
53+
worksheet.Range(ToLetters(columnAlphaCount) + "2:" + ToLetters(columnAlphaCount + 1).ToString() + "2").Merge();
54+
worksheet.Cell(ToLetters(columnAlphaCount) + "2").Value = Parameters.Currency + " " + Parameters.Items[i].Price.ToString();
55+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "2"), true, true, XLColor.FromArgb(200, 218, 247));
56+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "2").CellRight(), true, true, XLColor.FromArgb(200, 218, 247));
57+
58+
worksheet.Cell(ToLetters(columnAlphaCount) + "3").Value = "Qty.";
59+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "3"), false, true, XLColor.FromArgb(200, 218, 247));
60+
worksheet.Cell(ToLetters(columnAlphaCount + 1) + "3").Value = "Price (" + Parameters.Currency + ")";
61+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount + 1).ToString() + "3"), false, true, XLColor.FromArgb(200, 218, 247));
62+
}
63+
64+
if (Parameters.TaxPercentage > 0)
65+
{
66+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "1"), true, true, XLColor.FromArgb(200, 218, 247));
67+
worksheet.Range(ToLetters(columnAlphaCount) + "1:" + ToLetters(columnAlphaCount) + "3").Merge();
68+
worksheet.Cell(ToLetters(columnAlphaCount) + "1").Value = "Tax (" + Parameters.TaxPercentage + "%)";
69+
70+
columnAlphaCount++;
71+
}
72+
73+
worksheet.Cell(ToLetters(columnAlphaCount) + "1").Value = "Total (" + Parameters.Currency + ")";
74+
MakeCell(worksheet.Cell(ToLetters(columnAlphaCount) + "1"), true, true, XLColor.FromArgb(200, 218, 247));
75+
worksheet.Range(ToLetters(columnAlphaCount) + "1:" + ToLetters(columnAlphaCount) + "3").Merge();
76+
77+
worksheet.Cell(ToLetters(columnAlphaCount) + "2").Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin);
78+
worksheet.Cell(ToLetters(columnAlphaCount) + "3").Style.Border.SetOutsideBorder(XLBorderStyleValues.Thin);
79+
worksheet.Cell(ToLetters(columnAlphaCount) + "2").Style.Border.SetOutsideBorderColor(XLColor.FromArgb(180, 195, 221));
80+
worksheet.Cell(ToLetters(columnAlphaCount) + "3").Style.Border.SetOutsideBorderColor(XLColor.FromArgb(180, 195, 221));
81+
82+
for (i = 0; i < Parameters.PeopleNames.Count; i++)
83+
{
84+
ActiveCell = worksheet.Cell("A" + (i + 4).ToString());
85+
ActiveCell.Value = " " + Parameters.PeopleNames[i];
86+
MakeCell(ActiveCell, true, false, XLColor.FromArgb(217, 217, 217));
87+
}
88+
89+
ActiveCell = worksheet.Cell("A" + (i + 4).ToString());
90+
ActiveCell.Value = "TOTAL";
91+
MakeCellRange(worksheet.Range("A" + (i + 4).ToString() + ":" + ToLetters(columnAlphaCount) + (i + 4).ToString()), true, true, XLColor.FromArgb(244, 204, 204));
92+
93+
IXLRange Range = worksheet.Range("B" + (i + 4).ToString() + ":" + ToLetters(columnAlphaCount - ((Parameters.TaxPercentage > 0) ? 2 : 1)) + (i + 4).ToString());
94+
Range.Merge();
95+
Range.Style.Border.SetOutsideBorderColor(XLColor.FromArgb(180, 195, 221));
96+
97+
worksheet.LastRowUsed(XLCellsUsedOptions.NormalFormats).Height = 30;
98+
99+
double grandTotal = 0;
100+
double taxTotal = 0;
101+
List<string> TaxExcemptedItems = new List<string>();
102+
103+
for (i = 0; i < Parameters.PeopleNames.Count; i++)
104+
{
105+
double Total = 0;
106+
double TaxReducedTotal = 0;
107+
for (int j = 0; j < Parameters.Maps[i].Items.Count; j++)
108+
for (int k = 0; k < Parameters.Items.Count; k++)
109+
if (Parameters.Maps[i].Items[j] == Parameters.Items[k].Name)
110+
{
111+
ActiveCell = worksheet.Cell(((char)(k * 2 + 66)).ToString() + (i + 4).ToString());
112+
if (Parameters.Maps[i].Denominators[j] == 1)
113+
ActiveCell.SetValue(Parameters.Maps[i].Quantities[j]);
114+
else
115+
ActiveCell.SetValue(Parameters.Maps[i].Quantities[j].ToString() + "/" + Parameters.Maps[i].Denominators[j].ToString());
116+
ActiveCell.CellRight().Value = ((Parameters.Maps[i].Quantities[j] * Parameters.Items[k].Price) / Parameters.Maps[i].Denominators[j]).ToString();
117+
Total += (Parameters.Maps[i].Quantities[j] * Parameters.Items[k].Price) / Parameters.Maps[i].Denominators[j];
118+
if (Parameters.Items[k].TaxExcempted)
119+
{
120+
if (!TaxExcemptedItems.Contains(Parameters.Items[k].Name))
121+
TaxExcemptedItems.Add(Parameters.Items[k].Name);
122+
}
123+
else
124+
TaxReducedTotal += (Parameters.Maps[i].Quantities[j] * Parameters.Items[k].Price) / Parameters.Maps[i].Denominators[j];
125+
}
126+
127+
grandTotal += Total;
128+
129+
if (Parameters.TaxPercentage > 0)
130+
{
131+
ActiveCell = worksheet.Cell(ToLetters(columnAlphaCount - 1) + (i + 4).ToString());
132+
ActiveCell.Value = ((Parameters.TaxPercentage / 100) * TaxReducedTotal).ToString();
133+
134+
ActiveCell = worksheet.Cell(ToLetters(columnAlphaCount) + (i + 4).ToString());
135+
ActiveCell.Value = ((Parameters.TaxPercentage / 100) * TaxReducedTotal + Total).ToString();
136+
137+
taxTotal += (Parameters.TaxPercentage / 100) * TaxReducedTotal;
138+
}
139+
else
140+
{
141+
ActiveCell = worksheet.Cell(ToLetters(columnAlphaCount) + (i + 4).ToString());
142+
ActiveCell.Value = Total.ToString();
143+
}
144+
145+
MakeCellRange(worksheet.Range("B" + (i + 4).ToString() + ":" + ToLetters(columnAlphaCount) + (i + 4).ToString()), false, true, XLColor.FromArgb(239, 239, 239));
146+
}
147+
148+
worksheet.Cell(ToLetters(columnAlphaCount) + (i + 4).ToString()).Value = (grandTotal + taxTotal).ToString();
149+
if (Parameters.TaxPercentage > 0)
150+
worksheet.Cell(ToLetters(columnAlphaCount - 1) + (i + 4).ToString()).Value = taxTotal.ToString();
151+
152+
AdjustContent(worksheet, columnAlphaCount, i + 3);
153+
154+
if (TaxExcemptedItems.Count > 0)
155+
{
156+
worksheet.Cell(ToLetters(columnAlphaCount + 1) + "1").Value = "Tax* (" + Parameters.TaxPercentage + "%)";
157+
worksheet.Range("A" + (i + 5).ToString() + ":" + ToLetters(columnAlphaCount) + (i + 5).ToString()).Merge();
158+
ActiveCell = worksheet.Cell("A" + (i + 5).ToString());
159+
ActiveCell.Style.Font.SetItalic(true);
160+
ActiveCell.Style.Font.SetFontSize(8);
161+
ActiveCell.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left);
162+
ActiveCell.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
163+
ActiveCell.Value = "* The " + Parameters.TaxPercentage.ToString() + "% tax was not accounted for " + string.Join(", ", TaxExcemptedItems) + " in the calculations.";
164+
}
165+
166+
string FileName = Parameters.ShopName + " - Bill Breakdown.xlsx";
167+
workbook.SaveAs(FileName);
168+
169+
return FileName;
170+
}
171+
}
172+
173+
private void AdjustContent(IXLWorksheet worksheet, int LastCol, int LastRow)
174+
{
175+
int maxWidth = 0;
176+
try
177+
{
178+
for (int i = 0; i < LastRow; i++)
179+
{
180+
int len = worksheet.Column(1).Cell(i + 1).Value.ToString().Length;
181+
if (len > maxWidth)
182+
maxWidth = len;
183+
}
184+
worksheet.Column(1).Width = maxWidth + 1;
185+
186+
int optimumWidth = 10;
187+
for (int Col = 2; Col <= LastCol; Col++, Col++)
188+
{
189+
maxWidth = 0;
190+
for (int i = 1; i <= LastRow; i++)
191+
{
192+
int len;
193+
if (i < 3)
194+
len = worksheet.Column(ToLetters(Col)).Cell(i).Value.ToString().Length / 2;
195+
else
196+
{
197+
int len1 = worksheet.Column(ToLetters(Col)).Cell(i).Value.ToString().Length;
198+
int len2 = worksheet.Column(ToLetters(Col + 1)).Cell(i).Value.ToString().Length;
199+
200+
len = len1 > len2 ? len1 : len2;
201+
}
202+
203+
if (len > maxWidth)
204+
maxWidth = len;
205+
}
206+
207+
if (maxWidth > optimumWidth)
208+
{
209+
worksheet.Column(Col.ToString()).Width = maxWidth;
210+
worksheet.Column(ToLetters(Col + 1)).Width = maxWidth;
211+
}
212+
else
213+
{
214+
worksheet.Column(ToLetters(Col)).Width = optimumWidth;
215+
worksheet.Column(ToLetters(Col + 1)).Width = optimumWidth;
216+
}
217+
}
218+
219+
maxWidth = 0;
220+
for (int i = 0; i < LastRow; i++)
221+
{
222+
int len = worksheet.Column(ToLetters(LastCol)).Cell(i + 1).Value.ToString().Length;
223+
if (len > maxWidth)
224+
maxWidth = len;
225+
}
226+
worksheet.Column(ToLetters(LastCol)).Width = maxWidth + 4;
227+
228+
if (Parameters.TaxPercentage > 0)
229+
{
230+
maxWidth = 0;
231+
for (int i = 0; i < LastRow; i++)
232+
{
233+
int len = worksheet.Column(ToLetters(LastCol - 1)).Cell(i + 1).Value.ToString().Length;
234+
235+
if (len > maxWidth)
236+
maxWidth = len;
237+
}
238+
239+
worksheet.Column(ToLetters(LastCol - 1)).Width = maxWidth + 2;
240+
}
241+
}
242+
catch (Exception e)
243+
{
244+
245+
}
246+
}
247+
248+
private void MakeCell(IXLCell Cell, bool Bold, bool Center, XLColor Color)
249+
{
250+
if (Bold)
251+
Cell.Style.Font.SetBold();
252+
253+
if (Center)
254+
Cell.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
255+
256+
Cell.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
257+
258+
Cell.Style.Fill.SetBackgroundColor(Color);
259+
Cell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
260+
Cell.Style.Border.SetOutsideBorderColor(XLColor.FromArgb(180, 195, 221));
261+
}
262+
263+
private void MakeCellRange(IXLRange Range, bool Bold, bool Center, XLColor Color)
264+
{
265+
foreach (IXLCell Cell in Range.Cells())
266+
{
267+
if (Bold)
268+
Cell.Style.Font.SetBold();
269+
270+
if (Center)
271+
Cell.Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center);
272+
273+
Cell.Style.Alignment.SetVertical(XLAlignmentVerticalValues.Center);
274+
275+
Cell.Style.Fill.SetBackgroundColor(Color);
276+
Cell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
277+
Cell.Style.Border.SetOutsideBorderColor(XLColor.FromArgb(180, 195, 221));
278+
}
279+
}
280+
}
281+
282+
public class PersonItemMap
283+
{
284+
public int PersonIndex;
285+
public List<string> Items;
286+
public List<double> Quantities;
287+
public List<double> Denominators;
288+
289+
public PersonItemMap()
290+
{
291+
Items = new List<string>();
292+
Quantities = new List<double>();
293+
Denominators = new List<double>();
294+
}
295+
}
296+
297+
public class BillieData
298+
{
299+
public List<string> PeopleNames;
300+
public List<ItemDetails> Items;
301+
public List<PersonItemMap> Maps;
302+
public DateTime PurchaseDate;
303+
public double TaxPercentage;
304+
public string ShopName;
305+
public string Currency;
306+
307+
public BillieData()
308+
{
309+
PeopleNames = new List<string>();
310+
Items = new List<ItemDetails>();
311+
Maps = new List<PersonItemMap>();
312+
}
313+
}
314+
315+
public class ItemDetails
316+
{
317+
public string Name;
318+
public double Price;
319+
public bool TaxExcempted;
320+
}
321+
}

0 commit comments

Comments
 (0)