Beispiel #1
0
def outstanding_bookings():
    try:
        outstanding = []
        today = datetime.date.today()
        for b in Booking.objects.filter(is_canceled=False,departure__gte=today).exclude(booking_type__in=['1','3']):
            if not b.paid:
                outstanding.append(b)


        strIO = StringIO()
        fieldnames = ['Confirmation Number','Customer','Campground','Arrival','Departure','Outstanding Amount']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)
        for o in outstanding:
            fullname = '{} {}'.format(o.details.get('first_name'),o.details.get('last_name'))
            writer.writerow([o.confirmation_number,fullname,o.campground.name,o.arrival.strftime('%d/%m/%Y'),o.departure.strftime('%d/%m/%Y'),o.outstanding])
        strIO.flush()
        strIO.seek(0)
        _file = strIO

        dt = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
        recipients = []
        recipients = OutstandingBookingRecipient.objects.all()
        email = EmailMessage(
            'Unpaid Bookings Summary as at {}'.format(dt),
            'Unpaid Bookings as at {}'.format(dt),
            settings.DEFAULT_FROM_EMAIL,
            to=[r.email for r in recipients]if recipients else [settings.NOTIFICATION_EMAIL]
        )
        email.attach('OustandingBookings_{}.csv'.format(dt), _file.getvalue(), 'text/csv')
        email.send()
    except:
        raise
Beispiel #2
0
def bookings_report(_date):
    try:
        bpoint, cash = [], []
        bookings = Booking.objects.filter(created__date=_date)
        history_bookings = BookingHistory.objects.filter(created__date=_date)

        strIO = StringIO()
        fieldnames = ['Date','Confirmation Number','Name','Amount','Invoice','Booking Type']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        types = dict(Booking.BOOKING_TYPE_CHOICES)

        for b in bookings:
            b_name = u'{} {}'.format(b.details.get('first_name',''),b.details.get('last_name',''))
            created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
            writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),b.confirmation_number,b_name.encode('utf-8'),b.active_invoice.amount if b.active_invoice else '',b.active_invoice.reference if b.active_invoice else '', types[b.booking_type] if b.booking_type in types else b.booking_type])

        #for b in history_bookings:
        #    b_name = '{} {}'.format(b.details.get('first_name',''),b.details.get('last_name',''))
        #    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.booking.confirmation_number,b_name,b.invoice.amount,b.invoice.reference,'Yes'])
            

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #3
0
def user_report():
    strIO = None
    # Create or update view
    cursor = connection.cursor()
    sql = 'CREATE OR REPLACE VIEW accounts_emailuser_report_v AS \
            select md5(CAST((first_name,last_name,dob)AS text)) as hash,count(*) as occurence, first_name,last_name,\
            dob from accounts_emailuser group by first_name,last_name,dob;'
    cursor.execute(sql)

    users = EmailUserReport.objects.filter(occurence__gt=1)
    if users:
        strIO = StringIO()
        fieldnames = ['Occurence', 'Given Name(s)','Last Name','DOB']
        writer = csv.DictWriter(strIO, fieldnames=fieldnames)
        writer.writeheader()

        for u in users:
            info = {
                'Occurence': u.occurence,
                'Given Name(s)': u.first_name,
                'Last Name': u.last_name,
                'DOB': u.dob
            }
            writer.writerow(info)
        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #4
0
def user_report():
    strIO = None
    # Create or update view
    cursor = connection.cursor()
    sql = 'CREATE OR REPLACE VIEW accounts_emailuser_report_v AS \
            select md5(CAST((first_name,last_name,dob)AS text)) as hash,count(*) as occurence, first_name,last_name,\
            dob from accounts_emailuser group by first_name,last_name,dob;'
    cursor.execute(sql)

    users = EmailUserReport.objects.filter(occurence__gt=1)
    if users:
        strIO = StringIO()
        fieldnames = ['Occurence', 'First Name','Last Name','DOB']
        writer = csv.DictWriter(strIO, fieldnames=fieldnames)
        writer.writeheader()

        for u in users:
            info = {
                'Occurence': u.occurence,
                'First Name': u.first_name,
                'Last Name': u.last_name,
                'DOB': u.dob
            }
            writer.writerow(info)
        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #5
0
def bookings_report(_date):
    try:
        bookings = Booking.objects.filter(created__date=_date)

        strIO = StringIO()
        fieldnames = [
            'Date', 'Confirmation Number', 'Name', 'Amount', 'Invoice',
            'Booking Type'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        types = dict(Booking.BOOKING_TYPE_CHOICES)

        for b in bookings:
            b_name = u'{} {}'.format(b.details.get('first_name', ''),
                                     b.details.get('last_name', ''))
            created = timezone.localtime(b.created,
                                         pytz.timezone('Australia/Perth'))
            writer.writerow([
                created.strftime('%d/%m/%Y %H:%M:%S'), b.confirmation_number,
                b_name.encode('utf-8'),
                b.active_invoice.amount if b.active_invoice else '',
                b.active_invoice.reference if b.active_invoice else '',
                types[b.booking_type]
                if b.booking_type in types else b.booking_type
            ])

        strIO.flush()
        strIO.seek(0)
        return strIO
    except BaseException:
        raise
Beispiel #6
0
def annual_admissions_booking_report(aadata):
    try:
        bookings = []
        today = datetime.date.today()
        strIO = StringIO()
        fieldnames = [
            'ID', 'First Name', 'Last Name', 'Mobile', 'Phone', 'Vessel Rego',
            'Vessel Length', 'Sticker No', 'Year', 'Status', 'Booking Period',
            'Postal Address 1', 'Postal Address 2', 'Suburb', 'Post Code',
            'State', 'Country'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)
        for o in aadata:
            email = ''
            country = ''
            postal_address_line_1 = ''
            postal_address_line_2 = ''
            post_code = ''
            state = ''
            vessel_length = ''
            vessel_name = ''
            vessel_rego = ''
            phone = ''
            mobile = ''
            sticker_no = ''
            suburb = ''
            if o['sticker_no']:
                sticker_no = o['sticker_no']
            if o['details']:
                phone = o['details']['phone']
                mobile = o['details']['mobile']
                vessel_length = o['details']['vessel_length']
                vessel_name = o['details']['vessel_name']
                vessel_rego = o['details']['vessel_rego']
                country = o['details']['country']
                postal_address_line_1 = o['details']['postal_address_line_1']
                postal_address_line_2 = o['details']['postal_address_line_2']
                state = o['details']['state']
                vessel_length = o['details']['vessel_length']
                if 'postcode' in o['details']:
                    post_code = o['details']['post_code']
                if 'suburb' in o['details']:
                    suburb = o['details']['suburb']

            writer.writerow([
                'AA' + str(o['id']), o['customer']['first_name'],
                o['customer']['last_name'], mobile, phone, vessel_rego,
                vessel_length, sticker_no, o['year'], o['status'],
                o['annual_booking_period_group_name'], postal_address_line_1,
                postal_address_line_2, suburb, post_code, state, country
            ])

        strIO.flush()
        strIO.seek(0)
        return strIO

    except:
        raise
Beispiel #7
0
class CaptureStdOut(object):
    """
    An logger that both prints to stdout and writes to file.
    """
    def __init__(self, log_file_path=None, print_to_console=True):
        """
        :param log_file_path: The path to save the records, or None if you just want to keep it in memory
        :param print_to_console:
        """
        self._print_to_console = print_to_console
        if log_file_path is not None:
            # self._log_file_path = os.path.join(base_dir, log_file_path.replace('%T', now))
            make_file_dir(log_file_path)
            self.log = open(log_file_path, 'w')
        else:
            self.log = StringIO()
        self._log_file_path = log_file_path
        self.old_stdout = _ORIGINAL_STDOUT

    def __enter__(self):

        self.old_stdout = sys.stdout
        self.old_stderr = sys.stderr

        sys.stdout = self
        sys.stderr = self
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout.flush()
        sys.stderr.flush()
        sys.stdout = self.old_stdout
        sys.stderr = self.old_stderr
        self.close()

    def get_log_file_path(self):
        assert self._log_file_path is not None, "You never specified a path when you created this logger, so don't come back and ask for one now"
        return self._log_file_path

    def write(self, message):
        if self._print_to_console:
            self.old_stdout.write(message)
        self.log.write(message)
        self.log.flush()

    def close(self):
        if self._log_file_path is not None:
            self.log.close()

    def read(self):
        if self._log_file_path is None:
            return self.log.getvalue()
        else:
            with open(self._log_file_path) as f:
                txt = f.read()
            return txt

    def __getattr__(self, item):
        return getattr(self.old_stdout, item)
Beispiel #8
0
def load_cookies_file(cookies_file):

    logging.debug('Loading cookie file %s into memory.', cookies_file)

    cookies = StringIO()
    cookies.write('# Netscape HTTP Cookie File')
    cookies.write(open(cookies_file, 'rU').read())
    cookies.flush()
    cookies.seek(0)
    return cookies
Beispiel #9
0
def generateOracleParserFile(oracle_codes):
    strIO = StringIO()
    fieldnames = ['Activity Code', 'Amount']
    writer = csv.writer(strIO)
    writer.writerow(fieldnames)
    for k, v in oracle_codes.items():
        if v != 0:
            writer.writerow([k, v])
    strIO.flush()
    strIO.seek(0)
    return strIO
Beispiel #10
0
def generateOracleParserFile(oracle_codes):
    strIO = StringIO()
    fieldnames = ['Activity Code','Amount']
    writer = csv.writer(strIO)
    writer.writerow(fieldnames)
    for k,v in oracle_codes.items():
        if v != 0:
            writer.writerow([k,v])
    strIO.flush()
    strIO.seek(0)
    return strIO
Beispiel #11
0
def booking_bpoint_settlement_report(_date):
    try:
        bpoint, cash = [], []
        bpoint.extend([x for x in BpointTransaction.objects.filter(created__date=_date,response_code=0,crn1__startswith='0019').exclude(crn1__endswith='_test')])
        cash = CashTransaction.objects.filter(created__date=_date,invoice__reference__startswith='0019').exclude(type__in=['move_out','move_in'])

        strIO = StringIO()
        fieldnames = ['Payment Date','Settlement Date','Confirmation Number','Name','Type','Amount','Invoice']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for b in bpoint:
            booking, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)
                try:
                    booking = BookingInvoice.objects.get(invoice_reference=invoice.reference).booking
                except BookingInvoice.DoesNotExist:
                    pass
                    
                if booking:
                    b_name = u'{} {}'.format(booking.details.get('first_name',''),booking.details.get('last_name',''))
                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),b.settlement_date.strftime('%d/%m/%Y'),booking.confirmation_number,b_name.encode('utf-8'),str(b.action),b.amount,invoice.reference])
                else:
                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.settlement_date.strftime('%d/%m/%Y'),'','',str(b.action),b.amount,invoice.reference])
            except Invoice.DoesNotExist:
                pass

        for b in cash:
            booking, invoice = None, None
            try:
                invoice = b.invoice 
                try:
                    booking = BookingInvoice.objects.get(invoice_reference=invoice.reference).booking
                except BookingInvoice.DoesNotExist:
                    pass
                    
                if booking:
                    b_name = u'{} {}'.format(booking.details.get('first_name',''),booking.details.get('last_name',''))
                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),b.created.strftime('%d/%m/%Y'),booking.confirmation_number,b_name.encode('utf-8'),str(b.type),b.amount,invoice.reference])
                else:
                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.created.strftime('%d/%m/%Y'),'','',str(b.type),b.amount,invoice.reference])
            except Invoice.DoesNotExist:
                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #12
0
class EncodedStringIO(object):
    def __init__(self):
        self._data = StringIO()
        self.encoding = "ascii"

    def read(self):
        return self._data.read()

    def write(self, data):
        return self._data.write(data)

    def flush(self):
        self._data.flush()
Beispiel #13
0
Datei: utils.py Projekt: pv/bento
class EncodedStringIO(object):
    def __init__(self):
        self._data = StringIO()
        self.encoding = "ascii"

    def read(self):
        return self._data.read()

    def write(self, data):
        return self._data.write(data)

    def flush(self):
        self._data.flush()
Beispiel #14
0
def mooring_booking_created(start, end):
    try:

        strIO = StringIO()
        fieldnames = [
            'Booking ID', 'Arrival', 'Departure', 'Booking Type', 'Cost Total',
            'Cancelled', 'Created', 'Customer ID', 'Phone', 'Mobile',
            'First Name', 'Last Name', 'Admission Booking ID'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)
        bookings = models.Booking.objects.filter(
            Q(created__gte=start, created__lte=end)
            & Q(Q(booking_type=1) | Q(booking_type=4) | Q(booking_type=5)))
        for b in bookings:
            admission_id = ''
            customer_id = ''
            first_name = ''
            last_name = ''
            created_nice = ''
            phone = ''
            mobile = ''
            if b.admission_payment:
                admission_id = b.admission_payment.id
            if b.customer:
                customer_id = b.customer.id
            if b.details:
                if 'first_name' in b.details:
                    first_name = b.details['first_name']
                if 'last_name' in b.details:
                    last_name = b.details['last_name']
                if 'phone' in b.details:
                    phone = b.details['phone']
                if 'mobile' in b.details:
                    mobile = b.details['mobile']

                created = b.created + timedelta(hours=8)
                created_nice = created.strftime('%d/%m/%Y %H:%M')
            writer.writerow([
                b.id, b.arrival, b.departure,
                b.get_booking_type_display(), b.cost_total, b.is_canceled,
                created_nice, customer_id, phone, mobile, first_name,
                last_name, admission_id
            ])

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #15
0
def _test_progress_bar(len):
    out = StringIO()
    fill_str = ('123456890' * (len//10))[:len]
    pb = DialogUI(out).get_progressbar('label', fill_str, maxval=10)
    pb.start()
    for x in range(11):
        pb.update(x)
        out.flush()  # needed atm
        pstr = out.getvalue()
        ok_startswith(pstr, 'label:')
        assert_in(' %d%% ' % (10*x), pstr)
        assert_in('ETA', pstr)
    pb.finish()
    ok_endswith(out.getvalue(), '\n')
Beispiel #16
0
def captured_logging(name=None):
    buffer = StringIO()
    logger = logging.getLogger(name)
    handlers = logger.handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)
    handler = logging.StreamHandler(buffer)
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    yield buffer
    buffer.flush()
    logger.removeHandler(handler)
    for handler in handlers:
        logger.addHandler(handler)
Beispiel #17
0
def captured_logging(name=None):
    buffer = StringIO()
    logger = logging.getLogger(name)
    handlers = logger.handlers
    for handler in logger.handlers:
        logger.removeHandler(handler)
    handler = logging.StreamHandler(buffer)
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler)
    yield buffer
    buffer.flush()
    logger.removeHandler(handler)
    for handler in handlers:
        logger.addHandler(handler)
Beispiel #18
0
def load_cookies_file(cookies_file):
    """
    Loads the cookies file.

    We pre-pend the file with the special Netscape header because the cookie
    loader is very particular about this string.
    """

    cookies = StringIO()
    cookies.write('# Netscape HTTP Cookie File')
    cookies.write(open(cookies_file, 'rU').read())
    cookies.flush()
    cookies.seek(0)
    return cookies
Beispiel #19
0
def load_cookies_file(cookies_file):
    """
    Loads the cookies file.

    We pre-pend the file with the special Netscape header because the cookie
    loader is very particular about this string.
    """

    cookies = StringIO()
    cookies.write('# Netscape HTTP Cookie File')
    cookies.write(open(cookies_file, 'rU').read())
    cookies.flush()
    cookies.seek(0)
    return cookies
Beispiel #20
0
def _test_progress_bar(len):
    out = StringIO()
    fill_str = ('123456890' * (len // 10))[:len]
    pb = DialogUI(out).get_progressbar('label', fill_str, maxval=10)
    pb.start()
    for x in range(11):
        pb.update(x)
        out.flush()  # needed atm
        pstr = out.getvalue()
        ok_startswith(pstr, 'label:')
        assert_in(' %d%% ' % (10 * x), pstr)
        assert_in('ETA', pstr)
    pb.finish()
    ok_endswith(out.getvalue(), '\n')
class _StreamCapturerBase(io.IOBase):
    """A base class for input/output stream capturers."""
    def __init__(self, real_stream):
        self._real_stream = real_stream
        self._capturing_stream = StringIO()

    def isatty(self, *args, **kwargs):
        return True

    def flush(self):
        self._capturing_stream.flush()
        self._real_stream.flush()

    def GetValue(self):
        return self._capturing_stream.getvalue()
Beispiel #22
0
def load_cookies_file(cookies_file):
    """
    Loads the cookies file.

    We pre-pend the file with the special Netscape header because the cookie
    loader is very particular about this string.
    """

    logging.debug("Loading cookie file %s into memory.", cookies_file)

    cookies = StringIO()
    cookies.write("# Netscape HTTP Cookie File")
    cookies.write(open(cookies_file, "rU").read())
    cookies.flush()
    cookies.seek(0)
    return cookies
Beispiel #23
0
class _StreamCapturerBase(io.IOBase):
  """A base class for input/output stream capturers."""

  def __init__(self, real_stream):
    self._real_stream = real_stream
    self._capturing_stream = StringIO()

  def isatty(self, *args, **kwargs):
    return True

  def flush(self):
    self._capturing_stream.flush()
    self._real_stream.flush()

  def GetValue(self):
    return self._capturing_stream.getvalue()
class _StreamCapturerBase(io.IOBase):
    """A base class for input/output stream capturers."""

    # pylint: disable=super-init-not-called, All this is about to be deleted.
    def __init__(self, real_stream):
        self._real_stream = real_stream
        self._capturing_stream = StringIO()

    def isatty(self, *args, **kwargs):
        return True

    def flush(self):
        self._capturing_stream.flush()
        self._real_stream.flush()

    def GetValue(self):
        return self._capturing_stream.getvalue()
Beispiel #25
0
class StdoutCapture(object):
    def __init__(self):
        self.captured = StringIO()

    def start(self):
        sys.stdout = self.captured
        return self

    def stop(self):
        sys.stdout = sys.__stdout__
        return self

    def value(self):
        self.captured.flush()
        return self.captured.getvalue()

    def close(self):
        self.captured.close()
Beispiel #26
0
class StdoutCapture(object):
    def __init__(self):
        self.captured = StringIO()

    def start(self):
        sys.stdout = self.captured
        return self

    def stop(self):
        sys.stdout = sys.__stdout__
        return self

    def value(self):
        self.captured.flush()
        return self.captured.getvalue()

    def close(self):
        self.captured.close()
Beispiel #27
0
def outstanding_bookings():
    try:
        outstanding = []
        today = datetime.date.today()
        for b in Booking.objects.filter(
                is_canceled=False,
                departure__gte=today).exclude(booking_type__in=['1', '3']):
            if not b.paid:
                outstanding.append(b)

        strIO = StringIO()
        fieldnames = [
            'Confirmation Number', 'Customer', 'Campground', 'Arrival',
            'Departure', 'Outstanding Amount'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)
        for o in outstanding:
            fullname = '{} {}'.format(o.details.get('first_name'),
                                      o.details.get('last_name'))
            writer.writerow([
                o.confirmation_number, fullname, o.campground.name,
                o.arrival.strftime('%d/%m/%Y'),
                o.departure.strftime('%d/%m/%Y'), o.outstanding
            ])
        strIO.flush()
        strIO.seek(0)
        _file = strIO

        dt = datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')
        recipients = []
        recipients = OutstandingBookingRecipient.objects.all()
        email = EmailMessage('Unpaid Bookings Summary as at {}'.format(dt),
                             'Unpaid Bookings as at {}'.format(dt),
                             settings.DEFAULT_FROM_EMAIL,
                             to=[r.email for r in recipients]
                             if recipients else [settings.NOTIFICATION_EMAIL])
        email.attach('OustandingBookings_{}.csv'.format(dt), _file.getvalue(),
                     'text/csv')
        email.send()
    except:
        raise
Beispiel #28
0
def _test_progress_bar(backend, len, increment):
    out = StringIO()
    fill_str = ('123456890' * (len//10))[:len]
    pb = DialogUI(out).get_progressbar('label', fill_str, maxval=10, backend=backend)
    pb.start()
    # we can't increment 11 times
    for x in range(11):
        if not (increment and x == 0):
            # do not increment on 0
            pb.update(x if not increment else 1, increment=increment)
        out.flush()  # needed atm
        pstr = out.getvalue()
        if backend not in ('annex-remote',):  # no str repr
            ok_startswith(pstr.lstrip('\r'), 'label:')
            assert_re_in(r'.*\b%d%%.*' % (10*x), pstr)
        if backend == 'progressbar':
            assert_in('ETA', pstr)
    pb.finish()
    if backend not in ('annex-remote',):
        ok_endswith(out.getvalue(), '\n')
Beispiel #29
0
def admission_booking_created(start, end):
    try:

        strIO = StringIO()
        fieldnames = [
            'Admission Booking ID', 'Booking Type', 'Cost Total', 'Created',
            'Customer ID', 'Mobile', 'First Name', 'Last Name'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)
        bookings = models.AdmissionsBooking.objects.filter(
            Q(created__gte=start, created__lte=end)
            & Q(Q(booking_type=1) | Q(booking_type=4) | Q(booking_type=5)))
        for b in bookings:
            admission_id = ''
            customer_id = ''
            first_name = ''
            last_name = ''
            created_nice = ''
            if b.customer:
                customer_id = b.customer.id
                first_name = b.customer.first_name
                last_name = b.customer.last_name
                created = b.created + timedelta(hours=8)
                created_nice = created.strftime('%d/%m/%Y %H:%M')

            writer.writerow([
                b.id,
                b.get_booking_type_display(), b.totalCost, created_nice,
                customer_id, b.mobile, first_name, last_name
            ])

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #30
0
def booking_bpoint_settlement_report(_date):
    try:
        bpoint, bpay, cash = [], [], []
        bpoint.extend([
            x for x in BpointTransaction.objects.filter(
                created__date=_date, response_code=0,
                crn1__startswith='0517').exclude(crn1__endswith='_test')
        ])
        bpay.extend([
            x for x in BpayTransaction.objects.filter(
                p_date__date=_date, crn__startswith='0517').exclude(
                    crn__endswith='_test')
        ])
        cash = CashTransaction.objects.filter(
            created__date=_date,
            invoice__reference__startswith='0517').exclude(
                type__in=['move_out', 'move_in'])

        strIO = StringIO()
        fieldnames = [
            'Payment Date', 'Settlement Date', 'Confirmation Number', 'Name',
            'Type', 'Amount', 'Invoice'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for b in bpoint:
            approval, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)

                try:
                    approval = AnnualRentalFee.objects.get(
                        invoice_reference=invoice.reference).approval
                except AnnualRentalFee.DoesNotExist:
                    pass

                try:
                    application_fee = ApplicationFeeInvoice.objects.get(
                        invoice_reference=invoice.reference).application_fee
                except ApplicationFeeInvoice.DoesNotExist:
                    pass

                if approval:
                    b_name = u'{}'.format(approval.relevant_applicant)
                    created = timezone.localtime(
                        b.created, pytz.timezone('Australia/Perth'))
                    settlement_date = b.settlement_date.strftime(
                        '%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        approval.lodgement_number, b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                elif application_fee:
                    b_name = u'{}'.format(application_fee.proposal.applicant)
                    created = timezone.localtime(
                        application_fee.created,
                        pytz.timezone('Australia/Perth'))
                    settlement_date = b.settlement_date.strftime(
                        '%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        application_fee.proposal.lodgement_number, b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                else:
                    writer.writerow([
                        b.created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.settlement_date.strftime('%d/%m/%Y'), '', '',
                        str(b.action), b.amount, invoice.reference
                    ])
            except Invoice.DoesNotExist:
                pass

        for b in bpay:
            booking, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn)
                try:
                    booking = AnnualRentalFee.objects.get(
                        invoice_reference=invoice.reference).booking
                except AnnualRentalFee.DoesNotExist:
                    pass

                if booking:
                    b_name = u'{}'.format(booking.proposal.applicant)
                    created = timezone.localtime(
                        b.created, pytz.timezone('Australia/Perth'))
                    settlement_date = b.p_date.strftime('%d/%m/%Y')
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        booking.admission_number, b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                else:
                    writer.writerow([
                        b.created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.settlement_date.strftime('%d/%m/%Y'), '', '',
                        str(b.action), b.amount, invoice.reference
                    ])
            except Invoice.DoesNotExist:
                pass

        for b in cash:
            booking, invoice = None, None
            try:
                invoice = b.invoice
                try:
                    booking = AnnualRentalFee.objects.get(
                        invoice_reference=invoice.reference).booking
                except AnnualRentalFee.DoesNotExist:
                    pass

                if booking:
                    b_name = u'{} {}'.format(
                        booking.details.get('first_name', ''),
                        booking.details.get('last_name', ''))
                    created = timezone.localtime(
                        b.created, pytz.timezone('Australia/Perth'))
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.created.strftime('%d/%m/%Y'),
                        booking.confirmation_number, b_name,
                        str(b.type), b.amount, invoice.reference
                    ])
                else:
                    writer.writerow([
                        b.created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.created.strftime('%d/%m/%Y'), '', '',
                        str(b.type), b.amount, invoice.reference
                    ])
            except Invoice.DoesNotExist:
                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #31
0
def generate_trans_csv(system, start, end, region=None, district=None):
    # Get invoices matching the system and date range
    strIO = None
    invoices = Invoice.objects.filter(system=system)
    if invoices:
        strIO = StringIO()
        fieldnames = [
            'Created', 'Payment Method', 'Transaction Type', 'Amount',
            'Approved', 'Source', 'Product Names', 'Product Codes'
        ]
        writer = csv.DictWriter(strIO, fieldnames=fieldnames)
        writer.writeheader()
        for i in invoices:
            items = item_codes = bpay = bpoint = cash = None
            item_names = []
            oracle_codes = []
            # Get all items for this invoice
            if i.order:
                items = i.order.lines.all().values('title', 'oracle_code')
                for item in items:
                    item_names.append(item.get('title'))
                    code = item.get('oracle_code')
                    if not code: code = 'N\A'
                    oracle_codes.append(code)
                item_names = '|'.join(item_names)
                oracle_codes = '|'.join(oracle_codes)
            # Get all transactions for this invoice
            '''params = {
                'created__gte':start,
                'created__lte': end
            }'''
            cash = i.cash_transactions.filter(created__gte=start,
                                              created__lte=end,
                                              district=district)
            if not district:
                bpoint = i.bpoint_transactions.filter(created__gte=start,
                                                      created__lte=end)
                bpay = i.bpay_transactions.filter(p_date__gte=start,
                                                  p_date__lte=end)
            # Write out the cash transactions
            for c in cash:
                cash_info = {
                    'Created': c.created.strftime('%Y-%m-%d'),
                    'Payment Method': 'Cash',
                    'Transaction Type': c.type.lower(),
                    'Amount': c.amount,
                    'Approved': 'True',
                    'Source': c.source,
                    'Product Names': item_names,
                    'Product Codes': oracle_codes
                }
                writer.writerow(cash_info)
            if not district:
                # Write out all bpay transactions
                for b in bpay:
                    bpay_info = {
                        'Created': b.created.strftime('%Y-%m-%d'),
                        'Payment Method': 'BPAY',
                        'Transaction Type': b.get_p_instruction_code_display(),
                        'Amount': b.amount,
                        'Approved': b.approved,
                        'Source': 'N/A',
                        'Product Names': item_names,
                        'Product Codes': oracle_codes
                    }
                    writer.writerow(bpay_info)
                # Write out all bpoint transactions
                for bpt in bpoint:
                    bpoint_info = {
                        'Created': bpt.created.strftime('%Y-%m-%d'),
                        'Payment Method': 'BPOINT',
                        'Transaction Type': bpt.action.lower(),
                        'Amount': bpt.amount,
                        'Approved': bpt.approved,
                        'Source': 'N/A',
                        'Product Names': item_names,
                        'Product Codes': oracle_codes
                    }
                    writer.writerow(bpoint_info)
        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #32
0
def generate_items_csv(system,start,end,banked_start,banked_end,region=None,district=None):
    strIO = None
    invoices = Invoice.objects.filter(system=system)
    dates, banked_dates = [], []
    date_amounts, banked_date_amounts = [], []
    items = []
    date_format = '%A %d/%m/%y'
    if invoices:
        strIO = StringIO()
        fieldnames = ['Account Code', 'Day']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for d in daterange(start,end):
            dates.append(d.strftime(date_format))
            date_amounts.append({
                'date':d.strftime(date_format),
                'amounts':{
                    'card': D('0.0'),
                    'bpay': D('0.0'),
                    'eft': D('0.0'),
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })
        for d in daterange(banked_start,banked_end):
            banked_dates.append(d.strftime(date_format))
            banked_date_amounts.append({
                'date':d.strftime(date_format),
                'amounts':{
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })

        dates_row = ''
        for date in dates:
            dates_row += '{},,,'.format(date)

        # Dates row
        writer.writerow(['']+ dates_row.split(','))
        writer.writerow([''] + ['Credit Card','Bpay','EFTPOS'] * len(dates) + ['','Credit Card','Bpay','EFTPOS'])
        for i in invoices:
            # Add items of invoice if not in list
            if i.order:
                for x in i.order.lines.all():
                    item_date_amounts, banked_item_dates_amounts = [], []
                    for d in dates:
                        item_date_amounts.append({
                            'date':d,
                            'amounts':{
                                'card': D('0.0'),
                                'bpay': D('0.0'),
                                'eft': D('0.0'),
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })
                    for d in banked_dates:
                        banked_item_dates_amounts.append({
                            'date':d,
                            'amounts':{
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })
                    
                    if not any(it.get('item', None).oracle_code == x.oracle_code for it in items):
                        items.append({
                            'dates':item_date_amounts,
                            'banked_dates': banked_item_dates_amounts,
                            'item': x,
                            'card': D('0.0'),
                            'bpay': D('0.0'),
                            'eft': D('0.0'),
                            'cash': D('0.0'),
                            'cheque': D('0.0'),
                            'money_order': D('0.0')
                        })
            # Get all transactions
            cash = i.cash_transactions.filter(created__gte=start, created__lte=end)
            banked_cash = i.cash_transactions.filter(created__gte=banked_start, created__lte=banked_end)
            bpoint = i.bpoint_transactions.filter(created__gte=start, created__lte=end)
            bpay = i.bpay_transactions.filter(p_date__gte=start, p_date__lte=end)
            # Go through items

            for item in items:
                # Banked Cash
                for d in item['banked_dates']:
                    index = 0
                    for l in banked_date_amounts:
                        if l.get('date') == d.get('date'):
                            date_amount_index = index
                            break
                        index += 1
                    for c in banked_cash:
                        for s in CashTransaction.SOURCE_TYPES:
                            source = str(s[0])
                            if c.source == source and source != 'eft':
                                if c.created.strftime(date_format) == d.get('date'):
                                    if c.type == 'payment':
                                        d['amounts'][source] += D(item.get('item').line_price_before_discounts_incl_tax)
                                        item[source] += D(item.get('item').line_price_before_discounts_incl_tax)
                                        banked_date_amounts[date_amount_index]['amounts'][source] += D(item.get('item').line_price_before_discounts_incl_tax)
                                    else:
                                        d['amounts'][source] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                        item[source] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                        banked_date_amounts[date_amount_index]['amounts'][source] -= D(item.get('item').line_price_before_discounts_incl_tax)
                # Other transactions
                for d in item['dates']:
                    index = 0
                    for l in date_amounts:
                        if l.get('date') == d.get('date'):
                            date_amount_index = index
                            break
                        index += 1
                    # EFT
                    for c in cash:
                        if c.created.strftime(date_format) == d.get('date'):
                            if c.type == 'payment'and c.source == 'eft':
                                d['amounts']['eft'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                item['eft'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                date_amounts[date_amount_index]['amounts']['eft'] += D(item.get('item').line_price_before_discounts_incl_tax)
                            else:
                                d['amounts']['eft'] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                item['eft'] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                date_amounts[date_amount_index]['amounts']['eft'] -= D(item.get('item').line_price_before_discounts_incl_tax)

                    # Card
                    for c in bpoint:
                        if c.approved:
                            if c.created.strftime(date_format) == d.get('date'):
                                if c.action == 'payment' or c.action == 'capture':
                                    d['amounts']['card'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                    item['card'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts']['card'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                elif c.action == 'reversal' or c.action == 'refund':
                                    d['amounts']['card'] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                    item['card'] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts']['card'] -= D(item.get('item').line_price_before_discounts_incl_tax)
                    # BPAY
                    for b in bpay:
                        if b.approved:
                            if b.p_date.strftime(date_format) == d.get('date'):
                                if b.p_instruction_code == '05' and b.type == '399':
                                    d['amounts']['bpay'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                    item['bpay'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts']['bpay'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                elif b.p_instruction_code == '25' and b.type == '699':
                                    d['amounts']['bpay'] += D(item.get('item').line_price_before_discounts_incl_tax)
                                    item['bpay'] -= D(item.get('item').line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts']['bpay'] -= D(item.get('item').line_price_before_discounts_incl_tax)
        for i in items:
            item_str = ''
            item_str += '{},'.format(str(i.get('item').oracle_code))
            for d in i['dates']:
                item_str += '{},{},{},'.format(d['amounts']['card'],d['amounts']['bpay'],d['amounts']['eft'])
            item_str += ',{},{},{},'.format(i['card'],i['bpay'],i['eft'])
            writer.writerow(item_str.split(','))

        total_str = 'Totals,'
        total_amounts = {
            'card': D('0.0'),
            'bpay': D('0.0'),
            'eft': D('0.0')
        }
        for d in date_amounts:
            total_amounts['card'] += d['amounts']['card']
            total_amounts['bpay'] += d['amounts']['bpay']
            total_amounts['eft'] += d['amounts']['eft']
            total_str += '{},{},{},'.format(d['amounts']['card'],d['amounts']['bpay'],d['amounts']['eft'])
        total_str += ',{},{},{},'.format(total_amounts['card'],total_amounts['bpay'],total_amounts['eft'])
        writer.writerow('')
        writer.writerow(total_str.split(','))

        # Banked Items
        writer.writerow('')
        writer.writerow(fieldnames)
        banked_dates_row = ''
        for date in banked_dates:
            banked_dates_row += '{},,,'.format(date)
        writer.writerow(['']+ banked_dates_row.split(','))
        writer.writerow([''] + ['Cash','Cheque','Money Order'] * len(banked_dates) + ['','Cash','Cheque','Money Order','Banked(Cash,Money Order,Cheque)'])

        for i in items:
            banked_item_str = ''
            banked_item_str += '{},'.format(str(i.get('item').oracle_code))
            for d in i['banked_dates']:
                banked_item_str += '{},{},{},'.format(d['amounts']['cash'],d['amounts']['cheque'],d['amounts']['money_order'])
            banked_item_str += ',{},{},{},'.format(i['cash'],i['cheque'],i['money_order'])
            writer.writerow(banked_item_str.split(','))

        banked_total_str = 'Totals,'
        banked_total_amounts = {
            'cash': D('0.0'),
            'cheque': D('0.0'),
            'money_order': D('0.0')
        }
        for d in banked_date_amounts:
            banked_total_amounts['cash'] += d['amounts']['cash']
            banked_total_amounts['cheque'] += d['amounts']['cheque']
            banked_total_amounts['money_order'] += d['amounts']['money_order']
            banked_total_str += '{},{},{},'.format(d['amounts']['cash'],d['amounts']['cheque'],d['amounts']['money_order'])
        banked_total_str += ',{},{},{},'.format(banked_total_amounts['cash'],banked_total_amounts['cheque'],banked_total_amounts['money_order'])
        writer.writerow('')
        writer.writerow(banked_total_str.split(','))

        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #33
0
def generate_trans_csv(system,start,end,region=None,district=None):
    # Get invoices matching the system and date range
    strIO = None
    invoices = Invoice.objects.filter(system=system)
    if invoices:
        strIO = StringIO()
        fieldnames = ['Created', 'Payment Method', 'Transaction Type', 'Amount', 'Approved', 'Source', 'Product Names',
                      'Product Codes']
        writer = csv.DictWriter(strIO, fieldnames=fieldnames)
        writer.writeheader()
        for i in invoices:
            items = item_codes = bpay = bpoint = cash = None
            item_names = []
            oracle_codes = []
            # Get all items for this invoice
            if i.order:
                items = i.order.lines.all().values('title', 'oracle_code')
                for item in items:
                    item_names.append(item.get('title'))
                    code = item.get('oracle_code')
                    if not code: code = 'N\A'
                    oracle_codes.append(code)
                item_names = '|'.join(item_names)
                oracle_codes = '|'.join(oracle_codes)
            # Get all transactions for this invoice
            '''params = {
                'created__gte':start,
                'created__lte': end
            }'''
            cash = i.cash_transactions.filter(created__gte=start, created__lte=end, district=district)
            if not district:
                bpoint = i.bpoint_transactions.filter(created__gte=start, created__lte=end)
                bpay = i.bpay_transactions.filter(p_date__gte=start, p_date__lte=end)
            # Write out the cash transactions
            for c in cash:
                cash_info = {
                    'Created': c.created.strftime('%Y-%m-%d'),
                    'Payment Method': 'Cash',
                    'Transaction Type': c.type.lower(),
                    'Amount': c.amount,
                    'Approved': 'True',
                    'Source': c.source,
                    'Product Names': item_names,
                    'Product Codes': oracle_codes
                }
                writer.writerow(cash_info)
            if not district:
                # Write out all bpay transactions
                for b in bpay:
                    bpay_info = {
                        'Created': b.created.strftime('%Y-%m-%d'),
                        'Payment Method': 'BPAY',
                        'Transaction Type': b.get_p_instruction_code_display(),
                        'Amount': b.amount,
                        'Approved': b.approved,
                        'Source': 'N/A',
                        'Product Names': item_names,
                        'Product Codes': oracle_codes
                    }
                    writer.writerow(bpay_info)
                # Write out all bpoint transactions
                for bpt in bpoint:
                    bpoint_info = {
                        'Created': bpt.created.strftime('%Y-%m-%d'),
                        'Payment Method': 'BPOINT',
                        'Transaction Type': bpt.action.lower(),
                        'Amount': bpt.amount,
                        'Approved': bpt.approved,
                        'Source': 'N/A',
                        'Product Names': item_names,
                        'Product Codes': oracle_codes
                    }
                    writer.writerow(bpoint_info)
        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #34
0
def generate_trans_csv(system,start,end,region=None,district=None):
    # Get invoices matching the system and date range
    strIO = None
    invoices = []

    cash = None
    bpoint = None
    bpay = None

    # Get all transactions
    cash = CashTransaction.objects.filter(created__gte=start, created__lte=end,district=district,invoice__system=system).order_by('-created')
    bpoint = BpointTransaction.objects.filter(settlement_date__gte=start, settlement_date__lte=end,crn1__startswith=system).order_by('-created').exclude(crn1__endswith='_test')
    bpay = BpayTransaction.objects.filter(p_date__gte=start, p_date__lte=end,crn__startswith=system).order_by('-created')

    # Print the header
    strIO = StringIO()
    fieldnames = ['Created','Settlement Date', 'Payment Method', 'Transaction Type', 'Amount', 'Approved', 'Source', 'Product Names',
                  'Product Codes', 'Invoice']
    writer = csv.DictWriter(strIO, fieldnames=fieldnames)
    writer.writeheader()

    # Iterate through transactions
    for c in cash:
        i = c.invoice
        items = item_codes = None
        item_names = []
        oracle_codes = []
        # Get all items for this invoice
        if i.order:
            items = i.order.lines.all().values('title', 'oracle_code')
            for item in items:
                item_names.append(item.get('title'))
                code = item.get('oracle_code')
                if not code: code = 'N\A'
                oracle_codes.append(code)
            item_names = '|'.join(item_names)
            oracle_codes = '|'.join(oracle_codes)

        
        if c.type not in ['move_in','move_out']:
            cash_info = {
                'Created': c.created.astimezone(PERTH_TIMEZONE).strftime('%d/%m/%Y %H:%M:%S'),
                'Settlement Date': c.created.strftime('%d/%m/%Y'),
                'Invoice': c.invoice.reference,
                'Payment Method': 'Cash',
                'Transaction Type': c.type.lower(),
                'Amount': c.amount if c.type not in ['refund','move_out'] else '-{}'.format(c.amount),
                'Approved': 'True',
                'Source': c.source,
                'Product Names': item_names,
                'Product Codes': oracle_codes
            }
            writer.writerow(cash_info)
    if not district:
        # Write out all bpay transactions
        if bpay:
            for b in bpay:
                i = Invoice.objects.get(reference=b.crn)
                items = item_codes = None
                item_names = []
                oracle_codes = []
                # Get all items for this invoice
                if i.order:
                    items = i.order.lines.all().values('title', 'oracle_code')
                    for item in items:
                        item_names.append(item.get('title'))
                        code = item.get('oracle_code')
                        if not code: code = 'N\A'
                        oracle_codes.append(code)
                    item_names = '|'.join(item_names)
                    oracle_codes = '|'.join(oracle_codes)

                bpay_info = {
                    'Created': b.created.astimezone(PERTH_TIMEZONE).strftime('%d/%m/%Y %H:%M:%S'),
                    'Settlement Date': b.p_date.strftime('%d/%m/%Y'),
                    'Invoice': b.crn,
                    'Payment Method': 'BPAY',
                    'Transaction Type': b.get_p_instruction_code_display(),
                    'Amount': b.amount if b.p_instruction_code == '05' and b.type == '399' else '-{}'.format(b.amount),
                    'Approved': b.approved,
                    'Source': 'N/A',
                    'Product Names': item_names,
                    'Product Codes': oracle_codes
                }
                writer.writerow(bpay_info)
        # Write out all bpoint transactions
        if bpoint:
            for bpt in bpoint:
                i = Invoice.objects.get(reference=bpt.crn1)
                items = item_codes = None
                item_names = []
                oracle_codes = []
                # Get all items for this invoice
                if i.order:
                    items = i.order.lines.all().values('title', 'oracle_code')
                    for item in items:
                        item_names.append(item.get('title'))
                        code = item.get('oracle_code')
                        if not code: code = 'N\A'
                        oracle_codes.append(code)
                    item_names = '|'.join(item_names)
                    oracle_codes = '|'.join(oracle_codes)

                bpoint_info = {
                    'Created': bpt.created.astimezone(PERTH_TIMEZONE).strftime('%d/%m/%Y %H:%M:%S'),
                    'Settlement Date': bpt.settlement_date.strftime('%d/%m/%Y'),
                    'Invoice': bpt.crn1,
                    'Payment Method': 'BPOINT',
                    'Transaction Type': bpt.action.lower(),
                    'Amount': bpt.amount if bpt.action not in ['refund'] else '-{}'.format(bpt.amount),
                    'Approved': bpt.approved,
                    'Source': 'N/A',
                    'Product Names': item_names,
                    'Product Codes': oracle_codes
                }
                writer.writerow(bpoint_info)

    strIO.flush()
    strIO.seek(0)
    return strIO
Beispiel #35
0
def generate_items_csv(system,start,end,banked_start,banked_end,region=None,district=None):
    strIO = None
    invoices = []
    invoice_list = []
    dates, banked_dates = [], []
    date_amounts, banked_date_amounts = [], []
    items = []
    oracle_codes = {}
    banked_oracle_codes = {}
    date_format = '%d/%m/%y'

    eftpos = []
    banked_cash = []
    bpoint = []
    bpay = []

    # Get all transactions
    if not district:
        eftpos.extend([x for x in CashTransaction.objects.filter(created__gte=start, created__lte=end, source='eftpos').exclude(district__isnull=False)])
        banked_cash.extend([x for x in CashTransaction.objects.filter(created__gte=banked_start, created__lte=banked_end).exclude(source='eftpos').exclude(district__isnull=False)])
        bpoint.extend([x for x in BpointTransaction.objects.filter(settlement_date__gte=start, settlement_date__lte=end).exclude(crn1__endswith='_test')])
        bpay.extend([x for x in BpayTransaction.objects.filter(p_date__gte=start, p_date__lte=end)])
    else:
        eftpos.extend([x for x in CashTransaction.objects.filter(created__gte=start, created__lte=end, source='eftpos',district=district)])
        banked_cash.extend([x for x in CashTransaction.objects.filter(created__gte=banked_start, created__lte=banked_end,district=district).exclude(source='eftpos')])

    # Get the required invoices
    for e in eftpos:
        if e.invoice.reference not in invoice_list:
            if e.invoice.system == system:
                invoices.append(e.invoice)
                invoice_list.append(str(e.invoice.reference))
    for b in banked_cash:
        if b.invoice.reference not in invoice_list:
            if b.invoice.system == system:
                invoices.append(b.invoice)
                invoice_list.append(str(b.invoice.reference))
    for b in bpoint:
        if b.crn1 not in invoice_list:
            invoice = Invoice.objects.get(reference=b.crn1)
            if invoice.system == system:
                invoices.append(invoice)
                invoice_list.append(str(b.crn1))
    for b in bpay:
        if b.crn not in invoice_list:
            invoice = Invoice.objects.get(reference=b.crn)
            if invoice.system == system:
                invoices.append(invoice)
                invoice_list.append(str(b.crn))

    invoice_list = list(set(invoice_list))

    if invoices:
        strIO = StringIO()
        fieldnames = ['Account Code', 'Day']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for d in daterange(start,end):
            dates.append(d.strftime(date_format))
            date_amounts.append({
                'date':d.strftime(date_format),
                'amounts':{
                    'card': D('0.0'),
                    'bpay': D('0.0'),
                    'eftpos': D('0.0'),
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })
        for d in daterange(banked_start,banked_end):
            banked_dates.append(d.strftime(date_format))
            banked_date_amounts.append({
                'date':d.strftime(date_format),
                'amounts':{
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })

        dates_row = ''
        for date in dates:
            dates_row += '{},,,'.format(date)

        # Dates row
        writer.writerow(['']+ dates_row.split(','))
        writer.writerow([''] + ['Credit Card','Bpay','EFTPOS'] * len(dates) + ['','Credit Card','Bpay','EFTPOS'])

        # Loop through the payments and not the invoices
        for i in invoices:
            # Add items of invoice if not in list

            if i.order:# and not i.voided:
                lines = i.order.lines.all()
                for x in lines:
                    #print((i, i.__dict__, x, x.oracle_code))
                    item_date_amounts, banked_item_dates_amounts = [], []
                    for d in dates:
                        item_date_amounts.append({
                            'date':d,
                            'amounts':{
                                'card': D('0.0'),
                                'bpay': D('0.0'),
                                'eftpos': D('0.0'),
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })
                    for d in banked_dates:
                        banked_item_dates_amounts.append({
                            'date':d,
                            'amounts':{
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })

                    items.append({
                        'dates':item_date_amounts,
                        'banked_dates': banked_item_dates_amounts,
                        'item': x,
                        'card': D('0.0'),
                        'bpay': D('0.0'),
                        'eftpos': D('0.0'),
                        'cash': D('0.0'),
                        'cheque': D('0.0'),
                        'money_order': D('0.0')
                    })

                    # create empty subtotal list for each oracle code
                    if x.oracle_code not in oracle_codes:
                        oracle_codes[x.oracle_code] = []
                        for d in daterange(start,end):
                            oracle_codes[x.oracle_code].append({
                                'date':d.strftime(date_format),
                                'amounts':{
                                    'card': D('0.0'),
                                    'bpay': D('0.0'),
                                    'eftpos': D('0.0'),
                                    'cash': D('0.0'),
                                    'cheque': D('0.0'),
                                    'money_order': D('0.0')
                                }
                            })

                    if x.oracle_code not in banked_oracle_codes:
                        banked_oracle_codes[x.oracle_code] = []
                        for d in daterange(banked_start,banked_end):
                            banked_oracle_codes[x.oracle_code].append({
                                'date':d.strftime(date_format),
                                'amounts':{
                                    'cash': D('0.0'),
                                    'cheque': D('0.0'),
                                    'money_order': D('0.0')
                                }
                            })

        for item in items:
            price = D(item.get('item').line_price_before_discounts_incl_tax)
            code = item.get('item').oracle_code
            order = item.get('item').order

            payment_details = item.get('item').payment_details
            refund_details = item.get('item').refund_details
            deduction_details = item.get('item').deduction_details

            # Banked Cash
            for d in item['banked_dates']:
                index = 0
                for l in banked_date_amounts:
                    if l.get('date') == d.get('date'):
                        date_amount_index = index
                        break
                    index += 1

                for k,v in payment_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source in set(['cash','cheque','money_order']):
                        if c.created.strftime(date_format) == d.get('date'):
                            banked_oracle_codes[code][date_amount_index]['amounts'][source] += D(v)
                            item[source] += D(v)
                            banked_date_amounts[date_amount_index]['amounts'][source] += D(v)

                for k,v in refund_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source in set(['cash','cheque','money_order']):
                        if c.created.strftime(date_format) == d.get('date'):
                            banked_oracle_codes[code][date_amount_index]['amounts'][source] -= D(v)
                            item[source] -= D(v)
                            banked_date_amounts[date_amount_index]['amounts'][source] -= D(v)

                for k,v in deduction_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source in set(['cash','cheque','money_order']):
                        if c.created.strftime(date_format) == d.get('date'):
                            banked_oracle_codes[code][date_amount_index]['amounts'][source] -= D(v)
                            item[source] -= D(v)
                            banked_date_amounts[date_amount_index]['amounts'][source] -= D(v)


            # Other transactions
            for d in oracle_codes[code]:
                index = 0
                for l in date_amounts:
                    if l.get('date') == d.get('date'):
                        date_amount_index = index
                        break
                    index += 1
                # EFT
                for k,v in payment_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source == 'eftpos':
                        if c.created.strftime(date_format) == d.get('date'):
                            oracle_codes[code][date_amount_index]['amounts']['eftpos'] += D(v)
                            item['eftpos'] += D(v)
                            date_amounts[date_amount_index]['amounts']['eftpos'] += D(v)

                for k,v in refund_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source == 'eftpos':
                        if c.created.strftime(date_format) == d.get('date'):
                            oracle_codes[code][date_amount_index]['amounts']['eftpos'] -= D(v)
                            item['eftpos'] -= D(v)
                            date_amounts[date_amount_index]['amounts']['eftpos'] -= D(v)

                # Card
                for k,v in payment_details['card'].items():
                    c = BpointTransaction.objects.get(id=int(k))
                    if c.settlement_date.strftime(date_format) == d.get('date') and c.approved:
                        oracle_codes[code][date_amount_index]['amounts']['card'] += D(v)
                        item['card'] += D(v)
                        date_amounts[date_amount_index]['amounts']['card'] += D(v)

                for k,v in refund_details['card'].items():
                    c = BpointTransaction.objects.get(id=int(k))
                    if c.settlement_date.strftime(date_format) == d.get('date') and c.approved:
                        oracle_codes[code][date_amount_index]['amounts']['card'] -= D(v)
                        item['card'] -= D(v)
                        date_amounts[date_amount_index]['amounts']['card'] -= D(v)

                # BPAY
                for k,v in payment_details['bpay'].items():
                    b = BpayTransaction.objects.get(id=int(k))
                    if b.approved and b.p_date.strftime(date_format) == d.get('date'):
                        oracle_codes[code][date_amount_index]['amounts']['bpay'] += D(v)
                        item['bpay'] += D(v)
                        date_amounts[date_amount_index]['amounts']['bpay'] += D(v)

                for k,v in refund_details['bpay'].items():
                    b = BpayTransaction.objects.get(id=int(k))
                    if b.approved and b.p_date.strftime(date_format) == d.get('date'):
                        oracle_codes[code][date_amount_index]['amounts']['bpay'] -= D(v)
                        item['bpay'] -= D(v)
                        date_amounts[date_amount_index]['amounts']['bpay'] -= D(v)

        for code in oracle_codes:
            item_str = ''
            item_str += '{},'.format(code)
            card_total = D('0.0')
            bpay_total = D('0.0')
            eftpos_total = D('0.0')
            for d in oracle_codes[code]:
                item_str += '{},{},{},'.format(d['amounts']['card'],d['amounts']['bpay'],d['amounts']['eftpos'])
                card_total += d['amounts']['card']
                bpay_total += d['amounts']['bpay']
                eftpos_total += d['amounts']['eftpos']
            item_str += ',{},{},{},'.format(card_total, bpay_total, eftpos_total)
            if not ((card_total == D('0.0')) and (bpay_total == D('0.0')) and (eftpos_total == D('0.0'))):
                writer.writerow(item_str.split(','))

        total_str = 'Totals,'
        total_amounts = {
            'card': D('0.0'),
            'bpay': D('0.0'),
            'eftpos': D('0.0')
        }
        for d in date_amounts:
            total_amounts['card'] += d['amounts']['card']
            total_amounts['bpay'] += d['amounts']['bpay']
            total_amounts['eftpos'] += d['amounts']['eftpos']
            total_str += '{},{},{},'.format(d['amounts']['card'],d['amounts']['bpay'],d['amounts']['eftpos'])
        total_str += ',{},{},{},'.format(total_amounts['card'],total_amounts['bpay'],total_amounts['eftpos'])
        writer.writerow('')
        writer.writerow(total_str.split(','))

        # Banked Items
        writer.writerow('')
        writer.writerow(fieldnames)
        banked_dates_row = ''
        for date in banked_dates:
            banked_dates_row += '{},,,'.format(date)
        writer.writerow(['']+ banked_dates_row.split(','))
        writer.writerow([''] + ['Cash','Cheque','Money Order'] * len(banked_dates) + ['','Cash','Cheque','Money Order','Banked(Cash,Money Order,Cheque)'])

        for code in banked_oracle_codes:
            banked_item_str = ''
            banked_item_str += '{},'.format(code)
            cash_total = D('0.0')
            cheque_total = D('0.0')
            moneyorder_total = D('0.0')
            for d in banked_oracle_codes[code]:
                banked_item_str += '{},{},{},'.format(d['amounts']['cash'],d['amounts']['cheque'],d['amounts']['money_order'])
                cash_total += d['amounts']['cash']
                cheque_total += d['amounts']['cheque']
                moneyorder_total += d['amounts']['money_order']
            banked_item_str += ',{},{},{},'.format(cash_total, cheque_total, moneyorder_total)
            if not ((cash_total == D('0.0')) and (cheque_total == D('0.0')) and (moneyorder_total == D('0.0'))):
                writer.writerow(banked_item_str.split(','))

        banked_total_str = 'Totals,'
        banked_total_amounts = {
            'cash': D('0.0'),
            'cheque': D('0.0'),
            'money_order': D('0.0')
        }
        for d in banked_date_amounts:
            banked_total_amounts['cash'] += d['amounts']['cash']
            banked_total_amounts['cheque'] += d['amounts']['cheque']
            banked_total_amounts['money_order'] += d['amounts']['money_order']
            banked_total_str += '{},{},{},'.format(d['amounts']['cash'],d['amounts']['cheque'],d['amounts']['money_order'])
        banked_total_str += ',{},{},{},'.format(banked_total_amounts['cash'],banked_total_amounts['cheque'],banked_total_amounts['money_order'])
        writer.writerow('')
        writer.writerow(banked_total_str.split(','))

        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #36
0
            self.assertEqual(UserAgent, "{0} delfick_app_tests/0.1".format(original))

    describe "mainline":
        it "catches DelfickError errors and prints them nicely":
            fle = StringIO()
            class MyApp(App):
                def execute(slf, args_obj, args_dict, extra_args, handler):
                    raise DelfickError("Well this should work", blah=1, _errors=[DelfickError("SubError", meh=2), DelfickError("SubError2", stuff=3)])

            try:
                MyApp().mainline([], print_errors_to=fle)
                assert False, "This should have failed"
            except SystemExit as error:
                self.assertEqual(error.code, 1)

            fle.flush()
            fle.seek(0)
            self.assertEqual(fle.read(), dedent("""
                !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                Something went wrong! -- DelfickError
                \t"Well this should work"\tblah=1
                errors:
                =======

                \t"SubError"\tmeh=2
                -------
                \t"SubError2"\tstuff=3
                -------
            """))

        it "Converts KeyboardInterrupt into a UserQuit":
Beispiel #37
0
def generate_trans_csv(system,start,end,region=None,district=None):
    # Get invoices matching the system and date range
    strIO = None
    invoices = []

    cash = None
    bpoint = None
    bpay = None

    # Get all transactions
    cash = CashTransaction.objects.filter(created__gte=start, created__lte=end,district=district,invoice__system=system).order_by('-created')
    bpoint = BpointTransaction.objects.filter(settlement_date__gte=start, settlement_date__lte=end,crn1__startswith=system).order_by('-created').exclude(crn1__endswith='_test')
    bpay = BpayTransaction.objects.filter(p_date__gte=start, p_date__lte=end,crn__startswith=system).order_by('-created')

    # Print the header
    strIO = StringIO()
    fieldnames = ['Created','Settlement Date', 'Payment Method', 'Transaction Type', 'Amount', 'Approved', 'Source', 'Product Names',
                  'Product Codes', 'Invoice']
    writer = csv.DictWriter(strIO, fieldnames=fieldnames)
    writer.writeheader()

    # Iterate through transactions
    for c in cash:
        i = c.invoice
        items = item_codes = None
        item_names = []
        oracle_codes = []
        # Get all items for this invoice
        if i.order:
            items = i.order.lines.all().values('title', 'oracle_code')
            for item in items:
                item_names.append(item.get('title'))
                code = item.get('oracle_code')
                if not code: code = 'N\A'
                oracle_codes.append(code)
            item_names = '|'.join(item_names)
            oracle_codes = '|'.join(oracle_codes)

        
        if c.type not in ['move_in','move_out']:
            cash_info = {
                'Created': c.created.astimezone(PERTH_TIMEZONE).strftime('%d/%m/%Y %H:%M:%S'),
                'Settlement Date': c.created.strftime('%d/%m/%Y'),
                'Invoice': c.invoice.reference,
                'Payment Method': 'Cash',
                'Transaction Type': c.type.lower(),
                'Amount': c.amount if c.type not in ['refund','move_out'] else '-{}'.format(c.amount),
                'Approved': 'True',
                'Source': c.source,
                'Product Names': item_names,
                'Product Codes': oracle_codes
            }
            writer.writerow(cash_info)
    if not district:
        # Write out all bpay transactions
        if bpay:
            for b in bpay:
                i = Invoice.objects.get(reference=b.crn)
                items = item_codes = None
                item_names = []
                oracle_codes = []
                # Get all items for this invoice
                if i.order:
                    items = i.order.lines.all().values('title', 'oracle_code')
                    for item in items:
                        item_names.append(item.get('title'))
                        code = item.get('oracle_code')
                        if not code: code = 'N\A'
                        oracle_codes.append(code)
                    item_names = '|'.join(item_names)
                    oracle_codes = '|'.join(oracle_codes)

                bpay_info = {
                    'Created': b.created.astimezone(PERTH_TIMEZONE).strftime('%d/%m/%Y %H:%M:%S'),
                    'Settlement Date': b.p_date.strftime('%d/%m/%Y'),
                    'Invoice': b.crn,
                    'Payment Method': 'BPAY',
                    'Transaction Type': b.get_p_instruction_code_display(),
                    'Amount': b.amount if b.p_instruction_code == '05' and b.type == '399' else '-{}'.format(b.amount),
                    'Approved': b.approved,
                    'Source': 'N/A',
                    'Product Names': item_names,
                    'Product Codes': oracle_codes
                }
                writer.writerow(bpay_info)
        # Write out all bpoint transactions
        if bpoint:
            for bpt in bpoint:
                i = Invoice.objects.get(reference=bpt.crn1)
                items = item_codes = None
                item_names = []
                oracle_codes = []
                # Get all items for this invoice
                if i.order:
                    items = i.order.lines.all().values('title', 'oracle_code')
                    for item in items:
                        item_names.append(item.get('title'))
                        code = item.get('oracle_code')
                        if not code: code = 'N\A'
                        oracle_codes.append(code)
                    item_names = '|'.join(item_names)
                    oracle_codes = '|'.join(oracle_codes)

                bpoint_info = {
                    'Created': bpt.created.astimezone(PERTH_TIMEZONE).strftime('%d/%m/%Y %H:%M:%S'),
                    'Settlement Date': bpt.settlement_date.strftime('%d/%m/%Y'),
                    'Invoice': bpt.crn1,
                    'Payment Method': 'BPOINT',
                    'Transaction Type': bpt.action.lower(),
                    'Amount': bpt.amount if bpt.action not in ['refund'] else '-{}'.format(bpt.amount),
                    'Approved': bpt.approved,
                    'Source': 'N/A',
                    'Product Names': item_names,
                    'Product Codes': oracle_codes
                }
                writer.writerow(bpoint_info)

    strIO.flush()
    strIO.seek(0)
    return strIO
Beispiel #38
0
def debugprint(obj, depth=-1, print_type=False,
               file=None, ids='CHAR', stop_on_name=False,
               done=None):
    """Print a computation graph as text to stdout or a file.

    :type obj: Variable, Apply, or Function instance
    :param obj: symbolic thing to print
    :type depth: integer
    :param depth: print graph to this depth (-1 for unlimited)
    :type print_type: boolean
    :param print_type: whether to print the type of printed objects
    :type file: None, 'str', or file-like object
    :param file: print to this file ('str' means to return a string)
    :type ids: str
    :param ids: How do we print the identifier of the variable
                id - print the python id value
                int - print integer character
                CHAR - print capital character
                "" - don't print an identifier
    :param stop_on_name: When True, if a node in the graph has a name,
                         we don't print anything below it.
    :type done: None or dict
    :param done: A dict where we store the ids of printed node.
        Useful to have multiple call to debugprint share the same ids.

    :returns: string if `file` == 'str', else file arg

    Each line printed represents a Variable in the graph.
    The indentation of lines corresponds to its depth in the symbolic graph.
    The first part of the text identifies whether it is an input
    (if a name or type is printed) or the output of some Apply (in which case
    the Op is printed).
    The second part of the text is an identifier of the Variable.
    If print_type is True, we add a part containing the type of the Variable

    If a Variable is encountered multiple times in the depth-first search,
    it is only printed recursively the first time. Later, just the Variable
    identifier is printed.

    If an Apply has multiple outputs, then a '.N' suffix will be appended
    to the Apply's identifier, to indicate which output a line corresponds to.

    """
    if not isinstance(depth, int):
        raise Exception("depth parameter must be an int")
    if file == 'str':
        _file = StringIO()
    elif file is None:
        _file = sys.stdout
    else:
        _file = file
    if done is None:
        done = dict()
    results_to_print = []
    profile_list = []
    order = []
    if isinstance(obj, (list, tuple)):
        lobj = obj
    else:
        lobj = [obj]
    for obj in lobj:
        if isinstance(obj, gof.Variable):
            results_to_print.append(obj)
            profile_list.append(None)
        elif isinstance(obj, gof.Apply):
            results_to_print.extend(obj.outputs)
            profile_list.extend([None for item in obj.outputs])
        elif isinstance(obj, Function):
            results_to_print.extend(obj.maker.fgraph.outputs)
            profile_list.extend(
                [obj.profile for item in obj.maker.fgraph.outputs])
            order = obj.maker.fgraph.toposort()
        elif isinstance(obj, gof.FunctionGraph):
            results_to_print.extend(obj.outputs)
            profile_list.extend([None for item in obj.outputs])
            order = obj.toposort()
        elif isinstance(obj, (integer_types, float, np.ndarray)):
            print(obj)
        elif isinstance(obj, (theano.In, theano.Out)):
            results_to_print.append(obj.variable)
            profile_list.append(None)
        else:
            raise TypeError("debugprint cannot print an object of this type",
                            obj)

    scan_ops = []
    for r, p in zip(results_to_print, profile_list):
        # Add the parent scan op to the list as well
        if (hasattr(r.owner, 'op') and
                isinstance(r.owner.op, theano.scan_module.scan_op.Scan)):
                    scan_ops.append(r)

        if p is not None:
            print("""
Timing Info
-----------
--> <time> <% time> - <total time> <% total time>'

<time>         computation time for this node
<% time>       fraction of total computation time for this node
<total time>   time for this node + total times for this node's ancestors
<% total time> total time for this node over total computation time

N.B.:
* Times include the node time and the function overhead.
* <total time> and <% total time> may over-count computation times
  if inputs to a node share a common ancestor and should be viewed as a
  loose upper bound. Their intended use is to help rule out potential nodes
  to remove when optimizing a graph because their <total time> is very low.
""", file=_file)

        debugmode.debugprint(r, depth=depth, done=done, print_type=print_type,
                             file=_file, order=order, ids=ids,
                             scan_ops=scan_ops, stop_on_name=stop_on_name,
                             profile=p)

    if len(scan_ops) > 0:
        print("", file=_file)
        new_prefix = ' >'
        new_prefix_child = ' >'
        print("Inner graphs of the scan ops:", file=_file)

        for s in scan_ops:
            # prepare a dict which maps the scan op's inner inputs
            # to its outer inputs.
            if hasattr(s.owner.op, 'fn'):
                # If the op was compiled, print the optimized version.
                inner_inputs = s.owner.op.fn.maker.fgraph.inputs
            else:
                inner_inputs = s.owner.op.inputs
            outer_inputs = s.owner.inputs
            inner_to_outer_inputs = \
                dict([(inner_inputs[i], outer_inputs[o])
                      for i, o in
                      s.owner.op.var_mappings['outer_inp_from_inner_inp']
                      .items()])

            print("", file=_file)
            debugmode.debugprint(
                s, depth=depth, done=done,
                print_type=print_type,
                file=_file, ids=ids,
                scan_ops=scan_ops,
                stop_on_name=stop_on_name,
                scan_inner_to_outer_inputs=inner_to_outer_inputs)
            if hasattr(s.owner.op, 'fn'):
                # If the op was compiled, print the optimized version.
                outputs = s.owner.op.fn.maker.fgraph.outputs
            else:
                outputs = s.owner.op.outputs
            for idx, i in enumerate(outputs):

                if hasattr(i, 'owner') and hasattr(i.owner, 'op'):
                    if isinstance(i.owner.op, theano.scan_module.scan_op.Scan):
                        scan_ops.append(i)

                debugmode.debugprint(
                    r=i, prefix=new_prefix,
                    depth=depth, done=done,
                    print_type=print_type, file=_file,
                    ids=ids, stop_on_name=stop_on_name,
                    prefix_child=new_prefix_child,
                    scan_ops=scan_ops,
                    scan_inner_to_outer_inputs=inner_to_outer_inputs)

    if file is _file:
        return file
    elif file == 'str':
        return _file.getvalue()
    else:
        _file.flush()
Beispiel #39
0
def generate_items_csv(system,
                       start,
                       end,
                       banked_start,
                       banked_end,
                       region=None,
                       district=None):
    strIO = None
    invoices = Invoice.objects.filter(system=system)
    dates, banked_dates = [], []
    date_amounts, banked_date_amounts = [], []
    items = []
    oracle_codes = {}
    banked_oracle_codes = {}
    date_format = '%A %d/%m/%y'

    eftpos = []
    banked_cash = []
    bpoint = []
    bpay = []

    if invoices:
        strIO = StringIO()
        fieldnames = ['Account Code', 'Day']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for d in daterange(start, end):
            dates.append(d.strftime(date_format))
            date_amounts.append({
                'date': d.strftime(date_format),
                'amounts': {
                    'card': D('0.0'),
                    'bpay': D('0.0'),
                    'eftpos': D('0.0'),
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })
        for d in daterange(banked_start, banked_end):
            banked_dates.append(d.strftime(date_format))
            banked_date_amounts.append({
                'date': d.strftime(date_format),
                'amounts': {
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })

        dates_row = ''
        for date in dates:
            dates_row += '{},,,'.format(date)

        # Dates row
        writer.writerow([''] + dates_row.split(','))
        writer.writerow([''] + ['Credit Card', 'Bpay', 'EFTPOS'] * len(dates) +
                        ['', 'Credit Card', 'Bpay', 'EFTPOS'])

        for i in invoices:
            # Add items of invoice if not in list
            if i.order:
                for x in i.order.lines.all():
                    #print((i, i.__dict__, x, x.oracle_code))
                    item_date_amounts, banked_item_dates_amounts = [], []
                    for d in dates:
                        item_date_amounts.append({
                            'date': d,
                            'amounts': {
                                'card': D('0.0'),
                                'bpay': D('0.0'),
                                'eftpos': D('0.0'),
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })
                    for d in banked_dates:
                        banked_item_dates_amounts.append({
                            'date': d,
                            'amounts': {
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })

                    items.append({
                        'dates': item_date_amounts,
                        'banked_dates': banked_item_dates_amounts,
                        'item': x,
                        'card': D('0.0'),
                        'bpay': D('0.0'),
                        'eftpos': D('0.0'),
                        'cash': D('0.0'),
                        'cheque': D('0.0'),
                        'money_order': D('0.0')
                    })

                    # create empty subtotal list for each oracle code
                    if x.oracle_code not in oracle_codes:
                        oracle_codes[x.oracle_code] = []
                        for d in daterange(start, end):
                            oracle_codes[x.oracle_code].append({
                                'date':
                                d.strftime(date_format),
                                'amounts': {
                                    'card': D('0.0'),
                                    'bpay': D('0.0'),
                                    'eftpos': D('0.0'),
                                    'cash': D('0.0'),
                                    'cheque': D('0.0'),
                                    'money_order': D('0.0')
                                }
                            })

                    if x.oracle_code not in banked_oracle_codes:
                        banked_oracle_codes[x.oracle_code] = []
                        for d in daterange(banked_start, banked_end):
                            banked_oracle_codes[x.oracle_code].append({
                                'date':
                                d.strftime(date_format),
                                'amounts': {
                                    'cash': D('0.0'),
                                    'cheque': D('0.0'),
                                    'money_order': D('0.0')
                                }
                            })

            # Get all transactions
            if not district:
                eftpos.extend([
                    x for x in i.cash_transactions.filter(
                        created__gte=start, created__lte=end,
                        source='eftpos').exclude(district__isnull=False)
                ])
                banked_cash.extend([
                    x for x in i.cash_transactions.filter(
                        created__gte=banked_start, created__lte=banked_end).
                    exclude(source='eftpos').exclude(district__isnull=False)
                ])
                bpoint.extend([
                    x for x in i.bpoint_transactions.filter(created__gte=start,
                                                            created__lte=end)
                ])
                bpay.extend([
                    x for x in i.bpay_transactions.filter(p_date__gte=start,
                                                          p_date__lte=end)
                ])
            else:
                eftpos.extend([
                    x for x in i.cash_transactions.filter(created__gte=start,
                                                          created__lte=end,
                                                          source='eftpos',
                                                          district=district)
                ])
                banked_cash.extend([
                    x for x in i.cash_transactions.filter(
                        created__gte=banked_start,
                        created__lte=banked_end,
                        district=district).exclude(source='eftpos')
                ])
        # Go through items

        for item in items:
            price = D(item.get('item').line_price_before_discounts_incl_tax)
            code = item.get('item').oracle_code
            order = item.get('item').order

            # Banked Cash
            for d in item['banked_dates']:
                index = 0
                for l in banked_date_amounts:
                    if l.get('date') == d.get('date'):
                        date_amount_index = index
                        break
                    index += 1
                for c in banked_cash:
                    for s in CashTransaction.SOURCE_TYPES:
                        source = str(s[0])
                        if c.source == source and c.invoice.order == order:
                            if c.created.strftime(date_format) == d.get(
                                    'date'):
                                if c.type == 'payment':
                                    banked_oracle_codes[code][
                                        date_amount_index]['amounts'][
                                            source] += price
                                    item[source] += price
                                    banked_date_amounts[date_amount_index][
                                        'amounts'][source] += price
                                elif c.type in ('refund', 'reversal'):
                                    banked_oracle_codes[code][
                                        date_amount_index]['amounts'][
                                            source] -= price
                                    item[source] -= price
                                    banked_date_amounts[date_amount_index][
                                        'amounts'][source] -= price
            # Other transactions
            for d in oracle_codes[code]:
                index = 0
                for l in date_amounts:
                    if l.get('date') == d.get('date'):
                        date_amount_index = index
                        break
                    index += 1
                # EFT
                for c in eftpos:
                    if c.created.strftime(date_format) == d.get(
                            'date') and c.invoice.order == order:
                        if c.type == 'payment':
                            oracle_codes[code][date_amount_index]['amounts'][
                                'eftpos'] += price
                            item['eftpos'] += price
                            date_amounts[date_amount_index]['amounts'][
                                'eftpos'] += price
                        elif c.type in ('refund', 'reversal'):
                            oracle_codes[code][date_amount_index]['amounts'][
                                'eftpos'] -= price
                            item['eftpos'] -= price
                            date_amounts[date_amount_index]['amounts'][
                                'eftpos'] -= price

                # Card
                for c in bpoint:
                    if c.approved and c.order == order:
                        if c.created.strftime(date_format) == d.get('date'):
                            #print(('CC', price, code, c, c.__dict__, d))
                            if c.action in ('payment', 'capture'):
                                oracle_codes[code][date_amount_index][
                                    'amounts']['card'] += price
                                item['card'] += price
                                date_amounts[date_amount_index]['amounts'][
                                    'card'] += price
                            elif c.action in ('refund', 'reversal'):
                                oracle_codes[code][date_amount_index][
                                    'amounts']['card'] -= price
                                item['card'] -= price
                                date_amounts[date_amount_index]['amounts'][
                                    'card'] -= price
                # BPAY
                for b in bpay:
                    if b.approved and b.order == order:
                        if b.p_date.strftime(date_format) == d.get('date'):
                            if b.p_instruction_code == '05' and b.type == '399':
                                oracle_codes[code][date_amount_index][
                                    'amounts']['bpay'] += price
                                item['bpay'] += price
                                date_amounts[date_amount_index]['amounts'][
                                    'bpay'] += price
                            elif b.p_instruction_code == '25' and b.type == '699':
                                oracle_codes[code][date_amount_index][
                                    'amounts']['bpay'] -= price
                                item['bpay'] -= price
                                date_amounts[date_amount_index]['amounts'][
                                    'bpay'] -= price

        for code in oracle_codes:
            item_str = ''
            item_str += '{},'.format(code)
            card_total = D('0.0')
            bpay_total = D('0.0')
            eftpos_total = D('0.0')
            for d in oracle_codes[code]:
                item_str += '{},{},{},'.format(d['amounts']['card'],
                                               d['amounts']['bpay'],
                                               d['amounts']['eftpos'])
                card_total += d['amounts']['card']
                bpay_total += d['amounts']['bpay']
                eftpos_total += d['amounts']['eftpos']
            item_str += ',{},{},{},'.format(card_total, bpay_total,
                                            eftpos_total)
            if not ((card_total == D('0.0')) and (bpay_total == D('0.0')) and
                    (eftpos_total == D('0.0'))):
                writer.writerow(item_str.split(','))

        total_str = 'Totals,'
        total_amounts = {
            'card': D('0.0'),
            'bpay': D('0.0'),
            'eftpos': D('0.0')
        }
        for d in date_amounts:
            total_amounts['card'] += d['amounts']['card']
            total_amounts['bpay'] += d['amounts']['bpay']
            total_amounts['eftpos'] += d['amounts']['eftpos']
            total_str += '{},{},{},'.format(d['amounts']['card'],
                                            d['amounts']['bpay'],
                                            d['amounts']['eftpos'])
        total_str += ',{},{},{},'.format(total_amounts['card'],
                                         total_amounts['bpay'],
                                         total_amounts['eftpos'])
        writer.writerow('')
        writer.writerow(total_str.split(','))

        # Banked Items
        writer.writerow('')
        writer.writerow(fieldnames)
        banked_dates_row = ''
        for date in banked_dates:
            banked_dates_row += '{},,,'.format(date)
        writer.writerow([''] + banked_dates_row.split(','))
        writer.writerow([''] +
                        ['Cash', 'Cheque', 'Money Order'] * len(banked_dates) +
                        [
                            '', 'Cash', 'Cheque', 'Money Order',
                            'Banked(Cash,Money Order,Cheque)'
                        ])

        for code in banked_oracle_codes:
            banked_item_str = ''
            banked_item_str += '{},'.format(code)
            cash_total = D('0.0')
            cheque_total = D('0.0')
            moneyorder_total = D('0.0')
            for d in banked_oracle_codes[code]:
                banked_item_str += '{},{},{},'.format(
                    d['amounts']['cash'], d['amounts']['cheque'],
                    d['amounts']['money_order'])
                cash_total += d['amounts']['cash']
                cheque_total += d['amounts']['cheque']
                moneyorder_total += d['amounts']['money_order']
            banked_item_str += ',{},{},{},'.format(cash_total, cheque_total,
                                                   moneyorder_total)
            if not ((cash_total == D('0.0')) and (cheque_total == D('0.0')) and
                    (moneyorder_total == D('0.0'))):
                writer.writerow(banked_item_str.split(','))

        banked_total_str = 'Totals,'
        banked_total_amounts = {
            'cash': D('0.0'),
            'cheque': D('0.0'),
            'money_order': D('0.0')
        }
        for d in banked_date_amounts:
            banked_total_amounts['cash'] += d['amounts']['cash']
            banked_total_amounts['cheque'] += d['amounts']['cheque']
            banked_total_amounts['money_order'] += d['amounts']['money_order']
            banked_total_str += '{},{},{},'.format(d['amounts']['cash'],
                                                   d['amounts']['cheque'],
                                                   d['amounts']['money_order'])
        banked_total_str += ',{},{},{},'.format(
            banked_total_amounts['cash'], banked_total_amounts['cheque'],
            banked_total_amounts['money_order'])
        writer.writerow('')
        writer.writerow(banked_total_str.split(','))

        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #40
0
def generate_items_csv_allocated(system,start,end,banked_start,banked_end,region=None,district=None):
    strIO = None
    invoices = []
    invoice_list = []
    dates, banked_dates = [], []
    date_amounts, banked_date_amounts = [], []
    items = []
    oracle_codes = {}
    banked_oracle_codes = {}
    date_format = '%d/%m/%y'

    eftpos = []
    banked_cash = []
    bpoint = []
    bpay = []

#    # Get all transactions
#    if not district:
#        eftpos.extend([x for x in CashTransaction.objects.filter(created__gte=start, created__lte=end, source='eftpos').exclude(district__isnull=False)])
#        banked_cash.extend([x for x in CashTransaction.objects.filter(created__gte=banked_start, created__lte=banked_end).exclude(source='eftpos').exclude(district__isnull=False)])
#        bpoint.extend([x for x in BpointTransaction.objects.filter(settlement_date__gte=start, settlement_date__lte=end).exclude(crn1__endswith='_test')])
#        bpay.extend([x for x in BpayTransaction.objects.filter(p_date__gte=start, p_date__lte=end)])
#    else:
#        eftpos.extend([x for x in CashTransaction.objects.filter(created__gte=start, created__lte=end, source='eftpos',district=district)])
#        banked_cash.extend([x for x in CashTransaction.objects.filter(created__gte=banked_start, created__lte=banked_end,district=district).exclude(source='eftpos')])
#
#    # Get the required invoices
#    for e in eftpos:
#        if e.invoice.reference not in invoice_list:
#            if e.invoice.system == system:
#                invoices.append(e.invoice)
#                invoice_list.append(str(e.invoice.reference))
#    for b in banked_cash:
#        if b.invoice.reference not in invoice_list:
#            if b.invoice.system == system:
#                invoices.append(b.invoice)
#                invoice_list.append(str(b.invoice.reference))
#    for b in bpoint:
#        if b.crn1 not in invoice_list:
#            invoice = Invoice.objects.get(reference=b.crn1)
#            if invoice.system == system:
#                invoices.append(invoice)
#                invoice_list.append(str(b.crn1))
#    for b in bpay:
#        if b.crn not in invoice_list:
#            invoice = Invoice.objects.get(reference=b.crn)
#            if invoice.system == system:
#                invoices.append(invoice)
#                invoice_list.append(str(b.crn))
#
    print (str(banked_start)+ ":"+str(banked_end)+":"+str(system))
    invoices = Invoice.objects.filter(created__gte=banked_start, created__lte=banked_end, system=system)

    print ("INVOICES")
    print (invoices)
    invoice_list = invoices
    invoice_list = list(set(invoice_list))
    if invoices:
        strIO = StringIO()
        fieldnames = ['Account Code', 'Day']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        ########################################################
        ## Header Dates START ##
        ########################################################
        for d in daterange(start,end):
            dates.append(d.strftime(date_format))
            date_amounts.append({
                'date':d.strftime(date_format),
                'amounts':{
                    'card': D('0.0'),
                    'bpay': D('0.0'),
                    'eftpos': D('0.0'),
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0'),
                    'order': D('0.0'), 
                }
            })

        for d in daterange(banked_start,banked_end):
            banked_dates.append(d.strftime(date_format))
            banked_date_amounts.append({
                'date':d.strftime(date_format),
                'amounts':{
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0'),
                    'order': D('0.0'),
                }
            })

        dates_row = ''
        for date in dates:
            dates_row += '{},,,,'.format(date)

        # Dates row
        writer.writerow(['']+ dates_row.split(','))
        writer.writerow([''] + ['Credit Card','Bpay','EFTPOS','Order'] * len(dates) + ['','Credit Card','Bpay','EFTPOS','Order'])
        ########################################################
        #### Header Dates END ####
        ########################################################       

        ####################################################################################################
        ### Oracle Code Grouping Per Day Per Amount START (Electronic Payments, Credit Card,EFTpos Bpay,) ##
        ####################################################################################################

        # Loop through the payments and not the invoices
        for i in invoices:
            # Add items of invoice if not in list
            if i.order:# and not i.voided:
                lines = i.order.lines.all()
                for x in lines:
                    #print((i, i.__dict__, x, x.oracle_code))
                    item_date_amounts, banked_item_dates_amounts = [], []
                    for d in dates:
                        item_date_amounts.append({
                            'date':d,
                            'amounts':{
                                'card': D('0.0'),
                                'bpay': D('0.0'),
                                'eftpos': D('0.0'),
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0'),
                                'order' : D('0.0'),
                            }
                        })

                    for d in banked_dates:
                        banked_item_dates_amounts.append({
                            'date':d,
                            'amounts':{
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })

                    items.append({
                        'dates':item_date_amounts,
                        'banked_dates': banked_item_dates_amounts,
                        'item': x,
                        'card': D('0.0'),
                        'bpay': D('0.0'),
                        'eftpos': D('0.0'),
                        'cash': D('0.0'),
                        'cheque': D('0.0'),
                        'money_order': D('0.0'),
                        'order' : D('0.0'),
                    })

                    
                    # create empty subtotal list for each oracle code
                    if x.oracle_code not in oracle_codes:
                        oracle_codes[x.oracle_code] = []
                        for d in daterange(start,end):
                            oracle_codes[x.oracle_code].append({
                                'date':d.strftime(date_format),
                                'amounts':{
                                    'card': D('0.0'),
                                    'bpay': D('0.0'),
                                    'eftpos': D('0.0'),
                                    'cash': D('0.0'),
                                    'cheque': D('0.0'),
                                    'money_order': D('0.0'),
                                    'order': D('0.0')
                                }
                            })
                    
                    if x.oracle_code not in banked_oracle_codes:
                        banked_oracle_codes[x.oracle_code] = []
                        for d in daterange(banked_start,banked_end):
                            banked_oracle_codes[x.oracle_code].append({
                                'date':d.strftime(date_format),
                                'amounts':{
                                    'cash': D('0.0'),
                                    'cheque': D('0.0'),
                                    'money_order': D('0.0'),
                                    'order' : D('0.0')
                                }
                            })


        for item in items:
            price = D(item.get('item').line_price_before_discounts_incl_tax)
            code = item.get('item').oracle_code
            #order = item.get('item').order
            date_placed = item.get('item').order.date_placed
            payment_details = item.get('item').payment_details
            refund_details = item.get('item').refund_details
            deduction_details = item.get('item').deduction_details

            # Banked Cash
            for d in item['banked_dates']:
                index = 0
                for l in banked_date_amounts:
                    if l.get('date') == d.get('date'):
                        date_amount_index = index
                        break
                    index += 1
                for k,v in payment_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source in set(['cash','cheque','money_order','order']):
                        if c.created.strftime(date_format) == d.get('date'):
                            banked_oracle_codes[code][date_amount_index]['amounts'][source] += D(v)
                            item[source] += D(v)
                            banked_date_amounts[date_amount_index]['amounts'][source] += D(v)

                for k,v in refund_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source in set(['cash','cheque','money_order','order']):
                        if c.created.strftime(date_format) == d.get('date'):
                            banked_oracle_codes[code][date_amount_index]['amounts'][source] -= D(v)
                            item[source] -= D(v)
                            banked_date_amounts[date_amount_index]['amounts'][source] -= D(v)

                for k,v in deduction_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source in set(['cash','cheque','money_order','order']):
                        if c.created.strftime(date_format) == d.get('date'):
                            banked_oracle_codes[code][date_amount_index]['amounts'][source] -= D(v)
                            item[source] -= D(v)
                            banked_date_amounts[date_amount_index]['amounts'][source] -= D(v)

            # Other transactions
            for d in oracle_codes[code]:
                index = 0
                for l in date_amounts:
                    if l.get('date') == d.get('date'):
                        date_amount_index = index
                        break
                    index += 1
                for k,v in payment_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source == 'eftpos':
                        if c.created.strftime(date_format) == d.get('date'):
                            oracle_codes[code][date_amount_index]['amounts']['eftpos'] += D(v)
                            item['eftpos'] += D(v)
                            date_amounts[date_amount_index]['amounts']['eftpos'] += D(v)

                for k,v in refund_details["cash"].items():
                    c = CashTransaction.objects.get(id=int(k))
                    source = c.source
                    if source == 'eftpos':
                        if c.created.strftime(date_format) == d.get('date'):
                            oracle_codes[code][date_amount_index]['amounts']['eftpos'] -= D(v)
                            item['eftpos'] -= D(v)
                            date_amounts[date_amount_index]['amounts']['eftpos'] -= D(v)

#                # Card
#                for k,v in payment_details['card'].items():
#                    c = BpointTransaction.objects.get(id=int(k))
#                    if c.settlement_date.strftime(date_format) == d.get('date') and c.approved:
#                        oracle_codes[code][date_amount_index]['amounts']['card'] += D(v)
#                        item['card'] += D(v)
#                        date_amounts[date_amount_index]['amounts']['card'] += D(v)
#
#                for k,v in refund_details['card'].items():
#                    c = BpointTransaction.objects.get(id=int(k))
#                    if c.settlement_date.strftime(date_format) == d.get('date') and c.approved:
#                        oracle_codes[code][date_amount_index]['amounts']['card'] -= D(v)
#                        item['card'] -= D(v)
#                        date_amounts[date_amount_index]['amounts']['card'] -= D(v)
#
#                # BPAY
#                for k,v in payment_details['bpay'].items():
#                    b = BpayTransaction.objects.get(id=int(k))
#                    if b.approved and b.p_date.strftime(date_format) == d.get('date'):
#                        oracle_codes[code][date_amount_index]['amounts']['bpay'] += D(v)
#                        item['bpay'] += D(v)
#                        date_amounts[date_amount_index]['amounts']['bpay'] += D(v)
#
#                for k,v in refund_details['bpay'].items():
#                    b = BpayTransaction.objects.get(id=int(k))
#                    if b.approved and b.p_date.strftime(date_format) == d.get('date'):
#                        oracle_codes[code][date_amount_index]['amounts']['bpay'] -= D(v)
#                        item['bpay'] -= D(v)
#                        date_amounts[date_amount_index]['amounts']['bpay'] -= D(v)
#

                # Order
                if d.get('date') == date_placed.strftime(date_format):
                    if 'order' in payment_details:
                        print ("ORDER")
                        print (payment_details['order'])
                        for k,v in payment_details['order'].items():
                            #c = BpointTransaction.objects.get(id=int(k))
                            #if c.settlement_date.strftime(date_format) == d.get('date') and c.approved:
                            
                            oracle_codes[code][date_amount_index]['amounts']['order'] += D(v)
                            item['order'] += D(v)
                            date_amounts[date_amount_index]['amounts']['order'] += D(v)
    
                    if 'order' in refund_details: 
                        for k,v in refund_details['order'].items():
                            #c = BpointTransaction.objects.get(id=int(k))
                            #if c.settlement_date.strftime(date_format) == d.get('date') and c.approved:
                            oracle_codes[code][date_amount_index]['amounts']['order'] -= D(v)
                            item['order'] -= D(v)
                            date_amounts[date_amount_index]['amounts']['order'] -= D(v)
    
        for code in oracle_codes:
            item_str = ''
            item_str += '{},'.format(code)
            card_total = D('0.0')
            bpay_total = D('0.0')
            eftpos_total = D('0.0')
            order_total = D('0.0')
            for d in oracle_codes[code]:
                item_str += '{},{},{},{},'.format(d['amounts']['card'],d['amounts']['bpay'],d['amounts']['eftpos'],d['amounts']['order'])
                card_total += d['amounts']['card']
                bpay_total += d['amounts']['bpay']
                eftpos_total += d['amounts']['eftpos']
                order_total += d['amounts']['order']

            item_str += ',{},{},{},{},'.format(card_total, bpay_total, eftpos_total, order_total)
            #if not ((card_total == D('0.0')) and (bpay_total == D('0.0')) and (eftpos_total == D('0.0')) and (order_total == D('0.0'))):
            writer.writerow(item_str.split(','))


        ##################################################################################################
        ### Oracle Code Grouping Per Day Per Amount END (Electronic Payments, Credit Card,EFTpos Bpay,) ## 
        ##################################################################################################


        ##############################################################################################
        ### Oracle Code Grouping Per Day Per Amount END (Manual Payments,  Cash Cheque, Money Order ##
        ##############################################################################################




        total_str = 'Totals,'
        total_amounts = {
            'card': D('0.0'),
            'bpay': D('0.0'),
            'eftpos': D('0.0'),
            'order': D('0.0'),
        }
        for d in date_amounts:
            total_amounts['card'] += d['amounts']['card']
            total_amounts['bpay'] += d['amounts']['bpay']
            total_amounts['eftpos'] += d['amounts']['eftpos']
            total_amounts['order'] += d['amounts']['order']

            total_str += '{},{},{},{},'.format(d['amounts']['card'],d['amounts']['bpay'],d['amounts']['eftpos'],d['amounts']['order'])
        total_str += ',{},{},{},{},'.format(total_amounts['card'],total_amounts['bpay'],total_amounts['eftpos'], total_amounts['order'])
        writer.writerow('')
        writer.writerow(total_str.split(','))
        # Banked Items
        writer.writerow('')
        writer.writerow(fieldnames)
        banked_dates_row = ''
        for date in banked_dates:
            banked_dates_row += '{},,,'.format(date)
        writer.writerow(['']+ banked_dates_row.split(','))
        writer.writerow([''] + ['Cash','Cheque','Money Order'] * len(banked_dates) + ['','Cash','Cheque','Money Order','Banked(Cash,Money Order,Cheque)'])

        for code in banked_oracle_codes:
            banked_item_str = ''
            banked_item_str += '{},'.format(code)
            cash_total = D('0.0')
            cheque_total = D('0.0')
            moneyorder_total = D('0.0')
            order_total = D('0.0')
 
            for d in banked_oracle_codes[code]:
                banked_item_str += '{},{},{},{},'.format(d['amounts']['cash'],d['amounts']['cheque'],d['amounts']['money_order'], d['amounts']['order'])
                cash_total += d['amounts']['cash']
                cheque_total += d['amounts']['cheque']
                moneyorder_total += d['amounts']['money_order']
                order_total +=  d['amounts']['order']
            banked_item_str += ',{},{},{},{},'.format(cash_total, cheque_total, moneyorder_total, order_total)
            if not ((cash_total == D('0.0')) and (cheque_total == D('0.0')) and (moneyorder_total == D('0.0')) and (order_total == D('0.0'))):
                writer.writerow(banked_item_str.split(','))

        banked_total_str = 'Totals,'
        banked_total_amounts = {
            'cash': D('0.0'),
            'cheque': D('0.0'),
            'money_order': D('0.0'),
            'order' : D('0.0'),
        }
        for d in banked_date_amounts:
            banked_total_amounts['cash'] += d['amounts']['cash']
            banked_total_amounts['cheque'] += d['amounts']['cheque']
            banked_total_amounts['money_order'] += d['amounts']['money_order']
            if 'order' in d['amounts']:
                banked_total_amounts['order'] += d['amounts']['order']
            else:
                banked_total_amounts['order'] = D('0.0')
                d['amounts']['order'] = D('0.0')
            banked_total_str += '{},{},{},{},'.format(d['amounts']['cash'],d['amounts']['cheque'],d['amounts']['money_order'],d['amounts']['order'])
        banked_total_str += ',{},{},{},{},'.format(banked_total_amounts['cash'],banked_total_amounts['cheque'],banked_total_amounts['money_order'],banked_total_amounts['order'])
        writer.writerow('')
        writer.writerow(banked_total_str.split(','))

        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #41
0
def booking_bpoint_settlement_report(_date):
    try:
        # Start writing the headers
        strIO = StringIO()
        fieldnames = [
            'Payment Date',
            'Settlement Date',
            'Confirmation Number',
            'Name',
            'Type',
            'Amount',
            'Invoice',
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

#        # Retrieve all the oracle codes used in this app
#        oracle_codes = []
#        for application_type in ApplicationType.objects.all():
#            oracle_codes.append(application_type.get_oracle_code_by_date())
#
#        # crn1 starts with one of the oracle codes retrieved
#        queries = Q()
#        for oracle_code in oracle_codes:
#            queries |= Q(crn1__startswith=oracle_code)

        bpoint = []
        bpoint.extend([x for x in BpointTransaction.objects.filter(
            Q(created__date=_date),
            Q(response_code=0),
            Q(crn1__startswith=PAYMENT_SYSTEM_PREFIX),
            # queries  # crn1__startswith='0517'
        ).exclude(crn1__endswith='_test')])

        for b in bpoint:
            dcv_permit_fee, dcv_admission_fee, app_type_dcv_permit, compliance = None, None, None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)

                try:
                    dcv_permit_fee = DcvPermitFee.objects.get(invoice_reference=invoice.reference)
                except DcvPermitFee.DoesNotExist:
                    pass

                try:
                    dcv_admission_fee = DcvAdmissionFee.objects.get(invoice_reference=invoice.reference)
                except DcvAdmissionFee.DoesNotExist:
                    pass

                try:
                    application_fee = ApplicationFee.objects.get(invoice_reference=invoice.reference)
                except ApplicationFee.DoesNotExist:
                    pass

                try:
                    compliance = Compliance.objects.get(fee_invoice_reference=invoice.reference)
                except Compliance.DoesNotExist:
                    pass

                if dcv_permit_fee:
                    b_name = u'{}'.format(dcv_permit_fee.created_by)
                    created = timezone.localtime(dcv_permit_fee.created, pytz.timezone(TIME_ZONE))
                    settlement_date = b.settlement_date.strftime('%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),
                                     settlement_date,
                                     dcv_permit_fee.dcv_permit.lodgement_number,
                                     b_name,
                                     invoice.get_payment_method_display(),
                                     invoice.amount,
                                     invoice.reference])
                elif dcv_admission_fee:
                    b_name = u'{}'.format(dcv_admission_fee.created_by)
                    created = timezone.localtime(dcv_admission_fee.created, pytz.timezone(TIME_ZONE))
                    settlement_date = b.settlement_date.strftime('%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),
                                     settlement_date,
                                     dcv_admission_fee.dcv_admission.lodgement_number,
                                     b_name,
                                     invoice.get_payment_method_display(),
                                     invoice.amount,
                                     invoice.reference])
                elif application_fee:
                    b_name = u'{}'.format(application_fee.proposal.applicant)
                    created = timezone.localtime(application_fee.created, pytz.timezone('Australia/Perth'))
                    settlement_date = b.settlement_date.strftime('%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),
                                     settlement_date,
                                     application_fee.proposal.lodgement_number,
                                     b_name,
                                     invoice.get_payment_method_display(),
                                     invoice.amount,
                                     invoice.reference])
                elif compliance:
                    submitter = compliance.approval.submitter if compliance.approval else compliance.proposal.submitter
                    b_name = u'{}'.format(submitter)
                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
                    settlement_date = b.settlement_date.strftime('%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),
                                     settlement_date,
                                     compliance.lodgement_number,
                                     b_name,
                                     invoice.get_payment_method_display(),
                                     invoice.amount,
                                     invoice.reference])
                else:
                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),
                                     b.settlement_date.strftime('%d/%m/%Y'),
                                     '',
                                     '',
                                     str(b.action),
                                     b.amount,
                                     invoice.reference])
            except Invoice.DoesNotExist:
                pass

#        bpay = []
#        bpay.extend([x for x in BpayTransaction.objects.filter(
#            p_date__date=_date,
#            crn__startswith='0517'
#        ).exclude(crn__endswith='_test')])
#
#        for b in bpay:
#            booking, invoice = None, None
#            try:
#                invoice = Invoice.objects.get(reference=b.crn)
#                try:
#                    booking = AnnualRentalFee.objects.get(invoice_reference=invoice.reference).booking
#                except AnnualRentalFee.DoesNotExist:
#                    pass
#
#                if booking:
#                    b_name = u'{}'.format(booking.proposal.applicant)
#                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
#                    settlement_date = b.p_date.strftime('%d/%m/%Y')
#                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),settlement_date,booking.admission_number,b_name,invoice.get_payment_method_display(),invoice.amount,invoice.reference])
#                else:
#                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.settlement_date.strftime('%d/%m/%Y'),'','',str(b.action),b.amount,invoice.reference])
#            except Invoice.DoesNotExist:
#                pass
#
#        cash = CashTransaction.objects.filter(
#            created__date=_date,
#            invoice__reference__startswith='0517'
#        ).exclude(type__in=['move_out', 'move_in'])
#
#        for b in cash:
#            booking, invoice = None, None
#            try:
#                invoice = b.invoice
#                try:
#                    booking = AnnualRentalFee.objects.get(invoice_reference=invoice.reference).booking
#                except AnnualRentalFee.DoesNotExist:
#                    pass
#
#                if booking:
#                    b_name = u'{} {}'.format(booking.details.get('first_name',''),booking.details.get('last_name',''))
#                    created = timezone.localtime(b.created, pytz.timezone('Australia/Perth'))
#                    writer.writerow([created.strftime('%d/%m/%Y %H:%M:%S'),b.created.strftime('%d/%m/%Y'),booking.confirmation_number,b_name,str(b.type),b.amount,invoice.reference])
#                else:
#                    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.created.strftime('%d/%m/%Y'),'','',str(b.type),b.amount,invoice.reference])
#            except Invoice.DoesNotExist:
#                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #42
0
def bookings_report(_date):
    try:
        bpoint, cash = [], []
        bookings = Booking.objects.filter(created__date=_date).exclude(
            booking_type=3)
        admission_bookings = AdmissionsBooking.objects.filter(
            created__date=_date).exclude(booking_type=3)

        history_bookings = BookingHistory.objects.filter(
            created__date=_date).exclude(booking__booking_type=3)

        strIO = StringIO()
        fieldnames = [
            'Date', 'Confirmation Number', 'Name', 'Invoice Total',
            'Override Price', 'Override Reason', 'Override Details', 'Invoice',
            'Booking Type', 'Created By'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        types = dict(Booking.BOOKING_TYPE_CHOICES)
        types_admissions = dict(AdmissionsBooking.BOOKING_TYPE_CHOICES)
        for b in bookings:
            b_name = 'No Name'
            if b.details:
                b_name = u'{} {}'.format(b.details.get('first_name', ''),
                                         b.details.get('last_name', ''))
            created = timezone.localtime(b.created,
                                         pytz.timezone('Australia/Perth'))
            created_by = ''
            if b.created_by is not None:
                created_by = b.created_by

            writer.writerow([
                created.strftime('%d/%m/%Y %H:%M:%S'), b.confirmation_number,
                b_name.encode('utf-8'),
                b.active_invoice.amount if b.active_invoice else '',
                b.override_price, b.override_reason, b.override_reason_info,
                b.active_invoice.reference if b.active_invoice else '',
                types[b.booking_type]
                if b.booking_type in types else b.booking_type, created_by
            ])

        for b in admission_bookings:
            b_name = 'No Name'
            if b.customer:
                b_name = u'{}'.format(b.customer)
            created = timezone.localtime(b.created,
                                         pytz.timezone('Australia/Perth'))
            created_by = ''
            if b.created_by is not None:
                created_by = b.created_by

            writer.writerow([
                created.strftime('%d/%m/%Y %H:%M:%S'), b.confirmation_number,
                b_name.encode('utf-8'),
                b.active_invoice.amount if b.active_invoice else '', '', '',
                '', b.active_invoice.reference if b.active_invoice else '',
                types_admissions[b.booking_type] if b.booking_type
                in types_admissions else b.booking_type, created_by
            ])

        #for b in history_bookings:
        #    b_name = '{} {}'.format(b.details.get('first_name',''),b.details.get('last_name',''))
        #    writer.writerow([b.created.strftime('%d/%m/%Y %H:%M:%S'),b.booking.confirmation_number,b_name,b.invoice.amount,b.invoice.reference,'Yes'])

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #43
0
def booking_bpoint_settlement_report(_date):
    try:
        bpoint, cash = [], []
        bpoint.extend([
            x for x in BpointTransaction.objects.filter(
                created__date=_date, response_code=0,
                crn1__startswith='0019').exclude(crn1__endswith='_test')
        ])
        cash = CashTransaction.objects.filter(
            created__date=_date,
            invoice__reference__startswith='0019').exclude(
                type__in=['move_out', 'move_in'])

        strIO = StringIO()
        fieldnames = [
            'Payment Date', 'Settlement Date', 'Confirmation Number', 'Name',
            'Type', 'Amount', 'Invoice'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for b in bpoint:
            booking, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)
                try:
                    booking = BookingInvoice.objects.get(
                        invoice_reference=invoice.reference).booking
                except BookingInvoice.DoesNotExist:
                    pass

                if booking:
                    b_name = u'{} {}'.format(
                        booking.details.get('first_name', ''),
                        booking.details.get('last_name', ''))
                    created = timezone.localtime(
                        b.created, pytz.timezone('Australia/Perth'))
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.settlement_date.strftime('%d/%m/%Y'),
                        booking.confirmation_number,
                        b_name.encode('utf-8'),
                        str(b.action), b.amount, invoice.reference
                    ])
                else:
                    writer.writerow([
                        b.created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.settlement_date.strftime('%d/%m/%Y'), '', '',
                        str(b.action), b.amount, invoice.reference
                    ])
            except Invoice.DoesNotExist:
                pass

        for b in cash:
            booking, invoice = None, None
            try:
                invoice = b.invoice
                try:
                    booking = BookingInvoice.objects.get(
                        invoice_reference=invoice.reference).booking
                except BookingInvoice.DoesNotExist:
                    pass

                if booking:
                    b_name = u'{} {}'.format(
                        booking.details.get('first_name', ''),
                        booking.details.get('last_name', ''))
                    created = timezone.localtime(
                        b.created, pytz.timezone('Australia/Perth'))
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.created.strftime('%d/%m/%Y'),
                        booking.confirmation_number,
                        b_name.encode('utf-8'),
                        str(b.type), b.amount, invoice.reference
                    ])
                else:
                    writer.writerow([
                        b.created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.created.strftime('%d/%m/%Y'), '', '',
                        str(b.type), b.amount, invoice.reference
                    ])
            except Invoice.DoesNotExist:
                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #44
0
def debugprint(obj, depth=-1, print_type=False,
               file=None, ids='CHAR', stop_on_name=False,
               done=None):
    """Print a computation graph as text to stdout or a file.

    :type obj: Variable, Apply, or Function instance
    :param obj: symbolic thing to print
    :type depth: integer
    :param depth: print graph to this depth (-1 for unlimited)
    :type print_type: boolean
    :param print_type: whether to print the type of printed objects
    :type file: None, 'str', or file-like object
    :param file: print to this file ('str' means to return a string)
    :type ids: str
    :param ids: How do we print the identifier of the variable
                id - print the python id value
                int - print integer character
                CHAR - print capital character
                "" - don't print an identifier
    :param stop_on_name: When True, if a node in the graph has a name,
                         we don't print anything below it.
    :type done: None or dict
    :param done: A dict where we store the ids of printed node.
        Useful to have multiple call to debugprint share the same ids.

    :returns: string if `file` == 'str', else file arg

    Each line printed represents a Variable in the graph.
    The indentation of lines corresponds to its depth in the symbolic graph.
    The first part of the text identifies whether it is an input
    (if a name or type is printed) or the output of some Apply (in which case
    the Op is printed).
    The second part of the text is an identifier of the Variable.
    If print_type is True, we add a part containing the type of the Variable

    If a Variable is encountered multiple times in the depth-first search,
    it is only printed recursively the first time. Later, just the Variable
    identifier is printed.

    If an Apply has multiple outputs, then a '.N' suffix will be appended
    to the Apply's identifier, to indicate which output a line corresponds to.

    """
    if not isinstance(depth, int):
        raise Exception("depth parameter must be an int")
    if file == 'str':
        _file = StringIO()
    elif file is None:
        _file = sys.stdout
    else:
        _file = file
    if done is None:
        done = dict()
    results_to_print = []
    profile_list = []
    order = []
    if isinstance(obj, (list, tuple)):
        lobj = obj
    else:
        lobj = [obj]
    for obj in lobj:
        if isinstance(obj, gof.Variable):
            results_to_print.append(obj)
            profile_list.append(None)
        elif isinstance(obj, gof.Apply):
            results_to_print.extend(obj.outputs)
            profile_list.extend([None for item in obj.outputs])
        elif isinstance(obj, Function):
            results_to_print.extend(obj.maker.fgraph.outputs)
            profile_list.extend(
                [obj.profile for item in obj.maker.fgraph.outputs])
            order = obj.maker.fgraph.toposort()
        elif isinstance(obj, gof.FunctionGraph):
            results_to_print.extend(obj.outputs)
            profile_list.extend([None for item in obj.outputs])
            order = obj.toposort()
        elif isinstance(obj, (integer_types, float, np.ndarray)):
            print(obj)
        elif isinstance(obj, (theano.In, theano.Out)):
            results_to_print.append(obj.variable)
            profile_list.append(None)
        else:
            raise TypeError("debugprint cannot print an object of this type",
                            obj)

    scan_ops = []
    if any([p for p in profile_list if p is not None and p.fct_callcount > 0]):
        print("""
Timing Info
-----------
--> <time> <% time> - <total time> <% total time>'

<time>         computation time for this node
<% time>       fraction of total computation time for this node
<total time>   time for this node + total times for this node's ancestors
<% total time> total time for this node over total computation time

N.B.:
* Times include the node time and the function overhead.
* <total time> and <% total time> may over-count computation times
  if inputs to a node share a common ancestor and should be viewed as a
  loose upper bound. Their intended use is to help rule out potential nodes
  to remove when optimizing a graph because their <total time> is very low.
""", file=_file)

    for r, p in zip(results_to_print, profile_list):
        # Add the parent scan op to the list as well
        if (hasattr(r.owner, 'op') and
                isinstance(r.owner.op, theano.scan_module.scan_op.Scan)):
                    scan_ops.append(r)

        debugmode.debugprint(r, depth=depth, done=done, print_type=print_type,
                             file=_file, order=order, ids=ids,
                             scan_ops=scan_ops, stop_on_name=stop_on_name,
                             profile=p)

    if len(scan_ops) > 0:
        print("", file=_file)
        new_prefix = ' >'
        new_prefix_child = ' >'
        print("Inner graphs of the scan ops:", file=_file)

        for s in scan_ops:
            # prepare a dict which maps the scan op's inner inputs
            # to its outer inputs.
            if hasattr(s.owner.op, 'fn'):
                # If the op was compiled, print the optimized version.
                inner_inputs = s.owner.op.fn.maker.fgraph.inputs
            else:
                inner_inputs = s.owner.op.inputs
            outer_inputs = s.owner.inputs
            inner_to_outer_inputs = \
                dict([(inner_inputs[i], outer_inputs[o])
                      for i, o in
                      s.owner.op.var_mappings['outer_inp_from_inner_inp']
                      .items()])

            print("", file=_file)
            debugmode.debugprint(
                s, depth=depth, done=done,
                print_type=print_type,
                file=_file, ids=ids,
                scan_ops=scan_ops,
                stop_on_name=stop_on_name,
                scan_inner_to_outer_inputs=inner_to_outer_inputs)
            if hasattr(s.owner.op, 'fn'):
                # If the op was compiled, print the optimized version.
                outputs = s.owner.op.fn.maker.fgraph.outputs
            else:
                outputs = s.owner.op.outputs
            for idx, i in enumerate(outputs):

                if hasattr(i, 'owner') and hasattr(i.owner, 'op'):
                    if isinstance(i.owner.op, theano.scan_module.scan_op.Scan):
                        scan_ops.append(i)

                debugmode.debugprint(
                    r=i, prefix=new_prefix,
                    depth=depth, done=done,
                    print_type=print_type, file=_file,
                    ids=ids, stop_on_name=stop_on_name,
                    prefix_child=new_prefix_child,
                    scan_ops=scan_ops,
                    scan_inner_to_outer_inputs=inner_to_outer_inputs)

    if file is _file:
        return file
    elif file == 'str':
        return _file.getvalue()
    else:
        _file.flush()
Beispiel #45
0
def booking_bpoint_settlement_report(_date):
    try:
        # Start writing the headers
        strIO = StringIO()
        fieldnames = [
            'Payment Date',
            'Settlement Date',
            'Confirmation Number',
            'Name',
            'Type',
            'Amount',
            'Invoice',
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        bpoint = []
        bpoint.extend([
            x for x in BpointTransaction.objects.filter(
                Q(created__date=_date),
                Q(response_code=0),
                Q(crn1__startswith=PAYMENT_SYSTEM_PREFIX),
            ).exclude(crn1__endswith='_test')
        ])

        for b in bpoint:
            dcv_permit_fee, dcv_admission_fee, app_type_dcv_permit, compliance = None, None, None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)

                try:
                    dcv_permit_fee = DcvPermitFee.objects.get(
                        invoice_reference=invoice.reference)
                except DcvPermitFee.DoesNotExist:
                    pass

                try:
                    dcv_admission_fee = DcvAdmissionFee.objects.get(
                        invoice_reference=invoice.reference)
                except DcvAdmissionFee.DoesNotExist:
                    pass

                try:
                    application_fee = ApplicationFee.objects.get(
                        invoice_reference=invoice.reference)
                except ApplicationFee.DoesNotExist:
                    pass

                try:
                    compliance = Compliance.objects.get(
                        fee_invoice_reference=invoice.reference)
                except Compliance.DoesNotExist:
                    pass

                if dcv_permit_fee:
                    b_name = u'{}'.format(dcv_permit_fee.created_by)
                    created = timezone.localtime(dcv_permit_fee.created,
                                                 pytz.timezone(TIME_ZONE))
                    settlement_date = b.settlement_date.strftime(
                        '%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        dcv_permit_fee.dcv_permit.lodgement_number, b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                elif dcv_admission_fee:
                    b_name = u'{}'.format(dcv_admission_fee.created_by)
                    created = timezone.localtime(dcv_admission_fee.created,
                                                 pytz.timezone(TIME_ZONE))
                    settlement_date = b.settlement_date.strftime(
                        '%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        dcv_admission_fee.dcv_admission.lodgement_number,
                        b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                elif application_fee:
                    b_name = u'{}'.format(application_fee.proposal.applicant)
                    created = timezone.localtime(
                        application_fee.created,
                        pytz.timezone('Australia/Perth'))
                    settlement_date = b.settlement_date.strftime(
                        '%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        application_fee.proposal.lodgement_number, b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                elif compliance:
                    submitter = compliance.approval.submitter if compliance.approval else compliance.proposal.submitter
                    b_name = u'{}'.format(submitter)
                    created = timezone.localtime(
                        b.created, pytz.timezone('Australia/Perth'))
                    settlement_date = b.settlement_date.strftime(
                        '%d/%m/%Y') if b.settlement_date else ''
                    writer.writerow([
                        created.strftime('%d/%m/%Y %H:%M:%S'), settlement_date,
                        compliance.lodgement_number, b_name,
                        invoice.get_payment_method_display(), invoice.amount,
                        invoice.reference
                    ])
                else:
                    writer.writerow([
                        b.created.strftime('%d/%m/%Y %H:%M:%S'),
                        b.settlement_date.strftime('%d/%m/%Y'), '', '',
                        str(b.action), b.amount, invoice.reference
                    ])
            except Invoice.DoesNotExist:
                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #46
0
def booking_refunds(start, end):
    try:
        bpoint, cash = [], []
        bpoint.extend([
            x for x in BpointTransaction.objects.filter(
                settlement_date__gte=start,
                settlement_date__lte=end,
                action='refund',
                response_code=0)
        ])
        cash.extend([
            x for x in CashTransaction.objects.filter(
                created__gte=start, created__lte=end, type='refund')
        ])

        strIO = StringIO()
        fieldnames = [
            'Confirmation Number', 'Name', 'Type', 'Amount', 'Oracle Code',
            'Date', 'Refunded By', 'Reason', 'Invoice'
        ]
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        # Get the required invoices
        for e in cash:
            booking, invoice = None, None
            if e.invoice.system == '0019':
                try:
                    booking = BookingInvoice.objects.get(
                        invoice_reference=e.invoice.reference).booking
                    invoice = e.invoice
                except BookingInvoice.DoesNotExist:
                    pass
                    #raise ValidationError('Couldn\'t find a booking matched to invoice reference {}'.format(e.invoice.reference))
                for line in invoice.order.lines.all():
                    for k, v in line.refund_details['cash'].items():
                        if k == str(e.id):
                            track = None
                            try:
                                track = TrackRefund.objects.get(type=1,
                                                                refund_id=k)
                            except TrackRefund.DoesNotExist:
                                pass
                            name = ''
                            reason = ''
                            if track:
                                name = track.user.get_full_name(
                                ) if track.user.get_full_name(
                                ) else track.user.email
                                reason = track.details
                            if booking:
                                b_name = '{} {}'.format(
                                    booking.details.get('first_name', ''),
                                    booking.details.get('last_name', ''))
                                writer.writerow([
                                    booking.confirmation_number, b_name,
                                    'Manual', v, line.oracle_code,
                                    e.created.strftime('%d/%m/%Y'), name,
                                    reason, invoice.reference
                                ])
                            else:
                                writer.writerow([
                                    '', '', 'Manual', v, line.oracle_code,
                                    e.created.strftime('%d/%m/%Y'), name,
                                    invoice.reference
                                ])
        for b in bpoint:
            booking, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)
                if invoice.system == '0019':
                    try:
                        booking = BookingInvoice.objects.get(
                            invoice_reference=invoice.reference).booking
                    except BookingInvoice.DoesNotExist:
                        pass
                        #raise ValidationError('Couldn\'t find a booking matched to invoice reference {}'.format(e.invoice.reference))
                    for line in invoice.order.lines.all():
                        for k, v in line.refund_details['card'].items():
                            if k == str(b.id):
                                track = None
                                try:
                                    track = TrackRefund.objects.get(
                                        type=2, refund_id=k)
                                except TrackRefund.DoesNotExist:
                                    pass
                                name = ''
                                reason = ''
                                if track:
                                    name = track.user.get_full_name(
                                    ) if track.user.get_full_name(
                                    ) else track.user.email
                                    reason = track.details
                                if booking:
                                    b_name = '{} {}'.format(
                                        booking.details.get('first_name', ''),
                                        booking.details.get('last_name', ''))
                                    writer.writerow([
                                        booking.confirmation_number, b_name,
                                        'Card', v, line.oracle_code,
                                        b.created.strftime('%d/%m/%Y'), name,
                                        reason, invoice.reference
                                    ])
                                else:
                                    writer.writerow([
                                        '', '', 'Card', v, line.oracle_code,
                                        b.created.strftime('%d/%m/%Y'), name,
                                        invoice.reference
                                    ])
            except Invoice.DoesNotExist:
                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #47
0
def booking_refunds(start,end):
    try:
        bpoint, cash = [], []
        bpoint.extend([x for x in BpointTransaction.objects.filter(settlement_date__gte=start, settlement_date__lte=end,action='refund',response_code=0)])
        cash.extend([x for x in CashTransaction.objects.filter(created__gte=start, created__lte=end,type='refund')])

        strIO = StringIO()
        fieldnames = ['Confirmation Number', 'Name', 'Type','Amount','Oracle Code','Date','Refunded By','Reason','Invoice']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        # Get the required invoices
        for e in cash:
            booking, invoice = None, None
            if e.invoice.system == '0516':
                try:
                    booking = BookingInvoice.objects.get(invoice_reference=e.invoice.reference).booking
                    invoice = e.invoice
                except BookingInvoice.DoesNotExist:
                    pass
                    #raise ValidationError('Couldn\'t find a booking matched to invoice reference {}'.format(e.invoice.reference))
                for line in invoice.order.lines.all():
                    for k,v in line.refund_details['cash'].items():
                        if k == str(e.id):
                            track = None
                            try:
                                track = TrackRefund.objects.get(type=1,refund_id=k)
                            except TrackRefund.DoesNotExist:
                                pass
                            name = ''
                            reason = ''
                            if track:
                                name = track.user.get_full_name() if track.user.get_full_name() else track.user.email
                                reason = track.details
                            if booking:
                                b_name = '{} {}'.format(booking.details.get('first_name',''),booking.details.get('last_name',''))
                                writer.writerow([booking.confirmation_number,b_name,'Manual',v,line.oracle_code,e.created.strftime('%d/%m/%Y'),name,reason,invoice.reference])
                            else:
                                writer.writerow(['','','Manual',v,line.oracle_code,e.created.strftime('%d/%m/%Y'),name,invoice.reference])
        for b in bpoint:
            booking, invoice = None, None
            try:
                invoice = Invoice.objects.get(reference=b.crn1)
                if invoice.system == '0516':
                    try:
                        booking = BookingInvoice.objects.get(invoice_reference=invoice.reference).booking
                    except BookingInvoice.DoesNotExist:
                        pass
                        #raise ValidationError('Couldn\'t find a booking matched to invoice reference {}'.format(e.invoice.reference))
                    for line in invoice.order.lines.all():
                        for k,v in line.refund_details['card'].items():
                            if k == str(b.id):
                                track = None
                                try:
                                    track = TrackRefund.objects.get(type=2,refund_id=k)
                                except TrackRefund.DoesNotExist:
                                    pass
                                name = ''
                                reason = ''
                                if track:
                                    name = track.user.get_full_name() if track.user.get_full_name() else track.user.email
                                    reason = track.details
                                if booking:
                                    b_name = '{} {}'.format(booking.details.get('first_name',''),booking.details.get('last_name',''))
                                    writer.writerow([booking.confirmation_number,b_name,'Card',v,line.oracle_code,b.created.strftime('%d/%m/%Y'),name,reason,invoice.reference])
                                else:
                                    writer.writerow(['','','Card',v,line.oracle_code,b.created.strftime('%d/%m/%Y'),name,invoice.reference])
            except Invoice.DoesNotExist:
                pass

        strIO.flush()
        strIO.seek(0)
        return strIO
    except:
        raise
Beispiel #48
0
def generate_items_csv(system,
                       start,
                       end,
                       banked_start,
                       banked_end,
                       region=None,
                       district=None):
    strIO = None
    invoices = Invoice.objects.filter(system=system)
    dates, banked_dates = [], []
    date_amounts, banked_date_amounts = [], []
    items = []
    date_format = '%A %d/%m/%y'
    if invoices:
        strIO = StringIO()
        fieldnames = ['Account Code', 'Day']
        writer = csv.writer(strIO)
        writer.writerow(fieldnames)

        for d in daterange(start, end):
            dates.append(d.strftime(date_format))
            date_amounts.append({
                'date': d.strftime(date_format),
                'amounts': {
                    'card': D('0.0'),
                    'bpay': D('0.0'),
                    'eft': D('0.0'),
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })
        for d in daterange(banked_start, banked_end):
            banked_dates.append(d.strftime(date_format))
            banked_date_amounts.append({
                'date': d.strftime(date_format),
                'amounts': {
                    'cash': D('0.0'),
                    'cheque': D('0.0'),
                    'money_order': D('0.0')
                }
            })

        dates_row = ''
        for date in dates:
            dates_row += '{},,,'.format(date)

        # Dates row
        writer.writerow([''] + dates_row.split(','))
        writer.writerow([''] + ['Credit Card', 'Bpay', 'EFTPOS'] * len(dates) +
                        ['', 'Credit Card', 'Bpay', 'EFTPOS'])
        for i in invoices:
            # Add items of invoice if not in list
            if i.order:
                for x in i.order.lines.all():
                    item_date_amounts, banked_item_dates_amounts = [], []
                    for d in dates:
                        item_date_amounts.append({
                            'date': d,
                            'amounts': {
                                'card': D('0.0'),
                                'bpay': D('0.0'),
                                'eft': D('0.0'),
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })
                    for d in banked_dates:
                        banked_item_dates_amounts.append({
                            'date': d,
                            'amounts': {
                                'cash': D('0.0'),
                                'cheque': D('0.0'),
                                'money_order': D('0.0')
                            }
                        })

                    if not any(
                            it.get('item', None).oracle_code == x.oracle_code
                            for it in items):
                        items.append({
                            'dates': item_date_amounts,
                            'banked_dates': banked_item_dates_amounts,
                            'item': x,
                            'card': D('0.0'),
                            'bpay': D('0.0'),
                            'eft': D('0.0'),
                            'cash': D('0.0'),
                            'cheque': D('0.0'),
                            'money_order': D('0.0')
                        })
            # Get all transactions
            cash = i.cash_transactions.filter(created__gte=start,
                                              created__lte=end)
            banked_cash = i.cash_transactions.filter(created__gte=banked_start,
                                                     created__lte=banked_end)
            bpoint = i.bpoint_transactions.filter(created__gte=start,
                                                  created__lte=end)
            bpay = i.bpay_transactions.filter(p_date__gte=start,
                                              p_date__lte=end)
            # Go through items

            for item in items:
                # Banked Cash
                for d in item['banked_dates']:
                    index = 0
                    for l in banked_date_amounts:
                        if l.get('date') == d.get('date'):
                            date_amount_index = index
                            break
                        index += 1
                    for c in banked_cash:
                        for s in CashTransaction.SOURCE_TYPES:
                            source = str(s[0])
                            if c.source == source and source != 'eft':
                                if c.created.strftime(date_format) == d.get(
                                        'date'):
                                    if c.type == 'payment':
                                        d['amounts'][source] += D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                                        item[source] += D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                                        banked_date_amounts[date_amount_index][
                                            'amounts'][source] += D(
                                                item.get('item').
                                                line_price_before_discounts_incl_tax
                                            )
                                    else:
                                        d['amounts'][source] -= D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                                        item[source] -= D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                                        banked_date_amounts[date_amount_index][
                                            'amounts'][source] -= D(
                                                item.get('item').
                                                line_price_before_discounts_incl_tax
                                            )
                # Other transactions
                for d in item['dates']:
                    index = 0
                    for l in date_amounts:
                        if l.get('date') == d.get('date'):
                            date_amount_index = index
                            break
                        index += 1
                    # EFT
                    for c in cash:
                        if c.created.strftime(date_format) == d.get('date'):
                            if c.type == 'payment' and c.source == 'eft':
                                d['amounts']['eft'] += D(
                                    item.get('item').
                                    line_price_before_discounts_incl_tax)
                                item['eft'] += D(
                                    item.get('item').
                                    line_price_before_discounts_incl_tax)
                                date_amounts[date_amount_index]['amounts'][
                                    'eft'] += D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                            else:
                                d['amounts']['eft'] -= D(
                                    item.get('item').
                                    line_price_before_discounts_incl_tax)
                                item['eft'] -= D(
                                    item.get('item').
                                    line_price_before_discounts_incl_tax)
                                date_amounts[date_amount_index]['amounts'][
                                    'eft'] -= D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)

                    # Card
                    for c in bpoint:
                        if c.approved:
                            if c.created.strftime(date_format) == d.get(
                                    'date'):
                                if c.action == 'payment' or c.action == 'capture':
                                    d['amounts']['card'] += D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    item['card'] += D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts'][
                                        'card'] += D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                                elif c.action == 'reversal' or c.action == 'refund':
                                    d['amounts']['card'] -= D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    item['card'] -= D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts'][
                                        'card'] -= D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                    # BPAY
                    for b in bpay:
                        if b.approved:
                            if b.p_date.strftime(date_format) == d.get('date'):
                                if b.p_instruction_code == '05' and b.type == '399':
                                    d['amounts']['bpay'] += D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    item['bpay'] += D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts'][
                                        'bpay'] += D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
                                elif b.p_instruction_code == '25' and b.type == '699':
                                    d['amounts']['bpay'] += D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    item['bpay'] -= D(
                                        item.get('item').
                                        line_price_before_discounts_incl_tax)
                                    date_amounts[date_amount_index]['amounts'][
                                        'bpay'] -= D(
                                            item.get('item').
                                            line_price_before_discounts_incl_tax
                                        )
        for i in items:
            item_str = ''
            item_str += '{},'.format(str(i.get('item').oracle_code))
            for d in i['dates']:
                item_str += '{},{},{},'.format(d['amounts']['card'],
                                               d['amounts']['bpay'],
                                               d['amounts']['eft'])
            item_str += ',{},{},{},'.format(i['card'], i['bpay'], i['eft'])
            writer.writerow(item_str.split(','))

        total_str = 'Totals,'
        total_amounts = {'card': D('0.0'), 'bpay': D('0.0'), 'eft': D('0.0')}
        for d in date_amounts:
            total_amounts['card'] += d['amounts']['card']
            total_amounts['bpay'] += d['amounts']['bpay']
            total_amounts['eft'] += d['amounts']['eft']
            total_str += '{},{},{},'.format(d['amounts']['card'],
                                            d['amounts']['bpay'],
                                            d['amounts']['eft'])
        total_str += ',{},{},{},'.format(total_amounts['card'],
                                         total_amounts['bpay'],
                                         total_amounts['eft'])
        writer.writerow('')
        writer.writerow(total_str.split(','))

        # Banked Items
        writer.writerow('')
        writer.writerow(fieldnames)
        banked_dates_row = ''
        for date in banked_dates:
            banked_dates_row += '{},,,'.format(date)
        writer.writerow([''] + banked_dates_row.split(','))
        writer.writerow([''] +
                        ['Cash', 'Cheque', 'Money Order'] * len(banked_dates) +
                        [
                            '', 'Cash', 'Cheque', 'Money Order',
                            'Banked(Cash,Money Order,Cheque)'
                        ])

        for i in items:
            banked_item_str = ''
            banked_item_str += '{},'.format(str(i.get('item').oracle_code))
            for d in i['banked_dates']:
                banked_item_str += '{},{},{},'.format(
                    d['amounts']['cash'], d['amounts']['cheque'],
                    d['amounts']['money_order'])
            banked_item_str += ',{},{},{},'.format(i['cash'], i['cheque'],
                                                   i['money_order'])
            writer.writerow(banked_item_str.split(','))

        banked_total_str = 'Totals,'
        banked_total_amounts = {
            'cash': D('0.0'),
            'cheque': D('0.0'),
            'money_order': D('0.0')
        }
        for d in banked_date_amounts:
            banked_total_amounts['cash'] += d['amounts']['cash']
            banked_total_amounts['cheque'] += d['amounts']['cheque']
            banked_total_amounts['money_order'] += d['amounts']['money_order']
            banked_total_str += '{},{},{},'.format(d['amounts']['cash'],
                                                   d['amounts']['cheque'],
                                                   d['amounts']['money_order'])
        banked_total_str += ',{},{},{},'.format(
            banked_total_amounts['cash'], banked_total_amounts['cheque'],
            banked_total_amounts['money_order'])
        writer.writerow('')
        writer.writerow(banked_total_str.split(','))

        strIO.flush()
        strIO.seek(0)
    return strIO
Beispiel #49
0
class CaptureStdOut(object):
    """
    An logger that both prints to stdout and writes to file.
    """

    def __init__(self, log_file_path = None, print_to_console = True, prefix = None):
        """
        :param log_file_path: The path to save the records, or None if you just want to keep it in memory
        :param print_to_console:
        """
        self._print_to_console = print_to_console
        if log_file_path is not None:
            # self._log_file_path = os.path.join(base_dir, log_file_path.replace('%T', now))
            make_file_dir(log_file_path)
            self.log = open(log_file_path, 'w')
        else:
            self.log = StringIO()
        self._log_file_path = log_file_path
        self.old_stdout = _ORIGINAL_STDOUT
        self.prefix = None if prefix is None else prefix

    def __enter__(self):

        self.old_stdout = sys.stdout
        self.old_stderr = sys.stderr

        sys.stdout = self
        sys.stderr = self
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout.flush()
        sys.stderr.flush()
        sys.stdout = self.old_stdout
        sys.stderr = self.old_stderr
        self.close()

    def get_log_file_path(self):
        assert self._log_file_path is not None, "You never specified a path when you created this logger, so don't come back and ask for one now"
        return self._log_file_path

    def write(self, message):
        if self._print_to_console:
            self.old_stdout.write(message if self.prefix is None or message=='\n' else self.prefix+message)
        self.log.write(message)
        self.log.flush()

    def close(self):
        if self._log_file_path is not None:
            self.log.close()

    def read(self):
        if self._log_file_path is None:
            return self.log.getvalue()
        else:
            with open(self._log_file_path) as f:
                txt = f.read()
            return txt

    def __getattr__(self, item):
        return getattr(self.old_stdout, item)