Esempio n. 1
0
        /// <summary>
        /// 確認賣出時經過的資料
        /// </summary>
        /// <param name="product"></param>
        /// <param name="txViewModel"></param>
        /// <param name="txRecord">賣出時更新的交易資料</param>
        /// <param name="oddTransaction">賣出時被合併的零股資料</param>
        private void CheckSoldReceived(Product product, TransactionViewModel txViewModel, TransactionRecord txRecord, TransactionRecord oddTransaction = null)
        {
            dbConnection.Received().QueryableTransactionRecords.OrderBy(p => p.TransactionTime).ThenByDescending(p => p.UnitPrice)
            .Where(p => p.ProductSeq == txViewModel.ProductSeq && p.InStock > 0);

            //零股交易且剩餘零股小於交易零股的話將零股合併後再計算
            var needMargeOdd = oddTransaction != null && oddTransaction.InStock < txViewModel.Num;

            if (needMargeOdd)
            {
                //SetMargeModel
                txRecord.InStock    += oddTransaction.InStock;
                txRecord.TotalPrice += oddTransaction.InStock * oddTransaction.UnitPrice;
                txRecord.UnitPrice   = txRecord.TotalPrice / txRecord.InStock;

                dbConnection.Modified(Arg.Is <TransactionRecord>(p =>
                                                                 p.Seq == oddTransaction.Seq &&
                                                                 p.InStock == 0
                                                                 ), EntityState.Modified);
            }

            var updateStock = txRecord.InStock - txViewModel.Num;

            dbConnection.Received()
            .Modified(Arg.Is <TransactionRecord>(p => p.ProductSeq == product.ProductSeq && p.InStock == updateStock),
                      EntityState.Modified);
            dbConnection.Received()
            .Modified(Arg.Is <Bookkeeping>(p => p.ProductSeq == txViewModel.ProductSeq), EntityState.Added);

            //判斷沒有經過買入的資料
            dbConnection.DidNotReceive().Modified(Arg.Is <TransactionRecord>(p => p.ProductSeq == product.ProductSeq), EntityState.Added);
            dbConnection.DidNotReceive()
            .Modified(Arg.Is <TransactionRecord>(p => p.ProductSeq == product.ProductSeq && p.Num == txViewModel.Num),
                      EntityState.Added);
        }
Esempio n. 2
0
        /// <summary>
        /// 新增配股配息紀錄
        /// </summary>
        /// <param name="dividends"></param>
        /// <returns></returns>
        public string AddDividends(Dividends dividends)
        {
            dividends.CreateTime = DateTime.Now;

            dbConnection.Modified(dividends, EntityState.Added);
            dbConnection.SaveChanges();
            return("Success");
        }
        public string EditProduct(Product product)
        {
            var isNewProduct = product.ProductSeq == 0;

            if (!isNewProduct)
            {
                var oldData = dbConnection.QueryableProducts.FirstOrDefault(p => p.ProductSeq == product.ProductSeq);
                if (oldData == null)
                {
                    throw new DataNotFoundException();
                }

                oldData.ProductName = product.ProductName;
                oldData.Remark      = product.Remark;
                oldData.Owner       = product.Owner;

                dbConnection.Modified(oldData, EntityState.Modified);
            }
            else
            {
                dbConnection.Modified(product, EntityState.Added);
            }

            try
            {
                dbConnection.SaveChanges();
                if (isNewProduct)
                {
                    assetRepository.AddNewAsset(product);
                }

                return("Success");
            }
            catch (Exception ee)
            {
                return(ee.Message);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// 建立完產品後會新增該產品的資產資料供日後計算
        /// </summary>
        /// <param name="product"></param>
        public void AddNewAsset(Product ProductData)
        {
            CheckProductIsNull(ProductData);

            if (CheckAssetIsNew(ProductData))
            {
                var newAsset = new Asset()
                {
                    ProductSeq = ProductData.ProductSeq,
                    Num        = 0,
                    TotalPrice = 0,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                };

                dbConnection.Modified(newAsset, EntityState.Added);
                dbConnection.SaveChanges();
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 交易紀錄_賣出
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="transNum">交易數量(負數)</param>
        /// <param name="transCost">交易成本(負數)</param>
        public void Tx_SaleFunction(TransactionViewModel transaction, out int transNum, out int transCost)
        {
            var tranList = dbConnection.QueryableTransactionRecords.OrderBy(p => p.TransactionTime).ThenByDescending(p => p.UnitPrice)
                           .Where(p => p.ProductSeq == transaction.ProductSeq && p.InStock > 0);
            var updateTransaction = new TransactionRecord();

            var oddTransaction = tranList.FirstOrDefault(p => p.InStock < 1000);

            //除了零股足夠外其餘都直接取整張做交易
            updateTransaction = tranList.FirstOrDefault(p => p.InStock == 1000);

            if (transaction.Num < 1000)
            {
                //確認資產是否有零股且零股足夠交易
                if (oddTransaction != null && oddTransaction.InStock >= transaction.Num)
                {
                    updateTransaction = oddTransaction;
                }
                else if (oddTransaction != null)
                {
                    //不夠的話賣出整張後把餘額合併到原本的零股
                    updateTransaction.InStock    += oddTransaction.InStock;
                    updateTransaction.TotalPrice += oddTransaction.InStock * oddTransaction.UnitPrice;
                    updateTransaction.UnitPrice   = updateTransaction.TotalPrice / updateTransaction.InStock;

                    //清除舊有的零股
                    oddTransaction.InStock = 0;
                    oddTransaction.Remark += $"零股賣出合併至編號:{updateTransaction.Seq}";
                    dbConnection.Modified(oddTransaction, EntityState.Modified);
                }
            }


            updateTransaction.SaleTime  = transaction.TransactionTime;
            updateTransaction.SalePrice = transaction.UnitPrice;
            updateTransaction.SaleTax   = transaction.Fee;
            updateTransaction.InStock  -= transaction.Num;
            updateTransaction.Remark   += $"/ {transaction.Remark}";

            dbConnection.Modified(updateTransaction, EntityState.Modified);

            transNum = 0 - transaction.Num;
            //扣的資產應為交易本身的成本而非售價(此cost為負數)
            transCost = 0 - (int)(updateTransaction.UnitPrice * transaction.Num);

            //計算本次收益到記帳本
            var salePrice = (transaction.Num * transaction.UnitPrice);
            var incom     = salePrice + transCost - transaction.Fee - updateTransaction.AdministractionFee;


            var newBookkepping = new Bookkeeping()
            {
                ProductSeq = transaction.ProductSeq,
                Type       = BookkeepingType.TransactionRecord.ToString(),
                RelatedSeq = updateTransaction.Seq,
                Amount     = (int)incom,
                CreateTime = DateTime.Now,
            };

            dbConnection.Modified(newBookkepping, EntityState.Added);
        }