def test_create_date_interval_bi_weekly(self): dates = create_date_interval(INTERVALTYPE_BIWEEK, datetime.date(2012, 1, 1), datetime.date(2012, 12, 31)) self.assertEquals(dates.count(), 27) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[26], datetime.datetime(2012, 12, 30))
def test_create_date_interval_month1(self): dates = create_date_interval(INTERVALTYPE_MONTH, datetime.date(2011, 1, 1), datetime.date(2011, 12, 31)) self.assertEqual(dates.count(), 12) for i, day in enumerate([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]): self.assertEqual(dates[i], datetime.datetime(2011, i + 1, day))
def test_create_date_interval_month1(self): dates = create_date_interval(INTERVALTYPE_MONTH, datetime.date(2011, 1, 1), datetime.date(2011, 12, 31)) self.assertEquals(dates.count(), 12) for i, day in enumerate([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]): self.assertEquals(dates[i], datetime.datetime(2011, i + 1, day))
def testCreateDateIntervalMonth5(self): dates = create_date_interval(INTERVALTYPE_MONTH, datetime.date(2011, 1, 5), datetime.date(2011, 12, 31)) self.assertEquals(dates.count(), 12) for i, day in enumerate([5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]): self.assertEquals(dates[i], datetime.datetime(2011, i + 1, day))
def test_create_repeat_yearly(self): dates = create_date_interval(INTERVALTYPE_YEAR, datetime.date(2012, 1, 1), datetime.date(2015, 12, 31)) self.assertEquals(dates.count(), 4) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[1], datetime.datetime(2013, 1, 1)) self.assertEquals(dates[2], datetime.datetime(2014, 1, 1)) self.assertEquals(dates[3], datetime.datetime(2015, 1, 1))
def test_create_repeat_quarterly(self): dates = create_date_interval(INTERVALTYPE_QUARTER, datetime.date(2012, 1, 1), datetime.date(2012, 12, 31)) self.assertEquals(dates.count(), 4) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[1], datetime.datetime(2012, 4, 1)) self.assertEquals(dates[2], datetime.datetime(2012, 7, 1)) self.assertEquals(dates[3], datetime.datetime(2012, 10, 1))
def test_create_date_interval_weekly(self): dates = create_date_interval(INTERVALTYPE_WEEK, datetime.date(2012, 1, 1), datetime.date(2012, 12, 31)) self.assertEquals(dates.count(), 53) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[52], datetime.datetime(2012, 12, 30)) for date in dates: self.assertEquals(date.weekday(), dates[0].weekday())
def testCreateDateIntervalMonth31(self): dates = create_date_interval(INTERVALTYPE_MONTH, datetime.date(2011, 1, 31), datetime.date(2011, 12, 31)) self.assertEquals(dates.count(), 12) for i, day in enumerate([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]): self.assertEquals(dates[i], datetime.datetime(2011, i + 1, day))
def test_create_date_interval_count_year(self): dates = create_date_interval(interval_type=INTERVALTYPE_YEAR, count=5, interval=1, start_date=datetime.date(2012, 1, 1)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[1], datetime.datetime(2013, 1, 1)) self.assertEquals(dates[2], datetime.datetime(2014, 1, 1)) self.assertEquals(dates[3], datetime.datetime(2015, 1, 1)) self.assertEquals(dates[4], datetime.datetime(2016, 1, 1))
def create_repeated(cls, store, payment, repeat_type, start_date, end_date, temporary_identifiers=False): """Create a set of repeated payments. Given a type of interval (*repeat_type*), a start date and an end_date, this creates a list of payments for that interval. Note, this will also update the description of the payment that's passed in. :param store: a store :param payment: the payment to repeat :param repeat_type: the kind of repetition (weekly, monthly etc) :param start_date: the date to start this repetition :param end_date: the date to end this repetition :param temporary_identifiers: If the payments should be created with temporary identifiers :returns: a list of repeated payments """ dates = create_date_interval(interval_type=repeat_type, start_date=start_date, end_date=end_date) n_dates = dates.count() if n_dates == 1: raise AssertionError description = payment.description payment.description = u'1/%d %s' % (n_dates, description) payment.due_date = dates[0] payments = [] for i, date in enumerate(dates[1:]): temporary_identifier = None if temporary_identifiers: temporary_identifier = Payment.get_temporary_identifier(store) p = Payment(open_date=payment.open_date, identifier=temporary_identifier, branch=payment.branch, station=payment.station, payment_type=payment.payment_type, status=payment.status, description=u'%d/%d %s' % (i + 2, n_dates, description), value=payment.value, base_value=payment.base_value, due_date=date, method=payment.method, group=payment.group, category=payment.category, store=store) payments.append(p) return payments
def test_create_date_interval_count_month(self): dates = create_date_interval(interval_type=INTERVALTYPE_MONTH, count=5, interval=1, start_date=datetime.date(2012, 1, 1)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[1], datetime.datetime(2012, 2, 1)) self.assertEquals(dates[2], datetime.datetime(2012, 3, 1)) self.assertEquals(dates[3], datetime.datetime(2012, 4, 1)) self.assertEquals(dates[4], datetime.datetime(2012, 5, 1)) dates = create_date_interval(interval_type=INTERVALTYPE_MONTH, count=5, interval=1, start_date=datetime.date(2012, 1, 30)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 30)) self.assertEquals(dates[1], datetime.datetime(2012, 2, 29)) # leap day self.assertEquals(dates[2], datetime.datetime(2012, 3, 30)) self.assertEquals(dates[3], datetime.datetime(2012, 4, 30)) self.assertEquals(dates[4], datetime.datetime(2012, 5, 30))
def test_create_date_interval_count_week(self): dates = create_date_interval(interval_type=INTERVALTYPE_WEEK, count=5, interval=1, start_date=datetime.date(2012, 1, 1)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[1], datetime.datetime(2012, 1, 8)) self.assertEquals(dates[2], datetime.datetime(2012, 1, 15)) self.assertEquals(dates[3], datetime.datetime(2012, 1, 22)) self.assertEquals(dates[4], datetime.datetime(2012, 1, 29)) dates = create_date_interval(interval_type=INTERVALTYPE_WEEK, count=5, interval=1, start_date=datetime.date(2012, 1, 30)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 30)) self.assertEquals(dates[1], datetime.datetime(2012, 2, 6)) self.assertEquals(dates[2], datetime.datetime(2012, 2, 13)) self.assertEquals(dates[3], datetime.datetime(2012, 2, 20)) self.assertEquals(dates[4], datetime.datetime(2012, 2, 27))
def test_create_date_interval_count_day(self): dates = create_date_interval(interval_type=INTERVALTYPE_DAY, count=5, interval=1, start_date=datetime.date(2012, 1, 1)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 1)) self.assertEquals(dates[1], datetime.datetime(2012, 1, 2)) self.assertEquals(dates[2], datetime.datetime(2012, 1, 3)) self.assertEquals(dates[3], datetime.datetime(2012, 1, 4)) self.assertEquals(dates[4], datetime.datetime(2012, 1, 5)) dates = create_date_interval(interval_type=INTERVALTYPE_DAY, count=5, interval=1, start_date=datetime.date(2012, 1, 30)) self.assertEquals(dates.count(), 5) self.assertEquals(dates[0], datetime.datetime(2012, 1, 30)) self.assertEquals(dates[1], datetime.datetime(2012, 1, 31)) self.assertEquals(dates[2], datetime.datetime(2012, 2, 1)) self.assertEquals(dates[3], datetime.datetime(2012, 2, 2)) self.assertEquals(dates[4], datetime.datetime(2012, 2, 3))
def create_repeated(cls, store, payment, repeat_type, start_date, end_date, temporary_identifiers=False): """Create a set of repeated payments. Given a type of interval (*repeat_type*), a start date and an end_date, this creates a list of payments for that interval. Note, this will also update the description of the payment that's passed in. :param store: a store :param payment: the payment to repeat :param repeat_type: the kind of repetition (weekly, monthly etc) :param start_date: the date to start this repetition :param end_date: the date to end this repetition :param temporary_identifiers: If the payments should be created with temporary identifiers :returns: a list of repeated payments """ dates = create_date_interval(interval_type=repeat_type, start_date=start_date, end_date=end_date) n_dates = dates.count() if n_dates == 1: raise AssertionError description = payment.description payment.description = u'1/%d %s' % (n_dates, description) payment.due_date = dates[0] payments = [] for i, date in enumerate(dates[1:]): temporary_identifier = None if temporary_identifiers: temporary_identifier = Payment.get_temporary_identifier(store) p = Payment(open_date=payment.open_date, identifier=temporary_identifier, branch=payment.branch, payment_type=payment.payment_type, status=payment.status, description=u'%d/%d %s' % (i + 2, n_dates, description), value=payment.value, base_value=payment.base_value, due_date=date, method=payment.method, group=payment.group, category=payment.category, store=store) payments.append(p) return payments
def add_payments(self, installments_number, start_date, interval, interval_type): values = generate_payments_values(self.total_value, installments_number) due_dates = create_date_interval(interval_type=interval_type, interval=interval, count=installments_number, start_date=start_date) bank_account = None self.clear_payments() for i in range(installments_number): description = self.method.describe_payment(self.group, i + 1, installments_number) if self._has_bank_account(): bank_account = _TemporaryBankData() self.add_payment(description, currency(values[i]), due_dates[i], None, bank_account, False) self.update_view()
def post(self, store): # FIXME: Check branch state and force fail if no override for that product is present. self.ensure_printer() data = request.get_json() client_id = data.get('client_id') products = data['products'] payments = data['payments'] client_category_id = data.get('price_table') document = raw_document(data.get('client_document', '') or '') if document: document = format_document(document) if client_id: client = store.get(Client, client_id) elif document: person = Person.get_by_document(store, document) client = person and person.client else: client = None # Create the sale branch = api.get_current_branch(store) group = PaymentGroup(store=store) user = api.get_current_user(store) sale = Sale( store=store, branch=branch, salesperson=user.person.sales_person, client=client, client_category_id=client_category_id, group=group, open_date=localnow(), coupon_id=None, ) # Add products for p in products: sellable = store.get(Sellable, p['id']) item = sale.add_sellable(sellable, price=currency(p['price']), quantity=decimal.Decimal(p['quantity'])) # XXX: bdil has requested that when there is a special discount, the discount does # not appear on the coupon. Instead, the item wil be sold using the discount price # as the base price. Maybe this should be a parameter somewhere item.base_price = item.price # Add payments sale_total = sale.get_total_sale_amount() money_payment = None payments_total = 0 for p in payments: method_name = p['method'] tef_data = p.get('tef_data', {}) if method_name == 'tef': p['provider'] = tef_data['card_name'] method_name = 'card' method = PaymentMethod.get_by_name(store, method_name) installments = p.get('installments', 1) or 1 due_dates = list(create_date_interval( INTERVALTYPE_MONTH, interval=1, start_date=localnow(), count=installments)) payment_value = currency(p['value']) payments_total += payment_value p_list = method.create_payments( Payment.TYPE_IN, group, branch, payment_value, due_dates) if method.method_name == 'money': # FIXME Frontend should not allow more than one money payment. this can be changed # once https://gitlab.com/stoqtech/private/bdil/issues/75 is fixed? if not money_payment or payment_value > money_payment.value: money_payment = p_list[0] elif method.method_name == 'card': for payment in p_list: card_data = method.operation.get_card_data_by_payment(payment) card_type = p['mode'] # Stoq does not have the voucher comcept, so register it as a debit card. if card_type == 'voucher': card_type = 'debit' device = self._get_card_device(store, 'TEF') provider = self._get_provider(store, p['provider']) if tef_data: card_data.nsu = tef_data['aut_loc_ref'] card_data.auth = tef_data['aut_ext_ref'] card_data.update_card_data(device, provider, card_type, installments) card_data.te.metadata = tef_data # If payments total exceed sale total, we must adjust money payment so that the change is # correctly calculated.. if payments_total > sale_total and money_payment: money_payment.value -= (payments_total - sale_total) assert money_payment.value >= 0 # Confirm the sale group.confirm() sale.order() till = Till.get_last(store) sale.confirm(till) # Fiscal plugins will connect to this event and "do their job" # It's their responsibility to raise an exception in case of # any error, which will then trigger the abort bellow # FIXME: Catch printing errors here and send message to the user. SaleConfirmedRemoteEvent.emit(sale, document) # This will make sure we update any stock or price changes products may # have between sales return True
def test_create_date_interval_daily(self): dates = create_date_interval(INTERVALTYPE_DAY, datetime.date(2011, 1, 1), datetime.date(2011, 12, 31)) self.assertEquals(dates.count(), 365)