465 lines
18 KiB
C#
465 lines
18 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Logging;
|
||
using NPOI.SS.UserModel;
|
||
using NPOI.SS.Util;
|
||
using NPOI.XSSF.UserModel;
|
||
using SolarPower.Models;
|
||
using SolarPower.Models.PowerStation;
|
||
using SolarPower.Repository.Interface;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace SolarPower.Controllers
|
||
{
|
||
public class ElectricitySoldRecordController : MyBaseController<ElectricitySoldRecordController>
|
||
{
|
||
private readonly IElectricitySoldRecordRepository electricitySoldRecordRepository;
|
||
private readonly IPowerStationRepository powerStationRepository;
|
||
|
||
public ElectricitySoldRecordController(IElectricitySoldRecordRepository electricitySoldRecordRepository, IPowerStationRepository powerStationRepository) : base()
|
||
{
|
||
this.electricitySoldRecordRepository = electricitySoldRecordRepository;
|
||
this.powerStationRepository = powerStationRepository;
|
||
}
|
||
public IActionResult Index()
|
||
{
|
||
return View();
|
||
}
|
||
public async Task<ApiResult<string>> SaveSoldMoney(ElectricitySoldRecord post)
|
||
{
|
||
ApiResult<string> apiResult = new ApiResult<string>();
|
||
|
||
PowerStation powerStation = null;
|
||
|
||
try
|
||
{
|
||
powerStation = await powerStationRepository.GetOneAsync(post.PowerstationId);
|
||
|
||
if (post.Id == 0)
|
||
{
|
||
ElectricitySoldRecord record = new ElectricitySoldRecord()
|
||
{
|
||
EndAt = post.EndAt,
|
||
CreatedBy = myUser.Id,
|
||
Kwh = post.Kwh,
|
||
Money = post.Money,
|
||
PowerstationId = post.PowerstationId,
|
||
StartAt = post.StartAt
|
||
};
|
||
List<string> properties = new List<string>()
|
||
{
|
||
"EndAt",
|
||
"CreatedBy",
|
||
"Kwh",
|
||
"Money",
|
||
"PowerstationId",
|
||
"StartAt"
|
||
};
|
||
await electricitySoldRecordRepository.AddAsync(record, properties);
|
||
|
||
apiResult.Code = "0000";
|
||
apiResult.Msg = "新增成功";
|
||
}
|
||
else
|
||
{
|
||
ElectricitySoldRecord record = new ElectricitySoldRecord()
|
||
{
|
||
Id = post.Id,
|
||
EndAt = post.EndAt,
|
||
UpdatedBy = myUser.Id,
|
||
Kwh = post.Kwh,
|
||
Money = post.Money,
|
||
StartAt = post.StartAt
|
||
};
|
||
List<string> properties = new List<string>()
|
||
{
|
||
"Id",
|
||
"EndAt",
|
||
"UpdatedBy",
|
||
"Kwh",
|
||
"Money",
|
||
"StartAt"
|
||
};
|
||
await electricitySoldRecordRepository.Update(record, properties);
|
||
apiResult.Code = "0000";
|
||
apiResult.Msg = "儲存成功";
|
||
}
|
||
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
apiResult.Code = "9999";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
string json = System.Text.Json.JsonSerializer.Serialize(post);
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + json);
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
||
}
|
||
|
||
return apiResult;
|
||
}
|
||
|
||
[HttpPost]
|
||
public async Task<ActionResult> RecordTable(ElectricitySoldRecordTablePost info)
|
||
{
|
||
List<ElectricitySoldRecordTable> electricitySoldRecordTable = new List<ElectricitySoldRecordTable>();
|
||
ApiResult<List<ElectricitySoldRecordTable>> apiResult = new ApiResult<List<ElectricitySoldRecordTable>>();
|
||
try
|
||
{
|
||
if(info.StationId == null || info.Time == null)
|
||
{
|
||
apiResult.Code = "0000";
|
||
}
|
||
else
|
||
{
|
||
electricitySoldRecordTable = await electricitySoldRecordRepository.RecordTable(info);
|
||
foreach (ElectricitySoldRecordTable a in electricitySoldRecordTable)
|
||
{
|
||
a.Function = @"
|
||
<button type='button' class='btn btn-success btn-pills waves-effect waves-themed bill-btn'>發票</button>
|
||
<button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
|
||
<button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
|
||
a.CreatedDay = Convert.ToDateTime(a.CreatedAt).ToString("yyyy-MM-dd");
|
||
}
|
||
apiResult.Code = "0000";
|
||
apiResult.Data = electricitySoldRecordTable;
|
||
}
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
apiResult.Code = "9999";
|
||
apiResult.Msg = exception.ToString();
|
||
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "info=" + System.Text.Json.JsonSerializer.Serialize(info));
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
||
}
|
||
var result = Json(new
|
||
{
|
||
data = apiResult
|
||
});
|
||
return result;
|
||
}
|
||
|
||
public async Task<ApiResult<ElectricitySoldRecord>> GetOnePowerStation(int id)
|
||
{
|
||
ApiResult<ElectricitySoldRecord> apiResult = new ApiResult<ElectricitySoldRecord>();
|
||
ElectricitySoldRecord record = new ElectricitySoldRecord();
|
||
try
|
||
{
|
||
record = await electricitySoldRecordRepository.GetOneAsync(id);
|
||
apiResult.Code = "0000";
|
||
apiResult.Data = record;
|
||
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
apiResult.Code = "9999";
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id);
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
||
|
||
}
|
||
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
return apiResult;
|
||
}
|
||
|
||
public async Task<ApiResult<string>> DeleteRecord(int id)
|
||
{
|
||
ApiResult<string> apiResult = new ApiResult<string>();
|
||
try
|
||
{
|
||
await electricitySoldRecordRepository.DeleteOne(id);
|
||
apiResult.Code = "0000";
|
||
apiResult.Msg = "刪除成功";
|
||
|
||
}
|
||
catch (Exception exception)
|
||
{
|
||
apiResult.Code = "9999";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id);
|
||
Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message);
|
||
}
|
||
|
||
return apiResult;
|
||
|
||
}
|
||
|
||
|
||
public FileResult BillExcel (int post)
|
||
{
|
||
var workbook = new XSSFWorkbook();
|
||
#region excel設定
|
||
IFont font26 = workbook.CreateFont();
|
||
font26.FontName = "新細明體";
|
||
font26.FontHeightInPoints = 26;
|
||
font26.IsBold = true;
|
||
ICellStyle style26 = workbook.CreateCellStyle();
|
||
style26.SetFont(font26);
|
||
style26.Alignment = HorizontalAlignment.Center;
|
||
style26.VerticalAlignment = VerticalAlignment.Center;
|
||
|
||
//IFont font12Times = workbook.CreateFont();
|
||
//font12Times.FontName = "Times New Roman";
|
||
//font12Times.FontHeightInPoints = 12;
|
||
//IFont font18 = workbook.CreateFont();
|
||
//font18.FontName = "新細明體";
|
||
//font18.FontHeightInPoints = 18;
|
||
//font18.IsBold = true;
|
||
//ICellStyle styleTitle18 = workbook.CreateCellStyle();
|
||
//styleTitle18.SetFont(font18);
|
||
//styleTitle18.Alignment = HorizontalAlignment.Center;
|
||
//styleTitle18.VerticalAlignment = VerticalAlignment.Center;
|
||
//ICellStyle styleLeft12 = workbook.CreateCellStyle();
|
||
//styleLeft12.SetFont(font12);
|
||
//styleLeft12.Alignment = HorizontalAlignment.Left;
|
||
//styleLeft12.VerticalAlignment = VerticalAlignment.Center;
|
||
//ICellStyle styleLine12 = workbook.CreateCellStyle();
|
||
//styleLine12.SetFont(font12);
|
||
//styleLine12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
|
||
//styleLine12.VerticalAlignment = VerticalAlignment.Center;
|
||
//styleLine12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//styleLine12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//styleLine12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//styleLine12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//ICellStyle stylein12 = workbook.CreateCellStyle();
|
||
//stylein12.SetFont(font12Times);
|
||
//stylein12.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;
|
||
//stylein12.VerticalAlignment = VerticalAlignment.Center;
|
||
//stylein12.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//stylein12.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//stylein12.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//stylein12.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
|
||
//stylein12.WrapText = true;
|
||
|
||
IFont font14 = workbook.CreateFont();
|
||
font14.FontName = "新細明體";
|
||
font14.FontHeightInPoints = 14;
|
||
font14.IsBold = true;
|
||
ICellStyle style14 = workbook.CreateCellStyle();
|
||
style14.SetFont(font14);
|
||
style14.Alignment = HorizontalAlignment.Left;
|
||
style14.VerticalAlignment = VerticalAlignment.Center;
|
||
|
||
ICellStyle buttomLine14 = workbook.CreateCellStyle();
|
||
buttomLine14.BorderBottom = BorderStyle.Medium;
|
||
|
||
IFont fontBlue14 = workbook.CreateFont();
|
||
fontBlue14.FontName = "新細明體";
|
||
fontBlue14.FontHeightInPoints = 14;
|
||
fontBlue14.IsBold = true;
|
||
fontBlue14.Color = NPOI.HSSF.Util.HSSFColor.Indigo.Index;
|
||
ICellStyle buttomLineBlue14 = workbook.CreateCellStyle();
|
||
buttomLineBlue14.SetFont(fontBlue14);
|
||
buttomLineBlue14.BorderBottom = BorderStyle.Medium;
|
||
|
||
ICellStyle style14upleft = workbook.CreateCellStyle();
|
||
style14upleft.SetFont(font14);
|
||
style14upleft.Alignment = HorizontalAlignment.Center;
|
||
style14upleft.VerticalAlignment = VerticalAlignment.Center;
|
||
style14upleft.BorderTop = BorderStyle.Medium;
|
||
style14upleft.BorderLeft = BorderStyle.Medium;
|
||
style14upleft.BorderRight = BorderStyle.Thin;
|
||
style14upleft.BorderBottom = BorderStyle.Thin;
|
||
|
||
ICellStyle style14up = workbook.CreateCellStyle();
|
||
style14up.SetFont(font14);
|
||
style14up.Alignment = HorizontalAlignment.Center;
|
||
style14up.VerticalAlignment = VerticalAlignment.Center;
|
||
style14up.BorderTop = BorderStyle.Medium;
|
||
style14up.BorderLeft = BorderStyle.Thin;
|
||
style14up.BorderRight = BorderStyle.Thin;
|
||
style14up.BorderBottom = BorderStyle.Thin;
|
||
|
||
|
||
#endregion
|
||
|
||
var bill = electricitySoldRecordRepository.GetBill(post);
|
||
var sheet = workbook.CreateSheet("附件一-開立發票申請表");
|
||
|
||
IRow row;
|
||
ICell cell;
|
||
sheet.SetColumnWidth(0, 1 * 160 * 4);
|
||
sheet.SetColumnWidth(1, 4 * 160 * 8);
|
||
sheet.SetColumnWidth(2, 6 * 160 * 8);
|
||
sheet.SetColumnWidth(3, 4 * 160 * 8);
|
||
sheet.SetColumnWidth(4, 6 * 160 * 8);
|
||
sheet.SetColumnWidth(5, 6 * 160 * 8);
|
||
sheet.SetColumnWidth(6, 6 * 160 * 8);
|
||
|
||
CellRangeAddress region;
|
||
row = sheet.CreateRow(1);
|
||
cell = row.CreateCell(1);
|
||
region = new CellRangeAddress(1, 1, 1, 6);
|
||
sheet.AddMergedRegion(region);
|
||
cell.SetCellValue(bill.Result.Name);
|
||
cell.CellStyle = style26;
|
||
|
||
row = sheet.CreateRow(2);
|
||
cell = row.CreateCell(1);
|
||
region = new CellRangeAddress(2, 2, 1, 6);
|
||
sheet.AddMergedRegion(region);
|
||
cell.SetCellValue("開立統一發票申請表");
|
||
cell.CellStyle = style26;
|
||
|
||
row = sheet.CreateRow(4);
|
||
cell = row.CreateCell(5);
|
||
cell.SetCellValue("單據編號:");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(6);
|
||
cell.SetCellValue("");
|
||
cell.CellStyle = buttomLine14;
|
||
|
||
row = sheet.CreateRow(5); //第5行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("申 請 人 :");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("");
|
||
cell.CellStyle = buttomLine14;
|
||
|
||
cell = row.CreateCell(3);
|
||
cell.SetCellValue("分 機:");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(4);
|
||
cell.SetCellValue("");
|
||
cell.CellStyle = buttomLine14;
|
||
|
||
row = sheet.CreateRow(6); //第6行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("成本中心 :");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("");
|
||
cell.CellStyle = buttomLine14;
|
||
|
||
cell = row.CreateCell(3);
|
||
cell.SetCellValue("行動電話 :");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(4);
|
||
cell.SetCellValue("");
|
||
cell.CellStyle = buttomLine14;
|
||
|
||
row = sheet.CreateRow(7); //第7行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("領取方式 :");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(2);
|
||
region = new CellRangeAddress(7, 7, 2, 3);
|
||
sheet.AddMergedRegion(region);
|
||
cell.SetCellValue("□ 親自領取 □ 郵寄領取");
|
||
cell.CellStyle = style14;
|
||
|
||
row = sheet.CreateRow(9); //第9行
|
||
region = new CellRangeAddress(9, 9, 1, 2);
|
||
sheet.AddMergedRegion(region);
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("統一發票開立基本資料");
|
||
cell.CellStyle = style14;
|
||
|
||
row = sheet.CreateRow(10); //第10行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("開立方式:");
|
||
cell.CellStyle = style14;
|
||
|
||
region = new CellRangeAddress(10, 10, 2, 6);
|
||
sheet.AddMergedRegion(region);
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("□一次性開立 □ 週期性開立 (自 年 月至 年 月),請檢附合約影本。");
|
||
cell.CellStyle = style14;
|
||
|
||
row = sheet.CreateRow(12); //第12行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("買 受 人 :");
|
||
cell.CellStyle = style14;
|
||
|
||
region = new CellRangeAddress(12, 12, 2, 3);
|
||
sheet.AddMergedRegion(region);
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("台灣電力股份有限公司桃園區營業處");
|
||
cell.CellStyle = buttomLineBlue14;
|
||
|
||
row = sheet.CreateRow(13); //第13行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("統一編號 :");
|
||
cell.CellStyle = style14;
|
||
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("02488533");
|
||
cell.CellStyle = buttomLineBlue14;
|
||
|
||
|
||
row = sheet.CreateRow(14); //第14行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("地 址:");
|
||
cell.CellStyle = style14;
|
||
|
||
region = new CellRangeAddress(14, 14, 2, 4);
|
||
sheet.AddMergedRegion(region);
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("330桃園市觀音區中山路一段280巷90號(屋頂)");
|
||
cell.CellStyle = buttomLineBlue14;
|
||
|
||
row = sheet.CreateRow(16); //第16行
|
||
region = new CellRangeAddress(16, 16, 1, 2);
|
||
sheet.AddMergedRegion(region);
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("統一發票開立內容:");
|
||
cell.CellStyle = style14;
|
||
|
||
row = sheet.CreateRow(17); //第17行
|
||
cell = row.CreateCell(1);
|
||
cell.SetCellValue("項次");
|
||
cell.CellStyle = style14upleft;
|
||
|
||
cell = row.CreateCell(2);
|
||
cell.SetCellValue("品名");
|
||
cell.CellStyle = style14up;
|
||
|
||
cell = row.CreateCell(3);
|
||
cell.SetCellValue("數量");
|
||
cell.CellStyle = style14up;
|
||
|
||
cell = row.CreateCell(4);
|
||
cell.SetCellValue("發票開立金額(含稅)");
|
||
cell.CellStyle = style14up;
|
||
|
||
cell = row.CreateCell(5);
|
||
region = new CellRangeAddress(17, 17, 5, 6);
|
||
sheet.AddMergedRegion(region);
|
||
cell.SetCellValue("備註(Memo)");
|
||
cell.CellStyle = style14up;
|
||
|
||
cell = row.CreateCell(6);
|
||
cell.CellStyle = style14up;
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
var ms = new NpoiMemoryStream
|
||
{
|
||
AllowClose = false
|
||
};
|
||
workbook.Write(ms);
|
||
ms.Flush();
|
||
ms.Seek(0, SeekOrigin.Begin);
|
||
return File(ms, "application/vnd.ms-excel", "台電躉售發票" + ".xlsx");
|
||
}
|
||
|
||
|
||
}
|
||
}
|