public List<ProductModel> SaveProduct(ProductModel product)
 {
     try
     {
         List<ProductModel> result = new List<ProductModel>();
         using (FacturandoEntities context = new FacturandoEntities())
         {
             var productTemp = context.Product.Where(x => x.Id == product.Id).FirstOrDefault();
             if (productTemp == null)
             {
                 context.Product.Add(new Product
                 {
                     Id = product.Id,
                     Description = product.Description,
                     IdUnit = product.IdUnit,
                     Free = product.FreeProduct                            
                 });
             }
             else
             {
                 productTemp.Description = product.Description;
                 productTemp.IdUnit = product.IdUnit;
                 productTemp.Free = product.FreeProduct;
             }
             context.SaveChanges();
             result.Add(product);
             return result;
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
        public RemissionSaveModel SaveRemission(RemissionSaveModel remission)
        {
            try
            {
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    if (remission.Client.IsNew)
                    {
                        // verificar nuevamente si el cliente realmente no existe

                        var clientValidate = context.Client
                            .Where(x => x.IdIdentificationType == remission.Client.IdIdentificationType
                            && x.IdentificationNumber.Equals(remission.Client.IdentificationNumber)).FirstOrDefault();

                        if (clientValidate == null)
                        {
                            context.Client.Add(new Client
                            {
                                Address = remission.Client.Adress,
                                DisccountPercent = remission.Client.DiscountPercent,
                                Email = remission.Client.Email,
                                Id = remission.Client.Id,
                                IdIdentificationType = remission.Client.IdIdentificationType,
                                IdentificationNumber = remission.Client.IdentificationNumber,
                                Name = remission.Client.Name,
                                Phone = remission.Client.Phone,
                                DateEvent = DateTime.Now
                            });
                        }
                        else
                        {
                            remission.Client.Id = clientValidate.Id;
                            remission.Remission.IdClient = clientValidate.Id;
                        }
                    }

                    context.Remission.Add(new Remission
                    {
                        Id = remission.Remission.Id,
                        RemissionNumber = remission.Remission.RemissionNumber,
                        IdClient = remission.Remission.IdClient,
                        Total = remission.Remission.Total,
                        DateEvent = remission.Remission.DateEvent
                    });

                    foreach (var item in remission.RemissionDetail)
                    {
                        context.RemissionDetail.Add(new RemissionDetail
                        {
                            Id = item.Id,
                            IdRemission = item.IdRemission,
                            IdProduct = item.IdProduct,
                            Quantity = item.Quantity,
                            UnitPrice = item.UnitPrice,
                            Total = item.Total
                        });
                    }
                    context.SaveChanges();
                    return remission;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #3
0
        public List<BillModel> CancelBill(List<BillModel> billList)
        {
            try
            {
                DateTime cancelDate = DateTime.Now;
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    InventoryInterface inventoryData = new InventoryData();
                    var inventoryTypeList = inventoryData.GetInventoryType();
                    InventoryTypeModel inventoryTypeIn = inventoryTypeList.Where(x => x.Description.Equals("DevoluciĆ³n Cliente")).FirstOrDefault();
                    InventoryTypeModel inventoryTypeOut = inventoryTypeList.Where(x => x.Description.Equals("Venta")).FirstOrDefault();

                    foreach (var item in billList)
                    {
                        var billTemp = context.Bill.Where(x => x.Id == item.Id).FirstOrDefault();

                        // Simon Ariza - 10-03-2016 - inventory affect
                        if (billTemp.IsCanceled != item.IsCanceled)
                        {   
                            var billDetailTemp = context.BillDetail.Where(x => x.IdBill == item.Id).ToList();
                            foreach (var billProduct in billDetailTemp)
                            {
                                InventoryModel inventoryTemp = inventoryData.GetInventoryByProductId(billProduct.IdProduct.Value);
                                InventoryDetailModel inventoryDetailTemp = inventoryData.GetLastInventoryDetailInByProductId(billProduct.IdProduct.Value);
                                InventorySaveModel inventorySaveTemp = new InventorySaveModel {  Inventory = inventoryTemp, InventoryDetail = inventoryDetailTemp };
                                if (item.IsCanceled) // inventory in
                                {
                                    inventorySaveTemp.InventoryDetail.IdInventoryType = inventoryTypeIn.Id;                                    
                                }
                                else // inventory out
                                {
                                    inventorySaveTemp.InventoryDetail.IdInventoryType = inventoryTypeOut.Id;                                   
                                }
                                inventorySaveTemp.InventoryDetail.Quantity = billProduct.Quantity;
                                inventoryData.SaveInventory(inventorySaveTemp);
                            } 
                        }
                        //fin cambio

                        if (item.IsCanceled)
                        {
                            billTemp.IsCanceled = item.IsCanceled;
                            billTemp.CancelDate = cancelDate;
                            item.CancelDate = cancelDate;                            
                        }
                        else
                        {
                            billTemp.IsCanceled = item.IsCanceled;
                            billTemp.CancelDate = null;
                            item.CancelDate = null;
                        }
                    }
                    context.SaveChanges();
                    return billList;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 public List<ClientModel> SaveClient(List<ClientModel> clientList)
 {
     try
     {
         using (FacturandoEntities context = new FacturandoEntities())
         {
             foreach (var item in clientList)
             {
                 var clientTemp = context.Client.Where(x => x.Id == item.Id).FirstOrDefault();
                 if (clientTemp == null)
                 {
                     context.Client.Add(new Client
                     {
                         Id = item.Id,
                         Address = item.Adress,
                         DateEvent = item.DateEvent,
                         DisccountPercent = item.DiscountPercent,
                         Email = item.Email,
                         IdIdentificationType = item.IdIdentificationType,                                
                         IdentificationNumber = item.IdentificationNumber,
                         Name = item.Name,
                         Phone = item.Phone
                     });
                 }
                 else
                 {
                     clientTemp.Address = item.Adress;
                     clientTemp.DisccountPercent = item.DiscountPercent;
                     clientTemp.Email = item.Email;
                     clientTemp.Name = item.Name;
                     clientTemp.Phone = item.Phone;
                 }
             }
             context.SaveChanges();
             return clientList;
         }
     }
     catch (Exception)
     {
         throw;
     }
 }
Example #5
0
        public BillSaveModel SaveBill(BillSaveModel bill)
        {
            try
            {
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    if (bill.Client.IsNew)
                    {
                        // verificar nuevamente si el cliente realmente no existe

                        var clientValidate = context.Client
                            .Where(x => x.IdIdentificationType == bill.Client.IdIdentificationType
                            && x.IdentificationNumber.Equals(bill.Client.IdentificationNumber)).FirstOrDefault();

                        if (clientValidate == null)
                        {
                            context.Client.Add(new Client
                            {
                                Address = bill.Client.Adress,
                                DisccountPercent = bill.Client.DiscountPercent,
                                Email = bill.Client.Email,
                                Id = bill.Client.Id,
                                IdIdentificationType = bill.Client.IdIdentificationType,
                                IdentificationNumber = bill.Client.IdentificationNumber,
                                Name = bill.Client.Name,
                                Phone = bill.Client.Phone,
                                DateEvent = DateTime.Now
                            });
                        }
                        else
                        {
                            bill.Client.Id = clientValidate.Id;
                            bill.Bill.IdClient = clientValidate.Id;
                        }
                    }
                    context.Bill.Add(new Bill
                    {
                        Id = bill.Bill.Id,
                        BillNumber = bill.Bill.BillNumber,
                        IdClient = bill.Bill.IdClient,
                        Total = bill.Bill.Total,
                        DateEvent = bill.Bill.DateEvent
                    });
                    foreach (var item in bill.BillDetail)
                    {
                        context.BillDetail.Add(new BillDetail
                        {
                            Id = item.Id,
                            IdBill = item.IdBill,
                            IdProduct = item.IdProduct,
                            Quantity = item.Quantity,
                            UnitPrice = item.UnitPrice,
                            Total = item.Total
                        });
                    }
                    foreach (var item in bill.BillTaxes)
                    {
                        context.BillTaxes.Add(new BillTaxes
                        {
                            Id = item.Id,
                            IdBill = item.IdBill,
                            IdTax = item.IdTax,
                            Total = item.Total
                        });
                    }
                    context.SaveChanges();
                    return bill;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<UnitMeasureModel> SaveUnitMeasure(List<UnitMeasureModel> unitMeasures)
        {
            try
            {
                var unitUpdateList = unitMeasures.Where(x => !x.IsNewRegister).ToList();
                var unitNewList = unitMeasures.Where(x => x.IsNewRegister).ToList();
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    foreach (var item in unitUpdateList)
                    {
                        UnitMeasure unitTemp = context.UnitMeasure
                            .Where(x => x.Id == item.Id)
                            .FirstOrDefault();
                        unitTemp.Description = item.Description;
                    }
                    foreach (var item in unitNewList)
                    {
                        context.UnitMeasure.Add(new UnitMeasure
                        {
                            Id = item.Id,
                            Description = item.Description
                        });
                    }

                    context.SaveChanges();
                    return context.UnitMeasure.Select(x => new UnitMeasureModel
                    {
                        Id = x.Id,
                        Description = x.Description
                    }).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<ConfigurationModel> SaveConfiguration(List<ConfigurationModel> configurationList)
        {
            try
            {
                var configurationUpdateList = configurationList.Where(x => !x.IsNewRegister).ToList();
                var configurationNewList = configurationList.Where(x => x.IsNewRegister).ToList();
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    foreach (var item in configurationUpdateList)
                    {
                        ConfigurationSystem configurationModelTemp = context.ConfigurationSystem
                            .Where(x => x.Id == item.Id)
                            .FirstOrDefault();
                        configurationModelTemp.IdCountry = item.IdCountry;
                        configurationModelTemp.IdCurrency = item.IdCurrency;
                        configurationModelTemp.OperationsInitDate = item.OperationsInitDate;
                        configurationModelTemp.FiscalYear = item.FiscalYear;
                        configurationModelTemp.AuthorizedBillingInit = item.AuthorizedBillingInit;
                        configurationModelTemp.AuthorizedBillingEnd = item.AuthorizedbillingEnd;
                    }
                    foreach (var item in configurationNewList)
                    {
                        context.ConfigurationSystem.Add(new ConfigurationSystem
                        {
                            Id = item.Id,
                            FiscalYear = item.FiscalYear,
                            OperationsInitDate = item.OperationsInitDate,
                            AuthorizedBillingEnd = item.AuthorizedbillingEnd,
                            AuthorizedBillingInit = item.AuthorizedBillingInit,
                            IdCountry = item.IdCountry,
                            IdCurrency = item.IdCurrency
                        });
                    }

                    context.SaveChanges();
                    return context.ConfigurationSystem.Select(x => new ConfigurationModel
                    {
                        Id = x.Id,
                        AuthorizedbillingEnd = x.AuthorizedBillingEnd,
                        AuthorizedBillingInit = x.AuthorizedBillingInit,
                        FiscalYear = x.FiscalYear,
                        IdCountry = x.IdCountry.Value,
                        IdCurrency = x.IdCurrency.Value,
                        OperationsInitDate = x.OperationsInitDate
                    }).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<IdentificationTypeModel> SaveIdentificationType(List<IdentificationTypeModel> identificationTypes)
        {
            try
            {
                var identificationUpdateList = identificationTypes.Where(x => !x.IsNewRegister).ToList();
                var identificationNewList = identificationTypes.Where(x => x.IsNewRegister).ToList();
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    foreach (var item in identificationUpdateList)
                    {
                        IdentificacionType identificationTemp = context.IdentificacionType
                            .Where(x => x.Id == item.Id)
                            .FirstOrDefault();
                        identificationTemp.Description = item.Description;
                    }
                    foreach (var item in identificationNewList)
                    {
                        context.IdentificacionType.Add(new IdentificacionType
                        {
                            Id = item.Id,
                            Description = item.Description
                        });
                    }

                    context.SaveChanges();
                    return context.IdentificacionType.Select(x => new IdentificationTypeModel
                    {
                        Id = x.Id,
                        Description = x.Description
                    }).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<InventoryLocationModel> SaveInventoryLocation(List<InventoryLocationModel> locations)
        {
            try
            {
                var locationUpdateList = locations.Where(x => !x.IsNewRegister).ToList();
                var locationNewList = locations.Where(x => x.IsNewRegister).ToList();
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    foreach (var item in locationUpdateList)
                    {
                        InventoryLocation locationTemp = context.InventoryLocation
                            .Where(x => x.Id == item.Id)
                            .FirstOrDefault();
                        locationTemp.Description = item.Description;
                    }
                    foreach (var item in locationNewList)
                    {
                        context.InventoryLocation.Add(new InventoryLocation
                        {
                            Id = item.Id,
                            Description = item.Description
                        });
                    }

                    context.SaveChanges();
                    return context.InventoryLocation.Select(x => new InventoryLocationModel
                    {
                        Id = x.Id,
                        Description = x.Description
                    }).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public List<TaxModel> SaveTax(List<TaxModel> taxes)
        {
            try
            {
                var taxUpdateList = taxes.Where(x => !x.IsNewRegister).ToList();
                var taxNewList = taxes.Where(x => x.IsNewRegister).ToList();
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    foreach (var item in taxUpdateList)
                    {
                        Tax taxTemp = context.Tax
                            .Where(x => x.Id == item.Id)
                            .FirstOrDefault();
                        taxTemp.Description = item.Description;
                        taxTemp.PercentageValue = item.PercentageValue;
                    }
                    foreach (var item in taxNewList)
                    {
                        context.Tax.Add(new Tax
                        {
                            Id = item.Id,
                            Description = item.Description,
                            PercentageValue = item.PercentageValue
                        });
                    }

                    context.SaveChanges();
                    return context.Tax.Select(x => new TaxModel
                    {
                        Id = x.Id,
                        Description = x.Description,
                        PercentageValue = x.PercentageValue
                    }).ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public InventorySaveModel UpdateInventory(InventorySaveModel inventory)
        {
            try
            {
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    bool isNewDetail = false;
                    InventoryDetail inventoryDetail = context.InventoryDetail
                        .Where(x => x.Id == inventory.InventoryDetail.Id)
                        .ToList()
                        .FirstOrDefault();

                    int inventoryQuantityTemp = 0;
                    int actualQuantity = inventoryDetail.Quantity;
                    int newQuantity = inventory.InventoryDetail.Quantity;

                    string actualSign = inventoryDetail.InventoryType.Sign;
                    string newSign = context.InventoryType
                        .Where(x => x.Id == inventory.InventoryDetail.IdInventoryType)
                        .ToList()
                        .FirstOrDefault().Sign;

                    Inventory inventoryTemp = context.Inventory
                           .Where(x => x.Id == inventory.Inventory.Id)
                           .ToList()
                           .FirstOrDefault();

                    inventoryTemp.LastSalePrice = inventory.InventoryDetail.SalePrice;

                    if (actualSign.Equals("+") && newSign.Equals("+"))
                    {
                        inventoryQuantityTemp = newQuantity - actualQuantity;
                        inventoryTemp.Quantity += inventoryQuantityTemp;
                    }

                    if (actualSign.Equals("-") && newSign.Equals("-"))
                    {
                        inventoryQuantityTemp = newQuantity - actualQuantity;
                        inventoryTemp.Quantity -= inventoryQuantityTemp;
                    }

                    if (actualSign.Equals("-") && newSign.Equals("+"))
                    {
                        inventoryQuantityTemp = newQuantity;
                        inventoryTemp.Quantity += inventoryQuantityTemp;
                        isNewDetail = true;
                    }

                    if (actualSign.Equals("+") && newSign.Equals("-"))
                    {
                        inventoryQuantityTemp = newQuantity;
                        inventoryTemp.Quantity -= inventoryQuantityTemp;
                        isNewDetail = true;
                    }

                    if (isNewDetail)
                    {
                        SaveInventory(inventory);
                    }
                    else
                    {
                        inventoryDetail.BarCodeData = inventory.InventoryDetail.BarCodeData;
                        inventoryDetail.ConstructionDate = inventory.InventoryDetail.ConstructDate;
                        inventoryDetail.DueDate = inventory.InventoryDetail.ConstructDate;
                        inventoryDetail.EventDate = inventory.InventoryDetail.EventDate;
                        inventoryDetail.IdInventoryClassification = inventory.InventoryDetail.IdInventoryClassification;
                        inventoryDetail.IdInventoryLocalization = inventory.InventoryDetail.IdInventoryLocalization;
                        inventoryDetail.PurchasePrice = inventory.InventoryDetail.PurchasePrice;
                        inventoryDetail.SalePrice = inventory.InventoryDetail.SalePrice;
                        inventoryDetail.IdInventoryType = inventory.InventoryDetail.IdInventoryType;
                        inventoryDetail.Quantity = inventory.InventoryDetail.Quantity;
                    }
                    
                    context.SaveChanges();
                    return inventory;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }        
        public InventorySaveModel SaveInventory(InventorySaveModel inventory)
        {
            try
            {
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    Inventory inventoryExists = context.Inventory
                        .Where(x => x.IdProduct == inventory.Inventory.IdProduct).ToList().FirstOrDefault();

                    int newQuantity = inventory.InventoryDetail.Quantity;

                    string newSign = context.InventoryType
                            .Where(x => x.Id == inventory.InventoryDetail.IdInventoryType)
                            .ToList().FirstOrDefault().Sign;
                    if (newSign.Equals("-"))
                    {
                        newQuantity = newQuantity * -1;
                    }

                    if (inventoryExists != null)
                    {
                        inventory.Inventory.Id = inventoryExists.Id;
                        inventoryExists.Quantity += newQuantity;
                        inventoryExists.LastSalePrice = inventory.InventoryDetail.SalePrice;
                        inventory.Inventory.IdProduct = inventoryExists.IdProduct.Value;
                        inventory.Inventory.Quantity = inventoryExists.Quantity;
                    }
                    else
                    {
                        inventory.Inventory.Id = Guid.NewGuid();
                        context.Inventory.Add(new Inventory
                        {
                            Id = inventory.Inventory.Id,
                            IdProduct = inventory.Inventory.IdProduct,
                            Quantity = newQuantity,
                            LastSalePrice = inventory.InventoryDetail.SalePrice
                        });
                    }
                    inventory.InventoryDetail.Id = Guid.NewGuid();
                    context.InventoryDetail.Add(new InventoryDetail
                    {
                        Id = inventory.InventoryDetail.Id,
                        BarCodeData = inventory.InventoryDetail.BarCodeData,
                        ConstructionDate = inventory.InventoryDetail.ConstructDate,
                        DueDate = inventory.InventoryDetail.ConstructDate,
                        EventDate = inventory.InventoryDetail.EventDate,
                        IdInventory = inventory.Inventory.Id,
                        IdInventoryClassification = inventory.InventoryDetail.IdInventoryClassification,
                        IdInventoryLocalization = inventory.InventoryDetail.IdInventoryLocalization,
                        IdInventoryType = inventory.InventoryDetail.IdInventoryType,
                        PurchasePrice = inventory.InventoryDetail.PurchasePrice,
                        SalePrice = inventory.InventoryDetail.SalePrice,
                        Quantity = inventory.InventoryDetail.Quantity
                    });
                    context.SaveChanges();
                    return inventory;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }