Exemplo n.º 1
0
        private void GenerateInvoice(DataClasses1DataContext dbContext, bool preview)
        {
            //Adress Eigenschaften
            string street = string.Empty,
            streetNumber = string.Empty,
            zipcode = string.Empty,
            city = string.Empty,
            country = string.Empty,
            invoiceRecipient = string.Empty,
            footerText = string.Empty;
            // OrderItem Eigenschaften
            string ProductName = string.Empty,
            Amount = string.Empty;
            Guid customerId = new Guid(CustomerDropDownList.SelectedValue);
            street = StreetTextBox.Text;
            streetNumber = StreetNumberTextBox.Text;
            zipcode = ZipcodeTextBox.Text;
            city = CityTextBox.Text;
            country = CountryTextBox.Text;
            invoiceRecipient = InvoiceRecipient.Text;
            footerText = FooterTextBox.Text;
            Invoice newInvoice = null;
            Adress newAdress = null;
            try
            {
                newAdress = Adress.CreateAdress(street, streetNumber, zipcode, city, country, dbContext);
                newInvoice = Invoice.CreateInvoice(dbContext, new Guid(Session["CurrentUserId"].ToString()), invoiceRecipient, newAdress.Id, new Guid(CustomerDropDownList.SelectedValue), null, getFullInvoiceName(RechnungsTypComboBox.SelectedValue));
                newInvoice.InvoiceText = footerText;
                //Submiting new Invoice and Adress
                if (!preview)
                {
                    dbContext.SubmitChanges();
                }
                else
                {
                    newInvoice.Adress = newAdress;
                    newInvoice.Customer = dbContext.Customer.SingleOrDefault(q => q.Id == new Guid(CustomerDropDownList.SelectedValue));
                }
                List<SelectedInvoiceItems> virtualItems = new List<SelectedInvoiceItems>();
                SelectedInvoiceItems currItem = new SelectedInvoiceItems();
                foreach (GridDataItem item in RadGridAbrechnung.SelectedItems)
                {
                    currItem = new SelectedInvoiceItems();
                    currItem.ProductName = item["ProductName"].Text;
                    currItem.Amount = decimal.Parse(item["Amount"].Text, NumberStyles.Currency).ToString();

                    currItem.OrderItemId = new Guid(item["OrderItemId"].Text);
                    if (!String.IsNullOrEmpty(item["CostCenterId"].Text) && item["CostCenterId"].Text.Length > 8)
                    {
                        currItem.CostCenterId = new Guid(item["CostCenterId"].Text);
                    }
                    currItem.ItemCount = Convert.ToInt32(item["ItemCount"].Text);
                    currItem.OrderLocationName = item["Location"].Text == "&nbsp;" ? "" : item["Location"].Text;
                    currItem.OrderId = new Guid(item["OrderId"].Text);
                    currItem.OrderLocationId = !EmptyStringIfNull.IsGuid(item["OrderLocation"].Text) ? Guid.Empty : new Guid(item["OrderLocation"].Text);
                    virtualItems.Add(currItem);
                }
                var groupedInvoiceItems = virtualItems.GroupBy(q => q.OrderLocationId.ToString()).ToList();
                if (groupedInvoiceItems.Count > 0 && Session["currentLocationIndex"] != null && EmptyStringIfNull.IsNumber(Session["currentLocationIndex"].ToString()))
                {
                    var myCustomer = dbContext.Customer.FirstOrDefault(q => q.Id == customerId);
                    using (TransactionScope scope = new TransactionScope())
                    {
                        foreach (var item in groupedInvoiceItems[((int)Session["currentLocationIndex"])])
                        {
                            InvoiceItem newInvoiceItem = newInvoice.AddInvoiceItem(item.ProductName, Convert.ToDecimal(item.Amount), item.ItemCount, item.OrderItemId, item.CostCenterId, dbContext);
                            if (newInvoiceItem != null)
                            {
                                var OrderItemToUpdate = dbContext.OrderItem.SingleOrDefault(q => q.Id == item.OrderItemId);
                                OrderItemToUpdate.LogDBContext = dbContext;
                                OrderItemToUpdate.Status = 900;
                                if (myCustomer.SmallCustomer != null)
                                {
                                    newInvoiceItem.VAT = myCustomer.VAT;
                                }
                                dbContext.SubmitChanges();
                                UpdateOrderStatus(dbContext, item.OrderId);
                            }
                        }
                        if (!preview)
                        {
                            dbContext.SubmitChanges();
                            scope.Complete();

                        }
                        else if (groupedInvoiceItems.Count > 0)
                        {
                            using (MemoryStream memS = new MemoryStream())
                            {
                                string fileName = "RechnungsVorschau_" + DateTime.Now.Ticks.ToString() + ".pdf";
                                string serverPath = ConfigurationManager.AppSettings["DataPath"] + "\\UserData";
                                newInvoice.PrintPreview(dbContext, memS, "");
                                if (!Directory.Exists(serverPath)) Directory.CreateDirectory(serverPath);
                                if (!Directory.Exists(serverPath + "\\" + Session["CurrentUserId"].ToString())) Directory.CreateDirectory(serverPath + "\\" + Session["CurrentUserId"].ToString());
                                serverPath = serverPath + "\\" + Session["CurrentUserId"].ToString();
                                File.WriteAllBytes(serverPath + "\\" + fileName, memS.ToArray());
                                OpenPrintfile(fileName);
                            }
                        }
                        AllesIstOkeyLabel.Text = "Rechnung für " + myCustomer.Name + " wurde erfolgreich hinzugefügt";
                    }
                        if (Session["currentLocationIndex"] != null && ((int)Session["currentLocationIndex"]) < groupedInvoiceItems.Count() - 1)
                        {
                            Session["currentLocationIndex"] = ((int)Session["currentLocationIndex"]) + 1;
                            AddAdressButton_Click(this, new EventArgs());
                        }
                        else
                        {
                            if (!preview)
                            {
                                RadGridAbrechnung.MasterTableView.ClearChildEditItems();
                                RadGridAbrechnung.MasterTableView.ClearEditItems();
                                RadGridAbrechnung.Rebind();
                                string script = "function f(){$find(\"" + AddAdressRadWindow.ClientID + "\").close(); Sys.Application.remove_load(f);}Sys.Application.add_load(f); $find(\"" + RadGridAbrechnung.ClientID + "\").get_masterTableView().rebind();";
                                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true);
                            }
                            Session["currentLocationIndex"] = 0;
                        }
                }
                else
                {
                    throw new Exception("Achtung die Session ist abgelaufen! Bitte loggen Sie sich erneut ein.");
                }
            }
            catch (Exception ex)
            {
                try
                {
                    if (newInvoice != null)
                    {

                        dbContext.Invoice.DeleteOnSubmit(newInvoice);
                        dbContext.SubmitChanges();
                        if (newAdress != null)
                            dbContext.Adress.DeleteOnSubmit(newAdress);
                            dbContext.SubmitChanges();
                    }
                }
                catch { AbrechnungSaveErrorLabel.Text = "Leider war es nicht möglich durch das System die defekte Rechnung zu löschen (Datenbankkonflikt), Sie können diese auch manuell  im Reiter 'Rechnung erstellen' stornieren"; }
                AbrechnungSaveErrorLabel.Visible = true;
                AbrechnungSaveErrorLabel.Text += "Fehler:" + ex.Message;
               // WindowManager1.RadAlert("Fehler:" + ex.Message, 380, 180, "Fehler", "");
            }
        }
Exemplo n.º 2
0
 protected bool SetValuesForAdressWindow()
 {
     DataClasses1DataContext dbContext = new DataClasses1DataContext();
     Adress newAdress = null;
     AbrechnungSaveErrorLabel.Visible = false;
     if (String.IsNullOrEmpty(CustomerDropDownList.SelectedValue))
     {
         AbrechnungSaveErrorLabel.Visible = true;
         AbrechnungSaveErrorLabel.Text = "Bitte wählen Sie einen Kunden aus!";
         return true;
     }
     if (RadGridAbrechnung.SelectedItems.Count <= 0)
     {
         AbrechnungSaveErrorLabel.Visible = true;
         AbrechnungSaveErrorLabel.Text = "Bitte wählen Sie mindestens eine Auftragsposition aus!";
         return true;
     }
         try
         {
         List<SelectedInvoiceItems> virtualItems = new List<SelectedInvoiceItems>();
         SelectedInvoiceItems currItem = new SelectedInvoiceItems();
         foreach (GridDataItem item in RadGridAbrechnung.SelectedItems)
         {
                 currItem = new SelectedInvoiceItems();
                 currItem.ProductName = item["ProductName"].Text;
                 currItem.Amount = decimal.Parse(item["Amount"].Text, NumberStyles.Currency).ToString();
                 currItem.OrderItemId = new Guid(item["OrderItemId"].Text);
                 if (!String.IsNullOrEmpty(item["CostCenterId"].Text) && item["CostCenterId"].Text.Length > 8)
                 {
                     currItem.CostCenterId = new Guid(item["CostCenterId"].Text);
                 }
                 currItem.ItemCount = Convert.ToInt32(item["ItemCount"].Text);
                 currItem.OrderLocationName=item["Location"].Text;
                 currItem.OrderId = new Guid(item["OrderId"].Text);
                 currItem.OrderLocationId=!EmptyStringIfNull.IsGuid(item["OrderLocation"].Text)? Guid.Empty : new Guid(item["OrderLocation"].Text);
                 virtualItems.Add(currItem);
         }
         var groupedInvoiceItems = virtualItems.GroupBy(q => q.OrderLocationId).ToList();
         if (groupedInvoiceItems.Count > 0 && Session["currentLocationIndex"] != null && EmptyStringIfNull.IsNumber(Session["currentLocationIndex"].ToString()))
         {
             if(new Guid(groupedInvoiceItems[((int)Session["currentLocationIndex"])].Key.ToString())!=Guid.Empty)
             newAdress = Invoice.GetInitialInvoiceAdress(new Guid(CustomerDropDownList.SelectedValue), new Guid(groupedInvoiceItems[((int)Session["currentLocationIndex"])].Key.ToString()), dbContext);
             else
                 newAdress = Invoice.GetInitialInvoiceAdress(new Guid(CustomerDropDownList.SelectedValue), null, dbContext);
             StreetTextBox.Text = newAdress.Street;
             StreetNumberTextBox.Text = newAdress.StreetNumber;
             ZipcodeTextBox.Text = newAdress.Zipcode;
             CityTextBox.Text = newAdress.City;
             CountryTextBox.Text = newAdress.Country;
             FooterTextBox.Text = Invoice.GetDefaultInvoiceText(dbContext, new Guid(CustomerDropDownList.SelectedValue), new Guid(Session["CurrentUserId"].ToString()));
             InvoiceRecipient.Text = groupedInvoiceItems[((int)Session["currentLocationIndex"])].First().OrderLocationName == "&nbsp;" ? "" : groupedInvoiceItems[((int)Session["currentLocationIndex"])].First().OrderLocationName;
             AllesIstOkeyLabel.Text = string.Empty;
         }
         else
         {
             throw new Exception("Achtung die Session ist abgelaufen! Bitte loggen Sie sich erneut ein.");
         }
         }
         catch(Exception ex)
         {
             WindowManager1.RadAlert("Fehler:" + Server.HtmlEncode(ex.Message).RemoveLineEndings(), 380, 180, "Fehler", "");
             AbrechnungSaveErrorLabel.Visible = true;
             RadGridAbrechnung.DataBind();
             return true;
         }
     LocationLabelWindow.Text = "Fügen Sie bitte die Adresse für " + InvoiceRecipient.Text + " hinzu";
     return false;
 }