//
        // GET: /Materials/Create
        public ActionResult Get(Page model)
        {
            using (AppDB db = new AppDB())
            {

                return Json(IEnumerableData.GetPageResponse<MaterialModel>(model, (from m in db.Materials
                                                                                   join s in db.Sellers on m.SellerId equals s.Id
                                                                                   join o in db.Orders on m.OrderId equals o.Id
                                                                                   join u in db.Employees on m.CreatedBy equals u.Id
                                                                                   where m.OrderId == model.id
                                                                                   select new MaterialModel() {
                                                                                    Id=m.Id,
                                                                                    SellerId=m.SellerId,
                                                                                    Seller=s.Name,
                                                                                    OrderId = m.OrderId,
                                                                                    Order=o.CodeNumber,
                                                                                    Amount = m.Amount,
                                                                                    Description = m.Description,
                                                                                    OrderDate = m.OrderDate,
                                                                                    DeliveryDate = m.DeliveryDate,
                                                                                    CreatedBy = m.CreatedBy,
                                                                                    CreatedByName=u.Name,
                                                                                    ReceivedDate = m.ReceivedDate
                                                                                   })), JsonRequestBehavior.AllowGet);
            }
        }
 public ActionResult Get(Page model)
 {
     ResponseJson response = new ResponseJson() { Data = new List<object>() };
     var data = new Pagger<dynamic>();
     using (AppDB db = new AppDB())
     {
         response = IEnumerableData.GetPageResponse<BundleModel>(model, (from b in db.Bundles
                                                                         join s in db.Styles on b.StyleId equals s.Id
                                                                         join st in db.Status on b.Status equals st.Id
                                                                         join o in db.Employees on b.OperatorId equals o.Id
                                                                         join l in db.Employees on b.LinkingBy equals l.Id
                                                                         where b.Status == 2
                                                                         select new BundleModel()
                                                                         {
                                                                             BarCode = b.BarCode,
                                                                             Style = s.Name,
                                                                             StyleId = b.StyleId,
                                                                             Id = b.Id,
                                                                             Quantity = b.Quantity,
                                                                             Status = st.Name,
                                                                             StatusId = st.Id,
                                                                             KnittingMachine = b.KnittingMachine,
                                                                             LinkingEndAt = b.LinkingEndAt,
                                                                             OperatorId = b.OperatorId,
                                                                             Operator = o.Name,
                                                                             LinkingBy = l.Name,
                                                                             LinkingById = l.Id
                                                                         }));
     }
     return Json(response, JsonRequestBehavior.AllowGet);
 }
 public ActionResult Get(Page model)
 {
     ResponseJson response = new ResponseJson() { Data = new List<object>() };
     var data = new Pagger<dynamic>();
     using (AppDB db = new AppDB())
     {
         response = IEnumerableData.GetPageResponse<StyleModel>(model, (from s in db.Styles
                                                                             join b in db.Buyers on s.BuyerId equals b.Id
                                                                             join st in db.Status on s.Status equals st.Id
                                                                             select new StyleModel() {
                                                                                 BarCode=s.BarCode,
                                                                                 Buyer=b.Name,
                                                                                 BuyerId=s.BuyerId,
                                                                                 Id=s.Id,
                                                                                 Name=s.Name,
                                                                                 Quantity=s.Quantity,
                                                                                 Status=st.Name,
                                                                                 StatusId=st.Id,
                                                                                 ShippingDate=s.ShippingDate,
                                                                                 Description=s.Description,
                                                                                 YarnType=s.YarnType,
                                                                                 Size=s.Size,
                                                                             }));
     }
     return Json(response, JsonRequestBehavior.AllowGet);
 }
        public ActionResult Details(Guid id)
        {
            ResponseJson response = new ResponseJson();
              using (AppDB db = new AppDB())
              {
            response.Data = (from o in db.Orders
                         join b in db.Buyers on o.BuyerId equals b.Id
                         where o.Status != 10
                         select new OrderModel()
                         {
                           Id = b.Id,
                           Buyer = b.Name,
                           BuyerId = o.BuyerId,
                           CodeNumber = o.CodeNumber,
                           Color = o.Color,
                           DeliveryDate = o.DeliveryDate,
                           Description = o.Description,
                           Size = o.Size,
                           Style = o.Style,
                           Quantity = o.TotalQuantity,
                           Completed = o.TotalCompleted,
                           ProductionStartAT = o.ProductionStartAT,
                           CurrentDate = DateTime.Now
                         }).ToList();

              }
              return Json(response, JsonRequestBehavior.AllowGet);
        }
 public ActionResult Seller()
 {
     ResponseJson response = new ResponseJson();
     using (AppDB db = new AppDB())
     {
         response.Data = db.Sellers.OrderBy(c => c.Name).Select(s => new { text = s.Name, value = s.Id }).ToList();
     }
     return Json(response, JsonRequestBehavior.AllowGet);
 }
 public ActionResult Designations()
 {
     ResponseJson response = new ResponseJson();
       using (AppDB db = new AppDB())
       {
     var currentRole = db.Roles.Where(r => CurrentUser.Roles.Contains(r.Code)).Min(r => r.Priority);
     response.Data = db.Roles.Where(r => r.Priority > currentRole).OrderBy(c => c.Priority).Select(s => new { text = s.Name, value = s.Code }).ToList();
       }
       return Json(response, JsonRequestBehavior.AllowGet);
 }
        public ActionResult Create(OrderModel model)
        {
            ResponseJson response = new ResponseJson();
              try
              {
            if (!ModelState.IsValid)
            {
              return Json(Globals.SetValidationError(ModelState));
            }
            //response = Validation(response, model);
            //if (response.IsError)
            //{
            //    return Json(response);
            //}
            //ObjectParameter ErrorCode = new ObjectParameter("ErrorCode", 0);
            UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Order Created", Type = 1, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
            string login = "";
            using (AppDB db = new AppDB())
            {

              DAL.Order order = new DAL.Order()
              {
            BuyerId = model.BuyerId,
            CodeNumber = "",
            Color = model.Color,
            CreatedBy = CurrentUser.Id,
            DeliveryDate = model.DeliveryDate,
            Description = model.Description,
            Id = Guid.NewGuid(),
            OrderDate = DateTime.Now,
            Size = model.Size,
            Status = 0,
            Style = model.Style,
            TotalQuantity = model.Quantity,
            UpdateInfo = new JavaScriptSerializer().Serialize(updatedInfo)
              };
              db.Orders.Add(order);
              db.SaveChanges();
              //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");

            }

              }
              catch (Exception ex)
              {
            response.IsError = true;
            response.Id = -6;
              }
              return Json(response);
        }
        public ActionResult Create(SellerModel model)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }
                //response = Validation(response, model);
                //if (response.IsError)
                //{
                //    return Json(response);
                //}
                //ObjectParameter ErrorCode = new ObjectParameter("ErrorCode", 0);
                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Seller Created", Type = 1, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };

                using (AppDB db = new AppDB())
                {

                    DAL.Seller seller = new DAL.Seller()
                    {
                        CreatedBy = CurrentUser.Id,
                        Description = model.Description,
                        Id = Guid.NewGuid(),
                        Name=model.Name,
                        CreatedAt = DateTime.Now,
                        Phone = model.Phone,
                        Email = model.Email,
                        Address = model.Address,
                        UpdateInfo = new JavaScriptSerializer().Serialize(updatedInfo)
                    };
                    db.Sellers.Add(seller);
                    db.SaveChanges();
                    response.Data = new { Id = seller.Id, Name = seller.Name };
                    //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");

                }

            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }
        public ActionResult Save(string employeeBarCode, string bundleBarCode)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }

                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Linking Started", Type = 4, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
                using (AppDB db = new AppDB())
                {
                    var bundle = db.Bundles.Where(s => s.BarCode == bundleBarCode).SingleOrDefault();
                    var employee = db.Employees.Where(s => s.BarCode == employeeBarCode).SingleOrDefault();
                    if (bundle != null && bundle.Status !=2 && employee != null)
                    {
                        bundle.LinkingStartAt = DateTime.Now;
                        bundle.LinkingBy = employee.Id;
                        bundle.Status = 2;
                        bundle.UpdateInfo += "," + new JavaScriptSerializer().Serialize(updatedInfo);
                        db.SaveChanges();
                    }
                    else if (bundle == null)
                    {
                        response.IsError = true;
                        response.Id = -11;
                    }
                    else if (bundle.Status == 2)
                    {
                        response.IsError = true;
                        response.Id = -12;
                    }
                    else
                    {
                        response.IsError = true;
                        response.Id = -13;
                    }
                }
            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }
        public ActionResult Create(StyleModel model)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }
                //response = Validation(response, model);
                //if (response.IsError)
                //{
                //    return Json(response);
                //}
                //ObjectParameter ErrorCode = new ObjectParameter("ErrorCode", 0);
                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Style Created", Type = 1, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
                using (AppDB db = new AppDB())
                {
                    DAL.Style style = new DAL.Style()
                    {
                        Id=Guid.NewGuid(),
                        BuyerId = model.BuyerId,
                        BarCode = Globals.GetBarCode(),
                        Name = model.Name,
                        CreatedBy = CurrentUser.Id,
                        YarnType = model.YarnType,
                        Size = model.Size,
                        Description = model.Description,
                        ShippingDate = model.ShippingDate,
                        Status = 0,
                        CreatedAt = DateTime.Now,
                        Quantity = model.Quantity,
                        UpdateInfo = new JavaScriptSerializer().Serialize(updatedInfo)
                    };
                    db.Styles.Add(style);
                    db.SaveChanges();
                    //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");
                }

            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }
        public ActionResult Save(string bundleBarCode)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }

                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Linking Ended", Type = 5, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
                using (AppDB db = new AppDB())
                {
                    var bundle = db.Bundles.Where(s => s.BarCode == bundleBarCode).SingleOrDefault();
                    if (bundle != null && bundle.Status==2)
                    {
                        bundle.LinkingEndAt = DateTime.Now;
                        //bundle.LinkingEndAt = employee.Id;
                        bundle.UpdateInfo += "," + new JavaScriptSerializer().Serialize(updatedInfo);
                        bundle.Status = 3;
                        db.SaveChanges();
                    } else if (bundle == null)
                    {
                        response.IsError = true;
                        response.Id = -11;
                    }
                    else if (bundle.Status != 2)
                    {
                        response.IsError = true;
                        response.Id = -12;
                    } else
                    {
                        response.IsError = true;
                        response.Id = -13;
                    }
                    //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");
                }

            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }
        public ActionResult Create(EmployeeModel model)
        {
            ResponseJson response = new ResponseJson();
              try
              {
            if (!ModelState.IsValid)
            {
              return Json(Globals.SetValidationError(ModelState));
            }

            UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Employee Created", Type = 1, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
            using (AppDB db = new AppDB())
            {
              DAL.Employee bundle = new DAL.Employee()
              {
            Id = Guid.NewGuid(),
            Name = model.Name,
            BarCode = Globals.GetBarCode(),
            CreatedBy = CurrentUser.Id,
            UserName = model.UserName,
            Password = Globals.Encrypt(model.Password),
            Phone = model.Phone,
            Email = model.Email,
            IsActive = true,
            Address = model.Address,
            Role = model.Role,
            CreatedAt = DateTime.Now,
            UpdateInfo = new JavaScriptSerializer().Serialize(updatedInfo)
              };
              db.Employees.Add(bundle);
              db.SaveChanges();
              //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");
            }

              }
              catch (Exception ex)
              {
            response.IsError = true;
            response.Id = -6;
              }
              return Json(response);
        }
        public ActionResult Create(MaterialModel model)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }

                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Material Created", Type = 1, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };

                using (AppDB db = new AppDB())
                {

                    DAL.Material seller = new DAL.Material()
                    {
                        SellerId=model.SellerId,
                        OrderId=model.OrderId,
                        Amount=model.Amount,
                        OrderDate=DateTime.Now,
                        DeliveryDate=model.DeliveryDate,
                        CreatedBy = CurrentUser.Id,
                        Description = model.Description,
                        Id = Guid.NewGuid(),
                        CreatedAt = DateTime.Now,
                        UpdateInfo = new JavaScriptSerializer().Serialize(updatedInfo)
                    };
                    db.Materials.Add(seller);
                    db.SaveChanges();
                }

            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }
 public ActionResult Details(Guid id)
 {
     ResponseJson response = new ResponseJson();
       using (AppDB db = new AppDB())
       {
     response.Data = (from b in db.Employees
                  join e in db.Employees on b.CreatedBy equals e.Id
                  join r in db.Roles on b.Role equals r.Code
                  where b.Id == id
                  select new
                  {
                    Name = b.Name,
                    Email = b.Email,
                    Phone = b.Phone,
                    CreatedBy = e.Name,
                    CreatedAt = b.CreatedAt,
                    IsActive = b.IsActive,
                    Role = r.Name
                  }).SingleOrDefault();
       }
       return Json(response, JsonRequestBehavior.AllowGet);
 }
 public ActionResult Details(Guid id)
 {
     ResponseJson response = new ResponseJson();
     using(AppDB db=new AppDB())
     {
         response.Data = (from b in db.Buyers
                          join e in db.Employees on b.CreatedBy equals e.Id
                          where b.Id == id
                          select new
                          {
                              Name = b.Name,
                              Email = b.Email,
                              Phone = b.Phone,
                              CreatedBy=e.Name,
                              CreatedById=b.CreatedBy,
                              Description=b.Description,
                              UpdateInfo=b.UpdateInfo,
                              CreatedAt = b.CreatedAt
                          }).SingleOrDefault();
     }
     return Json(response, JsonRequestBehavior.AllowGet);
 }
        public ActionResult Create(BundleModel model)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }

                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Bundle Created", Type = 1, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
                using (AppDB db = new AppDB())
                {
                    DAL.Bundle bundle = new DAL.Bundle()
                    {
                        Id = Guid.NewGuid(),
                        StyleId = model.StyleId,
                        BarCode = Globals.GetBarCode(),
                        CreatedBy = CurrentUser.Id,
                        KnittingMachine = model.KnittingMachine,
                        OperatorId = model.OperatorId,
                        Status = 0,
                        CreatedAt = DateTime.Now,
                        Quantity = model.Quantity,
                        UpdateInfo = new JavaScriptSerializer().Serialize(updatedInfo)
                    };
                    db.Bundles.Add(bundle);
                    db.SaveChanges();
                    //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");
                }

            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }
        public ActionResult Get(Page model)
        {
            ResponseJson response = new ResponseJson() { Data = new List<object>() };
              using (AppDB db = new AppDB())
              {
            response = IEnumerableData.GetPageResponse<OrderModel>(model, (from o in db.Orders
                                                                       join b in db.Buyers on o.BuyerId equals b.Id
                                                                       where o.Status != 10
                                                                       select new OrderModel()
                                                                       {
                                                                         Id = o.Id,
                                                                         Buyer = b.Name,
                                                                         BuyerId = o.BuyerId,
                                                                         CodeNumber = o.CodeNumber,
                                                                         Color = o.Color,
                                                                         DeliveryDate = o.DeliveryDate,
                                                                         Description = o.Description,
                                                                         Size = o.Size,
                                                                         Style = o.Style,
                                                                         Quantity = o.TotalQuantity,
                                                                         Completed = o.TotalCompleted,
                                                                         ProductionStartAT = o.ProductionStartAT,
                                                                         OrderDate = o.OrderDate,
                                                                         CurrentDate = DateTime.Now
                                                                       }).ToList());

              }
              return Json(response, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Get(Page model)
        {
            ResponseJson response = new ResponseJson() { Data = new List<object>() };

              using (AppDB db = new AppDB())
              {
            var currentRole = db.Roles.Where(r => CurrentUser.Roles.Contains(r.Code)).Min(r => r.Priority);
            response = IEnumerableData.GetPageResponse<EmployeeModel>(model, (from b in db.Employees
                                                                          join
                                                                          r in db.Roles on b.Role equals r.Code
                                                                          where r.Priority > currentRole
                                                                          select new EmployeeModel()
                                                                        {
                                                                          Id = b.Id,
                                                                          BarCode = b.BarCode,
                                                                          Name = b.Name,
                                                                          Phone = b.Phone,
                                                                          Email = b.Email,
                                                                          Address = b.Address,
                                                                          Role = r.Name,
                                                                          CreatedAt = b.CreatedAt,
                                                                          IsActive = b.IsActive
                                                                        }));
              }
              return Json(response, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SaveChange(EmployeeModel model)
        {
            ResponseJson response = new ResponseJson();
              try
              {
            if (!ModelState.IsValid)
            {
              return Json(Globals.SetValidationError(ModelState));
            }

            UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Update Employee", Type = 2, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
            using (AppDB db = new AppDB())
            {
              var bundle = db.Employees.Where(s => s.Id == model.Id).SingleOrDefault();

              if (bundle != null)
              {
            bundle.Name = model.Name;
            bundle.Phone = model.Phone;
            bundle.Email = model.Email;
            bundle.Address = model.Address;
            bundle.Role = model.Role;
            bundle.UpdateInfo += "," + new JavaScriptSerializer().Serialize(updatedInfo);
            db.SaveChanges();
              }
              else
              {
            response.IsError = true;
            response.Id = -3;
              }
              //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");
            }

              }
              catch (Exception ex)
              {
            response.IsError = true;
            response.Id = -6;
              }
              return Json(response);
        }
 public ActionResult Operators()
 {
     ResponseJson response = new ResponseJson();
     using (AppDB db = new AppDB())
     {
         response.Data = db.Employees.Where(e=>e.Role=="OPT").OrderBy(c => c.Name).Select(s => new { text = s.Name, value = s.Id }).ToList();
     }
     return Json(response, JsonRequestBehavior.AllowGet);
 }
        public ActionResult Get(Page model)
        {
            ResponseJson response = new ResponseJson() { Data = new List<object>() };

            using (AppDB db = new AppDB())
            {
                response = IEnumerableData.GetPageResponse<BuyerModel>(model, (from b in db.Buyers
                                                                               select new BuyerModel()
                                                                                  {
                                                                                      Id = b.Id,
                                                                                      Name = b.Name,
                                                                                      Phone = b.Phone,
                                                                                      Email = b.Email,
                                                                                      Address = b.Address,
                                                                                      CreatedAt = b.CreatedAt
                                                                                  }));
            }
            return Json(response, JsonRequestBehavior.AllowGet);
        }
        public ActionResult SaveChange(StyleModel model)
        {
            ResponseJson response = new ResponseJson();
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(Globals.SetValidationError(ModelState));
                }

                UpdatedInfoModel updatedInfo = new UpdatedInfoModel() { Comment = "Update Style", Type = 2, UpdatedTime = DateTime.Now, UserId = CurrentUser.Id };
                using (AppDB db = new AppDB())
                {
                    var style = db.Styles.Where(s => s.Id == model.Id).SingleOrDefault();

                    if (style != null)
                    {
                        style.BuyerId = model.BuyerId;
                        style.Name = model.Name;
                        style.YarnType = model.YarnType;
                        style.Size = model.Size;
                        style.Description = model.Description;
                        style.ShippingDate = model.ShippingDate;
                        style.Quantity = model.Quantity;
                        style.UpdateInfo += "," + new JavaScriptSerializer().Serialize(updatedInfo);

                        //db.Styles.Add(style);
                        db.SaveChanges();
                    }
                    else
                    {
                        response.IsError = true;
                        response.Id = -3;
                    }
                    //EmailSender.SendToCHWUser(model.Email, login, login, model.Surname, "Sector Executive Officer");
                }

            }
            catch (Exception ex)
            {
                response.IsError = true;
                response.Id = -6;
            }
            return Json(response);
        }