Exemplo n.º 1
0
def remove_quotation(n_quotation):
    ddao = QuotationDAO()
    didao = QuotationItemDAO()
    if didao.delete(didao.where('id_quotation', n_quotation)):
        ddao.delete(ddao.where('number', n_quotation))
        flash(
            _("The quotation n°%1 has been deleted successfull").replace(
                '%1', n_quotation), 'success')
        logging.info('remove quotation %s OK', n_quotation)
    else:
        flash(
            _('Error while suppression of quotation n°%1 !').replace(
                '%1', n_quotation), 'danger')
        logging.info('remove quotation %s FAILED', n_quotation)
Exemplo n.º 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")
Exemplo n.º 3
0
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')