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(): 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_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(): 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(): po = object_faker.purchase_order(number_of_line=2) receiving = Receiving.create_draft_recv_from_po(po) self.assertEquals(po, receiving.purchase_order) self.assertEquals(len(po.lines), len(receiving.lines)) self.assertEquals(len(po.lines), len(receiving.inventory_transaction.lines)) self.assertEquals(po.order_date, receiving.date) self.assertEquals(receiving.status.code, const.RECEIVING_DRAFT_STATUS_KEY) self.assertEquals(po.supplier, receiving.supplier) self.assertEquals(receiving.inventory_transaction.date, po.order_date) self.assertEquals(receiving.inventory_transaction.type.code, const.PURCHASE_IN_INV_TRANS_KEY) self.assertIsNotNone(receiving.inventory_transaction) for line in receiving.lines: self.assertIsNotNone(line) self.assertIsNotNone(line.purchase_order_line) self.assertIsNotNone(line.inventory_transaction_line) self.assertEquals(line.quantity, line.purchase_order_line.quantity) self.assertEquals(line.price, line.purchase_order_line.unit_price) self.assertEquals(line.product, line.purchase_order_line.product) self.assertEquals( line.inventory_transaction_line.in_transit_quantity, line.quantity) self.assertEquals(line.inventory_transaction_line.product, line.product) self.assertEquals(line.inventory_transaction_line.price, line.price)
def test_purchase_price_show_and_hidden_detail_page(self): from tests.fixture import run_as_admin user, password = object_faker.user(role_names=[ 'purchase_price_view', 'direct_purchase_order_view', 'product_view' ]) po = object_faker.purchase_order(number_of_line=1, creator=user) po_url = url_for('dpo.details_view', id=po.id) product_url = url_for('product.details_view', id=po.lines[0].product.id) run_as_admin(self.test_client, self.logic_for_detail_edit_page, user, password, po, po_url, product_url)
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_logic(): po = object_faker.purchase_order() po.status = EnumValues.get(const.PO_RECEIVED_STATUS_KEY) receiving = Receiving() receiving.purchase_order = po receiving.date = datetime.now() receiving.status = EnumValues.get(const.RECEIVING_DRAFT_STATUS_KEY) db = Info.get_db() db.session.add(po) db.session.add(receiving) db.session.commit() receiving_returned = receiving.filter_by_po_id(1)[0] self.assertEqual(receiving, receiving_returned)
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) 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 generate_fake_order(): """ Load a set of fake data to the system * 10 Suppliers and customers * 5 purchase orders and sales_orders """ from tests.object_faker import object_faker from psi.app.models import User from random import randint user = database.session.query(User).get(1) for i in range(5): purchase_order = object_faker.purchase_order(creator=user, number_of_line=randint( 1, 9)) sales_order = object_faker.sales_order(creator=user, number_of_line=randint(1, 9)) database.session.add(purchase_order) database.session.add(sales_order) database.session.commit()
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_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_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