from proteus import config, Model
from decimal import getcontext, Decimal, ROUND_HALF_UP


# XML-RPC Configuration
HTTPS = 'https://'
SERVER_URL = 'itsa.open-squared.tech'
DATABASE_NAME = 'tradon'
USERNAME = 'admin'
PASSWORD = 'dsproject'


config = config.set_xmlrpc(f'{HTTPS}{USERNAME}:{PASSWORD}@{SERVER_URL}/{DATABASE_NAME}/')
    

Company = Model.get('company.company')
Party = Model.get('party.party')
Currency = Model.get('currency.currency')
Purchase = Model.get('purchase.purchase')
Product = Model.get('product.product')
Wb = Model.get('purchase.weight.basis')

# Récupération des records
company = Company(6)
party = Party(2776)

# Création de la commande d'achat
purchase = Purchase()
purchase.company = company
purchase.party = party
purchase.currency = company.currency
purchase.tol_min = Decimal(1)
purchase.wb = Wb(1)
# Ligne d'achat
product = Product(12)  # id du produit
line = purchase.lines.new()
line.product = product
line.quantity = 10
line.unit_price = product.cost_price

# Sauvegarde
purchase.save()

print(f"Purchase créée : {purchase.id}")