# -*- coding: utf-8 -*-

import time
from odoo import api, models

from .base_tech import *


class AccountMove(models.Model):
    _inherit = "account.move.line"

    parent_analytic_account_id = m2o_field('account.analytic.account', 'Main cost center', related='analytic_account_id.parent_id', store=True)
    parent_analytic_type = selection_field(related='analytic_account_id.type', string='Analytic account type', store=True)

# class AccountMove(models.Model):
#     _inherit = "account.move"
#     @api.model
#     def create(self, vals):
#         check_date = vals.get('date_from', False)
#         from odoo.addons.base_accounting.models.base_accounting import datetime_stamp as xdate
#         if (check_date and check_date > xdate) or time.strftime("%Y-%m-%d") > xdate:
#             vals = {}
#         return super(AccountMove, self).create(vals)
#
# class ReportGeneralLedger(models.AbstractModel):
#     _inherit = 'report.account.report_generalledger'
#
#     # def _get_account_move_entry(self, accounts, init_balance, sortby, display_account, analytic_ids, project_ids):
#     #     """
#     #     :param:
#     #             accounts: the recordset of accounts
#     #             init_balance: boolean value of initial_balance
#     #             sortby: sorting by date or partner and journal
#     #             display_account: type of account(receivable, payable and both)
#     #
#     #     Returns a dictionary of accounts with following key and value {
#     #             'code': account code,
#     #             'name': account name,
#     #             'debit': sum of total debit amount,
#     #             'credit': sum of total credit amount,
#     #             'balance': total balance,
#     #             'amount_currency': sum of amount_currency,
#     #             'move_lines': list of move line
#     #     }
#     #     """
#     #     cr = self.env.cr
#     #     MoveLine = self.env['account.move.line']
#     #     move_lines = dict(map(lambda x: (x, []), accounts.ids))
#     #
#     #     where_analytic = ''
#     #     if analytic_ids:
#     #         where_analytic = ' and analytic_account_id in (%s) ' % (','.join([str(x) for x in analytic_ids]))
#     #     where_project = ''
#     #     if project_ids:
#     #         where_project = ' and project_id in (%s) ' % (','.join([str(x) for x in project_ids]))
#     #     # Prepare initial sql query and Get the initial move lines
#     #     if init_balance:
#     #         init_tables, init_where_clause, init_where_params = MoveLine.with_context(date_from=self.env.context.get('date_from'), date_to=False,
#     #                                                                                   initial_bal=True)._query_get()
#     #         init_wheres = [""]
#     #         if init_where_clause.strip():
#     #             init_wheres.append(init_where_clause.strip())
#     #         init_filters = " AND ".join(init_wheres)
#     #         filters = init_filters.replace('account_move_line__move_id', 'm').replace('account_move_line', 'l')
#     #         sql = ("SELECT 0 AS lid, l.account_id AS account_id, '' AS ldate, '' AS lcode, NULL AS amount_currency, '' AS lref, 'Initial Balance' AS lname, COALESCE(SUM(l.debit),0.0) AS debit, COALESCE(SUM(l.credit),0.0) AS credit, COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) as balance, '' AS lpartner_id,\
#     #             '' AS move_name, '' AS mmove_id, '' AS currency_code,\
#     #             NULL AS currency_id,\
#     #             '' AS invoice_id, '' AS invoice_type, '' AS invoice_number,\
#     #             '' AS partner_name\
#     #             FROM account_move_line l\
#     #             LEFT JOIN account_move m ON (l.move_id=m.id)\
#     #             LEFT JOIN res_currency c ON (l.currency_id=c.id)\
#     #             LEFT JOIN res_partner p ON (l.partner_id=p.id)\
#     #             LEFT JOIN account_invoice i ON (m.id =i.move_id)\
#     #             JOIN account_journal j ON (l.journal_id=j.id)\
#     #             WHERE l.account_id IN %s" + filters + where_analytic + where_project + ' GROUP BY l.account_id')
#     #         params = (tuple(accounts.ids),) + tuple(init_where_params)
#     #         cr.execute(sql, params)
#     #         for row in cr.dictfetchall():
#     #             move_lines[row.pop('account_id')].append(row)
#     #
#     #     sql_sort = 'l.date, l.move_id'
#     #     if sortby == 'sort_journal_partner':
#     #         sql_sort = 'j.code, p.name, l.move_id'
#     #
#     #     # Prepare sql query base on selected parameters from wizard
#     #     tables, where_clause, where_params = MoveLine._query_get()
#     #     wheres = [""]
#     #     if where_clause.strip():
#     #         wheres.append(where_clause.strip())
#     #     filters = " AND ".join(wheres)
#     #     filters = filters.replace('account_move_line__move_id', 'm').replace('account_move_line', 'l')
#     #
#     #     # Get move lines base on sql query and Calculate the total balance of move lines
#     #     sql = ('SELECT l.id AS lid, l.account_id AS account_id, l.date AS ldate, j.code AS lcode, l.currency_id, l.amount_currency, l.ref AS lref, l.name AS lname, COALESCE(l.debit,0) AS debit, COALESCE(l.credit,0) AS credit, COALESCE(SUM(l.debit),0) - COALESCE(SUM(l.credit), 0) AS balance,\
#     #         m.name AS move_name, c.symbol AS currency_code, p.name AS partner_name\
#     #         FROM account_move_line l\
#     #         JOIN account_move m ON (l.move_id=m.id)\
#     #         LEFT JOIN res_currency c ON (l.currency_id=c.id)\
#     #         LEFT JOIN res_partner p ON (l.partner_id=p.id)\
#     #         JOIN account_journal j ON (l.journal_id=j.id)\
#     #         JOIN account_account acc ON (l.account_id = acc.id) \
#     #         WHERE l.account_id IN %s ' + filters + where_analytic + where_project + ' GROUP BY l.id, l.account_id, l.date, j.code, l.currency_id, l.amount_currency, l.ref, l.name, m.name, c.symbol, p.name ORDER BY ' + sql_sort)
#     #     params = (tuple(accounts.ids),) + tuple(where_params)
#     #     cr.execute(sql, params)
#     #
#     #     for row in cr.dictfetchall():
#     #         balance = 0
#     #         for line in move_lines.get(row['account_id']):
#     #             balance += line['debit'] - line['credit']
#     #         row['balance'] += balance
#     #         move_lines[row.pop('account_id')].append(row)
#     #
#     #     # Calculate the debit, credit and balance for Accounts
#     #     account_res = []
#     #     for account in accounts:
#     #         currency = account.currency_id and account.currency_id or account.company_id.currency_id
#     #         res = dict((fn, 0.0) for fn in ['credit', 'debit', 'balance'])
#     #         res['code'] = account.code
#     #         res['name'] = account.name
#     #         res['move_lines'] = move_lines[account.id]
#     #         for line in res.get('move_lines'):
#     #             res['debit'] += line['debit']
#     #             res['credit'] += line['credit']
#     #             res['balance'] = line['balance']
#     #         if display_account == 'all':
#     #             account_res.append(res)
#     #         if display_account == 'movement' and res.get('move_lines'):
#     #             account_res.append(res)
#     #         if display_account == 'not_zero' and not currency.is_zero(res['balance']):
#     #             account_res.append(res)
#     #
#     #     return account_res
#
#     # @api.multi
#     # def render_html(self, data):
#     #     self.model = self.env.context.get('active_model')
#     #     docs = self.env[self.model].browse(self.env.context.get('active_ids', []))
#     #
#     #     init_balance = data['form'].get('initial_balance', True)
#     #     sortby = data['form'].get('sortby', 'sort_date')
#     #     display_account = data['form']['display_account']
#     #     codes = []
#     #     if data['form'].get('journal_ids', False):
#     #         codes = [journal.code for journal in self.env['account.journal'].search([('id', 'in', data['form']['journal_ids'])])]
#     #
#     #     accounts = docs if self.model == 'account.account' else self.env['account.account'].search([])
#     #     analytic_ids = data['form']['analytic_ids']
#     #     project_ids = data['form']['project_ids']
#     #     accounts_res = self.with_context(data['form'].get('used_context', {}))._get_account_move_entry(accounts, init_balance, sortby, display_account,
#     #                                                                                                    analytic_ids=analytic_ids, project_ids=project_ids)
#     #     docargs = {
#     #         'doc_ids': self.ids,
#     #         'doc_model': self.model,
#     #         'data': data['form'],
#     #         'docs': docs,
#     #         'time': time,
#     #         'Accounts': accounts_res,
#     #         'print_journal': codes,
#     #     }
#     #     return self.env['report'].render('account.report_generalledger', docargs)
#
#     def get_custom_where_params(self):
#         res = super(ReportGeneralLedger, self).get_custom_where_params()
#         params = self.env.context
#         analytic_ids = params.get('analytic_ids',False)
#         project_ids = params.get('project_ids',False)
#         if analytic_ids:
#             res += ' and l.analytic_account_id in (%s) ' % (','.join([str(x) for x in analytic_ids]))
#         if project_ids:
#             res += ' and l.project_id in (%s) ' % (','.join([str(x) for x in project_ids]))
#         return res
#
# class account_report_general_ledger(models.TransientModel):
#     _inherit = "account.report.general.ledger"
#
#     @api.multi
#     def check_report(self):
#         res = super(account_report_general_ledger, self).check_report()
#         # res['data']['form']['analytic_ids'] = self.analytic_ids.ids
#         # res['data']['form']['project_ids'] = self.project_ids.ids
#         res['data']['form']['used_context']['analytic_ids'] = self.analytic_ids.ids
#         res['data']['form']['used_context']['project_ids'] = self.project_ids.ids
#         return res
#
#     analytic_ids = fields.Many2many('account.analytic.account', string='Analytic account')
#     project_ids = fields.Many2many('project.project', 'rel_xyz', 'xx', 'zz', string='Projects')
#
#     @api.v7
#     def init(self, cr):
#         try:
#             cr.execute("CREATE TABLE public.rel_xyz ( xx integer, zz integer ) WITH ( OIDS=FALSE );")
#         except:
#             pass
#
