123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- # -*- 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 as rd
- from dateutil.parser import parse
- from operator import itemgetter
- class AccountVoucher(models.Model):
- _inherit = 'account.voucher'
- @api.model
- def create_from_pos(self, values):
- date_now = fields.Date.context_today(self)
- date_format = '%Y-%m-%d'
- # 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': 5
- }
- 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 = parse(date_now) + rd(days=days_to_due)
- invoice.write({
- 'date_invoice': date_now,
- 'date_due': due_date.strftime(date_format)
- })
- # 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))
- total_lines = []
- for line in invoice.payment_term.line_ids:
- due_date = (parse(date_now) + rd(days=line.days + line.days2)).strftime(date_format)
- if percent_paid:
- total_lines.append((date_now, round(percent_paid, decimal_precision)))
- percent_paid = 0
- if date_format == date_now:
- distribute_percent = -((total_lines[0] - line.value_amount) / (len(invoice.payment_term.line_ids) - 1))
- continue
- if line.value != 'balance':
- total_lines.append((due_date, round(line.value_amount + distribute_percent, decimal_precision)))
- continue
-
- total_lines.append((due_date, line.value_amount))
- import pdb; pdb.set_trace()
- print total_lines
- # -----------------------------------------------------------------------------------------------------
- # 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
|