Source

wd_pys60 / wd_jin_xiao_cun.py

Full commit
# -*- coding: utf-8 -*-
VERSION="1.0.1"

import sys
sys.setdefaultencoding("utf8")

import e32dbm
import random
import audio
import time

import appuifw,globalui
from graphics import *
import e32
from key_codes import *

cn=lambda text : text.decode("utf8")
HELP_CONTENT="\
\n→右方向键 出售\
\n←右方向键 进货\
\n1键 上一页\
\n3键 上一页\
\n0键 首页\
\n5键 根据编号定位\
\n中键 编辑价格和名字"

class JinXiaoCun(object):
    FORMAT='"%s,%d,%d,%d"%(self.name,self.price,self.cun,self.cun_jia)'
    DB_FILE="e:\\jin_xiao_cun"
    DB = e32dbm.open(DB_FILE, 'c')
    KEYS=DB.keys()
    def __init__(self,ID=None,name=None,cun=None,price=None):
        #print self.KEYS
        self.ID=str(ID)
        if name:
            self.update(name,price,cun)
        self.name,self.price,self.cun,self.cun_jia=self.read()
        if self.ID not in self.KEYS:
            self.KEYS.append(self.ID)
    def update(self,name,price,cun):
        try:
            self.name,self.price,self.cun=name,long(price),long(cun)
            self.cun_jia=self.cun*self.price
            self.write()
        except Exception,m:
            print m
    def jin(self,jin):
        self.cun+=jin
        self.cun_jia=self.cun*self.price
        self.write()
    def xiao(self,xiao):
        self.cun-=xiao
        self.cun_jia=self.cun*self.price
        self.write()
    def write(self):
        con= eval(self.FORMAT)
        self._write(self.ID,con)
    def read(self):
        try:
            con=self._read(self.ID).split(",")
            name=con[0]
            price=long(con[1])
            cun=long(con[2])
            cun_jia=long(con[3])
            return name,price,cun,cun_jia    
        except Exception,m:
            #print Exception,m
            return "None",1,1,1
    
    def get_name(self):
        return self.read()[0]
    def get_price(self):
        return self.read()[1]
    def get_cun(self):
        return self.read()[2]
    def _write(self,key,value):
        try:
            self.DB[key]=value
            #print value
            #print repr(value)
        except:
            print value
            print repr(value)
    def _read(self,key):
        try:
            return self.DB[key]
        except Exception,m:
            #print Exception,m
            return "none,1,1,1"
    def __repr__(self):
        #print eval(self.FORMAT)
        return "%3s|%4d|%4d|%7d|%15s"%(self.ID,self.price,self.cun,self.cun_jia,self.name)
    def say_str(self):
        #print eval(self.FORMAT)
        return "存货量为%d\n存价格为%d"%(self.cun,self.cun_jia)


class Ui(object):
    """
    
    """
    msg=cn("信息栏,左键菜单,右键退出")
    data=None#data to show in a table 
    row=0#row number right now
    page=0#page number right now
    center=(120,160)
    rowsPerPage=15
    max_id=max([int(k) for k in JinXiaoCun.KEYS] or [1])
    max_page=max_id/rowsPerPage
    def __init__(self,center=None):
        #print self.all
        appuifw.app.screen='full'
        self.img=None
        self.myfont=u'Sans MT 936_S60'
        self.running=1
        self.canvas=appuifw.Canvas(
            event_callback=self._handle_event,
            redraw_callback=self._handle_redraw)
        appuifw.app.body=self.canvas
        self.img=Image.new(self.canvas.size)
        appuifw.app.exit_key_handler = lambda :self.quit()
        appuifw.app.title = cn("进销存")
        appuifw.app.menu=[
            (cn("销售/出货(右方向键)"), self.xiao),
            (cn("进货(左方向键)"), self.jin),
            (cn("上一页(1键)"), self.previouspage),
            (cn("下一页(3键)"), self.nextpage),
            (cn("首页(0键)"), self.frontpage),
            (cn("编辑(中键)"), self.modify),
            (cn("根据编号定位(5键)"), self.get_by_id),
            (cn("同步()"), self.sync_to_gae),
            (cn("帮助"), self.display_help),
            ]
        appuifw.app.body.bind(55,lambda:self.quit())
    def quit(self):
        if appuifw.query(cn("确定退出?"),"query"):
            self.running=0
            appuifw.app.set_exit()
    def _handle_redraw(self,rect):
        if self.img:
            #print self.row
            #self.msg=cn(str(self.row))
            self.img.clear(0xffffff)
            width=300/self.rowsPerPage
            pos=(self.row%self.rowsPerPage)*width+width/2
            self.img.line(((0,pos),(240,pos)),0xcccccc,width=width)
            self.img.line(((0,312),(240,312)),0xdedede,width=width)
            self.img.text((0,318),self.msg,0x000000,self.myfont)
            for i in range(self.page*self.rowsPerPage,(self.page+1)*self.rowsPerPage):
                try:
                    self.img.text((0,(i%self.rowsPerPage)*width+width),unicode(cn(repr(JinXiaoCun(i)))),0x000000,self.myfont)
                except Exception,l:
                    #print Exception,l
                    self.img.text((0,(i%self.rowsPerPage)*width+width),unicode(cn("%d|未定义"%i)),0x00ff00,self.myfont)
                    
            self.canvas.blit(self.img)
    def _handle_event(self,event):
        #print event
        # keyboard_state={}
        if event['type'] == appuifw.EEventKey:
            #print event['scancode']
            if event['scancode']==14:
                self.jin()
            elif event['scancode']==16:
                self.up()
                #print "up"
            elif event['scancode']==17:
                self.down()
            elif event['scancode']==15:
                self.xiao()
            elif event['scancode']==EKey1:
                self.previouspage()
            elif event['scancode']==EKey3:
                self.nextpage()
            elif event['scancode']==EKey0:
                self.frontpage()
            elif event['scancode']==EKey2:
                audio.say(JinXiaoCun(self.row).say_str())
            elif event['scancode']==167:
                self.modify()
            elif event['scancode']==EKey5:
                self.get_by_id()
            elif event['scancode']==EKey6:
                self.display_help()
        self._handle_redraw(())
    def display_help(self):
        globalui.global_msg_query(cn(HELP_CONTENT),cn("帮助"))
    def sync_to_gae(self):
        self.csv_output()
        csv=file("e:\\jin_xiao_cun_t.csv","r")
        import urllib,urllib2
        data=urllib.urlencode({"content":"test\ntest"})
        data=csv.read().replace("\n","\\n")
        csv.close()
        print data
        urllib2.urlopen("http://wd-database-gae.weakdancer.com/write?type=string&key_name=jxc&content=%s"%cn(data))
        
    def csv_output(self):
        csv=file("e:\\jin_xiao_cun_t.csv","w+")
        print >> csv,"编号,名称,单价,存货量,总价"
        JinXiaoCun.KEYS=JinXiaoCun.DB.keys()
        self.max_id=max([int(k) for k in JinXiaoCun.KEYS] or [1])
        for i in range(self.max_id):
            print >> csv,"%s,"%i+JinXiaoCun(i)._read(str(i))
        csv.close()
    def get_by_id(self):
        value = appuifw.query(cn("货物编号ID"), "number")
        if value:
            if value<=self.max_id:
                self._set_id(value)
    def _set_id(self,value):
        self.row=value%self.rowsPerPage
        self.page=value/self.rowsPerPage
    def _get_id(self):
        return self.row
    def run(self):
        self._handle_redraw(())
        while self.running:
            e32.ao_sleep(0.01)
            e32.ao_yield()
    def frontpage(self):
        self.page=0
        self.row=0
    def previouspage(self):
        self.page-=1
        if self.page<0:
            self.page=0
        self.row=self.page*self.rowsPerPage
    def nextpage(self):
        self.page+=1
        if self.page>self.max_page+1:
            self.page=self.max_page+1
        self.row=self.page*self.rowsPerPage
    def up(self):
        if self.row%self.rowsPerPage==0:
            self.row=(self.page+1)*self.rowsPerPage-1
        else:
            self.row-=1
    def down(self):
        self.row+=1
        if self.row>(self.page+1)*self.rowsPerPage-1:
            self.row=self.page*self.rowsPerPage
    def jin(self):
        #jin huo
        jxc=JinXiaoCun(self.row)
        value = appuifw.query(cn("进货数量"), "number")
        if value:
            jxc.jin(value)
            self.msg=cn("编号%d进货:%s(上次操作)"%(self.row,value))
        pass
    def xiao(self):
        #xiao shou
        jxc=JinXiaoCun(self.row)
        value = appuifw.query(cn("出货数量"), "number")
        if value:
            jxc.xiao(value)
            self.msg=cn("编号%d出货:%s(上次操作)"%(self.row,value))
        pass
    def modify(self):
        #xiao shou
        jxc=JinXiaoCun(self.row)
        price = appuifw.query(cn("货物单价"), "number",int(jxc.get_price()))
        name = appuifw.query(cn("货物名称(必须以数字结束)"), "text",unicode(jxc.get_name(),"utf8"))
        #name="她们0"
        cun = appuifw.query(cn("存货量"), "number",int(jxc.get_cun()))
        if name and price and cun:
            jxc.update(name.encode("utf8"),long(price),long(cun))
        
def createTestData():
    f=file("e:\\uuu.txt","w+")
    for i in range(2,112):
        print >> f,"%s,%d,%d"%("窗帘"+str(i),random.randint(1,i*4),random.randint(1,i*9))
    f.close()

def test():
    #createTestData()
    print "#"*15
    t=time.time()
    data=f=file("e:\\jxc.csv","r").read().split("\n")[:-2]
    for d in data:
        #print d
        #print d.split(",")
        jxc=JinXiaoCun(data.index(d),*d.split(","))
        #print repr(jxc)
    print time.time()-t
#test()


ui=Ui()
ui.run()