public VisorFactura(BillSaveModel bill)
 {
     InitializeComponent();
     _bill.Client = new List<ClientModel>();
     _bill.Client.Add(bill.Client);
     _bill.Bill = new List<BillModel>();
     _bill.Bill.Add(bill.Bill);
     _bill.BillDetail = bill.BillDetail;
     _bill.BillTaxes = bill.BillTaxes;
     _bill.BillText = new List<BillText>();
     _bill.BillText.Add(AppText.Instance.AppTextData.BillData);
 }
        public VisorFactura(BillSaveModel bill, bool readOnly)
        {
            InitializeComponent();
            _bill.Client = new List<ClientModel>();
            _bill.Client.Add(bill.Client);
            _bill.Bill = new List<BillModel>();
            _bill.Bill.Add(bill.Bill);
            _bill.BillDetail = bill.BillDetail;
            _bill.BillTaxes = bill.BillTaxes;
            _bill.BillText = new List<BillText>();
            _bill.BillText.Add(AppText.Instance.AppTextData.BillData);

            btnNuevaFactura.Enabled = false;
        }
        public BillSaveModel GetBillData(BillModel bill)
        {
            try
            {
                using (FacturandoEntities context = new FacturandoEntities())
                {
                    BillSaveModel result = new BillSaveModel();
                    result.Bill = bill;
                    result.BillDetail = context.BillDetail
                        .Where(x => x.IdBill == result.Bill.Id)
                        .Select(x => new BillDetailModel
                        {
                            Id = x.Id,
                            IdBill = x.IdBill,
                            IdProduct = x.IdProduct.Value,
                            Product = string.Concat(x.Product.Description, " ", x.Product.UnitMeasure.Description),
                            Quantity = x.Quantity,
                            Total = x.Total,
                            UnitPrice = x.UnitPrice != null ? x.UnitPrice.Value : 0
                        }).ToList();

                    result.Client = context.Client
                        .Where(x => x.Id == result.Bill.IdClient)
                        .Select(x => new ClientModel
                        {
                            Id = x.Id,
                            Adress = x.Address,
                            DateEvent = x.DateEvent != null ? x.DateEvent.Value : DateTime.MinValue,
                            DiscountPercent = x.DisccountPercent,
                            Email = x.Email,
                            IdentificationNumber = x.IdentificationNumber,
                            IdIdentificationType = x.IdIdentificationType.Value,
                            Name = x.Name,
                            Phone = x.Phone
                        }).FirstOrDefault();

                    result.BillTaxes = context.BillTaxes
                        .Where(x => x.IdBill == result.Bill.Id)
                        .Select(x => new BillTaxesModel
                        {
                            Id = x.Id,
                            Description = x.Tax.Description,
                            IdBill = x.IdBill.Value,
                            IdTax = x.IdTax.Value,
                            PercentageValue = x.Tax.PercentageValue,
                            Total = x.Total
                        }).ToList();

                    return result;

                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        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;
            }
        }
        private void ClearControls()
        {
            _billData = new BillData();
            _inventoryData = new InventoryData();
            _client = null;
            _billDetail = new List<BillDetailModel>();
            _bill = new BillModel();
            _billTaxes = new List<BillTaxesModel>();
            _billSaveModel = new BillSaveModel();

            _bill = new BillModel
            {
                Id = Guid.NewGuid(),
                BillNumber = _billData.GetBillNumber(),
                DateEvent = DateTime.Now
            };
            lblNumeroFactura.Text = _bill.BillNumber.ToString().PadLeft(8, '0');
            _billTaxes = _billData.GetBillTaxes();
            _billTaxes.ForEach(x => x.IdBill = _bill.Id);
            dtgImpuestos.DataSource = _billTaxes;
            lblGranTotal.Text = "$0,00";
            txtSubTotal.Text = string.Format("{0:0.00}", 0.00);
            txtDescuentoFinal.Text = string.Format("{0:0.00}", 0.00);
            txtDescuentoCliente.Text = string.Format("{0:0.00}", 0.00);
            dtgDetalleFactura.DataSource = new List<BillDetailModel>();
            txtIdentificacionCliente.Text = string.Empty;
            txtNombreCliente.Text = string.Empty;
            txtDireccion.Text = string.Empty;
            txtTelefono.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtCodigoBarras.Text = string.Empty;
            txtNombreProducto.Text = string.Empty;
            lstProducto.DataSource = new List<InventoryModel>();
            btnBuscarProducto.Enabled = false;
            btnFacturar.Enabled = false;
        }