コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="purchaseReturn"></param>
        public void Add(PurchaseReturn purchaseReturn)
        {
            var flag = false;

            try
            {
                _unitOfWork.BeginTransaction();
                flag = true;
                var identity = (LoginIdentity)Thread.CurrentPrincipal.Identity;
                #region PurchaseReturn
                purchaseReturn.Id                  = GenerateAutoId(identity.CompanyId, identity.BranchId, "PurchaseReturn");
                purchaseReturn.Sequence            = GetAutoSequence();
                purchaseReturn.PurchaseReturnDate  = DateTime.Now;
                purchaseReturn.Active              = true;
                purchaseReturn.SynchronizationType = SynchronizationType.Server.ToString();
                purchaseReturn.CompanyId           = identity.CompanyId;
                purchaseReturn.BranchId            = identity.BranchId;
                purchaseReturn.AddedBy             = identity.Name;
                purchaseReturn.AddedDate           = DateTime.Now;
                purchaseReturn.AddedFromIp         = identity.IpAddress;
                _purchaseReturnRepository.Add(purchaseReturn);
                #endregion

                #region PurchaseReturn Products
                if (purchaseReturn.PurchaseReturnDetails != null)
                {
                    var sqnc     = GetAutoSequence("PurchaseReturnDetail");
                    int detailId = Convert.ToInt32(GenerateAutoId(purchaseReturn.CompanyId, purchaseReturn.BranchId, "PurchaseReturnDetail"));
                    foreach (var purchaseReturnDetail in purchaseReturn.PurchaseReturnDetails)
                    {
                        var productDb      = _productRepository.GetOne(x => x.Id == purchaseReturnDetail.ProductId);
                        var purchaseDetail = _purchaseDetailRepository.GetOne(r => r.Id == purchaseReturnDetail.PurchaseDetailId);
                        #region Purchase return detail
                        purchaseReturnDetail.Id               = detailId.ToString();
                        purchaseReturnDetail.Sequence         = sqnc;
                        purchaseReturnDetail.PurchaseReturnId = purchaseReturn.Id;
                        decimal alreadyReturnQty = GetAlreadyReturnQty(purchaseReturnDetail.PurchaseId, purchaseReturnDetail.ProductId);
                        purchaseReturnDetail.RemainingQuantity = purchaseReturnDetail.PurchaseQuantity - (alreadyReturnQty + purchaseReturnDetail.ReturnQuantity);
                        purchaseDetail.Returned = purchaseDetail.Quantity == alreadyReturnQty + purchaseReturnDetail.ReturnQuantity;
                        purchaseReturnDetail.ProductCategoryId           = productDb.ProductCategoryId;
                        purchaseReturnDetail.ProductSubCategoryId        = productDb.ProductSubCategoryId;
                        purchaseReturnDetail.ProductSubsidiaryCategoryId = productDb.ProductSubsidiaryCategoryId;
                        purchaseReturnDetail.UOMId   = productDb.UOMId;
                        purchaseReturnDetail.BrandId = productDb.BrandId;
                        purchaseReturnDetail.GradeId = productDb.GradeId;
                        purchaseReturnDetail.PurchaseReturnDetailDate = DateTime.Now;
                        purchaseReturnDetail.CompanyId           = identity.CompanyId;
                        purchaseReturnDetail.BranchId            = identity.BranchId;
                        purchaseReturnDetail.Active              = true;
                        purchaseReturnDetail.SynchronizationType = SynchronizationType.Server.ToString();
                        purchaseReturnDetail.AddedBy             = identity.Name;
                        purchaseReturnDetail.AddedDate           = DateTime.Now;
                        purchaseReturnDetail.AddedFromIp         = identity.IpAddress;
                        _purchaseReturnDetailRepository.Add(purchaseReturnDetail);
                        detailId++;
                        sqnc++;
                        #endregion
                    }
                }
                #endregion
                #region Supplier Ledger
                var supplier = _supplierRepository.GetOne(x => x.Id == purchaseReturn.SupplierId);
                if (!string.IsNullOrEmpty(supplier?.Id))
                {
                    var supplierLedger = new SupplierLedger
                    {
                        Id                  = GenerateAutoId(identity.CompanyId, identity.BranchId, "SupplierLedger"),
                        Sequence            = GetAutoSequence("SupplierLedger"),
                        CompanyId           = identity.CompanyId,
                        BranchId            = identity.BranchId,
                        PurchaseId          = purchaseReturn.Id,
                        TrackingNo          = GenerateTrackingNo(identity.CompanyId, identity.BranchId, "SupplierLedger"),
                        SupplierId          = supplier.Id,
                        SupplierPhone       = supplier.Phone1,
                        Particulars         = TransactionType.PurchaseReturn.ToString(),
                        TransactionType     = TransactionType.PurchaseReturn.ToString(),
                        TransactionDate     = DateTime.Now,
                        DebitAmount         = purchaseReturn.TotalAmount,
                        CreditAmount        = 0,
                        RunningBalance      = 0,
                        Active              = true,
                        SynchronizationType = SynchronizationType.Server.ToString(),
                        AddedBy             = identity.Name,
                        AddedDate           = DateTime.Now,
                        AddedFromIp         = identity.IpAddress
                    };
                    _supplierLedgerRepository.Add(supplierLedger);
                }
                #endregion
                _unitOfWork.SaveChanges();
                ChangePurchaseStatus(purchaseReturn.PurchaseId);
                ChangePurchaseReturnStatus(purchaseReturn.Id);
                flag = false;
                _unitOfWork.Commit();
                _rawSqlService.UpdateSupplierLedgerRunningBalance(supplier?.Id);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (flag)
                {
                    _unitOfWork.Rollback();
                }
            }
        }