Example #1
0
        public OperationResult Create(ProductViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                // context 橋梁 "承上啟下" 資料庫的上下文
                BizRepository <Product> repository
                    = new BizRepository <Product>(context);
                // viewModel 轉成 Model 再給資料庫
                Product entity = new Product()
                {
                    PartNo = input.PartNo, PartName = input.PartName
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Example #2
0
        public OperationResult Create(SellingViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                using (var transaction = context.Database.BeginTransaction())//Rollback
                {
                    var     sellCount         = input.Quantity;
                    var     procurementRepo   = new BizRepository <Procurement>(context);
                    var     sellingSourceRepo = new BizRepository <SellingSource>(context);
                    var     sellingRepo       = new BizRepository <Selling>(context);
                    Selling entity            = new Selling()
                    {
                        PartNo         = input.PartNo,
                        Quantity       = input.Quantity,
                        SalesJobNumber = input.SalesJobNumber,
                        SellingDay     = input.SellingDay,
                        UnitPrice      = input.UnitPrice
                    };
                    sellingRepo.Create(entity);
                    context.SaveChanges();
                    var products = procurementRepo.GetAll()
                                   .Where((x) => x.PartNo == input.PartNo).OrderBy((x) => x.PurchasingDay);
                    foreach (var p in products)
                    {
                        if (sellCount <= 0)
                        {
                            break;
                        }
                        else
                        {
                            if (p.InvetoryQuantity >= sellCount)
                            {
                                p.InvetoryQuantity = p.InvetoryQuantity - sellCount;
                                CreateSellingSource(sellingSourceRepo, entity.SellingId, p.ProcurementId, sellCount);
                                sellCount = 0;
                            }
                            else
                            {
                                sellCount = sellCount - p.InvetoryQuantity;
                                CreateSellingSource(sellingSourceRepo, entity.SellingId, p.ProcurementId, p.InvetoryQuantity);
                                p.InvetoryQuantity = 0;
                            }
                        }
                    }

                    context.SaveChanges();
                    result.IsSuccessful = true;
                    transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Example #3
0
        public ProcurementListQueryViewModel GetStockList()
        {
            var      result          = new ProcurementListQueryViewModel();
            BizModel context         = new BizModel();
            var      procurementRepo = new BizRepository <Procurement>(context);
            var      productRepo     = new BizRepository <Product>(context);
            var      temp            = from p in productRepo.GetAll()
                                       join q in procurementRepo.GetAll()
                                       on p.PartNo equals q.PartNo
                                       select new
            {
                PartNo   = p.PartNo,
                PartName = p.PartName,
                Quantity = q.InvetoryQuantity
            };
            var group = from t in temp
                        group t by new { t.PartNo, t.PartName } into g
                select new ProcurementQueryViewModel
            {
                PartName = g.Key.PartName,
                PartNo   = g.Key.PartNo,
                TotalInvetoryQuantity = g.Sum((x) => x.Quantity)
            };

            result.Items = group.ToList();
            return(result);
        }
Example #4
0
        public OperationResult Create(ProductViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                BizRepository <Product> repository
                    = new BizRepository <Product>(context);
                Product entity = new Product()
                {
                    PartNo = input.PartNo, PartName = input.PartName
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;

                /*if (FakeProducts.Any((x) => x.PartNo == product.PartNo))
                 * {
                 *  throw new ArgumentException($"PartNo {product.PartNo} exsists");
                 * }
                 * else
                 * {
                 *  FakeProducts.Add(product);
                 *  result.IsSuccessful = true;
                 * }*/
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Example #5
0
        public OperationResult Create(ProcurementViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel    context    = new BizModel();
                var         repository = new BizRepository <Procurement>(context);
                Procurement entity     = new Procurement()
                {
                    PartNo           = input.PartNo,
                    PurchasingDay    = input.PurchasingDay,
                    Quantity         = input.Quantity,
                    InvetoryQuantity = input.InvetoryQuantity,
                    UintPrice        = input.UnitPrice
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Example #6
0
        public bool IsNameExist(SalesManViewModel input)
        {
            BizModel context = new BizModel();
            BizRepository <SalesMan> repository = new BizRepository <SalesMan>(context);

            return(repository.GetAll().Any((x) => x.Name == input.Name));
        }
Example #7
0
        public SellingListQueryViewModel GetSellingBySalesAndDay(int jobNumber, DateTime begin, DateTime end)
        {
            var      result       = new SellingListQueryViewModel();
            BizModel context      = new BizModel();
            var      sellingRepo  = new BizRepository <Selling>(context);
            var      salesManRepo = new BizRepository <SalesMan>(context);
            var      temp         =
                from selling in sellingRepo.GetAll()
                join sales in salesManRepo.GetAll()
                on selling.SalesJobNumber equals sales.JobNumber
                where selling.SalesJobNumber == jobNumber &&
                selling.SellingDay >= begin &&
                selling.SellingDay < end
                select new SellingQueryViewModel
            {
                PartNo         = selling.PartNo,
                Quantity       = selling.Quantity,
                SalesJobNumber = selling.SalesJobNumber,
                SalesName      = sales.Name,
                SellingDay     = selling.SellingDay,
                SellingId      = selling.SellingId,
                UnitPrice      = selling.UnitPrice,
                TotalPrice     = selling.UnitPrice * selling.Quantity
            };

            result.Items = temp.ToList();

            return(result);
        }
Example #8
0
        public SellingListQueryViewModel GetSellingBySalesMan(int JobNumber)
        {
            var result = new SellingListQueryViewModel();

            result.Item = new List <SellingQueryViewModel>();
            BizModel context = new BizModel();
            BizRepository <Selling>  SellingRepo  = new BizRepository <Selling>(context);
            BizRepository <SalesMan> SalesManRepo = new BizRepository <SalesMan>(context);

            var temp = from p in SellingRepo.GetAll()
                       join s in SalesManRepo.GetAll()
                       on p.SalesJobNumber equals s.JobNumber
                       where s.JobNumber == JobNumber
                       select new SellingQueryViewModel
            {
                PartNo         = p.PartNo,
                Quantity       = p.Quantity,
                UnitPrice      = p.UnitPrice,
                SalesJobNumber = s.JobNumber,
                SalesName      = s.Name,
                SellingDay     = p.SellingDay,
                SellingId      = p.SellingId,
                TotalPrice     = p.UnitPrice * p.Quantity
            };

            result.Item = temp.ToList();
            return(result);
        }
Example #9
0
        public List <Product> GetAll()
        {
            BizModel context = new BizModel();
            BizRepository <Product> repository = new BizRepository <Product>(context);
            var result = repository.GetAll().ToList();

            return(result);
        }
Example #10
0
        public Product GetByPartNo(string partNo)
        {
            BizModel context = new BizModel();
            BizRepository <Product> repository = new BizRepository <Product>(context);
            var result = repository.GetAll().FirstOrDefault(x => x.PartNo == partNo);

            return(result);
        }
Example #11
0
        public void Create(Product entity)
        {
            BizModel context = new BizModel();
            BizRepository <Product> repository = new BizRepository <Product>(context);

            repository.Create(entity);
            context.SaveChanges();
        }
Example #12
0
        public List <Product> GetAll()
        {
            BizModel content = new BizModel();
            BizRepository <Product> repository = new BizRepository <Product>(content);
            var result = repository.GetAll().OrderBy((x) => x.PartNo).ToList();

            return(result);
        }
Example #13
0
        private void CreateSellingSource(BizRepository <SellingSource> sellingSourceRepo, int sellingId, int procurementId, int sellCount)
        {
            SellingSource sellingSource = new SellingSource()
            {
                ProcurementId = procurementId,
                SellingId     = sellingId,
                Quantity      = sellCount
            };

            sellingSourceRepo.Create(sellingSource);
        }
Example #14
0
        public int GetStock(string partNo)
        {
            BizModel context    = new BizModel();
            var      repository = new BizRepository <Procurement>(context);
            var      items      = repository.GetAll().Where((x) => x.PartNo == partNo);
            int      result     = 0;

            if (items.Count() > 0)
            {
                result = items.Sum((x) => x.InvetoryQuantity);
            }
            return(result);
        }
Example #15
0
        public int GetStocks(string PartNo)
        {
            BizModel context = new BizModel();
            BizRepository <Procurement> repository = new BizRepository <Procurement>(context);
            var item   = repository.GetAll().Where(x => x.PartNo == PartNo);
            var result = 0;

            if (item.Count() > 0)
            {
                result = item.Sum(x => x.InvetoryQuantity);
            }
            return(result);
        }
Example #16
0
 public ProductListViewModel GetProducts()
 {
     var result = new ProductListViewModel();
     result.Items = new List<ProductViewModel>();
     BizModel context = new BizModel();
     BizRepository<Product> repository = new BizRepository<Product>(context);
     foreach (var item in repository.GetAll().OrderBy((x) => x.PartNo))
     {
         var p = new ProductViewModel()
         {
             PartNo = item.PartNo,
             PartName = item.PartName
         };
         result.Items.Add(p);
     }
     return result;
 }
Example #17
0
        public SalesManListViewModel GetSalesMen()
        {
            var result = new SalesManListViewModel();

            result.Items = new List <SalesManViewModel>();
            BizModel context    = new BizModel();
            var      repository = new BizRepository <SalesMan>(context);

            foreach (var item in repository.GetAll().OrderBy((x) => x.JobNumber))
            {
                var p = new SalesManViewModel()
                {
                    JobNumber = item.JobNumber,
                    Name      = item.Name
                };
                result.Items.Add(p);
            }
            return(result);
        }
Example #18
0
        public OperationResult Create(SalesManViewModel input)
        {
            var result = new OperationResult();

            try
            {
                BizModel context = new BizModel();
                BizRepository <SalesMan> repository = new BizRepository <SalesMan>(context);
                SalesMan entity = new SalesMan()
                {
                    Name = input.Name
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex) {
                result.IsSuccessful = false;
                result.exception    = ex;
            }
            return(result);
        }
Example #19
0
        //假的行為,到目前為止還不知道資料庫的真實內容,測試用
        //public static List<ProductViewModel> FakeProducts = new List<ProductViewModel>();

        public OperationResult Create(ProductViewModel input)
        {
            var result = new OperationResult();

            try
            {
                //if (FakeProducts.Any((x) => x.ParNo == product.PartNo) != null)
                //{
                //    throw new ArgumentException($"PartNo {product.PartNo} is not exsist");
                //}
                //else
                //{
                //    FakeProduct.Add(product);
                //    result.IsSuccessful = true;
                //}

                BizModel context = new BizModel();
                BizRepository <Product> repository = new BizRepository <Product>(context);
                //context 橋梁、承上啟下,接通兩個不同的端點(EX:資料庫、程式)
                Product entity = new Product()
                {
                    PartNo   = input.PartNo,
                    PartName = input.PartName
                };
                repository.Create(entity);
                context.SaveChanges();
                result.IsSuccessful = true;
            }
            catch (Exception ex)
            {
                result.IsSuccessful = false;
                result.exception    = ex;
            }

            return(result);
        }