Example #1
0
        public JsonResult ChangeStatus(InvoiceChangeStatusViewModel model)
        {
            try
            {
                var currentUser = GetAuthenticatedUser();
                var invoice     = _context.Invoice.Single(x => x.InvoiceStatusId != InvoiceStatus.Deleted.Id && x.Id == model.invoiceId);

                invoice.InvoiceStatusId = model.statusId;
                invoice.ModifyUserId    = currentUser.id;
                invoice.ModifyDate      = GetDatetime();
                invoice.ModifyIp        = GetCurrentIp();

                var invoiceLog = new InvoiceLog()
                {
                    InvoiceId    = model.invoiceId,
                    StatusId     = model.statusId,
                    Description  = model.description.ToStandardPersian(),
                    CreateUserId = currentUser.id,
                    CreateDate   = GetDatetime(),
                    CreateIp     = GetCurrentIp()
                };

                _context.InvoiceLog.Add(invoiceLog);
                _context.SaveChanges();

                return(Success("تغییر وضعیت با موفقیت انجام شد."));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));
            }
        }
        public async Task <(bool IsSuccess, string ErrorMessage)> CreateInvoiceLog(InvoiceLog invoiceLog)
        {
            try
            {
                using (ZubairEntities dbContext = new ZubairEntities())
                {
                    if (invoiceLog != null)
                    {
                        var tempInvoiceItem = new invoicelog
                        {
                            invoice_id      = invoiceLog.InvoiceId,
                            invoicestatusid = invoiceLog.InvoiceStatusID,
                            datecreated     = DateTime.Now
                        };

                        dbContext.invoicelogs.Add(tempInvoiceItem);
                        await dbContext.SaveChangesAsync();

                        return(true, null);
                    }
                }
                return(false, "Not Found");
            }
            catch (Exception ex)
            {
                //logger?.LogError(ex.ToString());
                return(false, ex.Message);
            }
        }
        public IActionResult Index()
        {
            // random customer
            Random r            = new Random();
            int    invoiceLogId = r.Next(1, 1000);
            int    customerId   = r.Next(1, 5000);

            List <Setting> settings = (
                from s in db.Settings
                orderby s.Name
                select s
                ).ToList();
            Customer customer = (
                from c in db.Customer
                where c.CustomerId == customerId
                select c
                ).First(); // assume it exists
            InvoiceLog invoiceLog = (
                from s in db.InvoiceLog
                where s.InvoiceLogId == invoiceLogId
                select s
                ).First(); // assume it exists

            return(View(new HomeViewModel
            {
                Settings = settings,
                FeaturedCustomer = customer,
                FeaturedInvoiceLog = invoiceLog
            }));
        }
Example #4
0
        public JsonResult InvoicePaid(InvoicePaidViewModel model)
        {
            try
            {
                if (model.invoiceId <= 0)
                {
                    return(Error("مبلغ برای ثبت می بایست از "));
                }

                var currentUser = GetAuthenticatedUser();

                var invoice = _context.Invoice.Where(x => x.InvoiceStatusId != InvoiceStatus.Deleted.Id && x.Id == model.invoiceId).Single();
                if (invoice.UnpaidPrice < model.price)
                {
                    return(Error("مبلغ وارد شده از میزان بدهی فاکتور بیشتر است."));
                }

                invoice.UnpaidPrice -= model.price;
                invoice.ModifyUserId = currentUser.id;
                invoice.ModifyDate   = GetDatetime();
                invoice.ModifyIp     = GetCurrentIp();


                var invoiceLog = new InvoiceLog()
                {
                    InvoiceId    = model.invoiceId,
                    StatusId     = invoice.InvoiceStatusId,
                    Description  = "ثبت پرداخت به مبلغ " + model.price.ToString("N0") + " " + AsefianMetadata.CurrencyName,
                    CreateUserId = currentUser.id,
                    CreateDate   = GetDatetime(),
                    CreateIp     = GetCurrentIp()
                };

                _context.InvoiceLog.Add(invoiceLog);
                _context.SaveChanges();

                return(Success("ثبت پرداخت با موفقیت انجام شد."));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));

                throw;
            }
        }
Example #5
0
        public async Task <List <InvoiceDto> > ChangeStateOfInvoiceList(InvoiceChangeStatusDto input, string userId)
        {
            var invoiceList = await UnitOfWork.InvoiceRepository.GetByNumberListAsync(input.Invoices);

            foreach (var item in invoiceList)
            {
                InvoiceLog log = new InvoiceLog()
                {
                    Id          = Guid.NewGuid(),
                    FromState   = item.InvoiceStatus,
                    ToState     = input.InvoiceStatus,
                    Description = input.Description,
                    UserId      = userId
                };

                item.InvoiceStatus = input.InvoiceStatus;
                UnitOfWork.InvoiceRepository.Update(item);
                UnitOfWork.InvoiceLogRepository.Create(log);
            }

            await UnitOfWork.SaveAsync();

            return(invoiceList.MapTo <List <InvoiceDto> >());
        }
Example #6
0
        public ActionResult Shipping(ShippingViewModel model)
        {
            try
            {
                var currentUser = GetAuthenticatedUser();
                var cartList    = _context.Cart.Where(x => x.UserId == currentUser.id).ToList();

                if (cartList.Count == 0)
                {
                    return(Error("سبد خرید شما خالی است."));
                }

                var addressList = _context.UserAddress.Where(x => x.StatusId == UserAddressStatus.Active.Id && x.UserId == currentUser.id).ToList();
                var address     = addressList.Single(x => x.Id == model.addressId);
                if (model.addressId > 0)
                {
                    if (addressList.SingleOrDefault(x => x.Id == model.addressId) == null)
                    {
                        return(Error("آدرس انتخاب شده صحیح نیست."));
                    }
                    else
                    {
                        addressList.ForEach(x => x.MainAddress = false);
                        address.MainAddress = true;
                    }
                }
                else
                {
                    return(Error("لطفا آدرس تحویل سفارش خود را مشخص کنید."));
                }

                if (model.deliveryType > 0)
                {
                    if (model.deliveryType == 1 && string.IsNullOrEmpty(model.time) && model.time.Split(' ').Length != 2)
                    {
                        return(Error("لطفا زمان تحویل سفارش خود را مشخص کنید."));
                    }
                }
                else
                {
                    return(Error("لطفا نحوه تحویل سفارش خود را مشخص کنید."));
                }

                Response couponResponse = null;
                Coupon   coupon         = null;
                if (!string.IsNullOrEmpty(model.coupon))
                {
                    couponResponse = CouponController.CheckCoupon(_context, currentUser, model.coupon, out coupon);
                    if (couponResponse.status != ResponseStatus.Ok)
                    {
                        return(Error(couponResponse.message));
                    }
                }

                var invoiceDetailList = new List <InvoiceDetail>();
                foreach (var item in cartList)
                {
                    var totalPrice    = item.Product.Price - item.Product.Discount;
                    var invoiceDetail = new InvoiceDetail()
                    {
                        ProductId        = item.ProductId,
                        ProductFeatureId = item.ProductFeatureId,
                        Count            = item.Count,
                        Price            = totalPrice,
                        StatusId         = InvoiceDetailStatus.Accepted.Id,
                        CreateUserId     = currentUser.id,
                        ModifyUserId     = currentUser.id,
                        CreateDate       = GetDatetime(),
                        ModifyDate       = GetDatetime(),
                        CreateIp         = GetCurrentIp(),
                        ModifyIp         = GetCurrentIp()
                    };
                    invoiceDetailList.Add(invoiceDetail);
                }

                var price = invoiceDetailList.Sum(x => x.Count * x.Price);

                if (coupon != null)
                {
                    if (coupon.TypeId == CouponType.Amount.Id)
                    {
                        price = price - coupon.Value;
                    }
                    else if (coupon.TypeId == CouponType.Percentage.Id)
                    {
                        price = price - ((price / 100) * coupon.Value);
                    }

                    if (price < 0)
                    {
                        price = 0;
                    }
                }

                var keys             = new string[] { BaseInformationKey.OrderGapDay, BaseInformationKey.MinDeliveryFreePrice, BaseInformationKey.DeliveryPrice, BaseInformationKey.DeliveryManCity };
                var baseInformations = _context.BaseInformation.Where(x => keys.Any(y => y == x.Key));

                var GapDay = int.Parse(baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.OrderGapDay)?.Value ?? AsefianMetadata.DefaultGapDay);
                var MinDeliveryFreePrice = long.Parse(baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.MinDeliveryFreePrice)?.Value ?? AsefianMetadata.DefaultMinDeliveryFreePrice);
                var DeliveryPrice        = long.Parse(baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.DeliveryPrice)?.Value ?? AsefianMetadata.DefaultDeliveryPrice);

                if (model.deliveryType == DeliveryType.DeliveryMan.Id)
                {
                    var deliveryManCity = baseInformations.SingleOrDefault(x => x.Key == BaseInformationKey.DeliveryManCity)?.Value.Split('-').Select(x => int.Parse(x)).ToArray() ?? new int[] { };
                    if (deliveryManCity.Count(x => x == address.CityId) == 0)
                    {
                        return(Error("برای آدرس مورد نظر شما امکان ارسال با پیک فراهم نیست."));
                    }
                }

                var deliveryPrice = DeliveryPrice;
                if (price >= MinDeliveryFreePrice)
                {
                    deliveryPrice = 0;
                }

                DateTime?deliveryDate = null;
                if (!string.IsNullOrEmpty(model.time))
                {
                    deliveryDate = DateUtility.GetDateTime(model.time.Split(' ')[0]);
                }

                if (deliveryDate != null && DateTime.Today.AddDays(GapDay) > deliveryDate)
                {
                    return(Error("تاریخ وارد شده برای تحویل سفارش صحیح نیست."));
                }

                var invoice = new Invoice()
                {
                    UserId          = currentUser.id,
                    InvoiceStatusId = InvoiceStatus.Registered.Id,
                    PaymentTypeId   = PaymentType.Unknown.Id,
                    InvoiceNo       = GetNewInvoiceNo(),
                    Price           = price,
                    UnpaidPrice     = price,
                    DeliveryPrice   = deliveryPrice,
                    DeliveryTypeId  = model.deliveryType,
                    AddressId       = model.addressId,
                    DeliveryDate    = deliveryDate,
                    DeliveryTime    = model.time?.Split(' ')[1],
                    CouponId        = coupon?.Id,
                    CreateUserId    = currentUser.id,
                    ModifyUserId    = currentUser.id,
                    CreateDate      = GetDatetime(),
                    ModifyDate      = GetDatetime(),
                    CreateIp        = GetCurrentIp(),
                    ModifyIp        = GetCurrentIp(),

                    DetailList = invoiceDetailList
                };

                _context.Cart.RemoveRange(cartList);
                _context.Invoice.Add(invoice);

                var invoiceLog = new InvoiceLog()
                {
                    Invoice      = invoice,
                    StatusId     = InvoiceStatus.Registered.Id,
                    CreateUserId = currentUser.id,
                    CreateDate   = GetDatetime(),
                    CreateIp     = GetCurrentIp()
                };
                _context.InvoiceLog.Add(invoiceLog);

                _context.SaveChanges();

                return(Success("سفارش شما با موفقیت ثبت شد.", new
                {
                    id = invoice.Id
                }));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));
            }
        }
Example #7
0
        public ActionResult SubmitPayment(SubmitPaymentViewModel model)
        {
            var currentUser = GetAuthenticatedUser();

            if (model.paymentType <= 0)
            {
                TempData["SubmitPaymentError"] = "انتخاب نوع پرداخت اجباری است.";
                return(Redirect("/invoice/payment/" + model.id));
            }

            var invoice = _context.Invoice.Where(x => x.InvoiceStatusId == InvoiceStatus.Registered.Id && x.Id == model.id && x.UserId == currentUser.id).SingleOrDefault();

            if (invoice == null)
            {
                return(HttpNotFound());
            }

            if (invoice.InvoiceStatusId == InvoiceStatus.InProgress.Id)
            {
                return(Redirect("/invoice/detail/" + model.id));
            }

            if (invoice.CouponId != null && !string.IsNullOrEmpty(model.coupon))
            {
                Response couponResponse = null;

                couponResponse = CouponController.CheckCoupon(_context, currentUser, model.coupon, out Coupon coupon);
                if (couponResponse.status != ResponseStatus.Ok)
                {
                    TempData["SubmitPaymentError"] = "انتخاب نوع پرداخت اجباری است.";
                    return(Redirect("/invoice/payment/" + model.id));
                }

                if (coupon.TypeId == CouponType.Amount.Id)
                {
                    invoice.Price -= coupon.Value;
                }
                else if (coupon.TypeId == CouponType.Percentage.Id)
                {
                    invoice.Price -= ((invoice.Price / 100) * coupon.Value);
                }

                if (invoice.Price < 0)
                {
                    invoice.Price = 0;
                }

                invoice.CouponId = coupon.Id;
            }

            invoice.InvoiceStatusId = InvoiceStatus.InProgress.Id;
            invoice.PaymentTypeId   = model.paymentType;
            invoice.ModifyUserId    = currentUser.id;
            invoice.ModifyDate      = GetDatetime();
            invoice.ModifyIp        = GetCurrentIp();

            var invoiceLog = new InvoiceLog()
            {
                InvoiceId    = invoice.Id,
                StatusId     = InvoiceStatus.InProgress.Id,
                CreateUserId = currentUser.id,
                CreateDate   = GetDatetime(),
                CreateIp     = GetCurrentIp()
            };

            _context.InvoiceLog.Add(invoiceLog);

            _context.SaveChanges();

            return(Redirect("/invoice/detail/" + model.id));
        }
Example #8
0
        public JsonResult RemoveProduct(InvoiceChangeProductViewModel model)
        {
            try
            {
                var currentUser = GetAuthenticatedUser();

                var product       = _context.Product.Single(x => x.StatusId != ProductStatus.Deleted.Id && x.Id == model.productId);
                var invoice       = _context.Invoice.Single(x => x.InvoiceStatusId != InvoiceStatus.Deleted.Id && x.Id == model.invoiceId);
                var invoiceDetail = invoice.DetailList.Single(x => x.StatusId != InvoiceDetailStatus.Deleted.Id && x.ProductId == model.productId);

                var availableInvoiceStatusList = new InvoiceStatus[] { InvoiceStatus.Registered, InvoiceStatus.InProgress, InvoiceStatus.Factored, InvoiceStatus.Preparing };
                if (!availableInvoiceStatusList.Any(x => x.Id == invoice.InvoiceStatusId))
                {
                    return(Error("فاکتور در وضعیتی نیست که بتوان از آن محصول کسر کرد."));
                }

                if (model.count > invoiceDetail.Count)
                {
                    return(Error("تعداد وارد شده از تعداد ثبت شده در فاکتور بیشتر است."));
                }

                var price = model.count * invoiceDetail.Price;
                if (price > invoice.UnpaidPrice)
                {
                    var user = _context.User.Single(x => x.Id == currentUser.id);
                    user.Balance = user.Balance + price;

                    var balanceLog = new UserBalanceLog()
                    {
                        UserId       = invoice.User.Id,
                        Amount       = price,
                        TypeId       = BalanceType.Positive.Id,
                        Description  = string.Format("بابت کسر محصول {0} به تعداد {1} از فاکتور {2}", product.Code, model.count, invoice.InvoiceNo),
                        CreateUserId = currentUser.id,
                        CreateDate   = GetDatetime(),
                        CreateIp     = GetCurrentIp()
                    };
                    _context.UserBalanceLog.Add(balanceLog);
                }
                else
                {
                    invoice.UnpaidPrice -= price;
                }

                invoice.Price       = invoice.Price - price;
                invoiceDetail.Count = invoiceDetail.Count - model.count;

                if (invoiceDetail.Count == 0)
                {
                    invoiceDetail.StatusId = InvoiceDetailStatus.Deleted.Id;
                }

                var invoiceLog = new InvoiceLog()
                {
                    InvoiceId    = invoice.Id,
                    StatusId     = invoice.InvoiceStatusId,
                    Description  = string.Format("کسر محصول {0} به تعداد {1} از فاکتور", product.Code, model.count),
                    CreateUserId = currentUser.id,
                    CreateDate   = GetDatetime(),
                    CreateIp     = GetCurrentIp()
                };
                _context.InvoiceLog.Add(invoiceLog);

                _context.SaveChanges();

                return(Success("محصول مورد نظر با موفقیت از فاکتور کسر شد."));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));
            }
        }
Example #9
0
        public JsonResult Addproduct(InvoiceChangeProductViewModel model)
        {
            try
            {
                var currentUser = GetAuthenticatedUser();

                var product = _context.Product.Single(x => x.StatusId != ProductStatus.Deleted.Id && x.Id == model.productId);
                if (model.count <= 0)
                {
                    return(Error("تعداد وارد شده صحیح نیست."));
                }
                if (product.Count < model.count)
                {
                    return(Error("تعداد موجودی از تعداد درخواستی برای اضافه شدن به فاکتور بیشتر است."));
                }

                var invoice = _context.Invoice.Single(x => x.InvoiceStatusId != InvoiceStatus.Deleted.Id && x.Id == model.invoiceId);

                var availableInvoiceStatusList = new InvoiceStatus[] { InvoiceStatus.Registered, InvoiceStatus.InProgress, InvoiceStatus.Factored, InvoiceStatus.Preparing };
                if (!availableInvoiceStatusList.Any(x => x.Id == invoice.InvoiceStatusId))
                {
                    return(Error("فاکتور در وضعیتی نیست که بتوان به آن محصول اضافه کرد."));
                }

                var invoiceDetail = new InvoiceDetail()
                {
                    InvoiceId        = invoice.Id,
                    ProductId        = model.productId,
                    ProductFeatureId = model.productFeatureId,
                    Count            = model.count,
                    Price            = product.Price,
                    StatusId         = InvoiceDetailStatus.Accepted.Id,
                    CreateUserId     = currentUser.id,
                    ModifyUserId     = currentUser.id,
                    CreateDate       = GetDatetime(),
                    ModifyDate       = GetDatetime(),
                    CreateIp         = GetCurrentIp(),
                    ModifyIp         = GetCurrentIp()
                };
                _context.InvoiceDetail.Add(invoiceDetail);

                invoice.Price       += (invoiceDetail.Count * invoiceDetail.Price);
                invoice.UnpaidPrice += (invoiceDetail.Count * invoiceDetail.Price);
                invoice.ModifyUserId = currentUser.id;
                invoice.ModifyDate   = GetDatetime();
                invoice.ModifyIp     = GetCurrentIp();

                var invoiceLog = new InvoiceLog()
                {
                    InvoiceId    = invoice.Id,
                    StatusId     = invoice.InvoiceStatusId,
                    Description  = string.Format("اضافه کردن محصول {0} به تعداد {1} در فاکتور", product.Code, model.count),
                    CreateUserId = currentUser.id,
                    CreateDate   = GetDatetime(),
                    CreateIp     = GetCurrentIp()
                };
                _context.InvoiceLog.Add(invoiceLog);

                product.Count       -= model.count;
                product.ModifyUserId = currentUser.id;
                product.ModifyDate   = GetDatetime();
                product.ModifyIp     = GetCurrentIp();

                _context.SaveChanges();

                return(Success("محصول مورد نظر با موفقیت به فاکتور اضافه شد."));
            }
            catch (Exception ex)
            {
                return(ServerError(ex));
            }
        }
Example #10
0
        public ActionResult ChequeRealize(ChequeRealizeViewModel model)
        {
            if (ModelState.IsValid)
            {
                int           GeneralLedgerHeadId = (int)db.BankAccountLedgerHeads.Find(1).GeneralLedgerHeadId;
                GeneralLedger glObj = new GeneralLedger {
                    Amount = model.Amount, ApplicationUserId = User.Identity.GetUserId(), GeneralLedgerHeadId = GeneralLedgerHeadId, Notes = model.RealizationRemarks + " - " + model.Remarks + " - " + "Cheque Realization", PaymentMethods = PaymentMethod.Cheque, StatementTypes = TransactionType.Income, SysDateTime = DateTime.Now, GeneralLedgerType = LedgerType.Credit
                };
                db.GeneralLedgers.Add(glObj);
                db.SaveChanges();

                Agent agent = db.Agents.Find(model.Agents.Id);
                agent.Balance         = (agent.Balance - model.Amount);
                db.Entry(agent).State = EntityState.Modified;
                db.SaveChanges();

                AgentLedger alObj = new AgentLedger {
                    AgentId = model.Agents.Id, AgentLedgerHeadId = 3, Amount = model.Amount, ApplicationUserId = glObj.ApplicationUserId, Balance = agent.Balance, Remarks = glObj.Notes, SystemDate = glObj.SysDateTime
                };
                db.AgentLedgers.Add(alObj);
                db.SaveChanges();

                BankAccount baObj = db.BankAccounts.Find(model.BankAccountId);
                baObj.Balance         = (baObj.Balance + model.Amount);
                db.Entry(baObj).State = EntityState.Modified;
                db.SaveChanges();

                BankAccountLedger bclObj = new BankAccountLedger {
                    Amount = model.Amount, ApplicationUserId = glObj.ApplicationUserId, Balance = baObj.Balance, BankAccountId = model.BankAccountId, BankAccountLedgerHeadId = 1, LedgerTypes = LedgerType.Credit, Notes = glObj.Notes, PaymentMethods = PaymentMethod.Cheque, RelationId = null, SysDateTime = glObj.SysDateTime
                };
                db.BankAccountLedgers.Add(bclObj);
                db.SaveChanges();

                InvoicePayment ipObj = new InvoicePayment {
                    Amount = model.Amount, ApplicationUserId = glObj.ApplicationUserId, GeneralLedgerId = glObj.Id, InvoiceId = model.InvoiceId, PaymentMethods = PaymentMethod.Cheque, Remarks = model.RealizationRemarks + " - " + model.Remarks + " - " + "Cheque Realization", SysDateTime = glObj.SysDateTime, AgentLedgerId = alObj.Id, BankAccountLedgerId = bclObj.Id
                };
                db.InvoicePayments.Add(ipObj);
                db.SaveChanges();

                InvoiceLog ilObj = new InvoiceLog {
                    ApplicationUserId = glObj.ApplicationUserId, InvoiceId = model.InvoiceId, Remarks = "Payment Received by Cheque Transaction - Realization", SysDateTime = glObj.SysDateTime
                };
                db.InvoiceLogs.Add(ilObj);
                db.SaveChanges();

                IPChequeDetail ipchObj = db.IPChequeDetails.Find(model.IPChequeDetailId);
                ipchObj.GeneralLedgerId  = glObj.Id;
                ipchObj.InvoicePaymentId = ipObj.Id;
                ipchObj.BulkPayment      = false;
                ipchObj.Status           = ChequeStatus.Passed;
                db.Entry(ipchObj).State  = EntityState.Modified;
                db.SaveChanges();

                bclObj.RelationId       = ipchObj.Id;
                db.Entry(ipchObj).State = EntityState.Modified;
                db.SaveChanges();
                FlashMessage.Confirmation("Floating Cheque Realized");
                return(RedirectToAction("FloatingCheque", "Accounting"));
            }
            else
            {
                return(RedirectToAction("FloatingCheque", "Accounting"));
            }
        }