Sage50 US 2020 COM API (c#) - Как применить счет-фактуру к заказу на покупку - PullRequest
0 голосов
/ 07 мая 2020

Я создаю заказ на поставку в версии sage 50 US 2020 с кодом c# SDK. Мне также нужно было создать счет-фактуру, чтобы обновить количество товаров, но с SDK это было невозможно, поэтому я выбрал COM API. Я нашел код для создания XML и импорта для счета-фактуры, который работает нормально. Единственная проблема, с которой я столкнулся, заключается в том, что в xml нет поля для применения этого счета-фактуры к заказу на покупку. ЕСЛИ я сделаю это вручную, мне понравится изображение ниже

enter image description here

и вот моя функция c# для создания xml для покупки счет-фактура

public void createItemXML(string sPathName, orders mQueueResult, Sage.Peachtree.API.Company company, Vendor vendor)
    {
        if (mQueueResult.ToString() != "")
        {
            var jsonResult = mQueueResult;
            var jsonRequestString = JsonConvert.DeserializeObject<jsonRequestString>(jsonResult.jsonRequestString);
            var items = jsonRequestString.items;
            int totalLines = 0;

            DateTime oDate = Convert.ToDateTime(jsonRequestString.transactionDate);
            var Date = oDate.Month + "/" + oDate.Day + "/" +  oDate.Year;

            int totalAmount = 0;
            var purchaseOrderNum = jsonRequestString.purchaseOrderNumber;

            List<XElement> itemXml = new List<XElement>();
            if (items != null && items.Count > 0)
            {
                foreach (Item element in items)
                {
                    if (System.Math.Abs(element.Amount) > 0)
                    {
                        var itemName = element.Name;
                        var itemDescription = !string.IsNullOrEmpty(element.Description) ? element.Description : element.Name;
                        var accountName = element.AccountName;
                        var itemAmount = element.Amount;
                        totalAmount = totalAmount + Convert.ToInt16(itemAmount);
                        var itemQuantity = element.Quantity;
                        var unitPrice = itemAmount / itemQuantity;
                        var itemID = "";
                        var itemGLAccount = "";
                        totalLines = totalLines + 1;
                        InventoryItem inventory = WinFormUtils.GetInventoryItemByID(itemName, company);
                        if (inventory != null)
                        {
                            itemID = inventory.ID;
                            dynamic entityRef = Sales.getEntityReference(inventory, company);
                            Account acct = company.Factories.AccountFactory.Load(entityRef.SalesAccountReference);
                            itemGLAccount = acct.ID;
                        }
                        else
                        {
                            myService.WriteToLogFile("Item #" + itemName + " not found.");
                            return;
                        }

                        itemXml.Add (new XElement("PurchaseLine",
                                new XElement("Quantity", itemQuantity),

                                new XElement("Item_ID", new XAttribute(XNamespace.Xmlns + "type", "paw:ID"), itemID),
                                new XElement("GL_Account", new XAttribute(XNamespace.Xmlns + "type", "paw:ID"), itemGLAccount),

                                new XElement("Description", itemDescription),
                                new XElement("Unit_Price", unitPrice),
                                new XElement("Amount", itemAmount)
                            ));




                    }
                }
            }
            else
            {
                myService.WriteToLogFile("No items found in requestdata.");
            }



            var xmlNode =
                new XElement("PAW_Purchases",
                    new XAttribute(XNamespace.Xmlns + "paw", "urn:schemas-peachtree-com/paw8.02-datatypes"),
                    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2000/10/XMLSchema-instance"),
                    new XAttribute(XNamespace.Xmlns + "xsd", "http://www.w3.org/2000/10/XMLSchema-datatypes"),
                    new XElement("PAW_Purchase",
                        new XAttribute(XNamespace.Xmlns + "type", "paw:Invoice"),
                        new XElement("VendorID",
                            new XAttribute(XNamespace.Xmlns + "type", "paw:ID"), vendor.ID),
                        new XElement("VendorName", vendor.Name),
                        new XElement("Date",
                            new XAttribute(XNamespace.Xmlns + "type", "paw:date"), Date),
                        new XElement("Invoice_Number", purchaseOrderNum),
                        new XElement("Line1", vendor.MailToContact.Address.Address1),
                        new XElement("Line2", vendor.MailToContact.Address.Address2),
                        new XElement("City", vendor.MailToContact.Address.City),
                        new XElement("State"),
                        new XElement("Zip", vendor.MailToContact.Address.Zip),
                        new XElement("AP_Account",
                            new XAttribute(XNamespace.Xmlns + "type", "paw:ID"), "2000"),
                        new XElement("AP_Amount", totalAmount),
                        new XElement("Number_of_Distributions", totalLines),
                        new XElement("Applied_To_Purchase_Order", purchaseOrderNum),
                        new XElement("PurchaseLines", itemXml)
                    )
                );
                xmlNode.Save(sPathName);
        } else
        {
            myService.WriteToLogFile("There is no data in mQueueResult you have passed to create xml");
        }

    }

Он генерирует следующее xml

<?xml version="1.0" encoding="utf-8"?>
<PAW_Purchases xmlns:paw="urn:schemas-peachtree-com/paw8.02-datatypes" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2000/10/XMLSchema-datatypes">
  <PAW_Purchase xmlns:type="paw:Invoice">
    <VendorID xmlns:type="paw:ID">HarishKumar</VendorID>
    <VendorName>HarishKumar</VendorName>
    <Date xmlns:type="paw:date">4/11/2020</Date>
    <Invoice_Number>067</Invoice_Number>
    <Line1>harish Inc</Line1>
    <Line2>street1</Line2>
    <City>Ludhiana</City>
    <State />
    <Zip>141015</Zip>
    <AP_Account xmlns:type="paw:ID">2000</AP_Account>
    <AP_Amount>25</AP_Amount>
    <Number_of_Distributions>2</Number_of_Distributions>
    <Applied_To_Purchase_Order>067</Applied_To_Purchase_Order>
    <PurchaseLines>
      <PurchaseLine>
        <Quantity>5</Quantity>
        <Item_ID xmlns:type="paw:ID">item1</Item_ID>
        <GL_Account xmlns:type="paw:ID">2340</GL_Account>
        <Description>item desc</Description>
        <Unit_Price>3</Unit_Price>
        <Amount>15</Amount>
      </PurchaseLine>
      <PurchaseLine>
        <Quantity>2</Quantity>
        <Item_ID xmlns:type="paw:ID">item2</Item_ID>
        <GL_Account xmlns:type="paw:ID">4050</GL_Account>
        <Description>item 2 desc</Description>
        <Unit_Price>5</Unit_Price>
        <Amount>10</Amount>
      </PurchaseLine>
    </PurchaseLines>
  </PAW_Purchase>
</PAW_Purchases>

** и вот моя функция импорта **

private void Importfile(string sPathName)
    {
        ptApp = (Interop.PeachwServer.Application)ptLogin.GetApplication(myService.COMApiUserName, myService.COMApiPassword);
        importer = (Interop.PeachwServer.Import)ptApp.CreateImporter(Interop.PeachwServer.PeachwIEObj.peachwIEObjPurchaseJournal);
        importer.ClearImportFieldList();
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_VendorId);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_VendorName);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_Date);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_InvoiceNumber);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_ShipToAddressLine1);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_ShipToAddressLine2);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_ShipToCity);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_ShipToState);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_ShipToZip);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_APAccountId);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_APAmount);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_NumberOfDistributions);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_AppliedToPurchaseOrder);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_Quantity);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_ItemId);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_Description);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_GLAccountId);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_UnitPrice);
        importer.AddToImportFieldList((short)PeachwIEObjPurchaseJournalField.peachwIEObjPurchaseJournalField_Amount);
        importer.SetFilename(sPathName);
        importer.SetFileType(PeachwIEFileType.peachwIEFileTypeXML);
        try
        {
            importer.Import();
            myService.WriteToLogFile("Purchase invoice created successfully");
            File.Delete(sPathName);
        }
        catch (System.Exception e)
        {
            myService.WriteToLogFile("Exception in import Purchase invoice xml" + e.Message);
        }
    }

Мне что-то не хватает, чтобы применить это к заказу покупки?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...