protected void btnOrder_Click(object sender, EventArgs e) { DateTime currentDate = DateTime.Now; string username = Request.Cookies["userLoginSystem"].Value; var acc = AccountController.GetByUsername(username); if (acc != null) { if (acc.RoleID == 0 || acc.RoleID == 2) { // Change user string OrderNote = ""; if (username != hdfUsernameCurrent.Value) { OrderNote = "Được tính tiền giúp bởi " + username; username = hdfUsernameCurrent.Value; } int AgentID = Convert.ToInt32(acc.AgentID); int OrderType = hdfOrderType.Value.ToInt(); string AdditionFee = "0"; string DisCount = "0"; int CustomerID = 0; string CustomerPhone = txtPhone.Text.Trim().Replace(" ", ""); string CustomerName = txtFullname.Text.Trim(); string Nick = txtNick.Text.Trim(); string CustomerEmail = ""; string CustomerAddress = txtAddress.Text.Trim(); var checkCustomer = CustomerController.GetByPhone(CustomerPhone); if (checkCustomer != null) { CustomerID = checkCustomer.ID; string kq = CustomerController.Update(CustomerID, CustomerName, checkCustomer.CustomerPhone, CustomerAddress, "", Convert.ToInt32(checkCustomer.CustomerLevelID), Convert.ToInt32(checkCustomer.Status), checkCustomer.CreatedBy, currentDate, username, false, checkCustomer.Zalo, checkCustomer.Facebook, checkCustomer.Note, checkCustomer.ProvinceID.ToString(), Nick, checkCustomer.Avatar, Convert.ToInt32(checkCustomer.ShippingType), Convert.ToInt32(checkCustomer.PaymentType), Convert.ToInt32(checkCustomer.TransportCompanyID), Convert.ToInt32(checkCustomer.TransportCompanySubID), checkCustomer.CustomerPhone2); } else { string kq = CustomerController.Insert(CustomerName, CustomerPhone, CustomerAddress, CustomerEmail, 0, 0, currentDate, username, false, "", "", "", "", Nick); if (kq.ToInt(0) > 0) { CustomerID = kq.ToInt(0); } } string totalPrice = hdfTotalPrice.Value.ToString(); string totalPriceNotDiscount = hdfTotalPriceNotDiscount.Value; int PaymentStatus = 3; int ExcuteStatus = 2; int PaymentType = 1; int ShippingType = 1; bool IsHidden = false; int WayIn = 1; double DiscountPerProduct = Convert.ToDouble(pDiscount.Value); double TotalDiscount = Convert.ToDouble(pDiscount.Value) * Convert.ToDouble(hdfTotalQuantity.Value); string FeeShipping = pFeeShip.Value.ToString(); double GuestPaid = Convert.ToDouble(pGuestPaid.Value); double GuestChange = Convert.ToDouble(totalPrice) - GuestPaid; string OtherFeeName = txtOtherFeeName.Text; double OtherFeeValue = Convert.ToDouble(pOtherFee.Value); var ret = OrderController.InsertOnSystem(AgentID, OrderType, AdditionFee, DisCount, CustomerID, CustomerName, CustomerPhone, CustomerAddress, CustomerEmail, totalPrice, totalPriceNotDiscount, PaymentStatus, ExcuteStatus, IsHidden, WayIn, currentDate, username, DiscountPerProduct, TotalDiscount, FeeShipping, GuestPaid, GuestChange, PaymentType, ShippingType, OrderNote, DateTime.Now, OtherFeeName, OtherFeeValue, 1); int OrderID = ret.ID; if (OrderID > 0) { ProductPOS POS = JsonConvert.DeserializeObject <ProductPOS>(hdfListProduct.Value); List <tbl_OrderDetail> orderDetails = new List <tbl_OrderDetail>(); List <tbl_StockManager> stockManager = new List <tbl_StockManager>(); foreach (ProductGetOut item in POS.productPOS) { orderDetails.Add( new tbl_OrderDetail() { AgentID = AgentID, OrderID = OrderID, SKU = item.SKU, ProductID = item.ProductType == 1 ? item.ProductID : 0, ProductVariableID = item.ProductType == 1 ? 0 : item.ProductVariableID, ProductVariableDescrition = item.ProductVariableSave, Quantity = item.QuantityInstock, Price = item.Giabanle, Status = 1, DiscountPrice = 0, ProductType = item.ProductType, CreatedDate = currentDate, CreatedBy = username, IsCount = true } ); int parentID = item.ProductID; var variable = ProductVariableController.GetByID(item.ProductVariableID); if (variable != null) { parentID = Convert.ToInt32(variable.ProductID); } stockManager.Add( new tbl_StockManager() { AgentID = AgentID, ProductID = item.ProductType == 1 ? item.ProductID : 0, ProductVariableID = item.ProductType == 1 ? 0 : item.ProductVariableID, Quantity = item.QuantityInstock, QuantityCurrent = 0, Type = 2, NoteID = "Xuất kho bán POS", OrderID = OrderID, Status = 3, SKU = item.SKU, CreatedDate = currentDate, CreatedBy = username, MoveProID = 0, ParentID = parentID } ); } OrderDetailController.Insert(orderDetails); StockManagerController.Insert(stockManager); string refund = Request.Cookies["refund"].Value; if (refund != "1") { string[] RefundID = refund.Split('|'); var update = RefundGoodController.UpdateStatus(RefundID[0].ToInt(), username, 2, OrderID); var updateor = OrderController.UpdateRefund(OrderID, RefundID[0].ToInt(), username); } Response.Cookies["refund"].Expires = DateTime.Now.AddDays(-1d); Response.Cookies.Add(Response.Cookies["refund"]); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$(function () { printInvoice(" + OrderID + ") });", true); } } } }
protected void btnOrder_Click(object sender, EventArgs e) { try { DateTime currentDate = DateTime.Now; string username = Request.Cookies["usernameLoginSystem"].Value; var acc = AccountController.GetByUsername(username); if (acc != null) { if (acc.RoleID == 0 || acc.RoleID == 2) { #region Lấy thông tin khởi tạo Order // Change user string UserHelp = ""; if (username != hdfUsernameCurrent.Value) { UserHelp = username; username = hdfUsernameCurrent.Value; } int AgentID = Convert.ToInt32(acc.AgentID); int OrderType = hdfOrderType.Value.ToInt(); string AdditionFee = "0"; string DisCount = "0"; int CustomerID = 0; string CustomerPhone = Regex.Replace(txtPhone.Text.Trim(), @"[^\d]", ""); string CustomerName = txtFullname.Text.Trim().ToLower().ToTitleCase(); string CustomerEmail = ""; string CustomerAddress = txtAddress.Text.Trim().ToTitleCase(); int ProvinceID = hdfProvinceID.Value.ToInt(0); int DistrictID = hdfDistrictID.Value.ToInt(0); int WardID = hdfWardID.Value.ToInt(0); var checkCustomer = CustomerController.GetByPhone(CustomerPhone); string kq = ""; #region Cập nhật thông tin khách hàng if (checkCustomer != null) { CustomerID = checkCustomer.ID; kq = CustomerController.Update(CustomerID, CustomerName, checkCustomer.CustomerPhone, CustomerAddress, "", checkCustomer.CustomerLevelID.Value, checkCustomer.Status.Value, checkCustomer.CreatedBy, currentDate, username, false, checkCustomer.Zalo, checkCustomer.Facebook, checkCustomer.Note, checkCustomer.Nick, checkCustomer.Avatar, checkCustomer.ShippingType.Value, checkCustomer.PaymentType.Value, checkCustomer.TransportCompanyID.Value, checkCustomer.TransportCompanySubID.Value, checkCustomer.CustomerPhone2, ProvinceID, DistrictID, WardID); } else { kq = CustomerController.Insert(CustomerName, CustomerPhone, CustomerAddress, CustomerEmail, 0, 0, currentDate, username, false, "", "", "", "", "", 0, 0, 0, 0, "", ProvinceID, DistrictID, WardID); if (kq.ToInt(0) > 0) { CustomerID = kq.ToInt(0); } } #endregion string totalPrice = hdfTotalPrice.Value.ToString(); string totalPriceNotDiscount = hdfTotalPriceNotDiscount.Value; int PaymentStatus = 3; int ExcuteStatus = 2; int PaymentType = 1; int ShippingType = 1; bool IsHidden = false; int WayIn = 1; double DiscountPerProduct = Convert.ToDouble(pDiscount.Value); double TotalDiscount = Convert.ToDouble(pDiscount.Value) * Convert.ToDouble(hdfTotalQuantity.Value); string FeeShipping = pFeeShip.Value.ToString(); double GuestPaid = Convert.ToDouble(pGuestPaid.Value); double GuestChange = Convert.ToDouble(totalPrice) - GuestPaid; var couponID = hdfCouponID.Value.ToInt(0); var couponValue = hdfCouponValue.Value.ToDecimal(0); tbl_Order order = new tbl_Order() { AgentID = AgentID, OrderType = OrderType, AdditionFee = AdditionFee, DisCount = DisCount, CustomerID = CustomerID, CustomerName = CustomerName, CustomerPhone = CustomerPhone, CustomerAddress = CustomerAddress, CustomerEmail = CustomerEmail, TotalPrice = totalPrice, TotalPriceNotDiscount = totalPriceNotDiscount, PaymentStatus = PaymentStatus, ExcuteStatus = ExcuteStatus, IsHidden = IsHidden, WayIn = WayIn, CreatedDate = currentDate, CreatedBy = username, DiscountPerProduct = DiscountPerProduct, TotalDiscount = TotalDiscount, FeeShipping = FeeShipping, GuestPaid = GuestPaid, GuestChange = GuestChange, PaymentType = PaymentType, ShippingType = ShippingType, OrderNote = String.Empty, DateDone = DateTime.Now, OtherFeeName = String.Empty, OtherFeeValue = 0, PostalDeliveryType = 1, UserHelp = UserHelp, CouponID = couponID, CouponValue = couponValue }; var ret = OrderController.InsertOnSystem(order); int OrderID = ret.ID; #endregion #region Khởi tạo Other Fee if (!String.IsNullOrEmpty(hdfOtherFees.Value)) { JavaScriptSerializer serializer = new JavaScriptSerializer(); var fees = serializer.Deserialize <List <Fee> >(hdfOtherFees.Value); if (fees != null) { foreach (var fee in fees) { fee.OrderID = ret.ID; fee.CreatedBy = acc.ID; fee.CreatedDate = DateTime.Now; fee.ModifiedBy = acc.ID; fee.ModifiedDate = DateTime.Now; } FeeController.Update(ret.ID, fees); } } #endregion #region Cập nhật Coupon if (order.CouponID.HasValue && order.CouponID.Value > 0) { CouponController.updateStatusCouponCustomer(CustomerID, order.CouponID.Value, false); } #endregion if (OrderID > 0) { #region Khởi tạo chi tiết đơn hàng ProductPOS POS = JsonConvert.DeserializeObject <ProductPOS>(hdfListProduct.Value); List <tbl_OrderDetail> orderDetails = new List <tbl_OrderDetail>(); List <tbl_StockManager> stockManager = new List <tbl_StockManager>(); // Reverser POS.productPOS.Reverse(); foreach (ProductGetOut item in POS.productPOS) { orderDetails.Add( new tbl_OrderDetail() { AgentID = AgentID, OrderID = OrderID, SKU = item.SKU, ProductID = item.ProductType == 1 ? item.ProductID : 0, ProductVariableID = item.ProductType == 1 ? 0 : item.ProductVariableID, ProductVariableDescrition = item.ProductVariableSave, Quantity = item.QuantityInstock, Price = item.Giabanle, Status = 1, DiscountPrice = 0, ProductType = item.ProductType, CreatedDate = currentDate, CreatedBy = username, IsCount = true } ); int parentID = item.ProductID; var variable = ProductVariableController.GetByID(item.ProductVariableID); if (variable != null) { parentID = Convert.ToInt32(variable.ProductID); } stockManager.Add( new tbl_StockManager() { AgentID = AgentID, ProductID = item.ProductType == 1 ? item.ProductID : 0, ProductVariableID = item.ProductType == 1 ? 0 : item.ProductVariableID, Quantity = item.QuantityInstock, QuantityCurrent = 0, Type = 2, NoteID = "Xuất kho bán POS", OrderID = OrderID, Status = 3, SKU = item.SKU, CreatedDate = currentDate, CreatedBy = username, MoveProID = 0, ParentID = parentID } ); } OrderDetailController.Insert(orderDetails); #endregion // Cập nhật lại sô lượng và giá vố vào đơn hàng OrderController.updateQuantityCOGS(OrderID); // Cập nhật lại thông tin kho hàng StockManagerController.Insert(stockManager); #region Khởi tạo đơn hàng đổi trả string refund = hdSession.Value; if (refund != "1") { string[] RefundID = refund.Split('|'); var update = RefundGoodController.UpdateStatus(RefundID[0].ToInt(), username, 2, OrderID); var updateor = OrderController.UpdateRefund(OrderID, RefundID[0].ToInt(), username); } #endregion // Hoàn thành khởi tạo đơn hàng nên gán lại giá trị trang lúc ban đầu hdStatusPage.Value = "Create"; ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$(function () { HoldOn.close(); printInvoice(" + OrderID + ") });", true); } } } } catch (Exception) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "script", "$(function () { handleErrorSubmit(); });", true); } }