447 lines
16 KiB
C#
447 lines
16 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 static publicFun;
|
|
using DocumentFormat.OpenXml.EMMA;
|
|
|
|
|
|
public partial class admin_stock_new_reg : MyWeb.config
|
|
{
|
|
private Model.ezEntities _db = new Model.ezEntities();
|
|
public string _cate = "";
|
|
public string _item = "";
|
|
public string _kind = "";
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
CallAjax();
|
|
if (!IsPostBack)
|
|
{
|
|
|
|
InitEnumsOptions<Model.stock.type>(category); //進出類型
|
|
|
|
if (isStrNull(Request["num"]))
|
|
{
|
|
uptime.Text = DateTime.Now.ToString("yyyy-MM-dd");
|
|
}
|
|
else
|
|
{
|
|
int _num = Val(Request["num"]);
|
|
var qry = _db.stocks.AsEnumerable();
|
|
var prod = qry.Where(q => q.num == _num).FirstOrDefault();
|
|
if (prod != null)
|
|
{
|
|
|
|
try
|
|
{
|
|
foreach (Control obj in cardBodyPanel.Controls)
|
|
{
|
|
|
|
if (obj is TextBox)
|
|
{
|
|
var ObjValue = prod.GetType().GetProperty(obj.ID).GetValue(prod, null);
|
|
if (!isStrNull(ObjValue))
|
|
{
|
|
var textBox = (TextBox)obj;
|
|
if (textBox.TextMode == TextBoxMode.Date)
|
|
{
|
|
textBox.Text = Convert.ToDateTime(ObjValue).ToString("yyyy-MM-dd");
|
|
}
|
|
else
|
|
{
|
|
textBox.Text = ObjValue.ToString();
|
|
}
|
|
}
|
|
}
|
|
else if (obj is DropDownList && ((DropDownList)obj).SelectedIndex == 0)
|
|
{
|
|
var ObjValue = prod.GetType().GetProperty(obj.ID).GetValue(prod, null);
|
|
if (!isStrNull(ObjValue))
|
|
{
|
|
var dropDown = (DropDownList)obj;
|
|
dropDown.SelectedValue = ObjValue.ToString();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
changeCate();
|
|
if (prod.kind.HasValue)
|
|
{
|
|
kind_txt.Value = prod.stock_kind.kind.ToString();
|
|
kind.Value = prod.kind.ToString();
|
|
_kind = prod.kind.ToString();
|
|
}
|
|
if (prod.activity_num.HasValue)
|
|
{
|
|
activity_num_txt.Value = prod.activity.subject.ToString();
|
|
activity_num.Value = prod.activity_num.ToString();
|
|
}
|
|
|
|
if (prod.actItem_num.HasValue)
|
|
{
|
|
actItem_num_txt.Value = prod.actItem.subject.ToString();
|
|
actItem_num.Value = prod.actItem_num.ToString();
|
|
_item = prod.actItem_num.ToString();
|
|
}
|
|
if (prod.mem_num.HasValue)
|
|
{
|
|
mem_num_txt.Value = prod.member.u_name.ToString();
|
|
mem_num.Value = prod.mem_num.ToString();
|
|
}
|
|
if (prod.supplier.HasValue)
|
|
{
|
|
supplierTxt.Value = prod.supplier1.u_name.ToString();
|
|
supplier.Value = prod.supplier.ToString();
|
|
}
|
|
if (prod.reason.HasValue)
|
|
{
|
|
reasonTxt.Value = prod.stock_reason.kind.ToString();
|
|
reason.Value = prod.reason.ToString();
|
|
}
|
|
if (prod.category.HasValue) { _cate = prod.category.Value.ToString(); }
|
|
if (prod.final_stock.HasValue) { final_stock.Text = prod.final_stock.Value.ToString(); }
|
|
if (prod.price.HasValue) { price.Text = prod.price.Value.ToString(); }
|
|
|
|
|
|
if (prod.reg_time.HasValue)
|
|
{
|
|
timePanel1.Visible = true;
|
|
reg_time.Text = prod.reg_time.Value.ToString("yyyy/MM/dd HH:mm:ss");
|
|
}
|
|
|
|
edit.Visible = true;
|
|
goback.Visible = true;
|
|
add.Visible = false;
|
|
|
|
|
|
}
|
|
else
|
|
{
|
|
Response.Redirect("index.aspx");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
protected void goback_Click(object sender, EventArgs e)
|
|
{
|
|
Response.Redirect("index.aspx?page=" + Convert.ToString(Request["page"]));
|
|
}
|
|
|
|
|
|
|
|
#region 資料新增
|
|
|
|
protected void add_Click(object sender, EventArgs e)
|
|
{
|
|
if (Page.IsValid) {
|
|
L_msg.Text = "";
|
|
|
|
int _fq = getFinalStock(Val(actItem_num.Value), Val(kind.Value));
|
|
if (chk())
|
|
{
|
|
if (_fq >= 0)
|
|
{
|
|
|
|
Model.stock stock = new Model.stock();
|
|
if (!isStrNull(uptime.Text)) { stock.uptime = ValDate(uptime.Text); }
|
|
stock.category = Val(category.SelectedValue);
|
|
if (!isStrNull(kind.Value)) { stock.kind = Val(kind.Value); }
|
|
if (!isStrNull(activity_num.Value)) { stock.activity_num = Val(activity_num.Value); }
|
|
if (!isStrNull(actItem_num.Value)) { stock.actItem_num = Val(actItem_num.Value); }
|
|
if (!isStrNull(qty.Text)) { stock.qty = Val(qty.Text); }
|
|
if ((Val(category.SelectedValue) ==(int)Model.stock.type.Purchase) && !isStrNull(price.Text)) { stock.price = ValFloat(price.Text); }
|
|
if (!isStrNull(mem_num.Value)) { stock.mem_num = Val(mem_num.Value); }
|
|
if ((Val(category.SelectedValue) == (int)Model.stock.type.Purchase) && !isStrNull(supplier.Value)) { stock.supplier = Val(supplier.Value); }
|
|
if (!isStrNull(reason.Value)) { stock.reason = Val(reason.Value); }
|
|
stock.debtor = debtor.Text;
|
|
stock.demo = demo.Text;
|
|
|
|
if (Val(category.SelectedValue) == 1 || Val(category.SelectedValue) == 4)
|
|
{
|
|
_fq += Val(qty.Text);
|
|
}
|
|
else if (Val(category.SelectedValue) == 2 || Val(category.SelectedValue) == 3)
|
|
{
|
|
_fq -= Val(qty.Text);
|
|
}
|
|
stock.final_stock = _fq;
|
|
|
|
int _rq = getFinalStock(Val(actItem_num.Value), Val(kind.Value), true);
|
|
if (Val(category.SelectedValue) == 3)
|
|
{
|
|
_rq += Val(qty.Text);
|
|
}
|
|
else if (Val(category.SelectedValue) == 4)
|
|
{
|
|
_rq -= Val(qty.Text);
|
|
}
|
|
stock.rent_stock = _rq;
|
|
|
|
stock.reg_time = DateTime.Now;
|
|
|
|
try
|
|
{
|
|
_db.stocks.Add(stock);
|
|
_db.SaveChanges();
|
|
Model.admin_log admin_log = new Model.admin_log();
|
|
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Stock, (int)Model.admin_log.Status.Insert, uptime.Text+ actItem_num_txt.Value);
|
|
|
|
Response.Redirect("index.aspx");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
L_msg.Text = ex.Message;
|
|
L_msg.Type = alert_type.danger;
|
|
//L_msg.Text = "操作失敗";
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
L_msg.Text = "結餘量不可為負";
|
|
L_msg.Type = alert_type.danger;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 資料修改
|
|
|
|
protected void edit_Click(object sender, EventArgs e)
|
|
{
|
|
if (Page.IsValid)
|
|
{
|
|
L_msg.Text = "";
|
|
int _num = Val(Request["num"]);
|
|
|
|
//if (chk(_num))
|
|
//{
|
|
|
|
|
|
Model.stock stock = _db.stocks.Where(q => q.num == _num).FirstOrDefault();
|
|
// if (stock != null)
|
|
// {
|
|
// if (!isStrNull(uptime.Text)) { stock.uptime = ValDate(uptime.Text); }
|
|
// stock.category = Val(category.SelectedValue);
|
|
|
|
// if (!isStrNull(kind.Value)) { stock.kind = Val(kind.Value); }
|
|
// if (!isStrNull(activity_num.Value)) { stock.activity_num = Val(activity_num.Value); }
|
|
// if (!isStrNull(actItem_num.Value)) { stock.actItem_num = Val(actItem_num.Value); }
|
|
// if (!isStrNull(qty.Text)) { stock.qty = Val(qty.Text); }
|
|
// if (!isStrNull(price.Text)) { stock.price = ValFloat(price.Text); }
|
|
// if (!isStrNull(mem_num.Value)) { stock.mem_num = Val(mem_num.Value); }
|
|
// stock.debtor = debtor.Text;
|
|
// stock.demo = demo.Text;
|
|
// }
|
|
|
|
// try
|
|
// {
|
|
// _db.SaveChanges();
|
|
// Response.Redirect("index.aspx?page=" + Convert.ToString(Request["page"]));
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// L_msg.Type = alert_type.danger;
|
|
// //L_msg.Text = ex.InnerException.Message;
|
|
// L_msg.Text = "操作失敗";
|
|
// }
|
|
|
|
//}
|
|
|
|
//只可修改備註
|
|
if (stock != null)
|
|
{
|
|
stock.demo = demo.Text;
|
|
|
|
try
|
|
{
|
|
_db.SaveChanges();
|
|
Model.admin_log admin_log = new Model.admin_log();
|
|
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Stock, (int)Model.admin_log.Status.Update, uptime.Text + actItem_num_txt.Value);
|
|
|
|
Response.Redirect("index.aspx?page=" + Convert.ToString(Request["page"]));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
L_msg.Type = alert_type.danger;
|
|
//L_msg.Text = ex.InnerException.Message;
|
|
L_msg.Text = "操作失敗";
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 檢查
|
|
protected bool chk(int num = 0)
|
|
{
|
|
bool success = true;
|
|
L_msg.Text = "";
|
|
|
|
//庫存不足時,若進出類型選擇 庫存減少 或 租借,須有庫存不足的提醒
|
|
|
|
//新增 -> 減項? ->檢查(庫存所有+本次qty)
|
|
//修改 -> 減項? ->檢查(庫存排除本num,+本次qty)
|
|
//有活動 ? ->加項(租借歸還) ->檢查(品項&活動相同)->租借歸還 不可大於 租借
|
|
//有活動 ? ->減項 ->檢查(品項&活動相同)->租借 ->新增 不可大於 目前庫存 /修改 (排除本num)
|
|
|
|
int _qty = Val(qty.Text);
|
|
int item_id = Val(actItem_num.Value);
|
|
int activity_id = !isStrNull(activity_num.Value) ? Val(activity_num.Value) : 0;
|
|
int _type = Val(category.SelectedValue);
|
|
|
|
|
|
var qry = _db.stocks.AsEnumerable();
|
|
qry = qry.Where(o => o.actItem_num == item_id);
|
|
qry = qry.Where(o => o.kind == Convert.ToInt32(kind.Value));
|
|
|
|
var allAdd = qry.Where(o => (o.category == ((int)Model.stock.type.Purchase) || o.category == ((int)Model.stock.type.Return)));
|
|
|
|
|
|
|
|
if (_type == (int)Model.stock.type.Reduce || _type == (int)Model.stock.type.Rent)
|
|
{
|
|
var allRed = qry.Where(o => (o.category == ((int)Model.stock.type.Reduce) || o.category == ((int)Model.stock.type.Rent)));
|
|
if (num > 0)//修改
|
|
{
|
|
allAdd = allAdd.Where(o => o.num != num); //排除自己
|
|
allRed = allRed.Where(o => o.num != num); //排除自己
|
|
}
|
|
int _allAddQty = Val(allAdd.Select(x => x.qty).Sum());
|
|
int _allRedQty = Val(allRed.Select(x => x.qty).Sum());
|
|
|
|
if (_allAddQty < _allRedQty + _qty)
|
|
{
|
|
success = false;
|
|
L_msg.Text = "庫存不足";
|
|
}
|
|
}
|
|
|
|
|
|
if (_type == (int)Model.stock.type.Return) //活動出租歸還沖銷
|
|
{
|
|
var actQry = qry;
|
|
if (activity_id > 0) //有活動
|
|
actQry = actQry.Where(o => o.activity_num == activity_id);
|
|
else
|
|
actQry = actQry.Where(o => o.activity_num == null);
|
|
|
|
//Rent
|
|
var actRed = actQry.Where(o => o.category == ((int)Model.stock.type.Rent));
|
|
if (num > 0)//修改
|
|
actQry = actRed.Where(o => o.num != num); //排除自己
|
|
int _actRedQty = Val(actRed.Select(x => x.qty).Sum()); //本活動出租數量
|
|
|
|
//Return
|
|
var actAdd = actQry.Where(o => o.category == ((int)Model.stock.type.Return));
|
|
if (num > 0)//修改
|
|
actAdd = actAdd.Where(o => o.num != num); //排除自己
|
|
int _actAddQty = Val(actAdd.Select(x => x.qty).Sum()); //本活動出租歸還數量
|
|
|
|
|
|
if (_actAddQty + _qty > _actRedQty)
|
|
{
|
|
success = false;
|
|
L_msg.Text = (activity_id > 0 ? "活動" : "") + "出租數量 : " + _actRedQty;
|
|
}
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
protected int getFinalStock(int actItem , int kind,bool rent=false)
|
|
{
|
|
int _fq = 0;
|
|
var qry = _db.stocks.AsEnumerable()
|
|
.Where(q => q.kind == kind) //同倉庫
|
|
.Where(q => q.actItem_num == actItem) //同品項
|
|
.OrderByDescending(q => q.num).FirstOrDefault();
|
|
if (qry != null)
|
|
{
|
|
_fq = qry.final_stock.HasValue ? qry.final_stock.Value : 0;
|
|
if(rent)
|
|
_fq = qry.rent_stock.HasValue ? qry.rent_stock.Value : 0;
|
|
|
|
}
|
|
return _fq;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 類型
|
|
protected void category_SelectedIndexChanged(object sender, EventArgs e)
|
|
{
|
|
changeCate();
|
|
}
|
|
protected void changeCate()
|
|
{
|
|
Panel1.Visible = category.SelectedValue == "1";
|
|
}
|
|
#endregion
|
|
|
|
#region Ajax
|
|
protected void CallAjax()
|
|
{
|
|
if (!isStrNull(Request["type"]))
|
|
{
|
|
Response.Clear();
|
|
Response.ContentType = "application/json";
|
|
AjaxInfo info = new AjaxInfo();
|
|
info.isOk = false;
|
|
info.fQty = 0;
|
|
info.msg = "";
|
|
|
|
if (ValString(Request["type"]) == "getFinalStock")
|
|
{
|
|
if (!string.IsNullOrEmpty(Request["kind"]) && !string.IsNullOrEmpty(Request["item"]))
|
|
{
|
|
info.fQty = getFinalStock(Convert.ToInt32(Request["item"]), Convert.ToInt32(Request["kind"]));
|
|
info.isOk = true;
|
|
}
|
|
else
|
|
info.msg = "參數不正確";
|
|
|
|
}
|
|
else
|
|
{
|
|
info.msg = "參數不正確";
|
|
}
|
|
|
|
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(info));
|
|
Response.End();
|
|
}
|
|
}
|
|
public class AjaxInfo
|
|
{
|
|
public bool isOk { get; set; }
|
|
public string msg { get; set; }
|
|
public int fQty { get; set; }
|
|
|
|
}
|
|
#endregion
|
|
} |