예제 #1
0
def _get_service_item(sale, store):
    delivery_service = get_parameter(store, u'DELIVERY_SERVICE', Service)
    for item in sale.get_items():
        if item.sellable == delivery_service.sellable:
            # For most of cases, if the user didn't change the delivery
            # service, this should be all we need
            return item

    services = [item for item in sale.get_items() if item.sellable.service]
    if len(services) == 1:
        # If only one, that's for sure the delivery service
        return services[0]

    for service in services:
        # try to find the delivery service by it's price
        if service.base_price == delivery_service.sellable.price:
            return service

    # Try to figure out what's the service_item by diffing their prices
    diff_dict = dict(
        [(abs(delivery_service.sellable.price - service.base_price), service)
         for service in services]
    )
    min_ = min(diff_dict.keys())
    return diff_dict[min_]
예제 #2
0
def apply_patch(store):
    new_table_query = """
    CREATE TABLE user_branch_access(
        id serial NOT NULL PRIMARY KEY,
        te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
        te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),
        user_id bigint NOT NULL REFERENCES login_user(id) ON UPDATE CASCADE,
        branch_id bigint NOT NULL REFERENCES branch(id) ON UPDATE CASCADE,
        UNIQUE(user_id, branch_id)
    );"""
    store.execute(new_table_query)

    new_column_query = """
    ALTER TABLE employee ADD COLUMN branch_id bigint REFERENCES branch(id)
        ON UPDATE CASCADE;
    """
    store.execute(new_column_query)

    main_company = int(get_parameter(store, u'MAIN_COMPANY'))
    if main_company:
        update_employee = """UPDATE employee SET branch_id = ?"""
        store.execute(update_employee, (main_company, ))

    query = """
    SELECT branch.id as branch_id, login_user.id as user_id
    FROM branch, login_user
    WHERE branch.is_active = true AND login_user.is_active = true
    """
    accesses = store.execute(query).get_all()
    for (branch_id, user_id) in accesses:
        UserBranchAccess(store=store, user_id=user_id, branch_id=branch_id)
예제 #3
0
def apply_patch(store):
    new_table_query = """
    CREATE TABLE user_branch_access(
        id serial NOT NULL PRIMARY KEY,
        te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
        te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),
        user_id bigint NOT NULL REFERENCES login_user(id) ON UPDATE CASCADE,
        branch_id bigint NOT NULL REFERENCES branch(id) ON UPDATE CASCADE,
        UNIQUE(user_id, branch_id)
    );"""
    store.execute(new_table_query)

    new_column_query = """
    ALTER TABLE employee ADD COLUMN branch_id bigint REFERENCES branch(id)
        ON UPDATE CASCADE;
    """
    store.execute(new_column_query)

    main_company = int(get_parameter(store, u'MAIN_COMPANY'))
    if main_company:
        update_employee = """UPDATE employee SET branch_id = ?"""
        store.execute(update_employee, (main_company,))

    query = """
    SELECT branch.id as branch_id, login_user.id as user_id
    FROM branch, login_user
    WHERE branch.is_active = true AND login_user.is_active = true
    """
    accesses = store.execute(query).get_all()
    for (branch_id, user_id) in accesses:
        UserBranchAccess(store=store, user_id=user_id, branch_id=branch_id)
예제 #4
0
def apply_patch(store):
    # This might run on an empty database so make sure we have
    # the imbalance account created

    destination_account = get_parameter(store, u'IMBALANCE_ACCOUNT')
    # There is no parameter yet. This means the database is brand new. No need
    # to register the new payment now, since it will be created by
    # initialize_system
    if not destination_account:
        return

    method = store.find(PaymentMethod, method_name=u'credit').one()
    if not method:
        PaymentMethod(method_name=u'credit',
                      destination_account_id=int(destination_account),
                      store=store)
예제 #5
0
def apply_patch(store):
    # This might run on an empty database so make sure we have
    # the imbalance account created

    destination_account = get_parameter(store, u'IMBALANCE_ACCOUNT')
    # There is no parameter yet. This means the database is brand new. No need
    # to register the new payment now, since it will be created by
    # initialize_system
    if not destination_account:
        return

    method = store.find(PaymentMethod, method_name=u'credit').one()
    if not method:
        PaymentMethod(method_name=u'credit',
                      destination_account_id=int(destination_account),
                      store=store)
예제 #6
0
 def test_get_nonexistent_parameter(self):
     account = get_parameter(self.store, u'NONEXISTENT PARAMETER')
     self.assertEquals(account, None)
예제 #7
0
    def test_get_existent_parameter(self):
        imbalance = self.store.find(Account,
                                    description=u'Imbalance').one()
        account = get_parameter(self.store, u'IMBALANCE_ACCOUNT')

        self.assertEquals(imbalance.id, unicode(account))
예제 #8
0
 def test_get_nonexistent_parameter(self):
     account = get_parameter(self.store, u'NONEXISTENT PARAMETER')
     self.assertEqual(account, None)
예제 #9
0
    def test_get_existent_parameter(self):
        imbalance = self.store.find(Account,
                                    description=u'Imbalance').one()
        account = get_parameter(self.store, u'IMBALANCE_ACCOUNT')

        self.assertEqual(imbalance.id, str(account))
예제 #10
0
def apply_patch(store):
    store.execute("""
        CREATE TABLE returned_sale (
            id serial NOT NULL PRIMARY KEY,
            te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
            te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),

            identifier serial NOT NULL,
            return_date timestamp,
            reason text,
            invoice_number integer CONSTRAINT valid_invoice_number
                CHECK (invoice_number > 0 AND invoice_number <= 999999999)
                DEFAULT NULL UNIQUE,
            responsible_id bigint REFERENCES login_user(id) ON UPDATE CASCADE,
            branch_id bigint REFERENCES branch(id) ON UPDATE CASCADE,
            sale_id bigint REFERENCES sale(id) ON UPDATE CASCADE,
            new_sale_id bigint UNIQUE REFERENCES sale(id) ON UPDATE CASCADE
        );

        CREATE TABLE returned_sale_item (
            id serial NOT NULL PRIMARY KEY,
            te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
            te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),

            quantity numeric(20, 3) CONSTRAINT positive_quantity
                CHECK (quantity >= 0),
            price numeric(20, 2) CONSTRAINT positive_price
                CHECK (price >= 0),
            sellable_id bigint REFERENCES sellable(id) ON UPDATE CASCADE,
            sale_item_id bigint REFERENCES sale_item(id) ON UPDATE CASCADE,
            returned_sale_id bigint REFERENCES returned_sale(id) ON UPDATE CASCADE
        );
        """)

    # Migrate all renegotiation_data to returned_sale
    invoice_numbers = set()
    for sale_id, person_id, invoice_number, reason, penalty in store.execute(
        """SELECT sale_id, responsible_id, invoice_number, reason, penalty_value
               FROM renegotiation_data;""").get_all():
        sale = Sale.get(sale_id, store)
        person = Person.get(person_id, store)
        if invoice_number is not None:
            # invoice_number can be duplicated, since it wasn't unique before
            # First come, first served. Others will have no invoice number
            if invoice_number in invoice_numbers:
                invoice_number = None
            invoice_numbers.add(invoice_number)
        returned_sale = ReturnedSale(
            store=store,
            return_date=sale.return_date,
            invoice_number=invoice_number,
            responsible=person.login_user,
            reason=reason,
            branch=sale.branch,
            sale=sale,
        )
        for sale_item in sale.get_items():
            ReturnedSaleItem(
                store=store,
                sale_item=sale_item,
                returned_sale_id=returned_sale.id,
                quantity=sale_item.quantity,
            )

    store.execute("DROP TABLE renegotiation_data;")

    account = int(get_parameter(store, u'IMBALANCE_ACCOUNT'))
    # Only do that if IMBALANCE_ACCOUNT is already registered. Else, the
    # database is brand new and payment method will be created later.
    if account:
        # Register the new payment method, 'trade'
        method = store.find(PaymentMethod, method_name=u'trade').one()
        if not method:
            PaymentMethod(store=store,
                          method_name=u'trade',
                          destination_account_id=account,
                          max_installments=12)
예제 #11
0
def apply_patch(store):
    store.execute("""
        CREATE TABLE returned_sale (
            id serial NOT NULL PRIMARY KEY,
            te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
            te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),

            identifier serial NOT NULL,
            return_date timestamp,
            reason text,
            invoice_number integer CONSTRAINT valid_invoice_number
                CHECK (invoice_number > 0 AND invoice_number <= 999999999)
                DEFAULT NULL UNIQUE,
            responsible_id bigint REFERENCES login_user(id) ON UPDATE CASCADE,
            branch_id bigint REFERENCES branch(id) ON UPDATE CASCADE,
            sale_id bigint REFERENCES sale(id) ON UPDATE CASCADE,
            new_sale_id bigint UNIQUE REFERENCES sale(id) ON UPDATE CASCADE
        );

        CREATE TABLE returned_sale_item (
            id serial NOT NULL PRIMARY KEY,
            te_created_id bigint UNIQUE REFERENCES transaction_entry(id),
            te_modified_id bigint UNIQUE REFERENCES transaction_entry(id),

            quantity numeric(20, 3) CONSTRAINT positive_quantity
                CHECK (quantity >= 0),
            price numeric(20, 2) CONSTRAINT positive_price
                CHECK (price >= 0),
            sellable_id bigint REFERENCES sellable(id) ON UPDATE CASCADE,
            sale_item_id bigint REFERENCES sale_item(id) ON UPDATE CASCADE,
            returned_sale_id bigint REFERENCES returned_sale(id) ON UPDATE CASCADE
        );
        """)

    # Migrate all renegotiation_data to returned_sale
    invoice_numbers = set()
    for sale_id, person_id, invoice_number, reason, penalty in store.execute(
            """SELECT sale_id, responsible_id, invoice_number, reason, penalty_value
               FROM renegotiation_data;""").get_all():
        sale = Sale.get(sale_id, store)
        person = Person.get(person_id, store)
        if invoice_number is not None:
            # invoice_number can be duplicated, since it wasn't unique before
            # First come, first served. Others will have no invoice number
            if invoice_number in invoice_numbers:
                invoice_number = None
            invoice_numbers.add(invoice_number)
        returned_sale = ReturnedSale(
            store=store,
            return_date=sale.return_date,
            invoice_number=invoice_number,
            responsible=person.login_user,
            reason=reason,
            branch=sale.branch,
            sale=sale,
        )
        for sale_item in sale.get_items():
            ReturnedSaleItem(
                store=store,
                sale_item=sale_item,
                returned_sale_id=returned_sale.id,
                quantity=sale_item.quantity,
            )

    store.execute("DROP TABLE renegotiation_data;")

    account = int(get_parameter(store, u'IMBALANCE_ACCOUNT'))
    # Only do that if IMBALANCE_ACCOUNT is already registered. Else, the
    # database is brand new and payment method will be created later.
    if account:
        # Register the new payment method, 'trade'
        method = store.find(PaymentMethod, method_name=u'trade').one()
        if not method:
            PaymentMethod(store=store,
                          method_name=u'trade',
                          destination_account_id=account,
                          max_installments=12)