Exemplo n.º 1
0
        public static Result <object> ReInsertInventoryToWarehouse(LeadDetail detail, IDataRepositoryFactory _repositoryFactory, Warehouse currentWarehouse)
        {
            var productUnitsRepo      = _repositoryFactory.GetDataRepositories <UnitProductEquivalence>();
            var inventoryRepo         = _repositoryFactory.GetDataRepositories <Inventory>();
            var warehouseMovementRepo = _repositoryFactory.GetDataRepositories <WarehouseMovement>();
            var productRepo           = _repositoryFactory.GetDataRepositories <Product>();

            Inventory currentInventory = inventoryRepo.Get(x => x.AsNoTracking().Where(y => y.ProductId == detail.ProductId && y.WarehouseId == currentWarehouse.Id && y.Active == true));

            if (currentInventory != null)
            {
                detail.Product.ProductUnits = detail.Product.ProductUnits ?? productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active && y.ProductId == detail.ProductId)).Data.ToList();

                var convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                    currentInventory.Quantity,
                    detail.Product.ProductUnits.Where(pu => pu.IsPrimary).FirstOrDefault().UnitId,
                    detail.Product.ProductUnits.ToList()
                    );
                if (convertionResult.Status < 0)
                {
                    return(convertionResult);
                }
                currentInventory.Quantity = (decimal)convertionResult.Data.FirstOrDefault();

                convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                    detail.Quantity,
                    detail.UnitId.Value,
                    detail.Product.ProductUnits.ToList()
                    );
                if (convertionResult.Status < 0)
                {
                    return(convertionResult);
                }

                detail.Quantity = (decimal)convertionResult.Data.FirstOrDefault();

                currentInventory.Quantity += detail.Quantity;
                Product Product = productRepo.Get(detail.ProductId).Data.FirstOrDefault();
                Product.Existence += detail.Quantity;
                productRepo.Update(Product);
                inventoryRepo.Update(currentInventory);
            }
            else
            {
                inventoryRepo.Add(new Inventory()
                {
                    Active      = true,
                    Quantity    = detail.Quantity,
                    WarehouseId = currentWarehouse.Id,
                    CreatedBy   = detail.CreatedBy,
                    CreatedDate = DateTime.Now,
                    ProductId   = detail.ProductId,
                    UnitId      = detail.Product.ProductUnits.Where(u => u.IsPrimary).FirstOrDefault().Id
                });
            }

            return(new Result <object>(0, 0, "ok_msg"));
        }
Exemplo n.º 2
0
        protected override Result <LeadDetail> ProcessDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead invoice)
        {
            var newDetail = new LeadDetail(detail);

            newDetail.WarehouseId = null;
            newDetail.UnitId      = null;
            return(new Result <LeadDetail>(0, 0, "ok_msg", new List <LeadDetail>()
            {
                detail
            }));
        }
Exemplo n.º 3
0
        protected override Result <LeadDetail> ProcessDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead lead)
        {
            var detailService    = dataRepositoryFactory.GetCustomDataRepositories <IInvoiceDetailRepository>();
            var productRepo      = dataRepositoryFactory.GetDataRepositories <Product>();
            var productBasesRepo = dataRepositoryFactory.GetDataRepositories <CompositeProduct>();
            var productUnitRepo  = dataRepositoryFactory.GetDataRepositories <UnitProductEquivalence>();
            var productBases     = detail.Product.BaseCompositeProducts == null || detail.Product.BaseCompositeProducts.Count() == 0 ? productBasesRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data.ToList() :
                                   detail.Product.BaseCompositeProducts.ToList();

            detail.WarehouseId = null;
            detail.UnitId      = null;
            var detailResult = detailService.Add(detail);

            productBases.ForEach(p =>
            {
                var currentProduct = productRepo.Get(p.BaseProductId).Data.FirstOrDefault();

                var currentUnit = p.UnitProductEquivalence == null ? p.BaseProductUnitId.HasValue ?
                                  productUnitRepo.Get(p.BaseProductUnitId.Value).Data.FirstOrDefault() : null : p.UnitProductEquivalence;

                var instance = GetBillProductOrServiceInstance.GetBillingInstance(currentProduct);
                // decimal nuevaQuantity = p.Quantity * detail.Quantity;
                LeadDetail detailTemp = new LeadDetail(detail)
                {
                    Quantity  = p.Quantity * detail.Quantity,
                    UnitId    = currentUnit != null ? currentUnit.Unit.Id : p.BaseProductUnitId,
                    ProductId = p.BaseProductId,
                    ParentId  = detailResult.Id,
                    Product   = currentProduct,
                    Free      = true
                };
                var result = instance.ProcessProductService(branchOfficeId, detailTemp, dataRepositoryFactory, lead).Data.FirstOrDefault();
                if (result.SaveRegister)
                {
                    detailService.Add(result);
                }
            });
            detail.SaveRegister = false;
            return(new Result <LeadDetail>(0, 0, "ok_msg", new List <LeadDetail>()
            {
                detail
            }));
        }
Exemplo n.º 4
0
        protected override Result <LeadDetail> ProcessReturnDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead lead)
        {
            var invoiceDetailRepo = dataRepositoryFactory.GetCustomDataRepositories <IInvoiceDetailRepository>();
            var productRepo       = dataRepositoryFactory.GetDataRepositories <Product>();
            var productBasesRepo  = dataRepositoryFactory.GetDataRepositories <CompositeProduct>();
            var productUnitRepo   = dataRepositoryFactory.GetDataRepositories <UnitProductEquivalence>();

            var baseDetails = invoiceDetailRepo.GetChildren(detail.Id).ToList();

            baseDetails.ForEach(p =>
            {
                var currentProduct = p.Product ?? productRepo.Get(p.ProductId).Data.FirstOrDefault();



                var instance = GetBillProductOrServiceInstance.GetBillingInstance(currentProduct);
                // decimal nuevaQuantity = p.Quantity * detail.Quantity;
                LeadDetail detailTemp = new LeadDetail()
                {
                    Id            = p.Id,
                    Active        = p.Active,
                    WarehouseId   = p.WarehouseId ?? null,
                    Quantity      = p.Quantity * (detail.ReturnAmount / detail.Quantity),
                    ReturnAmount  = p.Quantity * (detail.ReturnAmount / detail.Quantity),
                    ParentId      = p.ParentId ?? null,
                    Defective     = detail.Defective,
                    InvoiceLeadId = p.InvoiceLeadId,
                    Product       = p.Product,
                    ProductId     = p.ProductId,
                    Unit          = p.Unit,
                    UnitId        = p.UnitId
                };

                var resultado = instance.ReturnProductService(branchOfficeId, detailTemp, dataRepositoryFactory, lead);
            });

            return(new Result <LeadDetail>(0, 0, "ok_msg", new List <LeadDetail>()
            {
                detail
            }));
        }
Exemplo n.º 5
0
        public static Result <LeadDetail> UpdateProductInventory(BranchOffice branchOffice, LeadDetail detail, IDataRepositoryFactory _repositoryFactory, InvoiceLead invoice)
        {
            LeadDetail newdetail     = new LeadDetail(detail);
            var        productRepo   = _repositoryFactory.GetDataRepositories <Product>();
            var        inventoryRepo = _repositoryFactory.GetDataRepositories <Inventory>();
            var        detailRepo    = _repositoryFactory.GetDataRepositories <LeadDetail>();

            var service = GetBillProductOrServiceInstance.GetBillingInstance(detail.Product);
            var result  = service.ProcessProductService(branchOffice.Id, detail, _repositoryFactory, invoice);

            if (result.Status >= 0)
            {
                result.Data = new List <LeadDetail>()
                {
                    newdetail
                }
            }
            ;


            return(result);
        }
    }
Exemplo n.º 6
0
        protected override Result <LeadDetail> ProcessReturnDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead invoice)
        {
            Inventory currentInventory = null;
            var       warehouseRepo    = dataRepositoryFactory.GetDataRepositories <Warehouse>();
            var       productRepo      = dataRepositoryFactory.GetDataRepositories <Product>();
            var       inventoryRepo    = dataRepositoryFactory.GetDataRepositories <Inventory>();
            var       productUnitsRepo = dataRepositoryFactory.GetDataRepositories <UnitProductEquivalence>();
            var       logMovementsRepo = dataRepositoryFactory.GetDataRepositories <WarehouseMovement>();

            if (detail.WarehouseId.HasValue)
            {
                detail.Product.ProductUnits = detail.Product.ProductUnits == null || detail.Product.ProductUnits.Count() == 0 ?
                                              productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data.ToList() : detail.Product.ProductUnits;

                if (detail.Defective)
                {
                    var defectiveWarehouse = warehouseRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.Code == "DEF" && y.BranchOfficeId == branchOfficeId));
                    if (defectiveWarehouse == null)
                    {
                        return(new Result <LeadDetail>(-1, -1, $"defWarehouseNotExit_msg"));
                    }

                    currentInventory = inventoryRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.BranchOfficeId == branchOfficeId && y.ProductId == detail.ProductId &&
                                                                                     y.WarehouseId == defectiveWarehouse.Id)) ??
                                       new Inventory()
                    {
                        WarehouseId    = defectiveWarehouse.Id,
                        ProductId      = detail.ProductId,
                        Active         = true,
                        Quantity       = 0,
                        CreatedBy      = detail.CreatedBy,
                        CreatedDate    = DateTime.Now,
                        BranchOfficeId = branchOfficeId,
                        UnitId         = detail.Product.ProductUnits.FirstOrDefault(u => u.IsPrimary).UnitId,
                        Product        = detail.Product
                    };
                }
                else
                {
                    currentInventory = inventoryRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId && y.WarehouseId == detail.WarehouseId));
                }
                if (currentInventory != null)
                {
                    currentInventory.Product.ProductUnits = currentInventory.Product.ProductUnits == null || currentInventory.Product.ProductUnits.Count() == 0 ?
                                                            productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == currentInventory.ProductId)).Data.ToList(): currentInventory.Product.ProductUnits;

                    currentInventory.Quantity = (decimal)ProductsHelper.ConvertToProductPrincipalUnit(
                        currentInventory.Quantity,
                        detail.Product.ProductUnits.FirstOrDefault(pu => pu.IsPrimary).UnitId,
                        detail.Product.ProductUnits.ToList()
                        ).Data.FirstOrDefault();
                    detail.Quantity = (decimal)ProductsHelper.ConvertToProductPrincipalUnit(
                        detail.Quantity,
                        detail.UnitId.Value,
                        detail.Product.ProductUnits.ToList()
                        ).Data.FirstOrDefault();
                    var units = detail.Product.ProductUnits == null || detail.Product.ProductUnits.Count() == 0 ?
                                productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data.ToList() : detail.Product.ProductUnits;

                    var warehouseMov = new WarehouseMovement(currentInventory.WarehouseId, detail.ProductId, detail.Quantity, true,
                                                             units.Where(u => u.IsPrimary).FirstOrDefault().UnitId, branchOfficeId, "IN", invoice.InvoiceNumber ?? invoice.DocumentNumber ?? string.Empty);
                    logMovementsRepo.Add(warehouseMov);

                    currentInventory.Quantity += detail.Quantity;
                    currentInventory.UnitId    = currentInventory.Product.ProductUnits.FirstOrDefault(x => x.IsPrimary).UnitId;
                    Product Product = productRepo.Get(detail.ProductId).Data.FirstOrDefault();
                    Product.Existence += detail.Quantity;
                    productRepo.Update(Product);
                    if (currentInventory.Id > 0)
                    {
                        inventoryRepo.Update(currentInventory);
                    }
                    else
                    {
                        inventoryRepo.Add(currentInventory);
                    }
                }
                else
                {
                    return(new Result <LeadDetail>(-1, -1, "outOfStock_msg"));
                }
            }

            return(new Result <LeadDetail>(0, 0, "ok_msg", new List <LeadDetail>()
            {
                detail
            }));
        }
Exemplo n.º 7
0
        protected override Result <LeadDetail> ProcessDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead invoiceLead)
        {
            var        inventoriesRepo  = dataRepositoryFactory.GetDataRepositories <Inventory>();
            var        warehouseRepo    = dataRepositoryFactory.GetDataRepositories <Warehouse>();
            var        productUnitsRepo = dataRepositoryFactory.GetDataRepositories <UnitProductEquivalence>();
            LeadDetail newDetail        = new LeadDetail(detail);

            detail.Product.ProductUnits = detail.Product.ProductUnits == null || detail.Product.ProductUnits.Count() == 0 ?
                                          productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data.ToList() : detail.Product.ProductUnits;
            var convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                newDetail.Quantity,
                newDetail.UnitId.Value,
                newDetail.Product.ProductUnits.ToList()
                );

            if (convertionResult.Status < 0)
            {
                return(new Result <LeadDetail>(-1, -1, convertionResult.Message));
            }

            decimal warehouseQuantity = (decimal)convertionResult.Data.FirstOrDefault();
            decimal quantitySumm      = warehouseQuantity;

            LeadDetail newDetailCustom = new LeadDetail(newDetail)
            {
                Quantity = warehouseQuantity
            };
            Warehouse warehouse = null;

            if ((invoiceLead.WarehouseId <= 0))
            {
                var branchOfficeWarehouses = warehouseRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.BranchOfficeId == branchOfficeId &&
                                                                                              y.Code.ToLower() != "DEF")).Data.ToList();
                // ojo ojo ojo
                var productInventory = inventoriesRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == newDetail.ProductId &&
                                                                                       y.Quantity >= warehouseQuantity && branchOfficeWarehouses.Select(r => r.Id).ToList().Contains(y.WarehouseId)));

                if (productInventory == null)
                {
                    return(new Result <LeadDetail>(-1, -1, "outOfStock_msg"));
                }
                // ojo ojo ojo

                warehouse = branchOfficeWarehouses.FirstOrDefault(x => x.Id == productInventory.WarehouseId);
            }
            else
            {
                warehouse = warehouseRepo.Get(invoiceLead.WarehouseId).Data.FirstOrDefault();
            }


            if ((invoiceLead.WarehouseId > 0) && (inventoriesRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == newDetail.ProductId &&
                                                                                                  y.Quantity >= warehouseQuantity && y.WarehouseId == invoiceLead.WarehouseId)) == null))
            {
                return(new Result <LeadDetail>(-1, -1, $"outOfStock_msg | {detail.Product?.Name?? ""}"));
            }

            if (warehouse == null)
            {
                var branchOfficeWarehouses = warehouseRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.BranchOfficeId == branchOfficeId &&
                                                                                              y.Code.ToLower() != "DEF")).Data.ToList();
                // ojo ojo ojo
                var branchOfficeInventory = inventoriesRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == newDetail.ProductId &&
                                                                                               y.Quantity > 0 && branchOfficeWarehouses.Select(r => r.Id).ToList().Contains(y.WarehouseId))).Data.ToList();

                if (branchOfficeInventory == null)
                {
                    return(new Result <LeadDetail>(-1, -1, $"outOfStock_msg | {newDetail.Product.Name}"));
                }


                convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                    branchOfficeInventory.Sum(p => p.Quantity),
                    branchOfficeInventory.FirstOrDefault().UnitId,
                    newDetail.Product.ProductUnits.ToList()
                    );
                if (convertionResult.Status < 0)
                {
                    return(new Result <LeadDetail>(-1, -1, convertionResult.Message));
                }

                decimal warehousesQuantitySumm = (decimal)convertionResult.Data.FirstOrDefault();

                if (warehousesQuantitySumm < warehouseQuantity)
                {
                    return(new Result <LeadDetail>(-1, -1, $"outOfStock_msg | {newDetail.Product.Name}"));
                }

                foreach (Inventory warehouseProduct in branchOfficeInventory)
                {
                    if (quantitySumm <= 0)
                    {
                        break;
                    }
                    decimal currentWarehouseQuantity = warehouseProduct.Quantity;
                    if (quantitySumm < currentWarehouseQuantity)
                    {
                        currentWarehouseQuantity = quantitySumm;
                    }

                    newDetailCustom          = new LeadDetail(newDetail);
                    newDetailCustom.Quantity = (decimal)ProductsHelper.ConvertFromProductPrincipalUnit(
                        currentWarehouseQuantity,
                        newDetail.UnitId.Value,
                        newDetail.Product.ProductUnits.ToList()
                        ).Data.FirstOrDefault();

                    newDetailCustom.WarehouseId = warehouseProduct.WarehouseId;
                    //  newDetailCustom.UnitId = newDetailCustom.Product.ProductUnits.FirstOrDefault(x => x.IsPrimary).UnitId;
                    InventoryHelper.UpdateInventory(newDetailCustom, warehouseRepo.Get(warehouseProduct.WarehouseId).Data.FirstOrDefault(), invoiceLead, dataRepositoryFactory);
                    newDetail.WarehouseId = warehouseProduct.WarehouseId;

                    quantitySumm -= currentWarehouseQuantity;
                }
            }
            else
            {
                newDetail.WarehouseId  = warehouse.Id;
                newDetailCustom.Unit   = newDetailCustom.Product.ProductUnits.FirstOrDefault(x => x.IsPrimary).Unit;
                newDetailCustom.UnitId = newDetailCustom.Unit.Id;
                InventoryHelper.UpdateInventory(newDetailCustom, warehouse, invoiceLead, dataRepositoryFactory);
            }

            return(new Result <LeadDetail>(0, 0, "ok_msg", new List <LeadDetail>()
            {
                newDetail
            }));
        }
Exemplo n.º 8
0
 protected override Result <LeadDetail> ProcessReturnDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead invoice)
 {
     return(new Result <LeadDetail>(0, 0, "ok_msg", new List <LeadDetail>()
     {
         detail
     }));
 }
Exemplo n.º 9
0
        public static Result <object> AddInventory(LeadDetail detail, InvoiceLead invoiceLead, IDataRepositoryFactory _repositoryFactory)
        {
            var warehouseMovementRepo = _repositoryFactory.GetDataRepositories <WarehouseMovement>();
            var productUnitsRepo      = _repositoryFactory.GetDataRepositories <UnitProductEquivalence>();
            var productRepo           = _repositoryFactory.GetDataRepositories <Product>();
            var inventoryRepo         = _repositoryFactory.GetDataRepositories <Inventory>();

            if (!detail.Product.IsService)
            {
                detail.Product.ProductUnits = detail.Product.ProductUnits ?? productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data.ToList();


                if (detail.WarehouseId.HasValue)
                {
                    Inventory currentInventory = inventoryRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId && y.WarehouseId == detail.WarehouseId.Value));
                    if (currentInventory != null)
                    {
                        var convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                            currentInventory.Quantity,
                            detail.Product.ProductUnits.Where(pu => pu.IsPrimary).FirstOrDefault().UnitId,
                            detail.Product.ProductUnits.ToList()
                            );
                        if (convertionResult.Status < 0)
                        {
                            return(convertionResult);
                        }

                        currentInventory.Quantity = (decimal)convertionResult.Data.FirstOrDefault();

                        convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                            detail.Quantity,
                            detail.UnitId.Value,
                            detail.Product.ProductUnits.ToList()
                            );
                        if (convertionResult.Status < 0)
                        {
                            return(convertionResult);
                        }

                        detail.Quantity = (decimal)convertionResult.Data.FirstOrDefault();
                        var units = detail.Product.ProductUnits ?? productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data.ToList();


                        currentInventory.Quantity += detail.Quantity;
                        Product Product = productRepo.Get(detail.ProductId).Data.FirstOrDefault();
                        Product.Existence += detail.Quantity;
                        productRepo.Update(Product);
                        inventoryRepo.Update(currentInventory);

                        WarehouseMovement movimientoAlmacen = new WarehouseMovement(currentInventory.WarehouseId, detail.ProductId, detail.Quantity, true,
                                                                                    units.Where(u => u.IsPrimary).FirstOrDefault().UnitId, 0, "IN", invoiceLead.InvoiceNumber ?? invoiceLead.DocumentNumber ?? string.Empty, currentInventory.Quantity);
                        warehouseMovementRepo.Add(movimientoAlmacen);
                        return(new Result <object>(0, 0, "ok_msg"));
                    }
                    else
                    {
                        return(new Result <object>(-1, -1, $"outOfStock_msg | {detail.Product.Name}"));
                    }
                }
            }

            return(new Result <object>(0, 0, "ok_msg"));
        }
Exemplo n.º 10
0
        public static Result <object> UpdateInventory(LeadDetail detail, Warehouse currentWarehouse, InvoiceLead invoiceLead, IDataRepositoryFactory _repositoryFactory)
        {
            var       warehouseMovementRepo = _repositoryFactory.GetDataRepositories <WarehouseMovement>();
            var       productUnitsRepo      = _repositoryFactory.GetDataRepositories <UnitProductEquivalence>();
            var       productRepo           = _repositoryFactory.GetDataRepositories <Product>();
            var       inventoryRepo         = _repositoryFactory.GetDataRepositories <Inventory>();
            Inventory currentInventory      = inventoryRepo.Get(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId && y.WarehouseId == currentWarehouse.Id));

            if (currentInventory != null)
            {
                var convertionResult = ProductsHelper.ConvertToProductPrincipalUnit(
                    detail.Quantity,
                    detail.UnitId.Value,
                    detail.Product.ProductUnits.ToList()
                    );
                if (convertionResult.Status >= 0)
                {
                    detail.Quantity = (decimal)convertionResult.Data.FirstOrDefault();
                }
                else
                {
                    return(convertionResult);
                }

                detail.UnitId    = detail.Product.ProductUnits.FirstOrDefault(pu => pu.IsPrimary).UnitId;
                convertionResult = ProductsHelper.ConvertFromProductPrincipalUnit(
                    currentInventory.Quantity,
                    detail.Product.ProductUnits.Where(pu => pu.IsPrimary).FirstOrDefault().UnitId,
                    detail.Product.ProductUnits.ToList()
                    );

                if (convertionResult.Status >= 0)
                {
                    currentInventory.Quantity = (decimal)convertionResult.Data.FirstOrDefault();
                }
                else
                {
                    return(convertionResult);
                }


                if (currentInventory.Quantity < detail.Quantity)
                {
                    return(new Result <object>(-1, -1, $"outOfStock_msg | {detail.Product.Name},{currentWarehouse.Name}"));
                }
                else
                {
                    currentInventory.Quantity -= detail.Quantity;
                    Product product = productRepo.Get(detail.ProductId).Data?.FirstOrDefault();
                    product.Existence -= detail.Quantity;
                    productRepo.Update(product);
                    inventoryRepo.Update(currentInventory);


                    var units = detail.Product.ProductUnits ?? productUnitsRepo.GetAll(x => x.AsNoTracking().Where(y => y.Active == true && y.ProductId == detail.ProductId)).Data;
                    WarehouseMovement movement = new WarehouseMovement(currentWarehouse.Id, detail.ProductId, detail.Quantity * -1,
                                                                       true, units.Where(u => u.IsPrimary).FirstOrDefault().UnitId, 0, "OUT",
                                                                       invoiceLead.InvoiceNumber ?? invoiceLead.DocumentNumber ?? string.Empty, currentInventory.Quantity);

                    warehouseMovementRepo.Add(movement);
                    return(new Result <object>(0, 0, "ok_msg"));
                }
            }
            else
            {
                return(new Result <object>(-1, -1, $"outOfStock_msg | {detail.Product.Name},{currentWarehouse.Name}"));
            }
        }
 protected abstract Result <LeadDetail> ProcessReturnDetail(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory dataRepositoryFactory, InvoiceLead lead);
 public Result <LeadDetail> ReturnProductService(long branchOfficeId, LeadDetail detail, IDataRepositoryFactory _dataRepositoryFactory, InvoiceLead lead)
 {
     return(ProcessReturnDetail(branchOfficeId, detail, _dataRepositoryFactory, lead));
 }
        private static int? SubmitOutcomeFromNewCall(CallOutcomeSubmission _los,
                                                     CallOutcomeContext _context,
                                                     int _callOutcomeID,
                                                     int? _outcomeID)
        {
            //            _los.eventLog.WriteEntry("CallOutcomeSubmission.SubmitOutcomeFromNewCall: Entered method");
            var _call = (from co in _context.T_MollyMaidCallOutcome
                         join ca in _context.T_Call on co.CallID equals ca.CallID
                         join cn in _context.T_CallNeed on ca.CallID equals cn.CallID
            //                            new { _callID = ca.CallID, _needID = ca.NeedID } equals
            //                            new { _callID = cn.CallID, _needID = cn.NeedID }
                         join cp in _context.T_Company on ca.CompanyID equals cp.CompanyID
                         join tln in _context.T_L_Need on ca.NeedID equals tln.NeedID
                         join tlo in _context.T_L_Outcome on ca.OutcomeID equals tlo.OutcomeID
                         join tlnf in _context.T_L_NeedField on cn.NeedFieldID equals tlnf.NeedFieldID
                         where co.CallOutcomeID == _callOutcomeID && cn.NeedID == ca.NeedID
                         select new
                         {
                             co.MollyMaidLeadActionID,
                             co.LeadAction,
                             ca.NeedID,
                             cp.FranchiseID,
                             tln.Need,
                             tlo.Outcome,
                             tlnf.FieldName,
                             cn.Visible,
                             cn.ValueData
                         }).ToList();

            Lead _lead = new Lead();
            _lead.FranNum = int.Parse(_call.FirstOrDefault().FranchiseID);

            LeadAction leadAction = new LeadAction();
            leadAction.ActionDate = DateTime.Now;
            leadAction.ActionType = ACTION_TYPE_CALL_ATTEMPT;
            switch (_outcomeID)
            {
                case 36:    // Sale
                    leadAction.Notes = "New Inbound Call: Scheduled Appointment for " +
                        (_call.Any(a => a.FieldName.Equals("AppointmentDateAndTime") && a.Visible && !String.IsNullOrEmpty(a.ValueData))
                        ? _call.Where(a => a.FieldName.Equals("AppointmentDateAndTime") && a.Visible).Single().ValueData.ToString()
                        : "<Appointment data not found>");           // if null check is false, set to empty string;
                    break;
                case 37:    // No Sale
                    leadAction.Notes = "New Inbound Call: No appointment scheduled - " +
                        (_call.Any(a => a.FieldName.Equals("DetailedOutcome") && a.Visible && !String.IsNullOrEmpty(a.ValueData))
                        ? _call.Where(a => a.FieldName.Equals("DetailedOutcome") && a.Visible).Single().ValueData.ToString()
                        : "<Detailed Outcome data not found>");           // if null check is false, set to empty string;
                    break;
                default:
                    leadAction.Notes = "New Inbound Call: Unknown Outcome";
                    break;
            }

            // set up the lead detail
            LeadDetail _detail = new LeadDetail();
            _detail.LeadDate = DateTime.Now;
            string[] _nameParts = _call.Any(a => a.FieldName.Equals("Name") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.Single(a => a.FieldName.Equals("Name") && a.Visible).ValueData.ToString().Split(' ')
                : new string[]{"<First Name not found>", "<Last name not found>"};
            _detail.FirstName = String.IsNullOrEmpty(_nameParts[0])
                ? "<First Name not found>"
                : _nameParts[0];
            // put any remaining _nameParts into LastName field
            for (int i = 1; i < _nameParts.Length; i++)
            {
                _detail.LastName += _nameParts[i] + " ";
            }
            if (String.IsNullOrEmpty(_detail.LastName))
            {
                _detail.LastName = "<Last Name not found>";
            }
            else
            {
                _detail.LastName.Trim();
            }
            _detail.Address1 = _call.Any(a => a.FieldName.Equals("Address") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("Address") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.City = _call.Any(a => a.FieldName.Equals("City") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("City") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.State = _call.Any(a => a.FieldName.Equals("State") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("State") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.Zip = _call.Any(a => a.FieldName.Equals("ZipCode") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("ZipCode") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.Phone1 = _call.Any(a => a.FieldName.Equals("Phone") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("Phone") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.Email = _call.Any(a => a.FieldName.Equals("Email") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("Email") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.Frequency = _call.Any(a => a.FieldName.Equals("ServiceFrequency") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                ConvertServiceFrequency(_call.FirstOrDefault(a => a.FieldName.Equals("ServiceFrequency") && a.Visible).ValueData.ToString(),
                                        _call.FirstOrDefault().Need.ToString())
                : ConvertServiceFrequency("", _call.FirstOrDefault().Need.ToString());
            _detail.SquareFeet = _call.Any(a => a.FieldName.Equals("SquareFootage") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("SquareFootage") && a.Visible).ValueData.ToString() : "Value not found"; ;
            string[] _roomParts = _call.Any(a => a.FieldName.Equals("BedroomsBathroom") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("BedroomsBathroom") && a.Visible).ValueData.ToString().Split('/') :
                "Value not found/Value Not Found".Split('/');
            _detail.Bedrooms = _roomParts[0];
            _detail.Bathrooms = _roomParts[1];
            _detail.Pets = _call.Any(a => a.FieldName.Equals("Pets") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("Pets") && a.Visible).ValueData.ToString() : "Value not found";
            _detail.LeadSource = "Other";
            _detail.LeadStatus = "New Lead";
            _detail.NextAction = "Initial Contact";
            _detail.Comments = _call.Any(a => a.FieldName.Equals("OtherInfo") && a.Visible && !String.IsNullOrEmpty(a.ValueData)) ?
                _call.FirstOrDefault(a => a.FieldName.Equals("OtherInfo") && a.Visible).ValueData.ToString() : "";
            _detail.Phone1Type = "Home";
            _lead.LeadDetails = _detail;

            Outcome outcome = new Outcome();
            outcome.NeedType = _call.FirstOrDefault().Need.ToString();
            outcome.CallOutcome = _call.FirstOrDefault().Outcome.ToString();
            try
            {
                outcome.CallOutcome = _call.FirstOrDefault().Outcome.ToString();
                // ternary check for null
                outcome.DetailedCallOutcome = _call.Any(a => a.FieldName.Equals("DetailedOutcome") && a.Visible && !String.IsNullOrEmpty(a.ValueData))
                    ? _call       // if value is not null
                    .Where(a => a.FieldName.Equals("DetailedOutcome") && a.Visible)
                    .Single().ValueData.ToString()
                    : "";         // if value is null, set to empty string
                // ternary check for null
                outcome.AdditionalNotes = _call.Any(a => a.FieldName.Equals("OtherInfo") && a.Visible && !String.IsNullOrEmpty(a.ValueData))
                    ? _call       // if value is not null
                    .Where(a => a.FieldName.Equals("OtherInfo") && a.Visible)
                    .Single().ValueData.ToString()
                    : "";         // if value is null, set to empty string

            //                _los.eventLog.WriteEntry("CallOutcomeSubmission.SubmitFinalOutcome: " + outcome.DetailedCallOutcome + " :: " + outcome.AdditionalNotes);
            }
            catch (Exception e)
            {
                _los.eventLog.WriteEntry("CallOutcomeSubmission.SubmitFinalOutcome: " + e.Message);
            }

            leadAction.Outcome = outcome;
            _lead.LeadAction = leadAction;

            leadActionResult = client.SubmitLead(_lead);
            _los.eventLog.WriteEntry("CallOutcomeSubmission.SubmitFinalOutcome: leadActionResult = " + leadActionResult);

            return leadActionResult;
        }
Exemplo n.º 14
0
        public static Result <InvoiceLead> UpdateDetails(InvoiceLead invoice, IDataRepositoryFactory dataRepositoryFactory)
        {
            var detailsRepo      = dataRepositoryFactory.GetCustomDataRepositories <IInvoiceDetailRepository>();
            var productUnitsRepo = dataRepositoryFactory.GetCustomDataRepositories <IUnitProductEquivalenceRepository>();
            var oldDetails       = detailsRepo.GetByInvoiceId(invoice.Id).ToList();
            var newDetails       = invoice.LeadDetails.Where(x => x.Id == 0).ToList();
            var modifiedDetails  = invoice.LeadDetails.Except(newDetails).ToList().Except(oldDetails).ToList();
            var untouchedDetails = invoice.LeadDetails.Intersect(oldDetails).ToList();



            oldDetails.ForEach(d =>
            {
                bool existe = (modifiedDetails.Exists(x => x.ProductId == d.ProductId && d.ParentId == null) || untouchedDetails.Exists(x => x.ProductId == d.ProductId && d.ParentId == null));
                if (!existe)
                {
                    detailsRepo.Remove(d.Id);
                    var result = InventoryHelper.AddInventory(d, invoice, dataRepositoryFactory);
                    if (result.Status < 0)
                    {
                        throw new Exception(result.Message);
                    }
                }
            });


            InvoiceLead newinvoice = new InvoiceLead(invoice);

            newinvoice.LeadDetails = newDetails;
            AddDetails(newinvoice, dataRepositoryFactory);


            modifiedDetails.ForEach(d =>
            {
                if (!d.Product.IsService)
                {
                    var oldDetail           = oldDetails.Where(x => x.Id == d.Id).FirstOrDefault();
                    decimal currentQuantity = (decimal)ProductsHelper.ConvertToProductPrincipalUnit(
                        d.Quantity,
                        d.UnitId.Value,
                        d.Product.ProductUnits == null || d.Product.ProductUnits.Count() == 0 ? productUnitsRepo.GetProductUnits(d.ProductId).ToList() : d.Product.ProductUnits.ToList()
                        ).Data.FirstOrDefault();

                    decimal oldQuantity = (decimal)ProductsHelper.ConvertToProductPrincipalUnit(
                        oldDetail.Quantity,
                        oldDetail.UnitId.Value,
                        oldDetail.Product.ProductUnits == null || oldDetail.Product.ProductUnits.Count() == 0 ? productUnitsRepo.GetProductUnits(oldDetail.ProductId).ToList() : oldDetail.Product.ProductUnits.ToList()
                        ).Data.FirstOrDefault();
                    decimal difference = currentQuantity - oldQuantity;
                    if (difference > 0)
                    {
                        var warehouse = invoice.BranchOffice.Warehouses?.Where(y => y.Id == d.WarehouseId).FirstOrDefault();
                        if (warehouse == null)
                        {
                            throw new Exception("warehouseError_msg");
                        }


                        LeadDetail nuevoDetalle = new LeadDetail()
                        {
                            Product   = d.Product,
                            ProductId = d.ProductId,
                            Quantity  = difference,
                            UnitId    = d.UnitId ?? null,
                            Unit      = d.Unit ?? null
                        };

                        var result = InventoryHelper.UpdateInventory(nuevoDetalle, warehouse, invoice, dataRepositoryFactory);
                        if (result.Status < 0)
                        {
                            throw new Exception(result.Message);
                        }
                    }
                    else if (difference < 0)
                    {
                        LeadDetail detailToInsert = new LeadDetail(d);
                        detailToInsert.Quantity   = Math.Abs(difference);
                        detailToInsert.Unit       = d.Product.ProductUnits.Where(u => u.IsPrimary).FirstOrDefault().Unit;
                        detailToInsert.UnitId     = detailToInsert.Unit.Id;
                        var result = InventoryHelper.AddInventory(detailToInsert, invoice, dataRepositoryFactory);
                        if (result.Status < 0)
                        {
                            throw new Exception(result.Message);
                        }
                    }
                    detailsRepo.Update(d);
                }
                else
                {
                    detailsRepo.Update(d);
                }
            });

            UpdateInvoiceTaxes(invoice, dataRepositoryFactory);
            return(new Result <InvoiceLead>(0, 0, "ok_msg", new List <InvoiceLead>()
            {
                invoice
            }));
        }