public void LoadAPLines()
        {
            if (LabelAdvancePaymentData.Text != "")
            {
                StringReader  TempData   = new StringReader(LabelAdvancePaymentData.Text);
                XmlTextReader CurrentAPs = new XmlTextReader(TempData);
                AdvancePaymentLines = new ArrayList();

                while (CurrentAPs.Read())
                {
                    if (CurrentAPs.Name == "AdvancePayment")
                    {
                        RelationAdvancePayment AP = new RelationAdvancePayment();

                        AP.Id          = Guid.Parse(CurrentAPs.GetAttribute("id"));
                        AP.Description = CurrentAPs.GetAttribute("description");
                        AP.Amount      = Convert.ToDouble(CurrentAPs.GetAttribute("amount"));
                        AdvancePaymentLines.Add(AP);
                    }
                }
            }
            else
            {
                SaveAPLines();
            }
        }
        protected void ButtonAddAdvancePaymentCorrection_Click(object sender, EventArgs e)
        {
            Boolean Continue = false;

            if (TextBoxAdvancePaymentAmountToBeCorrected.Text != "")
            {
                // values filled, check for numbers
                try
                {
                    Double Temp = Convert.ToDouble(TextBoxAdvancePaymentAmountToBeCorrected.Text);
                    Continue = (Temp != 0);
                }
                catch (Exception)
                { /*just suffocate */ }
            }

            if (Continue)
            {
                // add the material, type and ID to the grid
                LoadAPLines();

                RelationAdvancePayment TempLine = new RelationAdvancePayment();

                TempLine.Id          = Guid.Parse(DropDownListAdvancePayments.SelectedValue);
                TempLine.Description = DropDownListAdvancePayments.SelectedItem.Text;
                TempLine.Amount      = -Convert.ToDouble(TextBoxAdvancePaymentAmountToBeCorrected.Text);
                AdvancePaymentLines.Add(TempLine);

                SaveAPLines();
            }
        }
        protected void ButtonAddAPCorrection_Click(object sender, EventArgs e)
        {
            StandardSaveHandler(sender, e, false);

            try
            {
                RelationAdvancePayment AdvPay = ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationAdvancePaymentSet", "Id", Guid.Parse(DropDownListAPCorrection.SelectedValue))) as RelationAdvancePayment;
                (DataItem as Invoice).AddAdvancePaymentCorrection(ControlObjectContext,
                                                                  AdvPay,
                                                                  Convert.ToDouble(TextBoxCorrectionAP.Text) * -1,
                                                                  LabelAdvancePayment.Text + " " + AdvPay.Description);

                (DataItem as Invoice).RecalcTotals();
            }
            catch { }

            try
            {
                ControlObjectContext.SaveChanges(SaveOptions.DetectChangesBeforeSave);
            }
            catch (Exception ex)
            {
                Common.InformUserOnTransactionFail(ex, Page);
            }

            RebindControls();
            DataBind();
        }
Esempio n. 4
0
        protected void ButtonNew_Click(object sender, EventArgs e)
        {
            RelationAdvancePayment NewObj = new RelationAdvancePayment();
            ModelTMSContainer      Temp   = new ModelTMSContainer(Session["CustomerConnectString"].ToString(), Session);
            EntityKey TempKey             = new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(Request.Params["Id"]));
            Relation  TempObj             = Temp.GetObjectByKey(TempKey) as Relation;

            NewObj.Relation          = TempObj;
            NewObj.PaymentDateTime   = Common.CurrentClientDate(Session);
            NewObj.Ledger            = Temp.LedgerSet.First();
            NewObj.LedgerBookingCode = Temp.LedgerBookingCodeSet.First();

            Temp.AddToRelationAdvancePaymentSet(NewObj);
            Temp.SaveChanges(SaveOptions.DetectChangesBeforeSave);

            WebUserControlCustomerRelationAdvancePayment1.KeyID   = NewObj.Id;
            WebUserControlCustomerRelationAdvancePayment1.Visible = true;
        }
        protected void GridViewAdvancePaymentCorrections_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            e.Cancel = true;

            LoadAPLines();

            Guid SearchGuidID = Guid.Parse(e.Keys[0].ToString());

            for (int i = AdvancePaymentLines.Count - 1; i >= 0; i--)
            {
                RelationAdvancePayment TempLine = AdvancePaymentLines[i] as RelationAdvancePayment;
                if (TempLine.Id == SearchGuidID)
                {
                    AdvancePaymentLines.RemoveAt(i);
                    break;
                }
            }

            SaveAPLines();
        }
        protected void DropDownListAdvancePayments_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownListAdvancePayments.SelectedValue != "")
            {
                Guid SelID = Guid.Parse(DropDownListAdvancePayments.SelectedValue);

                try
                {
                    RelationAdvancePayment TempAP = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationAdvancePaymentSet", "Id", SelID)) as RelationAdvancePayment;

                    LabelAdvancePaymentInformation.Text =
                        LabelAPPayDate.Text + TempAP.PaymentDateTime.ToString() + "<BR>" +
                        LabelAPTotalAmount.Text + TempAP.Amount + "<BR>" +
                        LabelAPPaidBackAmout.Text + TempAP.AmountPaidBack + "<BR>" +
                        LabelAPStillOpen.Text + Convert.ToString(TempAP.Amount - TempAP.AmountPaidBack);
                }
                catch (Exception)
                {
                }

                try
                {
                    RelationWork TempWork = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationWorkSet", "Id", SelID)) as RelationWork;

                    LabelAdvancePaymentInformation.Text =
                        LabelAPPayDate.Text + TempWork.AgreementDateTime.ToString() + "<BR>" +
                        LabelAPTotalAmount.Text + TempWork.AmountEXVat + "<BR>" +
                        LabelAPPaidBackAmout.Text + TempWork.AmountPaidBack + "<BR>" +
                        LabelAPStillOpen.Text + Convert.ToString(TempWork.AmountEXVat - TempWork.AmountPaidBack);
                    if (TempWork.IsVATApplicable)
                    {
                        LabelAdvancePaymentInformation.Text = LabelAdvancePaymentInformation.Text + "<BR>" + String.Format(LabelAPVATWarning.Text, TempWork.VATPercentage);
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        public void SaveAPLines()
        {
            StringWriter  TempData   = new StringWriter();
            XmlTextWriter CurrentAPs = new XmlTextWriter(TempData);

            CurrentAPs.WriteStartElement("AdvancePaymentLines");
            for (int i = 0; i < AdvancePaymentLines.Count; i++)
            {
                RelationAdvancePayment TempAP = AdvancePaymentLines[i] as RelationAdvancePayment;

                CurrentAPs.WriteStartElement("AdvancePayment");

                CurrentAPs.WriteAttributeString("id", TempAP.Id.ToString());
                CurrentAPs.WriteAttributeString("description", TempAP.Description);
                CurrentAPs.WriteAttributeString("amount", TempAP.Amount.ToString());

                CurrentAPs.WriteEndElement();
            }
            CurrentAPs.WriteEndElement();

            TempData.Close();
            LabelAdvancePaymentData.Text = TempData.ToString();
        }
        protected void ButtonPrintAndProcess_Click(object sender, EventArgs e)
        {
            bool Success = false;

            // start transaction
            using (TransactionScope TS = new TransactionScope())
            {
                try
                {
                    // process order

                    // create order
                    Order TempOrder = new Order();
                    TempOrder.Relation        = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation;
                    TempOrder.Location        = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.LocationSet", "Id", Guid.Parse(DropDownListLocations.SelectedValue))) as Location;
                    TempOrder.Description     = TextBox_Description.Text;
                    TempOrder.OrderType       = LabelInvoiceType.Text;
                    TempOrder.BookingDateTime = Common.CurrentClientDateTime(Session);
                    TempOrder.YourDriverName  = TextBox_YourDriverName.Text;
                    TempOrder.YourTruckPlate  = TextBox_YourTruckPlate.Text;
                    TempOrder.FreightID       = TextBoxFreightID.Text;

                    TempOrder.LinkToSingleFreight(null);
                    if (LabelFreightGuid.Text != "")
                    {
                        Freight frg = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.FreightSet", "Id", Guid.Parse(LabelFreightGuid.Text))) as Freight;
                        TempOrder.LinkToSingleFreight(frg);
                    }

                    // assign the order number if not known
                    if (LabelOrderNr.Text == "")
                    {
                        TempOrder.AssignOrderNumber(_ControlObjectContext);
                    }
                    else
                    {
                        TempOrder.OrderNumber = Convert.ToInt64(LabelOrderNr.Text);
                    }

                    if (DropDownListProjects.SelectedValue != "")
                    {
                        TempOrder.RelationProject = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationProjectSet", "Id", Guid.Parse(DropDownListProjects.SelectedValue))) as RelationProject;
                    }

                    if (DropDownListCustomerFreights.SelectedValue != "")
                    {
                        TempOrder.Freight.Clear();
                        TempOrder.Freight.Add(_ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.FreightSet", "Id", Guid.Parse(DropDownListCustomerFreights.SelectedValue))) as Freight);
                    }

                    if (DropDownListCustomerLocations.SelectedValue != "")
                    {
                        TempOrder.RelationLocation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationLocationSet", "Id", Guid.Parse(DropDownListCustomerLocations.SelectedValue))) as RelationLocation;
                    }

//                    if (Session["CurrentUserID"] != null)
//                    {
//                        TempOrder.StaffMemberPurchaser = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.StaffMemberSet", "Id", Guid.Parse(Session["CurrentUserID"].ToString()))) as StaffMember;
//                    }
                    TempOrder.StaffMemberPurchaser = Common.CurrentLoggedInUser(Session, _ControlObjectContext);

                    //add the order to the controlobjectcontext
                    _ControlObjectContext.AddToOrderSet(TempOrder);

                    // add order lines
                    for (int i = 0; i < WebUserControlEditOrderMaterials1.OrderLines.Count; i++)
                    {
                        OrderLine TempLine = ((WebUserControlEditOrderMaterials1.OrderLines[i]) as OrderLine);
                        TempLine.Order = TempOrder;
                        TempLine.Id    = Guid.NewGuid();
                        //_ControlObjectContext.AddToOrderLineSet(TempLine);
                    }
                    TempOrder.RecalcTotals();

                    // add dirt if required
                    Double AddDirtAmount;
                    try { AddDirtAmount = System.Convert.ToDouble(TextBoxAddDirt.Text); }
                    catch { AddDirtAmount = 0; }
                    AddDirtAmount = (AddDirtAmount - TempOrder.TotalAmount);
                    if (AddDirtAmount > 0)
                    {
                        OrderLine TempLine = new OrderLine();
                        TempLine.Order        = TempOrder;
                        TempLine.Material     = TempOrder.Location.MaterialForDirt;
                        TempLine.PricePerUnit = TempLine.Material.PurchasePrice;
                        if (LabelInvoiceType.Text == "Sell")
                        {
                            TempLine.PricePerUnit = TempLine.Material.SalesPrice;
                        }
                        TempLine.Amount      = AddDirtAmount * TempOrder.Location.MaterialForDirt.MaterialUnit.StockKgMultiplier;
                        TempLine.Description = "- " + TempOrder.Location.MaterialForDirt.Description;
                        TempLine.RecalcTotals();
                        _ControlObjectContext.AddToOrderLineSet(TempLine);
                    }

                    // create invoice
                    Invoice TempInvoice = new Invoice();
                    // copy the group code if this is a correction from an old invoice
                    Guid OldGuid = Guid.Parse(LabelGeneratedInvoiceId.Text);
                    if (OldGuid != Guid.Empty)
                    {
                        try
                        {
                            Invoice OldInvoice = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.InvoiceSet", "Id", OldGuid)) as Invoice;
                            TempInvoice.GroupCode = OldInvoice.GroupCode;
                        }
                        catch { };
                    }
                    // reset the invoice
                    LabelGeneratedInvoiceId.Text = Guid.Empty.ToString();
                    // continue processing
                    TempInvoice.Relation = TempOrder.Relation;
                    TempInvoice.Location = TempOrder.Location;
                    //TempInvoice.Order.Add(TempOrder);
                    TempInvoice.Description     = TempOrder.Description;
                    TempInvoice.InvoiceType     = LabelInvoiceType.Text;
                    TempInvoice.BookingDateTime = Common.CurrentClientDateTime(Session);
                    TempInvoice.Ledger          = TempInvoice.Location.CashLedger;
                    TempInvoice.InvoiceNote     = TextBoxInvoiceNote.Text;
                    _ControlObjectContext.AddToInvoiceSet(TempInvoice);

                    // add the order to the invoice
                    TempInvoice.AddOrderToInvoice(_ControlObjectContext, TempOrder);

                    // subtract advance payments
                    WebUserControlEditAdvancePayments1.LoadAPLines();
                    for (int i = 0; i < WebUserControlEditAdvancePayments1.AdvancePaymentLines.Count; i++)
                    {
                        Guid TempGuid;
                        TempGuid = (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Id;

                        // load the work or advance payment object
                        RelationAdvancePayment CurrAP   = null;
                        RelationWork           CurrWork = null;
                        try
                        {
                            CurrAP = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationAdvancePaymentSet", "Id", TempGuid)) as RelationAdvancePayment;
                            TempInvoice.AddAdvancePaymentCorrection(_ControlObjectContext, CurrAP, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description);
                        }
                        catch (Exception) { };
                        try
                        {
                            CurrWork = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationWorkSet", "Id", TempGuid)) as RelationWork;
                            TempInvoice.AddWorkCorrection(_ControlObjectContext, CurrWork, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description);
                        }
                        catch (Exception) { };
                    }

                    // process order & invoice
                    TempInvoice.ProcessInvoice(_ControlObjectContext, TempInvoice.GroupCode, true, Common.CurrentClientDateTime(Session));

                    // and save to persistent storage
                    _ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);

                    // and save this invoice
                    LabelGeneratedInvoiceId.Text = TempInvoice.Id.ToString();

                    // commit the transaciton
                    TS.Complete();
                    Success = true;
                }
                catch (Exception ex) // commit or procedure failed somewhere
                {
                    // rollback transaction
                    TS.Dispose();

                    // inform user
                    Common.InformUserOnTransactionFail(ex, Page);
                }
            }

            if (Success)
            {
                // when success advance panel
                CurrentPanelLevel = CurrentPanelLevel + 1;
                ShowCorrectPanels();
            }
        }
        protected void ButtonPrintAndProcess_Click(object sender, EventArgs e)
        {
            ModelTMSContainer _ControlObjectContext = new ModelTMSContainer(Session["CustomerConnectString"].ToString(), Session);

            // start transaction
            using (TransactionScope TS = new TransactionScope())
            {
                try
                {
                    // create new invoice
                    Invoice NewInvoice = new Invoice();
                    NewInvoice.Relation = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationSet", "Id", Guid.Parse(DropDownListCustomers.SelectedValue))) as Relation;
                    if (DropDownListLocations.SelectedValue != "")
                    {
                        NewInvoice.Location = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.LocationSet", "Id", Guid.Parse(DropDownListLocations.SelectedValue))) as Location;
                    }
                    NewInvoice.Description     = TextBoxInvoiceDescription.Text;
                    NewInvoice.InvoiceNote     = TextBoxInvoiceNote.Text;
                    NewInvoice.InvoiceType     = LabelInvoiceType.Text;
                    NewInvoice.BookingDateTime = Common.CurrentClientDateTime(Session);
                    NewInvoice.InvoiceNote     = TextBoxInvoiceNote.Text;
                    _ControlObjectContext.AddToInvoiceSet(NewInvoice);

                    // add order lines
                    for (int i = 0; i < GridViewOpenOrders.Rows.Count; i++)
                    {
                        if ((GridViewOpenOrders.Rows[i].Cells[0].Controls[1] as CheckBox).Checked)
                        {
                            // load order
                            string Query = "SELECT VALUE it FROM OrderSet as it WHERE it.OrderNumber = @OrderNumber";
                            ObjectQuery <Order> query = new ObjectQuery <Order>(Query, _ControlObjectContext);
                            query.Parameters.Add(new ObjectParameter("OrderNumber", Convert.ToInt64(GridViewOpenOrders.Rows[i].Cells[2].Text)));
                            ObjectResult <Order> ilines = query.Execute(MergeOption.AppendOnly);
                            Order TempOrder             = ilines.First <Order>();

                            // save location in the invoice if this was unknown yet
                            if ((TempOrder.Location != null) && (NewInvoice.Location == null))
                            {
                                NewInvoice.Location = TempOrder.Location;

                                if (NewInvoice.Ledger == null)
                                // set ledger for this invoice (default bank)
                                {
                                    NewInvoice.Ledger = NewInvoice.Location.BankLedger;
                                }
                            }

                            // add to invoice
                            NewInvoice.AddOrderToInvoice(_ControlObjectContext, TempOrder);
                        }
                    }

                    // subtract advance payments
                    WebUserControlEditAdvancePayments1.LoadAPLines();
                    for (int i = 0; i < WebUserControlEditAdvancePayments1.AdvancePaymentLines.Count; i++)
                    {
                        Guid TempGuid;
                        TempGuid = (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Id;

                        // load the work or advance payment object
                        RelationAdvancePayment CurrAP   = null;
                        RelationWork           CurrWork = null;
                        try
                        {
                            CurrAP = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationAdvancePaymentSet", "Id", TempGuid)) as RelationAdvancePayment;
                            NewInvoice.AddAdvancePaymentCorrection(_ControlObjectContext, CurrAP, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description);
                        }
                        catch (Exception) { };
                        try
                        {
                            CurrWork = _ControlObjectContext.GetObjectByKey(new EntityKey("ModelTMSContainer.RelationWorkSet", "Id", TempGuid)) as RelationWork;
                            NewInvoice.AddWorkCorrection(_ControlObjectContext, CurrWork, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Amount, (WebUserControlEditAdvancePayments1.AdvancePaymentLines[i] as RelationAdvancePayment).Description);
                        }
                        catch (Exception) { };
                    }

                    // process the invoice
                    if (LabelGeneratedInvoiceId.Text != "")
                    {
                        // use old invoice id as group code
                        NewInvoice.GroupCode = new Guid(LabelGeneratedInvoiceId.Text);
                    }
                    NewInvoice.GenerateInvoiceNumber(_ControlObjectContext);
                    //NewInvoice.ProcessInvoice(_ControlObjectContext, NewInvoice.GroupCode); DO NOT PROCESS INVOICE !!! CUSTOMER HAS TO DO THAT SEPERATELY !!!
                    LabelGeneratedInvoiceId.Text = NewInvoice.Id.ToString();

                    // and save to persistent storage
                    _ControlObjectContext.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);

                    // commit the transaciton
                    TS.Complete();

                    // when success advance panel
                    CurrentPage = 5;
                    EnableCurrentPageElements();
                }
                catch (Exception ex)
                {
                    // rollback transaction
                    TS.Dispose();

                    // inform user
                    Common.InformUserOnTransactionFail(ex, Page);
                }
            }
        }