public JsonResult ResetConsignmentContract(long consignmentID)
 {
     try
     {
         var consignmentContract = AuctionRepository.GetConsignmentContract(consignmentID);
         var consignment         = AuctionRepository.GetConsignment(consignmentID);
         if (consignmentContract == null || consignment == null)
         {
             throw new Exception("Consignment does not exist.");
         }
         if (consignmentContract.StatusID == (int)Consts.ConsignmentContractStatus.Signed)
         {
             throw new Exception("The contract is already signed by seller.");
         }
         var filePath         = DiffMethods.TemplateDifferentOnDisk("ConsignmentContract.txt");
         var fileInfo         = new FileInfo(filePath);
         var contractTemplate = string.Empty;
         if (fileInfo.Exists)
         {
             contractTemplate = System.IO.File.ReadAllText(filePath);
             var buyerFee    = consignment.Event.BuyerFee.GetValueOrDefault(0).GetPrice();
             var buyerFeeStr = buyerFee.ToString();
             if (buyerFee == (int)buyerFee)
             {
                 buyerFeeStr = buyerFeeStr.Remove(buyerFeeStr.Length - 3);
             }
             contractTemplate =
                 contractTemplate.Replace("{{BuyersPremium}}", string.Format("{0}%", buyerFeeStr))
                 .Replace("{{CommissionRate}}", consignment.User.CommissionRate.LongDescription);
         }
         if (!string.IsNullOrWhiteSpace(consignmentContract.FileName))
         {
             fileInfo =
                 new FileInfo(DiffMethods.ConsignmentContractOnDisk(consignmentID, consignmentContract.FileName));
             if (fileInfo.Exists)
             {
                 fileInfo.Delete();
             }
         }
         consignmentContract.UpdateFields(consignmentContract.ConsignmentID,
                                          (int)Consts.ConsignmentContractStatus.NotGenerated, contractTemplate, string.Empty);
         AuctionRepository.UpdateConsignmentContract(consignmentContract);
         return
             (JSON(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS, "Contract was reset to default.",
                                         new
         {
             StatusText = Consts.ConsignmentContractStatus.NotGenerated.ToString(),
             ContractText = contractTemplate
         })));
     }
     catch (Exception exc)
     {
         return(JSON(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, exc.Message)));
     }
 }
        public FileStreamResult ShowConsignmentContract(long consignmentID)
        {
            var consignmentContract = AuctionRepository.GetConsignmentContract(consignmentID);

            if (consignmentContract == null ||
                consignmentContract.StatusID == (int)Consts.ConsignmentContractStatus.NotGenerated ||
                string.IsNullOrWhiteSpace(consignmentContract.FileName))
            {
                return(null);
            }
            var path = DiffMethods.ConsignmentContractOnDisk(consignmentID, consignmentContract.FileName);
            var fs   = new FileStream(path, FileMode.Open, FileAccess.Read);

            return(File(fs, "application/pdf"));
        }
 public FilePathResult GetConsignmentContract(long consignmentID)
 {
     try
     {
         var consignmentContract = AuctionRepository.GetConsignmentContract(consignmentID);
         if (consignmentContract == null ||
             consignmentContract.StatusID == (int)Consts.ConsignmentContractStatus.NotGenerated ||
             string.IsNullOrWhiteSpace(consignmentContract.FileName))
         {
             return(null);
         }
         var path = DiffMethods.ConsignmentContractOnDisk(consignmentID, consignmentContract.FileName);
         return(File(path, "application/pdf", consignmentContract.FileName));
     }
     catch
     {
         return(null);
     }
 }
 public JsonResult UpdateConsignmentContractText(long consignmentID, string contractText)
 {
     try
     {
         var consignment = AuctionRepository.GetConsignment(consignmentID);
         if (consignment == null)
         {
             throw new Exception("Consignment does not exist.");
         }
         var consignmentContract = AuctionRepository.GetConsignmentContract(consignmentID);
         if (consignmentContract == null)
         {
             throw new Exception("Consignment does not exist.");
         }
         if (consignmentContract.StatusID == (int)Consts.ConsignmentContractStatus.Signed)
         {
             throw new Exception("The contract is already signed by seller.");
         }
         consignmentContract.ContractText = contractText;
         var fileName   = string.Format("CA{0}.pdf", DateTime.Now.Ticks);
         var specialist = AuctionRepository.GetSpecialist(consignment.Specialist_ID.GetValueOrDefault(-1));
         if (specialist == null)
         {
             throw new Exception("Set specialist at first.");
         }
         var lelandsSignaturePath =
             DiffMethods.SignatureImagesOnDisk(string.Format("userSignature{0}.png", specialist.User_ID));
         var fileInfo = new FileInfo(lelandsSignaturePath);
         if (!fileInfo.Exists)
         {
             throw new Exception("You can't generate contract without specialist signature.");
         }
         if (!string.IsNullOrWhiteSpace(consignmentContract.FileName))
         {
             fileInfo =
                 new FileInfo(DiffMethods.ConsignmentContractOnDisk(consignment.ID, consignmentContract.FileName));
             if (fileInfo.Exists)
             {
                 fileInfo.Delete();
             }
         }
         var lelandsAddress = new Address
         {
             FirstName = "Lelands.com",
             LastName  = Consts.SiteEmail,
             Address_1 = Consts.CompanyAddress,
             City      = Consts.CompanyCity,
             State     = Consts.CompanyState,
             Zip       = Consts.CompanyZip,
             HomePhone = Consts.CompanyPhone,
             WorkPhone = Consts.CompanyFax
         };
         var consignor        = consignment.User;
         var consignorAddress = Address.GetAddress(consignor.AddressCard_Billing);
         var items            = AuctionRepository.GetAuctionsListByConsignor(consignment.ID);
         PdfReports.ConsignmentContract(DiffMethods.ConsignmentContractOnDisk(consignment.ID, fileName),
                                        DiffMethods.PublicImagesOnDisk("logo.png"), string.Empty, Consts.CompanyTitleName, string.Empty,
                                        lelandsAddress, lelandsSignaturePath, consignorAddress, consignor.Email, string.Empty, DateTime.Now,
                                        items, consignmentContract.ContractText);
         consignmentContract.UpdateFields(consignmentContract.ConsignmentID,
                                          (int)Consts.ConsignmentContractStatus.Unsigned, consignmentContract.ContractText, fileName);
         AuctionRepository.UpdateConsignmentContract(consignmentContract);
         return
             (JSON(new JsonExecuteResult(JsonExecuteResultTypes.SUCCESS, "Contract was generated successfully.",
                                         new
         {
             StatusText = Consts.ConsignmentContractStatus.Unsigned.ToString(),
             DownloadLink =
                 Url.Action("GetConsignmentContract", "Auction", new { consignmentID = consignment.ID }),
             ShowLink =
                 Url.Action("ShowConsignmentContract", "Auction", new { consignmentID = consignment.ID })
         })));
     }
     catch (Exception exc)
     {
         return(JSON(new JsonExecuteResult(JsonExecuteResultTypes.ERROR, exc.Message)));
     }
 }