public HttpResponseMessage Post([FromBody] Employee employee)
        {
            _db = new ERPContext();
            if (ModelState.IsValid)
            {

                if (employee.Id == 0)
                    _db.Employees.Add(employee);

                else
                {
                    var emp = _db.Employees.Find(employee.Id);
                    emp.Name = employee.Name;
                    emp.Nickname = employee.Nickname;
                    emp.Gender = employee.Gender;
                    emp.DepartmentId = employee.DepartmentId;
                    emp.PositionId = employee.PositionId;
                    emp.Phone = employee.Phone;
                    emp.Email = employee.Email;
                    emp.Code = employee.Code;
                    emp.NationalId = employee.NationalId;
                }
                _db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK, employee);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }



        }
        public IHttpActionResult PostFormData()
        {

            var httpRequest = HttpContext.Current.Request;
            string path = "";
            string filePath = "";


            try
            {
                if (httpRequest.Files.Count > 0)
                {
                    foreach (string file in httpRequest.Files)
                    {
                        long dataSourceId = 1;
                        var postedFile = httpRequest.Files[file];
                        filePath = HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/" + postedFile.FileName);
                        //postedFile.SaveAs(filePath);
                        Stream stream = postedFile.InputStream;
                        byte[] fileData = null;
                        using (var binaryReader = new BinaryReader(postedFile.InputStream))
                        {
                            fileData = binaryReader.ReadBytes(postedFile.ContentLength);
                        }
                        var strrr = new MemoryStream(fileData);
                        var tempfile = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
                        // Get random file name without 
                        using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                        {
                            // Write content of your memory stream into file stream
                            strrr.WriteTo(fs);
                        }                      
                    }
                }


                string check = "ATM";
                string con =
                    (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 Xml; HDR = YES; IMEX = 1';");

          
                BL bl = new BL(con);

                DataTable dtw = bl.GetTable("select * from [Sheet1$]");
                _db = new ERPContext();
                var employees = _db.Employees.ToList();


               var payingType= dtw.Rows[1].ItemArray[1];
                bool pay = true;
                if (payingType.Equals("3-مرتب تحويلات بنكية"))
                {
                    check = "Bank";
                    pay = false;
                }
                foreach (DataRow row in dtw.Rows)
                {

                 //   Employee emp = (from c in employees where (c.Code == Convert.ToInt32(row[4])) select (c)).FirstOrDefault();
                    int code=0;
                    bool codeparse = int.TryParse(row[4].ToString(), out code);

                    Employee emp = null;
                    if (codeparse)
                    {  emp = _db.Employees.FirstOrDefault(x => x.Code == code);
                    }
                   
                    if (emp != null)

                    {
                        emp.Sallary = pay;
                        emp.Name = row[5].ToString();
                        emp.NationalId = row[0].ToString();

                    }
                    else
                    {
                        Employee empl = new Employee
                        {
                            NationalId = row[0].ToString(),
                            Name = row[5].ToString(),
                            Code = Convert.ToInt32(row[4]),
                            Sallary = pay,
                            Other = true
                        };



                        _db.Employees.Add(empl);
                    }
                  

                          
                }
               _db.SaveChanges();
                _db.Dispose();
                //if (File.Exists(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/" + check + ".xls")))
                //{
                //    File.Delete(HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/" + check + ".xls"));
                //}

                //File.Move(filePath, HttpContext.Current.Server.MapPath("~/Uploads/SourceFile/" + check + ".xls"));

                return Ok(new { msg = "success" });
            }
            catch (Exception ex)
            {

                return BadRequest(ex.Message);
            }

           
        }
        public IHttpActionResult DeleteEmployee(int entityId)
        {
            _db = new ERPContext();
            Employee employee = _db.Employees.Find(entityId);
            if (employee == null)
            {
                return NotFound();
            }

            _db.Employees.Remove(employee);
            _db.SaveChanges();
            //string con =
            //    (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\File\Bank.xls;Extended Properties='Excel 12.0 Xml; HDR = YES; IMEX = 1';");



            //BL bl = new BL(con);

            //DataTable dtw = bl.GetTable("select * from [Sheet1$]");
            //_db = new ERPContext();
            //foreach (DataRow row in dtw.Rows)
            //{

            //    Employee EMP = new Employee();
            //    EMP.NationalId = row[0].ToString();
            //    EMP.Name = row[5].ToString();
            //    EMP.Code = Convert.ToInt32( row[4]);
            //    _db.Employees.Add(EMP);
            //    _db.SaveChanges();
            //}

            return Ok(new {msg = "Delete Successfully"});
        }
        public IHttpActionResult Delete(int Id)
        {

            _db = new ERPContext();
            var daily = _db.Dailies.Find(Id);


            if (daily != null)
            {
                _db.Dailies.Remove(daily);
                _db.SaveChanges();

                var filePath =
                       HttpContext.Current.Server.MapPath("~/Uploads/DailyFiles/Daily-" + Id);


                if (Directory.Exists(filePath))
                {
                    try
                    {
                        Directory.Delete(filePath, true);
                    }
                    catch (IOException)
                    {
                        Thread.Sleep(0);
                        Directory.Delete(filePath, true);
                    }
                }
                return Ok(new { msg = "success" });
            }
            return BadRequest("Wrong Request");
        }
        public IHttpActionResult Post([FromBody] Daily model)
        {
            _db = new ERPContext();
         


            model.TotalAmount = 0;
           

            if (ModelState.IsValid)
            {

                if (model.Id == 0)
                {
                    model.Open = true;
                    model.CreatedDate = DateTime.UtcNow;
                    _db.Dailies.Add(model);
                    _db.SaveChanges();
                    var filePath =
                      HttpContext.Current.Server.MapPath("~/Uploads/DailyFiles/Daily-" + model.Id);
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }

                }
                else
                {
                    var daily = _db.Dailies.Find(model.Id);
                    if (daily != null)
                    {
                        daily.Open = model.Open;
                        daily.Name = model.Name;
                        daily.ClosedDate = model.ClosedDate;
                        daily.CheckGP = model.CheckGP;
                        daily.DailyDay = model.DailyDay;
                        daily.ExpensessTypeId = model.ExpensessTypeId;
                        _db.SaveChanges();

                    }
                }

   


            }
            return Ok(new { msg = "success" });
        }