public ActionResult SaveEdit(QuotationPackagesRecord model)
        {
            bool isSuccess = false;

            recsys_quotation_packages record = this._db.recsys_quotation_packages.FirstOrDefault(qp => qp.id == model.id);

            //# validation
            if (record == null)
                throw new RecordNotFoundException();
            if (!record.update_date.HasValue)
                throw new ImproperDataException();
            int test = model.FetchRecordTime.CompareTo(record.update_date.Value);
            if (model.FetchRecordTime.CompareTo(record.update_date.Value) < -1)
                throw new OptimisticConcurrencyException("資料已被其他使用者更新");

            var quotationPackages = from qp in this._db.recsys_quotation_packages
                                    where qp.status == 1
                                    select qp;

            foreach (recsys_quotation_packages quotationPackage in quotationPackages)
            {
                if (!string.IsNullOrEmpty(model.code) && model.code.Trim().ToUpper() == quotationPackage.code.Trim().ToUpper() && (model.id!=quotationPackage.id))
                {
                    string message = "已有Quotation Package (" + model.code.Trim().ToUpper() + ")";

                    return Json(
                        new
                        {
                            isSuccess = isSuccess,
                            message = message
                        });
                }
            }

            if (string.IsNullOrEmpty(model.code)) {
                return Json(
                new
                {
                    isSuccess = isSuccess,
                    message = "必須輸入Code"
                });
            }
            else if (string.IsNullOrEmpty(model.districtID.ToString())) {
                return Json(
                new
                {
                    isSuccess = isSuccess,
                    message = "必須選擇地區"
                });
            }
            else
            {
                record.package_name = (model.packageName == null) ? "" : model.packageName.Trim();
                record.code = model.code.Trim() ?? "";
                record.district_id = model.districtID;
                record.customer_type = model.customerTypeID;
                record.remark = (model.remark == null)? "" : model.remark.Trim();
                record.update_date = DateTime.Now;
                Member _member = new Member("users");
                record.update_by = (int)_member.infoBySession("id");
            }

            try
            {
                this._db.SaveChanges();
                isSuccess = true;
            }
            catch (OptimisticConcurrencyException)
            {
                //# log down
            }

            return Json(
                new
                {
                    isSuccess = isSuccess
                });
        }
        public ActionResult Edit(int id)
        {
            QuotationPackagesRecord model;

            //# validation
            if (id < 0)
                throw new SystemException("Invalid Entity ID");

            //# get old record
            var records = from qp in this._db.recsys_quotation_packages
                          where qp.id == id
                          select new
                          {
                              ID = qp.id,
                              PackageName = qp.package_name,
                              Code = qp.code,
                              DistrictID = qp.district_id,
                              CustomerTypeID = qp.customer_type,
                              Remark = qp.remark,
                              Price = qp.price,
                              Status = qp.status
                          };

            var record = records.FirstOrDefault();

            //# validation
            if (record == null)
                throw new RecordNotFoundException();

            model = new QuotationPackagesRecord()
            {
                id = record.ID,
                packageName = record.PackageName,
                code = record.Code,
                districtID = record.DistrictID,
                customerTypeID = record.CustomerTypeID,
                remark = record.Remark,
                price = (record.Price == null)?0.0 : record.Price,
                status = record.Status,
                SaveResult = null,
                Mode = PageMode.Edit,
                FetchRecordTime = DateTime.Now,
                relateTableName = "Quotation",

                customerTypes = this.GetCustomerTypes()
            };

            //Get district name
            ViewBag.districtName = (from d in this._db.recsys_district where d.id == model.districtID select d.name).FirstOrDefault();

            return PartialView("AddEdit", model);
        }