public HttpResponseMessage update(PaymentOption post, Int32 languageId = 0)
        {
            // Check for errors
            if (post == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The post is null"));
            }
            else if (Language.MasterPostExists(languageId) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The language does not exist"));
            }
            else if (Unit.MasterPostExists(post.unit_id) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The unit does not exist"));
            }
            else if (ValueAddedTax.MasterPostExists(post.value_added_tax_id) == false)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The value added tax does not exist"));
            }

            // Make sure that the data is valid
            post.product_code      = AnnytabDataValidation.TruncateString(post.product_code, 50);
            post.name              = AnnytabDataValidation.TruncateString(post.name, 100);
            post.payment_term_code = AnnytabDataValidation.TruncateString(post.payment_term_code, 10);
            post.fee          = AnnytabDataValidation.TruncateDecimal(post.fee, 0, 9999999999.99M);
            post.account_code = AnnytabDataValidation.TruncateString(post.account_code, 10);

            // Get the saved post
            PaymentOption savedPost = PaymentOption.GetOneById(post.id, languageId);

            // Check if the post exists
            if (savedPost == null)
            {
                return(Request.CreateResponse <string>(HttpStatusCode.BadRequest, "The record does not exist"));
            }

            // Update the post
            PaymentOption.UpdateMasterPost(post);
            PaymentOption.UpdateLanguagePost(post, languageId);

            // Return the success response
            return(Request.CreateResponse <string>(HttpStatusCode.OK, "The update was successful"));
        } // End of the update method
Esempio n. 2
0
        public ActionResult edit(FormCollection collection)
        {
            // Get the current domain
            Domain currentDomain = Tools.GetCurrentDomain();
            ViewBag.CurrentDomain = currentDomain;

            // Get query parameters
            string returnUrl = collection["returnUrl"];
            ViewBag.QueryParams = new QueryParams(returnUrl);

            // Check if the administrator is authorized
            if (Administrator.IsAuthorized(new string[] { "Administrator", "Editor" }) == true)
            {
                ViewBag.AdminSession = true;
            }
            else if (Administrator.IsAuthorized(Administrator.GetAllAdminRoles()) == true)
            {
                ViewBag.AdminSession = true;
                ViewBag.AdminErrorCode = 1;
                ViewBag.TranslatedTexts = StaticText.GetAll(currentDomain.back_end_language, "id", "ASC");
                return View("index");
            }
            else
            {
                // Redirect the user to the start page
                return RedirectToAction("index", "admin_login");
            }

            // Get all the form values
            Int32 id = Convert.ToInt32(collection["txtId"]);
            string product_code = collection["txtProductCode"];
            string name = collection["txtName"];
            string payment_term_code = collection["txtPaymentTermCode"];
            decimal fee = 0;
            decimal.TryParse(collection["txtFee"].Replace(",", "."), NumberStyles.Any, CultureInfo.InvariantCulture, out fee);
            Int32 unit_id = Convert.ToInt32(collection["selectUnit"]);   
            Int32 connection = Convert.ToInt32(collection["selectConnection"]);
            Int32 value_added_tax_id = Convert.ToInt32(collection["selectValueAddedTax"]);
            string account_code = collection["txtAccountCode"];
            bool inactive = Convert.ToBoolean(collection["cbInactive"]);

            // Get the default admin language id
            Int32 adminLanguageId = currentDomain.back_end_language;

            // Get translated texts
            KeyStringList tt = StaticText.GetAll(adminLanguageId, "id", "ASC");

            // Get the payment option
            PaymentOption paymentOption = PaymentOption.GetOneById(id, adminLanguageId);

            // Check if the payment option exists
            if (paymentOption == null)
            {
                // Create a empty payment option
                paymentOption = new PaymentOption();
            }

            // Update values
            paymentOption.product_code = product_code;
            paymentOption.name = name;
            paymentOption.payment_term_code = payment_term_code;
            paymentOption.fee = fee;
            paymentOption.unit_id = unit_id;
            paymentOption.connection = connection;
            paymentOption.value_added_tax_id = value_added_tax_id;
            paymentOption.account_code = account_code;
            paymentOption.inactive = inactive;

            // Create a error message
            string errorMessage = string.Empty;

            // Check for errors in the payment option
            if (paymentOption.product_code.Length > 50)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), tt.Get("product_code"), "50") + "<br/>";
            }
            if (paymentOption.name.Length > 100)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), tt.Get("name"), "100") + "<br/>";
            }
            if (paymentOption.payment_term_code.Length > 10)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), tt.Get("payment_term_code"), "10") + "<br/>";
            }
            if (paymentOption.fee < 0 || paymentOption.fee > 9999999999.99M)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_range"), tt.Get("fee"), "9 999 999 999.99") + "<br/>";
            }
            if (paymentOption.account_code.Length > 10)
            {
                errorMessage += "&#149; " + String.Format(tt.Get("error_field_length"), tt.Get("account_code"), "10") + "<br/>";
            }

            // Check if there is errors
            if (errorMessage == "")
            {
                // Check if we should add or update the payment option
                if (paymentOption.id == 0)
                {
                    // Add the payment option
                    Int64 insertId = PaymentOption.AddMasterPost(paymentOption);
                    paymentOption.id = Convert.ToInt32(insertId);
                    PaymentOption.AddLanguagePost(paymentOption, adminLanguageId);
                }
                else
                {
                    // Update the payment option
                    PaymentOption.UpdateMasterPost(paymentOption);
                    PaymentOption.UpdateLanguagePost(paymentOption, adminLanguageId);
                }

                // Redirect the user to the list
                return Redirect("/admin_payment_options" + returnUrl);
            }
            else
            {
                // Set form values
                ViewBag.ErrorMessage = errorMessage;
                ViewBag.Units = Unit.GetAll(adminLanguageId, "name", "ASC");
                ViewBag.PaymentOption = paymentOption;
                ViewBag.TranslatedTexts = tt;
                ViewBag.ReturnUrl = returnUrl;

                // Return the edit view
                return View("edit");
            }

        } // End of the edit method