Skip to content

Commit

Permalink
Merge pull request #82 from Som-Energia/IMP_extend_fraccionament_extr…
Browse files Browse the repository at this point in the history
…aline_wiz
  • Loading branch information
susu105 committed Jan 11, 2022
2 parents 80cf4c1 + 3954856 commit 2eecf2f
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 112 deletions.
3 changes: 2 additions & 1 deletion giscedata_facturacio_som/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import giscedata_facturacio
import giscedata_facturacio
import wizard
4 changes: 3 additions & 1 deletion giscedata_facturacio_som/__terp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
],
"init_xml": [],
"demo_xml": [],
"update_xml":[],
"update_xml":[
"wizard/wizard_fraccionar_via_extralines_view.xml",
],
"active": False,
"installable": True
}
110 changes: 2 additions & 108 deletions giscedata_facturacio_som/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,108 +1,2 @@
# -*- coding: utf-8 -*-

from destral import testing
from destral.transaction import Transaction
from tools.misc import cache


class TestGiscedataFacturacioSom(testing.OOTestCaseWithCursor):
def setUp(self):
self.gff_obj = self.openerp.pool.get('giscedata.facturacio.factura')
super(TestGiscedataFacturacioSom, self).setUp()
gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', '=', False)], limit=2)
self.gff_obj.write(self.cursor, self.uid, gff_ids[0], {'origin': 'sample_origin1'})
self.gff_obj.write(self.cursor, self.uid, gff_ids[1], {'origin': 'sample_origin2'})

def set_account_invoice_number_cerca_exacte(self, cursor, uid, value):
res_obj = self.openerp.pool.get('res.config')
cache.clean_caches_for_db(cursor.dbname)
res_obj.set(cursor, uid, 'account_invoice_number_cerca_exacte', value)

def test_search_withPercentage_active(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00%')])

self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_active(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '0046/F')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_active(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00')])

self.assertEqual(len(gff_ids), 0)

def test_search_withPercentage_disabled(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00%')])

self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_disabled(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '0046/F')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_disabled(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00')])

self.assertGreater(len(gff_ids), 1)


def set_invoice_origin_cerca_exacte(self, cursor, uid, value):
res_obj = self.openerp.pool.get('res.config')
cache.clean_caches_for_db(cursor.dbname)
res_obj.set(cursor, uid, 'invoice_origin_cerca_exacte', value)

def test_search_withPercentage_active(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin%')])
self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_active(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin1')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_active(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin')])

self.assertEqual(len(gff_ids), 0)

def test_search_withPercentage_disabled(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin%')])

self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_disabled(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin1')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_disabled(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin')])

self.assertGreater(len(gff_ids), 1)
from tests_wizard_fraccionar_via_extralines import *
from tests_giscedata_facturacio_som import *
106 changes: 106 additions & 0 deletions giscedata_facturacio_som/tests/tests_giscedata_facturacio_som.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-

from destral import testing
from tools.misc import cache

class TestGiscedataFacturacioSom(testing.OOTestCaseWithCursor):
def setUp(self):
self.gff_obj = self.openerp.pool.get('giscedata.facturacio.factura')
super(TestGiscedataFacturacioSom, self).setUp()
gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', '=', False)], limit=2)
self.gff_obj.write(self.cursor, self.uid, gff_ids[0], {'origin': 'sample_origin1'})
self.gff_obj.write(self.cursor, self.uid, gff_ids[1], {'origin': 'sample_origin2'})

def set_account_invoice_number_cerca_exacte(self, cursor, uid, value):
res_obj = self.openerp.pool.get('res.config')
cache.clean_caches_for_db(cursor.dbname)
res_obj.set(cursor, uid, 'account_invoice_number_cerca_exacte', value)

def test_search_withPercentage_active(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00%')])

self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_active(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '0046/F')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_active(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00')])

self.assertEqual(len(gff_ids), 0)

def test_search_withPercentage_disabled(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00%')])

self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_disabled(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '0046/F')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_disabled(self):
self.set_account_invoice_number_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('number', 'ilike', '00')])

self.assertGreater(len(gff_ids), 1)


def set_invoice_origin_cerca_exacte(self, cursor, uid, value):
res_obj = self.openerp.pool.get('res.config')
cache.clean_caches_for_db(cursor.dbname)
res_obj.set(cursor, uid, 'invoice_origin_cerca_exacte', value)

def test_search_withPercentage_active(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin%')])
self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_active(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin1')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_active(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '1')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin')])

self.assertEqual(len(gff_ids), 0)

def test_search_withPercentage_disabled(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin%')])

self.assertGreater(len(gff_ids), 1)

def test_search_exactExist_disabled(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin1')])

self.assertEqual(len(gff_ids), 1)

def test_search_exactNotExist_disabled(self):
self.set_invoice_origin_cerca_exacte(self.cursor, self.uid, '0')

gff_ids = self.gff_obj.search(self.cursor, self.uid, [('origin', 'ilike', 'sample_origin')])

self.assertGreater(len(gff_ids), 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
from destral import testing
import mock

class TestWizardFraccionarViaExtralines(testing.OOTestCaseWithCursor):

def load_models(self, cursor, uid):
self.fact_obj = self.openerp.pool.get('giscedata.facturacio.factura')
self.wiz_obj = self.openerp.pool.get('wizard.fraccionar.via.extralines')
self.imd_obj = self.openerp.pool.get('ir.model.data')
self.factura_id = self.imd_obj.get_object_reference(
cursor, uid, 'giscedata_facturacio', 'factura_0001'
)[1]

@mock.patch("giscedata_facturacio.giscedata_facturacio.GiscedataFacturacioFactura.fraccionar_via_extralines")
def test_action_fraccionar_via_extralines_with_first_term_payment(self, fraccionar_via_extraline_mock):
cursor = self.cursor
uid = self.uid
self.load_models(cursor, uid)
values = {
'first_term_payment':True,
'ntermes': 4,
}
context = {'active_ids': [self.factura_id]}
wiz_id = self.wiz_obj.create(cursor, uid, values, context)
wizard = self.wiz_obj.browse(cursor, uid, wiz_id)
self.wiz_obj.action_fraccionar_via_extralines(cursor, uid, [wiz_id], context)

factura = self.fact_obj.browse(cursor, uid, self.factura_id)
expected_total = (3/4.)*(factura.amount_total)
fraccionar_via_extraline_mock.assert_called_with(mock.ANY, mock.ANY, factura.id, values['ntermes']-1,
wizard.data_inici, mock.ANY, amount=expected_total, context=context,
journal_id=wizard.journal_id.id)

@mock.patch("giscedata_facturacio.giscedata_facturacio.GiscedataFacturacioFactura.fraccionar_via_extralines")
def test_action_fraccionar_via_extralines_without_first_term_payment(self, fraccionar_via_extraline_mock):
cursor = self.cursor
uid = self.uid
self.load_models(cursor, uid)
values = {
'first_term_payment':False,
'ntermes': 4,
}
context = {'active_ids': [self.factura_id]}
wiz_id = self.wiz_obj.create(cursor, uid, values, context)
wizard = self.wiz_obj.browse(cursor, uid, wiz_id)
self.wiz_obj.action_fraccionar_via_extralines(cursor, uid, [wiz_id], context)

factura = self.fact_obj.browse(cursor, uid, self.factura_id)
expected_total = factura.amount_total
fraccionar_via_extraline_mock.assert_called_with(mock.ANY, mock.ANY, factura.id, values['ntermes'],
wizard.data_inici, mock.ANY, context=context,
journal_id=wizard.journal_id.id)
1 change: 1 addition & 0 deletions giscedata_facturacio_som/wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import wizard_fraccionar_via_extralines
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
from osv import osv, fields
from tools.translate import _
from datetime import timedelta, datetime
from osv.osv import TransactionExecute

class WizardFraccionarViaExtralines(osv.osv_memory):

_name = 'wizard.fraccionar.via.extralines'
_inherit = 'wizard.fraccionar.via.extralines'

def action_fraccionar_via_extralines(self, cursor, uid, ids, context=None):
if context is None:
context = {}

wiz = self.browse(cursor, uid, ids)[0]

if not wiz.first_term_payment:
return super(WizardFraccionarViaExtralines, self).action_fraccionar_via_extralines(cursor, uid, ids, context)

factura_ids = context.get("active_ids")
factura_o = self.pool.get("giscedata.facturacio.factura")
user_o = self.pool.get("res.users")
data_final = (datetime.strptime(wiz.data_inici, "%Y-%m-%d") + timedelta(days=365*10)).strftime("%Y-%m-%d")

msgs = []
has_errors = False
for info in factura_o.read(cursor, uid, factura_ids, ['number']):
base_res = _(u"Resultat '{0}' (ID {1}):\n").format(info['number'], info['id'])
try:
factura_teo = TransactionExecute(cursor.dbname, uid, 'giscedata.facturacio.factura')
factura = factura_o.browse(cursor, uid, info['id'])
if factura.residual != factura.amount_total:
raise Exception("La factura té un pagament parcial")
ntermes = wiz.ntermes-1
amount = (factura.amount_total / wiz.ntermes) * ntermes
factura_teo.fraccionar_via_extralines(
info['id'], ntermes, wiz.data_inici,
data_final, journal_id=wiz.journal_id.id,
amount=amount, context=context
)
comment_head = '{} ({}): Fraccionament extralines en {} quotes.\n'.format(
datetime.now().strftime("%Y-%m-%d"),
''.join([word[0] for word in user_o.read(cursor, uid, uid, ['name'])['name'].split(' ')]),
wiz.ntermes
)
old_comment = factura.comment or ''
new_comment = comment_head + old_comment
factura_teo.write(info['id'], {'comment': new_comment})

except Exception as e:
has_errors = True
if hasattr(e, "value"):
res = e.value
else:
res = str(e)
msgs.append(base_res+res)

if has_errors:
msgs = [_(u"Hi ha hagut problemes al processar {0} factures: ").format(len(msgs))] + msgs
msg = "\n\n".join(msgs)
else:
msg = _(u"S'han processat totes les factures correctament.")

msg = _(u"Número de factures processades: {0}.\n\n").format(len(factura_ids)) + msg

final_state = 'end' if not has_errors else 'error'
wiz.write({'info': msg, 'state': final_state})
return True

_columns = {
'first_term_payment': fields.boolean("Carregar primer termini a la factura actual", required=True),
}

_defaults = {
'first_term_payment': lambda *a: True,
'data_inici': lambda *a: (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d"),
}


WizardFraccionarViaExtralines()
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_wizard_fraccionar_via_extralines" model="ir.ui.view">
<field name="name">wizard.fraccionar.via.extralines.view.form</field>
<field name="model">wizard.fraccionar.via.extralines</field>
<field name="type">form</field>
<field name="inherit_id" ref="giscedata_facturacio.view_wizard_fraccionar_via_extralines"/>
<field name="arch" type="xml">
<field name="ntermes" position="after">
<field name="first_term_payment"/>
</field>
</field>
</record>
</data>
</openerp>
Loading

0 comments on commit 2eecf2f

Please sign in to comment.