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_batchDocuments(batchDocument entity)
		{
			this.SendPropertyChanging();
			entity.document = null;
		}
		private void attach_batchDocuments(batchDocument entity)
		{
			this.SendPropertyChanging();
			entity.document = this;
		}
 partial void DeletebatchDocument(batchDocument instance);
 partial void UpdatebatchDocument(batchDocument instance);
 partial void InsertbatchDocument(batchDocument instance);