def stripe_fee_line_item(payments, total, payment_id, stripe_fees_item, order_date, other_fees=0, qbo_class=None): charge = payments.get(payment_id) if charge is None: return transaction = stripe.BalanceTransaction.retrieve( charge.balance_transaction) stripe_fee_info = None for fee_info in transaction.fee_details: if fee_info.type == 'stripe_fee': stripe_fee_info = fee_info break if stripe_fee_info is None: logger.error(str(transaction.fee_details)) raise Exception("could not find stripe fee in fees") stripe_fee_amount = Decimal(stripe_fee_info.amount) / Decimal(100.0) logger.debug("stripe fee amount %s" % stripe_fee_amount) line = SalesItemLine() if stripe_fee_for_total(total) == stripe_fee_amount: line.Amount = (-1) * stripe_fee_for_total(total) line.Description = "Stripe fees of {:.1%} of ${:.2f} plus ${:.2f}".format( stripe_rate, total, stripe_fixed) elif stripe_fee_for_total_amex(total) == stripe_fee_amount: line.Amount = (-1) * stripe_fee_for_total_amex(total) line.Description = "Stripe AmEx fees of {:.1%} of ${:.2f}".format( stripe_amex_rate, total, stripe_fixed) else: logger.debug( "stripe fees of %s don't match standard fees of %s or amex fees of %s" % (stripe_fee_amount, stripe_fee_for_total(total), stripe_fee_for_total_amex(total))) line.Amount = (-1) * stripe_fee_amount line.Description = "Stripe fees" detail = SalesItemLineDetail() detail.ItemRef = stripe_fees_item.to_ref() detail.Qty = one detail.UnitPrice = line.Amount if qbo_class is not None: detail.ClassRef = qbo_class.to_ref() detail.ServiceDate = order_date.strftime("%Y-%m-%d") line.SalesItemLineDetail = detail return line
def vendor_unit_fee_line_item(vendor_rate, qty, vendor_fee_item, order_date, qbo_class=None): line = SalesItemLine() line.Amount = (-1) * vendor_rate * qty line.Description = "{} of ${:.2f} x {}".format(vendor_fee_item.Name, abs(vendor_rate), qty) detail = SalesItemLineDetail() detail.ItemRef = vendor_fee_item.to_ref() detail.Qty = qty detail.UnitPrice = line.Amount / qty if qbo_class is not None: detail.ClassRef = qbo_class.to_ref() detail.ServiceDate = order_date.strftime("%Y-%m-%d") line.SalesItemLineDetail = detail return line
def transaction_line_item(total, description, qty, item, service_date, qbo_class=None): line = SalesItemLine() line.Amount = total line.Description = description detail = SalesItemLineDetail() detail.Qty = qty detail.UnitPrice = line.Amount / detail.Qty detail.ItemRef = item.to_ref() if qbo_class is not None: detail.ClassRef = qbo_class.to_ref() if service_date: detail.ServiceDate = service_date.strftime("%Y-%m-%d") line.SalesItemLineDetail = detail return line
def qbo_create_invoice(so, customer_id): client = create_qbc() customer_ref = Customer.get(customer_id, qb=client).to_ref() line_detail = SalesItemLineDetail() line_detail.UnitPrice = 100 # in dollars line_detail.Qty = 1 # quantity can be decimal item_ref = Item.get(35, qb=client).to_ref() line_detail.ItemRef = item_ref line = SalesItemLine() line.Amount = 100 # in dollars line.SalesItemLineDetail = line_detail line.DetailType = "SalesItemLineDetail" invoice = Invoice() invoice.CustomerRef = customer_ref invoice.Line = [line] invoice.save(qb=client)
def send_to_quickbooks(self, request): # Before we ship to quickbooks, let's save the freshsheet used by this Order self.freshsheet = FreshSheet.objects.latest('published_at') self.save() client = get_qb_client() customer = Ref() # customer.value = 1 customer.value = self.created_by.qb_customer_id # customer.name = self.created_by.req_info.business_name customer.type = 'Customer' line_items = [] for item in self.items.all(): item_lookup = Item.where( f"Name = '{item.item.name}{item.unit_quantity}'", qb=client) if item_lookup: product = item_lookup[0] product.UnitPrice = item.unit_cost product.Type = 'NonInventory' product.IncomeAccountRef = Account.where( "Name = 'Sales'", qb=client)[0].to_ref() product.save(qb=client) else: product = Item() product.Name = f"{item.item.name}{item.unit_quantity}" product.UnitPrice = item.unit_cost product.Type = 'NonInventory' product.IncomeAccountRef = Account.where( "Name = 'Sales'", qb=client)[0].to_ref() product.save(qb=client) line_detail = SalesItemLineDetail() line_detail.ItemRef = product.to_ref() line_detail.UnitPrice = item.unit_cost # in dollars line_detail.Qty = item.quantity # quantity can be decimal # Need to change this date to be the DELIVERY DATE of shipment, # not the date on which it was created # Check if it's between Sunday and Tuesday (Yields Tuesday date().isoformat()) # Check if it's between Wednesday and Friday (Yields Friday date().isoformat()) line_detail.ServiceDate = get_next_service_date().isoformat() line = SalesItemLine() line.Id = '1' line.Amount = item.total_cost # in dollars line.Description = f"{item.quantity} {item.item.get_unit_verbose()} of {product.Name} from " \ f"{item.item.farm}." line.SalesItemLineDetail = line_detail line_items.append(line) invoice = Invoice() invoice.CustomerRef = customer invoice.Line = line_items invoice.save(qb=client) # NOTE: If we try to just save the user model, it _could_ overwrite some Quickbooks auth settings. # By getting a fresh model we'll for sure have the latest settings fresh_user_model = User.objects.get(pk=request.user.pk) fresh_user_model.cart = None fresh_user_model.save()