Exemple #1
0
        public void EndEdit()
        {
            DataSource.VatRate lUpdateItem = lDataContext.VatRates.SingleOrDefault(p => p.Id == this.Id);

            if (lUpdateItem == null)
            {
                DataSource.VatRate lNewItem = new DataSource.VatRate
                {
                    Name = this.Name,
                    Rate = this.Rate
                };

                // Add the new object to the Orders collection.
                lDataContext.VatRates.InsertOnSubmit(lNewItem);

                // Submit the change to the database.
                try
                {
                    lDataContext.SubmitChanges();

                    this.Id = lNewItem.Id;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
                lUpdateItem.Name = this.Name;
                lUpdateItem.Rate = this.Rate;

                lDataContext.SubmitChanges();
            }
        }
Exemple #2
0
        private Boolean SavingEnded()
        {
            //System.Threading.Thread.Sleep(2000);


            DataSource.Invoice lInvoice = lDataContext.Invoices.SingleOrDefault(p => p.Id == this.Invoice.Id);


            if (lInvoice == null)
            {
                this.Invoice.InvoiceNo = GetInvoiceNo();

                DataSource.Invoice newInvoice = new DataSource.Invoice
                {
                    CustomerId    = this.CustomerId,
                    InvoiceNo     = this.Invoice.InvoiceNo,
                    InvoiceDate   = this.Invoice.InvoiceDate,
                    DeliveryDate  = this.Invoice.DeliveryDate,
                    Description   = this.Invoice.Description,
                    Status        = this.Invoice.InvoiceStatusId,
                    EmpDeliveryId = this.DelivererId
                };

                lDataContext.Invoices.InsertOnSubmit(newInvoice);

                try
                {
                    lDataContext.SubmitChanges();

                    this.Invoice.Id = newInvoice.Id;
                }
                catch (SqlException ex)
                {
                    if (ex.Number == 2627)
                    {
                        MessageBox.Show("Cannot insert duplicate Invoice No [" + this.Invoice.InvoiceNo + "] Please save again.");
                    }
                    else
                    {
                        MessageBox.Show("SqlException : " + ex.Message);
                    }

                    ChangeSet pendingChanges = lDataContext.GetChangeSet();

                    foreach (object obj in pendingChanges.Inserts)
                    {
                        var tableToDeleteFrom = lDataContext.GetTable(obj.GetType());
                        //(tableToDeleteFrom as DataSource.Invoice).InvoiceNo = GetInvoiceNo();

                        //lDataContext.SubmitChanges();

                        //this.Invoice.Id = newInvoice.Id;

                        tableToDeleteFrom.DeleteOnSubmit(obj);
                    }

                    return(false);
                }
                catch (Exception ex)
                {
                    this.Message = "Faled to Save - Invoice Detail";
                    return(false);
                }
            }
            else
            {
                lInvoice.CustomerId    = this.CustomerId;
                lInvoice.InvoiceNo     = this.Invoice.InvoiceNo;
                lInvoice.InvoiceDate   = this.Invoice.InvoiceDate;
                lInvoice.DeliveryDate  = this.Invoice.DeliveryDate;
                lInvoice.Description   = this.Invoice.Description;
                lInvoice.EmpDeliveryId = this.DelivererId;
                lInvoice.Status        = this.Invoice.InvoiceStatusId;
                try
                {
                    lDataContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Message = "Faled to Update - Invoice Detail";
                    return(false);
                }
            }


            DataSource.Order lOrder = lDataContext.Orders.SingleOrDefault(p => p.Id == this.Id);

            if (lOrder == null)
            {
                DataSource.Order lnewOrder = new DataSource.Order
                {
                    CustomerId    = this.CustomerId,
                    OrderDate     = this.OrderDate.Value,
                    InvoiceId     = (this.Invoice == null) ? null : this.Invoice.Id,
                    EmpDeliveryId = this.DelivererId,
                    EmployeeId    = this.SellerId,
                    RequiredDate  = DateTime.Now,
                    ShippedDate   = DateTime.Now,
                    Description   = this.Description
                };


                lDataContext.Orders.InsertOnSubmit(lnewOrder);

                try
                {
                    lDataContext.SubmitChanges();
                    this.Id = lnewOrder.Id;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Message = "Faled to Save - Order Detail";
                    return(false);
                }
            }
            else
            {
                lOrder.CustomerId    = this.CustomerId;
                lOrder.OrderDate     = this.OrderDate.Value;
                lOrder.InvoiceId     = this.Invoice.Id;
                lOrder.EmpDeliveryId = this.DelivererId;
                lOrder.EmployeeId    = this.SellerId;
                lOrder.RequiredDate  = DateTime.Now;
                lOrder.ShippedDate   = DateTime.Now;
                lOrder.Description   = this.Description;

                try
                {
                    lDataContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    this.Message = "Faled to Update - Order Detail";
                    return(false);
                }
            }


            foreach (HSSupplyItem l in this.SupplyItems)
            {
                if (l.Id == -1 || l.Id == null)
                {
                    DataSource.OrderItem lDSOrderItem = new OrderItem();

                    lDSOrderItem.OrderId       = this.Id.Value;
                    lDSOrderItem.SalesStatusId = l.SalesStatusId;
                    lDSOrderItem.ProductId     = l.ProductId;
                    lDSOrderItem.VatId         = l.VatId;
                    lDSOrderItem.UnitPrice     = l.UnitPrice;
                    lDSOrderItem.Quantity      = l.Quantity;
                    lDSOrderItem.SellingUnitId = (l.SellingUnitId == null) ? 0 : l.SellingUnitId;
                    lDSOrderItem.Description   = l.Description;
                    lDataContext.OrderItems.InsertOnSubmit(lDSOrderItem);

                    try
                    {
                        lDataContext.SubmitChanges();
                        l.Id = lDSOrderItem.Id;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        this.Message = "Faled to Save - OrderItem Detail";
                        return(false);
                    }
                }
                else
                {
                    DataSource.OrderItem lItem = lDataContext.OrderItems.SingleOrDefault(p => p.Id == l.Id);

                    lItem.OrderId       = this.Id.Value;
                    lItem.SalesStatusId = l.SalesStatusId;
                    lItem.ProductId     = l.ProductId;
                    lItem.VatId         = l.VatId;
                    lItem.UnitPrice     = l.UnitPrice;
                    lItem.Quantity      = l.Quantity;
                    lItem.SellingUnitId = (l.SellingUnitId == null) ? 0 : l.SellingUnitId;
                    lItem.Description   = l.Description;

                    try
                    {
                        lDataContext.SubmitChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        this.Message = "Faled to Update - OrderItem Detail";
                        return(false);
                    }
                }
            }

            this.Message = "Saved Successfully...";

            return(false);
        }
Exemple #3
0
        public void EndEdit()
        {
            Boolean lChanged = false;

            DataSource.Employee lUpdate = lDataContext.Employees.SingleOrDefault(p => p.Id == this.Id);


            if (lUpdate == null)
            {
                DataSource.Employee newItem = new DataSource.Employee
                {
                    PersonId = this.PersonId,
                    DateIn   = this.DateIn,
                    DateOut  = this.DateOut,
                };
                // Add the new object to the Orders collection.
                lDataContext.Employees.InsertOnSubmit(newItem);

                // Submit the change to the database.
                try
                {
                    lDataContext.SubmitChanges();

                    this.Id = newItem.Id;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            else
            {
                lUpdate.PersonId = this.PersonId;
                lUpdate.DateIn   = this.DateIn;
                lUpdate.DateOut  = this.DateOut;

                lDataContext.SubmitChanges();
            }

            DataSource.Emp_Dept lEmpDept;

            foreach (HSDepartment l in this.Departments)
            {
                lEmpDept = lDataContext.Emp_Depts.SingleOrDefault(p => p.EmployeeId == this.Id && p.DepartmentId == l.Id);


                if (lEmpDept == null)
                {
                    if (l.IsSelected)
                    {
                        DataSource.Emp_Dept newEmpDept = new DataSource.Emp_Dept
                        {
                            EmployeeId   = this.Id,
                            DepartmentId = l.Id
                        };
                        // Add the new object to the Orders collection.
                        lDataContext.Emp_Depts.InsertOnSubmit(newEmpDept);
                        lChanged = true;
                    }
                }
                else
                {
                    if (l.IsSelected)
                    {
                        lEmpDept.EmployeeId   = this.Id;
                        lEmpDept.DepartmentId = l.Id;
                    }
                    else
                    {
                        lDataContext.Emp_Depts.DeleteOnSubmit(lEmpDept);
                    }

                    lChanged = true;
                }

                try
                {
                    if (lChanged)
                    {
                        lDataContext.SubmitChanges();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }