---
# System prepended metadata

title: Steps for next projects

---

# Steps for next projects

## New Cron Jobs
- Payment Reset Cron (@ 6AM Chile?)

## New PubSub resources

- seller-profile
- seller-profile-netsuite-pull
- seller_payment_initialization_topic
- seller_payment_initialization_subscription
- balance_data_retrieval_topic
- balance_data_retrieval_subscription
- pay_seller_balance_topic
- pay_seller_balance_subscription
- check_fpay_payment_status_topic
- check_fpay_payment_status_subscription

## New Tables in AG DB

### Countries
- id
- name
- enabled (bool)

### Sellers
<!--- Do sellers will be generated as multiple sellers when they have different type of operation (local|international) ? --->
- id
- fpay_id
- day_balance_generated (bool)
- countries (json)
    - Example:
```json=
{
    "cl": {
        "currency": "CLP",
        "enabled": true
    },
    "pe": {
        "currency": "USD",
        "enabled": false
    }
}
```
- erp_id
<!--- vendor_id NetSuite reference? -->
- type

### SellerBalances
- id
- seller_id
- date
- country
- invoices
- vendor_bills
- vendor_payments
<!--- vendor_payments document generated to report payment status --->
- amount_owed
- currency
- status
- Jobs (json)
    - Example:
```json=
[
    {
        "seller_payment_initialization": {
            "message_id": 12345, 
            "finished": true, 
            "successful": true,
            "last_execution": "2021-03-21 10:00:00"
        }
    }, {
        "balance_data_retrieval": {
            "message_id": 12345, 
            "finished": false, 
            "successful": false,
            "last_execution": "2021-03-21 10:00:00"
        }
    }
]
```
- bank_response
- settlement_id
- …

# Seller Creation

## Flow

https://confluence.falabella.com/display/CMP/Sellers+-+Create

## Seller Event

https://confluence.falabella.com/display/Catalyst/Outbound+Events+and+Schemas#OutboundEventsandSchemas-SellerOutboundEventv2

## Seller Creation Flow

- GSC will send Seller creation Event
- AG will receive event from PubSub
- AG will query GSC API for seller details.
- New seller will be created in DB.
- Seller will be created in NetSuite.

## Seller Update Flow

- GSC will send Seller Update Event with FPay ID
- AG will receive event from PubSub
- AG will query GSC API for seller details
- Seller FPay ID will be updated to AG DB
- (?) Set FPay ID in NetSuite
<!--- fpay_id is received as the account_number --->

# Payment Process

## Payment Reset Cron
- AG will run a query to set ALL seller’s day_balance_generated to false.
- Once its verified that all are false, it will queue a message of type: `seller_payment_initialization_topic`

## Seller Payment Initialization Job
- Read X number of sellers which have d tay_balance_generated == false
- For each seller
    - Generate an empty SellerBalance model for the previous day for each country the seller operates in. This should include a parameter for seller ID, country, currency, and the date, with the rest of the fields empty. (This should only happen for the ones that are enabled.  If not enabled, log the reason why the balance is not running for the given seller in country x).
    - Queue a message of type `balance_data_retrieval_topic` for each of the seller’s SellerBalance (making sure to include the `seller_balance_id` parameter for each one)
        - Add entry to SellerBalance `jobs` field which includes PubSub `message_id`.
    - Update the seller’s day_balance_generated to true
- Repeat the process until we query the DB and there are no sellers with day_balance_generated = false.
- Return true once that happens so that job can finish.  If anything goes wrong.. return false so that it runs again and processes remaining sellers on the next execution.

## Balance Data Retrieval Job
- Job will run with parameter of `seller_balance_id`.
- Query DB to get back model of SellerBalance with the given id.  This will tell us the date, seller, country, and currency we’re working with.
- Query NetSuite for ALL vendorBill and Invoice for this seller, country, day.
- Loop through each result
    - For each record found add to SellerBalance model.
        - If it's an invoice `$sellerBalance->addInvoice()`.
        - if its a vendorBill `$sellerBalance->addVendorBill()`.
- Once all NetSuite records have been added, calculate the balance and set the value in the model.
- Query DB to see which settlement this SellerBalance corresponds to.
    - If we don’t find a match
        - Alert log so we can let finance know.  This should only happen if someone manually created a VendorBill or Invoice.
    - If we do find a match
        - Set the `settlement_id` value of the SellerBalance.
        - If the amount is different alert log to let finance know.  This can happen if someone manually changes the values in NetSuite.
- Persist SellerBalance
- If the amount_owed is <= 0, we do not need to pay the seller.
    - (ask Alejandro if we need to do anything else.  Do we need to notify GSC if there will be no payment made?)
- If the amount_owed is > 0, we need to make a payment.
    - Queue the `pay_seller_balance_topic` with the `seller_balance_id` as parameter
    - Add entry to SellerBalance `jobs` field which includes PubSub `message_id`.

## Pay Seller Balance Job
- Job will run with parameter of seller_balance_id available.
- Query DB to get back model of SellerBalance with the given id.
- Query Seller based on the SellerBalance’s seller_id.
- Figure out for this seller, country, currency, which bank will be used (can hardcode FPay for now).
- Instantiate bank service
- Make payment to seller with the necessary amount and currency.
- Persist SellerBalance with the status and bank receipt details.
- (Ask if we need to notify NetSuite that payment was scheduled).
- If the web hook where FPay calls us to let us know the payment was successful/failed is not available we do the steps below.
    - Queue a `check_fpay_payment_status_topic` with the `seller_balance_id` as parameter.
    - Add entry to SellerBalance `jobs` field which includes PubSub `message_id`.

## Check Fpay Payment Status Job
- Job will run with parameter of `seller_balance_id` available.
- Query DB to get model of SellerBalance with the given id.
- Perform API request to FPay to get payment status details.
- If pending
    - Return false so that the job runs again in some time.
- If rejected
    - Evaluate the reason for rejection.
    - If the reason was because we don’t have enough money in the account or something like that.. alert so we can let finance know.
    - If the reason is because of something which can not be fixed.
        - Set status to FAILURE
        - Notify NetSuite somehow (ask Alejandro how they want us to let them know)
        - Return true so that this job does not try to run again later
- If successful
    - Notify NetSuite by updating the relevant documents
    - If the settlement id was matched successfully AND its 3P
        - Notify GSC by hitting their API endpoint

# Questions

### Bellido: Can one seller have multiple FPay ID or Payonir ID? Like one per country?
> It's possible to have a single seller/vendor created in NetSuite, with multiple subsidiaries. Therefore, we can attach to the seller their respective bank info based on its subsidiary.

### Bellido: Will a seller have 1 seller resource in NetSuite or have multiple like one for each country.
> Refer to the previous answer.
    
### Bellido: How do we alert finance of a system issue?
> With logs, and generate them, as more detailed as possible. Make sure we can give all the details of what failed at the moment.
    
### Bellido: IF seller balance is less than 0, do we still notify GSC?  Do we need to do anything else?
> Seller Balance less than 0 is viable. We can owe the seller, as well as the opposite. And NetSuite is responsible for the Seller Balance, and we need to rely on what it's reported by it.
    
### Isabelle: What happens if there are multiple bank info?
> Sellers will not have more than 1 bank info:
>
> * Either they will be national sellers and will be paid through Fpay
> * Or they will be international and will be paid in USD through another financial gateway (most likely payoneer)
>
> Note: sellers from LATAM that wish to sell in a different country (such as seller from CL selling in PE) is not our target as it's a border case today.

### Bellido: What happened if there is a mismatch in the settlements (SellerBalance vs SellerClosing)?
> It's expected this kind of behavior, and it is something NetSuite is responsible for. We can't delegate this task to our project and only rely on what NetSuite reports since it's our source of truth.
    
### Isabelle: What if another field changes in a seller update event?
> Yes, sellers could update any other field (expect their tax id (rut in CL)). I think Ale was going to check what could be updated in Netsuite with the Finance area/team.

### Bellido: Which fields of the seller can't be updated?
> The finance team needs to coordinate about this topic then they'll let us know which would be the fields that will be updatable. Depending on that communication, we can provide the list of fields allowed to be sent in the scenario of an update event.
