From 4fd65ce280f27679ebcea2f96bfa5b33e1e3b196 Mon Sep 17 00:00:00 2001
From: sks444 <krishnasingh.ss30@gmail.com>
Date: Mon, 14 Jun 2021 17:04:51 +0530
Subject: [PATCH] Fix migrations and editing

---
 .../projects/migrations/0036_add_vendor.py    | 42 ++++++------
 hypha/apply/projects/models/vendor.py         | 10 +--
 .../includes/supporting_documents.html        |  2 +-
 hypha/apply/projects/views/vendor.py          | 66 ++++++++++++-------
 4 files changed, 71 insertions(+), 49 deletions(-)

diff --git a/hypha/apply/projects/migrations/0036_add_vendor.py b/hypha/apply/projects/migrations/0036_add_vendor.py
index 2a7583005..162e2249a 100644
--- a/hypha/apply/projects/migrations/0036_add_vendor.py
+++ b/hypha/apply/projects/migrations/0036_add_vendor.py
@@ -1,4 +1,4 @@
-# Generated by Django 2.2.24 on 2021-06-14 07:09
+# Generated by Django 2.2.24 on 2021-06-14 11:33
 
 from django.conf import settings
 import django.core.files.storage
@@ -56,37 +56,37 @@ class Migration(migrations.Migration):
                 ('contractor_name_label', models.TextField(default="2. What is the individual's name who is signing the contract?", verbose_name='label')),
                 ('contractor_name_help_text', wagtail.core.fields.RichTextField(blank=True, default='This person is is authorised to sign contract on behalf of the person or organization named above.', verbose_name='help text')),
                 ('type_label', models.TextField(default='3. Is the bank account owned by the person or organisation in the Question 1 above?', verbose_name='label')),
-                ('type_help_text', wagtail.core.fields.RichTextField(default='The name of the bank account must be the same as on the contract.', verbose_name='help_text')),
+                ('type_help_text', wagtail.core.fields.RichTextField(blank=True, default='The name of the bank account must be the same as on the contract.', verbose_name='help_text')),
                 ('required_to_pay_taxes_label', models.TextField(default='Is the organisation required to pay US taxes?', verbose_name='label')),
                 ('required_to_pay_taxes_help_text', wagtail.core.fields.RichTextField(blank=True, default='', verbose_name='help_text')),
                 ('due_diligence_documents_label', models.TextField(default='Due Diligence Documents', verbose_name='label')),
-                ('due_diligence_documents_help_text', wagtail.core.fields.RichTextField(default='Upload Due Diligence Documents. E.g. w8/w9 forms.', verbose_name='help_text')),
+                ('due_diligence_documents_help_text', wagtail.core.fields.RichTextField(blank=True, default='Upload Due Diligence Documents. E.g. w8/w9 forms.', verbose_name='help_text')),
                 ('account_holder_name_label', models.TextField(default='Bank Account Holder name', verbose_name='label')),
-                ('account_holder_name_help_text', wagtail.core.fields.RichTextField(default='This name must be same as the person or organisation that signed the contract. This person is authorised to sign contracts on behalf of the person or organisation named above.', verbose_name='help_text')),
+                ('account_holder_name_help_text', wagtail.core.fields.RichTextField(blank=True, default='This name must be same as the person or organisation that signed the contract. This person is authorised to sign contracts on behalf of the person or organisation named above.', verbose_name='help_text')),
                 ('account_routing_number_label', models.TextField(default='Bank Account Routing number', verbose_name='label')),
-                ('account_routing_number_help_text', wagtail.core.fields.RichTextField(default='Depending on your country, this might be called the ACH, SWIFT, BIC or ABA number.', verbose_name='help_text')),
+                ('account_routing_number_help_text', wagtail.core.fields.RichTextField(blank=True, default='Depending on your country, this might be called the ACH, SWIFT, BIC or ABA number.', verbose_name='help_text')),
                 ('account_number_label', models.TextField(default='Bank Account Number', verbose_name='label')),
-                ('account_number_help_text', wagtail.core.fields.RichTextField(default='Depending on your country, this might be called the account number, IBAN, or BBAN number.', verbose_name='help_text')),
+                ('account_number_help_text', wagtail.core.fields.RichTextField(blank=True, default='Depending on your country, this might be called the account number, IBAN, or BBAN number.', verbose_name='help_text')),
                 ('account_currency_label', models.TextField(default='Bank Account Currency', verbose_name='label')),
-                ('account_currency_help_text', wagtail.core.fields.RichTextField(default='This is the currency of this bank account.', verbose_name='label')),
+                ('account_currency_help_text', wagtail.core.fields.RichTextField(blank=True, default='This is the currency of this bank account.', verbose_name='label')),
                 ('need_extra_info_label', models.TextField(default='Do you need to provide us with extra information?', verbose_name='label')),
-                ('need_extra_info_help_text', wagtail.core.fields.RichTextField(default='', verbose_name='help_text')),
+                ('need_extra_info_help_text', wagtail.core.fields.RichTextField(blank=True, default='', verbose_name='help_text')),
                 ('branch_address_label', models.TextField(default='Bank Account Branch Address', verbose_name='label')),
-                ('branch_address_help_text', models.TextField(default='The address of the bank branch where you have the bank account located(not the bank account holder address)', verbose_name='help_text')),
-                ('ib_account_routing_number_label', models.TextField(default='Bank Account Routing Number', verbose_name='label')),
-                ('ib_account_routing_number_help_text', wagtail.core.fields.RichTextField(default='Depending on your country, this might be called ACH, SWIFT, BIC or ABA number', verbose_name='help_text')),
-                ('ib_account_number_label', models.TextField(default='Bank Account Number', verbose_name='label')),
-                ('ib_account_number_help_text', wagtail.core.fields.RichTextField(default='Depending on your country, this might be called the account number, IBAN, or BBAN number', verbose_name='help_text')),
-                ('ib_account_currency_label', models.TextField(default='Bank Account Currency', verbose_name='label')),
-                ('ib_account_currency_help_text', wagtail.core.fields.RichTextField(default='This is the currency of this bank account', verbose_name='help_text')),
+                ('branch_address_help_text', models.TextField(blank=True, default='The address of the bank branch where you have the bank account located(not the bank account holder address)', verbose_name='help_text')),
+                ('ib_account_routing_number_label', models.TextField(default='Intermediary Bank Account Routing Number', verbose_name='label')),
+                ('ib_account_routing_number_help_text', wagtail.core.fields.RichTextField(blank=True, default='Depending on your country, this might be called ACH, SWIFT, BIC or ABA number', verbose_name='help_text')),
+                ('ib_account_number_label', models.TextField(default='Intermediary Bank Account Number', verbose_name='label')),
+                ('ib_account_number_help_text', wagtail.core.fields.RichTextField(blank=True, default='Depending on your country, this might be called the account number, IBAN, or BBAN number', verbose_name='help_text')),
+                ('ib_account_currency_label', models.TextField(default='Intermediary Bank Account Currency', verbose_name='label')),
+                ('ib_account_currency_help_text', wagtail.core.fields.RichTextField(blank=True, default='This is the currency of this bank account', verbose_name='help_text')),
                 ('ib_branch_address_label', models.TextField(default='Intermediary Bank Branch Address', verbose_name='label')),
-                ('ib_branch_address_help_text', wagtail.core.fields.RichTextField(default='Bank branch address(not the bank account holder address)', verbose_name='help_text')),
-                ('nid_type_label', models.TextField(default='National Identity Document Type', verbose_name='label')),
-                ('nid_type_help_text', wagtail.core.fields.RichTextField(default='This could be a passport, a National Identity number, or other national identity document.', verbose_name='help_text')),
-                ('nid_number_label', models.TextField(default='National Identity Document Number', verbose_name='label')),
-                ('nid_number_help_text', wagtail.core.fields.RichTextField(default='', verbose_name='help_text')),
+                ('ib_branch_address_help_text', wagtail.core.fields.RichTextField(blank=True, default='Bank branch address(not the bank account holder address)', verbose_name='help_text')),
+                ('nid_type_label', models.TextField(default='Account Holder National Identity Document Type', verbose_name='label')),
+                ('nid_type_help_text', wagtail.core.fields.RichTextField(blank=True, default='This could be a passport, a National Identity number, or other national identity document.', verbose_name='help_text')),
+                ('nid_number_label', models.TextField(default='Account Holder National Identity Document Number', verbose_name='label')),
+                ('nid_number_help_text', wagtail.core.fields.RichTextField(blank=True, default='', verbose_name='help_text')),
                 ('other_info_label', models.TextField(default='Other Information', verbose_name='label')),
-                ('other_info_help_text', wagtail.core.fields.RichTextField(default='If you need to include other information not listed above, provide it here.', verbose_name='help_text')),
+                ('other_info_help_text', wagtail.core.fields.RichTextField(blank=True, default='If you need to include other information not listed above, provide it here.', verbose_name='help_text')),
                 ('site', models.OneToOneField(editable=False, on_delete=django.db.models.deletion.CASCADE, to='wagtailcore.Site')),
             ],
             options={
diff --git a/hypha/apply/projects/models/vendor.py b/hypha/apply/projects/models/vendor.py
index f33a11dac..43385b512 100644
--- a/hypha/apply/projects/models/vendor.py
+++ b/hypha/apply/projects/models/vendor.py
@@ -185,7 +185,7 @@ class VendorFormSettings(BaseSetting):
     )
     ib_account_routing_number_label = models.TextField(
         'label',
-        default='Bank Account Routing Number'
+        default='Intermediary Bank Account Routing Number'
     )
     ib_account_routing_number_help_text = RichTextField(
         'help_text',
@@ -194,7 +194,7 @@ class VendorFormSettings(BaseSetting):
     )
     ib_account_number_label = models.TextField(
         'label',
-        default='Bank Account Number'
+        default='Intermediary Bank Account Number'
     )
     ib_account_number_help_text = RichTextField(
         'help_text',
@@ -203,7 +203,7 @@ class VendorFormSettings(BaseSetting):
     )
     ib_account_currency_label = models.TextField(
         'label',
-        default='Bank Account Currency'
+        default='Intermediary Bank Account Currency'
     )
     ib_account_currency_help_text = RichTextField(
         'help_text',
@@ -221,7 +221,7 @@ class VendorFormSettings(BaseSetting):
     )
     nid_type_label = models.TextField(
         'label',
-        default='National Identity Document Type'
+        default='Account Holder National Identity Document Type'
     )
     nid_type_help_text = RichTextField(
         'help_text',
@@ -231,7 +231,7 @@ class VendorFormSettings(BaseSetting):
     )
     nid_number_label = models.TextField(
         'label',
-        default='National Identity Document Number'
+        default='Account Holder National Identity Document Number'
     )
     nid_number_help_text = RichTextField(
         'help_text',
diff --git a/hypha/apply/projects/templates/application_projects/includes/supporting_documents.html b/hypha/apply/projects/templates/application_projects/includes/supporting_documents.html
index f8b7a96c3..089df603a 100644
--- a/hypha/apply/projects/templates/application_projects/includes/supporting_documents.html
+++ b/hypha/apply/projects/templates/application_projects/includes/supporting_documents.html
@@ -26,7 +26,7 @@
                 <p class="docs-block__title">Contractor Setup Form</p>
             </div>
             <div class="docs-block__row-inner">
-                <a class="docs-block__link" href="{% url 'apply:projects:vendor' pk=project.pk %}">Add info</a>
+                <a class="docs-block__link" href="{% url 'apply:projects:vendor' pk=project.pk %}">Update info</a>
             </div>
         </li>
 
diff --git a/hypha/apply/projects/views/vendor.py b/hypha/apply/projects/views/vendor.py
index 41c835cc6..c21906992 100644
--- a/hypha/apply/projects/views/vendor.py
+++ b/hypha/apply/projects/views/vendor.py
@@ -7,7 +7,7 @@ from wagtail.core.models import Site
 from formtools.wizard.views import SessionWizardView
 from hypha.apply.utils.storage import PrivateStorage
 
-from ..models import Project, Vendor, DueDiligenceDocument, BankInformation
+from ..models import Project, DueDiligenceDocument, BankInformation
 from ..forms import (
     CreateVendorFormStep1,
     CreateVendorFormStep2,
@@ -56,28 +56,50 @@ class CreateVendorView(VendorAccessMixin, SessionWizardView):
         cleaned_data = self.get_all_cleaned_data()
         vendor = vendor_project.vendor
         need_extra_info = cleaned_data['need_extra_info']
-        bank_information = BankInformation.objects.create(
-            account_holder_name=cleaned_data['account_holder_name'],
-            account_routing_number=cleaned_data['account_routing_number'],
-            account_number=cleaned_data['account_number'],
-            account_currency=cleaned_data['account_currency'],
-            need_extra_info=need_extra_info,
-        )
-        if need_extra_info:
-            intermediary_bank_information = BankInformation.objects.create(
-                account_routing_number=cleaned_data['ib_account_routing_number'],
-                account_number=cleaned_data['ib_account_number'],
-                account_currency=cleaned_data['ib_account_currency'],
-                # branch_address=cleaned_data['ib_branch_address']
+        bank_info = vendor.bank_info
+        account_holder_name = cleaned_data['account_holder_name']
+        account_routing_number = cleaned_data['account_routing_number']
+        account_number = cleaned_data['account_number']
+        account_currency = cleaned_data['account_currency']
+        if not bank_info:
+            bank_info = BankInformation.objects.create(
+                account_holder_name=account_holder_name,
+                account_number=account_number,
+                account_routing_number=account_routing_number,
+                account_currency=account_currency,
+                need_extra_info=need_extra_info,
             )
-            bank_information.branch_address = cleaned_data['branch_address']
-            bank_information.nid_type = cleaned_data['nid_type']
-            bank_information.nid_number = cleaned_data['nid_number']
-            bank_information.iba_info = intermediary_bank_information
-            bank_information.save()
-
-        vendor.bank_info = bank_information
-        vendor.other_info = cleaned_data['other_info']
+        else:
+            bank_info.account_holder_name = account_holder_name
+            bank_info.account_number = account_number
+            bank_info.account_currency = account_currency
+            bank_info.need_extra_info = need_extra_info
+        if need_extra_info:
+            ib_account_routing_number = cleaned_data['ib_account_routing_number']
+            ib_account_number = cleaned_data['ib_account_number']
+            ib_account_currency = cleaned_data['ib_account_currency']
+            # branch_address=cleaned_data['ib_branch_address']
+            iba_info = bank_info.iba_info
+            if not iba_info:
+                iba_info = BankInformation.objects.create(
+                    account_routing_number=ib_account_routing_number,
+                    account_number=ib_account_number,
+                    account_currency=ib_account_currency,
+                )
+            else:
+                iba_info.account_routing_number = ib_account_routing_number
+                iba_info.account_number = ib_account_number
+                iba_info.account_currency = ib_account_currency
+
+            bank_info.branch_address = cleaned_data['branch_address']
+            bank_info.nid_type = cleaned_data['nid_type']
+            bank_info.nid_number = cleaned_data['nid_number']
+            bank_info.iba_info = iba_info
+            vendor.other_info = cleaned_data['other_info']
+
+        bank_info.save()
+
+        vendor.bank_info = bank_info
         vendor.name = cleaned_data['name']
         vendor.contractor_name = cleaned_data['contractor_name']
         vendor.type = cleaned_data['type']
-- 
GitLab