Example #1
0
        public ValueDataResponse <string> InsertPurchaseOrder(UpsertPurchaseOrder purchages)
        {
            ValueDataResponse <string> response = new ValueDataResponse <string>();

            byte[] byteArray       = null;
            var    FileRepoBaseUrl = _config.Value.FileRepositoryUrl;

            try
            {
                var purchaseExists = _appContext.PurchageOrders.Where(x => x.PurchaseReference == purchages.PurchaseReference).FirstOrDefault();
                if (purchaseExists == null)
                {
                    PurchageOrder pro    = _mapper.Map <PurchageOrder>(purchages);
                    var           result = _appContext.PurchageOrders.Add(pro);
                    _appContext.SaveChanges();
                    foreach (var it in purchages.PurchaseItems)
                    {
                        _appContext.PurchageItemXrefs.Add(new PurchageItemXref
                        {
                            ItemId      = it.ItemId,
                            PurchageId  = pro.Id,
                            Quantity    = it.Quantity,
                            ExpectdCost = it.ExpectdCost,
                            Comments    = it.Comments
                        });
                    }
                    _appContext.SaveChanges();
                    var supplier = _appContext.Suppliers.Where(x => x.Id == pro.SupplierId).FirstOrDefault();
                    var project  = _appContext.Projects.Where(x => x.Id == pro.ProjectId).FirstOrDefault();
                    var store    = _appContext.LookUps.Where(x => x.Id == pro.StoreId).FirstOrDefault();


                    var items = (from pi in _appContext.PurchageItemXrefs
                                 join i in _appContext.Items on pi.ItemId equals i.Id
                                 join p in _appContext.PurchageOrders on pi.PurchageId equals p.Id
                                 select new GetItemsResponse
                    {
                        Id = pi.Id,
                        PurchaseId = pi.PurchageId,
                        PurchaseReference = p.PurchaseReference,
                        ItemReference = i.ItemReference,
                        ItemId = pi.ItemId,
                        ItemName = i.Name1,
                        Quantity = pi.Quantity,
                        Comments = pi.Comments,
                        ExpectedCost = pi.ExpectdCost,
                    }).Where(x => x.PurchaseId == pro.Id).ToList();
                    DateTime Date = pro.ArrivingDate;
                    // string message = EmailTemplates.GetPurchaseOrder(pro.PurchaseReference, pro.ArrivingDate, project.Name1, supplier.Name1, supplier.Address, store.Name1, pro.BillingAddress, pro.ShippingAddress, items, null);

                    // string message = EmailTemplates.GetPurchaseOrder(pro.PurchaseReference, pro.ArrivingDate, project.Name1, supplier.Name1, supplier.Address, store.Name1, pro.BillingAddress, pro.ShippingAddress, items.ItemName, items.Quantity, items.ExpectedCost, items.Comments, null);
                    byteArray = GeneratePurchaseOrderPdf(pro, supplier, items, project, store);
                    if (byteArray != null)
                    {
                        string ModuleName = "PurchaseOrder";
                        var    now        = DateTime.Now;
                        var    yearName   = now.ToString("yyyy");
                        var    monthName  = now.Month.ToString("d2");
                        var    dayName    = now.ToString("dd");

                        FileUploadService repo = new FileUploadService();

                        string FolderLocation = "FileRepository";
                        string ServerRootPath = _config.Value.ServerRootPath;

                        string Location = ServerRootPath + @"\" + FolderLocation + @"\" + yearName + @"\" + monthName + @"\" + dayName + @"\" + ModuleName;

                        pro.FileName = repo.UploadFile(byteArray, pro.FileExtention, Location);

                        pro.FileLocation = Path.Combine(yearName, monthName, dayName, ModuleName);

                        _appContext.SaveChanges();

                        //var converter = new HtmlConverter();
                        //var html = message;

                        //Byte[] bytes = converter.FromHtmlString(html);

                        //string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

                        //string ImageUrl = "data:image/png;base64," + base64String;

                        //byte[] FileBytes = Convert.FromBase64String(base64String);

                        //pro.FileName = repo.UploadFile(FileBytes, pro.FileExtention, Location);

                        //pro.FileLocation = Path.Combine(yearName, monthName, dayName, ModuleName);

                        //_appContext.SaveChanges();
                    }


                    if (pro != null)
                    {
                        response.Result          = string.Format("{0}/{1}/{2}{3}", FileRepoBaseUrl, pro.FileLocation, pro.FileName, pro.FileExtention);
                        response.IsSuccess       = true;
                        response.AffectedRecords = 1;
                        response.EndUserMessage  = "Purchage Order Added Successfully";
                    }
                    else
                    {
                        response.IsSuccess       = true;
                        response.AffectedRecords = 0;
                        response.EndUserMessage  = "Purchage Order Added Failed";
                    }
                }
                else
                {
                    response.IsSuccess       = false;
                    response.AffectedRecords = 0;
                    response.EndUserMessage  = "Purchage Order Reference Already Exists";
                }
            }
            catch (Exception ex)
            {
                response.IsSuccess       = false;
                response.AffectedRecords = 0;
                response.EndUserMessage  = ex.InnerException == null ? ex.Message : ex.InnerException.Message;
                response.Exception       = ex;
            }
            return(response);
        }
 public ValueDataResponse <string> Update(UpsertPurchaseOrder purchases)
 {
     return(_unitOfWork.Purchages.UpdatePurchaseOrder(purchases));
 }