def saveOrderData(order, data): for d in data: if len(d) != 9: raise Exception() row = GlassRow.create(name=d[0], width=d[1], height=d[2], quantity=d[3], rate=d[4], unitPrice=d[5], rubEdge=d[6], extra=d[7], extraCost=d[8], orderId=order, rubEdgeUnitPrice=1,) logger.info("create a row " + str(row)) row.save()
def testGlassRow(self): order = Order(client="test-order", phone=0, date=datetime.now(), price=0, cash=0, status=0, note=""); order.save() orderInDb = Order.objects.get(id=order.id) self.assertEquals(order.id, orderInDb.id) print Order.objects.all() # test insert row row = GlassRow.create(name='testrow', quantity=2, unitPrice=45, orderId=order, width=162, height=517, rate=1.2, rubEdge='22', rubEdgeUnitPrice=1, extra="", extraCost=0) row.save() rowInDb = GlassRow.objects.get(id=row.id) self.assertEquals(row.id, rowInDb.id) print GlassRow.objects.all() # test cal width = 382 height = 1490 quantity = 2 unitPrice = 50 expectPrice = 74.292 row2 = GlassRow.create(name="testrow", quantity=quantity, unitPrice=unitPrice, orderId=order, width=width, height=height, rate=1.2, rubEdge="22", rubEdgeUnitPrice=0.8, extra="", extraCost=0) self.assertEquals(68.3016, row2.glassPrice) self.assertEquals(5.9904, row2.rubEdgeCost) self.assertTrue(expectPrice - row2.price < 0.01) #
def importData(dataStr): CLIENT = "家和" data = fixGlassCSV(dataStr) data = data.strip() lines = data.split("\n") lastLine = "" lastOrder = Order.create(CLIENT) lastOrder.save() for i in range(len(lines)): l = lines[i] logger.info(i) if not l or "\n" == l: if not lastLine: # two block line a raw continue # block line mean a new order order = Order.create(CLIENT) logger.info("create a new order"+str(i+1)) order.save() lastOrder = order else: #l = l.replace("\r\n", "") l = l.strip() data = l.split(",") if (len(data) != 6): raise Exception("data format wrong :" + data) mode = int(data[0]) glassType = data[1] width = float(data[2]) height = float(data[3]) quantity = int(data[4]) rub = data[5] unitPrice = findPriceByGlassType(glassType) if (mode!=0) else 0 glassName = getGlassNameByGlassType(glassType) row = GlassRow.create(name=glassName, quantity=quantity, unitPrice=unitPrice, orderId=lastOrder, width=width, height=height, rate=1.2, rubEdge=rub, rubEdgeUnitPrice=0.8, extra="", extraCost=0) logger.info("create a row " + str(row)) row.save() lastLine = l return lines