public async Task <int> Add(ExportStockModel _model)
        {
            if (_model == null || _model.IssueDocket == null || _model.DocketDetails == null)
            {
                scopeContext.AddError("Lỗi dữ liệu đầu vào");
                return(0);
            }
            if (_model.IssueDocket.StockIssueDocketTypeId <= 0)
            {
                scopeContext.AddError("Chưa chọn loại phiếu nhập");
                return(0);
            }
            if (_model.IssueDocket.WarehouseId <= 0)
            {
                scopeContext.AddError("Chưa chọn kho nhập");
                return(0);
            }
            // lấy loại phiếu thu
            var issueDocketType = await svcStockIssueDocketType.GetDetail(_model.IssueDocket.StockIssueDocketTypeId);

            var receiptType = 0;

            if (issueDocketType != null && issueDocketType.ReceiptNeeded)
            {
                receiptType = issueDocketType.ReceiptTypeId;
            }

            // bắt đầu tạo phiếu
            using (var transaction = context.Database.BeginTransaction())
            {
                StockIssueDocket issueDocket = iMapper.Map <StockIssueDocket>(_model.IssueDocket);
                issueDocket.CustomerId   = _model.Receipt.PartnerId;
                issueDocket.CustomerName = _model.Receipt.PartnerName;
                ExpenditureDocket receipt = null;
                if (receiptType > 0)
                {
                    receipt             = iMapper.Map <ExpenditureDocket>(_model.Receipt);
                    receipt.Amount      = issueDocket.Amount;
                    receipt.Vat         = issueDocket.Vat;
                    receipt.WarehouseId = issueDocket.WarehouseId;
                    receipt.CreatedUser = scopeContext.UserCode;
                    receipt.IsReceipt   = true;
                }
                List <StockIssueDocketDetail>  docketDetails = iMapper.Map <List <StockIssueDocketDetail> >(_model.DocketDetails);
                List <ExpenditureDocketDetail> expendDetails = new List <ExpenditureDocketDetail>();
                decimal orderVAT         = 0;
                decimal orderAmount      = 0;
                decimal orderTotalAmount = 0;

                List <ProductInstockModel> productInstock = new List <ProductInstockModel>();
                foreach (var item in docketDetails)
                {
                    if (receiptType > 0)
                    {
                        item.Amount      = item.Quantity * item.UnitPrice;
                        item.Vat         = item.Amount * (item.VatPercent / (decimal)100);
                        item.TotalAmount = item.Amount + item.Vat;
                        ExpenditureDocketDetail exDetail = new ExpenditureDocketDetail
                        {
                            ExpenditureDocketId = receipt.Id,
                            Amount      = item.Amount,
                            Vat         = item.Vat,
                            TotalAmount = item.TotalAmount
                        };
                        expendDetails.Add(exDetail);
                        orderVAT         += item.Vat;
                        orderAmount      += item.Amount;
                        orderTotalAmount += item.TotalAmount;
                    }
                    else
                    {
                        item.UnitPrice   = 0;
                        item.Amount      = 0;
                        item.Vat         = 0;
                        item.TotalAmount = 0;
                    }

                    #region Trừ vào danh sách tồn - Tạm thời chưa chuyển đổi sang số lượng theo đơn vị tính chuẩn
                    var idx = productInstock.FindIndex(p => p.ProductId == item.ProductId);
                    if (idx >= 0)
                    {
                        productInstock[idx].Quantity += item.Quantity;
                    }
                    else
                    {
                        productInstock.Add(new ProductInstockModel()
                        {
                            ProductId     = item.ProductId,
                            ProductName   = "",
                            Quantity      = item.Quantity,
                            ProductUnitId = item.ProductUnitId
                        });
                    }
                    #endregion
                }
                // insert
                if (receiptType > 0)
                {
                    issueDocket.Vat         = orderVAT;
                    issueDocket.Amount      = orderAmount;
                    issueDocket.TotalAmount = orderTotalAmount;
                    issueDocket.Id          = await svcStockIssueDocket.Add(issueDocket);

                    receipt.Vat           = orderVAT;
                    receipt.Amount        = orderAmount;
                    receipt.TotalAmount   = orderTotalAmount;
                    receipt.StockDocketId = issueDocket.Id;
                    receipt.Id            = await svcExpenditureDocket.Add(receipt);
                }
                else
                {
                    issueDocket.Vat         = 0;
                    issueDocket.Amount      = 0;
                    issueDocket.TotalAmount = 0;
                    issueDocket.Id          = await svcStockIssueDocket.Add(issueDocket);
                }
                foreach (var item in docketDetails)
                {
                    item.StockIssueDocketId = issueDocket.Id;
                    item.Id = await svcStockIssueDocketDetail.Add(item);
                }
                foreach (var item in expendDetails)
                {
                    item.ExpenditureDocketId = receipt.Id;
                    item.Id = await svcExpenditureDocketDetail.Add(item);
                }

                #region Cập nhật vào bảng tồn
                foreach (var current in productInstock)
                {
                    var instock = await svcCurrentInStock.GetList(issueDocket.WarehouseId, current.ProductId);

                    if (instock == null || instock.Count == 0)
                    {
                        CurrentInStock cis = new CurrentInStock()
                        {
                            Amount        = 0 - current.Quantity,
                            AmountExpect  = 0 - current.Quantity,
                            ProductId     = current.ProductId,
                            ProductUnitId = current.ProductUnitId,
                            WarehouseId   = issueDocket.WarehouseId
                        };
                        cis.Id = await svcCurrentInStock.Add(cis);
                    }
                    else
                    {
                        CurrentInStock cis = instock[0];
                        cis.Amount       -= current.Quantity;
                        cis.AmountExpect -= current.Quantity;
                        await svcCurrentInStock.Modify(cis);
                    }
                }
                #endregion
                transaction.Commit();
                return(issueDocket.Id);
            }
        }
Пример #2
0
        public async Task <int> FeedingLivestock(FeedingLivestockModel _model)
        {
            if (_model.FishPondWarehouseId <= 0)
            {
                scopeContext.AddError("Chưa chọn ao cho ăn");
                return(0);
            }
            // dữ liệu ao
            var thisFishPond = await svcFishPond.GetByWarehouseId(_model.FishPondWarehouseId);

            if (thisFishPond == null)
            {
                scopeContext.AddError("Lỗi dữ liệu kho-ao " + _model.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.FishPondWarehouseId);

            if (thisFishPondWarehouse == null || thisFishPondWarehouse.DefaultWarehouseId <= 0)
            {
                scopeContext.AddError("Lỗi dữ liệu kho mặc định cho ao");
                return(0);
            }
            _model.FeedDate = _model.FeedDate.GetValueOrDefault(DateTime.UtcNow);
            // bắt đầu tạo phiếu
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    // Phiếu xuất kho mặc định
                    StockIssueDocket issueDocket = new StockIssueDocket();
                    issueDocket.CustomerId             = _model.FishPondWarehouseId;
                    issueDocket.CustomerName           = thisFishPond.Name;
                    issueDocket.Description            = "Cho ăn";
                    issueDocket.ExecutedDate           = _model.FeedDate.Value;
                    issueDocket.ExecutorCode           = scopeContext.UserCode;
                    issueDocket.IssueDate              = _model.FeedDate;
                    issueDocket.StockIssueDocketTypeId = (int)SystemIDEnum.FeedingLivestock_IssueType;
                    issueDocket.WarehouseId            = thisFishPondWarehouse.DefaultWarehouseId;
                    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.FeedDate.Value;
                    history.ActionType      = (int)SystemIDEnum.FarmingSeason_ActionType_Feed;
                    history.Description     = "Cho ăn";
                    history.FarmingSeasonId = thisFarmingSeason.Id;
                    history.Id = await svcFarmingSeasonHistory.Add(history);

                    // Chi tiết phiếu xuất kho mặc định
                    List <StockIssueDocketDetail> docketDetails = iMapper.Map <List <StockIssueDocketDetail> >(_model.Details);
                    decimal orderVAT         = 0;
                    decimal orderAmount      = 0;
                    decimal orderTotalAmount = 0;
                    foreach (var item in docketDetails)
                    {
                        item.StockIssueDocketId = issueDocket.Id;
                        item.UnitPrice          = 0;
                        item.Amount             = item.Quantity * item.UnitPrice;
                        item.Vat         = item.Amount * (item.VatPercent / (decimal)100);
                        item.TotalAmount = item.Amount + item.Vat;
                        item.Id          = await svcStockIssueDocketDetail.Add(item);

                        orderVAT         += item.Vat;
                        orderAmount      += item.Amount;
                        orderTotalAmount += item.TotalAmount;

                        #region Trừ vào danh sách tồn - Tạm thời chưa chuyển đổi sang số lượng theo đơn vị tính chuẩn
                        var instock = await svcCurrentInStock.GetList(issueDocket.WarehouseId, item.ProductId);

                        if (instock == null || instock.Count == 0)
                        {
                            CurrentInStock cis = new CurrentInStock()
                            {
                                Amount        = 0 - item.Quantity,
                                AmountExpect  = 0 - item.Quantity,
                                ProductId     = item.ProductId,
                                ProductUnitId = item.ProductUnitId,
                                WarehouseId   = issueDocket.WarehouseId
                            };
                            cis.Id = await svcCurrentInStock.Add(cis);
                        }
                        else
                        {
                            CurrentInStock cis = instock[0];
                            cis.Amount       -= item.Quantity;
                            cis.AmountExpect -= item.Quantity;
                            await svcCurrentInStock.Modify(cis);
                        }
                        #endregion
                        // Lịch sử hàng hóa
                        StockHistoryDetail historyDetail = new StockHistoryDetail();
                        historyDetail.HistoryId     = history.Id;
                        historyDetail.ProductId     = item.ProductId;
                        historyDetail.Amount        = item.Quantity;
                        historyDetail.ProductUnitId = item.ProductUnitId; // kg
                        historyDetail.Id            = await svcStockHistoryDetail.Add(historyDetail);
                    }
                    issueDocket.Vat         = orderVAT;
                    issueDocket.Amount      = orderAmount;
                    issueDocket.TotalAmount = orderTotalAmount;
                    await svcStockIssueDocket.Modify(issueDocket);

                    transaction.Commit();
                    return(issueDocket.Id);
                }
                catch
                {
                    transaction.Rollback();
                    return(0);
                }
            }
        }
        public async Task <int> Add(ImportStockModel _model)
        {
            if (_model == null || _model.ReceiveDocket == null || _model.Suppliers == null)
            {
                scopeContext.AddError("Lỗi dữ liệu đầu vào");
                return(0);
            }
            if (_model.ReceiveDocket.StockReceiveDocketTypeId <= 0)
            {
                scopeContext.AddError("Chưa chọn loại phiếu nhập");
                return(0);
            }
            if (_model.ReceiveDocket.WarehouseId <= 0)
            {
                scopeContext.AddError("Chưa chọn kho nhập");
                return(0);
            }
            // lấy loại phiếu chi
            var receiveDocketType = await svcStockReceiveDocketType.GetDetail(_model.ReceiveDocket.StockReceiveDocketTypeId);

            var paySlipType = 0;

            if (receiveDocketType != null && receiveDocketType.PayslipNeeded)
            {
                paySlipType = receiveDocketType.PayslipTypeId;
            }
            // bắt đầu tạo phiếu
            using (var transaction = context.Database.BeginTransaction())
            {
                try
                {
                    StockReceiveDocket docket = iMapper.Map <StockReceiveDocket>(_model.ReceiveDocket);
                    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);

                    decimal orderVAT         = 0;
                    decimal orderAmount      = 0;
                    decimal orderTotalAmount = 0;
                    List <ProductInstockModel> productInstock = new List <ProductInstockModel>();
                    foreach (ImportStockSupplierModel item in _model.Suppliers)
                    {
                        ExpenditureDocket exp = null;
                        if (paySlipType > 0)
                        {
                            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           = 0;
                            exp.TotalAmount      = 0;
                            exp.Vat = 0;
                            exp.Id  = await svcExpenditureDocket.Add(exp);
                        }
                        decimal supplierAmount      = 0;
                        decimal supplierTotalAmount = 0;
                        decimal supplierVat         = 0;
                        foreach (StockReceiveDocketDetailModel i in item.ReceiveDocketDetails)
                        {
                            StockReceiveDocketDetail docketDetail = iMapper.Map <StockReceiveDocketDetail>(i);
                            docketDetail.StockReceiveDocketId = docket.Id;
                            docketDetail.SupplierBranchId     = item.SupplierBranchId;
                            docketDetail.SupplierBranchName   = item.SupplierBranchName;
                            if (paySlipType > 0)
                            {
                                docketDetail.Amount      = i.Quantity * i.UnitPrice;
                                docketDetail.Vat         = docketDetail.Amount * (i.VatPercent / (decimal)100);
                                docketDetail.TotalAmount = docketDetail.Amount + docketDetail.Vat;
                            }
                            else
                            {
                                docketDetail.UnitPrice   = 0;
                                docketDetail.Amount      = 0;
                                docketDetail.Vat         = 0;
                                docketDetail.TotalAmount = 0;
                            }
                            docketDetail.Id = await svcStockReceiveDocketDetail.Add(docketDetail);

                            if (paySlipType > 0)
                            {
                                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 = paySlipType;
                                eD.Id = await svcExpenditureDocketDetail.Add(eD);
                            }
                            supplierAmount      += docketDetail.Amount;
                            supplierTotalAmount += docketDetail.TotalAmount;
                            supplierVat         += docketDetail.Vat;

                            #region Thêm vào danh sách tồn - Tạm thời chưa chuyển đổi sang số lượng theo đơn vị tính chuẩn
                            var idx = productInstock.FindIndex(p => p.ProductId == i.ProductId);
                            if (idx >= 0)
                            {
                                productInstock[idx].Quantity += i.Quantity;
                            }
                            else
                            {
                                productInstock.Add(new ProductInstockModel()
                                {
                                    ProductId     = i.ProductId,
                                    ProductName   = i.ProductName,
                                    Quantity      = i.Quantity,
                                    ProductUnitId = i.ProductUnitId
                                });
                            }
                            #endregion
                        }
                        if (paySlipType > 0)
                        {
                            exp.Amount      += supplierAmount;
                            exp.TotalAmount += supplierTotalAmount;
                            exp.Vat         += supplierVat;
                            await svcExpenditureDocket.Modify(exp);
                        }
                        orderVAT         += supplierVat;
                        orderAmount      += supplierAmount;
                        orderTotalAmount += supplierTotalAmount;
                    }
                    // 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);

                    #region Cập nhật vào bảng tồn
                    foreach (var current in productInstock)
                    {
                        var instock = await svcCurrentInStock.GetList(docket.WarehouseId, current.ProductId);

                        if (instock == null || instock.Count == 0)
                        {
                            CurrentInStock cis = new CurrentInStock()
                            {
                                Amount        = docket.IsActuallyReceived == true ? current.Quantity : 0,
                                AmountExpect  = current.Quantity,
                                ProductId     = current.ProductId,
                                ProductUnitId = current.ProductUnitId,
                                WarehouseId   = docket.WarehouseId
                            };
                            cis.Id = await svcCurrentInStock.Add(cis);
                        }
                        else
                        {
                            CurrentInStock cis = instock[0];
                            if (docket.IsActuallyReceived == true)
                            {
                                cis.Amount += current.Quantity;
                            }
                            cis.AmountExpect += current.Quantity;
                            await svcCurrentInStock.Modify(cis);
                        }
                    }
                    #endregion

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