123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- # -*- encoding: utf-8 -*-
- from openerp import models, fields, api, _
- from openerp.exceptions import Warning
- from pytz import timezone
- from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
- from datetime import datetime,timedelta
- from openerp import http
- DATE_FORMAT = '%Y-%m-%d'
- class import_purchase_line(models.Model):
- _name = 'import.purchase.line'
- import_id = fields.Many2one('import.cost', 'Compras de importación',ondelete='cascade')
- purchase_id = fields.Many2one('account.invoice', 'Compras',required=True)
- purchase_subtotal = fields.Float("Subtotal", related='purchase_id.amount_untaxed', store=True, readonly=True)
- purchase_taxes = fields.Float("Impuestos", related='purchase_id.amount_tax', store=True, readonly=True)
- purchase_total = fields.Float("Total", related='purchase_id.amount_total', store=True, readonly=True)
- product_qty = fields.Float("Producto por factura", compute="get_invoice_product_qty", store=True)
- @api.one
- @api.depends('purchase_id')
- def get_invoice_product_qty(self):
- suma = 0
- for item in self.purchase_id.invoice_line:
- line = self.env['account.invoice.line'].search([('id','=',item.id)])
- suma = suma + line.quantity
- self.product_qty = suma
- class import_expense_line(models.Model):
- _name = 'import.expense.line'
- import_id = fields.Many2one('import.cost', 'Gastos de importación',ondelete='cascade')
- expense_id = fields.Many2one('account.invoice', 'Gastos', required=True)
- expense_subtotal = fields.Float("Subtotal", related='expense_id.amount_untaxed', store=True, readonly=True)
- expense_taxes = fields.Float("Impuestos", related='expense_id.amount_tax', store=True, readonly=True)
- expense_total = fields.Float("Total", related='expense_id.amount_total', store=True, readonly=True)
- class import_cost(models.Model):
- _name = 'import.cost'
- name = fields.Char(string='Nombre', readonly=True)
- date = fields.Datetime('Fecha', default=datetime.now(), required=True)
- purchases = fields.One2many('import.purchase.line','import_id','Facturas de compra')
- expenses = fields.One2many('import.expense.line','import_id','Facturas de gasto')
- factor = fields.Float(string="Factor", readonly=True)
- state = fields.Selection([
- ('draft', 'Borrador'),
- ('approved', 'Aprobado'),
- ], 'Estado', readonly=True, select=True, default="draft")
- created_by = fields.Many2one('res.users', 'Creado por', default=lambda self: self.env.user.id, readonly=True)
- approved_by = fields.Many2one('res.users', 'Aprobado por')
- customs_broker = fields.Many2one('res.partner', 'Despachante')
- customs_clearance = fields.Char('Despacho Nro')
- @api.multi
- def _get_qty(self):
- for x in self:
- suma = 0
- purchase = self.env['import.purchase.line'].search([('import_id','=',x.id)])
- for each in purchase:
- suma = suma + each.product_qty
- x.product_qty = suma
- product_qty = fields.Float('Cantidad de productos', compute="_get_qty")
- note = fields.Char('Observación')
- currency_id = fields.Many2one('res.currency', 'Moneda')
- weight = fields.Float('Peso')
- @api.multi
- def _get_amount(self):
- for x in self:
- suma_purchase_subtotal = 0
- suma_purchase_taxes = 0
- suma_purchase_total = 0
- suma_expense_subtotal = 0
- suma_expense_taxes = 0
- suma_expense_total = 0
- invoice1 = self.env['import.purchase.line'].search([('import_id','=',x.id)])
- for each in invoice1:
- suma_purchase_subtotal = suma_purchase_subtotal + each.purchase_subtotal
- suma_purchase_taxes = suma_purchase_taxes + each.purchase_taxes
- suma_purchase_total = suma_purchase_total + each.purchase_total
- invoice2 = self.env['import.expense.line'].search([('import_id','=',x.id)])
- for item in invoice2:
- suma_expense_subtotal = suma_expense_subtotal + item.expense_subtotal
- suma_expense_taxes = suma_expense_taxes + item.expense_taxes
- suma_expense_total = suma_expense_total + item.expense_total
- x.purchases_subtotal = suma_purchase_subtotal
- x.purchases_taxes = suma_purchase_taxes
- x.purchases_total = suma_purchase_total
- x.expenses_subtotal = suma_expense_subtotal
- x.expenses_taxes = suma_expense_taxes
- x.expenses_total = suma_expense_total
- purchases_subtotal = fields.Float(string="Subtotal", compute="_get_amount")
- purchases_taxes = fields.Float(string="Impuestos", compute="_get_amount")
- purchases_total = fields.Float(string="Total", compute="_get_amount")
- expenses_subtotal = fields.Float(string="Subtotal", compute="_get_amount")
- expenses_taxes = fields.Float(string="Impuestos", compute="_get_amount")
- expenses_total = fields.Float(string="Total", compute="_get_amount")
- @api.multi
- def button_dummy(self):
- return True
- @api.model
- def approve_import_cost(self, value):
- approved_by = self.env.user.id
- import_cost = self.env['import.cost'].search([('id','=',value['import_id'])])
- import_cost.write({'state': 'approved','factor':value['factor'],'approved_by':approved_by})
- for item in value['purchases']:
- product = self.env['product.product'].search([('id','=',item['product_id'][0])])
- template = self.env['product.template'].search([('id','=',product.product_tmpl_id.id)])
- if(template.price_change == 'import'):
- template.write({'standard_price': item['import_cost']})
- return True
- @api.model
- def create(self,vals):
- ctx = dict(self._context or {})
- vals['name'] = self.env['ir.sequence'].get('import.cost') or '/'
- return super(import_cost, self.with_context(ctx)).create(vals)
|