public static InvoiceModel mapto_invoice_model(KeyValuePair <string, List <MyXmlData> > xmlitem)
        {
            InvoiceModel modelinfo = null;

            modelinfo = new InvoiceModel();
            foreach (var item in xmlitem.Value)
            {
                switch (item.MyXmlNode)
                {
                case "OCR_Image_Name":
                    modelinfo.OCR_Image_Name = item.MyXmlVal;
                    break;

                case "OCR_PO_Number":
                    modelinfo.OCR_PO_Number = item.MyXmlVal;
                    break;

                case "OCR_Invoice_Date":
                    modelinfo.OCR_Invoice_Date = Convert.ToDateTime(item.MyXmlVal);
                    break;

                case "OCR_Invoice_Number":
                    modelinfo.OCR_Invoice_Number = item.MyXmlVal;
                    break;

                case "OCR_supplier_Name":
                    modelinfo.OCR_supplier_Name = item.MyXmlVal;
                    break;

                case "OCR_Supplier_ID":
                    modelinfo.OCR_supplier_ID = item.MyXmlVal;
                    break;

                case "OCR_Sender_Email_ID":
                    modelinfo.OCR_Sender_Email_ID = item.MyXmlVal;
                    break;

                case "OCR_Invoice_Amount":
                    modelinfo.OCR_Invoice_Amount = Convert.ToDecimal(item.MyXmlVal);
                    break;

                case "OCR_Currency":
                    modelinfo.OCR_Currency = item.MyXmlVal;
                    break;

                case "OCR_Remit_to_address":
                    modelinfo.OCR_Remit_to_address = item.MyXmlVal;
                    break;

                case "OCR_Bill_to":
                    modelinfo.OCR_Bill_to = item.MyXmlVal;
                    break;

                case "OCR_Payment_Term":
                    modelinfo.OCR_Payment_Term = item.MyXmlVal;
                    break;

                case "OCR_AccountNo":
                    modelinfo.OCR_AccountNo = item.MyXmlVal;
                    break;

                case "OCR_Net_Amount":
                    modelinfo.OCR_Net_Amount = item.MyXmlVal;
                    break;

                case "CR_Tax_Amount":
                    modelinfo.OCR_Tax_Amount = item.MyXmlVal;
                    break;

                case "OCR_Probable_POs":
                    modelinfo.OCR_Probable_POs = item.MyXmlVal;
                    break;


                case "OCR_Description":
                    modelinfo.OCR_Description = item.MyXmlVal;
                    break;



                default:
                    break;
                }
            }

            return(modelinfo);
        }
        public static Supplier_Invoice_DataType process_po_Invoice(InvoiceModel XMLinvoicemodel)
        {
            Supplier_Invoice_DataType            sisd   = new Supplier_Invoice_DataType();
            WorkDay_Resourse_ManagementEntities1 Entity = new WorkDay_Resourse_ManagementEntities1();
            // PO_Header_Invoice headerinfo = Entity.PO_Header_Invoice.Where(x => x.Supplier_ID == XMLinvoicemodel.OCR_supplier_ID).FirstOrDefault();
            //////if (headerinfo != null)
            // {
            List <Supplier_Invoice_Line_Replacement_DataType> ReplacementLineobject = new List <Supplier_Invoice_Line_Replacement_DataType>();

            List <PO_Details> po_details = Entity.PO_Details.Where(x => x.Supllier_ID.Equals(XMLinvoicemodel.OCR_supplier_ID)).ToList();

            if (po_details.Count > 0)
            {
                decimal total_po_amount = po_details.Sum(x => x.Po_Amount) ?? 0;

                if (XMLinvoicemodel.OCR_Invoice_Amount > total_po_amount)
                {
                    foreach (PO_Details item in po_details)
                    {
                        LineInvoiceModelDTO lineobjdataDTO = new LineInvoiceModelDTO();
                        lineobjdataDTO.Line_Extended_Amount   = Convert.ToDecimal(item.Po_Amount);
                        lineobjdataDTO.Line_Spend_Category_ID = null;
                        lineobjdataDTO.Line_Cost_Center       = null;
                        lineobjdataDTO.Line_PO_Number         = XMLinvoicemodel.OCR_PO_Number;
                        ReplacementLineobject = build_po_invoiceline(lineobjdataDTO, ReplacementLineobject);
                    }


                    PO_Details          newitem     = po_details[po_details.Count - 1];
                    decimal             amountleft  = XMLinvoicemodel.OCR_Invoice_Amount - total_po_amount;
                    LineInvoiceModelDTO lineobjdata = new LineInvoiceModelDTO();
                    lineobjdata.Line_Extended_Amount   = amountleft;
                    lineobjdata.Line_Spend_Category_ID = newitem.Spend_Catagory;
                    lineobjdata.Line_Cost_Center       = newitem.Cost_Centre;

                    ReplacementLineobject = build_po_invoiceline(lineobjdata, ReplacementLineobject);
                }
                else if (XMLinvoicemodel.OCR_Invoice_Amount < total_po_amount)
                {
                    PO_Details          newitem     = po_details.FirstOrDefault();
                    decimal             amountleft  = Decimal.Subtract(XMLinvoicemodel.OCR_Invoice_Amount, 0.01M);
                    LineInvoiceModelDTO lineobjdata = new LineInvoiceModelDTO();

                    lineobjdata.Line_Extended_Amount = amountleft;

                    lineobjdata.Line_PO_Number = XMLinvoicemodel.OCR_PO_Number;

                    ReplacementLineobject = build_po_invoiceline(lineobjdata, ReplacementLineobject);

                    PO_Details          newlistdata    = po_details.FirstOrDefault();
                    decimal             newamountleft  = 0.01M;
                    LineInvoiceModelDTO newlineobjdata = new LineInvoiceModelDTO();
                    newlineobjdata.Line_Extended_Amount   = newamountleft;
                    newlineobjdata.Line_Spend_Category_ID = newlistdata.Spend_Catagory;
                    lineobjdata.Line_Cost_Center          = newlistdata.Cost_Centre;


                    ReplacementLineobject = build_po_invoiceline(lineobjdata, ReplacementLineobject);
                }


                sisd = build_po_header(XMLinvoicemodel);
                sisd.Invoice_Line_Replacement_Data = ReplacementLineobject.ToArray();
            }
            else
            {
                List <Supplier_Invoice_Line_Replacement_DataType> Supplier_invoice_line_replacement_Line = new List <Supplier_Invoice_Line_Replacement_DataType>();
                var dta = Entity.Companies.ToList();

                Company Company = Entity.Companies.Where(x => x.Company_Name.Trim() == XMLinvoicemodel.OCR_Bill_to.Trim()).FirstOrDefault();

                if (Company != null)
                {
                    NonPO_Line_Details_Default item        = Entity.NonPO_Line_Details_Default.Where(x => x.Company_ID.Equals(Company.Company_ID)).FirstOrDefault();
                    LineInvoiceModelDTO        lineobjdata = new LineInvoiceModelDTO();
                    lineobjdata.Line_Extended_Amount   = XMLinvoicemodel.OCR_Invoice_Amount;
                    lineobjdata.Line_Spend_Category_ID = item.Spend_Category;
                    lineobjdata.Line_PO_Number         = XMLinvoicemodel.OCR_PO_Number;
                    lineobjdata.Line_Cost_Center       = item.Cost_Center;
                    sisd = build_po_header(XMLinvoicemodel);
                    sisd.Invoice_Line_Replacement_Data = build_NON_PO_invoiceline(Supplier_invoice_line_replacement_Line, lineobjdata).ToArray();
                }
                else
                {
                    Console.WriteLine("No record  company record found  found");
                }
            }
            return(sisd);
        }
        public static Supplier_Invoice_DataType build_po_header(InvoiceModel invoicemodel)
        {
            WorkDay_Resourse_ManagementEntities1 Entity = new WorkDay_Resourse_ManagementEntities1();

            Company Company = Entity.Companies.Where(x => x.Company_Name.Equals(invoicemodel.OCR_Bill_to)).FirstOrDefault();


            Supplier_Invoice_DataType headerinfo = new Supplier_Invoice_DataType();

            CompanyObjectIDType[] cid = new CompanyObjectIDType[]
            {
                new CompanyObjectIDType
                {
                    type = "Company_Reference_ID", Value = Company.Company_ID
                }
            };
            CurrencyObjectIDType[] currencyid = new CurrencyObjectIDType[1]
            {
                new CurrencyObjectIDType
                {
                    type = "Currency_ID", Value = invoicemodel.OCR_Currency
                }
            };
            Payment_TermsObjectIDType[] paymentTerm = new Payment_TermsObjectIDType[1]
            {
                new Payment_TermsObjectIDType
                {
                    type = "Payment_Terms_ID", Value = "NET30DAYS"
                }
            };



            headerinfo.Supplier_Reference = new SupplierObjectType()
            {
                ID = new SupplierObjectIDType[]
                {
                    new SupplierObjectIDType
                    {
                        type  = "Supplier_ID",
                        Value = invoicemodel.OCR_supplier_ID
                    }
                }
            };

            headerinfo.Approver_Reference = new WorkerObjectType()
            {
                ID = new WorkerObjectIDType[] { new WorkerObjectIDType()
                                                {
                                                    type = "Employee_ID", Value = "30896"
                                                } }
            };

            string memo = "";

            if (String.IsNullOrEmpty(invoicemodel.OCR_PO_Number))
            {
                memo = invoicemodel.OCR_Description;
            }
            else
            {
                memo = invoicemodel.OCR_Description;
            }



            string path = @"C:\File_Repo\PDF\EM_US_20190406100035053.pdf";



            //  string url = @"\\prodwapa1\RPA\Output\Processed Files\Image";
            //
            List <string> Filesname = new List <string>();

            string[] filePaths = Directory.GetFiles(@"C:\File_Repo\PDF");
            //  filePaths.Contains(x => x.Equals());
            foreach (string item in filePaths)
            {
                string filename = Path.GetFileName(item);

                Filesname.Add(filename);
            }
            // string file = Filesname.Where(X => X.Equals((@"C:\File_Repo\PDF\" + invoicemodel.OCR_Image_Name));
            // string  foundfile = Filesname.Where(x => x.Contains(invoicemodel.OCR_Image_Name)).FirstOrDefault();
            string fls = filePaths.Where(x => x.Contains(@"C:\File_Repo\PDF\" + invoicemodel.OCR_Image_Name)).FirstOrDefault();



            //


            //  headerinfo.Document_Link = url;
            if (!string.IsNullOrEmpty(fls))
            {
                Byte[] bytes = File.ReadAllBytes(fls);
                float  mb    = (bytes.Length / 1024f) / 1024f;

                headerinfo.Attachment_Data = new Financials_Attachment_DataType[]
                {
                    new Financials_Attachment_DataType {
                        File_Content        = bytes,
                        Filename            = Path.GetFileName(fls),
                        Comment             = "PuruTesting",
                        Content_Type        = "pdf",
                        Encoding            = "base64",
                        Compressed          = true,
                        CompressedSpecified = true
                    }
                };
                Console.WriteLine("Found  PDF in local folder -: " + fls);
            }
            else
            {
                headerinfo.Attachment_Data = null;
                Console.WriteLine("No matching file found in local folder with Name: " + invoicemodel.OCR_Image_Name);
            }


            headerinfo.Suppliers_Invoice_Number = invoicemodel.OCR_Invoice_Number;
            headerinfo.Submit            = true;
            headerinfo.Locked_in_Workday = true;
            headerinfo.Company_Reference = new CompanyObjectType()
            {
                ID = cid
            };
            headerinfo.Currency_Reference = new CurrencyObjectType()
            {
                ID = currencyid
            };
            headerinfo.Invoice_Date            = invoicemodel.OCR_Invoice_Date;
            headerinfo.Due_Date_Override       = DateTime.Now.AddDays(30);
            headerinfo.Payment_Terms_Reference = new Payment_TermsObjectType {
                ID = paymentTerm
            };

            headerinfo.Control_Amount_Total = invoicemodel.OCR_Invoice_Amount;
            headerinfo.Memo = memo;



            return(headerinfo);
        }
        public static Supplier_Invoice_DataType process_NON_PO_Invoice(InvoiceModel invoicemodel)
        {
            WorkDay_Resourse_ManagementEntities1 Entity = new WorkDay_Resourse_ManagementEntities1();

            Console.WriteLine("Seraching Data in NonPO_Line_Details Table for OCR_supplier_ID:  " + invoicemodel.OCR_supplier_ID);


            List <NonPO_Line_Details> non_po_details = Entity.NonPO_Line_Details.Where(x => x.Supplier_ID.Equals(invoicemodel.OCR_supplier_ID)).ToList();

            List <Supplier_Invoice_Line_Replacement_DataType> Supplier_invoice_line_replacement_Line = new List <Supplier_Invoice_Line_Replacement_DataType>();

            Console.WriteLine("Record Count in NonPO_Line_Details Table :" + non_po_details.Count);
            Supplier_Invoice_DataType sisd = new Supplier_Invoice_DataType();

            if (non_po_details.Count > 0)
            {
                Console.WriteLine("Creating Record with NonPO_Line_Details Table");

                Console.WriteLine("Creating " + non_po_details.Count + "  Lines for OCR_supplier_ID " + invoicemodel.OCR_supplier_ID);

                foreach (NonPO_Line_Details item in non_po_details)
                {
                    LineInvoiceModelDTO lineobjdata = new LineInvoiceModelDTO();
                    decimal             getper      = Decimal.Divide(Convert.ToDecimal(item.Percetage), 100);
                    lineobjdata.Line_Extended_Amount   = getper * invoicemodel.OCR_Invoice_Amount;
                    lineobjdata.Line_Spend_Category_ID = item.Spend_Catagories;
                    lineobjdata.Line_Cost_Center       = item.Cost_Center;
                    lineobjdata.Line_Item_Description  = item.Item_Desription;
                    lineobjdata.Line_Custom_Organization_Reference_ID = item.Custom_Org;
                    lineobjdata.Line_Employee_ID = item.Employee;
                    lineobjdata.Line_Project_ID  = item.Project;

                    Console.WriteLine("XML Invoice Amount :" + invoicemodel.OCR_Invoice_Amount);
                    Console.WriteLine("% from DB :" + item.Percetage);
                    Console.WriteLine("Calulated Amount :" + lineobjdata.Line_Extended_Amount);

                    Console.WriteLine("------------------------------");

                    Supplier_invoice_line_replacement_Line = build_NON_PO_invoiceline(Supplier_invoice_line_replacement_Line, lineobjdata);
                }
            }
            else
            {
                var dta = Entity.Companies.ToList();

                Company Company = Entity.Companies.Where(x => x.Company_Name.Trim() == invoicemodel.OCR_Bill_to.Trim()).FirstOrDefault();

                Console.WriteLine("Creating Record with NonPO_Line_Details_Defult Table");

                Console.WriteLine("Bill To  from XML :" + invoicemodel.OCR_Bill_to);
                if (Company != null)
                {
                    Console.WriteLine("Comapny ID from DB " + Company.Company_ID);

                    NonPO_Line_Details_Default item        = Entity.NonPO_Line_Details_Default.Where(x => x.Company_ID.Equals(Company.Company_ID)).FirstOrDefault();
                    LineInvoiceModelDTO        lineobjdata = new LineInvoiceModelDTO();

                    if (item != null)
                    {
                        lineobjdata.Line_Extended_Amount   = invoicemodel.OCR_Invoice_Amount;
                        lineobjdata.Line_Spend_Category_ID = item.Spend_Category;
                        lineobjdata.Line_Cost_Center       = item.Cost_Center;
                        lineobjdata.Line_Custom_Organization_Reference_ID = item.Custom_Org;

                        Supplier_invoice_line_replacement_Line = build_NON_PO_invoiceline(Supplier_invoice_line_replacement_Line, lineobjdata);
                    }
                    else
                    {
                        Console.WriteLine("No record found in NonPO_Line_Details_Default for Company_ID:" + Company.Company_ID);
                    }
                }
                else
                {
                    Console.WriteLine("No record  company record found  found");
                }
            }


            sisd = build_po_header(invoicemodel);
            sisd.Invoice_Line_Replacement_Data = Supplier_invoice_line_replacement_Line.ToArray();
            return(sisd);
        }