/// <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 edit spo and delete item of spo - JJ /// </summary> /// <param name="supplierPO">object of SupplierPOAC</param> /// <returns>status</returns> public string EditSPO(SupplierPOAC supplierPO) { try { var supplierPurchaseOrder = _supplierPOContext.First(x => x.Id == supplierPO.Id); if (supplierPurchaseOrder.DueDate == supplierPO.DueDate) { supplierPurchaseOrder.IsNotProcessed = true; } else { supplierPurchaseOrder.IsNotProcessed = false; } supplierPurchaseOrder.DueDate = supplierPO.DueDate; supplierPurchaseOrder.IsRejected = false; supplierPurchaseOrder.ModifiedDateTime = DateTime.UtcNow; var oldItems = _purchaseOrderItemContext.Fetch(x => x.PurchaseOrderId == supplierPO.Id).ToList(); var firstCollection = from item in supplierPO.SupplierItem select new { FreeQuantity = item.FreeQuantity, ItemId = item.ItemId, OrderCostPrice = item.OrderCostPrice, OrderQuantity = item.OrderQuantity, ReceivingCostPrice = item.OrderCostPrice, ReceivingQuantity = item.OrderQuantity, PurchaseOrderId = supplierPurchaseOrder.Id, PercentageDiscount = item.PercentageDiscount }; var secCollection = from item in oldItems select new { FreeQuantity = item.FreeQuantity, ItemId = item.ItemId, OrderCostPrice = item.OrderCostPrice, OrderQuantity = item.OrderQuantity, ReceivingCostPrice = item.OrderCostPrice, ReceivingQuantity = item.OrderQuantity, PurchaseOrderId = supplierPurchaseOrder.Id, PercentageDiscount = item.PercentageDiscount }; var except = firstCollection.Except(secCollection); var length = except.ToList().Count; var except2 = secCollection.Except(firstCollection); var length2 = except2.ToList().Count; if (length > 0 || length2 > 0) { supplierPurchaseOrder.IsNotProcessed = false; DeleteItems(supplierPO.Id); 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, PurchaseOrderId = supplierPurchaseOrder.Id, PercentageDiscount = item.PercentageDiscount, IsPercentageDiscount = true, ReceivingDate = supplierPO.DueDate, UpdatedDate = DateTime.UtcNow, SPOReceivingStatus = SPOReceivingStatus.NotReceived }; _purchaseOrderItemContext.Add(poItem); _purchaseOrderItemContext.SaveChanges(); } } else { supplierPurchaseOrder.IsNotProcessed = true; } var oldBranches = _purchaseOrderBranchContext.Fetch(x => x.PurchaseOrderId == supplierPurchaseOrder.Id).ToList(); var oldBranchList = from branch in oldBranches select new { BranchId = branch.BranchId, }; var newBranchList = from branch in supplierPO.SPOBranch select new { BranchId = branch.Id, }; var exceptBranch = oldBranchList.Except(newBranchList); var blenghth = exceptBranch.ToList().Count; var exceptBranch2 = newBranchList.Except(oldBranchList); var blength2 = exceptBranch2.ToList().Count; if (blenghth > 0 || blength2 > 0) { supplierPurchaseOrder.IsNotProcessed = false; DeleteBranches(supplierPO.Id); foreach (var branch in supplierPO.SPOBranch) { var spoBranch = new PurchaseOrderBranch { CreatedDateTime = DateTime.UtcNow, BranchId = branch.Id, PurchaseOrderId = supplierPurchaseOrder.Id }; _purchaseOrderBranchContext.Add(spoBranch); _purchaseOrderBranchContext.SaveChanges(); } } else { if (supplierPurchaseOrder.IsNotProcessed) { supplierPurchaseOrder.IsNotProcessed = true; } } _supplierPOContext.Update(supplierPurchaseOrder); _supplierPOContext.SaveChanges(); return("ok"); } catch (Exception ex) { _errorLog.LogException(ex); throw; } }
/// <summary> /// This method is used for checking supplier bill list from database. - JJ /// </summary> /// <param name="PurchaseOrderId">id of PurchaseOrder</param> /// <returns>list of objects of SPOReceivingBillAC</returns> public List <SPOReceivingBillAC> CheckCondition(UserDetail currentUser, CompanyDetail company, List <SPOReceivingBillAC> SPOReceivingBill) { bool isCashPO; bool IsDiscountChanged = true; decimal discount = 0M; bool canBePaid = true; foreach (var spobill in SPOReceivingBill) { var poItems = _purchaseOrderItemContext.Fetch(x => x.SupplierPurchaseOrder.PurchaseOrderNumber == spobill.PurchaseOrderNo && x.SPOReceivingStatus != SPOReceivingStatus.NotReceived).ToList(); var bill = _poSupplierBillContext.FirstOrDefault(x => x.IsVerified && !x.IsPaid && x.SupplierPurchaseOrder.PurchaseOrderNumber == spobill.PurchaseOrderNo && x.Id == spobill.BillId); if (bill != null && bill.SupplierPurchaseOrder.IsApproved && bill.SupplierPurchaseOrder.IsReceived && bill.SupplierPurchaseOrder.IsSend && bill.SupplierPurchaseOrder.IsVerified) { if (bill.SupplierPurchaseOrder.SupplierProfile.SupplierType.ValueEn == StringConstants.Cash) { isCashPO = true; } else { isCashPO = false; } if (!isCashPO) { //TODO: replace with supplier days limit var suppDays = _supplierDaysLimit.Fetch(x => x.SupplierId == bill.SupplierPurchaseOrder.SupplierId).ToList(); if (suppDays.Any()) { var date = DateTime.UtcNow.Subtract(bill.SupplierPurchaseOrder.UpdatedDate); var days = Convert.ToInt32(Math.Round(date.TotalDays)); for (var i = 0; i < suppDays.Count; i++) { int index; if (i == 0 && days < suppDays[i].Days) // no limit arrived yet { IsDiscountChanged = false; break; } if (days == suppDays[i].Days) //in case its same days limit { index = i; if (bill.PresentDiscount == suppDays[index].Discount) { IsDiscountChanged = false; break; } } else if (suppDays[i].Days > days || //between the days (((i + 1) <= (suppDays.Count - 1)) && suppDays[i + 1].Days >= days)) { index = i + 1; if (bill.PresentDiscount == suppDays[index].Discount) { IsDiscountChanged = false; break; } else { discount = suppDays[index].Discount; IsDiscountChanged = true; break; } } } } else { IsDiscountChanged = false; } if (IsDiscountChanged && bill.PresentDiscount != discount) { bill.PresentDiscount = discount; _poSupplierBillContext.Update(bill); _poSupplierBillContext.SaveChanges(); } else { IsDiscountChanged = false; } PurchaseOrderBranch conditions = new PurchaseOrderBranch { IsDiscountChanged = IsDiscountChanged }; var workflowLog = _iWorkFlowDetailsRepository.GetInitiationActionWorkFlow(StringConstants.SPOPayment, StringConstants.PaySPO, currentUser, company, conditions, null, conditions); if (workflowLog != null) { WorkFlowLog log = (WorkFlowLog)workflowLog.Item1; var workFlowDetail = _workFlowContext.Find(log.WorkFlowId); if (workFlowDetail.NextActivity.IsClosed) { spobill.IsSelected = true; spobill.CanBePaid = true; if (workFlowDetail.ParentPermission.Name == StringConstants.ItemChangeRequest) { ChangePrices(spobill.PurchaseOrderNo, log.RecordId, currentUser, company, true, workFlowDetail.Id); break; } } else { if (workFlowDetail.ParentPermission.Name == StringConstants.ItemChangeRequest) { spobill.IsSelected = true; spobill.CanBePaid = true; spobill.IsICRCreated = true; spobill.IsICRAlreadyCreated = false; spobill.IsWorkFlowNotCreated = false; spobill.IsBillNotFound = false; ChangePrices(spobill.PurchaseOrderNo, log.RecordId, currentUser, company, true, workFlowDetail.Id); break; } } foreach (var item in poItems) { if (item.ItemProfile.IsItemChangeRequestGenerated) { canBePaid = false; break; } } if (!canBePaid) { spobill.IsICRAlreadyCreated = true; } } else { spobill.IsSelected = false; spobill.CanBePaid = false; spobill.IsICRCreated = false; spobill.IsICRAlreadyCreated = false; spobill.IsWorkFlowNotCreated = true; spobill.IsBillNotFound = false; } } else { spobill.IsSelected = true; spobill.CanBePaid = true; } } else { spobill.IsSelected = false; spobill.CanBePaid = false; spobill.IsBillNotFound = true; spobill.IsICRCreated = false; spobill.IsWorkFlowNotCreated = false; spobill.IsICRAlreadyCreated = false; } } return(SPOReceivingBill); }