using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bowin.Common.Utility;
using Bowin.Web.Controls.Mvc;
using EMIS.CommonLogic.TeachingMaterial;
using EMIS.ViewModel;
using EMIS.ViewModel.TeachingMaterial;
using EMIS.Web.Controls;
using Bowin.Common.Data;
using EMIS.CommonLogic.SystemServices;
using EMIS.CommonLogic.CalendarManage;
using System.IO;
using EMIS.Utility;
namespace EMIS.Web.Controllers.TeachingMaterial
{
[Authorization]
public class StockInController : Controller
{
public IStockInServices StockInServices { get; set; }
//编号生成
public ISerialNumberServices SerialNumberServices { get; set; }
public IStockInDetailServices StockInDetailServices { get; set; }
public ISchoolYearServices SchoolYearServices { get; set; }
public ISupplierDiscountSetServices supplierDiscountSetServices { get; set; }
///
/// 教材入库页面
///
///
[HttpGet]
public ActionResult List()
{
var schoolYear = SchoolYearServices.GetSchoolYearIsCurrent(true);
ViewBag.SchoolYearID = schoolYear == null ? "-1" : schoolYear.SchoolyearID.ToString();//默认当前学年
return View();
}
[HttpPost]
public ActionResult List(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//避开全选值
int? isLate = isLate = pararms.getExtraInt("LateDropdown");
DateTime? startStockInTime = DateTime.Now;
DateTime? endStockInTime = DateTime.Now;
Guid? supplierID = pararms.getExtraGuid("PublishDropdown");
Guid? schoolyearID = pararms.getExtraGuid("SchoolYearDropdown");
endStockInTime = pararms.getExtraDateTime("EndStockInTime");
startStockInTime = pararms.getExtraDateTime("StartStockInTime");
return base.Json(StockInServices.GetStockInViewGrid(configuretView, schoolyearID, supplierID, startStockInTime, endStockInTime, isLate,
new List { (int)CF_StockInType.PurchasingStockIn }, (int)pararms.page, (int)pararms.rows));
}
[HttpGet]
public ActionResult ReturnList()
{
var schoolYear = SchoolYearServices.GetSchoolYearIsCurrent(true);
ViewBag.SchoolYearID = schoolYear == null ? "-1" : schoolYear.SchoolyearID.ToString();//默认当前学年
return View();
}
[HttpPost]
public ActionResult ReturnList(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
//避开全选值
int? isLate = isLate = pararms.getExtraInt("LateDropdown");
DateTime? startStockInTime = DateTime.Now;
DateTime? endStockInTime = DateTime.Now;
Guid? supplierID = pararms.getExtraGuid("PublishDropdown");
Guid? schoolyearID = pararms.getExtraGuid("SchoolYearDropdown");
endStockInTime = pararms.getExtraDateTime("EndStockInTime");
startStockInTime = pararms.getExtraDateTime("StartStockInTime");
return base.Json(StockInServices.GetStockInViewGrid(configuretView, schoolyearID, supplierID, startStockInTime, endStockInTime, isLate,
new List { (int)CF_StockInType.Return }, (int)pararms.page, (int)pararms.rows));
}
[HttpGet]
public ActionResult Edit(Guid? stockInID)
{
var schoolYear = SchoolYearServices.GetSchoolYearIsCurrent(true);
StockInView stockInView;
if (stockInID != null && stockInID != Guid.Empty)
{
stockInView = StockInServices.GetSingleStockIn(stockInID.Value);
}
else
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
var defaultsupplier = supplierDiscountSetServices.GetSupplierDiscountSetViewList(configuretView, true, null).FirstOrDefault();
Guid? defaultID = null;
if (defaultsupplier != null)
{
defaultID = supplierDiscountSetServices.GetSupplierDiscountSetViewList(configuretView, true, null).FirstOrDefault().PublishID;
}
//ViewBag.DefaultID = defaultID;
string keyValue = SerialNumberServices.SetSN(DateTime.Now.ToString("yyyyMMdd"));
var stockInType = Convert.ToInt32(Request.Params["stockInType"]);//获取入库默认类型
stockInView = new StockInView()
{
StockInNumber = keyValue,
StockInType = stockInType,
StockInDocumentNo = StockInServices.GetStockInNo((CF_StockInType)stockInType),
StockInID = Guid.Empty,
SchoolyearID = schoolYear.SchoolyearID,
StockInDJTime = DateTime.Now,
SupplierID = defaultID
};
}
return View(stockInView);
}
[HttpPost]
public ActionResult Edit(StockInView stockInView)
{
try
{
string type = Request.Params["hid_ApprovalStatusName"];//0 保存 1 提交
var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
var TMList = DataGrid.GetTableData("dgStockInTMSelectList");//获取底部列表项数据
if (TMList == null || TMList.Count <= 0)
{
return Json(new ReturnMessage()
{
IsSuccess = false,
Message = "操作失败,原因:教材信息不能为空。"
});
}
else
{
StockInServices.Save(stockInView, TMList, user.UserID, type);
}
return Json(new ReturnMessage()
{
IsSuccess = true,
Message = "保存成功。"
});
}
catch (Exception ex)
{
return Json(new ReturnMessage()
{
IsSuccess = false,
Message = "保存失败,原因:" + ex.Message
});
}
}
///
/// Excel 导出
///
///
[HttpPost]
public ActionResult Excel()
{
NpoiExcelHelper neh = new NpoiExcelHelper();
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
int? isLate = isLate = Request.Form["LateDropdown"].ParseStrTo();
Guid? supplierID = Request.Form["PublishDropdown"].ParseStrTo();
Guid? schoolyearID = Request.Form["SchoolYearDropdown"].ParseStrTo();
DateTime? startStockInTime = DateTime.Now;
DateTime? endStockInTime = DateTime.Now;
endStockInTime = DateTime.Parse(Request.Form["EndStockInTime"]);
startStockInTime = DateTime.Parse(Request.Form["StartStockInTime"]);
var dt = StockInServices.GetStockInViewList(configuretView, schoolyearID, supplierID, startStockInTime, endStockInTime, isLate,
new List { (int)CF_StockInType.PurchasingStockIn }).Select(x => new
{
x.SchoolyearName,
x.StockInDocumentNo,
x.SupplierName,
x.HandlerUserName,
x.StockInDJTime,
x.StockInSumMoneyStr,
x.Desc
}).ToTable();
string[] liststring = { "学年学期", "入库单据号", "供应商", "经手人", "入库单据日期", "入库总金额", "备注" };
neh.Export(dt, liststring, "入库管理信息");
return RedirectToAction("MsgShow", "Common", new
{
msg = "导出成功!",
url = Url.Content("~/StockIn/List").AddMenuParameter()
});
}
///
/// 删除
///
///
///
[HttpPost]
public ActionResult Delete(string stockInIDs)
{
try
{
var stockInIDList = stockInIDs.Split(',').Select(x => (Guid)new Guid(x)).ToList();
StockInServices.Delete(stockInIDList);
return base.Json("删除成功。");
}
catch (Exception ex)
{
string mge = ex.Message;
// System.Data.SqlClient.SqlException num = ExceptionHelper.GetSqlException(ex);
return base.Json("删除失败,原因:" + mge);
}
}
///
/// 添加教材
///
///
public ActionResult TeachingMaterial()
{
var supplierID = Request["SupplierID"].ParseStrTo();
decimal? discount;
if (supplierID.HasValue)
{
discount = supplierDiscountSetServices.GetDiscountByPublishID((Guid)supplierID);
}
else {
discount = null;
}
ViewBag.Discount = discount;
ViewBag.SupplierID = supplierID;
return View();
}
///
/// 教材入库列表
///
///
[HttpGet]
public ActionResult GetStockInByTMList()
{
return View();
}
///
/// 查询教材入库列表
///
///
///
[HttpPost]
public ActionResult GetStockInByTMList(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(pararms);
var teachingMaterialPoolID = pararms.getExtraGuid("TeachingMaterialDropdown");
var teachingMaterialCodeID = pararms.getExtraGuid("TeachingMaterialCodeDropdown");
Guid? publishID = pararms.getExtraGuid("PublishDropdown");
var schoolyearID = pararms.getExtraGuid("SchoolyearID");
var supplierID = pararms.getExtraGuid("SupplierID");
decimal? discount = 1;
if (supplierID.HasValue)
{
discount = supplierDiscountSetServices.GetDiscountByPublishID((Guid)supplierID);
}
return base.Json(StockInServices.GetStockInByTeachingMaterialPoolViewGrid(configuretView, schoolyearID, teachingMaterialPoolID, teachingMaterialCodeID, discount, (int)pararms.page, (int)pararms.rows));
}
///
///
///
///
///
[HttpPost]
public ActionResult GetTMViewList(Guid? stockIn)
{
if (stockIn.HasValue)
{
var resultList = StockInServices.GetStockInByTMViewList(stockIn.Value);
return Json(new JsonDataGridResult { rows = resultList, total = resultList.Count });
}
else
{
return Json(new JsonDataGridResult());
}
}
///
/// 教材入库明细
///
///
///
[HttpGet]
public ActionResult StockInDetailList(string stockInID)
{
if (!string.IsNullOrEmpty(stockInID))
ViewData["stockInID"] = stockInID;
return View();
}
///
/// 教材入库明细
///
///
///
[HttpPost]
public ActionResult StockInDetailList(QueryParamsModel pararms)
{
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
Guid? stockId = Request["stockId"].ParseStrTo();
return base.Json(StockInDetailServices.GetStockInDetailViewGrid(stockId, (int)pararms.page, (int)pararms.rows));
}
///
/// Excel 导出
///
///
[HttpPost]
public ActionResult StockInDetailExcel(Guid? stockId)
{
NpoiExcelHelper neh = new NpoiExcelHelper();
ConfiguretView configuretView = ConfiguretExtensions.GetConfiguretermsView(null);
var dt = StockInDetailServices.GetStockInDetailViewList(configuretView, stockId).Select(x => new
{
x.SchoolyearName,
x.StockInDocumentNo,
x.StockInNumber,
x.TeachingMaterialCode,
x.TeachingMaterialName,
x.PublishName,
x.SupplierName,
x.PublishTime,
x.Author,
x.Quantity,
x.Price,
x.DiscountStr,
x.DiscountPriceStr,
x.TotalPriceStr,
x.TotalDollarStr,
x.HandlerUserName,
x.StockInUserName,
x.StockInTime
}).ToTable();
string[] liststring = { "学年学期", "入库单据号", "入库编号", "教材编号", "教材名称", "供应商",
"出版单位", "版本时间", "作者", "数量", "单价", "折扣率", "折合价", "码洋", "总价", "经手人", "入库人", "入库日期" };
neh.Export(dt, liststring, "入库明细信息");
return RedirectToAction("MsgShow", "Common", new
{
msg = "导出成功!",
url = Url.Content("~/StockInDetail/List").AddMenuParameter()
});
}
///
/// 提交
///
///
///
[HttpPost]
public ActionResult Submit(string stockInIDs)
{
try
{
var user = HttpContext.User as EMIS.Utility.FormValidate.CustomPrincipal;
var stockInIDList = stockInIDs.Split(',').Select(x => (Guid)new Guid(x)).ToList();
StockInServices.Submit(stockInIDList, user.UserID);
return base.Json("提交入库成功。");
}
catch (Exception ex)
{
string mge = ex.Message;
return base.Json("提交入库失败,原因:" + mge);
}
}
///
///
///
///
///
[HttpGet]
public ActionResult Import(string errorFile)
{
ViewBag.ErrorFile = errorFile;
return View();
}
[HttpPost]
public ActionResult Import(HttpPostedFileBase file)
{
try
{
//1.0 HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls
//2.0 XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx
if (!NpoiExcelHelper.GetIsCompatible(file.FileName))
{
throw new Exception("只允许上传xls和xlsx格式的Excel文件!");
}
return null;
}
catch (Exception ex)
{
return RedirectToAction("MsgShow", "Common", new
{
WindowID = Request["WindowID"],
msg = "导入失败,原因:" + ex.Message + "!",
url = Url.Action("List").AddMenuParameter()
});
}
}
///
/// 自动入库(根据采购计划自动入库)
///
///
[HttpGet]
public ActionResult AutoNewStockIn()
{
var schoolYear = SchoolYearServices.GetSchoolYearIsCurrent(true).SchoolyearID;
ViewData["schoolYear"] = schoolYear;
return View();
}
///
/// 自动入库确定(根据学生征订和教师征订)
///
///
///
[HttpPost]
public ActionResult AutoNewStockIn(Guid? schoolyearID)
{
try
{
var user = EMIS.Utility.FormValidate.CustomPrincipal.Current;
StockInServices.AutoNewStockIn(schoolyearID, user.UserID);
return Json(new ReturnMessage()
{
IsSuccess = true,
Message = "入库成功。"
});
}
catch (Exception ex)
{
return Json(new ReturnMessage()
{
IsSuccess = false,
Message = "入库失败,原因:" + ex.Message
});
}
}
}
}