private static void GetBatch(ref List<batch> batches, Int32 lineNum, XElement deliveryLineElement, Decimal quantity, Int32 itemId)
        {
            String batchNumber = String.Empty;
            String heatNumber = String.Empty;
            String certificateIndexNr = String.Empty;

            var batchNumberElement = deliveryLineElement.Elements("E1EDL43").Where(c => c.Element("QUALF").Value.Equals("X")).FirstOrDefault();
            if (batchNumberElement != null)
                batchNumber = batchNumberElement.Element("BELNR").Value;

            if (!String.IsNullOrEmpty(batchNumber))
            {
                var heatNumberElement = deliveryLineElement.Elements("E1EDL43").Where(c => c.Element("QUALF").Value.Equals("Y")).FirstOrDefault();
                if (heatNumberElement != null)
                {
                    var heatNumberElement2 = heatNumberElement.Element("BELNR");
                    if(heatNumberElement2 != null)
                        heatNumber = heatNumberElement2.Value;
                }

                var certificateIndexElement = deliveryLineElement.Elements("E1EDL43").Where(c => c.Element("QUALF").Value.Equals("Z")).FirstOrDefault();
                if (certificateIndexElement != null)
                {
                    var certificateIndexElement2 = certificateIndexElement.Element("BELNR");
                    if(certificateIndexElement2 != null)
                        certificateIndexNr = certificateIndexElement2.Value;
                }

                String certificateDocID = String.Empty;
                if (deliveryLineElement.Element("MFRPN") != null)
                    certificateDocID = deliveryLineElement.Element("MFRPN").Value;

                batch batch = new batch();
                batch.itemId = itemId;
                batch.batchNumber = batchNumber;
                batch.heatNumber = heatNumber;
                batch.ixosArchiveId = certificateDocID;
                batch.certificateIndexNumber = certificateIndexNr;

                batch.batchDocuments.Add(new batchDocument() { baseLineNum = lineNum, quantity = quantity });
                batches.Add(batch);
            }
        }
        private static Boolean ProcessBatches(delivery deliveryDocument, List<batch> batches)
        {
            DALPortalDataContext dc = new DALPortalDataContext();
            Boolean returnValue = true;

            // Remove all batches attached to this document
            BatchClass.RemoveBatchesDocument(deliveryDocument.docId, deliveryDocument.docType, dc);

            foreach (batch batchLine in batches)
            {
                try
                {
                    batch batch = BatchClass.GetBatch(batchLine.batchNumber, batchLine.heatNumber, batchLine.itemId, deliveryDocument.companyCode, dc);

                    Boolean newBatch = false;
                    if (batch == null)
                    {
                        batch = new batch();
                        newBatch = true;
                    }

                    if (newBatch)
                    {
                        batch.batchNumber = batchLine.batchNumber;
                        batch.companyCode = deliveryDocument.companyCode;
                    }

                    batch.itemId = batchLine.itemId;
                    batch.certificateIndexNumber = batchLine.certificateIndexNumber;
                    batch.heatNumber = batchLine.heatNumber;
                    batch.ixosArchiveId = batchLine.ixosArchiveId;

                    if (newBatch)
                    {
                        // Check if certificate is already on the server
                        String fileName = "CERT_" + batchLine.ixosArchiveId + ".pdf";
                        String filePath = Path.Combine(Parameters_DataProcessor.CompanyFilesRoot, "Certificates", fileName);
                        String dbPath = Path.Combine(@"~\Files\Certificates", fileName);

                        if (File.Exists(filePath))
                            batch.certificateLink = dbPath;
                    }

                    foreach (batchDocument batchDocLine in batchLine.batchDocuments)
                    {
                        // Check if Batch relates to document
                        batchDocument batchDoc = batch.batchDocuments.Where(c => c.baseDocId.Equals(deliveryDocument.docId) && c.baseLineNum.Equals(batchDocLine.baseLineNum) && c.baseDocType.Equals("DL")).FirstOrDefault();
                        Boolean newBatchDoc = false;
                        if (batchDoc == null)
                        {
                            newBatchDoc = true;
                            batchDoc = new batchDocument();
                            batchDoc.baseDocId = deliveryDocument.docId;
                            batchDoc.baseLineNum = batchDocLine.baseLineNum;
                            batchDoc.baseDocType = "DL";
                        }

                        batchDoc.quantity = batchDocLine.quantity;

                        if (newBatchDoc)
                            batch.batchDocuments.Add(batchDoc);
                    }

                    if (newBatch)
                        dc.batches.InsertOnSubmit(batch);

                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(String.Format("Error Processing Batches for Delivery {0} for company {1}. Error: {2}", deliveryDocument.documentNumber, deliveryDocument.companyCode, ex.Message), "ProcessBatches");
                    returnValue = false;
                }
            }

            return returnValue;
        }
		private void detach_batches(batch entity)
		{
			this.SendPropertyChanging();
			entity.company = null;
		}
		private void attach_batches(batch entity)
		{
			this.SendPropertyChanging();
			entity.company = this;
		}
 partial void Deletebatch(batch instance);
 partial void Updatebatch(batch instance);
 partial void Insertbatch(batch instance);