|
@@ -1,59 +1,103 @@
|
|
-from openerp import api, fields, models
|
|
+
|
|
|
|
+from openerp import api, fields, models, cli
|
|
from openerp.exceptions import except_orm
|
|
from openerp.exceptions import except_orm
|
|
|
|
|
|
|
|
+from datetime import datetime
|
|
|
|
+from dateutil.relativedelta import relativedelta
|
|
|
|
+
|
|
|
|
+from operator import itemgetter
|
|
|
|
+
|
|
class AccountVoucher(models.Model):
|
|
class AccountVoucher(models.Model):
|
|
- _inherit = 'account.voucher'
|
|
+ _inherit = 'account.voucher'
|
|
|
|
|
|
@api.model
|
|
@api.model
|
|
def create_from_pos(self, values):
|
|
def create_from_pos(self, values):
|
|
-
|
|
+ date_now = fields.Date.context_today(self)
|
|
-
|
|
+
|
|
- sale_order_model = self.env['sale.order']
|
|
+
|
|
-
|
|
+ sale_order_line_values = [
|
|
-
|
|
+ [0, False, {
|
|
-
|
|
+ 'product_id': int(item['id']),
|
|
-
|
|
+ 'product_uom_qty': float(item['qty']),
|
|
-
|
|
+ 'price_unit': float(item['price']) ,
|
|
-
|
|
+ 'state': 'confirmed'
|
|
-
|
|
+ }] for item in values['cart_items']]
|
|
-
|
|
+
|
|
-
|
|
+ sale_order_values = {
|
|
-
|
|
+ 'partner_id': int(values['customer_id']),
|
|
-
|
|
+ 'order_line': sale_order_line_values,
|
|
-
|
|
+ 'picking_policy': 'direct',
|
|
-
|
|
+ 'state': 'manual',
|
|
-
|
|
+ 'date_confirm': date_now,
|
|
-
|
|
+ 'payment_term': 4
|
|
- sale_order_id = 26
|
|
+ }
|
|
-
|
|
+
|
|
-
|
|
+ sale_order = self.env['sale.order'].create(sale_order_values)
|
|
- sale_order = sale_order_model.browse([sale_order_id])
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+ invoice_id = sale_order.action_invoice_create()
|
|
-
|
|
+
|
|
-
|
|
+ invoice = self.env['account.invoice'].browse(invoice_id)
|
|
-
|
|
+
|
|
-
|
|
+ days_to_due = max(invoice.payment_term.line_ids.mapped(lambda x: x.days + x.days2))
|
|
- account_id = False
|
|
+ due_date = datetime.strptime(date_now, '%Y-%m-%d') + relativedelta(days=days_to_due)
|
|
- decimal_precision = self.env['decimal.precision'].precision_get('Product Price')
|
|
+
|
|
- account_invoice_model = self.env['account.invoice']
|
|
+ invoice.write({
|
|
-
|
|
+ 'date_invoice': date_now,
|
|
- for sale_order_line in sale_order.order_line:
|
|
+ 'date_due': due_date.strftime('%Y-%m-%d')
|
|
- account_id = sale_order_line.product_id.property_account_income.id
|
|
+ })
|
|
-
|
|
+
|
|
- if not account_id:
|
|
+
|
|
- account_id = sale_order_line.product_id.categ_id.property_account_income_categ.id
|
|
+ amount_paid = float(values['amount_paid'])
|
|
-
|
|
+ decimal_precision = self.env['decimal.precision'].precision_get('Account')
|
|
- if not account_id:
|
|
+ invoice = invoice.with_context(lang=invoice.partner_id.lang)
|
|
- raise except_orm('Error', 'Es necesario denifir una cuenta de ingresos para este producto')
|
|
+
|
|
-
|
|
+ invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice_id)
|
|
- uos_quantity = sale_order_line.product_uos_qty or 0.0 if sale_order_line.product_uos else sale_order_line.product_uom_qty
|
|
+
|
|
- uos_id = sale_order_line.product_uos.id if sale_order_line.product_uos else sale_order_line.product_uom.id
|
|
+ compute_taxes = self.env['account.invoice.tax'].compute(invoice)
|
|
- fiscal_position = sale_order_line.order_id.fiscal_position or False
|
|
+ invoice.check_tax_lines(compute_taxes)
|
|
-
|
|
+
|
|
- subtotal = round(sale_order_line.price_unit * sale_order_line.product_uom_qty / uos_quantity, decimal_precision)
|
|
+ total, total_currency, invoice_move_lines = invoice.compute_invoice_totals(invoice.company_id.currency_id, invoice.reference, invoice_move_lines)
|
|
-
|
|
+
|
|
-
|
|
+ same_currency = invoice.currency_id != invoice.company_id.currency_id
|
|
-
|
|
+ percent_paid = amount_paid / total
|
|
-
|
|
+ distribute_percent = percent_paid / len(invoice.payment_term.line_ids)
|
|
|
|
+
|
|
|
|
+ for line in invoice.payment_term.line_ids:
|
|
|
|
+ due_date = datetime.strptime('%Y-%m-%d') + relativedelta(days=line.days)
|
|
|
|
+
|
|
|
|
+ if percent_paid and due_date == date_now:
|
|
|
|
+ distribute_percent = (line.value_amount - percent_paid) / (len(invoice.payment_term.line_ids) - 1)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|