def create_vat_transactions(header, lines): vat_trans = [] for line in lines: vat_trans.append( VatTransaction( header=header.pk, line=line.pk, module="NL", ref=header.ref, period=header.period, date=header.date, field="v", tran_type=header.type, vat_type=header.vat_type, vat_code=line.vat_code, vat_rate=line.vat_code.rate, goods=line.goods, vat=line.vat ) ) vat_trans = VatTransaction.objects.bulk_create(vat_trans) vat_trans = sort_multiple(vat_trans, *[(lambda v: v.line, False)]) lines = sort_multiple(lines, *[(lambda l: l.pk, False)]) for i, line in enumerate(lines): line.vat_transaction = vat_trans[i] NominalLine.objects.bulk_update(lines, ["vat_transaction"])
def create_invoice_with_nom_entries(header, lines, vat_nominal, control_nominal): header = PurchaseHeader.objects.create(**header) lines = create_lines(header, lines) nom_trans = [] for line in lines: if line.goods: nom_trans.append( NominalTransaction(module="PL", header=header.pk, line=line.pk, nominal=line.nominal, value=line.goods, ref=header.ref, period=header.period, date=header.date, field="g", type=header.type)) if line.vat: nom_trans.append( NominalTransaction(module="PL", header=header.pk, line=line.pk, nominal=vat_nominal, value=line.vat, ref=header.ref, period=header.period, date=header.date, field="v", type=header.type)) if line.goods or line.vat: nom_trans.append( NominalTransaction(module="PL", header=header.pk, line=line.pk, nominal=control_nominal, value=-1 * (line.goods + line.vat), ref=header.ref, period=header.period, date=header.date, field="t", type=header.type)) nom_trans = NominalTransaction.objects.bulk_create(nom_trans) nom_trans = sort_multiple(nom_trans, *[(lambda n: n.line, False)]) goods_and_vat = nom_trans[:-1] for i, line in enumerate(lines): line.goods_nominal_transaction = nom_trans[3 * i] line.vat_nominal_transaction = nom_trans[(3 * i) + 1] line.total_nominal_transaction = nom_trans[(3 * i) + 2] PurchaseLine.objects.bulk_update(lines, [ "goods_nominal_transaction", "vat_nominal_transaction", "total_nominal_transaction" ]) return header
def create_nom_trans(nom_tran_cls, line_cls, header, lines, bank_nominal, vat_nominal): nom_trans = [] for line in lines: if line.goods: nom_trans.append( nom_tran_cls(module="CB", header=header.pk, line=line.pk, nominal=line.nominal, value=-1 * line.goods, ref=header.ref, period=header.period, date=header.date, field="g", type=header.type)) if line.vat: nom_trans.append( nom_tran_cls(module="CB", header=header.pk, line=line.pk, nominal=vat_nominal, value=-1 * line.vat, ref=header.ref, period=header.period, date=header.date, field="v", type=header.type)) if line.goods or line.vat: nom_trans.append( nom_tran_cls(module="CB", header=header.pk, line=line.pk, nominal=bank_nominal, value=line.goods + line.vat, ref=header.ref, period=header.period, date=header.date, field="t", type=header.type)) nom_trans = NominalTransaction.objects.bulk_create(nom_trans) nom_trans = sort_multiple(nom_trans, *[(lambda n: n.line, False)]) goods_and_vat = nom_trans[:-1] for i, line in enumerate(lines): line.goods_nominal_transaction = nom_trans[3 * i] line.vat_nominal_transaction = nom_trans[(3 * i) + 1] line.total_nominal_transaction = nom_trans[(3 * i) + 2] line_cls.objects.bulk_update(lines, [ "goods_nominal_transaction", "vat_nominal_transaction", "total_nominal_transaction" ])
def test_vat_changed_to_no_vat_code(self): self.client.force_login(self.user) create_invoice_with_nom_entries( { "type": "pi", "supplier": self.supplier, "period": self.period, "ref": self.ref, "date": self.model_date, "due_date": self.model_due_date, "total": 2400, "paid": 0, "due": 2400, "goods": 2000, "vat": 400 }, [{ 'description': self.description, 'goods': 100, 'nominal': self.nominal, 'vat_code': self.vat_code, 'vat': 20 }] * 20, self.vat_nominal, self.purchase_control) headers = PurchaseHeader.objects.all().order_by("pk") lines = PurchaseLine.objects.all().order_by("pk") self.assertEqual(len(lines), 20) create_vat_transactions(headers[0], lines) vat_transactions = VatTransaction.objects.all().order_by("line") self.assertEqual(len(vat_transactions), 20) self.assertEqual(len(headers), 1) self.assertEqual(headers[0].total, 2400) self.assertEqual(headers[0].paid, 0) self.assertEqual(headers[0].due, 2400) nom_trans = NominalTransaction.objects.all() self.assertEqual(len(nom_trans), 20 + 20 + 20) nom_trans = sort_multiple(nom_trans, *[(lambda n: n.pk, False)]) header = headers[0] for i, line in enumerate(lines): self.assertEqual(line.line_no, i + 1) self.assertEqual(line.header, header) self.assertEqual(line.description, self.description) self.assertEqual(line.goods, 100) self.assertEqual(line.nominal, self.nominal) self.assertEqual(line.vat_code, self.vat_code) self.assertEqual(line.vat, 20) self.assertEqual(line.goods_nominal_transaction, nom_trans[3 * i]) self.assertEqual(line.vat_nominal_transaction, nom_trans[(3 * i) + 1]) self.assertEqual(line.total_nominal_transaction, nom_trans[(3 * i) + 2]) self.assertEqual(line.vat_transaction, vat_transactions[i]) for i, vat_tran in enumerate(vat_transactions): self.assertEqual(vat_tran.header, header.pk) self.assertEqual(vat_tran.line, lines[i].pk) self.assertEqual(vat_tran.module, "PL") self.assertEqual(vat_tran.ref, header.ref) self.assertEqual(vat_tran.period, header.period) self.assertEqual(vat_tran.date, header.date) self.assertEqual(vat_tran.field, "v") self.assertEqual(vat_tran.tran_type, header.type) self.assertEqual(vat_tran.vat_type, "i") self.assertEqual(vat_tran.vat_code, lines[i].vat_code) self.assertEqual(vat_tran.vat_rate, lines[i].vat_code.rate) self.assertEqual(vat_tran.goods, lines[i].goods) self.assertEqual(vat_tran.vat, lines[i].vat) self.assertEqual(header.goods, sum(vat_tran.goods for vat_tran in vat_transactions)) self.assertEqual(header.vat, sum(vat_tran.vat for vat_tran in vat_transactions)) goods_nom_trans = nom_trans[::3] vat_nom_trans = nom_trans[1::3] total_nom_trans = nom_trans[2::3] for i, tran in enumerate(goods_nom_trans): self.assertEqual(tran.value, 100) self.assertEqual(tran.nominal, self.nominal) self.assertEqual(tran.field, "g") self.assertEqual(lines[i].goods_nominal_transaction, tran) for i, tran in enumerate(vat_nom_trans): self.assertEqual(tran.value, 20) self.assertEqual(tran.nominal, self.vat_nominal) self.assertEqual(tran.field, "v") self.assertEqual(lines[i].vat_nominal_transaction, tran) for i, tran in enumerate(total_nom_trans): self.assertEqual(tran.value, -120) self.assertEqual(tran.nominal, self.purchase_control) self.assertEqual(tran.field, "t") self.assertEqual(lines[i].total_nominal_transaction, tran) matches = PurchaseMatching.objects.all() self.assertEqual(len(matches), 0) data = {} header_data = create_header( HEADER_FORM_PREFIX, { "type": header.type, "supplier": header.supplier.pk, "period": header.period.pk, "ref": header.ref, "date": header.date.strftime(DATE_INPUT_FORMAT), "due_date": header.due_date.strftime(DATE_INPUT_FORMAT), "total": header.total }) data.update(header_data) lines_as_dicts = [to_dict(line) for line in lines] line_trans = [ get_fields( line, ['id', 'description', 'goods', 'nominal', 'vat_code', 'vat']) for line in lines_as_dicts ] line_forms = line_trans for form in line_forms: form["vat_code"] = "" line_data = create_formset_data(LINE_FORM_PREFIX, line_forms) line_data["line-INITIAL_FORMS"] = 20 data.update(line_data) matching_data = create_formset_data(match_form_prefix, []) data.update(matching_data) url = reverse("purchases:edit", kwargs={"pk": headers[0].pk}) response = self.client.post(url, data) headers = PurchaseHeader.objects.all() self.assertEqual(len(headers), 1) self.assertEqual(headers[0].total, 2400) self.assertEqual(headers[0].paid, 0) self.assertEqual(headers[0].due, 2400) nom_trans = NominalTransaction.objects.all() nom_trans = sort_multiple(nom_trans, *[(lambda n: n.pk, False)]) self.assertEqual(len(nom_trans), 20 + 20 + 20) header = headers[0] lines = (PurchaseLine.objects.select_related( "vat_code").all().order_by("pk")) self.assertEqual(len(lines), 20) vat_transactions = VatTransaction.objects.all().order_by("line") self.assertEqual(len(vat_transactions), 0) lines = list(lines) for i, line in enumerate(lines): self.assertEqual(line.line_no, i + 1) self.assertEqual(line.header, header) self.assertEqual(line.description, self.description) self.assertEqual(line.goods, 100) self.assertEqual(line.nominal, self.nominal) self.assertEqual(line.vat_code, None) self.assertEqual(line.vat, 20) self.assertEqual(line.goods_nominal_transaction, nom_trans[3 * i]) self.assertEqual(line.vat_nominal_transaction, nom_trans[(3 * i) + 1]) self.assertEqual(line.total_nominal_transaction, nom_trans[(3 * i) + 2]) self.assertEqual(line.vat_transaction, None) for tran in goods_nom_trans: self.assertEqual(tran.value, 100) self.assertEqual(tran.nominal, self.nominal) self.assertEqual(tran.field, "g") for tran in vat_nom_trans: self.assertEqual(tran.value, 20) self.assertEqual(tran.nominal, self.vat_nominal) self.assertEqual(tran.field, "v") for tran in total_nom_trans: self.assertEqual(tran.value, -1 * 120) self.assertEqual(tran.nominal, self.purchase_control) self.assertEqual(tran.field, "t") matches = PurchaseMatching.objects.all() self.assertEqual(len(matches), 0) total = 0 for tran in nom_trans: total = total + tran.value self.assertEqual(total, 0)