Exemplo n.º 1
0
        public ActionResult Edit(int?Id)
        {
            var ProcessPayment = ProcessPaymentRepository.GetProcessPaymentById(Id.Value);

            if (ProcessPayment != null && ProcessPayment.IsDeleted != true)
            {
                var model = new ProcessPaymentViewModel();
                AutoMapper.Mapper.Map(ProcessPayment, model);

                if (model.CreatedUserId != Erp.BackOffice.Helpers.Common.CurrentUser.Id && Erp.BackOffice.Helpers.Common.CurrentUser.UserTypeId != 1)
                {
                    TempData["FailedMessage"] = "NotOwner";
                    return(RedirectToAction("Index"));
                }

                return(View(model));
            }
            if (Request.UrlReferrer != null)
            {
                return(Redirect(Request.UrlReferrer.AbsoluteUri));
            }
            return(RedirectToAction("Index"));
        }
        public void ResolveLiabilities(ResolveLiabilitiesViewModel model, string PaymentMethod, string receiptCode, int receiptId)
        {
            if (model.TargetModule == "Customer")
            {
                //Lấy đơn hàng và cập nhật số tiền đã trả
                var productInvoice = productInvoiceRepository.GetAllProductInvoice()
                                     .Where(item => item.IsArchive && item.IsDeleted == false && item.Code == model.MaChungTuGoc).FirstOrDefault();
                productInvoice.PaidAmount     += Convert.ToDecimal(model.Amount);
                productInvoice.RemainingAmount = productInvoice.TotalAmount - productInvoice.PaidAmount;
                productInvoiceRepository.UpdateProductInvoice(productInvoice);

                //Lấy thông tin KH
                var customer = customerRepository.GetCustomerById(productInvoice.CustomerId.Value);

                //Lập chi tiết phiếu thu

                var receiptDetail = new ReceiptDetail();
                receiptDetail.IsDeleted      = false;
                receiptDetail.CreatedUserId  = WebSecurity.CurrentUserId;
                receiptDetail.ModifiedUserId = WebSecurity.CurrentUserId;
                receiptDetail.AssignedUserId = WebSecurity.CurrentUserId;
                receiptDetail.CreatedDate    = DateTime.Now;
                receiptDetail.ModifiedDate   = DateTime.Now;

                receiptDetail.Name           = "Thu tiền khách hàng";
                receiptDetail.Amount         = model.Amount;
                receiptDetail.ReceiptId      = receiptId;
                receiptDetail.MaChungTuGoc   = productInvoice.Code;
                receiptDetail.LoaiChungTuGoc = "ProductInvoice";

                receiptDetailRepository.InsertReceiptDetail(receiptDetail);
                //Thêm vào quản lý chứng từ
                TransactionController.Create(new TransactionViewModel
                {
                    TransactionModule = "Receipt",
                    TransactionCode   = receiptCode,
                    TransactionName   = "Thu tiền khách hàng"
                });

                //Thêm chứng từ liên quan
                TransactionController.CreateRelationship(new TransactionRelationshipViewModel
                {
                    TransactionA = receiptCode,
                    TransactionB = productInvoice.Code
                });

                //Lấy lịch sử giao dịch thanh toán
                var q = transactionLiabilitiesRepository.GetAllvwTransaction()
                        .Where(item => item.MaChungTuGoc == productInvoice.Code).ToList();
                decimal du_no = 0;
                if (q.Count > 0)
                {
                    du_no = q.Sum(item => item.Debit - item.Credit);
                }


                bool da_thanh_toan_du = false;
                if (Convert.ToDecimal(model.Amount) == du_no)
                {
                    da_thanh_toan_du = true;
                }

                //Ghi Có TK 131 - Phải thu của khách hàng.
                Erp.BackOffice.Account.Controllers.TransactionLiabilitiesController.Create(
                    receiptCode,
                    "Receipt",
                    "Thu tiền khách hàng",
                    model.TargetCode,
                    model.TargetModule,
                    0,
                    Convert.ToDecimal(model.Amount),
                    model.MaChungTuGoc,
                    model.LoaiChungTuGoc,
                    model.PaymentMethod = PaymentMethod,
                    da_thanh_toan_du ? null : model.NextPaymentDate,
                    model.Note);

                //Cập nhật ngày hẹn trả cho đơn hàng này
                productInvoice.NextPaymentDate = da_thanh_toan_du ? null : model.NextPaymentDate;
                productInvoiceRepository.UpdateProductInvoice(productInvoice);
            }
            else
            {
                //Lấy đơn hàng và cập nhật số tiền đã trả
                var purchaseOrder = purchaseOrderRepository.GetAllPurchaseOrder()
                                    .Where(item => item.IsArchive.Value && item.IsDeleted == false && item.Code == model.MaChungTuGoc).FirstOrDefault();
                purchaseOrder.PaidAmount     += Convert.ToDecimal(model.Amount);
                purchaseOrder.RemainingAmount = purchaseOrder.TotalAmount - purchaseOrder.PaidAmount;
                purchaseOrderRepository.UpdatePurchaseOrder(purchaseOrder);

                //Lấy thông tin KH
                var supplier = supplierRepository.GetSupplierById(purchaseOrder.SupplierId.Value);
                var payment  = new Payment();
                payment.IsDeleted      = false;
                payment.CreatedUserId  = WebSecurity.CurrentUserId;
                payment.ModifiedUserId = WebSecurity.CurrentUserId;
                payment.AssignedUserId = WebSecurity.CurrentUserId;
                payment.CreatedDate    = DateTime.Now;
                payment.ModifiedDate   = DateTime.Now;
                payment.MaChungTuGoc   = model.MaChungTuGoc;
                payment.LoaiChungTuGoc = model.LoaiChungTuGoc;

                paymentRepository.InsertPayment(payment);

                payment.Code = Erp.BackOffice.Helpers.Common.GetOrderNo("Payment");
                paymentRepository.UpdatePayment(payment);
                Erp.BackOffice.Helpers.Common.SetOrderNo("Payment");
                //Thêm vào quản lý chứng từ
                TransactionController.Create(new TransactionViewModel
                {
                    TransactionModule = "Payment",
                    TransactionCode   = payment.Code,
                    TransactionName   = "Chi tiền nhà cung cấp"
                });

                //Thêm chứng từ liên quan
                TransactionController.CreateRelationship(new TransactionRelationshipViewModel
                {
                    TransactionA = payment.Code,
                    TransactionB = purchaseOrder.Code
                });
                //Lấy lịch sử giao dịch thanh toán
                var q = transactionLiabilitiesRepository.GetAllvwTransaction()
                        .Where(item => item.MaChungTuGoc == purchaseOrder.Code).ToList();
                decimal du_no = 0;
                if (q.Count > 0)
                {
                    du_no = q.Sum(item => item.Debit - item.Credit);
                }


                bool da_thanh_toan_du = false;
                if (Convert.ToDecimal(model.Amount) == du_no)
                {
                    da_thanh_toan_du = true;
                }
                //Ghi Có TK ??? - Phải trả nhà cung cấp
                Erp.BackOffice.Account.Controllers.TransactionLiabilitiesController.Create(
                    payment.Code,
                    "Payment",
                    "Chi tiền nhà cung cấp",
                    model.TargetCode,
                    model.TargetModule,
                    0,
                    Convert.ToDecimal(model.Amount),
                    model.MaChungTuGoc,
                    model.LoaiChungTuGoc,
                    model.PaymentMethod = PaymentMethod,
                    model.NextPaymentDate,
                    model.Note);
                //Cập nhật ngày hẹn trả cho đơn hàng này
                purchaseOrder.NextPaymentDate = da_thanh_toan_du ? null : model.NextPaymentDate;
                purchaseOrderRepository.UpdatePurchaseOrder(purchaseOrder);
            }

            //Update Process Payment
            if (model.ProcessPaymentId > 0)
            {
                var processPayment = processPaymentRepository.GetProcessPaymentById(model.ProcessPaymentId.Value);
                processPayment.ModifiedUserId = WebSecurity.CurrentUserId;
                processPayment.ModifiedDate   = DateTime.Now;
                processPayment.Status         = "Đã thanh toán";

                processPaymentRepository.UpdateProcessPayment(processPayment);
            }
        }