예제 #1
0
    def _prepare_sales_orders_and_items(self):
        self.expected_programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.expected_programme_1.save()
        self.expected_programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.expected_programme_2.save()
        self.sales_order_1 = SalesOrder(programme=self.expected_programme_1,
                                        order_number=20173918,
                                        date=datetime.date(2015, 12, 3))
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(programme=self.expected_programme_2,
                                        order_number=20174363,
                                        date=datetime.date(2015, 12, 14))
        self.sales_order_2.save()

        self.item_1 = Item(description='Scale,electronic,mother/child,150kgx100g',
                           material_code='S0141021')
        self.item_2 = Item(description='Laundry soap, Carton, 25 bars, 800 grams',
                           material_code='SL009100')

        self.sales_item_1 = SalesOrderItem(sales_order=self.sales_order_1,
                                           item=self.item_1,
                                           net_price=0,
                                           net_value=Decimal('51322.6500'),
                                           issue_date=datetime.date(2015, 12, 3),
                                           delivery_date=datetime.date(2015, 12, 3),
                                           description='Scale,electronic,mother/child,150kgx100g')
        self.sales_item_2 = SalesOrderItem(sales_order=self.sales_order_2,
                                           item=self.item_2,
                                           net_price=0,
                                           net_value=Decimal('2673'),
                                           issue_date=datetime.date(2015, 12, 14),
                                           delivery_date=datetime.date(2015, 12, 14),
                                           description='Laundry soap, Carton, 25 bars, 800 grams')
예제 #2
0
    def _prepare_sales_orders_and_items(self):
        self.expected_programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.expected_programme_1.save()
        self.expected_programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.expected_programme_2.save()
        self.sales_order_1 = SalesOrder(programme=self.expected_programme_1,
                                        order_number=20173918,
                                        date=datetime.date(2015, 12, 3))
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(programme=self.expected_programme_2,
                                        order_number=20174363,
                                        date=datetime.date(2015, 12, 14))
        self.sales_order_2.save()

        self.item_1 = Item(description='Scale,electronic,mother/child,150kgx100g',
                           material_code='S0141021')
        self.item_2 = Item(description='Laundry soap, Carton, 25 bars, 800 grams',
                           material_code='SL009100')

        self.sales_item_1 = SalesOrderItem(sales_order=self.sales_order_1,
                                           item=self.item_1,
                                           net_price=0,
                                           net_value=Decimal('51322.6500'),
                                           issue_date=datetime.date(2015, 12, 3),
                                           delivery_date=datetime.date(2015, 12, 3),
                                           description='Scale,electronic,mother/child,150kgx100g')
        self.sales_item_2 = SalesOrderItem(sales_order=self.sales_order_2,
                                           item=self.item_2,
                                           net_price=0,
                                           net_value=Decimal('2673'),
                                           issue_date=datetime.date(2015, 12, 14),
                                           delivery_date=datetime.date(2015, 12, 14),
                                           description='Laundry soap, Carton, 25 bars, 800 grams')
예제 #3
0
    def setUp(self):
        self.downloaded_programme = {'GetProgrammeStructureList_JSONResult':
                                         '[{"BUSINESS_AREA_CODE":"4380",' \
                                         '"BUSINESS_AREA_NAME":"Uganda",' \
                                         '"COUNTRY_PROGRAMME_WBS":"4380/A0/04",' \
                                         '"COUNTRY_PROGRAMME_NAME":"UGANDA COUNTRY PROGRAM (2010 - 2015) EXT",' \
                                         '"CP_START_DATE":"\\/Date(1262322000000)\\/",' \
                                         '"CP_END_DATE":"\\/Date(1451538000000)\\/",' \
                                         '"OUTCOME_WBS":"4380/A0/04/105",' \
                                         '"OUTCOME_ID":105,' \
                                         '"OUTCOME_DESCRIPTION":"YI105 - PCR 1 KEEP CHILDREN AND MOTHERS",' \
                                         '"OUTPUT_WBS":"4380/A0/04/105/004",' \
                                         '"OUTPUT_ID":4,' \
                                         '"OUTPUT_DESCRIPTION":"IR 1.4 NUTRITION",' \
                                         '"ACTIVITY_WBS":"4380/A0/04/105/004/090",' \
                                         '"ACTIVITY_ID":90,' \
                                         '"ACTIVITY_DESCRIPTION":"1.4.90 STAFF, TRAVEL AND RELATED COSTS",' \
                                         '"SIC_CODE":"04-06-01",' \
                                         '"SIC_NAME":"Nutrition # General",' \
                                         '"GIC_CODE":"013",' \
                                         '"GIC_NAME":"Operating costs # staff",' \
                                         '"HUMANITARIAN_TAG":"No",' \
                                         '"ACTIVITY_FOCUS_CODE":"6",' \
                                         '"ACTIVITY_FOCUS_NAME":"6 Management/Operations"},' \
                                         '' \
                                         '{"BUSINESS_AREA_CODE":"4380",' \
                                         '"BUSINESS_AREA_NAME":"Uganda",' \
                                         '"COUNTRY_PROGRAMME_WBS":"4380/A0/05",' \
                                         '"COUNTRY_PROGRAMME_NAME":"UGANDA CONTRY PROGRAMME (2016 - 2020)",' \
                                         '"CP_START_DATE":"\\/Date(1451624400000)\\/",' \
                                         '"CP_END_DATE":"\\/Date(1609390800000)\\/",' \
                                         '"OUTCOME_WBS":"4380/A0/05/113",' \
                                         '"OUTCOME_ID":113,' \
                                         '"OUTCOME_DESCRIPTION":"3: CHILD PROTECTION",' \
                                         '"OUTPUT_WBS":"4380/A0/05/113/003",' \
                                         '"OUTPUT_ID":3,' \
                                         '"OUTPUT_DESCRIPTION":"OUTPUT 3.3 BIRTH REGISTRATION",' \
                                         '"ACTIVITY_WBS":"4380/A0/05/113/003/001",' \
                                         '"ACTIVITY_ID":1,' \
                                         '"ACTIVITY_DESCRIPTION":"3.3.90 STAFF COSTS",' \
                                         '"SIC_CODE":"06-05-01",' \
                                         '"SIC_NAME":"Birth and civil registration",' \
                                         '"GIC_CODE":"013",' \
                                         '"GIC_NAME":"Operating costs # staff",' \
                                         '"HUMANITARIAN_TAG":"No",' \
                                         '"ACTIVITY_FOCUS_CODE":"6",' \
                                         '"ACTIVITY_FOCUS_NAME":"6 Management/Operations"}]'}

        self.expected_programme_1 = Programme(
            wbs_element_ex='4380/A0/04/105',
            name='YI105 - PCR 1 KEEP CHILDREN AND MOTHERS')
        self.expected_programme_2 = Programme(wbs_element_ex='4380/A0/05/113',
                                              name='3: CHILD PROTECTION')

        self.synchronizer = ProgrammeSynchronizer()
예제 #4
0
    def assert_programmes_were_created(self):
        programme_one = Programme(name='YI107 - PCR 3 KEEP CHILDREN SAFE',
                                  wbs_element_ex='4380/A0/04/107')
        programme_two = Programme(
            name='YI105 - PCR 1 KEEP CHILDREN AND MOTHERS',
            wbs_element_ex='4380/A0/04/105')
        programme_three = Programme(name='Y108 - PCR 4 CROSS SECTORAL',
                                    wbs_element_ex='4380/A0/04/108')

        self.assert_programmes_are_equal(programme_one,
                                         Programme.objects.all()[0])
        self.assert_programmes_are_equal(programme_two,
                                         Programme.objects.all()[1])
        self.assert_programmes_are_equal(programme_three,
                                         Programme.objects.all()[2])
예제 #5
0
    def setUp(self):
        self.downloaded_programme = downloaded_programme
        self.expected_programme = Programme(
            wbs_element_ex='4380/A0/04/105',
            name='YI105 - PCR 1 KEEP CHILDREN AND MOTHERS')

        self.synchronizer = ProgrammeSynchronizer()
예제 #6
0
    def _prepare_sales_and_purchase_order(self):
        Consignee.objects.create(customer_id='L626010000')
        Consignee.objects.create(customer_id='L626010384')
        self.programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.programme_1.save()
        self.programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.programme_2.save()
        self.sales_order_1 = SalesOrder(programme=self.programme_1,
                                        order_number=20173918,
                                        date=datetime.date(2015, 12, 3))
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(programme=self.programme_2,
                                        order_number=20174363,
                                        date=datetime.date(2015, 12, 14))
        self.sales_order_2.save()

        self.purchase_order_1 = PurchaseOrder(order_number=45143984,
                                              sales_order=self.sales_order_1,
                                              date=datetime.date(2015, 11, 30),
                                              po_type='NB')
        self.purchase_order_1.save()
        self.purchase_order_2 = PurchaseOrder(order_number=45144863,
                                              sales_order=self.sales_order_2,
                                              date=datetime.date(2015, 12, 14),
                                              po_type='ZLC')
        self.purchase_order_2.save()

        self.item_1 = Item(description='Scale,electronic,mother/child,150kgx100g',
                           material_code='S0141021')
        self.item_1.save()
        self.item_2 = Item(description='Laundry soap, Carton, 25 bars, 800 grams',
                           material_code='SL009100')
        self.item_2.save()
        self.purchase_order_item_1 = PurchaseOrderItem(purchase_order=self.purchase_order_1,
                                                       item=self.item_1,
                                                       item_number=10,
                                                       sales_order_item=None,
                                                       quantity=100.00,
                                                       value=Decimal('51322.65'))
        self.purchase_order_item_1.save()
        self.purchase_order_item_2 = PurchaseOrderItem(purchase_order=self.purchase_order_2,
                                                       item=self.item_2,
                                                       item_number=20,
                                                       sales_order_item=None,
                                                       quantity=80.00,
                                                       value=Decimal('2673'))
        self.purchase_order_item_2.save()
예제 #7
0
    def setUp(self):
        self.downloaded_sales_orders = downloaded_sales_orders
        self.converted_sales_orders = converted_sales_orders

        Programme.objects.create(wbs_element_ex='0060/A0/07/883')

        self.expected_programme = Programme(wbs_element_ex='0060/A0/07/883')
        self.expected_sales_order = SalesOrder(programme=self.expected_programme,
                                               order_number=20173918,
                                               date=datetime.date(2015, 12, 6))
        self.expected_item = Item(description='Scale,electronic,mother/child,150kgx100g',
                                  material_code='S0141021')
        self.expected_sales_order_item = SalesOrderItem(sales_order=self.expected_sales_order,
                                                        item=self.expected_item,
                                                        net_price=0,
                                                        net_value=Decimal('51322.6500'),
                                                        issue_date=datetime.date(2015, 12, 6),
                                                        delivery_date=datetime.date(2015, 12, 6),
                                                        description='Scale,electronic,mother/child,150kgx100g')
        start_date = '01122015'
        end_date = datetime.date.today().strftime('%d%m%Y')
        self.synchronizer = SalesOrderSynchronizer(start_date=start_date)
        base_url = 'https://devapis.unicef.org/BIService/BIWebService.svc/GetSalesOrderInfo_JSON/'
        self.expected_url = base_url + start_date + '/' + end_date
예제 #8
0
class TestSyncPurchaseOrder(TestCase):
    def setUp(self):
        self.downloaded_purchase_orders = downloaded_purchase_orders
        self.converted_purchase_orders = converted_purchase_orders

        self.purchase_order_which_do_not_have_reference_to_sales_order = \
            purchase_order_which_do_not_have_reference_to_sales_order
        self.purchase_order_item_which_do_not_have_reference_to_sales_order_item = \
            purchase_order_item_which_do_not_have_reference_to_sales_order_item
        self.purchase_order_item_with_invalid_po_type = \
            purchase_order_item_with_invalid_po_type

        self._prepare_sales_orders_and_items()
        self.expected_purchase_order = PurchaseOrder(order_number=45143984,
                                                     sales_order=self.sales_order_1,
                                                     date=datetime.date(2015, 11, 30),
                                                     po_type='NB')
        self.expected_purchase_order_item = PurchaseOrderItem(purchase_order=self.expected_purchase_order,
                                                              item=self.item_1,
                                                              item_number=10,
                                                              sales_order_item=self.sales_item_1,
                                                              quantity=100.00,
                                                              value=Decimal('51322.65'))
        start_date = '01122015'
        end_date = datetime.date.today().strftime('%d%m%Y')
        self.synchronizer = PurchaseOrderSynchronizer(start_date=start_date)
        base_url = 'https://devapis.unicef.org/BIService/BIWebService.svc/GetPurchaseOrderInfo_JSON/'
        self.expected_url = base_url + start_date + '/' + end_date

    def tearDown(self):
        SalesOrder.objects.all().delete()
        SalesOrderItem.objects.all().delete()
        Item.objects.all().delete()
        OrderItem.objects.all().delete()
        Programme.objects.all().delete()
        PurchaseOrder.objects.all().delete()
        PurchaseOrderItem.objects.all().delete()
        DistributionPlan.objects.all().delete()
        DistributionPlanNode.objects.all().delete()

    def test_should_point_to_correct_endpoint(self):
        self.synchronizer._load_records = MagicMock(return_value=[])
        self.synchronizer.sync()

        self.assertEqual(self.synchronizer.url, self.expected_url)

    def test_should_load_purchase_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._load_records.assert_called()
        self.synchronizer._save_records.assert_called()

    def test_should_convert_purchase_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._save_records.assert_called_with(self.converted_purchase_orders)

    def test_should_save_purchase_orders(self):
        self._sync_purchase_order()
        purchase_order = PurchaseOrder.objects.all().first()

        self._assert_purchase_order_equal(purchase_order, self.expected_purchase_order)

    def test_should_save_purchase_order_items(self):
        self._sync_purchase_order()

        expected_order_number_one = self.expected_purchase_order.order_number

        actual_purchase_item = PurchaseOrderItem.objects.get(purchase_order__order_number=expected_order_number_one)

        self._assert_purchase_order_item_equal(actual_purchase_item, self.expected_purchase_order_item)

    def test_should_NOT_import_purchase_order_which_can_not_refer_to_existing_sales_order(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = \
            MagicMock(return_value=self.purchase_order_which_do_not_have_reference_to_sales_order)
        self.synchronizer.sync()

        self.assertEqual(PurchaseOrder.objects.count(), 0)
        self.assertEqual(PurchaseOrderItem.objects.count(), 0)

    def test_should_import_purchase_order_item_which_can_not_refer_to_existing_sales_order_item(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = \
            MagicMock(return_value=self.purchase_order_item_which_do_not_have_reference_to_sales_order_item)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.all()[0]
        purchase_order_item = PurchaseOrderItem.objects.all()[0]

        self._assert_purchase_order_equal(purchase_order, self.expected_purchase_order)
        self.assertEqual(purchase_order_item.sales_order_item, None)

    def test_should_NOT_import_purchase_order_with_invalid_po_type(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = MagicMock(return_value=self.purchase_order_item_with_invalid_po_type)
        self.synchronizer.sync()

        self.assertEqual(PurchaseOrder.objects.count(), 0)
        self.assertEqual(PurchaseOrderItem.objects.count(), 0)

    def test_should_NOT_update_when_got_an_older_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()
        older_purchase_order = [{"PURCHASING_ORG_CODE": "1000",
                                 "PURCHASING_GROUP_CODE": "100",
                                 "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                                 "PLANT_CODE": "1000",
                                 "VENDOR_CODE": "1900000501",
                                 "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                                 "VENDOR_CTRY_NAME": "Indonesia",
                                 "GRANT_REF": "XP154478",
                                 "EXPIRY_DATE": u"/Date(1483160400000)/",
                                 "DONOR_NAME": "Ministry of National Health",
                                 "PREQ_NO": "0030344125",
                                 "PREQ_ITEM": 80,
                                 "PREQ_QTY": 51322.65,
                                 "SO_NUMBER": u"0020173918",
                                 "PO_NUMBER": u"0045143984",
                                 "PO_ITEM": 10,
                                 "PO_TYPE": "NB",
                                 "PO_DATE": u"/Date(1448859600000)/",
                                 "CREATE_DATE": u"/Date(1440859600000)/",
                                 "UPDATE_DATE": u"/Date(1440859600000)/",
                                 "PO_ITEM_QTY": 99,
                                 "CURRENCY_CODE": "USD",
                                 "AMOUNT_CURR": 51322.65,
                                 "AMOUNT_USD": 51322.65,
                                 "MATERIAL_CODE": "S0141021",
                                 "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g"}]

        self.synchronizer._load_records = MagicMock(return_value=older_purchase_order)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.get(order_number=45143984)
        self.assertEqual(purchase_order.date, datetime.datetime(2015, 11, 30, 13, 0).date())

        purchase_order_item = PurchaseOrderItem.objects.get(purchase_order=purchase_order, item_number=10)
        self.assertEqual(purchase_order_item.quantity, 100)

    def test_should_update_when_got_a_newer_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()
        older_purchase_order = [{"PURCHASING_ORG_CODE": "1000",
                                 "PURCHASING_GROUP_CODE": u"100",
                                 "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                                 "PLANT_CODE": "1000",
                                 "VENDOR_CODE": "1900000501",
                                 "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                                 "VENDOR_CTRY_NAME": "Indonesia",
                                 "GRANT_REF": "XP154478",
                                 "EXPIRY_DATE": u"/Date(1483160400000)/",
                                 "DONOR_NAME": "Ministry of National Health",
                                 "PREQ_NO": "0030344125",
                                 "PREQ_ITEM": 80,
                                 "PREQ_QTY": 51322.65,
                                 "SO_NUMBER": u"0020173918",
                                 "PO_NUMBER": u"0045143984",
                                 "PO_ITEM": 10,
                                 "PO_TYPE": "NB",
                                 "PO_DATE": u"/Date(1448859600000)/",
                                 "CREATE_DATE": u"/Date(1458859600000)/",
                                 "UPDATE_DATE": u"/Date(1458859600000)/",
                                 "PO_ITEM_QTY": 99,
                                 "CURRENCY_CODE": "USD",
                                 "AMOUNT_CURR": 51322.65,
                                 "AMOUNT_USD": 51322.65,
                                 "MATERIAL_CODE": "S0141021",
                                 "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g"}]

        self.synchronizer._load_records = MagicMock(return_value=older_purchase_order)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.get(order_number=45143984)
        self.assertEqual(purchase_order.date, datetime.datetime(2016, 3, 25, 6, 46, 40).date())

        purchase_order_item = PurchaseOrderItem.objects.get(purchase_order=purchase_order, item_number=10)
        self.assertEqual(purchase_order_item.quantity, 99)

    def _prepare_sales_orders_and_items(self):
        self.expected_programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.expected_programme_1.save()
        self.expected_programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.expected_programme_2.save()
        self.sales_order_1 = SalesOrder(programme=self.expected_programme_1,
                                        order_number=20173918,
                                        date=datetime.date(2015, 12, 3))
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(programme=self.expected_programme_2,
                                        order_number=20174363,
                                        date=datetime.date(2015, 12, 14))
        self.sales_order_2.save()

        self.item_1 = Item(description='Scale,electronic,mother/child,150kgx100g',
                           material_code='S0141021')
        self.item_2 = Item(description='Laundry soap, Carton, 25 bars, 800 grams',
                           material_code='SL009100')

        self.sales_item_1 = SalesOrderItem(sales_order=self.sales_order_1,
                                           item=self.item_1,
                                           net_price=0,
                                           net_value=Decimal('51322.6500'),
                                           issue_date=datetime.date(2015, 12, 3),
                                           delivery_date=datetime.date(2015, 12, 3),
                                           description='Scale,electronic,mother/child,150kgx100g')
        self.sales_item_2 = SalesOrderItem(sales_order=self.sales_order_2,
                                           item=self.item_2,
                                           net_price=0,
                                           net_value=Decimal('2673'),
                                           issue_date=datetime.date(2015, 12, 14),
                                           delivery_date=datetime.date(2015, 12, 14),
                                           description='Laundry soap, Carton, 25 bars, 800 grams')

    def _sync_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()

    def _assert_purchase_order_equal(self, actual_purchase_order, expected_purchase_order):
        self.assertEqual(actual_purchase_order.order_number, expected_purchase_order.order_number)
        self.assertEqual(actual_purchase_order.date, expected_purchase_order.date)
        self.assertEqual(actual_purchase_order.po_type, expected_purchase_order.po_type)

    def _assert_purchase_order_item_equal(self, actual_purchase_order_item, expected_purchase_order_item):
        self._assert_purchase_order_equal(actual_purchase_order_item.purchase_order,
                                          expected_purchase_order_item.purchase_order)
        self.assertEqual(actual_purchase_order_item.item_number, expected_purchase_order_item.item_number)
        self.assertEqual(actual_purchase_order_item.quantity, expected_purchase_order_item.quantity)
        self.assertEqual(actual_purchase_order_item.value, expected_purchase_order_item.value)
예제 #9
0
    def test_should_have_all_expected_fields(self):
        fields_in_item = Programme()._meta._name_map

        for field in ['name', 'focal_person_id']:
            self.assertIn(field, fields_in_item)
예제 #10
0
 def test_should_know_the_string_representation_for_a_programme_returns_the_name(
         self):
     dummy_name = "Test Name"
     programme = Programme(name=dummy_name)
     self.assertEqual(dummy_name, str(programme))
예제 #11
0
class TestSyncPurchaseOrder(TestCase):
    def setUp(self):
        self.downloaded_purchase_orders = [{"PURCHASING_ORG_CODE": "1000",
                                            "PURCHASING_GROUP_CODE": u"100",
                                            "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                                            "PLANT_CODE": "1000",
                                            "VENDOR_CODE": "1900000501",
                                            "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                                            "VENDOR_CTRY_NAME": "Indonesia",
                                            "GRANT_REF": "XP154478",
                                            "EXPIRY_DATE": u"/Date(1483160400000)/",
                                            "DONOR_NAME": "Ministry of National Health",
                                            "PREQ_NO": "0030344125",
                                            "PREQ_ITEM": 80,
                                            "PREQ_QTY": 51322.65,
                                            "SO_NUMBER": u"0020173918",
                                            "PO_NUMBER": u"0045143984",
                                            "PO_ITEM": 10,
                                            "PO_TYPE": "NB",
                                            "PO_DATE": u"/Date(1448859600000)/",
                                            "CREATE_DATE": u"/Date(1448859600000)/",
                                            "UPDATE_DATE": u"/Date(1448859600000)/",
                                            "PO_ITEM_QTY": 100,
                                            "CURRENCY_CODE": "USD",
                                            "AMOUNT_CURR": 51322.65,
                                            "AMOUNT_USD": 51322.65,
                                            "MATERIAL_CODE": "S0141021",
                                            "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g"},

                                           {"PURCHASING_ORG_CODE": "1000",
                                            "PURCHASING_GROUP_CODE": u"100",
                                            "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                                            "PLANT_CODE": "1000",
                                            "VENDOR_CODE": "1900000501",
                                            "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                                            "VENDOR_CTRY_NAME": "Indonesia",
                                            "GRANT_REF": "XP154478",
                                            "EXPIRY_DATE": u"/Date(1450069200000)/",
                                            "DONOR_NAME": "Ministry of National Health",
                                            "PREQ_NO": "0030344125",
                                            "PREQ_ITEM": 10,
                                            "PREQ_QTY": 2673,
                                            "SO_NUMBER": u"0020174363",
                                            "PO_NUMBER": u"0045144863",
                                            "PO_ITEM": 20,
                                            "PO_TYPE": "ZLC",
                                            "PO_DATE": u"/Date(1450069200000)/",
                                            "CREATE_DATE": u"/Date(1450069200000)/",
                                            "UPDATE_DATE": u"/Date(1450069200000)/",
                                            "PO_ITEM_QTY": 80,
                                            "CURRENCY_CODE": "USD",
                                            "AMOUNT_CURR": 2673,
                                            "AMOUNT_USD": 2673,
                                            "MATERIAL_CODE": "SL009100",
                                            "MATERIAL_DESC": "Laundry soap, Carton, 25 bars, 800 grams"}]

        self.converted_purchase_orders = [
            {'AMOUNT_CURR': 51322.65, 'PURCHASING_GROUP_CODE': 100, 'PURCHASING_ORG_CODE': '1000',
             'PREQ_NO': '0030344125',
             'PLANT_CODE': '1000', 'MATERIAL_DESC': 'Scale,electronic,mother/child,150kgx100g',
             'EXPIRY_DATE': datetime.datetime(2016, 12, 31, 8, 0), 'PO_ITEM_QTY': 100, 'PREQ_ITEM': 80,
             'PURCHASING_GROUP_NAME': 'IMMUNIZATION', 'PO_ITEM': 10, 'PO_DATE': datetime.datetime(2015, 11, 30, 8, 0),
             'CREATE_DATE': datetime.datetime(2015, 11, 30, 8, 0), 'UPDATE_DATE': datetime.datetime(2015, 11, 30, 8, 0),
             'VENDOR_CTRY_NAME': 'Indonesia', 'DONOR_NAME': 'Ministry of National Health', 'PREQ_QTY': 51322.65,
             'CURRENCY_CODE': 'USD', 'SO_NUMBER': 20173918, 'VENDOR_CODE': '1900000501', 'AMOUNT_USD': 51322.65,
             'GRANT_REF': 'XP154478', 'PO_NUMBER': 45143984, 'MATERIAL_CODE': 'S0141021', 'PO_TYPE': 'NB',
             'VENDOR_NAME': 'P.T. BIO FARMA (PERSERO)'},
            {'AMOUNT_CURR': 2673, 'PURCHASING_GROUP_CODE': 100, 'PURCHASING_ORG_CODE': '1000',
             'PREQ_NO': '0030344125',
             'PLANT_CODE': '1000', 'MATERIAL_DESC': 'Laundry soap, Carton, 25 bars, 800 grams',
             'EXPIRY_DATE': datetime.datetime(2015, 12, 14, 8, 0), 'PO_ITEM_QTY': 80, 'PREQ_ITEM': 10,
             'PURCHASING_GROUP_NAME': 'IMMUNIZATION', 'PO_ITEM': 20, 'PO_DATE': datetime.datetime(2015, 12, 14, 8, 0),
             'CREATE_DATE': datetime.datetime(2015, 12, 14, 8, 0), 'UPDATE_DATE': datetime.datetime(2015, 12, 14, 8, 0),
             'VENDOR_CTRY_NAME': 'Indonesia', 'DONOR_NAME': 'Ministry of National Health', 'PREQ_QTY': 2673,
             'CURRENCY_CODE': 'USD', 'SO_NUMBER': 20174363, 'VENDOR_CODE': '1900000501', 'AMOUNT_USD': 2673,
             'GRANT_REF': 'XP154478', 'PO_NUMBER': 45144863, 'MATERIAL_CODE': 'SL009100', 'PO_TYPE': 'ZLC',
             'VENDOR_NAME': 'P.T. BIO FARMA (PERSERO)'}]

        self.purchase_order_which_do_not_have_reference_to_sales_order = \
            [{'UPDATE_DATE': datetime.datetime(2015, 11, 30, 8, 0),
              'PURCHASING_GROUP_CODE': 100,
              # This so number don't exists in Eums when import this po
              'SO_NUMBER': 20170001,
              'PO_NUMBER': 45143984,
              'PO_TYPE': 'NB',
              'PO_ITEM': 10,
              'MATERIAL_DESC': 'Scale,electronic,mother/child,150kgx100g',
              'AMOUNT_USD': 51322.65,
              'MATERIAL_CODE': 'S0141021',
              'PREQ_ITEM': 80,
              'PO_ITEM_QTY': 100}]
        self.purchase_order_item_which_do_not_have_reference_to_sales_order_item = \
            [{'UPDATE_DATE': datetime.datetime(2015, 11, 30, 8, 0),
              'PURCHASING_GROUP_CODE': 100,
              'SO_NUMBER': 20173918,
              'PO_NUMBER': 45143984,
              'PO_TYPE': 'NB',
              'PO_ITEM': 10,
              'MATERIAL_DESC': 'Scale,electronic,mother/child,150kgx100g',
              'AMOUNT_USD': 51322.65,
              'MATERIAL_CODE': 'S0141021',
              # so item number in Eums is 80, but this one is 70, so this item can't refer to so item
              'PREQ_ITEM': 70,
              'PO_ITEM_QTY': 100}]
        self.purchase_order_item_with_invalid_po_type = \
            [{'UPDATE_DATE': datetime.datetime(2015, 11, 30, 8, 0),
              'PURCHASING_GROUP_CODE': 100,
              'SO_NUMBER': 20173918,
              'PO_NUMBER': 45143984,
              # For now only NB, ZLC, ZUB, ZOC are supported by Eums
              'PO_TYPE': 'ZAM',
              'PO_ITEM': 10,
              'MATERIAL_DESC': 'Scale,electronic,mother/child,150kgx100g',
              'AMOUNT_USD': 51322.65,
              'MATERIAL_CODE': 'S0141021',
              'PREQ_ITEM': 80,
              'PO_ITEM_QTY': 100}]

        self._prepare_sales_orders_and_items()
        self.expected_purchase_order_1 = PurchaseOrder(order_number=45143984,
                                                       sales_order=self.sales_order_1,
                                                       date=datetime.date(2015, 11, 30),
                                                       po_type='NB')
        self.expected_purchase_order_2 = PurchaseOrder(order_number=45144863,
                                                       sales_order=self.sales_order_2,
                                                       date=datetime.date(2015, 12, 14),
                                                       po_type='ZLC')
        self.expected_purchase_order_item_1 = PurchaseOrderItem(purchase_order=self.expected_purchase_order_1,
                                                                item=self.item_1,
                                                                item_number=10,
                                                                sales_order_item=self.sales_item_1,
                                                                quantity=100.00,
                                                                value=Decimal('51322.65'))
        self.expected_purchase_order_item_2 = PurchaseOrderItem(purchase_order=self.expected_purchase_order_2,
                                                                item=self.item_2,
                                                                item_number=20,
                                                                sales_order_item=self.sales_item_2,
                                                                quantity=80.00,
                                                                value=Decimal('2673'))
        start_date = '01122015'
        end_date = datetime.date.today().strftime('%d%m%Y')
        self.synchronizer = PurchaseOrderSynchronizer(start_date=start_date)
        base_url = 'https://devapis.unicef.org/BIService/BIWebService.svc/GetPurchaseOrderInfo_JSON/'
        self.expected_url = base_url + start_date + '/' + end_date

    def tearDown(self):
        SalesOrder.objects.all().delete()
        SalesOrderItem.objects.all().delete()
        Item.objects.all().delete()
        OrderItem.objects.all().delete()
        Programme.objects.all().delete()
        PurchaseOrder.objects.all().delete()
        PurchaseOrderItem.objects.all().delete()
        DistributionPlan.objects.all().delete()
        DistributionPlanNode.objects.all().delete()

    def test_should_point_to_correct_endpoint(self):
        self.synchronizer._load_records = MagicMock(return_value=[])
        self.synchronizer.sync()

        self.assertEqual(self.synchronizer.url, self.expected_url)

    def test_should_load_purchase_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._load_records.assert_called()
        self.synchronizer._save_records.assert_called()

    def test_should_convert_purchase_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._save_records.assert_called_with(self.converted_purchase_orders)

    def test_should_save_purchase_orders(self):
        self._sync_purchase_order()
        all_purchase_orders = PurchaseOrder.objects.all()
        actual_purchase_order_1 = all_purchase_orders[0]
        actual_purchase_order_2 = all_purchase_orders[1]

        self._assert_purchase_order_equal(actual_purchase_order_1, self.expected_purchase_order_1)
        self._assert_purchase_order_equal(actual_purchase_order_2, self.expected_purchase_order_2)

    def test_should_save_purchase_order_items(self):
        self._sync_purchase_order()

        expected_order_number_one = self.expected_purchase_order_1.order_number
        expected_order_number_two = self.expected_purchase_order_2.order_number

        actual_purchase_item_1 = PurchaseOrderItem.objects.get(purchase_order__order_number=expected_order_number_one)
        actual_purchase_item_2 = PurchaseOrderItem.objects.get(purchase_order__order_number=expected_order_number_two)

        self._assert_purchase_order_item_equal(actual_purchase_item_1, self.expected_purchase_order_item_1)
        self._assert_purchase_order_item_equal(actual_purchase_item_2, self.expected_purchase_order_item_2)

    def test_should_NOT_import_purchase_order_which_can_not_refer_to_existing_sales_order(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = \
            MagicMock(return_value=self.purchase_order_which_do_not_have_reference_to_sales_order)
        self.synchronizer.sync()

        self.assertEqual(PurchaseOrder.objects.count(), 0)
        self.assertEqual(PurchaseOrderItem.objects.count(), 0)

    def test_should_import_purchase_order_item_which_can_not_refer_to_existing_sales_order_item(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = \
            MagicMock(return_value=self.purchase_order_item_which_do_not_have_reference_to_sales_order_item)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.all()[0]
        purchase_order_item = PurchaseOrderItem.objects.all()[0]

        self._assert_purchase_order_equal(purchase_order, self.expected_purchase_order_1)
        self.assertEqual(purchase_order_item.sales_order_item, None)

    def test_should_NOT_import_purchase_order_with_invalid_po_type(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = MagicMock(return_value=self.purchase_order_item_with_invalid_po_type)
        self.synchronizer.sync()

        self.assertEqual(PurchaseOrder.objects.count(), 0)
        self.assertEqual(PurchaseOrderItem.objects.count(), 0)

    def test_should_NOT_update_when_got_an_older_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()
        older_purchase_order = [{"PURCHASING_ORG_CODE": "1000",
                                 "PURCHASING_GROUP_CODE": "100",
                                 "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                                 "PLANT_CODE": "1000",
                                 "VENDOR_CODE": "1900000501",
                                 "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                                 "VENDOR_CTRY_NAME": "Indonesia",
                                 "GRANT_REF": "XP154478",
                                 "EXPIRY_DATE": u"/Date(1483160400000)/",
                                 "DONOR_NAME": "Ministry of National Health",
                                 "PREQ_NO": "0030344125",
                                 "PREQ_ITEM": 80,
                                 "PREQ_QTY": 51322.65,
                                 "SO_NUMBER": u"0020173918",
                                 "PO_NUMBER": u"0045143984",
                                 "PO_ITEM": 10,
                                 "PO_TYPE": "NB",
                                 "PO_DATE": u"/Date(1448859600000)/",
                                 "CREATE_DATE": u"/Date(1440859600000)/",
                                 "UPDATE_DATE": u"/Date(1440859600000)/",
                                 "PO_ITEM_QTY": 99,
                                 "CURRENCY_CODE": "USD",
                                 "AMOUNT_CURR": 51322.65,
                                 "AMOUNT_USD": 51322.65,
                                 "MATERIAL_CODE": "S0141021",
                                 "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g"}]

        self.synchronizer._load_records = MagicMock(return_value=older_purchase_order)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.get(order_number=45143984)
        self.assertEqual(purchase_order.date, datetime.datetime(2015, 11, 30, 13, 0).date())

        purchase_order_item = PurchaseOrderItem.objects.get(purchase_order=purchase_order, item_number=10)
        self.assertEqual(purchase_order_item.quantity, 100)

    def test_should_update_when_got_a_newer_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()
        older_purchase_order = [{"PURCHASING_ORG_CODE": "1000",
                                 "PURCHASING_GROUP_CODE": u"100",
                                 "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                                 "PLANT_CODE": "1000",
                                 "VENDOR_CODE": "1900000501",
                                 "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                                 "VENDOR_CTRY_NAME": "Indonesia",
                                 "GRANT_REF": "XP154478",
                                 "EXPIRY_DATE": u"/Date(1483160400000)/",
                                 "DONOR_NAME": "Ministry of National Health",
                                 "PREQ_NO": "0030344125",
                                 "PREQ_ITEM": 80,
                                 "PREQ_QTY": 51322.65,
                                 "SO_NUMBER": u"0020173918",
                                 "PO_NUMBER": u"0045143984",
                                 "PO_ITEM": 10,
                                 "PO_TYPE": "NB",
                                 "PO_DATE": u"/Date(1448859600000)/",
                                 "CREATE_DATE": u"/Date(1458859600000)/",
                                 "UPDATE_DATE": u"/Date(1458859600000)/",
                                 "PO_ITEM_QTY": 99,
                                 "CURRENCY_CODE": "USD",
                                 "AMOUNT_CURR": 51322.65,
                                 "AMOUNT_USD": 51322.65,
                                 "MATERIAL_CODE": "S0141021",
                                 "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g"}]

        self.synchronizer._load_records = MagicMock(return_value=older_purchase_order)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.get(order_number=45143984)
        self.assertEqual(purchase_order.date, datetime.datetime(2016, 3, 25, 6, 46, 40).date())

        purchase_order_item = PurchaseOrderItem.objects.get(purchase_order=purchase_order, item_number=10)
        self.assertEqual(purchase_order_item.quantity, 99)

    def _prepare_sales_orders_and_items(self):
        self.expected_programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.expected_programme_1.save()
        self.expected_programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.expected_programme_2.save()
        self.sales_order_1 = SalesOrder(programme=self.expected_programme_1,
                                        order_number=20173918,
                                        date=datetime.date(2015, 12, 3))
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(programme=self.expected_programme_2,
                                        order_number=20174363,
                                        date=datetime.date(2015, 12, 14))
        self.sales_order_2.save()

        self.item_1 = Item(description='Scale,electronic,mother/child,150kgx100g',
                           material_code='S0141021')
        self.item_2 = Item(description='Laundry soap, Carton, 25 bars, 800 grams',
                           material_code='SL009100')

        self.sales_item_1 = SalesOrderItem(sales_order=self.sales_order_1,
                                           item=self.item_1,
                                           net_price=0,
                                           net_value=Decimal('51322.6500'),
                                           issue_date=datetime.date(2015, 12, 3),
                                           delivery_date=datetime.date(2015, 12, 3),
                                           description='Scale,electronic,mother/child,150kgx100g')
        self.sales_item_2 = SalesOrderItem(sales_order=self.sales_order_2,
                                           item=self.item_2,
                                           net_price=0,
                                           net_value=Decimal('2673'),
                                           issue_date=datetime.date(2015, 12, 14),
                                           delivery_date=datetime.date(2015, 12, 14),
                                           description='Laundry soap, Carton, 25 bars, 800 grams')

    def _sync_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()

    def _assert_purchase_order_equal(self, actual_purchase_order, expected_purchase_order):
        self.assertEqual(actual_purchase_order.order_number, expected_purchase_order.order_number)
        self.assertEqual(actual_purchase_order.date, expected_purchase_order.date)
        self.assertEqual(actual_purchase_order.po_type, expected_purchase_order.po_type)

    def _assert_purchase_order_item_equal(self, actual_purchase_order_item, expected_purchase_order_item):
        self._assert_purchase_order_equal(actual_purchase_order_item.purchase_order,
                                          expected_purchase_order_item.purchase_order)
        self.assertEqual(actual_purchase_order_item.item_number, expected_purchase_order_item.item_number)
        self.assertEqual(actual_purchase_order_item.quantity, expected_purchase_order_item.quantity)
        self.assertEqual(actual_purchase_order_item.value, expected_purchase_order_item.value)
예제 #12
0
class TestSyncPurchaseOrder(TestCase):
    def setUp(self):
        self.downloaded_purchase_orders = downloaded_purchase_orders
        self.converted_purchase_orders = converted_purchase_orders

        self.purchase_order_which_do_not_have_reference_to_sales_order = (
            purchase_order_which_do_not_have_reference_to_sales_order
        )
        self.purchase_order_item_which_do_not_have_reference_to_sales_order_item = (
            purchase_order_item_which_do_not_have_reference_to_sales_order_item
        )
        self.purchase_order_item_with_invalid_po_type = purchase_order_item_with_invalid_po_type

        self._prepare_sales_orders_and_items()
        self.expected_purchase_order = PurchaseOrder(
            order_number=45143984, sales_order=self.sales_order_1, date=datetime.date(2015, 11, 30), po_type="NB"
        )
        self.expected_purchase_order_item = PurchaseOrderItem(
            purchase_order=self.expected_purchase_order,
            item=self.item_1,
            item_number=10,
            sales_order_item=self.sales_item_1,
            quantity=100.00,
            value=Decimal("51322.65"),
        )
        start_date = "01122015"
        end_date = datetime.date.today().strftime("%d%m%Y")
        self.synchronizer = PurchaseOrderSynchronizer(start_date=start_date)
        base_url = "https://devapis.unicef.org/BIService/BIWebService.svc/GetPurchaseOrderInfo_JSON/"
        self.expected_url = base_url + start_date + "/" + end_date

    def tearDown(self):
        SalesOrder.objects.all().delete()
        SalesOrderItem.objects.all().delete()
        Item.objects.all().delete()
        OrderItem.objects.all().delete()
        Programme.objects.all().delete()
        PurchaseOrder.objects.all().delete()
        PurchaseOrderItem.objects.all().delete()
        DistributionPlan.objects.all().delete()
        DistributionPlanNode.objects.all().delete()

    def test_should_point_to_correct_endpoint(self):
        self.synchronizer._load_records = MagicMock(return_value=[])
        self.synchronizer.sync()

        self.assertEqual(self.synchronizer.url, self.expected_url)

    def test_should_load_purchase_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._load_records.assert_called()
        self.synchronizer._save_records.assert_called()

    def test_should_convert_purchase_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._save_records.assert_called_with(self.converted_purchase_orders)

    def test_should_save_purchase_orders(self):
        self._sync_purchase_order()
        purchase_order = PurchaseOrder.objects.all().first()

        self._assert_purchase_order_equal(purchase_order, self.expected_purchase_order)

    def test_should_save_purchase_order_items(self):
        self._sync_purchase_order()

        expected_order_number_one = self.expected_purchase_order.order_number

        actual_purchase_item = PurchaseOrderItem.objects.get(purchase_order__order_number=expected_order_number_one)

        self._assert_purchase_order_item_equal(actual_purchase_item, self.expected_purchase_order_item)

    def test_should_NOT_import_purchase_order_which_can_not_refer_to_existing_sales_order(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = MagicMock(
            return_value=self.purchase_order_which_do_not_have_reference_to_sales_order
        )
        self.synchronizer.sync()

        self.assertEqual(PurchaseOrder.objects.count(), 0)
        self.assertEqual(PurchaseOrderItem.objects.count(), 0)

    def test_should_import_purchase_order_item_which_can_not_refer_to_existing_sales_order_item(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = MagicMock(
            return_value=self.purchase_order_item_which_do_not_have_reference_to_sales_order_item
        )
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.all()[0]
        purchase_order_item = PurchaseOrderItem.objects.all()[0]

        self._assert_purchase_order_equal(purchase_order, self.expected_purchase_order)
        self.assertEqual(purchase_order_item.sales_order_item, None)

    def test_should_NOT_import_purchase_order_with_invalid_po_type(self):
        self.synchronizer._load_records = MagicMock()
        self.synchronizer._convert_records = MagicMock(return_value=self.purchase_order_item_with_invalid_po_type)
        self.synchronizer.sync()

        self.assertEqual(PurchaseOrder.objects.count(), 0)
        self.assertEqual(PurchaseOrderItem.objects.count(), 0)

    def test_should_NOT_update_when_got_an_older_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()
        older_purchase_order = [
            {
                "PURCHASING_ORG_CODE": "1000",
                "PURCHASING_GROUP_CODE": "100",
                "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                "PLANT_CODE": "1000",
                "VENDOR_CODE": "1900000501",
                "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                "VENDOR_CTRY_NAME": "Indonesia",
                "GRANT_REF": "XP154478",
                "EXPIRY_DATE": u"/Date(1483160400000)/",
                "DONOR_NAME": "Ministry of National Health",
                "PREQ_NO": "0030344125",
                "PREQ_ITEM": 80,
                "PREQ_QTY": 51322.65,
                "SO_NUMBER": u"0020173918",
                "PO_NUMBER": u"0045143984",
                "PO_ITEM": 10,
                "PO_TYPE": "NB",
                "PO_DATE": u"/Date(1448859600000)/",
                "CREATE_DATE": u"/Date(1440859600000)/",
                "UPDATE_DATE": u"/Date(1440859600000)/",
                "PO_ITEM_QTY": 99,
                "CURRENCY_CODE": "USD",
                "AMOUNT_CURR": 51322.65,
                "AMOUNT_USD": 51322.65,
                "MATERIAL_CODE": "S0141021",
                "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g",
            }
        ]

        self.synchronizer._load_records = MagicMock(return_value=older_purchase_order)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.get(order_number=45143984)
        self.assertEqual(purchase_order.date, datetime.datetime(2015, 11, 30, 13, 0).date())

        purchase_order_item = PurchaseOrderItem.objects.get(purchase_order=purchase_order, item_number=10)
        self.assertEqual(purchase_order_item.quantity, 100)

    def test_should_update_when_got_a_newer_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()
        older_purchase_order = [
            {
                "PURCHASING_ORG_CODE": "1000",
                "PURCHASING_GROUP_CODE": u"100",
                "PURCHASING_GROUP_NAME": "IMMUNIZATION",
                "PLANT_CODE": "1000",
                "VENDOR_CODE": "1900000501",
                "VENDOR_NAME": "P.T. BIO FARMA (PERSERO)",
                "VENDOR_CTRY_NAME": "Indonesia",
                "GRANT_REF": "XP154478",
                "EXPIRY_DATE": u"/Date(1483160400000)/",
                "DONOR_NAME": "Ministry of National Health",
                "PREQ_NO": "0030344125",
                "PREQ_ITEM": 80,
                "PREQ_QTY": 51322.65,
                "SO_NUMBER": u"0020173918",
                "PO_NUMBER": u"0045143984",
                "PO_ITEM": 10,
                "PO_TYPE": "NB",
                "PO_DATE": u"/Date(1448859600000)/",
                "CREATE_DATE": u"/Date(1458859600000)/",
                "UPDATE_DATE": u"/Date(1458859600000)/",
                "PO_ITEM_QTY": 99,
                "CURRENCY_CODE": "USD",
                "AMOUNT_CURR": 51322.65,
                "AMOUNT_USD": 51322.65,
                "MATERIAL_CODE": "S0141021",
                "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g",
            }
        ]

        self.synchronizer._load_records = MagicMock(return_value=older_purchase_order)
        self.synchronizer.sync()

        purchase_order = PurchaseOrder.objects.get(order_number=45143984)
        self.assertEqual(purchase_order.date, datetime.datetime(2016, 3, 25, 6, 46, 40).date())

        purchase_order_item = PurchaseOrderItem.objects.get(purchase_order=purchase_order, item_number=10)
        self.assertEqual(purchase_order_item.quantity, 99)

    def _prepare_sales_orders_and_items(self):
        self.expected_programme_1 = Programme(wbs_element_ex="0060/A0/07/883")
        self.expected_programme_1.save()
        self.expected_programme_2 = Programme(wbs_element_ex="4380/A0/04/105")
        self.expected_programme_2.save()
        self.sales_order_1 = SalesOrder(
            programme=self.expected_programme_1, order_number=20173918, date=datetime.date(2015, 12, 3)
        )
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(
            programme=self.expected_programme_2, order_number=20174363, date=datetime.date(2015, 12, 14)
        )
        self.sales_order_2.save()

        self.item_1 = Item(description="Scale,electronic,mother/child,150kgx100g", material_code="S0141021")
        self.item_2 = Item(description="Laundry soap, Carton, 25 bars, 800 grams", material_code="SL009100")

        self.sales_item_1 = SalesOrderItem(
            sales_order=self.sales_order_1,
            item=self.item_1,
            net_price=0,
            net_value=Decimal("51322.6500"),
            issue_date=datetime.date(2015, 12, 3),
            delivery_date=datetime.date(2015, 12, 3),
            description="Scale,electronic,mother/child,150kgx100g",
        )
        self.sales_item_2 = SalesOrderItem(
            sales_order=self.sales_order_2,
            item=self.item_2,
            net_price=0,
            net_value=Decimal("2673"),
            issue_date=datetime.date(2015, 12, 14),
            delivery_date=datetime.date(2015, 12, 14),
            description="Laundry soap, Carton, 25 bars, 800 grams",
        )

    def _sync_purchase_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_purchase_orders)
        self.synchronizer.sync()

    def _assert_purchase_order_equal(self, actual_purchase_order, expected_purchase_order):
        self.assertEqual(actual_purchase_order.order_number, expected_purchase_order.order_number)
        self.assertEqual(actual_purchase_order.date, expected_purchase_order.date)
        self.assertEqual(actual_purchase_order.po_type, expected_purchase_order.po_type)

    def _assert_purchase_order_item_equal(self, actual_purchase_order_item, expected_purchase_order_item):
        self._assert_purchase_order_equal(
            actual_purchase_order_item.purchase_order, expected_purchase_order_item.purchase_order
        )
        self.assertEqual(actual_purchase_order_item.item_number, expected_purchase_order_item.item_number)
        self.assertEqual(actual_purchase_order_item.quantity, expected_purchase_order_item.quantity)
        self.assertEqual(actual_purchase_order_item.value, expected_purchase_order_item.value)
예제 #13
0
class TestSyncReleaseOrder(TestCase):
    def setUp(self):
        self.downloaded_release_orders = [{"RELEASE_ORDER_NUMBER": u"0054155912",
                                           "RELEASE_ORDER_ITEM": 10,
                                           "RELEASE_ORDER_TYPE": "ZLO",
                                           "SALES_UNIT": "EA",
                                           "PLANT": "5631",
                                           "SHIP_TO_PARTY": "L626010384",
                                           "WAREHOUSE_NUMBER": "492",
                                           "CONSIGNEE": "L626010384",
                                           "DOCUMENT_DATE": u"/Date(1447390800000)/",
                                           "GOODS_ISSUE_DATE": u"/Date(1448254800000)/",
                                           "MATERIAL_NUMBER": "S0141021",
                                           "DELIVERY_QUANTITY": 55,
                                           "VALUE": 15030.86,
                                           "MOVING_AVG_PRICE": 273.29,
                                           "SO_NUMBER": u"0020173918",
                                           "PO_NUMBER": u"0045143984",
                                           "PO_ITEM": 10,
                                           "WBS_ELEMENT": "6260\/A0\/05\/401\/005\/001",
                                           "WAYBILL_NUMBER": u"0072124798",
                                           "FORWARDER_NO": None,
                                           "SHIPMENT_END_DATE": u"/Date(1448254800000)/",
                                           "ACTUAL_SHIPMENT_START_DATE": None,
                                           "SHIPMENT_COMPLETION_DATE": None,
                                           "RELEASE_ORDER_CREATE_DATE": u"/Date(1447390800000)/",
                                           "RELEASE_ORDER_UPDATE_DATE": u"/Date(1447390800000)/"}]
        self.converted_release_orders = [{"RELEASE_ORDER_NUMBER": 54155912,
                                          "RELEASE_ORDER_ITEM": 10,
                                          "RELEASE_ORDER_TYPE": "ZLO",
                                          "SALES_UNIT": "EA",
                                          "PLANT": "5631",
                                          "SHIP_TO_PARTY": "L626010384",
                                          "WAREHOUSE_NUMBER": "492",
                                          "CONSIGNEE": "L626010384",
                                          "DOCUMENT_DATE": datetime.datetime(2015, 11, 13, 8, 0),
                                          "GOODS_ISSUE_DATE": datetime.datetime(2015, 11, 23, 8, 0),
                                          "MATERIAL_NUMBER": "S0141021",
                                          "DELIVERY_QUANTITY": 55,
                                          "VALUE": 15030.86,
                                          "MOVING_AVG_PRICE": 273.29,
                                          "SO_NUMBER": 20173918,
                                          "PO_NUMBER": 45143984,
                                          "PO_ITEM": 10,
                                          "WBS_ELEMENT": "6260\/A0\/05\/401\/005\/001",
                                          "WAYBILL_NUMBER": 72124798,
                                          "FORWARDER_NO": None,
                                          "SHIPMENT_END_DATE": datetime.datetime(2015, 11, 23, 8, 0),
                                          "ACTUAL_SHIPMENT_START_DATE": None,
                                          "SHIPMENT_COMPLETION_DATE": None,
                                          "RELEASE_ORDER_CREATE_DATE": datetime.datetime(2015, 11, 13, 8, 0),
                                          "RELEASE_ORDER_UPDATE_DATE": datetime.datetime(2015, 11, 13, 8, 0)}]
        self._prepare_sales_and_purchase_order()
        consignee_1 = Consignee(customer_id='L626010384')
        self.expected_release_order_1 = ReleaseOrder(order_number=54155912,
                                                     waybill=72124798,
                                                     delivery_date=datetime.date(2015, 11, 23),
                                                     consignee=consignee_1,
                                                     sales_order=self.sales_order_1,
                                                     purchase_order=self.purchase_order_1)
        item = Item(material_code='S0141021')
        self.expected_release_order_item_1 = ReleaseOrderItem(release_order=self.expected_release_order_1,
                                                              purchase_order_item=self.purchase_order_item_1,
                                                              item=item,
                                                              item_number=10,
                                                              quantity=55,
                                                              value=Decimal('15030.86'))
        self.release_order_which_can_not_refer_to_sales_order \
            = [{'PO_ITEM': 10,
                'MATERIAL_NUMBER': 'S0141021',
                'RELEASE_ORDER_ITEM': 10,
                'VALUE': 15030.86,
                'DELIVERY_QUANTITY': 55,
                'PO_NUMBER': 45143984,
                'SHIPMENT_END_DATE': '/Date(1448254800000)/',
                'WAYBILL_NUMBER': 72124798,
                'CONSIGNEE': 'L626010384',
                'SO_NUMBER': 20170001,
                'RELEASE_ORDER_NUMBER': 54155912,
                'RELEASE_ORDER_UPDATE_DATE': datetime.datetime(2015, 11, 13, 8, 0)}]
        self.release_order_which_can_not_refer_to_purchase_order \
            = [{'PO_ITEM': 10,
                'MATERIAL_NUMBER': 'S0141021',
                'RELEASE_ORDER_ITEM': 10,
                'VALUE': 15030.86,
                'DELIVERY_QUANTITY': 55,
                'PO_NUMBER': 45140001,
                'SHIPMENT_END_DATE': '/Date(1448254800000)/',
                'WAYBILL_NUMBER': 72124798,
                'CONSIGNEE': 'L626010384',
                'SO_NUMBER': 20173918,
                'RELEASE_ORDER_NUMBER': 54155912,
                'RELEASE_ORDER_UPDATE_DATE': datetime.datetime(2015, 11, 13, 8, 0)}]

        start_date = '01122015'
        end_date = datetime.date.today().strftime('%d%m%Y')
        self.synchronizer = ReleaseOrderSynchronizer(start_date=start_date)
        base_url = 'https://devapis.unicef.org/BIService/BIWebService.svc/GetReleaseOrderInfo_JSON/'
        self.expected_url = base_url + start_date + '/' + end_date

    def tearDown(self):
        Consignee.objects.all().delete()
        SalesOrder.objects.all().delete()
        Item.objects.all().delete()
        OrderItem.objects.all().delete()
        Programme.objects.all().delete()
        PurchaseOrder.objects.all().delete()
        PurchaseOrderItem.objects.all().delete()
        ReleaseOrder.objects.all().delete()
        ReleaseOrderItem.objects.all().delete()

    def test_should_point_to_correct_endpoint(self):
        self.synchronizer._load_records = MagicMock(return_value=[])
        self.synchronizer.sync()

        self.assertEqual(self.synchronizer.url, self.expected_url)

    def test_should_load_release_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_release_orders)
        self.synchronizer._convert_records = MagicMock()
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._load_records.assert_called()
        self.synchronizer._convert_records.assert_called()
        self.synchronizer._save_records.assert_called()

    def test_should_convert_release_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_release_orders)
        self.synchronizer._save_records = MagicMock()
        self.synchronizer.sync()

        self.synchronizer._save_records.assert_called_with(self.converted_release_orders)

    def test_should_save_release_orders(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_release_orders)
        self.synchronizer.sync()

        all_release_orders = ReleaseOrder.objects.all()
        actual_release_order_1 = all_release_orders[0]

        self._assert_release_order_equal(actual_release_order_1, self.expected_release_order_1)

    def test_should_save_release_order_items(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_release_orders)
        self.synchronizer.sync()

        all_release_order_items = ReleaseOrderItem.objects.all()
        actual_release_order_item_1 = all_release_order_items[0]

        self._assert_release_order_item_equal(actual_release_order_item_1, self.expected_release_order_item_1)

    def test_should_NOT_save_release_order_which_can_not_refer_to_existing_sales_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.release_order_which_can_not_refer_to_sales_order)
        self.synchronizer.sync()

        self.assertEqual(ReleaseOrder.objects.count(), 0)

    def test_should_NOT_save_release_order_which_can_not_refer_to_exsiting_purchase_order(self):
        self.synchronizer._load_records = \
            MagicMock(return_value=self.release_order_which_can_not_refer_to_purchase_order)
        self.synchronizer.sync()

        self.assertEqual(ReleaseOrder.objects.count(), 0)

    def test_should_NOT_update_when_got_an_older_release_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_release_orders)
        self.synchronizer.sync()
        older_release_order = [{"RELEASE_ORDER_NUMBER": u"0054155912",
                                "RELEASE_ORDER_ITEM": 10,
                                "RELEASE_ORDER_TYPE": "ZLO",
                                "SALES_UNIT": "EA",
                                "PLANT": "5631",
                                "SHIP_TO_PARTY": "L626010000",
                                "WAREHOUSE_NUMBER": "492",
                                "CONSIGNEE": "L626010384",
                                "DOCUMENT_DATE": u"/Date(1447390800000)/",
                                "GOODS_ISSUE_DATE": u"/Date(1448254800000)/",
                                "MATERIAL_NUMBER": "S0141021",
                                "DELIVERY_QUANTITY": 100,
                                "VALUE": 15030.86,
                                "MOVING_AVG_PRICE": 273.29,
                                "SO_NUMBER": u"0020173918",
                                "PO_NUMBER": u"0045143984",
                                "PO_ITEM": 10,
                                "WBS_ELEMENT": "6260\/A0\/05\/401\/005\/001",
                                "WAYBILL_NUMBER": u"0072124798",
                                "FORWARDER_NO": None,
                                "SHIPMENT_END_DATE": u"/Date(1440004800000)/",
                                "ACTUAL_SHIPMENT_START_DATE": None,
                                "SHIPMENT_COMPLETION_DATE": None,
                                "RELEASE_ORDER_CREATE_DATE": u"/Date(1440390800000)/",
                                "RELEASE_ORDER_UPDATE_DATE": u"/Date(1440390800000)/"}]
        self.synchronizer._load_records = MagicMock(return_value=older_release_order)
        self.synchronizer.sync()

        release_order = ReleaseOrder.objects.get(order_number=54155912)
        self.assertEqual(Consignee.objects.get(pk=release_order.consignee.id).customer_id, 'L626010384')

        release_order_item = ReleaseOrderItem.objects.get(release_order=release_order)
        self.assertEqual(release_order_item.quantity, 55)

    def test_should_update_when_got_a_newer_release_order(self):
        self.synchronizer._load_records = MagicMock(return_value=self.downloaded_release_orders)
        self.synchronizer.sync()
        newer_release_order = [{"RELEASE_ORDER_NUMBER": u"0054155912",
                                "RELEASE_ORDER_ITEM": 10,
                                "RELEASE_ORDER_TYPE": "ZLO",
                                "SALES_UNIT": "EA",
                                "PLANT": "5631",
                                "SHIP_TO_PARTY": "L626010384",
                                "WAREHOUSE_NUMBER": "492",
                                "CONSIGNEE": "L626010000",
                                "DOCUMENT_DATE": u"/Date(1447390800000)/",
                                "GOODS_ISSUE_DATE": u"/Date(1448254800000)/",
                                "MATERIAL_NUMBER": "S0141021",
                                "DELIVERY_QUANTITY": 100,
                                "VALUE": 15030.86,
                                "MOVING_AVG_PRICE": 273.29,
                                "SO_NUMBER": u"0020173918",
                                "PO_NUMBER": u"0045143984",
                                "PO_ITEM": 10,
                                "WBS_ELEMENT": "6260\/A0\/05\/401\/005\/001",
                                "WAYBILL_NUMBER": u"0072124798",
                                "FORWARDER_NO": None,
                                "SHIPMENT_END_DATE": u"/Date(1448254800000)/",
                                "ACTUAL_SHIPMENT_START_DATE": None,
                                "SHIPMENT_COMPLETION_DATE": None,
                                "RELEASE_ORDER_CREATE_DATE": u"/Date(1447390800000)/",
                                "RELEASE_ORDER_UPDATE_DATE": u"/Date(1448390800000)/"}]
        self.synchronizer._load_records = MagicMock(return_value=newer_release_order)
        self.synchronizer.sync()

        release_order = ReleaseOrder.objects.get(order_number=54155912)
        self.assertEqual(Consignee.objects.get(pk=release_order.consignee.id).customer_id, 'L626010000')

        release_order_item = ReleaseOrderItem.objects.get(release_order=release_order)
        self.assertEqual(release_order_item.quantity, 100)

    def _prepare_sales_and_purchase_order(self):
        Consignee.objects.create(customer_id='L626010000')
        Consignee.objects.create(customer_id='L626010384')
        self.programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.programme_1.save()
        self.programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.programme_2.save()
        self.sales_order_1 = SalesOrder(programme=self.programme_1,
                                        order_number=20173918,
                                        date=datetime.date(2015, 12, 3))
        self.sales_order_1.save()
        self.sales_order_2 = SalesOrder(programme=self.programme_2,
                                        order_number=20174363,
                                        date=datetime.date(2015, 12, 14))
        self.sales_order_2.save()

        self.purchase_order_1 = PurchaseOrder(order_number=45143984,
                                              sales_order=self.sales_order_1,
                                              date=datetime.date(2015, 11, 30),
                                              po_type='NB')
        self.purchase_order_1.save()
        self.purchase_order_2 = PurchaseOrder(order_number=45144863,
                                              sales_order=self.sales_order_2,
                                              date=datetime.date(2015, 12, 14),
                                              po_type='ZLC')
        self.purchase_order_2.save()

        self.item_1 = Item(description='Scale,electronic,mother/child,150kgx100g',
                           material_code='S0141021')
        self.item_1.save()
        self.item_2 = Item(description='Laundry soap, Carton, 25 bars, 800 grams',
                           material_code='SL009100')
        self.item_2.save()
        self.purchase_order_item_1 = PurchaseOrderItem(purchase_order=self.purchase_order_1,
                                                       item=self.item_1,
                                                       item_number=10,
                                                       sales_order_item=None,
                                                       quantity=100.00,
                                                       value=Decimal('51322.65'))
        self.purchase_order_item_1.save()
        self.purchase_order_item_2 = PurchaseOrderItem(purchase_order=self.purchase_order_2,
                                                       item=self.item_2,
                                                       item_number=20,
                                                       sales_order_item=None,
                                                       quantity=80.00,
                                                       value=Decimal('2673'))
        self.purchase_order_item_2.save()

    def _assert_release_order_equal(self, actual_release_order, expected_release_order):
        self.assertEqual(actual_release_order.order_number, expected_release_order.order_number)
        self.assertEqual(actual_release_order.waybill, expected_release_order.waybill)
        self.assertEqual(actual_release_order.delivery_date, expected_release_order.delivery_date)
        self.assertEqual(actual_release_order.delivery_date, expected_release_order.delivery_date)

    def _assert_release_order_item_equal(self, actual_release_order_item, expected_release_order_item_1):
        self._assert_release_order_equal(actual_release_order_item.release_order,
                                         expected_release_order_item_1.release_order)
        self.assertEqual(actual_release_order_item.item_number, expected_release_order_item_1.item_number)
        self.assertEqual(actual_release_order_item.quantity, expected_release_order_item_1.quantity)
        self.assertEqual(actual_release_order_item.value, expected_release_order_item_1.value)
예제 #14
0
    def setUp(self):
        self.downloaded_sales_orders = [{
            "SALES_ORDER_NO": u"0020173918",
            "DOCUMENT_TYPE": "ZCOM",
            "DOCUMENT_DATE": u"/Date(1449118800000)/",
            "CREATE_DATE": u"/Date(1449118800000)/",
            "UPDATE_DATE": u"/Date(1449378000000)/",
            "BUDGET_YEAR": 2015,
            "SOLD_TO_PARTY": "Z00601",
            "SOLD_TO_PARTY_DESCRIPTION": "UNICEF-AFGHANISTAN-KABUL",
            "SHIP_TO_PARTY": "006",
            "SHIP_TO_PARTY_DESCRIPTION": "Afghanistan",
            "SO_ITEM_NO": 80,
            "REQUISITION_NO": "0030344383",
            "REQUISITION_ITEM_NO": 80,
            "PURCHASING_GROUP_NAME_SHORT": "NUTRITION",
            "WBS_REFERENCE": "0060A007883001002",
            "GRANT_REF": "Unknown",
            "GRANT_EXPIRY_DATE": None,
            "DONOR_NAME": "UNICEF-AFGHANISTAN-KABUL",
            "MATERIAL_CODE": "S0141021",
            "MATERIAL_DESC": "Scale,electronic,mother/child,150kgx100g",
            "SO_ITEM_DESC": "Scale,electronic,mother/child,150kgx100g",
            "NET_VALUE": 51322.65
        }, {
            "SALES_ORDER_NO": u"0020173918",
            "DOCUMENT_TYPE": "ZCOM",
            "DOCUMENT_DATE": u"/Date(1449118800000)/",
            "CREATE_DATE": u"/Date(1449118800000)/",
            "UPDATE_DATE": u"/Date(1449378000000)/",
            "BUDGET_YEAR": 2015,
            "SOLD_TO_PARTY": "Z00601",
            "SOLD_TO_PARTY_DESCRIPTION": "UNICEF-AFGHANISTAN-KABUL",
            "SHIP_TO_PARTY": "006",
            "SHIP_TO_PARTY_DESCRIPTION": "Afghanistan",
            "SO_ITEM_NO": 90,
            "REQUISITION_NO": "0030344383",
            "REQUISITION_ITEM_NO": 90,
            "PURCHASING_GROUP_NAME_SHORT": "NUTRITION",
            "WBS_REFERENCE": "0060A007883001002",
            "GRANT_REF": "Unknown",
            "GRANT_EXPIRY_DATE": None,
            "DONOR_NAME": "UNICEF-AFGHANISTAN-KABUL",
            "MATERIAL_CODE": "S0145620",
            "MATERIAL_DESC": "MUAC,Child 11.5 Red/PAC-50",
            "SO_ITEM_DESC": "MUAC,Child 11.5 Red/PAC-50",
            "NET_VALUE": 3655.16
        }, {
            "SALES_ORDER_NO": u"0020174363",
            "DOCUMENT_TYPE": "ZCOM",
            "DOCUMENT_DATE": u"/Date(1450069200000)/",
            "CREATE_DATE": u"/Date(1450069200000)/",
            "UPDATE_DATE": u"/Date(1450069200000)/",
            "BUDGET_YEAR": 2015,
            "SOLD_TO_PARTY": "Z43801",
            "SOLD_TO_PARTY_DESCRIPTION": "UNICEF-UGANDA-KAMPALA",
            "SHIP_TO_PARTY": "438",
            "SHIP_TO_PARTY_DESCRIPTION": "Uganda",
            "SO_ITEM_NO": 10,
            "REQUISITION_NO": "0030344855",
            "REQUISITION_ITEM_NO": 10,
            "PURCHASING_GROUP_NAME_SHORT": "Kampala, Uganda",
            "WBS_REFERENCE": "4380A004105007042",
            "GRANT_REF": "SM150377",
            "GRANT_EXPIRY_DATE": u"/Date(1451538000000)/",
            "DONOR_NAME": "UNICEF-UGANDA-KAMPALA",
            "MATERIAL_CODE": "SL009100",
            "MATERIAL_DESC": "Laundry soap, Carton, 25 bars, 800 grams",
            "SO_ITEM_DESC": "Laundry soap, Carton, 25 bars, 800 grams",
            "NET_VALUE": 2673
        }]

        self.converted_sales_orders = [{
            "SALES_ORDER_NO":
            20173918,
            "DOCUMENT_TYPE":
            "ZCOM",
            "DOCUMENT_DATE":
            datetime.datetime(2015, 12, 3, 8, 0),
            "CREATE_DATE":
            datetime.datetime(2015, 12, 3, 8, 0),
            "UPDATE_DATE":
            datetime.datetime(2015, 12, 6, 8, 0),
            "BUDGET_YEAR":
            2015,
            "SOLD_TO_PARTY":
            "Z00601",
            "SOLD_TO_PARTY_DESCRIPTION":
            "UNICEF-AFGHANISTAN-KABUL",
            "SHIP_TO_PARTY":
            "006",
            "SHIP_TO_PARTY_DESCRIPTION":
            "Afghanistan",
            "SO_ITEM_NO":
            80,
            "REQUISITION_NO":
            "0030344383",
            "REQUISITION_ITEM_NO":
            80,
            "PURCHASING_GROUP_NAME_SHORT":
            "NUTRITION",
            "WBS_REFERENCE":
            "0060/A0/07/883",
            "GRANT_REF":
            "Unknown",
            "GRANT_EXPIRY_DATE":
            None,
            "DONOR_NAME":
            "UNICEF-AFGHANISTAN-KABUL",
            "MATERIAL_CODE":
            "S0141021",
            "MATERIAL_DESC":
            "Scale,electronic,mother/child,150kgx100g",
            "SO_ITEM_DESC":
            "Scale,electronic,mother/child,150kgx100g",
            "NET_VALUE":
            51322.65
        }, {
            "SALES_ORDER_NO":
            20173918,
            "DOCUMENT_TYPE":
            "ZCOM",
            "DOCUMENT_DATE":
            datetime.datetime(2015, 12, 3, 8, 0),
            "CREATE_DATE":
            datetime.datetime(2015, 12, 3, 8, 0),
            "UPDATE_DATE":
            datetime.datetime(2015, 12, 6, 8, 0),
            "BUDGET_YEAR":
            2015,
            "SOLD_TO_PARTY":
            "Z00601",
            "SOLD_TO_PARTY_DESCRIPTION":
            "UNICEF-AFGHANISTAN-KABUL",
            "SHIP_TO_PARTY":
            "006",
            "SHIP_TO_PARTY_DESCRIPTION":
            "Afghanistan",
            "SO_ITEM_NO":
            90,
            "REQUISITION_NO":
            "0030344383",
            "REQUISITION_ITEM_NO":
            90,
            "PURCHASING_GROUP_NAME_SHORT":
            "NUTRITION",
            "WBS_REFERENCE":
            "0060/A0/07/883",
            "GRANT_REF":
            "Unknown",
            "GRANT_EXPIRY_DATE":
            None,
            "DONOR_NAME":
            "UNICEF-AFGHANISTAN-KABUL",
            "MATERIAL_CODE":
            "S0145620",
            "MATERIAL_DESC":
            "MUAC,Child 11.5 Red/PAC-50",
            "SO_ITEM_DESC":
            "MUAC,Child 11.5 Red/PAC-50",
            "NET_VALUE":
            3655.16
        }, {
            "SALES_ORDER_NO":
            20174363,
            "DOCUMENT_TYPE":
            "ZCOM",
            "DOCUMENT_DATE":
            datetime.datetime(2015, 12, 14, 8, 0),
            "CREATE_DATE":
            datetime.datetime(2015, 12, 14, 8, 0),
            "UPDATE_DATE":
            datetime.datetime(2015, 12, 14, 8, 0),
            "BUDGET_YEAR":
            2015,
            "SOLD_TO_PARTY":
            "Z43801",
            "SOLD_TO_PARTY_DESCRIPTION":
            "UNICEF-UGANDA-KAMPALA",
            "SHIP_TO_PARTY":
            "438",
            "SHIP_TO_PARTY_DESCRIPTION":
            "Uganda",
            "SO_ITEM_NO":
            10,
            "REQUISITION_NO":
            "0030344855",
            "REQUISITION_ITEM_NO":
            10,
            "PURCHASING_GROUP_NAME_SHORT":
            "Kampala, Uganda",
            "WBS_REFERENCE":
            "4380/A0/04/105",
            "GRANT_REF":
            "SM150377",
            "GRANT_EXPIRY_DATE":
            datetime.datetime(2015, 12, 31, 8, 0),
            "DONOR_NAME":
            "UNICEF-UGANDA-KAMPALA",
            "MATERIAL_CODE":
            "SL009100",
            "MATERIAL_DESC":
            "Laundry soap, Carton, 25 bars, 800 grams",
            "SO_ITEM_DESC":
            "Laundry soap, Carton, 25 bars, 800 grams",
            "NET_VALUE":
            2673
        }]

        Programme.objects.create(wbs_element_ex='0060/A0/07/883')
        Programme.objects.create(wbs_element_ex='4380/A0/04/105')

        self.expected_programme_1 = Programme(wbs_element_ex='0060/A0/07/883')
        self.expected_programme_2 = Programme(wbs_element_ex='4380/A0/04/105')
        self.expected_sales_order_1 = SalesOrder(
            programme=self.expected_programme_1,
            order_number=20173918,
            date=datetime.date(2015, 12, 6))
        self.expected_sales_order_2 = SalesOrder(
            programme=self.expected_programme_2,
            order_number=20174363,
            date=datetime.date(2015, 12, 14))
        self.expected_item_1 = Item(
            description='Scale,electronic,mother/child,150kgx100g',
            material_code='S0141021')
        self.expected_item_2 = Item(description='MUAC,Child 11.5 Red/PAC-50',
                                    material_code='S0145620')
        self.expected_item_3 = Item(
            description='Laundry soap, Carton, 25 bars, 800 grams',
            material_code='SL009100')
        self.expected_sales_order_item_1 = SalesOrderItem(
            sales_order=self.expected_sales_order_1,
            item=self.expected_item_1,
            net_price=0,
            net_value=Decimal('51322.6500'),
            issue_date=datetime.date(2015, 12, 6),
            delivery_date=datetime.date(2015, 12, 6),
            description='Scale,electronic,mother/child,150kgx100g')
        self.expected_sales_order_item_2 = SalesOrderItem(
            sales_order=self.expected_sales_order_1,
            item=self.expected_item_2,
            net_price=0,
            net_value=Decimal('3655.16'),
            issue_date=datetime.date(2015, 12, 6),
            delivery_date=datetime.date(2015, 12, 6),
            description='MUAC,Child 11.5 Red/PAC-50')
        self.expected_sales_order_item_3 = SalesOrderItem(
            sales_order=self.expected_sales_order_2,
            item=self.expected_item_3,
            net_price=0,
            net_value=Decimal('2673'),
            issue_date=datetime.date(2015, 12, 14),
            delivery_date=datetime.date(2015, 12, 14),
            description='Laundry soap, Carton, 25 bars, 800 grams')
        start_date = '01122015'
        end_date = datetime.date.today().strftime('%d%m%Y')
        self.synchronizer = SalesOrderSynchronizer(start_date=start_date)
        base_url = 'https://devapis.unicef.org/BIService/BIWebService.svc/GetSalesOrderInfo_JSON/'
        self.expected_url = base_url + start_date + '/' + end_date