Пример #1
0
        /// <summary>
        /// This method is used for end receiving SPO in database. - JJ
        /// </summary>
        /// <param name="SPOReceivingAC"> object of SPOReceivingAC</param>
        /// <param name="roleName">user's role</param>
        /// <param name="userName">user's name</param>
        /// <returns>status</returns>
        public bool EndReceiving(SPOReceivingAC SPOReceivingAC, string roleName, string userName)
        {
            try
            {
                var spo = _supplierPOContext.Find(SPOReceivingAC.PurchaseOrderId);
                if (spo != null)
                {
                    spo.IsReceived       = true;
                    spo.IsVerified       = false;
                    spo.UpdatedDate      = DateTime.UtcNow;
                    spo.ModifiedDateTime = DateTime.UtcNow;
                    _supplierPOContext.Update(spo);
                    _supplierPOContext.SaveChanges();
                    if (SPOReceivingAC.Comment == ".")
                    {
                        SPOReceivingAC.Comment = "";
                    }
                    _supplierPOWorkListRepository.SaveSupplierPurchaseOrderLog(StringConstants.ReceiveAction, SPOReceivingAC.Comment, SPOReceivingAC.PurchaseOrderId, spo.RecordId, roleName, "" + roleName + " " + StringConstants.ReceiveSPO, userName);

                    UpdateItem(spo.Id);
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                _errorLog.LogException(ex);
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// This method is used for insert new supplier purchase order in database. - JJ
        /// </summary>
        /// <param name="supplierPO"> object of SupplierPOAC</param>
        /// <param name="company">object of Company</param>
        /// <param name="userName">current user's username</param>
        /// <returns>status</returns>
        public string SaveSupplierPO(SupplierPOAC supplierPO, string userName, CompanyDetail company)
        {
            try
            {
                var    log                 = new WorkFlowLog();
                var    currentUser         = _userDetailContext.First(x => x.UserName == userName && x.IsDelete == false);
                var    companyConfig       = _companyConfigurationContext.First(x => x.CompanyId == company.Id);
                var    sponumber           = companyConfig.SPOInvoiceNo;
                bool   IsApproved          = false;
                bool   IsConfirmed         = false;
                bool   IsCanceled          = false;
                bool   IsRejected          = false;
                bool   IsPartiallyReceived = false;
                string ponumber            = PurchaseOrderNumberGenerator(sponumber, 4);
                if (supplierPO.IsSubmitted)
                {
                    var workFlowLog = _IWorkFlowDetailsRepository.GetInitiationActionWorkFlow(StringConstants.SupplierPurchaseOrder, StringConstants.CreateSupplierPurchaseOrder, currentUser, company, supplierPO, supplierPO.InitiationComment, supplierPO);
                    if (workFlowLog != null)
                    {
                        log = (WorkFlowLog)workFlowLog.Item1;
                        supplierPO.ParentRecordId = log.RecordId;
                    }
                    else
                    {
                        return(StringConstants.WorkFlowNotCreated);
                    }
                    var activityWorkFlow = _workFlowContext.FirstOrDefault(x => x.Id == log.WorkFlowId);
                    if (!activityWorkFlow.NextActivity.IsClosed)
                    {
                        IsApproved = false;
                    }
                    else
                    {
                        IsApproved = true;
                    }
                }

                var  supplierType = _paramTypeContext.Fetch(x => x.Param.Key == StringConstants.SupplierType);
                bool isCredit     = false;
                foreach (var type in supplierType)
                {
                    if (type.Id == supplierPO.SupplierTypeId && type.ValueEn == StringConstants.Credit)
                    {
                        isCredit = true;
                    }
                    else
                    {
                        isCredit = false;
                    }
                }

                var supplierPurchaseOrder = new SupplierPurchaseOrder
                {
                    UserId              = currentUser.Id,
                    RecordId            = supplierPO.ParentRecordId,
                    SupplierId          = supplierPO.SupplierId,
                    InitiationBranchId  = supplierPO.InitiationBranchId,
                    IsApproved          = IsApproved,
                    IsConfirmed         = IsConfirmed,
                    IsNotProcessed      = true,
                    IsRejected          = IsRejected,
                    IsCanceled          = IsCanceled,
                    IsPartiallyReceived = IsPartiallyReceived,
                    IsSend              = false,
                    IsSubmitted         = supplierPO.IsSubmitted,
                    DueDate             = supplierPO.DueDate,
                    CreatedDateTime     = DateTime.UtcNow,
                    CreditDaysLimit     = supplierPO.SupplierDaysLimit,
                    IsCreditPayment     = isCredit,
                    UpdatedDate         = DateTime.UtcNow,
                    PurchaseOrderNumber = ponumber
                };
                _supplierPOContext.Add(supplierPurchaseOrder);
                _supplierPOContext.SaveChanges();

                if (supplierPO.IsSubmitted)
                {
                    _supplierPOWorkListContext.SaveSupplierPurchaseOrderLog(log.Action, supplierPO.InitiationComment, supplierPurchaseOrder.Id, log.RecordId, currentUser.RoleName, log.Stage, currentUser.UserName);
                }

                foreach (var branch in supplierPO.SPOBranch)
                {
                    var spoBranch = new PurchaseOrderBranch
                    {
                        CreatedDateTime = DateTime.UtcNow,
                        BranchId        = branch.Id,
                        PurchaseOrderId = supplierPurchaseOrder.Id
                    };
                    _purchaseOrderBranchContext.Add(spoBranch);
                    _purchaseOrderBranchContext.SaveChanges();
                }
                foreach (var item in supplierPO.SupplierItem)
                {
                    var poItem = new PurchaseOrderItem
                    {
                        CreatedDateTime      = DateTime.UtcNow,
                        FreeQuantity         = item.FreeQuantity,
                        ItemId               = item.ItemId,
                        OrderCostPrice       = item.OrderCostPrice,
                        OrderQuantity        = item.OrderQuantity,
                        ReceivingCostPrice   = item.OrderCostPrice,
                        BillCostPrice        = item.OrderCostPrice,
                        ReceivingQuantity    = item.OrderQuantity,
                        SystemParameterId    = item.UnitParamTypeId,
                        PercentageDiscount   = item.PercentageDiscount,
                        IsPercentageDiscount = true,
                        PurchaseOrderId      = supplierPurchaseOrder.Id,
                        ReceivingDate        = supplierPO.DueDate,
                        UpdatedDate          = DateTime.UtcNow,
                        SPOReceivingStatus   = SPOReceivingStatus.NotReceived
                    };
                    _purchaseOrderItemContext.Add(poItem);
                    _purchaseOrderItemContext.SaveChanges();
                }
                return(StringConstants.SPOCreated);
            }
            catch (Exception ex)
            {
                _errorLog.LogException(ex);
                throw;
            }
        }
        /// <summary>
        /// This method is used to save SPO Payment. - JJ
        /// </summary>
        /// <param name="supplierPOPayment"> object of SPOPaymentAC</param>
        /// <param name="RoleName">rolename of the logged in user</param>
        /// <param name="userName">username of the logged in user</param>
        /// <returns>status</returns>
        public string SaveSupplierPayment(SPOPaymentAC supplierPOPayment, string RoleName, string userName)
        {
            decimal creditNoteAmount;
            string  poNumber = string.Empty;
            SupplierPaymentDetail spDetail = new SupplierPaymentDetail
            {
                Amount          = supplierPOPayment.Amount,
                CreatedDateTime = DateTime.UtcNow,
                IsPOBillPayment = true
            };

            _supplierPaymentDetailContext.Add(spDetail);
            _supplierPaymentDetailContext.SaveChanges();
            foreach (var bill in supplierPOPayment.SPOBill)
            {
                var poSupplierBill = _poSupplierBillContext.FirstOrDefault(x => x.BillNumber == bill.BillNumber && x.SupplierPurchaseOrder.PurchaseOrderNumber == bill.PurchaseOrderNo);
                if (poSupplierBill != null)
                {
                    poNumber = bill.PurchaseOrderNo;
                    poSupplierBill.IsPaid           = true;
                    poSupplierBill.ModifiedDateTime = DateTime.UtcNow;
                    _poSupplierBillContext.Update(poSupplierBill);
                    _poSupplierBillContext.SaveChanges();
                    POBillPayment poBill = new POBillPayment
                    {
                        CreatedDateTime   = DateTime.UtcNow,
                        VoucherNo         = supplierPOPayment.VoucherNo,
                        POSupplierBillId  = poSupplierBill.Id,
                        SupplierPaymentId = spDetail.Id
                    };
                    _poBillPaymentContext.Add(poBill);
                    _poBillPaymentContext.SaveChanges();
                }
            }

            var paymentTypeList = _paramTypeContext.Fetch(x => x.Param.Value.Equals(StringConstants.POSPaymentType)).ToList();

            if (supplierPOPayment.Cash != 0)
            {
                PaymentType type = new PaymentType
                {
                    Amount            = supplierPOPayment.Cash,
                    CreatedDateTime   = DateTime.UtcNow,
                    SupplierPaymentId = spDetail.Id,
                    TypeId            = paymentTypeList.FirstOrDefault(x => x.ValueEn.Equals(StringConstants.Cash)).Id
                };
                _paymentTypeContext.Add(type);
                _paymentTypeContext.SaveChanges();
            }
            if (supplierPOPayment.Cheque != 0)
            {
                PaymentType type = new PaymentType
                {
                    Amount            = supplierPOPayment.Cheque,
                    ChequeNo          = supplierPOPayment.ChequeNo,
                    CreatedDateTime   = DateTime.UtcNow,
                    SupplierPaymentId = spDetail.Id,
                    TypeId            = paymentTypeList.FirstOrDefault(x => x.ValueEn.Equals(StringConstants.Cheque)).Id
                };
                _paymentTypeContext.Add(type);
                _paymentTypeContext.SaveChanges();
            }

            creditNoteAmount = supplierPOPayment.Credit;

            if (supplierPOPayment.Credit != 0)
            {
                PaymentType type = new PaymentType
                {
                    Amount            = supplierPOPayment.Credit,
                    CreatedDateTime   = DateTime.UtcNow,
                    SupplierPaymentId = spDetail.Id,
                    TypeId            = _paramTypeContext.FirstOrDefault(x => x.ValueEn.Equals(StringConstants.CreditNote)).Id
                };
                _paymentTypeContext.Add(type);
                _paymentTypeContext.SaveChanges();

                if (supplierPOPayment.CreditNoteDetail != null && supplierPOPayment.CreditNoteDetail.Count > 0)
                {
                    foreach (var note in supplierPOPayment.CreditNoteDetail)
                    {
                        var creditNote = _creditNoteDetailContext.Find(note.Id);
                        if (creditNote != null)
                        {
                            if (creditNote.IsCollected)
                            {
                                if (creditNote.RemainigAmount >= creditNoteAmount)
                                {
                                    creditNote.RemainigAmount = creditNote.RemainigAmount - creditNoteAmount;
                                    creditNoteAmount          = 0;
                                }
                                else
                                {
                                    creditNoteAmount          = creditNoteAmount - creditNote.RemainigAmount;
                                    creditNote.RemainigAmount = 0;
                                }
                            }
                            else
                            {
                                creditNote.IsCollected = true;
                                if (creditNote.Amount >= creditNoteAmount)
                                {
                                    creditNote.RemainigAmount = creditNote.Amount - creditNoteAmount;
                                    creditNoteAmount          = 0;
                                }
                                else
                                {
                                    creditNoteAmount          = creditNoteAmount - creditNote.Amount;
                                    creditNote.RemainigAmount = 0;
                                }
                            }
                            creditNote.ModifiedDateTime = DateTime.UtcNow;
                            _creditNoteDetailContext.Update(creditNote);
                            _creditNoteDetailContext.SaveChanges();

                            PaymentTypeCreditNote notePaymentType = new PaymentTypeCreditNote
                            {
                                CreatedDateTime = DateTime.UtcNow,
                                CreditNoteId    = note.Id,
                                // insert as needed
                                PaymentTypeId = type.Id
                            };
                            _paymentTypeCreditNoteContext.Add(notePaymentType);
                            _paymentTypeCreditNoteContext.SaveChanges();

                            CreditNotePayment notePayment = new CreditNotePayment
                            {
                                CreatedDateTime   = DateTime.UtcNow,
                                SupplierPaymentId = spDetail.Id,
                                CreditNoteId      = note.Id
                            };
                            _creditNotePaymentContext.Add(notePayment);
                            _creditNotePaymentContext.SaveChanges();

                            if (creditNoteAmount <= 0)
                            {
                                break;
                            }
                        }
                    }
                }
            }

            var poSupplierBillList = _poSupplierBillContext.Fetch(x => x.SupplierPurchaseOrder.PurchaseOrderNumber == poNumber).ToList();
            var ispaid             = true;

            foreach (var bill in poSupplierBillList)
            {
                if (!bill.IsPaid)
                {
                    ispaid = false;
                }
            }
            var spo = _supplierPurchaseOrderContext.FirstOrDefault(x => x.PurchaseOrderNumber == poNumber);

            if (ispaid)
            {
                spo.IsPaid           = true;
                spo.ModifiedDateTime = DateTime.UtcNow;
                _supplierPurchaseOrderContext.Update(spo);
                _supplierPurchaseOrderContext.SaveChanges();

                if (supplierPOPayment.Comment == ".")
                {
                    supplierPOPayment.Comment = "";
                }

                _supplierPOWorkListRepository.SaveSupplierPurchaseOrderLog("Paid", supplierPOPayment.Comment, spo.Id, spo.RecordId, RoleName, "" + RoleName + " " + StringConstants.PaySPO, userName);
            }
            else
            {
                _supplierPOWorkListRepository.SaveSupplierPurchaseOrderLog("Partially Paid", supplierPOPayment.Comment, spo.Id, spo.RecordId, RoleName, "" + RoleName + " " + StringConstants.PaySPO, userName);
            }

            var discount = CalculateDiscount(spo.Id);

            InsertIntoAccountEntries(spo, supplierPOPayment, discount);
            return("ok");
        }