Request the bill (order to sale conversion)

This function is performed when it is required to send an order to the till.

In terms of API, it means transforming an order into a sale and changing the status of the starting order to "checkout".

1 - Let's start with a basic sale object

var sale = {  
    name: order.name,  
    status: 'open',  
    channel: order.channel,  
    createdby_id: opData.id,  
    seller_id: opData.id,  
    seller_name: opData.name,  
    currency:'EUR',  
    open_at: new Date().toISOString(),  
    payments: [],  
    price_changes: [],  
    sale_documents: [],  
    sale_items: [],  
    uuid: order.uuid,  
    channel:order.channel,  
    external_id:order.external_id,  
    notes:null,  
    order_id: order.id,  
    order_uuid:order.uuid,  
    order_type:order.type  
};

2- Add the service cost per person (optional)

They are managed automatically when a room is created in case in the shop preferences there is the key automated_add_cover and in case in the order we have the covers field> 0.

If these conditions are true there may be the shop preference orders.automated_add_cover.type which can have these values:

id I search for "orders.automated_add_cover.value", this field gives me an id that corresponds to the item id of the default cover. I run GET v2 / items /: id and create a completed sales_item (to be added to the sales_items list of the room being created):

var saleItem = {  
   added_at: (new Date()).toISOString(),  
   category_id: itemCover.category.id,  
   category_name: itemCover.category.name,  
   cost: itemCover.cost,  
   department_id: itemCover.department.id,  
   department_name:  itemCover.department.name,  
   item_id: itemCover.id,  
   lastupdate_at: (new Date()).toISOString(),  
   lastupdate_by: opData.id, // operator id of the order  
   name: itemCover.name,  
   notes: null,  
   price_changes: \[],  
   price: price, // price ottenuto dall’item in base al default_pricelist dell’order_customer  
   quantity: order.covers || 1,  
   seller_id: opData.id, // operator id of the order  
   seller_name: opData.name, // operator id of the order  
   sku: itemCover.sku,  
   type: "sale",  
   uuid: uuid4.generate(),  
   vat_perc: vat_perc // ottenuta o da itemCover.vat_perc o da itemCover.department.vat.value  
};

perc cerco “orders.automated_add_cover.value”, aggiungo una riga al price_changes così strutturata:

var coverPriceChange = {  
    type: "surcharge_perc",  
    description:"Maggiorazione coperto",  
    value:shop_preference["orders.automated_add_cover.value"]),  
};

3 - Add items

First of all, fill in sale.sales_items with the lines present in the order.order_items.

📘

It is not a mandatory condition that the item exists on the shop database.

4 - Allow automatic issuing of the receipt

To automatically print a sale follow the folowing requirements

  1. seller_id must set to 0
  2. Add a payment to the sale, with the full amount of the sale and the field paid = true
  3. Enable the automatic printing on the application settings via UI