Files
17168ERP/web/admin/activity/index2.aspx.cs
2025-08-29 01:27:25 +08:00

195 lines
8.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Spreadsheet;
using static TreeView;
public partial class admin_activity_index2 : MyWeb.config
{
private Model.ezEntities _db = new Model.ezEntities();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BuildKind();
InitEnumsOptions<Model.activity.category>(s_category);
}
}
#region
public void BuildKind()
{
s_kind.Items.Clear();
s_kind.Items.Add(new ListItem("請選擇", ""));
//buildMultiKind(s_kind, "actItem_kind", 0, "", 1, Model.activity.ItemLevelMax);
var qry1 = new TreeView().get_data2(_db.actItem_kind.Select(o => new TreeItem()
{
kind = o.kind,
num = o.num,
root = o.root,
range = o.range,
}).OrderBy(x => x.root).ThenBy(x => x.kind).ToList(), 0, 0);
if (qry1.Count() > 0)
foreach (var qq in qry1)
s_kind.Items.Add(new ListItem(new TreeView().RptDash(qq.Level) + qq.kind, qq.num.ToString()));
}
#endregion
#region Excel
protected void excel_Click(object sender, EventArgs e)
{
var memoryStream = new MemoryStream();
using (var doc = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook))
{
var wb = doc.AddWorkbookPart();
wb.Workbook = new Workbook();
var sheets = wb.Workbook.AppendChild(new Sheets());
//建立第一個頁籤
var ws = wb.AddNewPart<WorksheetPart>();
ws.Worksheet = new Worksheet();
sheets.Append(new Sheet()
{
Id = wb.GetIdOfPart(ws),
SheetId = 1,
Name = "品項管理​"
});
//設定欄寬
var cu = new Columns();
cu.Append(
new Column { Min = 1, Max = 5, Width = 15, CustomWidth = true },
new Column { Min = 6, Max = 7, Width = 10, CustomWidth = true }
);
ws.Worksheet.Append(cu);
//建立資料頁
var sd = new SheetData();
ws.Worksheet.AppendChild(sd);
//第一列資料
var tr = new Row();
tr.Append(
new Cell() { CellValue = new CellValue("項目名稱"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("項目分類"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("類別"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("預設金額"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("庫存狀態"), DataType = CellValues.String },
new Cell() { CellValue = new CellValue("停用"), DataType = CellValues.String }
);
sd.AppendChild(tr);
//查詢要匯出的資料
var fileDt = _db.files.AsEnumerable().Where(f => f.subject.Contains(fileTxt.Value)).Select(f => f.num.ToString());//文件
var qry = _db.actItems.AsEnumerable();
//每個品項在每個倉庫的結餘量
var stockDt = (
from t_min_max in (
from stock in _db.stocks
group stock by new { stock.kind, stock.actItem_num } into g
select new
{
g.Key.kind,
g.Key.actItem_num,
min_id = g.Min(x => x.num),
max_id = g.Max(x => x.num)
}
)
join t_min in _db.stocks on t_min_max.min_id equals t_min.num
join t_max in _db.stocks on t_min_max.max_id equals t_max.num into t_maxGroup
from t_max in t_maxGroup.DefaultIfEmpty()
select new
{
t_min_max.kind,
t_min_max.actItem_num,
final_last = t_min_max.min_id != t_min_max.max_id ? t_max.final_stock ?? 0 : t_min.final_stock ?? 0,
rent_last = t_min_max.min_id != t_min_max.max_id ? t_max.rent_stock ?? 0 : t_min.rent_stock ?? 0
}
).ToList();
if (!string.IsNullOrEmpty(s_subject.Value))
qry = qry.Where(o => o.subject.Contains(s_subject.Value.Trim()));
if (!string.IsNullOrEmpty(s_kind.SelectedValue))
qry = qry.Where(o => o.kind == Convert.ToInt32(s_kind.SelectedValue));
if (!string.IsNullOrEmpty(fileTxt.Value))
qry = qry.Where(o => o.actItem_files.Where(f2 => f2.actItem_num == o.num && fileDt.ToArray().Contains(f2.files_num.ToString())).Count() > 0);
var tdesc = publicFun.enum_desc<Model.activity.category>();
qry = qry.OrderByDescending(o => o.num);
var list = qry.ToList();
if (list.Count > 0)
{
foreach (var item in list)
{
var stock = new
{
//catePQty = (item.stocks.Where(o => o.category == (int)Model.stock.type.Purchase || o.category == (int)Model.stock.type.Return).Select(o => o.qty).Sum() ?? 0) - (item.stocks.Where(o => o.category == (int)Model.stock.type.Reduce || o.category == (int)Model.stock.type.Rent).Select(o => o.qty).Sum() ?? 0),
//cateRQty = (item.stocks.Where(o => o.category == (int)Model.stock.type.Rent).Select(o => o.qty).Sum() ?? 0) - (item.stocks.Where(o => o.category == (int)Model.stock.type.Return).Select(o => o.qty).Sum() ?? 0),
final_stock = stockDt.Where(o => o.actItem_num == item.num).Select(o => o.final_last).Sum(),
rent_stock = stockDt.Where(o => o.actItem_num == item.num).Select(o => o.rent_last).Sum(),
};
//新增資料列
tr = new Row();
tr.Append(
new Cell() { CellValue = new CellValue(item.subject), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.kind.HasValue ? item.actItem_kind.kind : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.category.HasValue ? tdesc[item.category ?? 1] : ""), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.price.HasValue ? ValMoney(item.price.Value) : ""), DataType = CellValues.String },
//new Cell() { CellValue = new CellValue((stock.catePQty > 0? stock.catePQty.ToString():"")+(stock.cateRQty > 0?("(租借未還:"+ stock.cateRQty+")"):"")), DataType = CellValues.String },
new Cell() { CellValue = new CellValue((stock.final_stock > 0? stock.final_stock.ToString():"")+(stock.rent_stock > 0?("(租借未還:"+ stock.rent_stock + ")"):"")), DataType = CellValues.String },
new Cell() { CellValue = new CellValue(item.status.Equals("N")? "是":""), DataType = CellValues.String }
);
sd.AppendChild(tr);
}
Model.admin_log admin_log = new Model.admin_log();
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Item, (int)Model.admin_log.Status.Excel, admin_log.LogViewBtn(list.Select(x => x.subject).ToList()));
}
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=品項管理.xlsx");
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.BinaryWrite(memoryStream.ToArray());
HttpContext.Current.Response.End();
}
#endregion
}