trytondo-sale_fast_food/sale.py
2023-07-25 23:06:21 -05:00

184 lines
6.3 KiB
Python

from trytond.pool import Pool, PoolMeta
from trytond.model import ModelView, fields
from trytond.transaction import Transaction
from trytond.exceptions import UserError
import requests
import json
class Sale(metaclass=PoolMeta):
"Sale Fast Food"
__name__ = 'sale.sale'
pizza_number = fields.Integer("Number pizza")
@classmethod
def __setup__(cls):
super(Sale, cls).__setup__()
cls._buttons.update({
'add_pizza': {},
'kitchen': {},
'bar': {},
'print_bill': {},
})
@classmethod
def default_pizza_number(cls):
return 0
def report_bill(records):
if not records:
return
pool = Pool()
ctx = Transaction().context
report = records[0]
User = pool.get('res.user')
data = {}
user = User(Transaction().user)
data["user"] = user.name
data["party"] = report.party.name
data["tax_identifier_type"] = report.party.tax_identifier.type_string
data["tax_identifier_code"] = report.party.tax_identifier.code
data["address"] = report.invoice_address.street
data["city"] = report.invoice_address.subdivision_municipality.name
data["zone"] = report.zone.name if report.zone else ""
data["table"] = report.table.name if report.table else ""
data["lines"] = [{'type': line.type,
"product": line.product.name if line.type != 'title' else None,
"quantity": line.quantity if line.type != 'title' else None,
"uom": line.unit.name if line.type != 'title' else None,
"unit_price": line.unit_price if line.type != 'title' else None,
"taxes": line.taxes[0] if line.type != 'title' and line.taxes else None
} for line in report.lines]
def report_customer_order(records):
if not records:
return
pool = Pool()
ctx = Transaction().context
report = records[0]
User = pool.get('res.user')
data = {}
user = User(Transaction().user)
data["user"] = user.name
data["party"] = report.party.name
data["tax_identifier_type"] = report.party.tax_identifier.type_string
data["tax_identifier_code"] = report.party.tax_identifier.code
data["address"] = report.invoice_address.street
data["city"] = report.invoice_address.subdivision_municipality.name
data["zone"] = report.zone.name if report.zone else ""
data["table"] = report.table.name if report.table else ""
data["lines"] = [{'type': line.type,
"product": line.product.name if line.type != 'title' else None,
"quantity": line.quantity if line.type != 'title' else None,
"uom": line.unit.name if line.type != 'title' else None} for line in report.lines]
return data
@classmethod
@ModelView.button
def add_pizza(cls, records):
pool = Pool()
saleLine = pool.get('sale.line')
for record in records:
record.pizza_number +=1
record.lines += (saleLine(type="title",
description="Pizza Combinada"),)
record.save()
@classmethod
@ModelView.button
def print_bill(cls, records):
pool = Pool()
context = Transaction().context
shop = context['shop']
Printer = pool.get('sale.printer')
printers = Printer.search([('zone', '=', 'reception'), ('shop', '=', shop)])
if not printers:
return
printer = printers[0]
url = f"http://{printer.api.ip_address}/print_bill"
customer_order = cls.report_customer_order(records)
content = {"content": str(json.dumps(customer_order)), "ip_printer": str(printer.ip_address)}
headers = {"accept": 'application/json', 'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(content), headers=headers)
@classmethod
@ModelView.button
def kitchen(cls, records):
pool = Pool()
context = Transaction().context
shop = context['shop']
Printer = pool.get('sale.printer')
printers = Printer.search([('zone', '=', 'kitchen'), ('shop', '=', shop)])
if not printers:
return
printer = printers[0]
url = f"http://{printer.api.ip_address}/order_kitchen"
customer_order = cls.report_customer_order(records)
content = {"content": str(json.dumps(customer_order)), "ip_printer": str(printer.ip_address)}
headers = {"accept": 'application/json', 'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(content), headers=headers)
#return response.status_code
@classmethod
@ModelView.button
def bar(cls, records):
pool = Pool()
context = Transaction().context
shop = context['shop']
Printer = pool.get('sale.printer')
printers = Printer.search([('zone', '=', 'bar'), ('shop', '=', shop)])
if not printers:
return
printer = printers[0]
url = f"http://{printer.api.ip_address}/order_bar"
customer_order = cls.report_customer_order(records)
content = {"content": str(json.dumps(customer_order)), "ip_printer": str(printer.ip_address)}
headers = {"accept": 'application/json', 'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(content), headers=headers)
class Line(metaclass=PoolMeta):
"Sale Line Fast Food"
__name__ = 'sale.line'
pizza = fields.Integer("Pizza")
@fields.depends('product', 'unit', 'sale',
'_parent_sale.party', '_parent_sale.invoice_party',
'_parent_sale.pizza_number',
methods=['compute_taxes', 'compute_unit_price',
'on_change_with_amount'])
def on_change_product(self):
super(Line, self).on_change_product()
if self.product.pizza:
self.pizza = self.sale.pizza_number
def get_production(self):
"Return production for the sale line"
Production = super(Line, self).get_production()
#Production.bom = self.product.template.boms.id
return Production