Ejemplo n.º 1
0
        public bool CreateOrUpdate(CustomerModels model, ref string Id, ref string msg)
        {
            NSLog.Logger.Info("CustomersCreateOrUpdate", model);
            var Result = true;

            using (var cxt = new CMS_Context())
            {
                var _isExits = cxt.CMS_Customer.Where(x => x.Email.Equals(model.Email) && x.IsActive.HasValue && x.Status != (byte)Commons.EStatus.Deleted && x.ID != model.ID).FirstOrDefault();
                try
                {
                    if (string.IsNullOrEmpty(model.ID)) /* insert */
                    {
                        if (_isExits == null)
                        {
                            Id = Guid.NewGuid().ToString();
                            var e = new CMS_Customer
                            {
                                ID          = Id,
                                FbID        = model.FbID,
                                FirstName   = model.FirstName,
                                LastName    = model.LastName,
                                IsActive    = model.IsActive,
                                Email       = model.Email,
                                Phone       = model.Phone,
                                Password    = model.Password,
                                Gender      = model.Gender,
                                Marital     = model.MaritalStatus,
                                JoinedDate  = DateTime.Now,
                                BirthDate   = model.BirthDate,
                                HomeStreet  = model.Address,
                                HomeCity    = model.City,
                                HomeCountry = model.Country,
                                HomeZipCode = model.Postcode,
                                ImageUrl    = model.ImageURL,
                                /* other info */


                                Status       = (byte)Commons.EStatus.Actived,
                                CreatedDate  = DateTime.Now,
                                CreatedUser  = model.CreatedBy,
                                ModifiedUser = model.CreatedBy,
                                LastModified = DateTime.Now,
                                Anniversary  = Commons.MinDate,
                                ValidTo      = Commons.MinDate,
                            };
                            cxt.CMS_Customer.Add(e);
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(model.FbID))
                            {
                                if (string.IsNullOrEmpty(_isExits.FbID)) /* update fb ID */
                                {
                                    Id            = _isExits.ID;
                                    _isExits.FbID = model.FbID;
                                }
                                else
                                {
                                    msg    = "Địa chỉ email đã tồn tại";
                                    Result = false;
                                }
                            }
                            else if (!string.IsNullOrEmpty(model.GoogleID))
                            {
                                if (string.IsNullOrEmpty(_isExits.GoogleID)) /* update google id */
                                {
                                    Id = _isExits.ID;
                                    _isExits.GoogleID = model.GoogleID;
                                }
                                else
                                {
                                    msg    = "Địa chỉ email đã tồn tại";
                                    Result = false;
                                }
                            }
                            else
                            {
                                msg    = "Địa chỉ email đã tồn tại";
                                Result = false;
                            }
                        }
                    }
                    else /* updated */
                    {
                        var e = cxt.CMS_Customer.Find(model.ID);
                        if (e != null)
                        {
                            if (e.Email.Equals(model.Email) || _isExits == null)
                            {
                                e.FirstName   = model.FirstName;
                                e.LastName    = model.LastName;
                                e.IsActive    = model.IsActive;
                                e.Email       = model.Email;
                                e.Phone       = model.Phone;
                                e.Password    = model.Password;
                                e.Gender      = model.Gender;
                                e.Marital     = model.MaritalStatus;
                                e.BirthDate   = model.BirthDate;
                                e.HomeStreet  = model.Address;
                                e.HomeCity    = model.City;
                                e.ImageUrl    = model.ImageURL;
                                e.HomeCountry = model.Country;
                                e.HomeZipCode = model.Postcode;

                                /* other info */

                                e.ModifiedUser = model.CreatedBy;
                                e.LastModified = DateTime.Now;
                            }
                            else
                            {
                                msg    = "Địa chỉ email đã tồn tại";
                                Result = false;
                            }
                        }
                        else
                        {
                            Result = false;
                            msg    = "Unable to find Discount.";
                        }
                    }
                    cxt.SaveChanges();
                    NSLog.Logger.Info("ResponseCustomersCreateOrUpdate", new { Result, msg });
                }
                catch (Exception ex)
                {
                    Result = false;
                    msg    = "System Error.";
                    NSLog.Logger.Error("ErrorCustomersCreateOrUpdate", ex);
                }
            }
            return(Result);
        }
Ejemplo n.º 2
0
        public bool CreateOrder(CMS_CheckOutModels model, ref string OrderId)
        {
            NSLog.Logger.Info("CreateOrder_Request:", model);
            var ret = true;

            using (var db = new CMS_Context())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    m_Semaphore.WaitOne();
                    try
                    {
                        var active = (byte)Commons.EStatus.Actived;
                        if (string.IsNullOrEmpty(model.Customer.Id) && !string.IsNullOrEmpty(model.Customer.LastName))
                        {
                            // create new customer
                            model.Customer.Id = Guid.NewGuid().ToString();
                            var eCus = new CMS_Customer
                            {
                                ID            = model.Customer.Id,
                                FirstName     = model.Customer.FirstName,
                                LastName      = model.Customer.LastName,
                                Email         = model.Customer.Email,
                                Phone         = model.Customer.Phone,
                                CreatedDate   = DateTime.Now,
                                LastModified  = DateTime.Now,
                                CustomerType  = (int)CMS_Common.Commons.ECustomerType.Anonymous,
                                HomeCountry   = model.Customer.Country,
                                OfficeZipCode = model.Customer.PostCode,
                                Status        = active,
                                Anniversary   = Commons.MinDate,
                                ValidTo       = Commons.MinDate,
                                HomeStreet    = model.Customer.Address,
                                OfficeStreet  = model.Customer.Address,
                            };
                            db.CMS_Customer.Add(eCus);
                        }
                        // create order
                        OrderId = Guid.NewGuid().ToString();
                        var eOrder = new CMS_Order
                        {
                            ID      = OrderId,
                            StoreID = model.StoreID,
                            //OrderNo = CommonHelper.RandomNumberOrder(),
                            OrderNo            = CommonHelper.GenerateOrderNo(model.StoreID, active, model.OrderType),
                            ReceiptNo          = model.IsTemp ? "" : CommonHelper.GenerateReceiptNo(model.StoreID, active, model.OrderType),
                            ReceiptCreatedDate = model.IsTemp ? Commons.MinDate : DateTime.Now,
                            CustomerID         = model.Customer.Id,
                            TotalBill          = model.TotalPrice,
                            SubTotal           = model.SubTotalPrice,
                            TotalDiscount      = model.TotalDiscount,
                            Cashier            = model.IsTemp ? "" : model.CreatedUser,
                            CreatedDate        = DateTime.Now,
                            LastModified       = DateTime.Now,
                            CreatedUser        = string.IsNullOrEmpty(model.CreatedUser) ? model.Customer.Id : model.CreatedUser,
                            ModifiedUser       = string.IsNullOrEmpty(model.ModifiedUser) ? model.Customer.Id : model.ModifiedUser,
                            Status             = active,
                            IsTemp             = model.IsTemp,
                            OrderType          = model.OrderType,
                        };
                        db.CMS_Order.Add(eOrder);
                        // create order detail
                        if (model.ListItem != null && model.ListItem.Any())
                        {
                            var lstOrderDetail = new List <CMS_OrderDetail>();
                            foreach (var item in model.ListItem)
                            {
                                lstOrderDetail.Add(new CMS_OrderDetail
                                {
                                    ID           = Guid.NewGuid().ToString(),
                                    OrderID      = OrderId,
                                    ProductID    = item.ProductID,
                                    Remark       = string.IsNullOrEmpty(item.ProductID) ? item.ProductName : "",
                                    Price        = item.Price,
                                    Quantity     = (decimal)item.Quantity,
                                    Description  = string.IsNullOrEmpty(model.Customer.Description) ? item.Description : model.Customer.Description,
                                    CreatedDate  = DateTime.Now,
                                    CreatedUser  = string.IsNullOrEmpty(model.CreatedUser) ? model.Customer.Id : model.CreatedUser,
                                    ModifiedUser = string.IsNullOrEmpty(model.ModifiedUser) ? model.Customer.Id : model.ModifiedUser,
                                    //CreatedUser = model.IsTemp ? model.Customer.Id : (string.IsNullOrEmpty(item.EmployeeID) ? model.CreatedUser : item.EmployeeID),
                                    //ModifiedUser = model.IsTemp ? model.Customer.Id : (string.IsNullOrEmpty(item.EmployeeID) ? model.ModifiedUser : item.EmployeeID),
                                    EmployeeID     = item.EmployeeID,
                                    LastModified   = DateTime.Now,
                                    DiscountID     = item.DiscountID,
                                    DiscountValue  = item.DiscountValue,
                                    DiscountType   = item.DiscountType,
                                    DiscountAmount = item.DiscountAmount,
                                    Status         = active,
                                });
                            }
                            db.CMS_OrderDetail.AddRange(lstOrderDetail);

                            /* update quantity of product */
                            var listProductID = model.ListItem.Select(o => o.ProductID).ToList();
                            var listProductDB = db.CMS_Products.Where(o => listProductID.Contains(o.ID) && o.Status == (byte)Commons.EStatus.Actived && o.TypeCode == (byte)Commons.EProductType.Product).ToList();
                            foreach (var product in listProductDB)
                            {
                                var sign = -1;
                                if (model.OrderType == (byte)Commons.EOrderType.Expense)
                                {
                                    sign = +1;
                                }

                                product.Quantity = product.Quantity + ((sign) * (decimal)(model.ListItem.Where(o => o.ProductID == product.ID).Select(o => o.Quantity).FirstOrDefault()));

                                //if (product.Quantity < 0)
                                //{
                                //    ret = false;
                                //}
                            }
                        }

                        if (ret == true)
                        {
                            db.SaveChanges();
                            trans.Commit();
                        }
                    }
                    catch (Exception ex)
                    {
                        NSLog.Logger.Error("CreateOrder_Error:", ex);
                        trans.Rollback();
                        ret = false;
                    }
                    finally
                    {
                        m_Semaphore.Release();
                        db.Dispose();
                    }
                }
            }
            return(ret);
        }