Ejemplo n.º 1
0
def sync_vouchers(test_run):
    vouchers = Voucher.objects.all()
    logger.info("Syncing {0} Voucher objects.".format(vouchers.count()))

    for voucher in vouchers:
        logger.info("Syncing Voucher: {0}".format(voucher))

        # Find the corresponding SF project.
        try:
            sfvoucher = SalesforceVoucher.objects.filter(external_id=voucher.id).get()
        except SalesforceVoucher.DoesNotExist:
            sfvoucher = SalesforceVoucher()

        # SF Layout: Donation Information section.
        #TODO: Error - sfvoucher.purchaser = SalesforceContact.objects.filter(external_id=voucher.sender).get()
        sfvoucher.voucher_type = voucher.status
        # This is a record type Voucher, probably better to create RecordType model and use the name 'Voucher' to get Id
        sfvoucher.record_type = "012A0000000BxfH"

        # SF Layout: Additional Information section.
        #Unknown: sfvoucher.description = voucher.description

        # SF Layout: System Information section.
        # TODO: Error - sfvoucher.receiver = SalesforceContact.objects.filter(external_id=voucher.receiver).get()

        # SF Other.
        # sfvoucher.voucher_id = voucher.id
        sfvoucher.external_id = voucher.id

        # Save the SF voucher.
        if not test_run:
            sfvoucher.save()
Ejemplo n.º 2
0
def sync_vouchers(dry_run, sync_from_datetime, loglevel):
    logger.setLevel(loglevel)
    error_count = 0
    success_count = 0

    vouchers = Voucher.objects.all()
    if sync_from_datetime:
        vouchers = vouchers.filter(updated__gte=sync_from_datetime)

    logger.info("Syncing {0} Voucher objects.".format(vouchers.count()))

    for voucher in vouchers:
        logger.debug("Syncing Voucher: {0}".format(voucher.id))

        # Find the corresponding SF vouchers.
        try:
            sfvoucher = SalesforceVoucher.objects.get(external_id_voucher=voucher.id)
        except SalesforceVoucher.DoesNotExist:
            sfvoucher = SalesforceVoucher()

        # Initialize the Contact object that refers to the voucher purchaser
        try:
            sfvoucher.purchaser = SalesforceContact.objects.get(external_id=voucher.sender_id)
        except SalesforceContact.DoesNotExist:
            logger.error("Unable to find purchaser contact id {0} in Salesforce for voucher id {1}".format(
                voucher.sender_id, voucher.id))

        # TODO: There should be a voucher.project.id, else remove from (parent) models
        # sfvoucher.project = SalesforceProject.objects.get(external_id=1)

        # TODO: There should be a voucher.receiver.id, , else remove from (parent) models
        # sfvoucher.receiver = SalesforceContact.objects.get(external_id=1)

        # SF Layout: Donation Information section.
        sfvoucher.amount = "%01.2f" % (float(voucher.amount) / 100)
        sfvoucher.close_date = voucher.created
        sfvoucher.description = voucher.message
        # sfvoucher.stage_name exists as state: "In progress", however this has been shifted to Donation?
        sfvoucher.stage_name = VoucherStatuses.values[voucher.status].title()

        #sfvoucher.payment_method = ""

        if sfvoucher.purchaser and sfvoucher.purchaser.last_name:
            if sfvoucher.purchaser.first_name:
                sfvoucher.name = sfvoucher.purchaser.first_name + " " + sfvoucher.purchaser.last_name
            else:
                sfvoucher.name = sfvoucher.purchaser.last_name
        else:
            sfvoucher.name = "1%MEMBER"

        # sfvoucher.name exist in production: "NOT YET USED 1%VOUCHER" when stage_name is "In progress"
        # sfvoucher.opportunity_type = ""

        # SF Other.
        sfvoucher.external_id_voucher = voucher.id
        sfvoucher.record_type = "012A0000000BxfHIAS"

        # Save the object to Salesforce
        if not dry_run:
            try:
                sfvoucher.save()
                success_count += 1
            except Exception as e:
                error_count += 1
                logger.error("Error while saving voucher id {0}: ".format(voucher.id) + str(e))

    return success_count, error_count