/// <inheritdoc />
        public override IQueryResult Execute(string expr, IEntitiesSerializer serializer)
        {
            var  voucherID = Parsing.Token(ref expr);
            Guid?guid      = null;
            var  guidT     = Guid.Empty;

            // ReSharper disable once AccessToModifiedClosure
            if (Parsing.Token(ref expr, true, s => Guid.TryParse(s, out guidT)) != null)
            {
                guid = guidT;
            }
            var name  = Parsing.Token(ref expr);
            var lifeT = Parsing.Double(ref expr);

            Parsing.Eof(expr);

            var voucher = Accountant.SelectVoucher(voucherID);

            if (voucher == null)
            {
                throw new ApplicationException("找不到记账凭证");
            }

            var detail = voucher.Details.Single(
                vd => (vd.Title == 1601 || vd.Title == 1701) &&
                (!guid.HasValue || string.Equals(
                     vd.Content,
                     guid.ToString(),
                     StringComparison.OrdinalIgnoreCase)));

            var isFixed = detail.Title == 1601;
            var life    = (int?)lifeT ?? (isFixed ? 3 : 0);

            var asset = new Asset
            {
                StringID                    = detail.Content,
                Name                        = name,
                Date                        = voucher.Date,
                User                        = detail.User,
                Currency                    = detail.Currency,
                Value                       = detail.Fund,
                Salvage                     = life == 0 ? detail.Fund : isFixed ? detail.Fund * 0.05 : 0,
                Life                        = life,
                Title                       = detail.Title,
                Method                      = life == 0 ? DepreciationMethod.None : DepreciationMethod.StraightLine,
                DepreciationTitle           = isFixed ? 1602 : 1702,
                DepreciationExpenseTitle    = 6602,
                DepreciationExpenseSubTitle = isFixed ? 7 : 11,
                DevaluationTitle            = isFixed ? 1603 : 1703,
                DevaluationExpenseTitle     = 6701,
                DevaluationExpenseSubTitle  = isFixed ? 5 : 6,
                Schedule                    = new List <AssetItem>
                {
                    new AcquisitionItem
                    {
                        Date      = voucher.Date,
                        VoucherID = voucher.ID,
                        // ReSharper disable once PossibleInvalidOperationException
                        OrigValue = detail.Fund.Value,
                    },
                },
            };

            Accountant.Upsert(asset);
            // ReSharper disable once PossibleInvalidOperationException
            asset = Accountant.SelectAsset(asset.ID.Value);
            Accountant.Depreciate(asset);
            Accountant.Upsert(asset);

            return(new DirtyText(serializer.PresentAsset(asset).Wrap()));
        }
Exemple #2
0
        /// <inheritdoc />
        public override IQueryResult Execute(string expr, IEntitiesSerializer serializer)
        {
            var voucherID = Parsing.Token(ref expr);
            var guids     = new List <string>();
            var guidT     = Guid.Empty;

            // ReSharper disable once AccessToModifiedClosure
            while (Parsing.Token(ref expr, true, s => Guid.TryParse(s, out guidT)) != null)
            {
                guids.Add(guidT.ToString());
            }
            Parsing.Eof(expr);

            var voucher = Accountant.SelectVoucher(voucherID);

            if (voucher == null)
            {
                throw new ApplicationException("找不到记账凭证");
            }

            var sb = new StringBuilder();

            foreach (var detail in voucher.Details.Where(vd => vd.Title is 1601 or 1701))
            {
                if (guids.Count != 0 &&
                    !guids.Contains(detail.Content))
                {
                    continue;
                }

                var asset = Accountant.SelectAsset(Guid.Parse(detail.Content));
                foreach (var item in asset.Schedule)
                {
                    if (item is DispositionItem)
                    {
                        throw new InvalidOperationException("已经处置");
                    }

                    if (item.Date < voucher.Date)
                    {
                        if (item.VoucherID == null)
                        {
                            throw new InvalidOperationException("尚未注册");
                        }
                    }
                    else
                    {
                        if (item.VoucherID != null)
                        {
                            throw new InvalidOperationException("注册过多");
                        }
                    }
                }

                var id = asset.Schedule.FindIndex(it => it.Date >= voucher.Date);
                if (id != -1)
                {
                    asset.Schedule.RemoveRange(id, asset.Schedule.Count - id);
                }

                asset.Schedule.Add(new DispositionItem {
                    Date = voucher.Date, VoucherID = voucher.ID
                });
                sb.Append(serializer.PresentAsset(asset).Wrap());
                Accountant.Upsert(asset);
            }

            if (sb.Length > 0)
            {
                return(new DirtyText(sb.ToString()));
            }

            return(new PlainSucceed());
        }