Exemplo n.º 1
0
        public static MvcHtmlString AppTextboxDecimal(this HtmlHelper htmlHelper, string name, object value, object htmlAttributes)
        {
            if (value != null)
            {
                if (NumUtil.IsNumeric(value))
                {
                    value = NumUtil.ParseDecimal(value.ToString());
                }
            }

            return(AppText(htmlHelper, new RenderTextboxDecimal(), name, value, htmlAttributes));
        }
Exemplo n.º 2
0
        private static MvcHtmlString AppTextboxDecimalFor <TModel, TProperty>(HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, int colspan, object htmlAttributes)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            if (metadata.Model != null)
            {
                if (NumUtil.IsNumeric(metadata.Model))
                {
                    metadata.Model = NumUtil.FormatCurrency(NumUtil.ParseDecimal(metadata.Model.ToString()), false);
                }
            }

            return(AppTextFor(htmlHelper, expression, new RenderTextboxDecimal(), colspan, htmlAttributes));
        }
        public async Task <IActionResult> Modify(PointAdjustDTO model, IFormFile file)
        {
            if (!_loginServices.isInAdminRoles(this.GetRoles()))
            {
                return(RedirectToAction("Login", "Accounts"));
            }
            if (model.valid)
            {
                if (model.PointAdjusts != null && model.PointAdjusts.Count() > 0)
                {
                    foreach (var item in model.PointAdjusts)
                    {
                        var point = new CustomerPoint();
                        point.CustomerID       = item.CustomerID;
                        point.Code             = item.ConditionCode;
                        point.Name             = item.Name;
                        point.Point            = item.Point;
                        point.PurchaseAmt      = item.PurchaseAmt;
                        point.TransacionTypeID = item.TransacionTypeID;
                        point.CustomerChanal   = item.CustomerChanal;
                        point.Source           = "tipsociety-adjust";

                        point.Create_On = item.Create_On;
                        point.Create_By = item.Create_By;
                        point.Update_On = item.Update_On;
                        point.Update_By = item.Update_By;
                        this._context.CustomerPoints.Add(point);
                    }
                    this._context.PointAdjusts.AddRange(model.PointAdjusts);
                    this._context.SaveChanges();
                    return(RedirectToAction("Index", new { result = 1 }));
                }
            }
            else
            {
                if (file == null)
                {
                    ModelState.AddModelError("Error", "ไม่พบข้อมูลไฟล์นำเข้า");
                }
                else
                {
                    model.PointAdjusts     = new List <PointAdjust>();
                    model.PointAdjustFails = new List <PointAdjustFail>();

                    ModelState.Clear();
                    using (var memoryStream = new MemoryStream())
                    {
                        await file.CopyToAsync(memoryStream).ConfigureAwait(false);

                        using (var package = new ExcelPackage(memoryStream))
                        {
                            var workbook  = package.Workbook;
                            var worksheet = workbook.Worksheets.First();

                            var rowCount = worksheet.Dimension?.Rows;
                            var colCount = worksheet.Dimension?.Columns;
                            if (rowCount.HasValue && colCount.HasValue)
                            {
                                for (int row = 2; row <= rowCount.Value; row++)
                                {
                                    if (colCount >= 2)
                                    {
                                        var username      = worksheet.Cells["A" + row].Value.ToString();
                                        var conditioncode = worksheet.Cells["B" + row].Value.ToString();
                                        var purchaseAmt   = NumUtil.ParseDecimal(worksheet.Cells["C" + row].Value);
                                        var condition     = this._context.PointConditions.Where(w => w.ConditionCode == conditioncode).FirstOrDefault();
                                        var customer      = this._context.Customers.Include(i => i.User).Where(w => w.User.UserName == username).FirstOrDefault();
                                        if (customer != null && condition != null)
                                        {
                                            var adjust = new PointAdjust();
                                            adjust.Customer         = customer;
                                            adjust.ConditionCode    = condition.ConditionCode;
                                            adjust.CustomerChanal   = customer.Channel;
                                            adjust.TransacionTypeID = condition.TransacionTypeID;
                                            adjust.CustomerID       = customer.ID;
                                            adjust.Point            = GetPoint(condition, customer, purchaseAmt, false);
                                            adjust.Name             = condition.Name;
                                            adjust.Create_On        = DateUtil.Now();
                                            adjust.Create_By        = this.HttpContext.User.Identity.Name;
                                            adjust.Update_On        = DateUtil.Now();
                                            adjust.Update_By        = this.HttpContext.User.Identity.Name;
                                            model.PointAdjusts.Add(adjust);
                                        }
                                        else
                                        {
                                            var msg = new StringBuilder();
                                            if (condition == null)
                                            {
                                                msg.AppendLine("ไม่พบข้อมูลเงื่อนไขการสะสมคะแนน");
                                            }
                                            if (customer == null)
                                            {
                                                msg.AppendLine("ไม่พบข้อมูลสมาชิก");
                                            }
                                            var fail = new PointAdjustFail();
                                            fail.username      = worksheet.Cells["A" + row].Value;
                                            fail.conditioncode = worksheet.Cells["B" + row].Value;
                                            fail.message       = msg.ToString();
                                            fail.row           = row + 1;
                                            model.PointAdjustFails.Add(fail);
                                        }
                                    }
                                    else
                                    {
                                        ModelState.AddModelError("Error", "รูปแบบไฟล์ไม่ถูกต้อง");
                                    }
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("Error", "รูปแบบไฟล์ไม่ถูกต้องหรือไม่มีข้อมูล");
                            }
                        }
                    }
                }
            }
            return(View("PointAdjust", model));
        }
Exemplo n.º 4
0
        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();

            using (StringWriter writer = new StringWriter(builder))
            {
                using (HtmlTextWriter html = new HtmlTextWriter(writer))
                {
                    if (_controltype != AppControlType.Button && _controltype != AppControlType.ActionLink)
                    {
                        html.AddAttribute(HtmlTextWriterAttribute.Class, "row");
                        html.RenderBeginTag(HtmlTextWriterTag.Div); //Tag Row
                        html.AddAttribute(HtmlTextWriterAttribute.Class, "col-md-" + _colSpan);
                        html.RenderBeginTag(HtmlTextWriterTag.Div); //Tag Colspan
                        html.AddAttribute(HtmlTextWriterAttribute.Class, "form-group");
                        html.RenderBeginTag(HtmlTextWriterTag.Div); //Tag Form Group

                        if (_controltype == AppControlType.CheckBox)
                        {
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "checkbox");
                            html.RenderBeginTag(HtmlTextWriterTag.Div);
                        }

                        if (_controltype == AppControlType.RadioButton)
                        {
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "radio");
                            html.RenderBeginTag(HtmlTextWriterTag.Div);
                        }

                        if (_controltype == AppControlType.FileUpload)
                        {
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "input-group");
                            html.RenderBeginTag(HtmlTextWriterTag.Div);  //Tag Fileupload 1
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "input-group-btn");
                            html.RenderBeginTag(HtmlTextWriterTag.Span); //Tag Fileupload 2
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "btn btn-primary btn-file");
                            html.RenderBeginTag(HtmlTextWriterTag.Span); //Tag Fileupload 3
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "fa fa-upload");
                            html.RenderBeginTag(HtmlTextWriterTag.I);    //Tag Fileupload 4
                            html.RenderEndTag();                         //End Tag Fileupload 4
                        }

                        if (_controltype == AppControlType.GridData)
                        {
                            html.AddAttribute(HtmlTextWriterAttribute.Class, "table-responsive");
                            html.RenderBeginTag(HtmlTextWriterTag.Div);//Tag Grid Data
                        }

                        if (_controltype == AppControlType.TextDate)
                        {
                            if (!_readonly)
                            {
                                html.RenderBeginTag(HtmlTextWriterTag.Script);
                                html.Write("$(function () { InitDatepickerByID('" + ID + "'); })");
                                html.RenderEndTag();
                            }
                        }
                        if (_controltype == AppControlType.TextTime)
                        {
                            if (!_readonly)
                            {
                                html.RenderBeginTag(HtmlTextWriterTag.Script);
                                html.Write("$(function () {  $('#" + ID + "').datetimepicker( {pickDate: false , format: 'HH:mm', pickSeconds: false, pick12HourFormat: false}) })");
                                html.RenderEndTag();
                            }
                        }
                    }
                    if (_controltype != AppControlType.ValidateMessage)
                    {
                        this.Attributes.AddAttributes(html);
                        html.RenderBeginTag(_tagname);//Tag Control
                        this.RenderContents(html);
                    }



                    if (_controltype == AppControlType.ComboBox)
                    {
                        if (_options != null && _options.Count > 0)
                        {
                            var i = 0;
                            foreach (var op in _options)
                            {
                                if (_selected != null)
                                {
                                    if (op.Value == _selected.ToString())
                                    {
                                        html.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
                                    }
                                }
                                else
                                {
                                    if (i == 0)
                                    {
                                        html.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");
                                    }
                                }

                                html.AddAttribute(HtmlTextWriterAttribute.Value, op.Value.Trim());
                                html.RenderBeginTag(HtmlTextWriterTag.Option); //Tag Option
                                html.WriteEncodedText(op.Text.Trim());
                                html.RenderEndTag();                           //End Option

                                i++;
                            }
                        }
                    }
                    else if (_controltype == AppControlType.GridData)
                    {
                        if (_gridcolumnHeader != null)
                        {
                            html.RenderBeginTag(HtmlTextWriterTag.Thead); //Tag thead
                            html.RenderBeginTag(HtmlTextWriterTag.Tr);    //Tag Tr
                            foreach (var col in _gridcolumnHeader)
                            {
                                if (col.Data_Type == GridDataType.none | col.Data_Type == GridDataType.usercontrol)
                                {
                                    html.AddAttribute(HtmlTextWriterAttribute.Class, "no-sort");
                                    if (!string.IsNullOrEmpty(col.Width))
                                    {
                                        html.AddStyleAttribute(HtmlTextWriterStyle.Width, col.Width);
                                    }
                                    else
                                    {
                                        html.AddStyleAttribute(HtmlTextWriterStyle.Width, "150px");
                                    }
                                    html.RenderBeginTag(HtmlTextWriterTag.Th);//Tag Th
                                }
                                else if (col.Data_Type == GridDataType.index)
                                {
                                    html.AddAttribute(HtmlTextWriterAttribute.Class, "sort-numeric");
                                    if (!string.IsNullOrEmpty(col.Width))
                                    {
                                        html.AddStyleAttribute(HtmlTextWriterStyle.Width, col.Width);
                                    }
                                    else
                                    {
                                        html.AddStyleAttribute(HtmlTextWriterStyle.Width, "70px");
                                    }
                                    html.RenderBeginTag(HtmlTextWriterTag.Th);//Tag Th
                                }
                                else
                                {
                                    html.AddAttribute(HtmlTextWriterAttribute.Class, "sort-" + col.Data_Type.ToString());
                                    if (!string.IsNullOrEmpty(col.Width))
                                    {
                                        html.AddStyleAttribute(HtmlTextWriterStyle.Width, col.Width);
                                    }
                                    html.RenderBeginTag(HtmlTextWriterTag.Th);//Tag Th
                                }
                                html.WriteEncodedText(col.Column_Name.Replace("_", " "));
                                html.RenderEndTag();                      //End Option
                            }
                            html.RenderEndTag();                          //End Tag Tr
                            html.RenderEndTag();                          //End Tag thead

                            html.RenderBeginTag(HtmlTextWriterTag.Tbody); //Tag Th;//Tag tbody
                            if (_gridrows != null)
                            {
                                foreach (var row in _gridrows)
                                {
                                    html.RenderBeginTag(HtmlTextWriterTag.Tr);//Tag Tr
                                    for (var i = 0; i < row.Item.Count; i++)
                                    {
                                        var item = row.Item[i];
                                        if (_gridcolumnHeader[i].Data_Type == GridDataType.amount)
                                        {
                                            html.AddAttribute(HtmlTextWriterAttribute.Class, "text-right");
                                            html.RenderBeginTag(HtmlTextWriterTag.Td);//Tag Td
                                            if (item.Value != null)
                                            {
                                                var amount = NumUtil.ParseDecimal(item.Value.ToString());
                                                html.WriteEncodedText(NumUtil.FormatCurrency(amount));
                                            }
                                            else
                                            {
                                                html.WriteEncodedText("0");
                                            }

                                            html.RenderEndTag();//End Tag Td
                                        }
                                        else if (_gridcolumnHeader[i].Data_Type == GridDataType.index)
                                        {
                                            html.AddAttribute(HtmlTextWriterAttribute.Class, "text-center");
                                            html.RenderBeginTag(HtmlTextWriterTag.Td);//Tag Td
                                            if (item.Value != null)
                                            {
                                                var amount = NumUtil.ParseInteger(item.Value.ToString());
                                                html.WriteEncodedText(amount.ToString("n0"));
                                            }
                                            html.RenderEndTag();//End Tag Td
                                        }
                                        else if (_gridcolumnHeader[i].Data_Type == GridDataType.numeric)
                                        {
                                            html.AddAttribute(HtmlTextWriterAttribute.Class, "text-right");
                                            html.RenderBeginTag(HtmlTextWriterTag.Td);//Tag Td
                                            if (item.Value != null)
                                            {
                                                var amount = NumUtil.ParseInteger(item.Value.ToString());
                                                html.WriteEncodedText(amount.ToString("n0"));
                                            }
                                            html.RenderEndTag();//End Tag Td
                                        }
                                        else if (_gridcolumnHeader[i].Data_Type == GridDataType.usercontrol)
                                        {
                                            html.RenderBeginTag(HtmlTextWriterTag.Td);//Tag Td
                                            if (item.Value != null)
                                            {
                                                html.Write(item.Value.ToString());
                                            }
                                            html.RenderEndTag();//End Tag Td
                                        }
                                        else
                                        {
                                            html.RenderBeginTag(HtmlTextWriterTag.Td);//Tag Td
                                            if (item.Value != null)
                                            {
                                                html.WriteEncodedText(item.Value.ToString());
                                            }
                                            html.RenderEndTag();//End Tag Td
                                        }
                                    }
                                    html.RenderEndTag();//End Tag Tr
                                }
                            }

                            html.RenderEndTag();//End Tag tbody
                        }
                    }
                    if (_controltype == AppControlType.Button || _controltype == AppControlType.ActionLink || _controltype == AppControlType.Label)
                    {
                        if (!string.IsNullOrEmpty(_icon))
                        {
                            html.AddAttribute(HtmlTextWriterAttribute.Class, _icon);
                            html.RenderBeginTag(HtmlTextWriterTag.I); //Tag i
                            html.RenderEndTag();                      //End Tag i
                        }
                        if (Text != null)
                        {
                            html.WriteEncodedText(Text);
                        }
                    }



                    if (_controltype == AppControlType.TextArea)
                    {
                        if (Value != null)
                        {
                            html.WriteEncodedText(Value.ToString());
                        }
                    }

                    if (_controltype != AppControlType.ValidateMessage)
                    {
                        html.RenderEndTag();//End Tag Control
                    }



                    if (_controltype == AppControlType.RadioButton | _controltype == AppControlType.CheckBox)
                    {
                        html.AddAttribute(HtmlTextWriterAttribute.For, ID);
                        html.RenderBeginTag(HtmlTextWriterTag.Label); //Tag Radio or Check box label
                        html.WriteEncodedText(Text);
                        html.RenderEndTag();                          //End Tag Radio or Check box label
                    }


                    if (this.HtmlValidateString != null)
                    {
                        html.AddAttribute(HtmlTextWriterAttribute.Class, "validation-error");
                        html.RenderBeginTag(HtmlTextWriterTag.Div); //Tag validation-error
                        html.Write(_htmlvalidateString.ToHtmlString());
                        html.RenderEndTag();                        //End Tag validation-error
                    }

                    if (_controltype != AppControlType.Button && _controltype != AppControlType.ActionLink)
                    {
                        if (_controltype == AppControlType.GridData)
                        {
                            html.RenderEndTag();                           //Tag Grid data

                            html.RenderBeginTag(HtmlTextWriterTag.Script); //Tag script
                            var addbutton = "null";
                            if (_gridAddButton != null)
                            {
                                addbutton = _gridAddButton.ToString();
                            }
                            if (!string.IsNullOrEmpty(addbutton) && addbutton != "null")
                            {
                                html.Write("$(function () { InitDatatable('" + ID + "', '" + addbutton + "', null); })");
                            }
                            else
                            {
                                html.Write("$(function () { InitDatatable('" + ID + "', null, null); })");
                            }
                            html.RenderEndTag(); //End script;
                        }

                        if (_controltype == AppControlType.FileUpload)
                        {
                            html.RenderEndTag();                                                         //Tag Fileupload 1
                            html.RenderEndTag();                                                         //Tag Fileupload 2
                            html.RenderBeginTag("input class='form-control'  type='text'  readonly=''"); //Tag Fileupload Text
                            html.RenderEndTag();                                                         //End Tag Fileupload Text
                            html.RenderEndTag();                                                         //Tag Fileupload 3
                        }

                        if (_controltype == AppControlType.RadioButton)
                        {
                            html.RenderEndTag();//End Tag Radio
                        }
                        if (_controltype == AppControlType.CheckBox)
                        {
                            html.RenderEndTag(); //End Tag Check box
                        }
                        html.RenderEndTag();     //End Tag Form Group
                        html.RenderEndTag();     //End Tag Colspan
                        html.RenderEndTag();     //End Row
                    }
                }
            }
            var str = builder.ToString();

            if (_controltype == AppControlType.ComboBox)
            {
                Regex RegexBetweenTags = new Regex(@">(?! )\s+", RegexOptions.Compiled);
                Regex RegexLineBreaks  = new Regex(@"([\n\s])+?(?<= {2,})<", RegexOptions.Compiled);
                str = RegexBetweenTags.Replace(str, ">");
                str = RegexLineBreaks.Replace(str, "<");
                str = Regex.Replace(str, @"\t|\n|\r", "");
                str = str.Trim();
            }
            return(str);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> ModifyPrivilegeCode(PrivilegeCodeValidate model, IFormFile file)
        {
            if (!_loginServices.isInAdminRoles(this.GetRoles()))
            {
                return(RedirectToAction("Login", "Accounts"));
            }
            var pri = this._context.Privileges.Where(w => w.PrivilegeID == model.PrivilegeID).FirstOrDefault();

            if (pri == null)
            {
                ModelState.AddModelError("Error", "ไม่พบข้อมูลสิทธิพิเศษ");
            }
            if (ModelState.IsValid)
            {
                if (model.valid)
                {
                    if (model.PrivilegeCodes != null && model.PrivilegeCodes.Count() > 0)
                    {
                        foreach (var item in model.PrivilegeCodes)
                        {
                            item.PrivilegeID   = model.PrivilegeID;
                            item.EffectiveDate = DateUtil.ToDate(item.effDate);
                            item.ExpiryDate    = DateUtil.ToDate(item.expDate);
                            item.Status        = item.Status;
                            item.MaxUse        = item.MaxUse;
                            item.Create_On     = DateUtil.Now();
                            this._context.PrivilegeCodes.Add(item);
                        }
                        this._context.SaveChanges();
                        return(RedirectToAction("PrivilegeCode", new { result = 1, PrivilegeID = model.PrivilegeID }));
                    }
                }
                else
                {
                    if (file == null)
                    {
                        ModelState.AddModelError("Error", "ไม่พบข้อมูลไฟล์นำเข้า");
                    }
                    else
                    {
                        model.PrivilegeCodes     = new List <PrivilegeCode>();
                        model.PrivilegeCodeFails = new List <PrivilegeCodeFail>();

                        ModelState.Clear();

                        using (var memoryStream = new MemoryStream())
                        {
                            await file.CopyToAsync(memoryStream).ConfigureAwait(false);

                            using (var package = new ExcelPackage(memoryStream))
                            {
                                var workbook  = package.Workbook;
                                var worksheet = workbook.Worksheets.First();

                                var rowCount = worksheet.Dimension?.Rows;
                                var colCount = worksheet.Dimension?.Columns;
                                if (rowCount.HasValue && colCount.HasValue)
                                {
                                    for (int row = 2; row <= rowCount.Value; row++)
                                    {
                                        try
                                        {
                                            var cformat = worksheet.Cells["C" + row].Style.Numberformat.Format;
                                            var eformat = worksheet.Cells["D" + row].Style.Numberformat.Format;
                                            worksheet.Cells["C" + row].Style.Numberformat.Format = "dd/MM/yyyy HH:mm";
                                            worksheet.Cells["D" + row].Style.Numberformat.Format = "dd/MM/yyyy HH:mm";

                                            var code   = worksheet.Cells["A" + row].Value != null ? worksheet.Cells["A" + row].Value.ToString() : "";
                                            var maxuse = worksheet.Cells["B" + row].Value != null ? worksheet.Cells["B" + row].Value.ToString() : "";
                                            var eff    = worksheet.Cells["C" + row].Value != null ? worksheet.Cells["C" + row].Value : null;
                                            var exp    = worksheet.Cells["D" + row].Value != null ? worksheet.Cells["D" + row].Value : null;
                                            var status = worksheet.Cells["E" + row].Value != null ? worksheet.Cells["E" + row].Value.ToString() : "";

                                            var msg = new StringBuilder();
                                            if (string.IsNullOrEmpty(code))
                                            {
                                                msg.AppendLine("ไม่พบข้อมูล Code");
                                            }
                                            else
                                            {
                                                var pricode = this._context.PrivilegeCodes.Where(w => w.Code == code).FirstOrDefault();
                                                if (pricode != null)
                                                {
                                                    msg.AppendLine("Code ซ้ำในระบบ");
                                                }
                                            }
                                            var pcode = new PrivilegeCode();
                                            pcode.PrivilegeID = pri.PrivilegeID;
                                            pcode.Code        = code;
                                            pcode.MaxUse      = NumUtil.ParseInteger(maxuse);
                                            if (pcode.MaxUse == 0)
                                            {
                                                pcode.MaxUse = 1;
                                            }

                                            if (!string.IsNullOrEmpty(cformat) && cformat[0].ToString().ToLower() == "m")
                                            {
                                                pcode.EffectiveDate = (DateTime)eff;
                                            }
                                            else
                                            {
                                                pcode.EffectiveDate = DateUtil.ToDate(eff.ToString());
                                            }

                                            if (!string.IsNullOrEmpty(eformat) && eformat[0].ToString().ToLower() == "m")
                                            {
                                                pcode.ExpiryDate = (DateTime)exp;
                                            }
                                            else
                                            {
                                                pcode.ExpiryDate = DateUtil.ToDate(exp.ToString());
                                            }

                                            if (eff != null)
                                            {
                                                if (pcode.EffectiveDate == null)
                                                {
                                                    pcode.EffectiveDate = DateTime.FromOADate((double)NumUtil.ParseDecimal(eff));
                                                }
                                            }
                                            if (exp != null)
                                            {
                                                if (pcode.ExpiryDate == null)
                                                {
                                                    pcode.ExpiryDate = DateTime.FromOADate((double)NumUtil.ParseDecimal(exp));
                                                }
                                            }
                                            pcode.effDate = DateUtil.ToDisplayDate(pcode.EffectiveDate);
                                            pcode.expDate = DateUtil.ToDisplayDate(pcode.ExpiryDate);
                                            pcode.Status  = StatusType.InActive;
                                            if (status == "Y")
                                            {
                                                pcode.Status = StatusType.Active;
                                            }
                                            if (string.IsNullOrEmpty(msg.ToString()))
                                            {
                                                model.PrivilegeCodes.Add(pcode);
                                            }
                                            else
                                            {
                                                model.PrivilegeCodes.Clear();
                                                var fail = new PrivilegeCodeFail();
                                                fail.Code          = code;
                                                fail.EffectiveDate = pcode.effDate;
                                                fail.ExpiryDate    = pcode.expDate;
                                                fail.MaxUse        = pcode.MaxUse;
                                                fail.message       = msg.ToString();
                                                fail.Status        = pcode.Status.toStatusName();
                                                fail.row           = row;
                                                model.PrivilegeCodeFails.Add(fail);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            ModelState.AddModelError("Error", ex.Message);
                                        }
                                    }
                                }
                                else
                                {
                                    ModelState.AddModelError("Error", "รูปแบบไฟล์ไม่ถูกต้องหรือไม่มีข้อมูล");
                                }
                            }
                        }
                    }
                }
            }

            return(View("PrivilegeCode", model));
        }
Exemplo n.º 6
0
        public async Task <JsonResult> ValidateCode(int?PrivilegeID, IFormFile excelfile)
        {
            if (!_loginServices.isInAdminRoles(this.GetRoles()))
            {
                return(Json(new
                {
                    responseCode = "-403",
                    responseDesc = "คุณไม่มีสิทธิ์เข้าถึงระบบ",
                }));
            }
            if (excelfile == null)
            {
                return(Json(new
                {
                    responseCode = "-1",
                    responseDesc = "ไม่พบข้อมูลไฟล์นำเข้า",
                }));
            }
            if (!PrivilegeID.HasValue || PrivilegeID == 0)
            {
                return(Json(new
                {
                    responseCode = "-1",
                    responseDesc = "ไม่พบข้อมูลสิทธิพิเศษ",
                }));
            }
            var pID = PrivilegeID;
            var pri = this._context.Privileges.Where(w => w.PrivilegeID == PrivilegeID).FirstOrDefault();

            if (pri == null)
            {
                return(Json(new
                {
                    responseCode = "-1",
                    responseDesc = "ไม่พบข้อมูลสิทธิพิเศษ",
                }));
            }

            var codelist = new List <PrivilegeCode>();

            using (var memoryStream = new MemoryStream())
            {
                await excelfile.CopyToAsync(memoryStream).ConfigureAwait(false);

                using (var package = new ExcelPackage(memoryStream))
                {
                    var workbook  = package.Workbook;
                    var worksheet = workbook.Worksheets.First();

                    var rowCount = worksheet.Dimension?.Rows;
                    var colCount = worksheet.Dimension?.Columns;
                    if (rowCount.HasValue && colCount.HasValue)
                    {
                        for (int row = 2; row <= rowCount.Value; row++)
                        {
                            try
                            {
                                var cformat = worksheet.Cells["C" + row].Style.Numberformat.Format;
                                var eformat = worksheet.Cells["D" + row].Style.Numberformat.Format;
                                worksheet.Cells["C" + row].Style.Numberformat.Format = "dd/MM/yyyy HH:mm";
                                worksheet.Cells["D" + row].Style.Numberformat.Format = "dd/MM/yyyy HH:mm";

                                var code   = worksheet.Cells["A" + row].Value != null ? worksheet.Cells["A" + row].Value.ToString() : "";
                                var maxuse = worksheet.Cells["B" + row].Value != null ? worksheet.Cells["B" + row].Value.ToString() : "";
                                var eff    = worksheet.Cells["C" + row].Value != null ? worksheet.Cells["C" + row].Value : null;
                                var exp    = worksheet.Cells["D" + row].Value != null ? worksheet.Cells["D" + row].Value : null;
                                var status = worksheet.Cells["E" + row].Value != null ? worksheet.Cells["E" + row].Value.ToString() : "";


                                var pcode = new PrivilegeCode();
                                pcode.PrivilegeID = pri.PrivilegeID;
                                pcode.Code        = code;
                                pcode.MaxUse      = NumUtil.ParseInteger(maxuse);
                                if (pcode.MaxUse == 0)
                                {
                                    pcode.MaxUse = null;
                                }

                                if (!string.IsNullOrEmpty(cformat) && cformat[0].ToString().ToLower() == "m")
                                {
                                    pcode.EffectiveDate = (DateTime)eff;
                                }
                                else
                                {
                                    pcode.EffectiveDate = DateUtil.ToDate(eff.ToString());
                                }

                                if (!string.IsNullOrEmpty(eformat) && eformat[0].ToString().ToLower() == "m")
                                {
                                    pcode.ExpiryDate = (DateTime)exp;
                                }
                                else
                                {
                                    pcode.ExpiryDate = DateUtil.ToDate(exp.ToString());
                                }

                                if (eff != null)
                                {
                                    if (pcode.EffectiveDate == null)
                                    {
                                        pcode.EffectiveDate = DateTime.FromOADate((double)NumUtil.ParseDecimal(eff));
                                    }
                                }
                                if (exp != null)
                                {
                                    if (pcode.ExpiryDate == null)
                                    {
                                        pcode.ExpiryDate = DateTime.FromOADate((double)NumUtil.ParseDecimal(exp));
                                    }
                                }
                                pcode.effDate = DateUtil.ToDisplayDate(pcode.EffectiveDate);
                                pcode.expDate = DateUtil.ToDisplayDate(pcode.ExpiryDate);
                                pcode.Status  = StatusType.InActive;
                                if (status == "Y")
                                {
                                    pcode.Status = StatusType.Active;
                                }
                                codelist.Add(pcode);
                            }
                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }
            }


            return(Json(new
            {
                responseCode = "1",
                responseDesc = "SUCCESS",
                data = codelist,
            }));
        }