Пример #1
0
        public async Task <int> Add(FishPondModel _model)
        {
            if (_model.DefaultWarehouseId.GetValueOrDefault(0) == 0)
            {
                scopeContext.AddError("Chưa chọn kho");
                return(0);
            }
            if (_model.FarmRegionId == 0)
            {
                scopeContext.AddError("Chưa chọn vùng nuôi");
                return(0);
            }
            using (var transaction = context.Database.BeginTransaction())
            {
                // create new fish-pond-type warehouse
                Warehouse warehouse = new Warehouse()
                {
                    DefaultWarehouseId = _model.DefaultWarehouseId.Value,
                    FarmRegionId       = _model.FarmRegionId,
                    Name            = FISHPONDTYPE_WAREHOUSE_PREFIX + _model.Name,
                    WarehouseTypeId = DEFAULT_FISHPONDTYPE_WAREHOUSE_TYPE
                };
                var warehouseId = await svcWarehouse.Add(warehouse);

                FishPond entity = iMapper.Map <FishPond>(_model);
                entity.WarehouseId = warehouseId;
                entity.Id          = await svcFishPond.Add(entity);

                transaction.Commit();
                return(entity.Id);
            }
        }
        public async Task <ExportStockDetailModel> GetDetail(int _id)
        {
            ExportStockDetailModel result      = new ExportStockDetailModel();
            StockIssueDocketModel  issueDocket = iMapper.Map <StockIssueDocketModel>(await svcStockIssueDocket.GetDetail(_id));

            if (issueDocket == null)
            {
                scopeContext.AddError("Mã phiếu xuất không tồn tại");
                return(null);
            }
            result.IssueDocket = issueDocket;
            List <StockIssueDocketDetailModel> details = iMapper.Map <List <StockIssueDocketDetailModel> >(await svcStockIssueDocketDetail.GetDetailsByDocketId(_id));

            result.Details = details;
            return(result);
        }
        public async Task <ImportStockDetailModel> GetDetail(int _id)
        {
            ImportStockDetailModel  result        = new ImportStockDetailModel();
            StockReceiveDocketModel receiveDocket = iMapper.Map <StockReceiveDocketModel>(await svcStockReceiveDocket.GetDetail(_id));

            if (receiveDocket == null)
            {
                scopeContext.AddError("Mã phiếu nhập không tồn tại");
                return(null);
            }
            result.ReceiveDocket = receiveDocket;
            List <StockReceiveDocketDetailModel> details = iMapper.Map <List <StockReceiveDocketDetailModel> >(await svcStockReceiveDocketDetail.GetDetailsByDocketId(_id));

            result.Details = details;
            return(result);
        }
Пример #4
0
 public async Task <List <ReportFarmingSeasonHistoryStock> > FarmingSeasonHistoryStock(ReportFarmingSeasonHistoryStockRequest _request)
 {
     if (_request.FarmingSeasonId <= 0)
     {
         scopeContext.AddError("Chưa chọn đợt nuôi");
         return(null);
     }
     return(await svcReport.FarmingSeasonHistoryStock(_request));
 }
Пример #5
0
        public async Task <Product> GetDetail(int _id)
        {
            var item = await context.Product.FirstOrDefaultAsync(x => x.Id == _id && x.IsDeleted == false);

            if (item == null)
            {
                scopeContext.AddError("Mã sản phẩm không tồn tại");
                return(null);
            }
            return(item);
        }
Пример #6
0
        public async Task <List <CurrentInStock> > GetList(int warehouseId, int productId = 0)
        {
            if (warehouseId <= 0)
            {
                scopeContext.AddError("Lỗi dữ kiệu đầu vào");
                return(null);
            }
            var items = await context.CurrentInStock.Where(i =>
                                                           i.WarehouseId == warehouseId &&
                                                           (productId == 0 || i.ProductId == productId)
                                                           ).ToListAsync();

            return(items);
        }
Пример #7
0
        public async Task <int> ReleaseLivestock(ReleaseLivestockModel _model)
        {
            if (_model == null || _model.LivestockDocket == null || _model.Livestock == null || _model.Suppliers == null)
            {
                scopeContext.AddError("Lỗi dữ liệu đầu vào");
                return(0);
            }
            if (_model.LivestockDocket.FishPondWarehouseId <= 0)
            {
                scopeContext.AddError("Chưa chọn ao thả giống");
                return(0);
            }
            // dữ liệu ao
            var thisFishPond = await svcFishPond.GetByWarehouseId(_model.LivestockDocket.FishPondWarehouseId);

            if (thisFishPond == null)
            {
                scopeContext.AddError("Lỗi dữ liệu kho-ao " + _model.LivestockDocket.FishPondWarehouseId);
                return(0);
            }
            // đợt nuôi
            var thisFarmingSeason = await svcFarmingSeason.GetByFishPondId(thisFishPond.Id);

            if (thisFarmingSeason == null)
            {
                scopeContext.AddError("Ao này chưa vào đợt nuôi");
                return(0);
            }
            // dữ liệu kho-ao
            var thisFishPondWarehouse = await svcWarehouse.GetDetail(_model.LivestockDocket.FishPondWarehouseId);

            if (thisFishPondWarehouse == null || thisFishPondWarehouse.DefaultWarehouseId <= 0)
            {
                scopeContext.AddError("Lỗi dữ liệu kho mặc định cho ao");
                return(0);
            }
            // bắt đầu tạo phiếu
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    // Phiếu nhập kho mặc định
                    StockReceiveDocket docket = new StockReceiveDocket();
                    docket.Description              = _model.LivestockDocket.Description;
                    docket.IsActuallyReceived       = _model.LivestockDocket.IsActuallyReceived;
                    docket.ReceiveDate              = _model.LivestockDocket.ReceiveDate;
                    docket.StockReceiveDocketTypeId = (int)SystemIDEnum.ReleaseLiveStock_ReceiveType;
                    docket.WarehouseId              = thisFishPondWarehouse.DefaultWarehouseId;
                    docket.ExecutorCode             = scopeContext.UserCode;
                    docket.ExecutedDate             = DateTime.UtcNow;
                    if (docket.IsActuallyReceived.GetValueOrDefault(false))
                    {
                        docket.ActuallyReceivedDate = docket.ExecutedDate;
                        docket.ActuallyReceivedCode = docket.ExecutorCode;
                    }
                    docket.Id = await svcStockReceiveDocket.Add(docket);

                    // Phiếu xuất kho mặc định
                    StockIssueDocket issueDocket = new StockIssueDocket();
                    issueDocket.CustomerId             = thisFishPondWarehouse.Id;
                    issueDocket.CustomerName           = thisFishPond.Name;
                    issueDocket.Description            = "Thả cá giống";
                    issueDocket.ExecutorCode           = scopeContext.UserCode;
                    issueDocket.ExecutedDate           = DateTime.UtcNow;
                    issueDocket.IssueDate              = _model.LivestockDocket.ReceiveDate;
                    issueDocket.StockIssueDocketTypeId = (int)SystemIDEnum.ReleaseLiveStock_IssueType;
                    issueDocket.StockReceiveDocketId   = docket.Id;
                    issueDocket.WarehouseId            = docket.WarehouseId;
                    issueDocket.Id = await svcStockIssueDocket.Add(issueDocket);

                    // Lịch sử đợt nuôi (master lịch sử ao nuôi)
                    FarmingSeasonHistory history = new FarmingSeasonHistory();
                    history.ActionDate      = _model.LivestockDocket.ReceiveDate.GetValueOrDefault(DateTime.UtcNow);
                    history.ActionType      = (int)SystemIDEnum.FarmingSeason_ActionType_Release;
                    history.Description     = "Nhập thả con giống";
                    history.FarmingSeasonId = thisFarmingSeason.Id;
                    history.Id = await svcFarmingSeasonHistory.Add(history);

                    decimal orderVAT            = 0;
                    decimal orderAmount         = 0;
                    decimal orderTotalAmount    = 0;
                    decimal livestockMassAmount = 0;
                    decimal livestockQuantity   = 0;
                    //List<FeedConversionRate> fcrList = new List<FeedConversionRate>();
                    foreach (ReleaseStockSupplierModel item in _model.Suppliers)
                    {
                        // Chi tiết phiếu nhập
                        StockReceiveDocketDetail docketDetail = new StockReceiveDocketDetail();
                        docketDetail.ProductId            = _model.Livestock.Id;
                        docketDetail.ProductUnitId        = _model.Livestock.DefaultUnitId;
                        docketDetail.Quantity             = item.MassAmount;
                        docketDetail.UnitPrice            = item.PricePerKg;
                        docketDetail.VatPercent           = _model.Livestock.TaxPercent;
                        docketDetail.StockReceiveDocketId = docket.Id;
                        docketDetail.SupplierBranchId     = item.SupplierBranchId;
                        docketDetail.SupplierBranchName   = item.SupplierBranchName;
                        docketDetail.Amount      = item.MassAmount * item.PricePerKg;
                        docketDetail.Vat         = docketDetail.Amount * (_model.Livestock.TaxPercent / (decimal)100);
                        docketDetail.TotalAmount = docketDetail.Amount + docketDetail.Vat;
                        docketDetail.Id          = await svcStockReceiveDocketDetail.Add(docketDetail);

                        // Chi tiết phiếu xuất
                        StockIssueDocketDetail issueDocketDetail = new StockIssueDocketDetail();
                        issueDocketDetail.StockIssueDocketId = issueDocket.Id;
                        issueDocketDetail.ProductId          = docketDetail.ProductId;
                        issueDocketDetail.ProductUnitId      = docketDetail.ProductUnitId;
                        issueDocketDetail.Quantity           = docketDetail.Quantity;
                        issueDocketDetail.UnitPrice          = docketDetail.UnitPrice;
                        issueDocketDetail.VatPercent         = docketDetail.VatPercent;
                        issueDocketDetail.Amount             = docketDetail.Amount;
                        issueDocketDetail.Vat         = docketDetail.Vat;
                        issueDocketDetail.TotalAmount = docketDetail.TotalAmount;
                        issueDocketDetail.Id          = await svcStockIssueDocketDetail.Add(issueDocketDetail);

                        ExpenditureDocket exp = null;
                        exp = new ExpenditureDocket();
                        exp.StockDocketId    = docket.Id;
                        exp.PartnerId        = item.SupplierBranchId;
                        exp.PartnerName      = item.SupplierBranchName;
                        exp.WarehouseId      = docket.WarehouseId;
                        exp.BillCode         = item.BillCode;
                        exp.BillSerial       = item.BillSerial;
                        exp.BillTemplateCode = item.BillTemplateCode;
                        exp.BillDate         = item.BillDate;
                        exp.CreatedUser      = scopeContext.UserCode;
                        exp.IsReceipt        = false;
                        exp.ExpendDate       = docket.ExecutedDate;
                        exp.Amount           = docketDetail.Amount;
                        exp.TotalAmount      = docketDetail.TotalAmount;
                        exp.Vat = docketDetail.Vat;
                        exp.Id  = await svcExpenditureDocket.Add(exp);

                        // Chi tiết phiếu chi
                        ExpenditureDocketDetail eD = new ExpenditureDocketDetail();
                        eD.ExpenditureDocketId = exp.Id;
                        eD.VatPercent          = docketDetail.VatPercent;
                        eD.Amount            = docketDetail.Amount;
                        eD.Vat               = docketDetail.Vat;
                        eD.TotalAmount       = docketDetail.TotalAmount;
                        eD.ProductId         = docketDetail.ProductId;
                        eD.ExpenditureTypeId = (int)SystemIDEnum.ReleaseLiveStock_ExpenditureType;
                        eD.Id = await svcExpenditureDocketDetail.Add(eD);

                        // Lịch sử con giống (detail lịch sử ao nuôi)
                        LivestockHistoryDetail historyDetail = new LivestockHistoryDetail();
                        historyDetail.HistoryId     = history.Id;
                        historyDetail.LivestockId   = _model.Livestock.Id;
                        historyDetail.Quantity      = item.Quantity;
                        historyDetail.MassAmount    = item.MassAmount;                                            // kg
                        historyDetail.LivestockSize = item.Size;                                                  // con/kg = (MassAmount / Quantity) * 1000
                        historyDetail.Weight        = 1000 / (historyDetail.Quantity / historyDetail.MassAmount); // gram/con
                        historyDetail.Id            = await svcLivestockHistoryDetail.Add(historyDetail);

                        orderVAT            += docketDetail.Vat;
                        orderAmount         += docketDetail.Amount;
                        orderTotalAmount    += docketDetail.TotalAmount;
                        livestockMassAmount += item.MassAmount;
                        livestockQuantity   += item.Quantity;
                    }
                    // cập nhật phiếu xuất
                    issueDocket.Vat         = orderVAT;
                    issueDocket.Amount      = orderAmount;
                    issueDocket.TotalAmount = orderTotalAmount;
                    await svcStockIssueDocket.Modify(issueDocket);

                    //Tỷ lệ tăng trọng
                    FeedConversionRate fcr = new FeedConversionRate();
                    fcr.FarmingSeasonId = thisFarmingSeason.Id;
                    fcr.IsAuto          = false;
                    fcr.ProductId       = _model.Livestock.Id;
                    fcr.SurveyDate      = _model.LivestockDocket.ReceiveDate.GetValueOrDefault(DateTime.UtcNow);
                    fcr.Quantity        = livestockQuantity;                      // số lượng cá
                    fcr.MassAmount      = livestockMassAmount;                    // tổng kg cá
                    fcr.Weight          = 1000 / (fcr.Quantity / fcr.MassAmount); // gram/con
                    fcr.ProductName     = _model.Livestock.Name;
                    fcr.Id = await svcFeedConversionRate.Add(fcr);

                    // nếu có chi phí phát sinh, tạo phiếu chi
                    if (_model.PaySlipDetails != null && _model.PaySlipDetails.Count > 0)
                    {
                        ExpenditureDocket expendDocket = new ExpenditureDocket();
                        expendDocket.StockDocketId = docket.Id;
                        expendDocket.WarehouseId   = docket.WarehouseId;
                        expendDocket.CreatedUser   = scopeContext.UserCode;
                        expendDocket.ExpendDate    = docket.ExecutedDate;
                        expendDocket.Amount        = 0;
                        expendDocket.TotalAmount   = 0;
                        expendDocket.Vat           = 0;
                        expendDocket.Id            = await svcExpenditureDocket.Add(expendDocket);

                        foreach (ExpenditureDocketDetailModel item in _model.PaySlipDetails)
                        {
                            ExpenditureDocketDetail eD = iMapper.Map <ExpenditureDocketDetail>(item);
                            eD.ExpenditureDocketId = expendDocket.Id;
                            eD.Id = await svcExpenditureDocketDetail.Add(eD);

                            expendDocket.Amount      += eD.Amount;
                            expendDocket.TotalAmount += eD.TotalAmount;
                            expendDocket.Vat         += eD.Vat;
                        }
                        await svcExpenditureDocket.Modify(expendDocket);

                        orderVAT         += expendDocket.Vat;
                        orderAmount      += expendDocket.Amount;
                        orderTotalAmount += expendDocket.TotalAmount;
                    }
                    // cập nhật phiếu nhập
                    docket.Vat         = orderVAT;
                    docket.Amount      = orderAmount;
                    docket.TotalAmount = orderTotalAmount;
                    await svcStockReceiveDocket.Modify(docket);

                    transaction.Commit();
                    return(docket.Id);
                }
                catch
                {
                    transaction.Rollback();
                    return(0);
                }
            }
        }