|
@@ -1,59 +1,103 @@
|
|
|
-from openerp import api, fields, models
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+from openerp import api, fields, models, cli
|
|
|
from openerp.exceptions import except_orm
|
|
|
|
|
|
+from datetime import datetime
|
|
|
+from dateutil.relativedelta import relativedelta
|
|
|
+
|
|
|
+from operator import itemgetter
|
|
|
+
|
|
|
class AccountVoucher(models.Model):
|
|
|
- _inherit = 'account.voucher'
|
|
|
+ _inherit = 'account.voucher'
|
|
|
|
|
|
@api.model
|
|
|
def create_from_pos(self, values):
|
|
|
-
|
|
|
- # Activity 1: Create Sale Order
|
|
|
- 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'])
|
|
|
- # }] for item in values['cart_items']]
|
|
|
-
|
|
|
- # sale_order_values = {
|
|
|
- # 'partner_id': int(values['customer_id']),
|
|
|
- # 'order_line': sale_order_line_values,
|
|
|
- # 'picking_policy': 'direct'
|
|
|
- # }
|
|
|
-
|
|
|
- # sale_order_id = sale_order_model.create(sale_order_values)
|
|
|
- sale_order_id = 26
|
|
|
-
|
|
|
- # Activity 2: Confirm Sale Order
|
|
|
- sale_order = sale_order_model.browse([sale_order_id])
|
|
|
- # sale_order.write({ 'state': 'manual' })
|
|
|
-
|
|
|
- # for sale_order_line in sale_order.order_line:
|
|
|
- # sale_order_line.write({ 'state': 'confirmed' })
|
|
|
-
|
|
|
- # Activity 3: Create Invoice
|
|
|
- account_id = False
|
|
|
- decimal_precision = self.env['decimal.precision'].precision_get('Product Price')
|
|
|
- account_invoice_model = self.env['account.invoice']
|
|
|
-
|
|
|
- for sale_order_line in sale_order.order_line:
|
|
|
- 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
|
|
|
-
|
|
|
- if not account_id:
|
|
|
- raise except_orm('Error', 'Es necesario denifir una cuenta de ingresos para este producto')
|
|
|
-
|
|
|
- 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
|
|
|
- fiscal_position = sale_order_line.order_id.fiscal_position or False
|
|
|
-
|
|
|
- subtotal = round(sale_order_line.price_unit * sale_order_line.product_uom_qty / uos_quantity, decimal_precision)
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ date_now = fields.Date.context_today(self)
|
|
|
+
|
|
|
+ # Step 1: Create Sale Order and Confirm
|
|
|
+ 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 = self.env['sale.order'].create(sale_order_values)
|
|
|
+
|
|
|
+ # Step 2: Create Account Invoice and calculate due_date
|
|
|
+ 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))
|
|
|
+ due_date = datetime.strptime(date_now, '%Y-%m-%d') + relativedelta(days=days_to_due)
|
|
|
+
|
|
|
+ invoice.write({
|
|
|
+ 'date_invoice': date_now,
|
|
|
+ 'date_due': due_date.strftime('%Y-%m-%d')
|
|
|
+ })
|
|
|
+
|
|
|
+ # Step 3: Create invoice move lines
|
|
|
+ amount_paid = float(values['amount_paid'])
|
|
|
+ decimal_precision = self.env['decimal.precision'].precision_get('Account')
|
|
|
+ invoice = invoice.with_context(lang=invoice.partner_id.lang)
|
|
|
+
|
|
|
+ invoice_move_lines = invoice._get_analytic_lines() + self.env['account.invoice.tax'].move_line_get(invoice_id)
|
|
|
+
|
|
|
+ compute_taxes = self.env['account.invoice.tax'].compute(invoice)
|
|
|
+ invoice.check_tax_lines(compute_taxes)
|
|
|
+
|
|
|
+ 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)
|
|
|
+
|
|
|
+# -----------------------------------------------------------------------------------------------------
|
|
|
+# def compute(self, cr, uid, id, value, date_ref=False, context=None):
|
|
|
+# if not date_ref:
|
|
|
+# date_ref = datetime.now().strftime('%Y-%m-%d')
|
|
|
+# pt = self.browse(cr, uid, id, context=context)
|
|
|
+# amount = value
|
|
|
+# result = []
|
|
|
+# obj_precision = self.pool.get('decimal.precision')
|
|
|
+# prec = obj_precision.precision_get(cr, uid, 'Account')
|
|
|
+# for line in pt.line_ids:
|
|
|
+# if line.value == 'fixed':
|
|
|
+# amt = round(line.value_amount, prec)
|
|
|
+# elif line.value == 'procent':
|
|
|
+# amt = round(value * line.value_amount, prec)
|
|
|
+# elif line.value == 'balance':
|
|
|
+# amt = round(amount, prec)
|
|
|
+# if amt:
|
|
|
+# next_date = (datetime.strptime(date_ref, '%Y-%m-%d') + relativedelta(days=line.days))
|
|
|
+# if line.days2 < 0:
|
|
|
+# next_first_date = next_date + relativedelta(day=1,months=1) #Getting 1st of next month
|
|
|
+# next_date = next_first_date + relativedelta(days=line.days2)
|
|
|
+# if line.days2 > 0:
|
|
|
+# next_date += relativedelta(day=line.days2, months=1)
|
|
|
+# result.append( (next_date.strftime('%Y-%m-%d'), amt) )
|
|
|
+# amount -= amt
|
|
|
+
|
|
|
+# amount = reduce(lambda x,y: x+y[1], result, 0.0)
|
|
|
+# dist = round(value-amount, prec)
|
|
|
+# if dist:
|
|
|
+# result.append( (time.strftime('%Y-%m-%d'), dist) )
|
|
|
+# return result
|
|
|
|