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 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);
        }