def add_quotation(form):
    profileSession = get_profile_from_session()
    if profileSession.id:
        id_profile = profileSession.id
    else:
        logging.warning('(Quotation) Session closed: %s', profileSession.id)
        flash(_("Impossible to add Quotation, Your session has been expired"),
              'danger')
        return

    ddao = QuotationDAO()
    n_quotation = form['quotation']

    if ddao.exist(ddao.where('number', n_quotation)):
        logging.info('quotation exist with number: %s', n_quotation)
        flash(
            _("Impossible to add Quotation, the number of quotation is ever used"
              ), 'danger')
        return

    client = form['client']
    date_sent = form['date_sent']
    date_validity = form['date_validity']
    tax = (form['tax'] == "true")
    lines = [(x.replace('lines[',
                        '').replace('][', '-').replace(']', ''), dict(form)[x])
             for x in dict(form) if x.startswith('lines[')]
    list_text = [
        dict(form)[x] for x in dict(form) if x.startswith('text_end[')
    ]
    quotation_obj = Quotation()
    quotation_obj.client = client
    quotation_obj.date_sent = date_sent
    quotation_obj.date_validity = date_validity
    quotation_obj.number = n_quotation
    quotation_obj.tax_price = 0
    quotation_obj.id_profile = id_profile
    quotation_obj.end_text = '\n'.join(list_text)

    didao = QuotationItemDAO()
    success = True
    nb_items = int(len(lines) / 3)
    list_quotation_item = list()
    for i in range(0, nb_items):
        quotationItem = QuotationItem()
        quotationItem.description = lines[(i * 3) + 0][1]
        quotationItem.quantity_text = lines[(i * 3) + 1][1]
        result = re.findall(r'[-+]?\d*\.\d+|^\d+', quotationItem.quantity_text)
        if len(result) == 0:
            result = [0]
        quotationItem.quantity = float(result[0])
        uprice = lines[(i * 3) + 2][1]
        if not uprice:
            uprice = 0
        quotationItem.unit_price = float(uprice)
        quotationItem.reduction = False
        list_quotation_item.append(quotationItem)
        quotation_obj.total += (quotationItem.quantity *
                                quotationItem.unit_price)
        if tax:
            quotation_obj.tax_price += (
                (quotationItem.quantity * quotationItem.unit_price) * 20 / 100)

    if not ddao.insert(quotation_obj):
        logging.info('add quotation %s FAILED', n_quotation)
        flash(
            _("Impossible to add quotation n°%1").replace('%1', n_quotation),
            'danger')
        return
    else:
        logging.info('add quotation %s OK', n_quotation)

    for quotationItem in list_quotation_item:
        quotationItem.id_quotation = quotation_obj.id
        success &= didao.insert(quotationItem)

    if not success:
        logging.warning('add quotation item %s FAILED', n_quotation)
        didao.delete(didao.where('id_quotation', n_quotation))
        ddao.delete(ddao.where('id', n_quotation))
        flash(
            _("Impossible to add quotation n°%1").replace('%1', n_quotation),
            'danger')
    else:
        logging.info('add quotation item %s OK', n_quotation)
        flash(
            _("The quotation n°%1 has been added successfull").replace(
                '%1', n_quotation), 'success')
示例#2
0
class QuotationTestCase(unittest.TestCase):
    def setUp(self):
        self.qdao = QuotationDAO(DB_PATH)
        self.quotation = Quotation()
        self.quotation.client = "TEST 1"
        self.quotation.date_sent = "01/01/2020"
        self.quotation.date_validity = "01/01/2021"
        self.quotation.end_text = "TEST 2\nTEST 3\nTEST 4"
        self.quotation.id_profile = 1
        self.quotation.number = 230
        self.quotation.total = 2030.20
        self.quotation.tax_price = 55.10

    def test_quotation_obj(self):
        self.assertIsNotNone(self.qdao,
                             msg="Impossible to instance QuotationDAO")
        self.assertTrue(self.qdao.create_table(),
                        msg="Impossible to create devis table in db")
        self.assertFalse(self.qdao.exist(self.quotation),
                         msg="Impossible to check Quotation exist in db")
        self.assertTrue(self.qdao.insert(self.quotation),
                        msg="Impossible to insert Quotation in db")
        list_quotation = self.qdao.get(
            self.qdao.where('number', self.quotation.number))
        self.assertIsInstance(list_quotation,
                              list,
                              msg="dao.get function not return a list")
        self.assertGreater(len(list_quotation), 0, msg="No Quotation in db")
        quota = list_quotation[0]
        self.assertIsInstance(
            quota,
            Quotation,
            msg="1st element of dao.get is not a Quotation obj")
        self.assertEqual(self.quotation.client,
                         quota.client,
                         msg="Quotation get, has no same attribute 'client'")
        self.assertEqual(
            self.quotation.date_sent,
            quota.date_sent,
            msg="Quotation get, has no same attribute 'date_sent'")
        self.assertEqual(
            self.quotation.date_validity,
            quota.date_validity,
            msg="Quotation get, has no same attribute 'date_validity'")
        self.assertEqual(self.quotation.end_text,
                         quota.end_text,
                         msg="Quotation get, has no same attribute 'end_text'")
        self.assertEqual(
            self.quotation.id_profile,
            quota.id_profile,
            msg="Quotation get, has no same attribute 'id_profile'")
        self.assertEqual(self.quotation.number,
                         quota.number,
                         msg="Quotation get, has no same attribute 'number'")
        self.assertEqual(self.quotation.total,
                         quota.total,
                         msg="Quotation get, has no same attribute 'total'")
        self.assertEqual(
            self.quotation.tax_price,
            quota.tax_price,
            msg="Quotation get, has no same attribute 'tax_price'")
        self.assertTrue(hasattr(quota, 'id'),
                        msg="Quotation get, has no attribute 'id'")
        quota.end_text = "TEST 1\nTEST 2\nTEST3"
        self.assertTrue(self.qdao.update(quota))
        list_quotation2 = self.qdao.get(self.qdao.where(
            'number', quota.number))
        self.assertIsInstance(list_quotation2,
                              list,
                              msg="dao.get function not return a list 2")
        self.assertGreater(len(list_quotation2), 0, msg="No Quotation 2 in db")
        quota2 = list_quotation2[0]
        self.assertEqual(
            quota2.end_text,
            quota.end_text,
            msg="Quotation get 2, has no same attribute 'end_text'")
        self.assertTrue(self.qdao.delete(quota),
                        msg="Impossible to delete devis from db")
        self.assertFalse(self.qdao.drop(True, False),
                         msg="Drop table devis not wanted")
        self.assertFalse(self.qdao.drop(False, True),
                         msg="Drop table devis not wanted")
        self.assertTrue(self.qdao.drop(True, True),
                        msg="Cannot drop table devis")
        self.assertFalse(self.qdao.drop(True, True),
                         msg="The table has not deleted before")
示例#3
0
class Test:
    def __init__(self):
        self.client = None
        self.insurance = None
        self.profile = None
        self.pdao = None

    def __init_db(self):
        print(bcolors.BOLD + '---------- CREATE DB ----------' + bcolors.ENDC)
        self.pdao = ProfileDAO(DB_PATH)
        pdaoret = self.pdao.create_table()
        print(bcolors.HEADER + 'ProfileDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if pdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.idao = InsuranceDAO(DB_PATH)
        idaoret = self.idao.create_table()
        print(bcolors.HEADER + 'InsuranceDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if idaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.fdao = InvoiceDAO(DB_PATH)
        fdaoret = self.fdao.create_table()
        print(bcolors.HEADER + 'InvoiceDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if fdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.cdao = ClientDAO(DB_PATH)
        cdaoret = self.cdao.create_table()
        print(bcolors.HEADER + 'ClientDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if cdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.qdao = QuotationDAO(DB_PATH)
        qdaoret = self.qdao.create_table()
        print(bcolors.HEADER + 'QuotationDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if qdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.qidao = QuotationItemDAO(DB_PATH)
        qidaoret = self.qidao.create_table()
        print(bcolors.HEADER + 'QuotationItemDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if qidaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        return pdaoret and idaoret and fdaoret and cdaoret and qdaoret and qidaoret

    def drop_db(self):
        print(bcolors.BOLD + '---------- DROP DB ----------' + bcolors.ENDC)
        pdaoret = self.pdao.drop(True, True)
        print(bcolors.HEADER + 'ProfileDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if pdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        idaoret = self.idao.drop(True, True)
        print(bcolors.HEADER + 'InsuranceDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if idaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        fdaoret = self.fdao.drop(True, True)
        print(bcolors.HEADER + 'InvoiceDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if fdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        cdaoret = self.cdao.drop(True, True)
        print(bcolors.HEADER + 'ClientDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if cdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        qdaoret = self.qdao.drop(True, True)
        print(bcolors.HEADER + 'QuotationDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if qdaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        qidaoret = self.qidao.drop(True, True)
        print(bcolors.HEADER + 'QuotationItemDAO ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if qidaoret else bcolors.FAIL + "KO",
              bcolors.ENDC)
        return pdaoret and idaoret and fdaoret and cdaoret and qdaoret and qidaoret

    def __profile_db(self):
        print(bcolors.BOLD + '---------- Profile DB ----------' + bcolors.ENDC)
        self.profile = Profile()
        self.profile.address = 'TEST 1'
        self.profile.comp_address = 'TEST 2'
        self.profile.zipcode = '13132'
        self.profile.email = '[email protected]'
        self.profile.name = 'TEST 3'
        self.profile.firstname = 'TEST 4'
        self.profile.password = '******'
        self.profile.country = 'FRANCE'
        self.profile.siret = '0292029102'
        self.profile.phone = '0439403920'
        self.profile.city = 'MARSEILLE'

        inspdao = self.pdao.insert(self.profile)
        print(bcolors.HEADER + 'insert profile ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if inspdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.profile.password = "******"
        chpasswdpdao = self.pdao.update(self.profile)
        print(bcolors.HEADER + 'update profile ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chpasswdpdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        get_profile_list = self.pdao.get(
            self.pdao.where('name', self.profile.name))
        print(
            bcolors.HEADER + 'get profile ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if get_profile_list else bcolors.FAIL +
            "KO", bcolors.ENDC)
        if not get_profile_list:
            return False
        get_profile = get_profile_list[0]
        chkauthpdao = self.pdao.check_auth(get_profile, "NEW PASSWORD")
        self.profile = get_profile
        print(bcolors.HEADER + 'auth profile ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chkauthpdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        return (inspdao and chpasswdpdao and chkauthpdao)

    def __insurance_db(self):
        print(bcolors.BOLD + '---------- Insurance DB ----------' +
              bcolors.ENDC)
        if not self.profile:
            print(
                bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC,
                bcolors.OKGREEN + 'OK' if False else bcolors.FAIL +
                "Please call __profile_db()", bcolors.ENDC)
            return
        self.insurance = Insurance()
        self.insurance.id_profile = self.profile.id
        self.insurance.n_contract = "1234567891011121314"
        self.insurance.name = "TEST 1"
        self.insurance.region = "FRANCE"
        self.insurance.sel = False
        self.insurance.type = "ALL INCLUDE"

        insidao = self.idao.insert(self.insurance)
        print(bcolors.HEADER + 'insert insurance ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if insidao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.insurance.sel = True
        chselidao = self.idao.update(self.insurance)
        print(bcolors.HEADER + 'update insurance ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chselidao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        get_insurance_list = self.idao.get(
            self.idao.where('n_contract', self.insurance.n_contract))
        print(
            bcolors.HEADER + 'get insurance ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if get_insurance_list else bcolors.FAIL +
            "KO", bcolors.ENDC)
        if not get_insurance_list:
            return False
        is_sel_insurance = get_insurance_list[0].sel
        print(
            bcolors.HEADER + 'change sel insurance ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if is_sel_insurance else bcolors.FAIL +
            "KO", bcolors.ENDC)
        return (insidao and chselidao and is_sel_insurance)

    def __client_db(self):
        print(bcolors.BOLD + '---------- Client DB ----------' + bcolors.ENDC)
        if not self.profile:
            print(
                bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC,
                bcolors.OKGREEN + 'OK' if False else bcolors.FAIL +
                "Please call __profile_db()", bcolors.ENDC)
            return
        self.client = Client()
        self.client.address = "TEST 1"
        self.client.comp_address = "TEST 2"
        self.client.zipcode = "TEST 3"
        self.client.id_profile = self.profile.id
        self.client.name = "TEST 4"
        self.client.country = "TEST 5"
        self.client.city = "TEST 6"

        inscdao = self.cdao.insert(self.client)
        print(bcolors.HEADER + 'insert client ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if inscdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.client.zipcode = "13013"
        chzipcodecdao = self.cdao.update(self.client)
        print(bcolors.HEADER + 'update client ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chzipcodecdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        get_client_list = self.cdao.get(
            self.cdao.where('name', self.client.name))
        print(
            bcolors.HEADER + 'get client ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if get_client_list else bcolors.FAIL + "KO",
            bcolors.ENDC)
        if not get_client_list:
            return False
        self.client = get_client_list[0]
        is_change_zipcode = (self.client.zipcode == "13013")
        print(
            bcolors.HEADER + 'change zip code client ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if is_change_zipcode else bcolors.FAIL +
            "KO", bcolors.ENDC)
        return (inscdao and chzipcodecdao and is_change_zipcode)

    def __invoice_db(self):
        print(bcolors.BOLD + '---------- Invoice DB ----------' + bcolors.ENDC)
        if not self.profile:
            print(
                bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC,
                bcolors.OKGREEN + 'OK' if False else bcolors.FAIL +
                "Please call __profile_db()", bcolors.ENDC)
            return
        if not self.client:
            print(
                bcolors.HEADER + 'no client ?> ' + bcolors.ENDC,
                bcolors.OKGREEN + 'OK' if False else bcolors.FAIL +
                "Please call __client_db()", bcolors.ENDC)
            return
        self.invoice = Invoice()
        self.invoice.date_expiry = '01/01/2020'
        self.invoice.date_sent = '01/01/2020'
        self.invoice.days = 10
        self.invoice.max_delay = '01/01/2020'
        self.invoice.id_client = self.client.id
        self.invoice.id_profile = self.profile.id
        self.invoice.name = 'Invoice TEST 1'
        self.invoice.sold = False
        self.invoice.project = 'TEST 1 2 3'
        self.invoice.day_rate = 5000
        self.invoice.total = '50000'
        self.invoice.tax = False

        insindao = self.fdao.insert(self.invoice)
        print(bcolors.HEADER + 'insert invoice ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if insindao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.invoice.sold = True
        chpyfdao = self.fdao.update(self.invoice)
        print(bcolors.HEADER + 'update invoice ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chpyfdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        get_invoice_list = self.fdao.get(
            self.fdao.where('id_client', self.invoice.id_client))
        print(
            bcolors.HEADER + 'get invoice ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if get_invoice_list else bcolors.FAIL +
            "KO", bcolors.ENDC)
        if not get_invoice_list:
            return False
        self.invoice = get_invoice_list[0]
        is_change_sold = self.invoice.sold
        print(
            bcolors.HEADER + 'change to bill invoice ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if is_change_sold else bcolors.FAIL + "KO",
            bcolors.ENDC)
        return (insindao and chpyfdao and is_change_sold)

    def __quotation_db(self):
        print(bcolors.BOLD + '---------- Quotation DB ----------' +
              bcolors.ENDC)
        if not self.profile:
            print(
                bcolors.HEADER + 'no profile ?> ' + bcolors.ENDC,
                bcolors.OKGREEN + 'OK' if False else bcolors.FAIL +
                "Please call __profile_db()", bcolors.ENDC)
            return
        self.quotation = Quotation()
        self.quotation.client = "TEST 1"
        self.quotation.date_sent = "01/01/2020"
        self.quotation.date_validity = "01/01/2021"
        self.quotation.end_text = "TEST 2\nTEST 3\nTEST 4"
        self.quotation.id_profile = self.profile.id
        self.quotation.number = 203
        self.quotation.total = 2030.20
        self.quotation.tax_price = (2030.20 * 0.2)

        insqdao = self.qdao.insert(self.quotation)
        print(bcolors.HEADER + 'insert quotation ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if insqdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.quotation.end_text = "TEST 1\nTEST 2"
        chpyqdao = self.qdao.update(self.quotation)
        print(bcolors.HEADER + 'update quotation ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chpyqdao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        get_quotation_list = self.qdao.get(
            self.qdao.where('number', self.quotation.number))
        print(
            bcolors.HEADER + 'get quotation ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if get_quotation_list else bcolors.FAIL +
            "KO", bcolors.ENDC)
        if not get_quotation_list:
            return False
        self.quotation = get_quotation_list[0]
        is_change_end_text = (self.quotation.end_text == "TEST 1\nTEST 2")
        print(
            bcolors.HEADER + 'change end text quotation ?> ' + bcolors.ENDC,
            bcolors.OKGREEN + 'OK' if is_change_end_text else bcolors.FAIL +
            "KO", bcolors.ENDC)
        return (insqdao and chpyqdao and is_change_end_text)

    def __quotation_item_db(self):
        print(bcolors.BOLD + '---------- QuotationItem DB ----------' +
              bcolors.ENDC)
        if not self.quotation:
            print(
                bcolors.HEADER + 'no quotation ?> ' + bcolors.ENDC,
                bcolors.OKGREEN + 'OK' if False else bcolors.FAIL +
                "Please call __quotation_db()", bcolors.ENDC)
            return
        self.quotation_item = QuotationItem()
        self.quotation_item.description = ""
        self.quotation_item.id_quotation = self.quotation.id
        self.quotation_item.quantity = 12
        self.quotation_item.quantity_text = "12m2"
        self.quotation_item.reduction = False
        self.quotation_item.unit_price = 1.34

        insqidao = self.qidao.insert(self.quotation_item)
        print(bcolors.HEADER + 'insert quotationitem ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if insqidao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        self.quotation_item.description = "Fondation en pierre"
        chdscqidao = self.qidao.update(self.quotation_item)
        print(bcolors.HEADER + 'update quotationitem ?> ' + bcolors.ENDC,
              bcolors.OKGREEN + 'OK' if chdscqidao else bcolors.FAIL + "KO",
              bcolors.ENDC)
        get_quotation_item_list = self.qidao.get(
            self.qidao.where('description', self.quotation_item.description))
        print(
            bcolors.HEADER + 'get quotationitem ?> ' + bcolors.ENDC,
            bcolors.OKGREEN +
            'OK' if get_quotation_item_list else bcolors.FAIL + "KO",
            bcolors.ENDC)
        if not get_quotation_item_list:
            return False
        self.quotation_item = get_quotation_item_list[0]
        is_change_description = (
            self.quotation_item.description == "Fondation en pierre")
        print(
            bcolors.HEADER + 'change end text quotationitem ?> ' +
            bcolors.ENDC, bcolors.OKGREEN +
            'OK' if is_change_description else bcolors.FAIL + "KO",
            bcolors.ENDC)
        return (insqidao and chdscqidao and is_change_description)

    def run_invoice(self):
        ret = False
        if self.__init_db():
            ret = (self.__profile_db() and self.__client_db()
                   and self.__invoice_db())
            self.drop_db()
        return ret

    def run_quotation(self):
        ret = False
        if self.__init_db():
            ret = (self.__profile_db() and self.__client_db()
                   and self.__quotation_db() and self.__quotation_item_db())
            self.drop_db()
        return ret