public bool CrearDocumento(List <Ticket> pLstTicket, SAPbobsCOM.BoObjectTypes pObjType, string pStrTableBase, int pIntBaseType, string pStrTableDetail)
        {
            bool lBolIsSuccess = false;

            try
            {
                string lStrDocEntry   = string.Empty;
                string lStrCostCenter = GetCostCenter();
                List <TicketDetail>  lLstTicketDetail = new List <TicketDetail>();
                SAPbobsCOM.Documents lObjDocument     = (SAPbobsCOM.Documents)DIApplication.Company.GetBusinessObject(pObjType); //SAPbobsCOM.BoObjectTypes.oInvoices);
                foreach (Ticket lObjTicket in pLstTicket)
                {
                    if (pStrTableBase == "ORDR")
                    {
                        lObjDocument.DocObjectCodeEx = "13";
                    }

                    lStrDocEntry          = mObjQueryManager.GetValue("DocEntry", "DocNum", lObjTicket.Number.ToString(), pStrTableBase);
                    lObjDocument.CardCode = lObjTicket.BPCode;

                    if (lObjTicket.CapType == 0)
                    {
                        lObjDocument.DocObjectCode = BoObjectTypes.oInvoices;
                    }
                    lLstTicketDetail = mObjTicketDAO.GetListTicketDetail(lObjTicket.Folio) as List <TicketDetail>;
                    if (pStrTableBase == "OINV")
                    {
                        // lLstTicketDetail = AdjustmentTicket(pLstTicket, true, false);
                    }
                    if (pStrTableBase == "OPCH")
                    {
                        //lLstTicketDetail = AdjustmentTicket(pLstTicket, false, false);
                    }

                    for (int i = 0; i < lLstTicketDetail.Count; i++)
                    {
                        if (lObjTicket.Number != 0 && VerifyDocItem(lStrDocEntry, lLstTicketDetail[i].Item, pStrTableDetail))
                        {
                            lObjDocument.Lines.BaseEntry = int.Parse(lStrDocEntry);
                            lObjDocument.Lines.BaseLine  = lLstTicketDetail[i].BaseLine;
                            lObjDocument.Lines.BaseType  = pIntBaseType;
                        }
                        // lObjDocument.Lines.AccountCode = "2180010000000";
                        if (lLstTicketDetail[i].netWeight < 0)
                        {
                            lLstTicketDetail[i].netWeight *= -1;
                        }

                        lObjDocument.Lines.ItemCode = lLstTicketDetail[i].Item;

                        lObjDocument.Lines.UnitsOfMeasurment = 0;
                        lObjDocument.Lines.UnitPrice         = lLstTicketDetail[i].Price;
                        lObjDocument.Lines.COGSCostingCode   = lStrCostCenter;

                        if (lObjTicket.CapType == 4)
                        {
                            lObjDocument.Lines.Quantity          = 1;
                            lObjDocument.Lines.UnitsOfMeasurment = 1;
                            string ss = lObjDocument.Lines.UoMCode;
                        }
                        else
                        {
                            lObjDocument.Lines.Quantity = lLstTicketDetail[i].netWeight;
                        }
                        lObjDocument.Lines.WarehouseCode = lLstTicketDetail[i].WhsCode;
                        // lObjDocument.Lines.ProjectCode = lObjTicket.Project;
                        lObjDocument.Lines.UserFields.Fields.Item("U_GLO_BagsBales").Value = lLstTicketDetail[i].BagsBales;
                        lObjDocument.Lines.UserFields.Fields.Item("U_PL_Ticket").Value     = lLstTicketDetail[i].Folio;
                        lObjDocument.Lines.Add();
                    }
                }
                if (lObjDocument.Add() != 0)
                {
                    UIApplication.ShowMessageBox(string.Format("Exception: {0}", DIApplication.Company.GetLastErrorDescription()));
                    LogService.WriteError("[ERROR]" + DIApplication.Company.GetLastErrorDescription());
                }
                else
                {
                    lBolIsSuccess = true;
                    LogService.WriteSuccess("[CrearDocumento] DocNum:" + lObjDocument.DocNum);
                    MemoryUtility.ReleaseComObject(lObjDocument);
                    UIApplication.ShowMessageBox(string.Format("Documento realizado correctamente"));
                }
            }
            catch (Exception ex)
            {
                UIApplication.ShowMessageBox(string.Format("Exception: {0}", ex.Message));
                LogService.WriteError("[CrearDocumento]" + ex.Message);
                LogService.WriteError(ex);
            }
            return(lBolIsSuccess);
        }
Beispiel #2
0
        static string gcrudBO <T>(SAPbobsCOM.BoObjectTypes bobsType,
                                  DIConnection.IConnRef t, string name, ref string id, string bstrXML, bool delete, bool put, bool post, bool schemaRequired, ref string xmlSchema, ref bool found)
        {
            T    bo      = t.company.GetBusinessObject(bobsType);
            bool deleted = false;

            if (schemaRequired)
            {
                xmlSchema = t.company.GetBusinessObjectXmlSchema(bobsType);
            }
            if (!string.IsNullOrEmpty(id))               // Find BO by ID
            {
                found = ((dynamic)bo).GetByKey(int.Parse(id));
            }
            if (post)               //Add new BO // Company must be set to XMLasString = true, otherwise this will not work
            {
                ((dynamic)bo).Browser.ReadXml(bstrXML, 0);
                int status = ((dynamic)bo).Add();
                if (status != 0)
                {
                    int    errorCode = t.company.GetLastErrorCode();
                    string errorText = t.company.GetLastErrorDescription();
                    throw new Exception($"Add status is {status} error code {errorCode} {errorText}");
                }
                else
                {
                    //Unfortunately, after addition the bo is not reloaded, to find the newly created value is as follows
                    string boKey  = t.company.GetNewObjectKey();
                    string boType = t.company.GetNewObjectType();
                    //This checking is fine, but what can we do, if it is not 202?
                    //if (boType == "202") throw new Exception("The returned object type is not 202 for a Production Order");
                    found = ((dynamic)bo).GetByKey(int.Parse(boKey));
                    id    = boKey;                  //With this scenario we know the key.
                }
            }
            else if (delete)
            {
                if (found)
                {
                    int status = ((dynamic)bo).Cancel();                     //No Delete operation defined for Production Order
                    if (status != 0)
                    {
                        int    errorCode = t.company.GetLastErrorCode();
                        string errorText = t.company.GetLastErrorDescription();
                        throw new Exception($"Cancel status is {status} error code {errorCode} {errorText}");
                    }
                    else
                    {
                        //This is not possible with a template typed variable
                        //bo = null;
                        deleted = true;
                    }
                }
            }
            else if (put)
            {
                if (found)
                {
                    ((dynamic)bo).Browser.ReadXml(bstrXML, 0);
                    int status = ((dynamic)bo).Update();
                    if (status != 0)
                    {
                        int    errorCode = t.company.GetLastErrorCode();
                        string errorText = t.company.GetLastErrorDescription();
                        throw new Exception($"Update status is {status} error code {errorCode} {errorText}");
                    }
                    else
                    {
                        //It's be better to reload the data, since the update in a header may have had rippling effects
                        found = ((dynamic)bo).GetByKey(int.Parse(id));
                    }
                }
            }
            return((found && !deleted) ? ((dynamic)bo)?.GetAsXML() : null);
        }