import_cost_calculation.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning
  4. from pytz import timezone
  5. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  6. from datetime import datetime,timedelta
  7. from openerp import http
  8. DATE_FORMAT = '%Y-%m-%d'
  9. class import_purchase_line(models.Model):
  10. _name = 'import.purchase.line'
  11. import_id = fields.Many2one('import.cost', 'Compras de importación',ondelete='cascade')
  12. purchase_id = fields.Many2one('account.invoice', 'Compras',required=True)
  13. purchase_subtotal = fields.Float("Subtotal", related='purchase_id.amount_untaxed', store=True, readonly=True)
  14. purchase_taxes = fields.Float("Impuestos", related='purchase_id.amount_tax', store=True, readonly=True)
  15. purchase_total = fields.Float("Total", related='purchase_id.amount_total', store=True, readonly=True)
  16. product_qty = fields.Float("Producto por factura", compute="get_invoice_product_qty", store=True)
  17. @api.one
  18. @api.depends('purchase_id')
  19. def get_invoice_product_qty(self):
  20. suma = 0
  21. for item in self.purchase_id.invoice_line:
  22. line = self.env['account.invoice.line'].search([('id','=',item.id)])
  23. suma = suma + line.quantity
  24. self.product_qty = suma
  25. class import_expense_line(models.Model):
  26. _name = 'import.expense.line'
  27. import_id = fields.Many2one('import.cost', 'Gastos de importación',ondelete='cascade')
  28. expense_id = fields.Many2one('account.invoice', 'Gastos', required=True)
  29. expense_subtotal = fields.Float("Subtotal", related='expense_id.amount_untaxed', store=True, readonly=True)
  30. expense_taxes = fields.Float("Impuestos", related='expense_id.amount_tax', store=True, readonly=True)
  31. expense_total = fields.Float("Total", related='expense_id.amount_total', store=True, readonly=True)
  32. class import_cost(models.Model):
  33. _name = 'import.cost'
  34. name = fields.Char(string='Nombre', readonly=True)
  35. date = fields.Datetime('Fecha', default=datetime.now(), required=True)
  36. purchases = fields.One2many('import.purchase.line','import_id','Facturas de compra')
  37. expenses = fields.One2many('import.expense.line','import_id','Facturas de gasto')
  38. factor = fields.Float(string="Factor", readonly=True)
  39. state = fields.Selection([
  40. ('draft', 'Borrador'),
  41. ('approved', 'Aprobado'),
  42. ], 'Estado', readonly=True, select=True, default="draft")
  43. created_by = fields.Many2one('res.users', 'Creado por', default=lambda self: self.env.user.id, readonly=True)
  44. approved_by = fields.Many2one('res.users', 'Aprobado por')
  45. customs_broker = fields.Many2one('res.partner', 'Despachante')
  46. customs_clearance = fields.Char('Despacho Nro')
  47. @api.multi
  48. def _get_qty(self):
  49. for x in self:
  50. suma = 0
  51. purchase = self.env['import.purchase.line'].search([('import_id','=',x.id)])
  52. for each in purchase:
  53. suma = suma + each.product_qty
  54. x.product_qty = suma
  55. product_qty = fields.Float('Cantidad de productos', compute="_get_qty")
  56. note = fields.Char('Observación')
  57. currency_id = fields.Many2one('res.currency', 'Moneda')
  58. weight = fields.Float('Peso')
  59. @api.multi
  60. def _get_amount(self):
  61. for x in self:
  62. suma_purchase_subtotal = 0
  63. suma_purchase_taxes = 0
  64. suma_purchase_total = 0
  65. suma_expense_subtotal = 0
  66. suma_expense_taxes = 0
  67. suma_expense_total = 0
  68. invoice1 = self.env['import.purchase.line'].search([('import_id','=',x.id)])
  69. for each in invoice1:
  70. suma_purchase_subtotal = suma_purchase_subtotal + each.purchase_subtotal
  71. suma_purchase_taxes = suma_purchase_taxes + each.purchase_taxes
  72. suma_purchase_total = suma_purchase_total + each.purchase_total
  73. invoice2 = self.env['import.expense.line'].search([('import_id','=',x.id)])
  74. for item in invoice2:
  75. suma_expense_subtotal = suma_expense_subtotal + item.expense_subtotal
  76. suma_expense_taxes = suma_expense_taxes + item.expense_taxes
  77. suma_expense_total = suma_expense_total + item.expense_total
  78. x.purchases_subtotal = suma_purchase_subtotal
  79. x.purchases_taxes = suma_purchase_taxes
  80. x.purchases_total = suma_purchase_total
  81. x.expenses_subtotal = suma_expense_subtotal
  82. x.expenses_taxes = suma_expense_taxes
  83. x.expenses_total = suma_expense_total
  84. purchases_subtotal = fields.Float(string="Subtotal", compute="_get_amount")
  85. purchases_taxes = fields.Float(string="Impuestos", compute="_get_amount")
  86. purchases_total = fields.Float(string="Total", compute="_get_amount")
  87. expenses_subtotal = fields.Float(string="Subtotal", compute="_get_amount")
  88. expenses_taxes = fields.Float(string="Impuestos", compute="_get_amount")
  89. expenses_total = fields.Float(string="Total", compute="_get_amount")
  90. @api.multi
  91. def button_dummy(self):
  92. return True
  93. @api.model
  94. def approve_import_cost(self, value):
  95. approved_by = self.env.user.id
  96. import_cost = self.env['import.cost'].search([('id','=',value['import_id'])])
  97. import_cost.write({'state': 'approved','factor':value['factor'],'approved_by':approved_by})
  98. for item in value['purchases']:
  99. product = self.env['product.product'].search([('id','=',item['product_id'][0])])
  100. template = self.env['product.template'].search([('id','=',product.product_tmpl_id.id)])
  101. if(template.price_change == 'import'):
  102. template.write({'standard_price': item['import_cost']})
  103. return True
  104. @api.model
  105. def create(self,vals):
  106. ctx = dict(self._context or {})
  107. vals['name'] = self.env['ir.sequence'].get('import.cost') or '/'
  108. return super(import_cost, self.with_context(ctx)).create(vals)