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': {}, 'impreso': {}, }) @classmethod def default_pizza_number(cls): return 0 def get_invoice_resolution(subtype): 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)]) @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: 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 @classmethod def report_bill(cls, records): if not records: return 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 data["tax_identifier_code"] = record.party.tax_identifier.code data["address"] = record.invoice_address.street 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 } 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] 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 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] 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] 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 impreso(cls, records): record = records[0] for line in record.lines: #line.analytic_accounts = tuple() line.impreso = True line.save() 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" 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'} 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)]) 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'} cls.impreso([record]) 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)]) record = records[0] if not printers: return 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) class Line(metaclass=PoolMeta): "Sale Line Fast Food" __name__ = 'sale.line' pizza = fields.Integer("Pizza") impreso = fields.Boolean("Impreso") @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 and 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