Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a service to generate the customer feed from hotwax to Netsuite. #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions data/ServiceJobData.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<entity-facade-xml type="seed">
<!-- ServiceJob data for create order from HC to netsuite -->
<moqui.service.job.ServiceJob jobName="generate_CustomerFeed" description="Generate HotWax Create Customer Feed for Netsuite"
serviceName="co.hotwax.netsuite.CustomerServices.generate#CustomerSyncFeed" cronExpression="0 0/15 * * * ?" paused="Y">
<parameters parameterName="systemMessageTypeId" parameterValue=""/>
<parameters parameterName="systemMessageRemoteId" parameterValue=""/>
<parameters parameterName="partyId" parameterValue=""/>
</moqui.service.job.ServiceJob>
</entity-facade-xml>
10 changes: 10 additions & 0 deletions data/SystemMessageData.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-facade-xml type="seed">
<!-- System Message Type for Create Customer Feed for Netsuite -->
<moqui.service.message.SystemMessageType systemMessageTypeId="CreateNetsuiteCustomerFeed"
description="Generate HotWax Create Customer Feed for Netsuite"
parentTypeId="LocalFeedFile"
sendPath="/home/${sftpUsername}/netsuite/customer/export"
sendServiceName="co.hotwax.ofbiz.SystemMessageServices.send#SystemMessageFileSftp"
receivePath="${contentRoot}/Customer/customer-Feed-${dateTime}.csv"/>
</entity-facade-xml>
98 changes: 98 additions & 0 deletions service/co/hotwax/netsuite/CustomerServices.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-3.xsd">
<service verb="generate" noun="CustomerSyncFeed" authenticate="anonymous-all">
<in-parameters>
<parameter name="partyId">
<description>Parameter to fetch party details for a specific customer.</description>
</parameter>
<parameter name="systemMessageTypeId" required="true">
<description>The System Message Type ID for generating the Customer Feed for Netsuite.</description>
</parameter>
<parameter name="systemMessageRemoteId" required="true">
<description>The System Message Remote Id for generating the Customer Feed for Netsuite.</description>
</parameter>
</in-parameters>
<actions>
<set field="nowDate" from="ec.user.nowTimestamp"/>
<log message="Generating Customer Feed file for party ${partyId} at time ${nowDate}"/>

<!-- Fetch the receivePath from SystemMessageType to prepare the path for creating the file in the receiving system. Ex: Moqui's datamanager directory in runtime for creating feeds.-->
<entity-find-one entity-name="moqui.service.message.SystemMessageType" value-field="systemMessageType"/>
<if condition="systemMessageType == null">
<return error="true" message="Could not find SystemMessageType with ID ${systemMessageTypeId}"/>
</if>
<!-- Prepare csv File Path -->
<set field="csvFilePathRef" from="ec.resource.expand(systemMessageType.receivePath, null,
[contentRoot: ec.user.getPreference('mantle.content.root') ?: 'dbresource://datamanager', date:ec.l10n.format(nowDate, 'yyyy-MM-dd'), dateTime:ec.l10n.format(nowDate, 'yyyy-MM-dd-HH-mm-ss-SSS'),
productStoreId:productStoreId], false)"/>
<set field="csvFilePath" from="ec.resource.getLocationReference(csvFilePathRef).getUri().getPath()"/>
<script>
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVPrinter
import java.nio.file.Files
import java.nio.file.Paths
import java.nio.charset.StandardCharsets

File csvFile = new File(csvFilePath)
if (!csvFile.parentFile.exists()) {
csvFile.parentFile.mkdirs()
}

Files.newBufferedWriter(Paths.get(csvFilePath), StandardCharsets.UTF_8).withCloseable { writer ->
def csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT)

// Fetch customers and check if there are eligible customers
def netsuiteCustomer_find = ec.entity.find("co.netsuite.customer.CustomerView")
if (partyId) {
netsuiteCustomer_find.condition("externalId", partyId)
}

try (netsuiteCustomer_itr = netsuiteCustomer_find.iterator()) {
if (!netsuiteCustomer_itr.hasNext()) {
return "No eligible customer at ${nowDate}, not generating the feed file."
}

// Process and write customer data (streaming approach)
def csvHeaders = null
netsuiteCustomer_itr.each { customer ->
def customerData = [:]
customerData.putAll(customer)

customerData.individual = "T"
customerData.status = "CUSTOMER-Closed Won"
customerData.taxable = "TRUE"
customerData.defaultOrderPriority = "5"

customerData.firstName = (customer.firstName?.length() > 13 ? customer.firstName.substring(0, 13) : customer.firstName) ?: "X"
customerData.lastName = (customer.lastName?.length() > 13 ? customer.lastName.substring(0, 13) : customer.lastName) ?: "X"

customerData.phone = [customer.countryCode, customer.areaCode, customer.contactNumber].findAll { it }?.join(' ') ?: null

// Remove unnecessary fields
customerData.remove("countryCode")
customerData.remove("areaCode")
customerData.remove("contactNumber")

// Set headers only once
if (csvHeaders == null) {
csvHeaders = customerData.keySet() as List
csvPrinter.printRecord(csvHeaders)
}

// Write each row
csvPrinter.printRecord(csvHeaders.collect { key -> customerData[key] ?: "" })
}
csvPrinter.flush()
}
}
</script>
<!-- Save the Json Feed File path in System Message messageText -->
<service-call name="org.moqui.impl.SystemMessageServices.queue#SystemMessage"
in-map="[systemMessageTypeId:systemMessageTypeId, systemMessageRemoteId:systemMessageRemoteId,
messageText:csvFilePath]" out-map="FeedSysMsgOut"/>

<log message="Generating Order Items Feed file with type ${systemMessageTypeId} and
remote ${systemMessageRemoteId} saved response in messages ${FeedSysMsgOut.systemMessageId}"/>
</actions>
</service>
</services>