trytonpsk-sale_web_channel/sale.py

284 lines
9.7 KiB
Python
Raw Normal View History

2020-06-06 01:11:40 +02:00
2023-10-07 18:30:22 +02:00
from . import rappi
from trytond.model import fields, ModelView
2020-06-23 06:24:08 +02:00
from trytond.pool import PoolMeta, Pool
from trytond.transaction import Transaction
from trytond.wizard import (Wizard, StateView, Button, StateReport,
StateTransition)
from trytond.report import Report
from trytond.pyson import Eval
2020-05-31 18:40:21 +02:00
2020-06-06 01:11:40 +02:00
2020-05-31 18:40:21 +02:00
class Sale(metaclass=PoolMeta):
__name__ = 'sale.sale'
2020-06-06 01:11:40 +02:00
is_paymented = fields.Function(fields.Boolean('Is Paymented'), 'get_is_paymented')
products_string = fields.Char('Products String')
2023-11-28 23:23:11 +01:00
# channel = fields.Many2One('sale.web_channel', 'channel')
2020-06-23 22:56:38 +02:00
uploaded_invoice = fields.Boolean('uploaded Invoice', readonly=True)
2020-06-26 04:50:30 +02:00
document_invoice = fields.Char('Document Invoice')
tracking_number = fields.Char('Tracking Number')
pack_id = fields.Char('Pack ID')
2020-06-06 01:11:40 +02:00
def get_is_paymented(self, name):
if self.residual_amount == 0:
return True
else:
return False
def get_string_lines(self, name):
string_ = ''
for line in self.lines:
string_ += line.product.rec_name + ' X ' + str(line.quantity)
return string_
2020-05-31 18:40:21 +02:00
2020-12-20 20:49:28 +01:00
# @classmethod
# def workflow_to_end(cls, sales):
# super(Sale, cls).workflow_to_end(sales)
# for sale in sales:
# if sale.channel:
2021-02-19 19:12:03 +01:00
# if sale.channel.channel_name == 'mercadolibre':
# MercadoLibre = Pool().get('sale.web_channel.mercado_libre')
# channel = MercadoLibre(sale.channel)
# channel.upload_invoice(sale)
2020-08-18 22:11:20 +02:00
@classmethod
def create(cls, records):
sales = super(Sale, cls).create(records)
for sale in sales:
string_ = ''
for line in sale.lines:
string_ += line.product.rec_name + ' X ' + str(line.quantity)
cls.write([sale], {'products_string': string_})
return sales
2023-10-07 18:30:22 +02:00
@classmethod
def quote(cls, sales):
super(Sale, cls).quote(sales)
for sale in sales:
2023-11-28 23:23:11 +01:00
# if sale.web_shop.type == 'rappi':
# print('si entra aqui en quote')
# rappi_rec = rappi.Rappi(sale.web_shop)
# rappi_rec.take_order(sale.web_id, 20)
pass
2023-10-07 18:30:22 +02:00
@classmethod
def confirm(cls, sales):
super(Sale, cls).confirm(sales)
for sale in sales:
if sale.web_shop.type == 'rappi':
print('si entra aqui en confirm')
rappi_rec = rappi.Rappi(sale.web_shop)
rappi_rec.ready_for_pickup(sale.web_id)
invoice = super(Sale, cls).create_invoice(sale)
invoice.save()
@classmethod
def cancel(cls, sales):
super(Sale, cls).confirm(sales)
for sale in sales:
if sale.web_shop.type == 'rappi':
print('si entra aqui en reject')
rappi_rec = rappi.Rappi(sale.web_shop)
rappi_rec.reject(sale.web_id)
2020-12-20 19:34:05 +01:00
# def create_invoice(self):
# 'Create and return an invoice'
# pool = Pool()
# Invoice = pool.get('account.invoice')
# if self.invoice_method == 'manual':
# return
#
# invoice_lines = []
# sales = [self]
# if self.pack_id:
# sales = self.search([
# ('pack_id', '=', self.pack_id)
# ])
# self.process_pack()
# for sale in sales:
# for line in sale.lines:
# if self.total_amount < 0 and line.quantity > 0:
# continue
# if self.total_amount > 0 and line.quantity < 0:
# continue
# invoice_lines.append(line.get_invoice_line())
# invoice_lines = list(chain(*invoice_lines))
# if not invoice_lines:
# return
#
# invoice = self._get_invoice_sale()
# if getattr(invoice, 'lines', None):
# invoice_lines = list(invoice.lines) + invoice_lines
# invoice.lines = invoice_lines
# invoice.save()
#
# Invoice.update_taxes([invoice])
# return invoice
2020-09-29 19:03:57 +02:00
@classmethod
def delete_duplicates(cls, sale):
cursor = Transaction().connection.cursor()
sales_ = cls.search([('reference', '=', sale.reference)])
if len(sales_) > 1:
for s in sales_:
if s.invoices or s.id == sale.id:
continue
cursor.execute('DELETE FROM sale_sale where id=%s' % s.id)
2020-12-20 20:49:28 +01:00
# @classmethod
# def process(cls, sales):
# for sale in sales:
# cls.delete_duplicates(sale)
# super(Sale, cls).process(sales)
# sale = sales[0]
# sale.process_pack()
2020-09-30 17:58:41 +02:00
def process_pack(self):
invoice_number = self.invoice_number
sales = []
for inv in self.invoices:
sales.extend(inv.sales)
self.write(sales, {
'state': 'processing',
'invoice_number': invoice_number
})
2020-05-31 18:40:21 +02:00
2020-06-23 06:24:08 +02:00
class SaleUploadInvoice(Wizard):
'Sale Upload Invoice'
__name__ = 'sale_web_channel.upload_invoice'
start_state = 'upload_invoice'
upload_invoice = StateTransition()
def transition_upload_invoice(self):
pool = Pool()
Sale = pool.get('sale.sale')
2020-06-23 06:24:08 +02:00
ids = Transaction().context['active_ids']
if not ids:
return 'end'
for sale in Sale.browse(ids):
if not sale.invoices or not sale.channel:
2020-06-23 06:24:08 +02:00
continue
if sale.channel.channel_name == 'mercadolibre':
MercadoLibre = pool.get('sale.web_channel.mercado_libre')
channel = MercadoLibre(sale.channel)
channel.upload_invoice(sale)
2021-02-19 19:12:03 +01:00
if sale.channel.channel_name == 'shopify':
Shopify = pool.get('sale.web_channel.shopify')
channel = Shopify(sale.channel)
channel.upload_invoice(sale)
2020-06-23 06:24:08 +02:00
return 'end'
class SaleForChannelStart(ModelView):
'Sale For Channel Start'
__name__ = 'sale_web_channel.sale_for_channel.start'
company = fields.Many2One('company.company', 'Company', required=True)
shop = fields.Many2One('sale.shop', 'Shop', required=True, domain=[
('company', '=', Eval('company'))
])
channels = fields.Many2Many('sale.web_channel', None, None, 'Channel')
start_date = fields.Date('Start Date', required=True)
end_date = fields.Date('End Date', required=True)
include_canceled = fields.Boolean('Include Canceled')
@staticmethod
def default_company():
return Transaction().context.get('company')
class SaleForChannel(Wizard):
'Sale For Channel'
__name__ = 'sale_web_channel.sale_for_channel'
start = StateView('sale_web_channel.sale_for_channel.start',
'sale_web_channel.sale_for_channel_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Print', 'print_', 'tryton-ok', default=True),
])
print_ = StateReport('sale_web_channel.sale_for_channel_report')
def do_print_(self, action):
2022-02-04 17:00:12 +01:00
report_context = {
'company': self.start.company.id,
'company_name': self.start.company.rec_name,
'shop': self.start.shop.id,
'shop_name': self.start.shop.name,
'channel': [],
'start_date': self.start.start_date,
'end_date': self.start.end_date,
'include_canceled': self.start.include_canceled,
2022-02-04 17:00:12 +01:00
}
report_context['channels'] = [{'id': c.id, 'name': c.rec_name} for c in self.start.channels]
return action, report_context
def transition_print_(self):
return 'end'
class SaleForChannelReport(Report):
'Sale For Channel Report'
__name__ = 'sale_web_channel.sale_for_channel_report'
@classmethod
2022-02-04 16:34:02 +01:00
def get_context(cls, records, header, data):
report_context = super().get_context(records, header, data)
pool = Pool()
Sale = pool.get('sale.sale')
channel_ids = [c['id'] for c in data['channels']]
channel_names = [c['name'] for c in data['channels']]
shop_id = data['shop']
shop_name = data['shop_name']
company_name = data['company_name']
dom_sales = [
('shop', '=', shop_id),
('sale_date', '>=', data['start_date']),
('sale_date', '<=', data['end_date']),
2022-02-04 16:34:02 +01:00
]
states_sale = ['processing', 'done']
if data['include_canceled']:
states_sale.append('cancel')
dom_sales.append(('state', 'in', states_sale))
if channel_ids:
dom_sales.append(
('channel', 'in', channel_ids)
)
sales = Sale.search(dom_sales, order=[('sale_date', 'DESC')])
untaxed_amount_ = []
tax_amount_ = []
total_amount_ = []
total_amount_ = []
for sale in sales:
untaxed_amount_.append(sale.untaxed_amount)
tax_amount_.append(sale.tax_amount)
total_amount_.append(sale.total_amount)
report_context['records'] = sales
report_context['total_amount'] = sum(total_amount_)
report_context['tax_amount'] = sum(tax_amount_)
report_context['untaxed_amount'] = sum(untaxed_amount_)
report_context['start_date'] = data['start_date']
report_context['end_date'] = data['end_date']
report_context['shop'] = shop_name
report_context['channels'] = ', '.join(channel_names)
report_context['company'] = company_name
return report_context
2020-05-31 18:40:21 +02:00
# class Invoice(metaclass=PoolMeta):
# __name__ = 'account.invoice'
# salesman = fields.Many2One('company.employee', 'Salesman',
# select=True, states={
# 'readonly': Eval('state') != 'draft',
# })