示例#1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (carDTO == null)
            {
                MessageBox.Show("Please choose a model!");
                return;
            }

            string sQuantity = txtQuantity.Text.Trim();
            int    Quantity  = int.Parse(sQuantity);

            if (Quantity <= 0)
            {
                MessageBox.Show("Quantity must be greater than 0!");
                return;
            }

            CarDAO carDAO          = new CarDAO();
            int    numberAvailable = carDAO.GetNumberAvailableCarByID(carDTO.ID);

            if (numberAvailable - Quantity < 0)
            {
                MessageBox.Show("Quantity is out of stock!");
                return;
            }

            string Fullname = txtFullname.Text.Trim();
            string Email    = txtEmail.Text.Trim();

            if (string.IsNullOrEmpty(Fullname) || string.IsNullOrEmpty(Email))
            {
                MessageBox.Show("Please complete customer's info!");
                return;
            }

            int insertCusResult = -1;

            if (cusDTO == null)
            {
                cusDTO = new CustomerDTO
                {
                    Fullname = Fullname,
                    Email    = Email,
                    Phone    = txtPhone.Text
                };

                CustomerDAO cusDAO = new CustomerDAO();
                insertCusResult = cusDAO.CreateCustomer(cusDTO);
                if (insertCusResult == -1)
                {
                    MessageBox.Show("Insert new customer failed!");
                    return;
                }
            }
            else
            {
                insertCusResult = cusDTO.ID;
            }

            InvoiceDAO invoiceDAO = new InvoiceDAO();
            int        invoiceID  = invoiceDAO.CreateNewInvoice(insertCusResult);

            InvoiceDetailsDAO invoiceDetailsDAO = new InvoiceDetailsDAO();
            bool createInvoiceDetails           = invoiceDetailsDAO.CreateInvoiceDetails(carDTO, invoiceID, Quantity);

            if (createInvoiceDetails)
            {
                MessageBox.Show("Add new invoice successfully!");
                cusDTO = null;
            }
            else
            {
                MessageBox.Show("Add new invoice failed!");
            }
            this.DialogResult = DialogResult.OK;
        }