Esempio n. 1
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);
        }
Esempio n. 2
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);
        }