Exemplo n.º 1
0
        public void SaveInvoice()
        {
            try
            {
                ServiceController sc       = new ServiceController();
                string            INV_Nmbr = "";
                DataTable         sdt      = new POS.InvoiceDataTable();
                if (Items.Count > 0)
                {
                    int    length       = 4;
                    string reference_no = sc.GetMaxId();
                    if (reference_no != "")
                    {
                        string[] lines = Regex.Split(reference_no, "/");
                        int      nmbr  = Convert.ToInt32(lines[0]);
                        nmbr     = nmbr + 1;
                        INV_Nmbr = nmbr.ToString().PadLeft(length, '0');
                    }

                    string CurrentMachineName    = Environment.MachineName;
                    List <ModelService> services = new List <ModelService>();
                    TTL = 0;
                    foreach (ListViewItem item in Items)
                    {
                        ModelService service = new ModelService()
                        {
                            ItemCode    = item.ItemCode,
                            sellprice   = item.ItemPrice,
                            costprice   = item.ItemTotalPrice,
                            INVDate     = DateTime.Now,
                            DisAmount   = (float)footer.discount,
                            ReAmount    = (float)(TTL - footer.discount),
                            INVNO       = INV_Nmbr,
                            comp_id     = 1,
                            ModeofPay   = "Cash",
                            FinYear     = "2014-2015",
                            Party       = "1",
                            Remarks     = "Thnaks",
                            Tr_Time     = DateTime.Now.TimeOfDay,
                            TrType      = 3,
                            userid      = "1",
                            Vkey        = "1",
                            workstation = CurrentMachineName,
                            BalAmount   = 0F,
                        };
                        // ModelWorker worker = item.GetSelectedWorker();
                        //service.WorkerID = worker.ID;
                        services.Add(service);
                        TTL = item.SetTotalPaisay(ref TTL);
                    }

                    OrderController saver = new OrderController();
                    string          error = "";
                    foreach (ModelService item in services)
                    {
                        if (!saver.AddNewSale(ref error, INV_Nmbr, Global.UserId + "", item))
                        {
                            MetroMessageBox.Show(this, error, "Sale not saved ");
                        }
                    }
                    error = "";
                    //  if (!saver.AddNewOrder(ref error, reference_no, services))
                    // {
                    //    MetroMessageBox.Show(this, error, "Order not saved successfully");
                    // }
                    foreach (ListViewItem item in Items)
                    {
                        String  _User = Global.Name;
                        DataRow nRow  = sdt.NewRow();
                        nRow["Service"]         = item.ItemTitle + "";
                        nRow["User"]            = _User;
                        nRow["Worker"]          = "Admin";
                        nRow["Price"]           = item.ItemPrice;
                        nRow["Discount"]        = item.discount.Value;
                        nRow["GrandTotal"]      = TTL;
                        nRow["OverAllDiscount"] = footer.discount_amount;
                        nRow["ItemCode"]        = item.ItemCode + "";
                        nRow["DiscountedTotal"] = footer.TTL;
                        sdt.Rows.Add(nRow);
                    }
                    POS ds = new POS();
                    ds.Merge(sdt);
                    FrmInvoice invoice = new FrmInvoice();
                    invoice.dataset = ds;
                    invoice.SetDataAdapter();
                    ClearList();
                    invoice.ShowDialog(this);
                }
                else
                {
                    MessageBox.Show("Nothing to save", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }