/// <summary> /// Saves all the parts of a Document from Portable document created online - ORG_TRX_Header, SYS_DOC_Header, SYS_DOC_Line, GLX_Header, GLX_Line and all movement and history /// </summary> /// <param name="connectionString">Connection string for the XPO datasource</param> /// <param name="userName">Username that created the document</param> /// <param name="jsonDoc">Portable document sent from web to be saved</param> /// <returns>Returns true if saved successfully.</returns> public static bool SaveDocument(string connectionString, string userName, string jsonDoc) { using (UnitOfWork uow = new UnitOfWork()) { uow.ConnectionString = connectionString; uow.Connect(); // Starts an explicit transaction. uow.ExplicitBeginTransaction(); try { PORT.Document document = (PORT.Document)JsonConvert.DeserializeObject(jsonDoc, typeof(PORT.Document)); document.documentheader.documentType = (byte)DAL.Enums.SYS_DOC_Type.SalesOrder; DAL.Datamodel.SEC_User sec_user = uow.Query <DAL.Datamodel.SEC_User>().Where(u => u.Username == userName).FirstOrDefault(); DAL.Datamodel.SYS_Tracking sysTracking = Core.SYS.SYS_TrackingProvider.New(uow, "WEB"); uow.CommitChanges(); DAL.Datamodel.ORG_TRX_Header orgHeader = Web.ORG.ORG_TRX_HeaderProvider.Create(uow, sec_user, sysTracking, document.documentheader.documentType, document); orgHeader.HeaderId = Web.SYS.SYS_DOC_HeaderProvider.New(uow, sec_user, sysTracking, document); uow.CommitChanges(); orgHeader.HeaderId.DocumentNumber = uow.Query <DAL.Datamodel.SYS_DOC_Header>().Where(d => d.TypeId == (DAL.Enums.SYS_DOC_Type)document.documentheader.documentType).Max(d => d.DocumentNumber) + 1; sysTracking.Initiator = String.Format("WEB - {0} {1}", "Sales Order", orgHeader.HeaderId.DocumentNumber.ToString()); uow.CommitChanges(); //CREATE TAX INVOICE FROM SALES ORDER ////////////////////////////////////////////////// document.documentheader.documentType = (byte)DAL.Enums.SYS_DOC_Type.TAXInvoice; DAL.Datamodel.ORG_TRX_Header orgHeaderInvoice = Web.ORG.ORG_TRX_HeaderProvider.Create(uow, sec_user, sysTracking, document.documentheader.documentType, document); orgHeaderInvoice.HeaderId = Web.SYS.SYS_DOC_HeaderProvider.New(uow, sec_user, sysTracking, document); uow.CommitChanges(); orgHeaderInvoice.HeaderId.DocumentNumber = uow.Query <DAL.Datamodel.SYS_DOC_Header>().Where(d => d.TypeId == (DAL.Enums.SYS_DOC_Type)document.documentheader.documentType).Max(d => d.DocumentNumber) + 1; uow.CommitChanges(); ////////////////////////////////////////////////// Core.ORG.ORG_CompanyProvider.UpdateCompanyHistory(uow, orgHeaderInvoice.CompanyId.Id, orgHeaderInvoice.HeaderId.SYS_DOC_Lines.Sum(l => l.Total)); DAL.Datamodel.GLX_Header glx_Header = Core.GLX.GLX_HeaderProvider.NewInvoice(uow, orgHeaderInvoice); Core.GLX.GLX_HeaderProvider.UpdateLedgerAccountBalance(uow, glx_Header); uow.CommitChanges(); uow.ExplicitCommitTransaction(); } catch { uow.ExplicitRollbackTransaction(); throw; } } return(true); }
public static PORT.Document GetById(string connectionString, long id) { using (UnitOfWork uow = new UnitOfWork()) { uow.ConnectionString = connectionString; uow.Connect(); XPQuery<DAL.Datamodel.ORG_TRX_Header> docs = new XPQuery<DAL.Datamodel.ORG_TRX_Header>(uow); PORT.Document document = docs.Where(d => d.Id == id) .Select(d => new PORT.Document() { id = d.HeaderId.Id, companyId = d.CompanyId.EntityId.EntityId.Id, companyName = d.CompanyId.EntityId.EntityId.Name, datePosted = d.DatePosted, referenceShort1 = d.ReferenceShort1, referenceShort2 = d.ReferenceShort2, referenceShort3 = d.ReferenceShort3, documentheader = new PORT.DocumentHeader() { documentNumber = d.HeaderId.DocumentNumber.ToString(), total = d.HeaderId.SYS_DOC_Lines.Sum(l => l.Total), totalVat = d.HeaderId.SYS_DOC_Lines.Sum(l => l.TotalTax), documentLines = d.HeaderId.SYS_DOC_Lines.Select(l => new PORT.DocumentLines() { amount = l.Amount, description = l.Description, itemId = l.ItemId.Id, lineOrder = l.LineOrder, quantity = l.Quantity, total = l.Total, totalVat = l.TotalTax, isNew = false }).ToList() } }).FirstOrDefault(); DAL.Datamodel.SYS_Address billing = uow.Query<DAL.Datamodel.ORG_CompanyAddress>().Where(a => a.CompanyId.EntityId.EntityId.Id == document.companyId && a.AddressId.TypeId == DAL.Enums.SYS_Type.BillingAddress).FirstOrDefault().AddressId; document.billingAddressLine1 = String.IsNullOrEmpty(billing.Line1) ? " " : billing.Line1; document.billingAddressLine2 = String.IsNullOrEmpty(billing.Line2) ? " " : billing.Line2; document.billingAddressLine3 = String.IsNullOrEmpty(billing.Line3) ? " " : billing.Line3; document.billingAddressLine4 = String.IsNullOrEmpty(billing.Line4) ? " " : billing.Line4; document.billingAddressCode = String.IsNullOrEmpty(billing.Code) ? " " : billing.Code; return document; } }
internal static DAL.Datamodel.SYS_DOC_Header New(UnitOfWork uow, DAL.Datamodel.SEC_User sec_user, DAL.Datamodel.SYS_Tracking sysTracking, PORT.Document document) { DAL.Datamodel.SYS_DOC_Header doc_header = new DAL.Datamodel.SYS_DOC_Header(uow) { TrackId = sysTracking , CreatedBy = sec_user.PersonId , TypeId = (DAL.Enums.SYS_DOC_Type)document.documentheader.documentType }; List <DAL.Datamodel.SYS_DOC_Line> lines = new List <DAL.Datamodel.SYS_DOC_Line>(); foreach (PORT.DocumentLines line in document.documentheader.documentLines) { lines.Add(new DAL.Datamodel.SYS_DOC_Line(uow) { Amount = line.amount, CreatedBy = sec_user.PersonId, Description = line.description, LineOrder = line.lineOrder, Quantity = line.quantity, Total = line.total - line.totalVat, TotalTax = line.totalVat, ItemId = uow.Query <DAL.Datamodel.SYS_Entity>().Where(i => i.Id == line.itemId).FirstOrDefault() }); } doc_header.SYS_DOC_Lines.AddRange(lines); return(doc_header); }
/// <summary> /// Creates a new ORG_TRX_Header from a Portable Document created online. /// </summary> /// <param name="uow">Unit of Work to create the Header on</param> /// <param name="createdBy">The user that created the Document</param> /// <param name="sysTracking">The tracking number for the Header</param> /// <param name="documentType">The Document type you want to create a header for</param> /// <param name="document">The Portable document to create the header from</param> /// <returns>A new ORG_TRX_Header created from a Portable document</returns> internal static DAL.Datamodel.ORG_TRX_Header Create(UnitOfWork uow, DAL.Datamodel.SEC_User createdBy, DAL.Datamodel.SYS_Tracking sysTracking, byte documentType, PORT.Document document) { DAL.Datamodel.ORG_TRX_Header org_trx_header = new DAL.Datamodel.ORG_TRX_Header(uow); org_trx_header.BillingAddressLine1 = document.billingAddressLine1; org_trx_header.BillingAddressLine2 = document.billingAddressLine2; org_trx_header.BillingAddressLine3 = document.billingAddressLine3; org_trx_header.BillingAddressLine4 = document.billingAddressLine4; org_trx_header.BillingAddressCode = document.billingAddressCode; org_trx_header.DatePosted = document.datePosted; org_trx_header.ReferenceShort1 = document.referenceShort1; org_trx_header.ReferenceShort2 = document.referenceShort2; org_trx_header.ReferenceShort3 = document.referenceShort3; org_trx_header.ShippingTypeId = DAL.Enums.ORG_TRX_ShippingType.Normal; org_trx_header.CompanyId = uow.Query<DAL.Datamodel.ORG_Company>().Where(c => c.EntityId.EntityId.Id == document.companyId).FirstOrDefault(); org_trx_header.CreatedBy = createdBy.PersonId; return org_trx_header; }