def on_model_change(self, form, model, is_created): from wtforms import ValidationError super(FranchisePurchaseOrderAdmin, self).on_model_change(form, model, is_created) for l in model.lines: if l.unit_price is None: l.unit_price = l.product.franchise_price if current_user.organization.type.code != const.FRANCHISE_STORE_ORG_TYPE_KEY: raise ValidationError( gettext( "Your organization is not a franchise store and is not allowed to create franchise purchase order" )) if current_user.organization.parent is None: raise ValidationError( gettext( "Franchise purchase order creation failed, your organization does not have a valid parent organization" )) if is_created: model.to_organization = current_user.organization.parent status = model.status if status.code == const.PO_ISSUED_STATUS_KEY: sales_order, incoming, expense = self.create_so_from_fpo(model) related_value = self.create_related_value(sales_order, model) db_util.save_objects_commit(sales_order, incoming, expense, related_value)
def test_logic(): fixture.login_as_admin(self.test_client) user, password = object_faker.user(role_names=[ 'franchise_sales_order_create', 'franchise_sales_order_view', 'franchise_sales_order_edit', 'product_view' ]) franchise_so_type = EnumValues.get(FRANCHISE_SO_TYPE_KEY) sales_order = object_faker.sales_order(creator=user, number_of_line=1, type=franchise_so_type) db_util.save_objects_commit(sales_order, user) so_id = sales_order.id shipped_status = EnumValues.get(SO_SHIPPED_STATUS_KEY) fixture.login_user(self.test_client, user.email, password) rv = self.test_client.put('/api/sales_order/' + str(so_id), follow_redirects=True, data=dict(status_id=shipped_status.id)) self.assertIn(b'message', rv.data) self.assertIn(b'Status update successfully', rv.data) self.assertEqual(rv.status_code, 200) so_from_db = Info.get_db().session.query(SalesOrder).get(so_id) self.assertIsNotNone(so_from_db) self.assertEquals(SO_SHIPPED_STATUS_KEY, so_from_db.status.code)
def test_create(self): from psi.app.models import EnumValues type_id = EnumValues.get(const.DIRECT_SELLING_STORE_ORG_TYPE_KEY).id with self.test_client: fixture.login_as_admin(self.test_client) org_name = object_faker.faker.name() org_desc = object_faker.faker.text(max_nb_chars=20) create_url = self.create_endpoint(view='organization') self.assertPageRendered(endpoint=create_url, method=self.test_client.get, expect_contents=['betterlife', '直营店']) self.assertPageRendered(endpoint=create_url, method=self.test_client.post, expect_contents=[org_name, org_desc], data={"type": type_id, "name": org_name, "description": org_desc, "parent": 1}) self.assertDeleteSuccessful(endpoint=url_for('organization.delete_view', id=2, url=url_for('organization.index_view')), deleted_data=[org_name, org_desc]) from psi.app.models import Organization user, pwd = object_faker.user(role_names=['organization_create', 'organization_view', 'organization_delete', 'organization_edit'], organization=Organization.query.get(1)) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, pwd) from psi.app.models import EnumValues org_type = EnumValues.get(const.DIRECT_SELLING_STORE_ORG_TYPE_KEY) self.assertCreateFail(endpoint=create_url, create_data=[org_name, org_desc], data={"type": org_type.id, "name": org_name, "description": org_desc, "parent": 1})
def test_all_page_rendered(self): user, password = of.user(role_names=[ 'sales_report_view', ]) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, password) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='overall')) self.assertPageRendered( endpoint=url_for('product_sales_report.index_view', type='today')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='yesterday')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='this_week')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='last_week')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='this_month')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='last_month')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='this_quarter')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='last_quarter')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='this_year')) self.assertPageRendered(endpoint=url_for( 'product_sales_report.index_view', type='last_year')) fixture.logout_user(self.test_client)
def test_saleable_qty(self): from psi.app.services.purchase_order import PurchaseOrderService with self.test_client: from tests.fixture import login_as_admin login_as_admin(self.test_client) from psi.app.models import EnumValues db = Info.get_db() po = of.purchase_order(number_of_line=1, type=EnumValues.get( const.DIRECT_PO_TYPE_KEY)) recv = PurchaseOrderService.create_receiving_if_not_exist(po) from psi.app.utils import db_util db_util.save_objects_commit(po, recv) recv.status = EnumValues.get(const.RECEIVING_COMPLETE_STATUS_KEY) inv_trans = recv.operate_inv_trans_by_recv_status() new_po = recv.update_purchase_order_status() db_util.save_objects_commit(new_po, inv_trans) self.assertEquals(inv_trans.lines[0].quantity, po.lines[0].quantity) self.assertEquals(inv_trans.lines[0].saleable_quantity, po.lines[0].quantity) self.assertEquals(inv_trans.lines[0].in_transit_quantity, 0) self.assertEqual(inv_trans.date, recv.date) self.assertEqual(inv_trans.lines[0].product, po.lines[0].product) self.assertEquals(inv_trans.lines[0].quantity, recv.lines[0].quantity) self.assertEquals(inv_trans.lines[0].saleable_quantity, recv.lines[0].quantity) self.assertEquals(inv_trans.lines[0].in_transit_quantity, 0) self.assertEqual(inv_trans.date, recv.date) self.assertEqual(inv_trans.lines[0].product, recv.lines[0].product)
def test_logic(): fixture.login_as_admin(self.test_client) user, password = object_faker.user(role_names=[ 'franchise_sales_order_create', 'franchise_sales_order_view', 'franchise_sales_order_edit', 'product_view' ]) franchise_so_type = EnumValues.get(FRANCHISE_SO_TYPE_KEY) sales_order = object_faker.sales_order(creator=user, number_of_line=1, type=franchise_so_type) db_util.save_objects_commit(sales_order, user) so_id = sales_order.id shipped_status = EnumValues.get(SO_SHIPPED_STATUS_KEY) fixture.login_user(self.test_client, user.email, password) rv = self.test_client.put('/api/sales_order/' + str(so_id), follow_redirects=True, data=dict(status_id=shipped_status.id)) self.assertIn('message', rv.data) self.assertIn('Status update successfully', rv.data) self.assertEqual(rv.status_code, 200) so_from_db = Info.get_db().session.query(SalesOrder).get(so_id) self.assertIsNotNone(so_from_db) self.assertEquals(SO_SHIPPED_STATUS_KEY, so_from_db.status.code)
def test_logic(): type = EnumValues.get(DIRECT_PO_TYPE_KEY) status = EnumValues.get(PO_ISSUED_STATUS_KEY) receiving_status = EnumValues.get(RECEIVING_DRAFT_STATUS_KEY) date = object_faker.faker.date_time_this_year() po = object_faker.purchase_order(number_of_line=2, type=type, status=status) db_util.save_objects_commit(po) remark = object_faker.faker.text(max_nb_chars=50) list_expect = [ receiving_status.display, date.strftime("%Y-%m-%d"), remark, po.supplier.name, po.order_date.strftime("%Y-%m-%d"), po.remark ] edit_expect = [ receiving_status.display, date.strftime("%Y-%m-%d"), remark, ] # Crete new receiving self.assertPageRendered( method=self.test_client.post, data=dict(purchase_order=po.id, status=receiving_status.id, create_lines='y', date=date, remark=remark), endpoint=self.create_endpoint(view='receiving'), expect_contents=list_expect) self.assertPageRendered( expect_contents=edit_expect, endpoint=self.edit_endpoint(view='receiving')) # Edit existing receiving new_remark = object_faker.faker.text(max_nb_chars=50) new_receive_date = object_faker.faker.date_time_this_year() complete_status = EnumValues.get(RECEIVING_COMPLETE_STATUS_KEY) new_expected = [ complete_status.display, new_receive_date.strftime("%Y-%m-%d"), new_remark, po.supplier.name, po.order_date.strftime("%Y-%m-%d"), po.remark ] self.assertPageRendered( method=self.test_client.post, endpoint=self.edit_endpoint(view='receiving'), data=dict(date=new_receive_date, status=complete_status.id, remark=new_remark), expect_contents=new_expected) # Detail page self.assertPageRendered( method=self.test_client.get, endpoint=self.details_endpoint(view='receiving'), expect_contents=new_expected)
def test_not_allowed_if_not_franchise_organization(self): from psi.app.models import EnumValues, Organization, PurchaseOrder with self.test_client: login_as_admin(self.test_client) org_type = EnumValues.get(const.DIRECT_SELLING_STORE_ORG_TYPE_KEY) organization = object_faker.organization( parent=Organization.query.get(1), type=org_type) user, pwd = object_faker.user(role_names=[ 'franchise_purchase_order_create', 'franchise_purchase_order_edit', 'franchise_purchase_order_delete', 'franchise_purchase_order_view' ], organization=organization) db_util.save_objects_commit(user, organization) login_user(self.test_client, user.email, pwd) draft_status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) order_date = object_faker.faker.date_time_this_year() logistic_amount = random.randint(0, 100) remark = object_faker.faker.text(max_nb_chars=50) rv = self.test_client.post(url_for('fpo.create_view', url=url_for('fpo.index_view')), data=dict( status=draft_status.id, order_date=order_date, logistic_amount=logistic_amount, remark=remark), follow_redirects=True) self.assertEqual(200, rv.status_code) self.assertIn( 'Your organization is not a franchise store and is not allowed to ' 'create franchise purchase order', rv.data) po = PurchaseOrder.query.all() self.assertEqual(0, len(po))
def test_logic(): supplier = object_faker.supplier() db_util.save_objects_commit(supplier) self.assertPageRendered(endpoint=url_for('dpo.index_view')) self.assertPageRendered(endpoint=url_for('dpo.create_view')) draft_status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) order_date = object_faker.faker.date_time_this_year() logistic_amount = random.randint(0, 100) remark = object_faker.faker.text(max_nb_chars=50) expect_content = [ supplier.name, draft_status.display, str(logistic_amount), order_date.strftime("%Y-%m-%d"), remark ] self.assertPageRendered( method=self.test_client.post, data=dict( supplier=supplier.id, status=draft_status.id, order_date=order_date, logistic_amount=logistic_amount, remark=remark), endpoint=self.create_endpoint(view='dpo'), expect_contents=expect_content) self.assertPageRendered( expect_contents=expect_content, endpoint=self.edit_endpoint(view='dpo')) new_remark = object_faker.faker.text(max_nb_chars=50) new_logistic_amount = random.randint(0, 100) new_order_date = object_faker.faker.date_time_this_year() new_expect_content = [ supplier.name, draft_status.display, str(new_logistic_amount), new_order_date.strftime("%Y-%m-%d"), new_remark ] self.assertPageRendered( method=self.test_client.post, endpoint=self.edit_endpoint(view='dpo'), data=dict( supplier=supplier.id, status=draft_status.id, order_date=new_order_date, logistic_amount=new_logistic_amount, remark=new_remark), expect_contents=new_expect_content) rv = self.assertPageRendered( method=self.test_client.post, endpoint=url_for('dpo.delete_view'), data=dict(url=url_for('dpo.index_view'), id='1')) self.assertNotIn(supplier.name.encode('utf-8'), rv.data) self.assertNotIn(draft_status.display.encode('utf-8'), rv.data) self.assertNotIn(new_order_date.strftime("%Y-%m-%d").encode('utf-8'), rv.data) self.assertNotIn(new_remark.encode('utf-8'), rv.data)
def test_sales_order_list_create_page(self): user, password = of.user( role_names=['direct_sales_order_create', 'direct_sales_order_view'] ) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, password) self.assertPageRendered(endpoint=url_for('salesorder.index_view')) self.assertPageRendered(endpoint=url_for('salesorder.create_view')) fixture.logout_user(self.test_client)
def test_all_page_rendered(self): user, password = of.user(role_names=[ 'report_view', ]) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, password) self.assertPageRendered(endpoint=url_for('sales_amount.index')) self.assertPageRendered(endpoint=url_for('sales_profit.index')) fixture.logout_user(self.test_client)
def test_all_page_rendered(self): user, password = of.user( role_names=['report_view', ] ) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, password) self.assertPageRendered(endpoint=url_for('sales_amount.index')) self.assertPageRendered(endpoint=url_for('sales_profit.index')) fixture.logout_user(self.test_client)
def test_logic(): supplier = object_faker.supplier() db_util.save_objects_commit(supplier) self.assertPageRendered(endpoint=url_for('dpo.index_view')) self.assertPageRendered(endpoint=url_for('dpo.create_view')) draft_status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) order_date = object_faker.faker.date_time_this_year() logistic_amount = random.randint(0, 100) remark = object_faker.faker.text(max_nb_chars=50) expect_content = [ supplier.name, draft_status.display, str(logistic_amount), order_date.strftime("%Y-%m-%d"), remark ] self.assertPageRendered(method=self.test_client.post, data=dict(supplier=supplier.id, status=draft_status.id, order_date=order_date, logistic_amount=logistic_amount, remark=remark), endpoint=self.create_endpoint(view='dpo'), expect_contents=expect_content) self.assertPageRendered(expect_contents=expect_content, endpoint=self.edit_endpoint(view='dpo')) new_remark = object_faker.faker.text(max_nb_chars=50) new_logistic_amount = random.randint(0, 100) new_order_date = object_faker.faker.date_time_this_year() new_expect_content = [ supplier.name, draft_status.display, str(new_logistic_amount), new_order_date.strftime("%Y-%m-%d"), new_remark ] self.assertPageRendered(method=self.test_client.post, endpoint=self.edit_endpoint(view='dpo'), data=dict( supplier=supplier.id, status=draft_status.id, order_date=new_order_date, logistic_amount=new_logistic_amount, remark=new_remark), expect_contents=new_expect_content) rv = self.assertPageRendered(method=self.test_client.post, endpoint=url_for('dpo.delete_view'), data=dict( url=url_for('dpo.index_view'), id='1')) self.assertNotIn(supplier.name.encode('utf-8'), rv.data) self.assertNotIn(draft_status.display.encode('utf-8'), rv.data) self.assertNotIn( new_order_date.strftime("%Y-%m-%d").encode('utf-8'), rv.data) self.assertNotIn(new_remark.encode('utf-8'), rv.data)
def test_logic(): po1, po2 = of.purchase_order(), of.purchase_order() from psi.app.models import EnumValues po1.status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) po2.status = EnumValues.get(const.PO_ISSUED_STATUS_KEY) db_util.save_objects_commit(po1, po2) po3 = PurchaseOrder.status_filter((const.PO_DRAFT_STATUS_KEY,)).all()[0] self.assertIsNotNone(po3) self.assertEqual(po1, po3) po4 = PurchaseOrder.status_filter((const.PO_ISSUED_STATUS_KEY,)).all()[0] self.assertIsNotNone(po4) self.assertEqual(po2, po4)
def test_franchise_purchase_order_pages(self): from psi.app.models import EnumValues, Organization org_type = EnumValues.get(const.FRANCHISE_STORE_ORG_TYPE_KEY) organization = object_faker.organization(parent=Organization.query.get(1), type=org_type) user, pwd = object_faker.user( role_names=['franchise_purchase_order_create', 'franchise_purchase_order_edit', 'franchise_purchase_order_delete', 'franchise_purchase_order_view'], organization=organization) db_util.save_objects_commit(user, organization) login_user(self.test_client, user.email, pwd) self.assertPageRendered(url_for('fpo.index_view'))
def parent(self, value): from sqlalchemy import text from psi.app.utils import db_util if value is not None: max_lft = value.rgt - 1 sql = text( '{u} rgt = rgt + 2 WHERE rgt > {val};{u} lft = lft + 2 WHERE ' 'lft > {val}'.format(val=max_lft, u=self.uos)) # set left and right of the new object self.lft = max_lft + 1 self.rgt = max_lft + 2 db.engine.execute(sql) db_util.save_objects_commit(self)
def test_logic(): po1, po2 = of.purchase_order(), of.purchase_order() from psi.app.models import EnumValues po1.status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) po2.status = EnumValues.get(const.PO_ISSUED_STATUS_KEY) db_util.save_objects_commit(po1, po2) po3 = PurchaseOrder.status_filter( (const.PO_DRAFT_STATUS_KEY, )).all()[0] self.assertIsNotNone(po3) self.assertEqual(po1, po3) po4 = PurchaseOrder.status_filter( (const.PO_ISSUED_STATUS_KEY, )).all()[0] self.assertIsNotNone(po4) self.assertEqual(po2, po4)
def test_logic(): type = EnumValues.get(DIRECT_PO_TYPE_KEY) status = EnumValues.get(PO_ISSUED_STATUS_KEY) draft_status = EnumValues.get(RECEIVING_DRAFT_STATUS_KEY) date = object_faker.faker.date_time_this_year() po = object_faker.purchase_order(number_of_line=2, type=type, status=status) db_util.save_objects_commit(po) remark = object_faker.faker.text(max_nb_chars=50) # Crete new receiving self.assertPageRendered( method=self.test_client.post, data=dict(purchase_order=po.id, status=draft_status.id, create_lines='y', date=date, remark=remark), endpoint=self.create_endpoint(view='receiving'), ) # Change status to complete new_remark = object_faker.faker.text(max_nb_chars=50) new_receive_date = object_faker.faker.date_time_this_year() complete_status = EnumValues.get(RECEIVING_COMPLETE_STATUS_KEY) self.assertPageRendered( method=self.test_client.post, endpoint=self.edit_endpoint(view='receiving'), data=dict(date=new_receive_date, status=complete_status.id, remark=new_remark), ) # Should not delete existing receiving with complete status endpoint = url_for('receiving.delete_view', id='1') data = dict(url=url_for('receiving.index_view'), id='1') rv = self.assertPageRendered(method=self.test_client.post, endpoint=endpoint, data=data) self.assertIn(complete_status.display, rv.data) self.assertIn(new_receive_date.strftime("%Y-%m-%d"), rv.data) self.assertIn(new_remark, rv.data) self.assertIn(po.supplier.name, rv.data) self.assertIn(po.order_date.strftime("%Y-%m-%d"), rv.data) self.assertIn(po.remark, rv.data) self.assertIn( 'Receiving document can not be update nor delete on complete status', rv.data)
def test_create(self): from psi.app.models import EnumValues type_id = EnumValues.get(const.DIRECT_SELLING_STORE_ORG_TYPE_KEY).id with self.test_client: fixture.login_as_admin(self.test_client) org_name = object_faker.faker.name() org_desc = object_faker.faker.text(max_nb_chars=20) create_url = self.create_endpoint(view='organization') self.assertPageRendered( endpoint=create_url, method=self.test_client.get, expect_contents=['betterlife', '直营店']) self.assertPageRendered( endpoint=create_url, method=self.test_client.post, expect_contents=[org_name, org_desc], data={ "type": type_id, "name": org_name, "description": org_desc, "parent": 1 }) self.assertDeleteSuccessful( endpoint=url_for( 'organization.delete_view', id=2, url=url_for('organization.index_view')), deleted_data=[org_name, org_desc]) from psi.app.models import Organization user, pwd = object_faker.user( role_names=[ 'organization_create', 'organization_view', 'organization_delete', 'organization_edit' ], organization=Organization.query.get(1)) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, pwd) from psi.app.models import EnumValues org_type = EnumValues.get(const.DIRECT_SELLING_STORE_ORG_TYPE_KEY) self.assertCreateFail( endpoint=create_url, create_data=[org_name, org_desc], data={ "type": org_type.id, "name": org_name, "description": org_desc, "parent": 1 })
def test_franchise_purchase_order_pages(self): from psi.app.models import EnumValues, Organization org_type = EnumValues.get(const.FRANCHISE_STORE_ORG_TYPE_KEY) organization = object_faker.organization( parent=Organization.query.get(1), type=org_type) user, pwd = object_faker.user(role_names=[ 'franchise_purchase_order_create', 'franchise_purchase_order_edit', 'franchise_purchase_order_delete', 'franchise_purchase_order_view' ], organization=organization) db_util.save_objects_commit(user, organization) login_user(self.test_client, user.email, pwd) self.assertPageRendered(url_for('fpo.index_view'))
def test_logic(): fixture.login_as_admin(self.test_client) user, password = object_faker.user(role_names=[ 'franchise_sales_order_create', 'franchise_sales_order_view', 'product_view' ]) sales_order = object_faker.sales_order(creator=user, number_of_line=1) db_util.save_objects_commit(sales_order, user) so_id = sales_order.id delivered_status = EnumValues.get(SO_DELIVERED_STATUS_KEY) fixture.login_user(self.test_client, user.email, password) rv = self.test_client.put('/api/sales_order/' + str(so_id), follow_redirects=True, data=dict(status_id=delivered_status.id)) self.assertEqual(rv.status_code, 403)
def test_enough_inventory(self): from psi.app.services.purchase_order import PurchaseOrderService with self.test_client: from tests.fixture import login_as_admin from psi.app.services import SalesOrderService from psi.app.models import EnumValues login_as_admin(self.test_client) po = of.purchase_order(number_of_line=2, type=EnumValues.get( const.DIRECT_PO_TYPE_KEY)) products = [l.product for l in po.lines] receiving = PurchaseOrderService.create_receiving_if_not_exist(po) receiving.status = EnumValues.get( const.RECEIVING_COMPLETE_STATUS_KEY) in_trans_line = receiving.operate_inv_trans_by_recv_status() po = receiving.update_purchase_order_status() from psi.app.utils import db_util db_util.save_objects_commit(po, receiving, in_trans_line) so = of.sales_order(products=products, number_of_line=2) shipping = SalesOrderService.create_or_update_shipping(so) db_util.save_objects_commit(so, shipping) out_inv_trans = shipping.inventory_transaction self.assertIsNotNone(out_inv_trans) self.assertEquals(2, len(out_inv_trans.lines)) for l in shipping.lines: self.assertEquals(1, len(l.inventory_links)) link = l.inventory_links[0] self.assertIsNotNone(link) so_line = None for l in so.lines: if l.product.id == link.product.id: self.assertEquals(link.out_price, l.unit_price) self.assertEquals(link.out_quantity, l.quantity) so_line = l for recv_l in receiving.lines: if recv_l.product.id == link.product.id: self.assertEquals(link.in_price, recv_l.price) in_trans_line = recv_l.inventory_transaction_line remain_qty = recv_l.purchase_order_line.quantity - so_line.quantity if remain_qty < 0: self.assertEquals(0, in_trans_line.saleable_quantity) else: self.assertEquals(remain_qty, in_trans_line.saleable_quantity)
def on_model_change(self, form, model, is_created): from wtforms import ValidationError super(FranchisePurchaseOrderAdmin, self).on_model_change(form, model, is_created) for l in model.lines: if l.unit_price is None: l.unit_price = l.product.franchise_price if current_user.organization.type.code != const.FRANCHISE_STORE_ORG_TYPE_KEY: raise ValidationError(gettext("Your organization is not a franchise store and is not allowed to create franchise purchase order")) if current_user.organization.parent is None: raise ValidationError(gettext("Franchise purchase order creation failed, your organization does not have a valid parent organization")) if is_created: model.to_organization = current_user.organization.parent status = model.status if status.code == const.PO_ISSUED_STATUS_KEY: sales_order, incoming, expense = self.create_so_from_fpo(model) related_value = self.create_related_value(sales_order, model) db_util.save_objects_commit(sales_order, incoming, expense, related_value)
def test_logic(): fixture.login_as_admin(self.test_client) user, password = object_faker.user(role_names=[ 'franchise_sales_order_create', 'franchise_sales_order_view', 'franchise_sales_order_edit', 'product_view' ]) sales_order = object_faker.sales_order(creator=user, number_of_line=1) db_util.save_objects_commit(sales_order, user) so_id = sales_order.id fixture.login_user(self.test_client, user.email, password) rv = self.test_client.put('/api/sales_order/' + str(so_id), follow_redirects=True, data=dict(status_id=5000)) self.assertEqual(rv.status_code, 201) self.assertIn(b'message', rv.data) self.assertIn(b'Invalid sales order status parameter', rv.data)
def test_all_page_rendered(self): user, password = of.user( role_names=['sales_report_view', ] ) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, password) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='overall')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='today')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='yesterday')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='this_week')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='last_week')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='this_month')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='last_month')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='this_quarter')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='last_quarter')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='this_year')) self.assertPageRendered(endpoint=url_for('supplier_sales_report.index_view', type='last_year')) fixture.logout_user(self.test_client)
def test_logic(): type = EnumValues.get(DIRECT_PO_TYPE_KEY) status = EnumValues.get(PO_ISSUED_STATUS_KEY) receiving_status = EnumValues.get(RECEIVING_DRAFT_STATUS_KEY) date = object_faker.faker.date_time_this_year() po = object_faker.purchase_order(number_of_line=2, type=type, status=status) db_util.save_objects_commit(po) remark = object_faker.faker.text(max_nb_chars=50) list_expect = [receiving_status.display, date.strftime("%Y-%m-%d"), remark, po.supplier.name, po.order_date.strftime("%Y-%m-%d"), po.remark] edit_expect = [receiving_status.display, date.strftime("%Y-%m-%d"), remark,] # Crete new receiving self.assertPageRendered(method=self.test_client.post, data=dict(purchase_order=po.id, status=receiving_status.id, create_lines='y', date=date, remark=remark), endpoint=self.create_endpoint(view='receiving'), expect_contents=list_expect) self.assertPageRendered(expect_contents=edit_expect, endpoint=self.edit_endpoint(view='receiving')) # Edit existing receiving new_remark = object_faker.faker.text(max_nb_chars=50) new_receive_date = object_faker.faker.date_time_this_year() complete_status = EnumValues.get(RECEIVING_COMPLETE_STATUS_KEY) new_expected = [complete_status.display, new_receive_date.strftime("%Y-%m-%d"), new_remark, po.supplier.name, po.order_date.strftime("%Y-%m-%d"), po.remark] self.assertPageRendered(method=self.test_client.post, endpoint=self.edit_endpoint(view='receiving'), data=dict(date=new_receive_date, status=complete_status.id, remark=new_remark), expect_contents=new_expected) # Detail page self.assertPageRendered(method=self.test_client.get, endpoint=self.details_endpoint(view='receiving'), expect_contents=new_expected)
def test_logic(): fixture.login_as_admin(self.test_client) user, password = object_faker.user(role_names=[ 'franchise_sales_order_create', 'franchise_sales_order_view', 'franchise_sales_order_edit', 'product_view' ]) sales_order = object_faker.sales_order(creator=user, number_of_line=1) shipped_status = EnumValues.get(SO_SHIPPED_STATUS_KEY) sales_order.status = shipped_status db_util.save_objects_commit(sales_order, user) so_id = sales_order.id fixture.login_user(self.test_client, user.email, password) rv = self.test_client.put('/api/sales_order/' + str(so_id), follow_redirects=True, data=dict(status_id=shipped_status.id)) self.assertEqual(rv.status_code, 201) self.assertIn(b'message', rv.data) self.assertIn(b'Status update not allowed', rv.data)
def test_to_organization_hide(self): from psi.app.models import EnumValues, PurchaseOrder, Organization from psi.app.service import Info from datetime import datetime t = EnumValues.get(const.FRANCHISE_STORE_ORG_TYPE_KEY) draft_status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) draft_status_id = draft_status.id parent = Info.get_db().session.query(Organization).get(1) organization = object_faker.organization(type=t, parent=parent) user, password = object_faker.user([ 'franchise_purchase_order_create', 'franchise_purchase_order_edit', 'franchise_purchase_order_view', 'franchise_purchase_order_delete' ], organization=organization) db_util.save_objects_commit(user) fixture.login_user(self.test_client, user.email, password) rv = self.test_client.get(url_for('fpo.create_view'), follow_redirects=True) self.assertEqual(rv.status_code, 200) self.assertNotIn('to_organization', rv.data) remark = u'-备注信息-' order_date = '2016-08-25 23:18:55' rv = self.test_client.post(url_for('fpo.create_view'), follow_redirects=True, data=dict(status=draft_status_id, logistic_amount=20, order_date=order_date, _continue_editing=u'保存并继续编辑', remark=remark)) self.assertEquals(rv.status_code, 200) po = PurchaseOrder.query.filter_by(remark=remark).first() self.assertIsNotNone(po) self.assertEquals(po.remark, remark) self.assertEquals(po.logistic_amount, 20) self.assertEquals(po.status.id, draft_status.id) self.assertEqual(po.order_date, datetime.strptime(order_date, "%Y-%m-%d %H:%M:%S")) self.assertEqual(po.to_organization, parent) self.assertEquals(po.organization, organization)
def test_enough_inventory(self): from psi.app.services.purchase_order import PurchaseOrderService with self.test_client: from tests.fixture import login_as_admin from psi.app.services import SalesOrderService from psi.app.models import EnumValues login_as_admin(self.test_client) po = of.purchase_order(number_of_line=2, type=EnumValues.get(const.DIRECT_PO_TYPE_KEY)) products = [l.product for l in po.lines] receiving = PurchaseOrderService.create_receiving_if_not_exist(po) receiving.status = EnumValues.get(const.RECEIVING_COMPLETE_STATUS_KEY) in_trans_line = receiving.operate_inv_trans_by_recv_status() po = receiving.update_purchase_order_status() from psi.app.utils import db_util db_util.save_objects_commit(po, receiving, in_trans_line) so = of.sales_order(products=products, number_of_line=2) shipping = SalesOrderService.create_or_update_shipping(so) db_util.save_objects_commit(so, shipping) out_inv_trans = shipping.inventory_transaction self.assertIsNotNone(out_inv_trans) self.assertEquals(2,len(out_inv_trans.lines)) for l in shipping.lines: self.assertEquals(1, len(l.inventory_links)) link = l.inventory_links[0] self.assertIsNotNone(link) so_line = None for l in so.lines: if l.product.id == link.product.id: self.assertEquals(link.out_price, l.unit_price) self.assertEquals(link.out_quantity, l.quantity) so_line = l for recv_l in receiving.lines: if recv_l.product.id == link.product.id: self.assertEquals(link.in_price, recv_l.price) in_trans_line = recv_l.inventory_transaction_line remain_qty = recv_l.purchase_order_line.quantity - so_line.quantity if remain_qty < 0: self.assertEquals(0, in_trans_line.saleable_quantity) else: self.assertEquals(remain_qty, in_trans_line.saleable_quantity)
def test_logic(): type = EnumValues.get(DIRECT_PO_TYPE_KEY) status = EnumValues.get(PO_ISSUED_STATUS_KEY) draft_status = EnumValues.get(RECEIVING_DRAFT_STATUS_KEY) date = object_faker.faker.date_time_this_year() po = object_faker.purchase_order(number_of_line=2, type=type, status=status) db_util.save_objects_commit(po) remark = object_faker.faker.text(max_nb_chars=50) # Crete new receiving self.assertPageRendered(method=self.test_client.post, data=dict(purchase_order=po.id, status=draft_status.id, create_lines='y', date=date, remark=remark), endpoint=self.create_endpoint(view='receiving'),) # Change status to complete new_remark = object_faker.faker.text(max_nb_chars=50) new_receive_date = object_faker.faker.date_time_this_year() complete_status = EnumValues.get(RECEIVING_COMPLETE_STATUS_KEY) self.assertPageRendered(method=self.test_client.post, endpoint=self.edit_endpoint(view='receiving'), data=dict(date=new_receive_date, status=complete_status.id, remark=new_remark),) # Should not delete existing receiving with complete status endpoint = url_for('receiving.delete_view', id='1') data = dict(url=url_for('receiving.index_view'), id='1') rv = self.assertPageRendered(method=self.test_client.post, endpoint=endpoint, data=data) self.assertIn(complete_status.display.encode('utf-8'), rv.data) self.assertIn(new_receive_date.strftime("%Y-%m-%d").encode('utf-8'), rv.data) self.assertIn(new_remark.encode('utf-8'), rv.data) self.assertIn(po.supplier.name.encode('utf-8'), rv.data) self.assertIn(po.order_date.strftime("%Y-%m-%d").encode('utf-8'),rv.data) self.assertIn(po.remark.encode('utf-8'), rv.data) self.assertIn(b'You are not allowed to delete this object', rv.data)
def test_login(): from psi.app.models import EnumValues f_type = EnumValues.get(const.FRANCHISE_PO_TYPE_KEY) po = object_faker.purchase_order(number_of_line=random.randint( 1, 10), type=f_type) db_util.save_objects_commit(po) from psi.app.views import FranchisePurchaseOrderAdmin sales_order, incoming, expense = FranchisePurchaseOrderAdmin.create_so_from_fpo( po) self.assertEquals(len(po.lines), len(sales_order.lines)) self.assertEqual(sales_order.order_date, po.order_date) self.assertEquals(sales_order.type, EnumValues.get(const.FRANCHISE_SO_TYPE_KEY)) self.assertEquals(sales_order.status, EnumValues.get(const.SO_CREATED_STATUS_KEY)) self.assertEquals(sales_order.organization, po.to_organization) # There's no expense associated with the PO when creating PO for the franchise organization. # That's done on after_model_change in BasePurchaseOrderAdmin class self.assertEquals(sales_order.actual_amount, incoming.amount) self.assertIsNone(expense)
def create_sales_order(self, status_key): from psi.app.models import EnumValues from psi.app.services.purchase_order import PurchaseOrderService user, password = of.user( role_names=['direct_sales_order_create', 'direct_sales_order_view', 'direct_sales_order_edit', 'direct_sales_order_delete'] ) db_util.save_objects_commit(user) fixture.login_as_admin(self.test_client) fixture.login_user(self.test_client, user.email, password) direct_po = EnumValues.get(const.DIRECT_PO_TYPE_KEY) po = of.purchase_order(number_of_line=2, type=direct_po, creator=user) po.status = EnumValues.get(const.PO_ISSUED_STATUS_KEY) fixture.login_as_admin(self.test_client) l_e, g_e, recv = PurchaseOrderService.create_expense_receiving(po) customer = of.customer(creator=user) db_util.save_objects_commit(po, l_e, g_e, recv, customer) fixture.logout_user(self.test_client) fixture.login_user(self.test_client, user.email, password) order_status = EnumValues.get(status_key) order_date = of.faker.date_time_this_year() logistic_amount = random.randint(0, 100) remark = of.faker.text(max_nb_chars=50) data = dict(customer=customer.id, status=order_status.id, order_date=order_date, logistic_amount=logistic_amount, remark=remark) total, data = self.prepare_so_lines_data_from_po(po, data) expect = [customer.name, order_status.display, order_date.strftime("%Y-%m-%d"), remark, str(total)] self.assertPageRendered(method=self.test_client.post, data=data, endpoint=url_for('salesorder.create_view', url=url_for( 'salesorder.index_view')), expect_contents=expect) return data, expect
def create_sales_order(self, status_key): from psi.app.models import EnumValues from psi.app.services.purchase_order import PurchaseOrderService user, password = of.user( role_names=['direct_sales_order_create', 'direct_sales_order_view', 'direct_sales_order_edit', 'direct_sales_order_delete'] ) db_util.save_objects_commit(user) fixture.login_as_admin(self.test_client) fixture.login_user(self.test_client, user.email, password) direct_po = EnumValues.get(const.DIRECT_PO_TYPE_KEY) po = of.purchase_order(number_of_line=2, type=direct_po, creator=user) po.status = EnumValues.get(const.PO_ISSUED_STATUS_KEY) fixture.login_as_admin(self.test_client) l_e, g_e, recv = PurchaseOrderService.create_expense_receiving(po) customer = of.customer(creator=user) db_util.save_objects_commit(po, l_e, g_e, recv, customer) fixture.logout_user(self.test_client) fixture.login_user(self.test_client, user.email, password) order_status = EnumValues.get(status_key) order_date = of.faker.date_time_this_year() logistic_amount = random.randint(0, 100) remark = of.faker.text(max_nb_chars=50) data = dict(customer=customer.id, status=order_status.id, order_date=order_date, logistic_amount=logistic_amount, remark=remark) total, data = self.prepare_so_lines_data_from_po(po, data) expect = [customer.name, order_status.display, order_date.strftime("%Y-%m-%d"), remark, str(total)] self.assertPageRendered(method=self.test_client.post, data=data, endpoint=self.create_endpoint(view='salesorder'), expect_contents=expect) return data, expect
def test_franchise_purchase_order_pages(self): from psi.app.models import EnumValues, Organization with self.test_client: login_as_admin(self.test_client) org_type = EnumValues.get(const.FRANCHISE_STORE_ORG_TYPE_KEY) organization = object_faker.organization( parent=Organization.query.get(1), type=org_type) user, pwd = object_faker.user(role_names=[ 'franchise_purchase_order_create', 'franchise_purchase_order_edit', 'franchise_purchase_order_delete', 'franchise_purchase_order_view' ], organization=organization) self.test_client.post('/login', data=dict(email_or_login=user.email, password=pwd), follow_redirects=True) db_util.save_objects_commit(user, organization) login_user(self.test_client, user.email, pwd) self.assertPageRendered(url_for('fpo.index_view')) self.assertPageRendered(url_for('fpo.create_view')) draft_status = EnumValues.get(const.PO_DRAFT_STATUS_KEY) order_date = object_faker.faker.date_time_this_year() logistic_amount = random.randint(0, 100) remark = object_faker.faker.text(max_nb_chars=50) expect_contents = [ draft_status.display, str(logistic_amount), order_date.strftime("%Y-%m-%d"), remark ] self.assertPageRendered(method=self.test_client.post, data=dict(status=draft_status.id, order_date=order_date, logistic_amount=logistic_amount, remark=remark), endpoint=url_for( 'fpo.create_view', url=url_for('fpo.index_view')), expect_contents=expect_contents) self.assertPageRendered(expect_contents=expect_contents, endpoint=url_for('fpo.edit_view', url=url_for( 'fpo.details_view', id=1), id=1)) new_remark = object_faker.faker.text(max_nb_chars=50) new_logistic_amount = random.randint(0, 100) new_order_date = object_faker.faker.date_time_this_year() new_expect_contents = [ draft_status.display, str(new_logistic_amount), new_order_date.strftime("%Y-%m-%d"), new_remark ] self.assertPageRendered( method=self.test_client.post, endpoint=url_for('fpo.edit_view', url=url_for('fpo.index_view'), id=1), data=dict(status=draft_status.id, order_date=new_order_date, logistic_amount=new_logistic_amount, remark=new_remark), expect_contents=new_expect_contents) rv = self.assertDeleteSuccessful( endpoint=url_for('fpo.delete_view'), deleted_data=[ draft_status.display, new_remark, new_order_date.strftime("%Y-%m-%d") ], data=dict(url=url_for('fpo.index_view'), id='1'))