CRM0115 Create Purchase Orders

Basic Information

Module CRM.Sales
Code CRM0115
Parent Document SalesOrder
Sub-document PurchaseOrder
Full Name Create Purchase Orders (for sale transactions between enterprise companies)
Status ACTIVE
Deterministic/Non-deterministic Deterministic
Transitional Documents No
Replace Generation Procedures GeneratePurchaseOrder
Processing Orphan Rows Ignore
Introduced In Version 2019.1
Date of Suspension

Business Logic

The generation procedure is designed for purchase and sale between the enterprise companies in the current data base. The Sales Order in the enterprise company selling the products generates purchasing document - Purchase Order - in the buying enterprise company, so the buying enterprise company would have the starting document so they can proceed the receipt of goods.

In order to provide all the data needed for correct Purchase Order there are few verifications processed:

  1. validate the customer in the Sales Order. The validation checks if the party of the customer (SalesOrder.Customer.Party) is equal to the party of the Destination Enterprise Company (PurchaseOrder.EnterpriseCompany.Party) (in the document flow specifies the enterprise company of the new sub-document). If both parties are one and the same the generation procedure continues, if not - the generation procedure is stopped. No error message is thrown, no new document created;
  2. if the validation from p.1 is processed succesfully, the next step is to find the correct supplier. So, supplier must be found with the following parameters:
    1. Supplier.Party == SalesOrder.EnterpriseCompany.Party;
    2. Supplier.EnterpriseCompany == PurchaseOrder.EnterpriseCompany;
    3. Supplier's validity period includes the document date (PurchaseOrder.DocumentDate). That would be:
      1. IsActive == true;
      2. FromDate <= PurchaseOrder.DocumentDate || FromDate == null
      3. ThruDate >= PurchaseOrder.DocumentDate || ThruDate == nul

If such supplier is found, this is the Supplier value. Otherwise the generation procedure breaks - error message is thrown, no new document created. The error message is:

The company '{SalesOrder.EnterpriseCompany.Party.PartyName}' is not defined as a supplier for enterprise company '{PurchaseOrder.EnterpriseCompany.Party.PartyName}'.

The Purchase Order document header is created as the Sales Order document header as follows:
PurchaseOrder.Supplier = **Supplier**

PurchaseOrder.DocumentCurrency = SalesOrder.DocumentCurrency

PurchaseOrder.PaymentDueDate = SalesOrder.PaymentDueDate

PurchaseOrder.PlannedDeliveryDate = SalesOrder.RequiredDeliveryDate

PurchaseOrder.PlannedReleaseDate = PurchaseOrder.DocumentDate

PurchaseOrder.PaymentAccount = Supplier.DefaultPaymentAccount
PurchaseOrder.PurchasePriceList = null

The attributes in the Documents table in the PurchaseOrder header are filled in as follows:

PurchaseOrder.Notes = null
PurchaseOrder.EnterpriseCompany = the enterprise company from the document route which party is equal to the party of the SalesOrder.Customer
PurchaseOrder.EnterpriseCompanyLocation = the enterprise company location from the document route
PurchaseOrder.CurrencyDirectory =
//CurrencyDirectories(CurrencyDirectory.DocumentDate == PurchaseOrder.DocumentDate; CurrencyDirectory.IsDefault == true; CurrencyDirectory.EnterpriseCompany == PurchaseOrder.EnterpriseCompany; CurrencyDirectory.IsValidField == true; CurrencyDirectory.Void == false; CurrencyDirectory.State >= Released) = list
     if (list.Count() > 0)
        than FIRST(list)
     else null
PurchaseOrder.DocumentDate = SalesOrder.DocumentDate
PurchaseOrder.FromCompanyDivision = null
PurchaseOrder.ToCompanyDivision = null
PurchaseOrder.FromParty = PurchaseOrder.EnterpriseCompany.Party
PurchaseOrder.ToParty = PurchaseOrder.Supplier.Party
PurchaseOrder.ParentDocument = SalesOrder
PurchaseOrder.PrimeCauseDocument = null
PurchaseOrder.PaymentAccount = PurchaseOrder.Supplier.DefaultPaymentAccount
PurchaseOrder.ResponsiblePerson = null

The rest of the document header attributes which are not listed here are filled in as described in Standard Document Attributes.

Note

The new purchase order is saved only if it has at least one row.

Fulfillments

Name SalesOrderLineToPurchaseOrderLine
Parent Entity SalesOrderLine
Child Entity PurchaseOrderLine
Parent / Child Relationship PurchaseOrderLine.ParentDocument = SalesOrder;PurchaseOrderLine.ParentLineNo =SalesOrder.LineNo
Fulfillment Name Metric Name Measurement Unit Parent Value Child Value
SalesOrderLineToPurchaseOrderLine Quantity QuantityUnit SalesOrderLine.Quantity PurchaseOrderLine.Quantity
QuantityBase Product.BaseMeasurementUnit SalesOrderLine.QuantityBase PurchaseOrderLine.QuantityBase

The SalesOrderLineToPurchaseOrderLine fulfilment creates PurchaseOrderLines as follows:

PurchaseOrderLine.LineNo = SalesOrderLine.LineNo

PurchaseOrderLine.Product = SalesOrderLine.Product

PurchaseOrderLine.ProductDescription = SalesOrderLine.ProductDescription

PurchaseOrderLine.Quantity = REMAINING(Quantity)

PurchaseOrderLine.ConfirmedQuantity = null

PurchaseOrderLine.QuantityUnit = SalesOrderLine.QuantityUnit

PurchaseOrderLine.Lot = SalesOrderLine.Lot

PurchaseOrderLine.QuantityBase = REMAINING(QuantityBase)

PurchaseOrderLine.ConfirmedQuantityBase = null

PurchaseOrderLine.PricePerUnit = ROUND(SalesOrderLine.LineAmount / SalesOrderLine.Quantity, 5)

PurchaseOrderLine.ConfirmedPricePerUnit = ROUND(SalesOrderLine.LineAmount / SalesOrderLine.Quantity, 5)

PurchaseOrderLine.PlannedDeliveryDate = SalesOrderLine.RequiredDeliveryDate

PurchaseOrderLine.ProductVariant = SalesOrderLine.ProductVariant

PurchaseOrderLine.ParentLineNo = SalesOrderLine.LineNo
PurchaseOrderLine.ParentDocument = SalesOrder
PurchaseOrderLine.Notes = null

The rest of the attributes of the Purchase Order Lines must be null.