示例#1
0
        public IActionResult Create(DealerSaleProdDtlViewModel dealerSaleProdDtlViewModel)
        {
            if (ModelState.IsValid)
            {
                string   OrderNo    = string.Empty;
                string   SalesOrder = "0";
                string   SalesToken = "0";
                string[] strOrderNo;
                if (_context.DealerSale.ToList().Count() > 0)
                {
                    var orderNo = _context.DealerSale.OrderByDescending(m => m.Id)
                                  .Where(w => w.OrderNo != null)
                                  .Select(s => s.OrderNo).FirstOrDefault();
                    if (orderNo != null)
                    {
                        strOrderNo = orderNo.Split('-');

                        if (strOrderNo.Length > 0)
                        {
                            SalesOrder = strOrderNo[1];
                            SalesToken = strOrderNo[3];
                        }
                    }
                }
                SalesOrder = Convert.ToString(Convert.ToInt16(SalesOrder) + 1);
                SalesOrder = SalesOrder.PadLeft(8 - SalesOrder.Length, '0');
                SalesToken = Convert.ToString(Convert.ToInt16(SalesToken) + 1);
                SalesToken = SalesToken.PadLeft(10 - SalesToken.Length, '0');
                OrderNo    = "S-" + SalesOrder + "-" + DateTime.Now.ToString("ddMMyy") + "-" + SalesToken;
                dealerSaleProdDtlViewModel.DealerSale.OrderNo = OrderNo;

                dealerSaleProdDtlViewModel.DealerSale.createDate = DateTime.Now;

                _context.Add(dealerSaleProdDtlViewModel.DealerSale);
                _context.SaveChanges();


                int DealerSaleId = dealerSaleProdDtlViewModel.DealerSale.Id;
                foreach (var item in dealerSaleProdDtlViewModel.DealerSaleProductDetail)
                {
                    item.DealerSaleId = DealerSaleId;
                    _context.Add(item);
                }
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }