Ejemplo n.º 1
0
        public async Task <KotaResponse> GetAll(string search, string order, string orderDir, int startRec, int pageSize, int draw)
        {
            var models = await dep.GetAll(search, order, orderDir, startRec, pageSize, draw);

            if (models == null)
            {
                return(null);
            }
            return(models);
        }
Ejemplo n.º 2
0
        public async Task <BarangResponse> SubmitUpload(string fileName, string username)
        {
            BarangResponse response = new BarangResponse();

            try
            {
                string    folderName = Path.Combine("Resources", "UploadDocs");
                string    filePath   = Path.Combine(Directory.GetCurrentDirectory(), folderName);
                string    sheetName  = "Master";
                DataTable dt         = new DataTable();

                string dbPath = Path.Combine(folderName, fileName);

                List <Kota> ListKota = await da_kota.GetAll();

                List <Merk> ListMerk = await da_merk.GetAllMerk();

                List <ModelBarang> ListModel = await da_model.GetAllModel();

                List <TypeBarang> ListType = await da_type.GetAllType();

                List <Warna> ListWarna = await da_warna.GetWarna();

                List <Barang> ListBarang = await dep.GetAllBarang();

                using (ExcelPackage pck = new ExcelPackage())
                {
                    using (FileStream stream = new FileStream(dbPath, FileMode.Open))
                    {
                        pck.Load(stream);
                        ExcelWorksheet oSheet = pck.Workbook.Worksheets[sheetName];
                        dt = WorksheetToDataTable(oSheet);
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            long KotaID       = 0;
                            long MerkID       = 0;
                            long ModelID      = 0;
                            long TypeID       = 0;
                            long WarnaID      = 0;
                            long BarangID     = 0;
                            long NegoBarangID = 0;

                            errMerk       = dt.Rows[i].ItemArray.GetValue(1).ToString();
                            errModel      = dt.Rows[i].ItemArray.GetValue(2).ToString();
                            errType       = dt.Rows[i].ItemArray.GetValue(3).ToString();
                            errWarna      = dt.Rows[i].ItemArray.GetValue(4).ToString();
                            errBarang     = dt.Rows[i].ItemArray.GetValue(5).ToString();
                            errOTR        = dt.Rows[i].ItemArray.GetValue(6).ToString();
                            errHargaFinal = dt.Rows[i].ItemArray.GetValue(8).ToString();

                            #region Insert To Kota
                            string Kota = dt.Rows[i].ItemArray.GetValue(0).ToString();
                            if (!ListKota.Where(x => x.Name.ToLower() == Kota.ToLower()).Any())
                            {
                                Kota k = new Kota();
                                k.Name        = Kota;
                                k.Description = Kota;

                                k.RowStatus = true;
                                k.Created   = DateTime.Now;
                                k.CreatedBy = username;
                                KotaID      = da_kota.AddPost(k).Result;
                                ListKota.Add(k);
                            }
                            else
                            {
                                KotaID = ListKota.Where(x => x.Name.ToLower() == Kota.ToLower() && x.RowStatus == true).First().Id;
                            }
                            #endregion

                            #region Insert To Merk
                            string Merk = dt.Rows[i].ItemArray.GetValue(1).ToString();
                            if (!ListMerk.Where(x => x.Name.ToLower() == Merk.ToLower()).Any())
                            {
                                Merk m = new Merk();
                                m.Name        = Merk;
                                m.Description = Merk;
                                m.RowStatus   = true;
                                m.Created     = DateTime.Now;
                                m.CreatedBy   = username;
                                MerkID        = da_merk.Add(m).Result;
                                ListMerk.Add(m);
                            }
                            else
                            {
                                MerkID = ListMerk.Where(x => x.Name.ToLower() == Merk.ToLower() && x.RowStatus == true).First().Id;
                            }
                            #endregion

                            #region Insert to ModelBarang
                            string Model = dt.Rows[i].ItemArray.GetValue(2).ToString();
                            if (!ListModel.Where(x => x.Name.ToLower() == Model.ToLower()).Any())
                            {
                                ModelBarang mb = new ModelBarang();
                                mb.MerkId      = MerkID;
                                mb.Name        = Model;
                                mb.Description = Model;
                                mb.RowStatus   = true;
                                mb.Created     = DateTime.Now;
                                mb.Createdby   = username;
                                ModelID        = da_model.Add(mb).Result;
                                ListModel.Add(mb);
                            }
                            else
                            {
                                ModelID = ListModel.Where(x => x.Name.ToLower() == Model.ToLower() && x.RowStatus == true).First().Id;
                            }
                            #endregion

                            #region Insert to TypeBarang
                            string Type = dt.Rows[i].ItemArray.GetValue(4).ToString();
                            if (!ListType.Where(x => x.Name.ToLower() == Type.ToLower()).Any())
                            {
                                TypeBarang tb = new TypeBarang();
                                tb.Name          = Type;
                                tb.ModelBarangId = ModelID;
                                tb.Description   = Type;
                                tb.RowStatus     = true;
                                tb.Created       = DateTime.Now;
                                tb.CreatedBy     = username;
                                TypeID           = da_type.Add(tb).Result;
                                ListType.Add(tb);
                            }
                            else
                            {
                                TypeID = ListType.Where(x => x.RowStatus == true && x.Name.ToLower() == Type.ToLower()).First().Id;
                            }
                            #endregion

                            #region Insert to Warna
                            string Warna = dt.Rows[i].ItemArray.GetValue(6).ToString();
                            if (!ListWarna.Where(x => x.Name.ToLower() == Warna.ToLower()).Any())
                            {
                                Warna w = new Warna();
                                w.Name        = Warna;
                                w.Description = Warna;
                                w.Sapcode     = dt.Rows[i].ItemArray.GetValue(5).ToString();
                                w.RowStatus   = true;
                                w.Created     = DateTime.Now;
                                w.CreatedBy   = username;
                                WarnaID       = da_warna.Add(w).Result;
                                ListWarna.Add(w);
                            }
                            else
                            {
                                WarnaID = ListWarna.Where(x => x.RowStatus == true && x.Name.ToLower() == Warna.ToLower()).First().Id;
                            }
                            #endregion

                            string OTRstrRaw = dt.Rows[i].ItemArray.GetValue(8).ToString();
                            string OTRstr    = OTRstrRaw.Contains('.') ? OTRstrRaw.Substring(0, OTRstrRaw.LastIndexOf('.')) : OTRstrRaw;

                            string DiscstrRaw = dt.Rows[i].ItemArray.GetValue(9).ToString();
                            string Discstr    = DiscstrRaw.Contains('.') ? DiscstrRaw.Substring(0, DiscstrRaw.LastIndexOf('.')) : DiscstrRaw;

                            string FinalRaw = dt.Rows[i].ItemArray.GetValue(10).ToString();
                            string Finalstr = FinalRaw.Contains('.') ? FinalRaw.Substring(0, FinalRaw.LastIndexOf('.')) : FinalRaw;

                            string YearstrRaw = dt.Rows[i].ItemArray.GetValue(7).ToString();
                            string Yearstr    = YearstrRaw.Contains('.') ? YearstrRaw.Substring(0, OTRstrRaw.LastIndexOf('.')) : YearstrRaw;

                            long OTR        = Convert.ToInt64(OTRstr);
                            long Discount   = Convert.ToInt64(Discstr);
                            long HargaFinal = Convert.ToInt64(Finalstr);
                            int  Year       = Convert.ToInt32(Yearstr);

                            #region Insert to Barang
                            Barang brg = new Barang()
                            {
                                RowStatus    = true,
                                Created      = DateTime.Now,
                                CreatedBy    = username,
                                HargaOtr     = OTR,
                                Name         = Type,
                                WarnaId      = WarnaID,
                                TypeBarangId = TypeID,
                                KotaId       = KotaID,
                                Year         = Year,
                                KodeType     = dt.Rows[i].ItemArray.GetValue(3).ToString()
                            };
                            BarangID = dep.AddPost(brg).Result;
                            #endregion

                            #region Insert to NegoBarang
                            NegoBarang nb = new NegoBarang()
                            {
                                RowStatus     = true,
                                BarangId      = BarangID,
                                UserProfileId = 3,
                                TypePenawaran = "ask",
                                Harga         = HargaFinal,
                                Created       = DateTime.Now,
                                CreatedBy     = username
                            };
                            NegoBarangID = da_nego.AddPost(nb).Result;
                            #endregion
                        }
                        response.IsSuccess = true;
                        response.Message   = "Success";
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                string Ket = string.Format("Merk={0}, Model={1}, Type={2}, Warna={3}, Barang={4}, HargaOTR={5}, Harga Final={6}, Error={7} ",
                                           errMerk, errModel, errType, errWarna, errBarang, errOTR, errHargaFinal, ex.ToString());
                response.Message   = Ket;// ex.ToString();
                response.IsSuccess = false;
            }
            return(response);
        }