Exemplo n.º 1
0
        public List <Cabinet> ToEntities()
        {
            var cabinets = new List <Cabinet>();

            for (int i = 0; i < CSV.Count; i++)
            {
                if (CSV[i].Count() != ColumnCount)
                {
                    throw new EntityParsingException("Invalid column count", i);
                }
                string name     = CSV[i][0];
                string typeName = CSV[i][1];
                string address  = CSV[i][2];
                string phone    = CSV[i][3];
                //check length
                if (name.Length > NameLength)
                {
                    throw new EntityParsingException("Invalid length", nameof(name), name, i);
                }
                if (typeName.Length > typeNameLength)
                {
                    throw new EntityParsingException("Invalid length", nameof(typeName), typeName, i);
                }
                if (address.Length > AddressLength)
                {
                    throw new EntityParsingException("Invalid length", nameof(address), address, i);
                }
                if (phone.Length > PhoneLength)
                {
                    throw new EntityParsingException("Invalid length", nameof(phone), phone, i);
                }
                //get id
                int?typeId;
                using (var context = new InventoryDbEntities())
                {
                    if (!IdTranslater.GetCabinetTypeId(typeName, out typeId, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "CabinetType", typeName, i);
                    }
                }
                var cabinet = new Cabinet()
                {
                    CabinetName   = name,
                    CabinetTypeId = typeId ?? -1,
                    Address       = address,
                    Phone         = phone
                };
                cabinets.Add(cabinet);
            }
            return(cabinets);
        }
Exemplo n.º 2
0
        public List <TransactionPermission> ToEntities()
        {
            var entities = new List <TransactionPermission>();

            for (int i = 0; i < CSV.Count; i++)
            {
                string pType       = CSV[i][0];
                string userName    = CSV[i][1];
                string cabinetType = CSV[i][2];
                string note        = CSV[i][3];
                if (note.Length > NoteLength)
                {
                    throw new EntityParsingException("Invalid length", nameof(note), note, i);
                }

                int cId, userId;

                int?id;
                using (var context = new InventoryDbEntities())
                {
                    //to cabinet id
                    if (!IdTranslater.GetCabinetTypeId(cabinetType, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "CabinetType", cabinetType, i);
                    }
                    cId = id ?? -1;
                    //username
                    if (!IdTranslater.GetUserId(userName, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "Username", userName, i);
                    }
                    userId = id ?? -1;
                }

                var per = new TransactionPermission()
                {
                    PermissionTypeName = pType,
                    CabinetTypeId      = cId,
                    UserId             = userId,
                    Note = note
                };
                entities.Add(per);
            }
            return(entities);
        }
Exemplo n.º 3
0
        public List <Item> ToEntities()
        {
            var entities = new List <Item>();

            for (int i = 0; i < CSV.Count; i++)
            {
                if (CSV[i].Count() != ColumnCount)
                {
                    throw new EntityParsingException("Invalid column count", i);
                }
                string code = CSV[i][0];
                string name = CSV[i][1];
                string cat  = CSV[i][2];
                if (code.Length > CodeLength)
                {
                    throw new EntityParsingException("Invalid length", "ItemCode", code, i);
                }
                if (name.Length > NameLength)
                {
                    throw new EntityParsingException("Invalid length", "ItemName", name, i);
                }
                int catId;
                int?id;
                using (var context = new InventoryDbEntities())
                {
                    if (!IdTranslater.GetCatId(cat, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "Item Category", cat, i);
                    }
                }
                catId = id ?? -1;
                var item = new Item()
                {
                    ItemCode = code,
                    ItemName = name,
                    CatId    = catId
                };
                entities.Add(item);
            }
            return(entities);
        }
Exemplo n.º 4
0
        public List <Transaction> ToEntities()
        {
            var    transactions = new List <Transaction>();
            string currentField = string.Empty;

            for (int i = 0; i < CSV.Count; i++)
            {
                if (CSV[i].Count() != ColumnCount)
                {
                    throw new EntityParsingException("Invalid column count", i);
                }
                string itemCode    = CSV[i][0];
                string fromCabinet = CSV[i][2];
                string toCabinet   = CSV[i][3];
                string quanlity    = CSV[i][1];
                string inputDate   = CSV[i][4];
                string price       = CSV[i][5];
                string note        = CSV[i][6];

                //convert to data type
                string   parseMessage = string.Empty;
                DateTime inputDatetime;
                int      priceInt, quanlityInt;
                if (!EntityParser.ParseInt(quanlity, out quanlityInt, out parseMessage))
                {
                    throw new EntityParsingException(parseMessage, nameof(quanlity), quanlity, i);
                }
                if (!EntityParser.ParseInt(price, out priceInt, out parseMessage))
                {
                    throw new EntityParsingException(parseMessage, nameof(price), price, i);
                }
                if (!EntityParser.ParseDatetime(inputDate, out inputDatetime, out parseMessage))
                {
                    throw new EntityParsingException(parseMessage, nameof(inputDate), inputDate, i);
                }
                //check valid
                if (priceInt < 0)
                {
                    throw new EntityParsingException("Price must > 0");
                }
                if (quanlityInt < 1)
                {
                    throw new EntityParsingException("Quanlity must > 0");
                }

                //check name length
                if (itemCode.Length > ItemCodeLength)
                {
                    throw new EntityParsingException("Invalid length", nameof(itemCode), itemCode, i);
                }
                if (fromCabinet.Length > CabinetNameLength)
                {
                    throw new EntityParsingException("Invalid length", "ItemName", nameof(fromCabinet), i);
                }
                if (toCabinet.Length > CabinetNameLength)
                {
                    throw new EntityParsingException("Invalid length", "ItemName", nameof(toCabinet), i);
                }

                //get ids
                int fromCabinetId, toCabinetId, itemId;
                int?id;
                using (var context = new InventoryDbEntities())
                {
                    //from cabinet id
                    if (!IdTranslater.GetCabinetId(fromCabinet, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "FromCabinet", fromCabinet, i);
                    }
                    fromCabinetId = id ?? -1;
                    //to cabinet id
                    if (!IdTranslater.GetCabinetId(toCabinet, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "ToCabinet", toCabinet, i);
                    }
                    toCabinetId = id ?? -1;
                    //item code
                    if (!IdTranslater.GetItemId(itemCode, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "ItemCode", itemCode, i);
                    }
                    itemId = id ?? -1;
                }
                var transaction = new Transaction()
                {
                    ItemId            = itemId,
                    Quanlity          = quanlityInt,
                    ProviderCabinetId = fromCabinetId,
                    ReceiverCabinetId = toCabinetId,
                    InputDate         = inputDatetime,
                    TransactionDate   = DateTime.Today,
                    Price             = priceInt,
                    Note   = note,
                    UserId = this.UserId
                };
                transactions.Add(transaction);
            }
            return(transactions);
        }
Exemplo n.º 5
0
        public List <Order> ToEntities()
        {
            var orders = new List <Order>();

            for (int i = 0; i < CSV.Count; i++)
            {
                if (CSV[i].Count() != ColumnCount)
                {
                    throw new EntityParsingException("Invalid column count", i);
                }
                string itemCode     = CSV[i][0];
                string provider     = CSV[i][3];
                string toCabinet    = CSV[i][2];
                string quanlityStr  = CSV[i][1];
                string priceStr     = CSV[i][4];
                string orderDateStr = CSV[i][5];
                string note         = CSV[i][6];
                //string userName = CSV[i][3];
                //check length
                if (itemCode.Length > ItemCodeLen)
                {
                    throw new EntityParsingException("Invalid length", nameof(itemCode), itemCode, i);
                }
                if (provider.Length > ProviderLen)
                {
                    throw new EntityParsingException("Invalid length", nameof(provider), provider, i);
                }
                if (toCabinet.Length > CabinetNameLen)
                {
                    throw new EntityParsingException("Invalid length", nameof(toCabinet), toCabinet, i);
                }
                if (note.Length > NoteLen)
                {
                    throw new EntityParsingException("Invalid length", nameof(note), note, i);
                }

                string   parseMessage = string.Empty;
                int      price, quanlity;
                DateTime orderDate;
                if (!EntityParser.ParseInt(quanlityStr, out quanlity, out parseMessage))
                {
                    throw new EntityParsingException(parseMessage, nameof(quanlity), quanlityStr, i);
                }
                if (!EntityParser.ParseInt(priceStr, out price, out parseMessage))
                {
                    throw new EntityParsingException(parseMessage, nameof(price), priceStr, i);
                }
                if (!EntityParser.ParseDatetime(orderDateStr, out orderDate, out parseMessage))
                {
                    throw new EntityParsingException(parseMessage, nameof(orderDate), orderDateStr, i);
                }

                //check valid
                if (price < 0)
                {
                    throw new EntityParsingException("Price must > 0");
                }
                if (quanlity < 1)
                {
                    throw new EntityParsingException("Quanlity must > 0");
                }

                //get ids
                int toCabinetId, providerId, itemId;
                int?id;
                using (var context = new InventoryDbEntities())
                {
                    //from cabinet id
                    if (!IdTranslater.GetCabinetId(toCabinet, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "ToCabinet", toCabinet, i);
                    }
                    toCabinetId = id ?? -1;
                    //to cabinet id
                    if (!IdTranslater.GetProviderId(provider, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "Provider", provider, i);
                    }
                    providerId = id ?? -1;
                    //item code
                    if (!IdTranslater.GetItemId(itemCode, out id, context))
                    {
                        throw new EntityParsingException("Name doesnt exist", "ItemCode", itemCode, i);
                    }
                    itemId = id ?? -1;
                }
                var order = new Order()
                {
                    ItemId     = itemId,
                    Quanlity   = quanlity,
                    Price      = price,
                    CabinetId  = toCabinetId,
                    ProviderId = providerId,
                    OrderDate  = DateTime.Now,
                    InputDate  = orderDate,
                    Note       = note,
                    UserId     = this.UserId
                };
                orders.Add(order);
            }
            return(orders);
        }