# -*- encoding: utf-8 -*- from openerp import models, api, fields from pytz import timezone from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT from datetime import datetime,timedelta DATE_FORMAT = '%Y-%m-%d' class PurchaseOrder(models.Model): _inherit = 'purchase.order' _name = 'purchase.order' invoice_number = fields.Char('Numero de Factura') contado = fields.Boolean('Contado') credito = fields.Boolean('Crédito') _defaults = { 'contado': True } @api.one @api.onchange('credito') def cambiar_estado_credito(self): self.contado = not self.credito @api.one @api.onchange('contado') def cambiar_estado_contado(self): self.credito = not self.contado ''' Timezone ''' def get_timezone(self): return timezone(self._context.get('tz') or self.env.user.tz) def _convert_str_to_datetime(self, date): return datetime.strptime(date,DEFAULT_SERVER_DATETIME_FORMAT) @api.multi def purchase_process_now(self): """ Confirms order and creates and validates invoice, confirms pickings. """ for purchase in self: self.update_state() inv_id = purchase.action_invoice_create() if inv_id: inv = self.env['account.invoice'].browse(inv_id) # import web_pdb; web_pdb.set_trace() date = self._convert_str_to_datetime(purchase.date_order) date = date.strftime(DATE_FORMAT) inv.write({ 'date_invoice':date, 'supplier_invoice_number':self.invoice_number, 'contado':self.contado, 'credito':self.credito, }) inv.signal_workflow('invoice_open') purchase.action_picking_create() for picking in purchase.picking_ids: picking.force_assign() picking.action_done() @api.multi def update_state(self): for order in self: order.write({'state': 'approved'}) return True class PurchaseOrderLine(models.Model): _inherit = 'purchase.order.line' _name = 'purchase.order.line' amount_total = fields.Float(compute='_calculate_total_purchase_line',string='Total',default=0.0) @api.depends('discount','price_unit','product_qty') def _calculate_total_purchase_line(self): for line in self: line.amount_total = line.product_qty * (line.price_unit - ((line.price_unit * (line.discount or 0.0))/100.0)) class AccountInvoice(models.Model): _inherit = 'account.invoice' _name = 'account.invoice' contado = fields.Boolean('Contado') credito = fields.Boolean('Crédito') _defaults = { 'contado': True } @api.one @api.onchange('credito') def cambiar_estado_credito(self): self.contado = not self.credito @api.one @api.onchange('contado') def cambiar_estado_contado(self): self.credito = not self.contado