Code to insert data in Purchase Invoices

amadman114amadman114 Member Posts: 36
Hi,
I'm getting code set up to import data from an external source into the purchase invoices. The process needs to create a new PI, and check the information in it.
This is what I have so far: (I don't think it's very complex)

Magic button - OnPush()
CreateNewInvoice('CE5036','77012345',TODAY,'PO0030612','418399-001',2,119.98)


CreateNewInvoice(VendorCode : Code[20];InvoiceNo : Code[20];InvoiceDate : Date;"PO No." : Text[30];PN : Code[20];QTY : Integer;"Unit Pr
//Insert top part
GrecPurchaseHeader.INIT;
GrecPurchaseHeader.FINDLAST;
"No." := GrecPurchaseHeader."No.";

//GrecVendor.SETRANGE("No.",VendorCode);
GrecVendor.SETRANGE("No.",'CE5036');
"Buy-from Vendor No." := GrecVendor."No.";
"Buy-from Vendor Name" := GrecVendor.Name;
"Buy-from Address" := GrecVendor.Address;
"Buy-from Address 2" := GrecVendor."Address 2";
"Buy-from City" := GrecVendor.City;
"Buy-from County" := GrecVendor.County;
"Buy-from Post Code" := GrecVendor."Post Code";

"Vendor Invoice No." := InvoiceNo;
"Posting Date" := InvoiceDate;


//Select single Purchase header
GrecPurchaseHeader.SETRANGE("No.","PO No.");

//Select single Purchase receipt line
GrecPurchRcptLine.SETFILTER("PO No.","PO No.");
GrecPurchRcptLine.SETFILTER("No.","PN");

//Init process
IF GrecPurchRcptLine.FIND('-') THEN
  BEGIN
  LcduGetReceipt.SetPurchHeader(GrecPurchaseHeader);
  LcduGetReceipt.CreateInvLines(GrecPurchRcptLine);
  END;

//Select new lines
GrecPurchaseLine.RESET;
GrecPurchaseLine.SETFILTER("Document No.","PO No.");
GrecPurchaseLine.SETFILTER("No.",PN);

//Check quantity
IF NOT (GrecPurchaseLine."Qty. to Invoice" >= QTY) THEN BEGIN
   MESSAGE('Not expecting that many! (Qty is too high)');
   EXIT;
END;

//Check price
//If it's expensive then it will do it by +-1%. Otherwise it will do it +-£3

IF "Unit Price" > 200 THEN BEGIN
  LdecCheckAmount := "Unit Price" / 100;
  IF NOT (GrecPurchaseLine."Unit Cost (LCY)" < "Unit Price" + LdecCheckAmount) OR 
  (GrecPurchaseLine."Unit Cost (LCY)" > "Unit Price" - LdecCheckAmount) THEN
    MESSAGE('Error - Total price incorrect1');

END ELSE BEGIN;
  IF NOT (GrecPurchaseLine."Unit Cost (LCY)" < "Unit Price" + 3) OR
  (GrecPurchaseLine."Unit Cost (LCY)" > "Unit Price" - 3) THEN
    MESSAGE('Error - Total price incorrect2');

END;

GrecPurchaseLine.Quantity := QTY;
GrecPurchaseLine."Unit Cost (LCY)" := "Unit Price";
GrecPurchaseLine.MODIFY;

MESSAGE('Ready to post');

The issue is, that when I try and run the function using my magic button, I get the MESSAGE('Not expecting that many! (Qty is too high)') error. When checking the Purchase Lines manually, I can see that we still have 6 to invoice. So, clearly I've missed something fairly major, any ideas?
Sign In or Register to comment.