def sync_bill(supplier, invoice_num, invoice_date, notes, lines, department=None): refresh_session() store_refs = {x.Name: x.to_ref() for x in Department.all()} # is this a credit if reduce(lambda x, y: x + atof(y[-1]), lines, 0.0) < 0.0: tx_type = VendorCredit item_sign = -1 else: item_sign = 1 tx_type = Bill # see if the invoice number already exists query = tx_type.filter(DocNumber=invoice_num) if len(query) == 0: # create the bill bill = tx_type() bill.DocNumber = invoice_num else: bill = query[0] bill.TxnDate = qb_date_format(invoice_date) bill.VendorRef = supplier.to_ref() bill.PrivateNote = notes bill.DepartmentRef = None if not department else store_refs[department] if item_sign > 0: bill.SalesTermRef = supplier.TermRef bill.DueDate = None # clear the lines bill.Line = [] line_num = 1 for bill_line in lines: line = AccountBasedExpenseLine() line.AccountBasedExpenseLineDetail = AccountBasedExpenseLineDetail() line.AccountBasedExpenseLineDetail.AccountRef = bill_line[0] line.LineNum = line_num line.Id = line_num line.Amount = Decimal(atof( bill_line[2])).quantize(TWOPLACES) * item_sign line.Description = bill_line[1] line_num += 1 bill.Line.append(line) try: bill.save() except Exception as ex: print(bill.to_json()) print(ex)
def sync_third_party_deposit(supplier, deposit_date, notes, lines, department=None): refresh_session() store_refs = {x.Name: x.to_ref() for x in Department.all()} # check if one already exists query = Deposit.filter(TxnDate=qb_date_format(deposit_date)) for d in query: if Decimal(d.Line[0].Amount).quantize(TWOPLACES) == Decimal( atof(lines[0][2])).quantize(TWOPLACES): print("Already imported skipping {} {} {}".format( deposit_date, d.Line[0].Amount, lines[0][2])) return deposit = Deposit() deposit.TxnDate = qb_date_format(deposit_date) deposit.PrivateNote = notes deposit.DepartmentRef = None if not department else store_refs[department] deposit.DepositToAccountRef = wmc_account_ref(1010) line_num = 1 for deposit_line in lines: line = DepositLine() line.DepositLineDetail = DepositLineDetail() line.DepositLineDetail.AccountRef = wmc_account_ref( int(deposit_line[0])) line.DepositLineDetail.Entity = Vendor.filter( DisplayName=supplier)[0].to_ref() line.LineNum = line_num line.Id = line_num line.Amount = Decimal(atof(deposit_line[2])).quantize(TWOPLACES) line.Description = deposit_line[1] line_num += 1 deposit.Line.append(line) try: deposit.save() except Exception as ex: print(deposit.to_json()) print(ex)
def test_qb_date_format(self): result = qb_date_format(date(2016, 7, 22)) self.assertEqual(result, '2016-07-22')
def test_qb_date_format(self): result = qb_date_format(date(2016, 7, 22)) self.assertEquals(result, '2016-07-22')
def create_daily_sales(txdate, daily_reports): refresh_session() pattern = re.compile(r"\d+\.\d\d") store_refs = {x.Name: x.to_ref() for x in Department.all()} existing_receipts = { x.DepartmentRef.name if x.DepartmentRef else "20025": x for x in SalesReceipt.filter(TxnDate=qb_date_format(txdate)) } new_receipts = {} for store, sref in store_refs.items(): if store in existing_receipts: new_receipts[store] = existing_receipts[store] # clear old lines new_receipts[store].Line.clear() else: new_receipts[store] = SalesReceipt() for store, new_receipt in new_receipts.items(): if not (store in daily_reports): continue new_receipts[store].DepartmentRef = store_refs[store] new_receipt.TxnDate = qb_date_format(txdate) new_receipt.CustomerRef = Customer.all()[0].to_ref() daily_report = daily_reports[store] line_num = 1 amount_total = Decimal(0.0) for line_item, line_id in detail_map.items(): daily_report[line_item] line = SalesItemLine() line.LineNum = line_num line.Description = "{} imported from ({})".format( line_item, daily_report[line_item]) if daily_report[line_item]: if daily_report[line_item].startswith("N"): line.Amount = 0 else: line.Amount = atof( daily_report[line_item].strip("$")) * line_id[1] amount_total += Decimal(line.Amount) else: line.Amount = 0 line.SalesItemLineDetail = SalesItemLineDetail() item = Item.query("select * from Item where id = '{}'".format( line_id[0]))[0] line.SalesItemLineDetail.ItemRef = item.to_ref() line.SalesItemLineDetail.ServiceDate = None new_receipt.Line.append(line) line_num += 1 # Payin line = SalesItemLine() line.LineNum = line_num line_num += 1 line.Description = daily_report["Payins"].strip() if line.Description.count("\n") > 0: amount = Decimal(0) for payin_line in line.Description.split("\n")[1:]: if payin_line.startswith("TOTAL"): continue amount = amount + Decimal( atof(pattern.search(payin_line).group())) line.Amount = amount.quantize(TWOPLACES) amount_total += amount else: line.Amount = 0 line.SalesItemLineDetail = SalesItemLineDetail() item = Item.query("select * from Item where id = '{}'".format(43))[0] line.SalesItemLineDetail.ItemRef = item.to_ref() line.SalesItemLineDetail.ServiceDate = None new_receipt.Line.append(line) # Register Audit line = SalesItemLine() line.LineNum = line_num line_num += 1 line.Description = daily_report["Bank Deposits"].strip() # test if there was a recorded deposit if line.Description: line.Amount = Decimal(atof(line.Description.split()[4])) - Decimal( amount_total).quantize(TWOPLACES) else: line.Amount = 0 line.SalesItemLineDetail = SalesItemLineDetail() item = Item.query("select * from Item where id = '{}'".format(31))[0] line.SalesItemLineDetail.ItemRef = item.to_ref() line.SalesItemLineDetail.ServiceDate = None new_receipt.Line.append(line) new_receipt.PrivateNote = json.dumps(daily_report, indent=1) new_receipt.save() return