示例#1
0
 public InventorySummaryReportController(IIdentityService identityService, IValidateService validateService, IInventorySummaryService service)
 {
     IdentityService = identityService;
     ValidateService = validateService;
     Service         = service;
     ApiVersion      = "1.0.0";
 }
        public async Task <int> Create(InventoryMovement model)
        {
            int Created = 0;

            var internalTransaction = DbContext.Database.CurrentTransaction == null;
            var transaction         = !internalTransaction ? DbContext.Database.CurrentTransaction : DbContext.Database.BeginTransaction();

            try
            {
                model.No = GenerateNo(model);
                model.FlagForCreate(IdentityService.Username, UserAgent);
                model.FlagForUpdate(IdentityService.Username, UserAgent);
                IInventorySummaryService summary = ServiceProvider.GetService <IInventorySummaryService>();

                this.DbSet.Add(model);
                Created = await DbContext.SaveChangesAsync();

                //var SumQty = this.dbSet.Where(a => a._IsDeleted == false && a.StorageId == model.StorageId && a.ProductId == model.ProductId && a.UomId == model.UomId).Sum(a => a.Quantity);
                var SumQty = this.DbSet.OrderByDescending(a => a._CreatedUtc).FirstOrDefault(a => a._IsDeleted == false && a.StorageId == model.StorageId && a.ProductId == model.ProductId && a.UomId == model.UomId);

                var SumStock = this.DbSet.Where(a => a._IsDeleted == false && a.StorageId == model.StorageId && a.ProductId == model.ProductId && a.UomId == model.UomId).Sum(a => a.StockPlanning);
                InventorySummary summaryModel = new InventorySummary
                {
                    ProductId     = model.ProductId,
                    ProductCode   = model.ProductCode,
                    ProductName   = model.ProductName,
                    UomId         = model.UomId,
                    UomUnit       = model.UomUnit,
                    StockPlanning = SumStock,
                    Quantity      = SumQty.After,
                    StorageId     = model.StorageId,
                    StorageCode   = model.StorageCode,
                    StorageName   = model.StorageName
                };
                await summary.Create(summaryModel);

                if (internalTransaction)
                {
                    transaction.Commit();
                }

                return(Created);
            }
            catch (Exception e)
            {
                if (internalTransaction)
                {
                    transaction.Rollback();
                }
                throw new Exception(e.Message);
            }
        }