From b737a148578b918132b08bf9f74ee08a89810204 Mon Sep 17 00:00:00 2001 From: sinergia Date: Sun, 13 Aug 2023 15:14:22 -0500 Subject: [PATCH] fix: clean home --- sale.py | 145 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/sale.py b/sale.py index a09bae5..9c8555a 100644 --- a/sale.py +++ b/sale.py @@ -1,17 +1,17 @@ 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__() @@ -23,7 +23,6 @@ class Sale(metaclass=PoolMeta): 'impreso': {}, }) - @classmethod def default_pizza_number(cls): return 0 @@ -32,25 +31,25 @@ class Sale(metaclass=PoolMeta): if subtype: resolution = subtype.sequence.invoice_resolution if resolution: - return dict([('resolution_number', resolution.resolution_number), - ('resolution_prefix', resolution.prefix), - ('valid_date_time_from', str(resolution.valid_date_time_from)), - ('valid_date_time_to', str(resolution.valid_date_time_to)), - ('from_number', resolution.from_number), - ('to_number', resolution.to_number)]) - + return dict([ + ('resolution_number', resolution.resolution_number), + ('resolution_prefix', resolution.prefix), + ('valid_date_time_from', str(resolution.valid_date_time_from)), + ('valid_date_time_to', str(resolution.valid_date_time_to)), + ('from_number', resolution.from_number), + ('to_number', resolution.to_number)]) + @classmethod def get_invoice(cls, record): pool = Pool() ctx = Transaction().context Shop = pool.get('sale.shop') shop = Shop.search([('id', '=', ctx["shop"])])[0] - if record.state != 'draft' and record.invoices: + if record.state != 'draft' and record.invoices: invoice = record.invoices[0] data = {} data['invoice_number'] = invoice.number subtype = invoice.subtype - #raise UserError(str(subtype)) data['resolution'] = cls.get_invoice_resolution(subtype) return data @@ -63,14 +62,11 @@ class Sale(metaclass=PoolMeta): pool = Pool() ctx = Transaction().context record = records[0] - User = pool.get('res.user') Shop = pool.get('sale.shop') data = {} - user = User(Transaction().user) shop = Shop.search([('id', '=', ctx["shop"])])[0] data["shop_name"] = shop.name data["shop_address"] = shop.address.street - #data["user"] = user.name data['invoice'] = cls.get_invoice(record) data["party"] = record.party.name data["tax_identifier_type"] = record.party.tax_identifier.type_string @@ -79,36 +75,33 @@ class Sale(metaclass=PoolMeta): data["city"] = record.invoice_address.subdivision_municipality.name data["zone"] = record.zone.name if record.zone else "" data["table"] = record.table.name if record.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.symbol if line.type != 'title' else None, - "unit_price": str(line.amount_w_tax) if line.type != 'title' else None, - "taxes": str(round(line.taxes[0].rate * 100, 2))+'%' if line.type != 'title' and line.taxes else None + 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.symbol if line.type != 'title' else None, + "unit_price": str(line.amount_w_tax) if line.type != 'title' else None, + "taxes": str(round(line.taxes[0].rate * 100, 2))+'%' + if line.type != 'title' and line.taxes else None } for line in record.lines] - + data["untaxed_amount"] = str(record.untaxed_amount) data["tax_amount"] = str(record.tax_amount) data["total"] = str(record.total_amount) if record.payments: - data['payments'] = [{"statement": payment.statement.journal.name, - "amount": str(payment.amount)} for payment in record.payments] - + data['payments'] = [{ + "statement": payment.statement.journal.name, + "amount": str(payment.amount)} for payment in record.payments] + return data 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) - #raise UserError(str(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 @@ -116,36 +109,39 @@ class Sale(metaclass=PoolMeta): 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] + 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] - 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 if line.impreso == False] + 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 if not line.impreso] 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.pizza_number += 1 record.lines += (saleLine(type="title", description="Pizza Combinada"),) record.save() - + @classmethod @ModelView.button def impreso(cls, records): record = records[0] for line in record.lines: - #line.analytic_accounts = tuple() + line.analytic_accounts = tuple() line.impreso = True line.save() record.save() @@ -157,17 +153,22 @@ class Sale(metaclass=PoolMeta): context = Transaction().context shop = context['shop'] Printer = pool.get('sale.printer') - printers = Printer.search([('zone', '=', 'reception'), ('shop', '=', shop)]) + printers = Printer.search([ + ('zone', '=', 'reception'), + ('shop', '=', shop)]) if not printers: return printer = printers[0] url = f"http://{printer.api.ip_address}/print_bill" - bill = cls.report_bill(records) - content = {"content": str(json.dumps(bill)), "ip_printer": str(printer.ip_address)} - headers = {"accept": 'application/json', 'Content-Type': 'application/json'} + user_name = context['employee.rec_name'] + content = {"content": str(json.dumps(bill)), + "ip_printer": str(printer.ip_address), + "user_name": user_name} + headers = {"accept": 'application/json', + 'Content-Type': 'application/json'} response = requests.post(url, data=json.dumps(content), headers=headers) @@ -178,24 +179,25 @@ class Sale(metaclass=PoolMeta): context = Transaction().context shop = context['shop'] Printer = pool.get('sale.printer') - printers = Printer.search([('zone', '=', 'kitchen'), ('shop', '=', shop)]) + printers = Printer.search([('zone', '=', 'kitchen'), + ('shop', '=', shop)]) record = records[0] - #raise UserError(str(printers)) 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'} + user_name = context['employee.rec_name'] + content = {"content": str(json.dumps(customer_order)), + "ip_printer": str(printer.ip_address), + "user_name": user_name} + headers = {"accept": 'application/json', + 'Content-Type': 'application/json'} cls.impreso([record]) - - response = requests.post(url, data=json.dumps(content), headers=headers) - - #return response.status_code + response = requests.post(url, data=json.dumps(content), + headers=headers) @classmethod @ModelView.button @@ -204,7 +206,8 @@ class Sale(metaclass=PoolMeta): context = Transaction().context shop = context['shop'] Printer = pool.get('sale.printer') - printers = Printer.search([('zone', '=', 'bar'), ('shop', '=', shop)]) + printers = Printer.search([('zone', '=', 'bar'), + ('shop', '=', shop)]) record = records[0] if not printers: @@ -212,19 +215,18 @@ class Sale(metaclass=PoolMeta): printer = printers[0] url = f"http://{printer.api.ip_address}/order_bar" - customer_order = cls.report_customer_order(records) cls.impreso([record]) - - #cls.impreso(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) + user_name = context['employee.rec_name'] + content = {"content": str(json.dumps(customer_order)), + "ip_printer": str(printer.ip_address), + "user_name": user_name} + 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' @@ -233,18 +235,17 @@ class Line(metaclass=PoolMeta): impreso = fields.Boolean("Impreso") @fields.depends('product', 'unit', 'sale', - '_parent_sale.party', '_parent_sale.invoice_party', + '_parent_sale.party', '_parent_sale.invoice_party', '_parent_sale.pizza_number', - methods=['compute_taxes', 'compute_unit_price', - 'on_change_with_amount']) + methods=['compute_taxes', 'compute_unit_price', + 'on_change_with_amount']) def on_change_product(self): super(Line, self).on_change_product() if self.product and self.product.pizza: self.pizza = self.sale.pizza_number - def get_production(self): + 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