Пример #1
0
 public ActionResult EditOrderSetting(ordersetting setting)
 {
     if (ModelState.IsValid)
     {
         db.Entry(setting).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("SettingIndex"));
     }
     return(View(setting));
 }
Пример #2
0
        public ActionResult CreateOrderSetting(ordersetting ordersetting)
        {
            if (ModelState.IsValid)
            {
                db.ordersettings.Add(ordersetting);
                db.SaveChanges();
                return(RedirectToAction("SettingIndex"));
            }

            return(View(ordersetting));
        }
Пример #3
0
        // GET: /setting/Edit/5
        public ActionResult EditOrderSetting(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ordersetting setting = db.ordersettings.Find(id);

            if (setting == null)
            {
                return(HttpNotFound());
            }
            return(View(setting));
        }
Пример #4
0
        public ActionResult Create(order order, int?checkBilldingShipping)
        {
            if (ModelState.IsValid)
            {
                if (Session["ShoppingCart"] == null)
                {
                    return(RedirectToAction("UpdateCart", "Home"));
                }
                else
                {
                    try
                    {
                        var oldPriceOrder = 0.0;
                        if (order.ocid > 0)
                        {
                            var _order = db.orders.Where(t => t.ocid == order.ocid).FirstOrDefault();
                            oldPriceOrder          = _order.paid_amount.HasValue ? _order.paid_amount.Value : 0;
                            db.Entry(_order).State = EntityState.Detached;
                            db.SaveChanges();
                        }
                        order.d_companyname = string.IsNullOrEmpty(order.d_companyname) ? order.companyname : order.d_companyname;
                        order.d_fname       = string.IsNullOrEmpty(order.d_fname) ? order.fname : order.d_fname;
                        order.d_lname       = string.IsNullOrEmpty(order.d_lname) ? order.lname : order.d_lname;
                        order.d_email       = string.IsNullOrEmpty(order.d_email) ? order.email : order.d_email;
                        order.d_phone       = string.IsNullOrEmpty(order.d_phone) ? order.phone : order.d_phone;
                        order.d_addr1       = string.IsNullOrEmpty(order.d_addr1) ? order.addr1 : order.d_addr1;
                        if (checkBilldingShipping.HasValue && checkBilldingShipping.Value == 1)
                        {
                            order.b_companyname = string.IsNullOrEmpty(order.d_companyname) ? order.companyname : order.d_companyname;
                            order.b_fname       = string.IsNullOrEmpty(order.d_fname) ? order.fname : order.d_fname;
                            order.b_lname       = string.IsNullOrEmpty(order.d_lname) ? order.lname : order.d_lname;
                            order.b_email       = string.IsNullOrEmpty(order.d_email) ? order.email : order.d_email;
                            order.b_phone       = string.IsNullOrEmpty(order.d_phone) ? order.phone : order.d_phone;
                            order.b_addr1       = string.IsNullOrEmpty(order.d_addr1) ? order.addr1 : order.d_addr1;
                        }
                        order.status = "2";
                        if (order.payoption == "PayPal")
                        {
                            order.paid_status = 2;
                        }
                        if (order.payoption == "COD")
                        {
                            order.paid_status = 1;
                        }
                        ShoppingCart Cart = new ShoppingCart();
                        Cart = (ShoppingCart)Session["ShoppingCart"];
                        order.paid_amount = Cart.CartTotal + Cart.taxTotal;
                        String randomKey = Guid.NewGuid().ToString();
                        if (order.ocid == 0)
                        {
                            order.paid_key = randomKey;
                            db.orders.Add(order);
                            db.SaveChanges();
                        }
                        else
                        {
                            db.Entry(order).State = EntityState.Modified;
                            db.SaveChanges();
                        }

                        Cart.paid_key = order.paid_key;
                        WebApplication1.Models.AllLoggedUserInfo userFullInfo = (WebApplication1.Models.AllLoggedUserInfo)Session["LoggedAccount"];
                        if (userFullInfo != null)
                        {
                            var user = db.users.Find(userFullInfo.user.Id);
                            user.paidorder       = (user.paidorder ?? 0) + (decimal)Cart.CartTotal + (decimal)(order.feeshipping ?? 0) - (decimal)oldPriceOrder;
                            db.Entry(user).State = EntityState.Modified;
                            db.SaveChanges();
                        }
                        double       cartAmount   = 0;
                        string       currencyName = "USD";
                        ordersetting ordersetting = db.ordersettings.Where(t => t.status == 1).FirstOrDefault();
                        if (ordersetting != null)
                        {
                            currencyName = ordersetting.name;
                        }

                        var itemList = new ItemList();
                        var items    = new List <Item>();

                        String paypalURL    = "";
                        var    paypayconfig = new PayPalConfiguration();
                        var    apiContext   = paypayconfig.GetAPIContext();

                        var _orderdetails = db.orderdetails.Where(t => t.ocid == order.ocid).ToList();
                        var listItems     = _orderdetails.Select(t => t.ocdetailcode);
                        var _items        = db.items.Where(t => listItems.Contains(t.ARTCODE)).ToList();
                        foreach (var item in _orderdetails)
                        {
                            if (item.stockId.HasValue && item.stockId.Value > 0)
                            {
                                var _item = _items.FirstOrDefault(t => t.ARTCODE == item.ocdetailcode);
                                if (_item != null)
                                {
                                    var _stock = db.stocks.Where(t => t.ARTNO == item.itemId && t.STOCKNO == item.stockId).FirstOrDefault();
                                    _stock.VOLUME         += item.ocdetailqty.Value;
                                    db.Entry(_stock).State = EntityState.Modified;
                                }
                            }
                        }
                        db.orderdetails.RemoveRange(_orderdetails);
                        var _listItems = Cart.cartItem.Select(t => t.Code);
                        var __items    = db.items.Where(t => _listItems.Contains(t.ARTCODE)).ToList();
                        foreach (var item in Cart.cartItem)
                        {
                            orderdetail od = new orderdetail();
                            od.ocid          = order.ocid;
                            od.ocdetailcode  = item.Code;
                            od.ocdetailname  = item.Name;
                            od.ocdetailprice = item.Price;
                            od.ocdetailqty   = item.Qty;
                            od.ocdetailgst   = item.Tax / item.Qty;
                            od.stockId       = item.StockId;
                            if (item.StockId > 0)
                            {
                                var _item = __items.FirstOrDefault(t => t.ARTCODE == od.ocdetailcode);
                                if (_item != null)
                                {
                                    var _stock = db.stocks.Where(t => t.ARTNO == _item.ARTNO && t.STOCKNO == item.StockId).FirstOrDefault();
                                    _stock.VOLUME         -= item.Qty;
                                    db.Entry(_stock).State = EntityState.Modified;
                                    od.itemId = _item.ARTNO;
                                }
                            }
                            db.orderdetails.Add(od);

                            var Item = new Item();
                            Item.name     = item.Code + " - " + item.Name;
                            Item.currency = currencyName;

                            Item.price    = (item.Price + (item.Tax / item.Qty)) + "";
                            Item.quantity = item.Qty + "";
                            items.Add(Item);
                        }
                        if (order.feeshipping.HasValue && order.feeshipping.Value > 0)
                        {
                            var Item = new Item();
                            Item.name     = "Fee Shipping";
                            Item.currency = currencyName;
                            Item.price    = order.feeshipping + "";
                            Item.quantity = "1";
                            items.Add(Item);
                        }
                        if (Cart.promotion != null)
                        {
                            if (Cart.promotion.TYPENO == 0)
                            {
                                var Item = new Item();
                                Item.name     = "Discount Promotion";
                                Item.currency = currencyName;
                                Item.price    = "-" + Cart.PromotionTotal + "";
                                Item.quantity = "1";
                                items.Add(Item);
                            }
                        }
                        cartAmount     = Cart.CartTotal + Cart.taxTotal + order.feeshipping ?? 0;
                        cartAmount     = Math.Round(cartAmount, 2);
                        itemList.items = items;
                        db.SaveChanges();
                        if (order.payoption == "PayPal")
                        {
                            var payer = new Payer()
                            {
                                payment_method = "paypal"
                            };
                            var redirUrls = new RedirectUrls()
                            {
                                cancel_url = UrlHelper.Root + "Checkout/PayPalCancel?" + UrlHelper.ToQueryString(new { paid_key = order.paid_key }),
                                return_url = UrlHelper.Root + "Checkout/PayPalSuccess?" + UrlHelper.ToQueryString(new { paid_key = order.paid_key })
                            };
                            var paypalAmount = new Amount()
                            {
                                currency = currencyName, total = cartAmount.ToString()
                            };

                            var transactionList = new List <PayPal.Api.Transaction>();
                            PayPal.Api.Transaction transaction = new PayPal.Api.Transaction();
                            transaction.amount    = paypalAmount;
                            transaction.item_list = itemList;
                            transactionList.Add(transaction);


                            var payment = new Payment()
                            {
                                intent        = "Sale",
                                payer         = payer,
                                transactions  = transactionList,
                                redirect_urls = redirUrls
                            };

                            try
                            {
                                var createdPayment = payment.Create(apiContext);
                                var links          = createdPayment.links.GetEnumerator();
                                while (links.MoveNext())
                                {
                                    var link = links.Current;
                                    if (link.rel.ToLower().Trim().Equals("approval_url"))
                                    {
                                        paypalURL = link.href;
                                    }
                                }
                                return(Redirect(paypalURL));
                            }
                            catch (PaymentsException ex)
                            {
                                paypalURL = "ERROR: " + ex.Response;
                            }
                        }
                        else
                        {
                            SendTemplateEmail(order.d_email, order.d_email, "", "Order Success #" + order.ocid + "" + DateTime.Now.Day, 1, Cart, order);
                            SendTemplateEmail(order.d_email, order.d_email, "", "Order Success #" + order.ocid + "" + DateTime.Now.Day, 3, Cart, order);
                            return(RedirectToAction("Thankyou"));
                        }
                    }
                    catch (DbEntityValidationException e)
                    {
                        foreach (var eve in e.EntityValidationErrors)
                        {
                            Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                              eve.Entry.Entity.GetType().Name, eve.Entry.State);
                            foreach (var ve in eve.ValidationErrors)
                            {
                                Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                                  ve.PropertyName, ve.ErrorMessage);
                            }
                        }
                    }
                }
            }
            return(RedirectToAction("Create"));
        }