Example #1
0
        public string Upload(IEnumerable <HttpPostedFileBase> fileRute)
        {
            ResponeModel response = new ResponeModel();

            if (fileRute != null)
            {
                foreach (var file in fileRute)
                {
                    try
                    {
                        using (var package = new ExcelPackage(file.InputStream))
                        {
                            var currentSheet = package.Workbook.Worksheets;
                            var workSheet    = currentSheet.First();
                            var noOfCol      = workSheet.Dimension.End.Column;
                            var noOfRow      = workSheet.Dimension.End.Row;

                            for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                            {
                                if (workSheet.Cells[rowIterator, 1].Value != null && workSheet.Cells[rowIterator, 2].Value != null &&
                                    workSheet.Cells[rowIterator, 3].Value != null && workSheet.Cells[rowIterator, 4].Value != null &&
                                    workSheet.Cells[rowIterator, 5].Value != null && workSheet.Cells[rowIterator, 7].Value != null)
                                {
                                    int id = 0;
                                    int resId;

                                    if (workSheet.Cells[rowIterator, 14].Value != null)
                                    {
                                        if (int.TryParse(workSheet.Cells[rowIterator, 14].Value.ToString(), out resId))
                                        {
                                            id = resId;
                                        }
                                    }

                                    Context.Rute db = new Context.Rute();
                                    try
                                    {
                                        if (id != 0)
                                        {
                                            db = RepoRute.FindByPK(id);
                                            db.RuteCheckPoint.Clear();
                                        }

                                        //parent
                                        db.Urutan       = RepoRute.getUrutan() + 1;
                                        db.Kode         = RepoRute.generateCode(db.Urutan);
                                        db.Nama         = workSheet.Cells[rowIterator, 1].Value.ToString();
                                        db.IdAsal       = RepoLocation.FindByCode(workSheet.Cells[rowIterator, 2].Value.ToString()).Id;
                                        db.IdAreaAsal   = RepoArea.FindByCode(workSheet.Cells[rowIterator, 3].Value.ToString()).Id;
                                        db.IdTujuan     = RepoLocation.FindByCode(workSheet.Cells[rowIterator, 4].Value.ToString()).Id;
                                        db.IdAreaTujuan = RepoArea.FindByCode(workSheet.Cells[rowIterator, 5].Value.ToString()).Id;
                                        if (workSheet.Cells[rowIterator, 6].Value != null && workSheet.Cells[rowIterator, 6].Value.ToString() != "")
                                        {
                                            db.IdMultiDrop = RepoMultidrop.FindByTujuan(workSheet.Cells[rowIterator, 6].Value.ToString()).Id;
                                        }
                                        db.WaktuKerja = int.Parse(workSheet.Cells[rowIterator, 7].Value.ToString());
                                        if (workSheet.Cells[rowIterator, 8].Value != null)
                                        {
                                            db.Toleransi = int.Parse(workSheet.Cells[rowIterator, 8].Value.ToString());
                                        }
                                        db.IsAreaPulang = workSheet.Cells[rowIterator, 9].Value == null ? false : bool.Parse(workSheet.Cells[rowIterator, 9].Value.ToString());
                                        db.IsKotaKota   = workSheet.Cells[rowIterator, 10].Value == null ? false : bool.Parse(workSheet.Cells[rowIterator, 10].Value.ToString());

                                        // child;
                                        int idx = 0;
                                        for (idx = rowIterator; idx <= noOfRow; idx++)
                                        {
                                            if (workSheet.Cells[idx, 1].Value == null || idx == rowIterator)
                                            {
                                                if (workSheet.Cells[idx, 10].Value != null && workSheet.Cells[idx, 11].Value != null &&
                                                    workSheet.Cells[idx, 12].Value != null && workSheet.Cells[idx, 13].Value != null)
                                                {
                                                    Context.RuteCheckPoint item = new Context.RuteCheckPoint();
                                                    item.longitude = workSheet.Cells[idx, 10].Value.ToString();
                                                    item.langitude = workSheet.Cells[idx, 11].Value.ToString();
                                                    item.radius    = int.Parse(workSheet.Cells[idx, 12].Value.ToString());
                                                    item.toleransi = int.Parse(workSheet.Cells[idx, 13].Value.ToString());
                                                    db.RuteCheckPoint.Add(item);
                                                }
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }

                                        if (idx != 0)
                                        {
                                            rowIterator = idx - 1;
                                        }

                                        RepoRute.save(db);
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                        }
                        response.Success = true;
                    }
                    catch (Exception e)
                    {
                        response.Success = false;
                        response.Message = e.Message.ToString();
                    }
                }
            }
            return(new JavaScriptSerializer().Serialize(new { Response = response }));
        }
Example #2
0
        public string Upload(IEnumerable <HttpPostedFileBase> filesMultidrop)
        {
            ResponeModel response = new ResponeModel();

            if (filesMultidrop != null)
            {
                foreach (var file in filesMultidrop)
                {
                    try
                    {
                        using (var package = new ExcelPackage(file.InputStream))
                        {
                            var  currentSheet = package.Workbook.Worksheets;
                            var  workSheet    = currentSheet.First();
                            var  noOfCol      = workSheet.Dimension.End.Column;
                            var  noOfRow      = workSheet.Dimension.End.Row;
                            bool isSaved      = true;

                            for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)
                            {
                                if (workSheet.Cells[rowIterator, 1].Value != null && workSheet.Cells[rowIterator, 2].Value != null &&
                                    workSheet.Cells[rowIterator, 3].Value != null && workSheet.Cells[rowIterator, 4].Value != null)
                                {
                                    int id = 0;
                                    int resId;

                                    if (workSheet.Cells[rowIterator, 5].Value != null)
                                    {
                                        if (int.TryParse(workSheet.Cells[rowIterator, 5].Value.ToString(), out resId))
                                        {
                                            id = resId;
                                        }
                                    }

                                    List <String>     listLocation = new List <string>();
                                    Context.Multidrop dbMultiDrop  = new Context.Multidrop();
                                    try
                                    {
                                        if (id != 0)
                                        {
                                            dbMultiDrop = RepoMultidrop.FindByPK(id);
                                        }

                                        dbMultiDrop.JumlahKota  = int.Parse(workSheet.Cells[rowIterator, 1].Value.ToString());
                                        dbMultiDrop.WaktuTempuh = int.Parse(workSheet.Cells[rowIterator, 2].Value.ToString());
                                        dbMultiDrop.WaktuKerja  = int.Parse(workSheet.Cells[rowIterator, 3].Value.ToString());

                                        Context.Location revLocation = RepoLocation.FindByCode(workSheet.Cells[rowIterator, 4].Value.ToString());
                                        if (revLocation != null)
                                        {
                                            listLocation.Add(revLocation.Nama);
                                        }

                                        for (int x = rowIterator + 1; x <= noOfRow; x++)
                                        {
                                            if (workSheet.Cells[x, 4].Value != null && workSheet.Cells[x, 1].Value == null &&
                                                workSheet.Cells[x, 2].Value == null && workSheet.Cells[x, 3].Value == null)
                                            {
                                                Context.Location loc = RepoLocation.FindByCode(workSheet.Cells[x, 4].Value.ToString());
                                                if (loc != null)
                                                {
                                                    listLocation.Add(loc.Nama);
                                                }
                                                else
                                                {
                                                    isSaved = false;
                                                    break;
                                                }
                                            }
                                            else
                                            {
                                                //isSaved = false;
                                                break;
                                            }
                                        }
                                        if (listLocation.Count > 1)
                                        {
                                            dbMultiDrop.tujuan = string.Join(" - ", listLocation);
                                        }

                                        if (RepoMultidrop.FindByTujuan(dbMultiDrop.tujuan.ToString()) != null)
                                        {
                                            continue;
                                        }

                                        if (isSaved)
                                        {
                                            RepoMultidrop.save(dbMultiDrop);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                    }
                                }
                            }
                        }
                        response.Success = true;
                    }
                    catch (Exception e)
                    {
                        response.Success = false;
                        response.Message = e.Message.ToString();
                    }
                }
            }
            return(new JavaScriptSerializer().Serialize(new { Response = response }));
        }