private void CmbClientFullName_SelectedIndexChanged(object sender, EventArgs e)
        {
            BLAddress           bLAddress      = new BLAddress();
            ClientFullNameModel clientFullName = (ClientFullNameModel)cmbClientFullName.SelectedItem;

            cmbAddress.DataSource    = bLAddress.GetFullAddress(clientFullName.ClientId);
            cmbAddress.DisplayMember = "FullAddress";
        }
        //public invoicedetailform(int _id, int _userid)
        //{
        //    initializecomponent();
        //    userid = _userid;
        //    invoiceid = _id;
        //    blclient blclient = new blclient();
        //    cmbclientfullname.datasource = blclient.getclientfullname(0);
        //    cmbclientfullname.displaymember = "clientfullname";
        //}
        public InvoiceDetailForm(int _id, int _userId)
        {
            InitializeComponent();
            //userId = _userId;
            invoiceId = _id;
            userId    = _userId;

            BLInvoice bLInvoice             = new BLInvoice();
            List <DisplayInvoiceModel> list = bLInvoice.GetDisplayInvoice(invoiceId, "", DateTime.Now, DateTime.Now, "");
            DisplayInvoiceModel        displayInvoiceModel = new DisplayInvoiceModel();

            if (list.Count > 0)
            {
                displayInvoiceModel = list[0];
            }
            //txtAddress.Text = displayInvoiceModel.Address;
            //txtClientFullName.Text = displayInvoiceModel.ClientFullName;
            txtInvoiceNumber.Text = displayInvoiceModel.InvoiceNumber;
            dateInvoice.Value     = (displayInvoiceModel.InvoiceDate == DateTime.Parse("1/1/0001 12:00:00 AM")) ? DateTime.Now : displayInvoiceModel.InvoiceDate;

            BLClient bLClient = new BLClient();
            List <ClientFullNameModel> clients = bLClient.GetClientFullName(0);

            cmbClientFullName.DataSource = clients;



            ClientFullNameModel clientFullName = bLClient.GetClientFullName(displayInvoiceModel.ClientId)[0];

            //int index = clients.IndexOf(clientFullName);

            int index = clients.FindIndex(a => a.ClientId == clientFullName.ClientId);



            cmbClientFullName.SelectedIndex = index;
            //MessageBox.Show(cmbClientFullName.SelectedIndex.ToString());
            //cmbClientFullName.

            //MessageBox.Show(cmbClientFullName.SelectedItem.ClientId);

            //cmbClientFullName.

            cmbClientFullName.DisplayMember = "ClientFullName";
            //cmbClientFullName.DisplayMember = "ClientSurname";
            //txtInvoiceDate.Text = displayInvoiceModel.InvoiceDate.Year.ToString()+"-"+ displayInvoiceModel.InvoiceDate.Month.ToString()+"-"+ displayInvoiceModel.InvoiceDate.Day.ToString();
            txtObservations.Text = displayInvoiceModel.Observations;
            BLInvoiceDetail bLInvoiceDetail = new BLInvoiceDetail();

            dataGridDetails.DataSource = bLInvoiceDetail.GetInvoiceDetail(invoiceId);

            dataGridDetails.Columns["InvoiceDetailId"].Visible = false;
            dataGridDetails.Columns["ItemId"].Visible          = false;
            dataGridDetails.Columns["InvoiceId"].Visible       = false;
            dataGridDetails.Columns["Status"].Visible          = false;
            dataGridDetails.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        private void Save(int invoiceId, int userId)
        {
            BLInvoice       bLInvoice       = new BLInvoice();
            BLInvoiceDetail bLInvoiceDetail = new BLInvoiceDetail();

            if (userId != 0 & invoiceId == 0)
            {
                string              invoiceNumber  = txtInvoiceNumber.Text;
                DateTime            invoiceDate    = dateInvoice.Value;
                ClientFullNameModel clientFullName = (ClientFullNameModel)cmbClientFullName.SelectedItem;
                FullAddressModule   fullAddress    = (FullAddressModule)cmbAddress.SelectedItem;
                if (fullAddress == null)
                {
                    MessageBox.Show("Clientul nu are o adresa, adaugati o adresa clientului pentru a creea o factura!");
                    return;
                }
                int    clientId        = clientFullName.ClientId;
                int    clientAddressId = fullAddress.ClientAddressId;
                string observations    = txtObservations.Text;

                MessageBox.Show(clientAddressId.ToString());

                bLInvoice.AddInvoice(invoiceNumber, invoiceDate, clientId, clientAddressId, userId, observations);
                MessageBox.Show("Factura a fost adaugata cu succes.");
                int invoiceIdValue = bLInvoice.GetInvoiceList(-1, "", DateTime.Now, DateTime.Now, invoiceNumber)[0].InvoiceId;
                Save(invoiceIdValue, 0);
            }
            if (userId == 0 & invoiceId != 0)
            {
                for (int i = 0; i < dataGridDetails.Rows.Count; i++)
                {
                    InvoiceDetailModel detail = new InvoiceDetailModel();
                    detail = (InvoiceDetailModel)dataGridDetails.Rows[i].DataBoundItem;
                    if (detail.Status == 1)
                    {
                        bLInvoiceDetail.AddDetail(invoiceId, detail.ItemName, detail.Qtty, detail.UnitPrice, detail.Amount, detail.Observations);
                    }
                    //INSERT
                    if (detail.Status == 2)
                    {
                        bLInvoiceDetail.UpdateDetail(invoiceId, detail.InvoiceDetailId, detail.ItemName, detail.Qtty, detail.UnitPrice, detail.Amount, detail.Observations);
                    }



                    //UPDATE
                    if (detail.Status == 3)
                    {
                        bLInvoiceDetail.DeleteDetail(invoiceId, detail.InvoiceDetailId);
                    }
                }
            }
        }