Exemplo n.º 1
0
        private void btnPrintInvoice_Click(object sender, EventArgs e)
        {
            DateTime selectedDate = dpInvoiceDate.Value;

            /*if (cmbClientRef.SelectedIndex == -1)
             * {
             *  MessageBox.Show(this, "Please select client from list shown near 'Print Invoice' button.", "Client not selected", MessageBoxButtons.OK, MessageBoxIcon.Stop);
             *  cmbClientRef.Focus();
             *  return;
             * }*/

            double discount = 0;

            if (!double.TryParse(txtDiscount.Value.ToString(), out discount))
            {
                MessageBox.Show(this, "Please enter valid Discount value.", "Discount Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                txtDiscount.Focus();
                return;
            }


            System.Collections.IEnumerable items = lvItemsSale.Objects;

            String   deliveryAddress = txtDeliveryAddress.Text.Trim();
            DateTime?deliveryDate    = null;

            if (deliveryAddress.Length > 0)
            {
                deliveryDate = dtDelivery.Value;
            }

            long client = 0;// ((classes.Client)cmbClientRef.SelectedItem).id;

            if (cmbClientRef.SelectedIndex > -1)
            {
                client = (cmbClientRef.SelectedItem as classes.Client).id;
            }
            else
            {
                try
                {
                    string clientName = cmbClientRef.Text;
                    if (clientName.Trim().Length == 0)
                    {
                        MessageBox.Show(this, "Please enter valid client name." + Environment.NewLine + "Unable to add client entry, please try again.", "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        cmbClientRef.Focus();
                        return;
                    }
                    else
                    {
                        if (!Job.Database.addClient(clientName, "", "", "", "", "", ""))
                        {
                            MessageBox.Show(this, "Client name not added successfully into database." + Environment.NewLine + "Unable to add client entry, please try again.", "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            cmbClientRef.Focus();
                            return;
                        }
                        client = Job.Database.last_inserted_rowid();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Unable to add client entry, please try again." + Environment.NewLine + "Error Message: " + ex, "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbClientRef.Focus();
                    return;
                }
            }

            new System.Threading.Thread(() =>
            {
                try
                {
                    #region Thread

                    if (items != null)
                    {
                        List <classes.TransactionItem> tItems = new List <classes.TransactionItem>();
                        double totalAmount = 0;
                        #region Prepare TransactionData List
                        foreach (classes.Item item in items)
                        {
                            int counter = 0;
                            searchItem: classes.Item oldItem = Job.Database.findItem(item.itemCode, item.itemName);
                            if (oldItem == null)
                            {
                                try
                                {
                                    Job.Database.addItem(item.itemName, "", item.itemPPU);
                                }
                                catch (Exception) { }
                                counter++;
                                if (counter < 4)
                                {
                                    goto searchItem;
                                }
                            }

                            if (oldItem != null)
                            {
                                classes.TransactionItem tItem = new classes.TransactionItem();
                                tItem.discount = 0;
                                tItem.item     = oldItem.itemName;
                                tItem.itemCode = oldItem.itemCode;
                                tItem.itemId   = oldItem.id;
                                tItem.ppu      = item.itemPPU;
                                tItem.qty      = item.qty;
                                tItem.discount = item.discount;
                                totalAmount   += ((item.qty * item.itemPPU) - item.discount);
                                tItems.Add(tItem);
                            }
                        }
                        #endregion

                        #region Submit new Transaction

                        bool added = Job.Database.addTransaction(totalAmount - discount, discount, selectedDate, classes.Transaction.TransactionType.SALE, deliveryAddress, deliveryDate, tItems, client);
                        if (added)
                        {
                            Invoke(new Action(() =>
                            {
                                MessageBox.Show(this, "Your sale transaction successfully submited.", "Submitted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                btnClearSaleForm_Click(btnClearSaleForm, new EventArgs());
                                lvItemsSale.ClearObjects();

                                if (MessageBox.Show(this, "Do you want to print invoice now ?", "Print Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                                {
                                    Job.showInvoiceDialog(this, Job.Database.LastTransactionID);
                                }
                            }));
                        }
                        else
                        {
                            Invoke(new Action(() =>
                            {
                                MessageBox.Show(this, "Sorry, your new transaction isn't added into database, please try again.", "Problem while adding transaction", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }));
                        }

                        #endregion
                    }

                    #endregion
                }
                catch (Exception ex)
                {
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show(this, "Sorry, unable to add new sale transaction, please try again." + Environment.NewLine + "Error message : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }));
                }
                finally
                {
                    Invoke(new Action(() =>
                    {
                        Enabled = true;
                        loadItems();
                    }));
                }
            }).Start();

            Enabled = false;
        }
Exemplo n.º 2
0
        private void btnAddToStock_Click(object sender, EventArgs e)
        {
            DateTime selectedDate = dpInvoiceDate_Purchase.Value;

            /*if(cmbClientRefPurchase.SelectedIndex==-1)
             * {
             *  MessageBox.Show(this, "Please select client from list shown near to 'Add to Stock' button.", "Client not selected", MessageBoxButtons.OK, MessageBoxIcon.Stop);
             *  cmbClientRefPurchase.Focus();
             *  return;
             * }*/

            // iterate each added item
            // find existing if not add that new item
            // prepare transaction item list
            // add new transaction with items
            long clientId = 0;

            if (cmbClientRefPurchase.SelectedIndex > -1)
            {
                clientId = (cmbClientRefPurchase.SelectedItem as classes.Client).id;
            }
            else
            {
                try
                {
                    string clientName = cmbClientRefPurchase.Text;
                    if (clientName.Trim().Length == 0)
                    {
                        MessageBox.Show(this, "Please enter valid client name." + Environment.NewLine + "Unable to add client entry, please try again.", "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        cmbClientRefPurchase.Focus();
                        return;
                    }
                    else
                    {
                        if (!Job.Database.addClient(clientName, "", "", "", "", "", ""))
                        {
                            MessageBox.Show(this, "Client name not added successfully into database." + Environment.NewLine + "Unable to add client entry, please try again.", "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            cmbClientRefPurchase.Focus();
                            return;
                        }
                        clientId = Job.Database.last_inserted_rowid();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "Unable to add client entry, please try again." + Environment.NewLine + "Error Message: " + ex, "Client Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    cmbClientRefPurchase.Focus();
                    return;
                }
            }
            System.Collections.IEnumerable items = lvItemsPurchase.Objects;

            new System.Threading.Thread(() =>
            {
                try
                {
                    #region Thread

                    if (items != null)
                    {
                        List <classes.TransactionItem> tItems = new List <classes.TransactionItem>();
                        double totalAmount = 0;
                        #region Prepare TransactionData List
                        foreach (classes.Item item in items)
                        {
                            int counter = 0;
                            searchItem: classes.Item oldItem = Job.Database.findItem(item.itemCode, item.itemName);
                            if (oldItem == null)
                            {
                                try
                                {
                                    Job.Database.addItem(item.itemName, "", item.itemPPU);
                                }
                                catch (Exception) { }
                                counter++;
                                if (counter < 4)
                                {
                                    goto searchItem;
                                }
                            }

                            if (oldItem != null)
                            {
                                classes.TransactionItem tItem = new classes.TransactionItem();
                                tItem.discount = 0;
                                tItem.item     = oldItem.itemName;
                                tItem.itemCode = oldItem.itemCode;
                                tItem.itemId   = oldItem.id;
                                tItem.ppu      = item.itemPPU;
                                tItem.qty      = item.qty;
                                totalAmount   += (item.qty * item.itemPPU);
                                tItems.Add(tItem);
                            }
                        }
                        #endregion

                        #region Submit new Transaction

                        bool added = Job.Database.addTransaction(totalAmount, 0, selectedDate, classes.Transaction.TransactionType.PURCHASE, "", null, tItems, clientId);
                        if (added)
                        {
                            Invoke(new Action(() =>
                            {
                                MessageBox.Show(this, "Your purchase transaction successfully submited.", "Submitted", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                btnClearPurchaseForm_Click(btnClearPurchaseForm, new EventArgs());
                                lvItemsPurchase.ClearObjects();
                            }));
                        }
                        else
                        {
                            Invoke(new Action(() =>
                            {
                                MessageBox.Show(this, "Sorry, your new transaction isn't added into database, please try again.", "Problem while adding transaction", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }));
                        }

                        #endregion
                    }

                    #endregion
                }
                catch (Exception ex)
                {
                    Invoke(new Action(() =>
                    {
                        MessageBox.Show(this, "Sorry, unable to add new purchase transaction, please try again." + Environment.NewLine + "Error message : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }));
                }
                finally
                {
                    Invoke(new Action(() =>
                    {
                        Enabled = true;
                        loadItems();
                    }));
                }
            }).Start();

            Enabled = false;
        }