Пример #1
0
        public ActionResult Save(SaveVoucherJsonModel model)
        {
            var alreadyUsedVoucherNo = _fR.GetAny <Order>(x => x.VoucherNumber == model.VoucherNo && x.Status && !x.IsDeleted);

            if (alreadyUsedVoucherNo)
            {
                return(Json(new Response {
                    Status = false, Message = "Belge no daha önce kullanılmış"
                }, JsonRequestBehavior.AllowGet));
            }

            using (System.Transactions.TransactionScope scope = new System.Transactions.TransactionScope())
            {
                try
                {
                    //
                    //order date parse useable datetime format
                    DateTime voucherDate;
                    try
                    {
                        voucherDate = DateTime.ParseExact(model.VoucherDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    }
                    catch (Exception ex)
                    {
                        return(Json(new Response {
                            Status = false, Message = "Tarih formatı yanlış Lütfen sayfayı yenileyip tekrar deneyin"
                        }, JsonRequestBehavior.AllowGet));
                    }
                    //
                    var order = new Order
                    {
                        PlatformCode  = model.PlatformCode,
                        CompanyId     = model.CustomerId,
                        SupplierId    = model.SupplierId,
                        VoucherNumber = model.VoucherNo,
                        VoucherDate   = voucherDate,
                        CreatedDate   = DateTime.Now,
                        Tax           = model.TaxPercent,
                        Status        = true
                    };
                    //
                    //save order
                    _fR.Add(order);
                    _fR.SaveChanges();

                    var productList = new List <OrderProduct>();
                    foreach (var item in model.Products)
                    {
                        var op = new OrderProduct
                        {
                            OrderId        = order.Id,
                            ProductId      = item.ProductId,
                            ProductBarcode = item.Barcode,
                            ProductName    = item.ProductName,
                            Discount       = item.ProductDiscount,
                            DiscountName   = item.ProductDiscountName,
                            Quantity       = item.ProductQuantity,
                            Price          = string.IsNullOrEmpty(item.ProductPrice) ? 0 : Convert.ToDecimal(item.ProductPrice),
                        };
                        //
                        op.Total = op.CalculateTotalPrice();
                        var taxAmount = op.Total * order.Tax / 100;
                        op.Total = op.Total + taxAmount;
                        //
                        productList.Add(op);
                    }
                    //
                    //save order
                    _fR.AddList(productList);
                    _fR.SaveChanges();
                    //
                    scope.Complete();
                    return(Json(new Response {
                        Status = true, EntityId = model.VoucherNo + 1, Message = "Belge kayıt edildi."
                    }, JsonRequestBehavior.AllowGet));
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    return(Json(new Response {
                        Status = false, EntityId = 0, Message = ex.InnerException?.InnerException.Message ?? ex.Message, TotalCount = 0
                    }));
                }
            }
        }