def delete_in_xero(self): try: if self.received_payments > 0: return {'success': False, 'error': 'This invoice already has payments'} if not self.is_in_xero: return {'success': True} xero = get_xero_client() res = xero.invoices.filter(InvoiceID=self.xero_invoice_id) if res: xero_invoice = res[0] if xero_invoice.get('AmountPaid') > 0: return {'success': False, 'error': 'This invoice already has payments'} else: xero_invoice['Status'] = 'VOIDED' xero.invoices.save(xero_invoice) return {'success': True} except Exception as e: print(str(e)) return {'success': False, 'error': 'Failed to delete invoice'}
def send_to_xero(self): try: xero = get_xero_client() res = self.matter.client.create_or_update_xero_contact() if not res.get('success'): return {'success': False, 'error': res.get('error')} invoice_data = prepare_xero_invoice_param(self) if self.xero_invoice_id: invoice_data['InvoiceID'] = self.xero_invoice_id xero.invoices.save(invoice_data) else: xero_invoice = xero.invoices.put(invoice_data)[0] self.xero_invoice_id = xero_invoice.get('InvoiceID') self.status = InvoiceStatus.objects.get(name='In Xero') self.save() return {'success': True} except Exception as e: print(str(e)) return {'success': False, 'error': 'Failed to create invoice in Xero'}
def check_contact_in_xero(client): xero = get_xero_client() res = xero.contacts.filter(Name=client.name) if res: client.xero_contact_id = res[0].get('ContactID') client.save() return False return True
def get_branding_theme_id_from_xero(self): if not self.xero_branding_theme_name: return False xero = get_xero_client() try: res = xero.brandingthemes.filter( Name=self.xero_branding_theme_name) if res: self.xero_branding_theme_id = res[0].get('BrandingThemeID') self.save() return True return False except Exception as e: print(str(e)) return False
def create_or_update_xero_contact(self, force_update=False): try: xero = get_xero_client() needs_create = False if not self.xero_contact_id: needs_create = check_contact_in_xero(self) else: res = xero.contacts.filter(ContactID=self.xero_contact_id) if not res: needs_create = check_contact_in_xero(self) contact_param = prepare_xero_contact_param(self) valid = contact_param.get('valid') if not valid: return {'success': False, 'error': contact_param.get('error')} xero_contact_data = contact_param.get('data') if needs_create: xero_contact = xero.contacts.put(xero_contact_data)[0] self.xero_contact_id = xero_contact.get('ContactID') self.save() elif force_update: xero_contact_data['ContactID'] = self.xero_contact_id xero.contacts.save(xero_contact_data) return {'success': True} except Exception as e: match = re.search(r'\((.*?)\)', str(e)) if match: error = 'Contact: {}'.format(match.group(1)) else: error = 'Failed to create contact in Xero' return {'success': False, 'error': error}
def can_update(self): try: if self.received_payments > 0: return {'success': False, 'error': 'This invoice already has payments'} if not self.is_in_xero: return {'success': True} xero = get_xero_client() res = xero.invoices.filter(InvoiceID=self.xero_invoice_id) if res: xero_invoice = res[0] if xero_invoice.get('AmountPaid') > 0: return {'success': False, 'error': 'This invoice already has payments'} return {'success': True} except Exception as e: print(str(e)) return {'success': False, 'error': 'This invoice can not be updated'}
def fetch_payments_from_xero(self): try: if not self.xero_invoice_id: return {'success': True} xero = get_xero_client() xero_payments = xero.payments.filter(Invoice_InvoiceID=self.xero_invoice_id) if not xero_payments: return {'success': True} for xero_payment in xero_payments: status = xero_payment.get('Status') payment_data = { 'xero_payment_id': xero_payment.get('PaymentID'), 'amount': xero_payment.get('Amount'), 'date': xero_payment.get('Date'), } method = get_payment_method(xero_payment.get('Reference')) if method: payment_data['method'] = method payment = self.payments.filter(xero_payment_id=xero_payment.get('PaymentID')).first() if not payment and status != 'DELETED': self.payments.create(**payment_data) elif payment and status != 'DELETED': payment.update(**payment_data) elif payment and status == 'DELETED': payment.delete() return {'success': True} except Exception as e: print(str(e)) return {'success': False, 'error': 'Failed to fetch payments from xero'}
def mutate(root, info, **args): errors = [] payment = None if not info.context.user.has_perm('invoicing.add_payment'): errors.append('You do not have a permission to add a payment!') return AddPaymentMutation(errors=errors) try: invoice_id = from_global_id(args.get('invoice_id'))[1] invoice = Invoice.objects.get(id=invoice_id) if not invoice.xero_invoice_id: errors.append('Invoice is manually entered in Xero') return AddPaymentMutation(errors=errors) xero = get_xero_client() res = xero.invoices.filter(InvoiceID=invoice.xero_invoice_id) if not res: errors.append('Invoice is manually entered in Xero') return AddPaymentMutation(errors=errors) payment_data = { 'Invoice': { 'InvoiceID': invoice.xero_invoice_id, }, 'Account': { 'Code': '090', }, 'Amount': args.get('amount'), 'Date': parser.parse(args.get('date')), 'Reference': METHODS[args.get('method') - 1], } xero_payment = xero.payments.put(payment_data)[0] print(xero_payment) payment = Payment.objects.create( invoice_id=invoice.id, amount=Decimal(args.get('amount')).quantize( Decimal('.01'), rounding=ROUND_HALF_UP), date=parser.parse(args.get('date')), method=args.get('method'), xero_payment_id=xero_payment.get('PaymentID')) payment.invoice.save() return AddPaymentMutation(errors=errors, payment=payment) except Invoice.DoesNotExist: errors.append('Invoice with provided id does not exist') return AddPaymentMutation(errors=errors) except XeroException as e: print(str(e)) errors.append('Failed to create payment in Xero') return AddPaymentMutation(errors=errors) except Exception as e: print(str(e)) errors.append('Failed to create payment') return AddPaymentMutation(errors=errors) return AddPaymentMutation(errors=errors, payment=payment)