def __sum_car_charge(self, closing_date): # get charge da = ChargeDataAccess() key = da.get_key(closing_date) q2 = Charge.query(ancestor=key) q2 = q2.filter(Charge.void==False) charges = q2.fetch() # sum by car charge_cars = {} for charge in charges: charge_obj = None if charge_cars.has_key(charge.car_reg_no): charge_obj = charge_cars[charge.car_reg_no] else: charge_obj = {} charge_obj['car_reg_no'] = charge.car_reg_no charge_obj['tran_date'] = charge.tran_date charge_obj['amt'] = 0 charge_cars[charge.car_reg_no] = charge_obj charge_obj['amt'] += charge.sub_total return charge_cars
def __get_by_day(self, date_from, date_to, date_format='%Y%m%d'): # get charge q = Charge.query() if date_from: q = q.filter(Charge.tran_date >= date_from) if date_to: q = q.filter(Charge.tran_date <= date_to) q = q.filter(Charge.void==False) charges = q.order(Charge.tran_date).fetch() # sum amt charge_list = [] charge_days = {} for charge in charges: key = "%s" % (charge.tran_date.strftime(date_format)) charge_day = None if charge_days.has_key(key): charge_day = charge_days[key] else: charge_day = ChargeSummaryViewModel() charge_day.tran_date = charge.tran_date charge_days[key] = charge_day charge_list.append(charge_day) charge_day.sub_total += charge.sub_total charge_day.comm_amt += charge.comm_amt charge_day.amt += charge.amt return charge_list
def __sum_car_charge(self, closing_date): # get charge da = ChargeDataAccess() key = da.get_key(closing_date) q2 = Charge.query(ancestor=key) q2 = q2.filter(Charge.void == False) charges = q2.fetch() # sum by car charge_cars = {} for charge in charges: charge_obj = None if charge_cars.has_key(charge.car_reg_no): charge_obj = charge_cars[charge.car_reg_no] else: charge_obj = {} charge_obj['car_reg_no'] = charge.car_reg_no charge_obj['tran_date'] = charge.tran_date charge_obj['amt'] = 0 charge_cars[charge.car_reg_no] = charge_obj charge_obj['amt'] += charge.sub_total return charge_cars
def post(self): json_values = {} try: date_from = self.request.get('dateFrom') date_to = self.request.get("dateTo") lot_no = self.request.get("lotNo") car_reg_no = self.request.get("carPlate") current_attendant = self.current_attendant() attendant_code = current_attendant.code q = Charge.query(Charge.attendant_code == attendant_code) if date_from and len(date_from) > 0: date_from = DateTime.to_date(date_from) q = q.filter(Charge.tran_date >= date_from) if date_to and len(date_to) > 0: date_to = DateTime.to_date(date_to) q = q.filter(Charge.tran_date <= date_to) if lot_no: q = q.filter(Charge.lot_no == lot_no) if car_reg_no: q = q.filter(Charge.car_reg_no == car_reg_no) if (not date_from and date_to and lot_no and car_reg_no): q = q.filter(Charge.attendant_code == attendant_code) charges = q.fetch() # create json data = [] for charge in charges: data.append({ 'attendantCode': charge.attendant_code, 'tranCode': charge.tran_code, 'date': DateTime.to_date_string(charge.tran_date), 'lotNo': charge.lot_no, 'carPlate': charge.car_reg_no, 'amount': charge.sub_total, }) json_values['returnStatus'] = True json_values['data'] = data except Exception, ex: json_values['returnStatus'] = False json_values['returnMessage'] = str(ex)
def __get_by_day_and_attendant(self, date_from, date_to, attendant_code, date_format='%Y%m%d'): # get charge q = Charge.query() if date_from: q = q.filter(Charge.tran_date >= date_from) if date_to: q = q.filter(Charge.tran_date <= date_to) if attendant_code and len(attendant_code) > 0: q = q.filter(Charge.attendant_code==attendant_code) q = q.filter(Charge.void==False) charges = q.order(Charge.tran_date, Charge.attendant_code).fetch() # sum amt charge_attendants = [] charge_attendant_codes = {} for charge in charges: key = "%s|%s" % (charge.tran_date.strftime(date_format), charge.attendant_code) charge_attendant = None if charge_attendant_codes.has_key(key): charge_attendant = charge_attendant_codes[key] else: charge_attendant = ChargeSummaryViewModel() charge_attendant.tran_date = charge.tran_date charge_attendant.attendant_code = charge.attendant_code charge_attendant_codes[key] = charge_attendant charge_attendants.append(charge_attendant) charge_attendant.sub_total += charge.sub_total charge_attendant.comm_amt += charge.comm_amt charge_attendant.amt += charge.amt return charge_attendants
def get(self, car_reg_no, tran_date): # get bf amt bal_amt = 0 bal_start_date = None car_mv = CarMovement.query(CarMovement.movement_date <= tran_date - timedelta(days=1) ).order(-CarMovement.movement_date).get() if car_mv: bal_amt = car_mv.bal_amt bal_start_date = car_mv.movement_date + timedelta(days=1) # get trans trans = Tran.query( Tran.tran_date >= bal_start_date, Tran.car_reg_no==car_reg_no ).order(Tran.tran_date, Tran.seq).fetch() registers = Register.query( Register.tran_date >= bal_start_date, Register.car_reg_no==car_reg_no, Charge.void==False, ).fetch() top_ups = TopUp.query( TopUp.tran_date >= bal_start_date, TopUp.car_reg_no==car_reg_no, Charge.void==False, ).fetch() charges = Charge.query( Charge.tran_date >= bal_start_date, Charge.car_reg_no==car_reg_no, Charge.void==False, ).fetch() # group tran by tran_code mix_tran_codes = {} for register in registers: mix_tran_codes[register.tran_code] = register for top_up in top_ups: mix_tran_codes[top_up.tran_code] = top_up for charge in charges: mix_tran_codes[charge.tran_code] = charge # ppl records return_values = [] # bf tran_bf_vm = StatementViewModel() tran_bf_vm.tran_date = bal_start_date tran_bf_vm.description = "B/F" tran_bf_vm.bal_amt = bal_amt return_values.append(tran_bf_vm) for tran in trans: tran_vm = StatementViewModel() tran_vm.bf_amt = bal_amt tran_vm.tran_date = tran.tran_date tran_vm.tran_code = tran.tran_code tran_vm.tran_type = tran.tran_type tran_vm.car_reg_no = tran.car_reg_no if mix_tran_codes.has_key(tran.tran_code): mix_tran = mix_tran_codes[tran.tran_code] if tran.tran_type == Tran.TRAN_TYPE_REGISTER: tran_vm.description = 'Register' tran_vm.db_amt = mix_tran.sub_total elif tran.tran_type == Tran.TRAN_TYPE_TOP_UP: tran_vm.description = 'Top Up' tran_vm.db_amt = mix_tran.sub_total elif tran.tran_type == Tran.TRAN_TYPE_CHARGE: tran_vm.description = 'Charge' tran_vm.cr_amt = mix_tran.sub_total tran_vm.cal_bal_amt() bal_amt = tran_vm.bal_amt return_values.append(tran_vm) return return_values
def create(self, vm, charge_time=None): try: if charge_time is None: vm.charge_time = DateTime.malaysia_now() else: vm.charge_time = charge_time self.validate_tran_date(vm) self.validate_closing(vm) self.__validate_attendant_code(vm) self.__validate_lot_no(vm) self.__validate_car_reg_no(vm) # get attendance comm attendant_da = AttendantDataAccess() attendant = attendant_da.get(vm.attendant_code) if attendant is None: raise Exception('Attendant not found.') vm.comm_per = attendant.comm_per # get system setting system_da = SystemSettingDataAccess() system_setting = system_da.get() # save charge da = ChargeDataAccess() # get charge q = Charge.query(ancestor=da.get_key(vm.tran_date)) q = q.filter(Charge.lot_no == vm.lot_no, Charge.car_reg_no == vm.car_reg_no, Charge.ended == False, Charge.void == False) charge = q.get() if charge is None: # new charge vm.start_time = vm.charge_time vm.last_charge_time = None else: # continue old charge vm.tran_code = charge.tran_code vm.start_time = charge.start_time vm.last_charge_time = charge.charge_time # if idle duration > 2hrs, end this charge # and create a new charge idle_duration = vm.idle_duration() if idle_duration > system_setting.reset_duration: # end charge da.end(vm) # create new charge vm.tran_code = '' vm.start_time = vm.charge_time vm.last_charge_time = None vm.cal_duration() vm.cal_sub_total() vm.cal_comm_amt() vm.cal_amt() if len(vm.tran_code) < 1: da.create(vm) else: da.update_charge(vm) except Exception, ex: audit_da = AttendantAuditTrailDataAccess() audit_da.create(vm.attendant_code, 'Create Charge', 'Fail. Error=%s' % str(ex)) raise ex
def __create(self, vm): # get master seq master_da = MasterDataAccess() master = master_da.get('Charge') master.seq += 1 master.put() # insert deposit tran_code = Charge.get_tran_code(master.seq) vm.tran_code = tran_code data = Charge(parent=self.get_key(vm.tran_date, vm.attendant_code), id=tran_code) data.tran_code = tran_code data.tran_type = vm.tran_type data.tran_date = vm.tran_date data.seq = master.seq data.remark = vm.remark data.attendant_code = vm.attendant_code data.attendant = vm.attendant.key data.lot_no = vm.lot_no data.car_reg_no = vm.car_reg_no data.car = vm.car.key data.start_time = vm.start_time data.charge_time = vm.charge_time data.duration = vm.duration data.sub_total = vm.sub_total data.comm_per = vm.comm_per data.comm_amt = vm.comm_amt data.amt = vm.amt data.ended = False data.created_by = '' data.created_date = DateTime.malaysia_now() data.modified_by = '' data.modified_date = None data.void_by = '' data.void_date = None data.void = False data.last_modified = str(data.created_date) data.put() # insert tran tran_obj = TranViewModel() tran_obj.tran_code = data.tran_code tran_obj.tran_date = data.tran_date tran_obj.tran_type = data.tran_type tran_obj.car_reg_no = data.car_reg_no tran_da = TranDataAccess() tran_da.create(tran_obj) # update car bal amt self.__update_car_bal_amt(vm.car_reg_no, vm.sub_total)
def fetch(self, tran_date, attendant_code=None, tran_code=None): key = self.get_key(tran_date, attendant_code, tran_code) return Charge.query(ancestor=key).fetch()
def get(self, tran_date, attendant_code, tran_code): key = self.get_key(tran_date, attendant_code, tran_code) return Charge.query(ancestor=key).get()
def post(self, reg_no): json_values = {} try: reg_no = self.request.get('reg_no') cars = Car.query(Car.reg_no == reg_no).fetch() car = cars[0] today_date = DateTime.malaysia_today() trans = Tran.query(Tran.car_reg_no == reg_no, Tran.tran_date == today_date).order( Tran.seq).fetch() registers = Register.query( Register.car_reg_no == reg_no, Register.tran_date == today_date).fetch() topups = TopUp.query(TopUp.car_reg_no == reg_no, TopUp.tran_date == today_date).fetch() charges = Charge.query(Charge.car_reg_no == car.reg_no, Charge.tran_date == today_date).fetch() # group by tran_code register_tran_codes = {} for register in registers: register_tran_codes[register.tran_code] = register topup_tran_codes = {} for topup in topups: topup_tran_codes[topup.tran_code] = topup charge_tran_codes = {} for charge in charges: charge_tran_codes[charge.tran_code] = charge # create json data = [] bal_amt = 0 for tran in trans: if register_tran_codes.has_key(tran.tran_code): register = register_tran_codes[tran.tran_code] bal_amt += register.sub_total bal_amt = round(bal_amt, 2) data.append({ 'carRegNo': car.reg_no, 'date': DateTime.to_date_string(register.tran_date), 'description': register.tran_code, 'dbAmt': register.sub_total, 'crAmt': 0, 'balAmt': bal_amt }) elif topup_tran_codes.has_key(tran.tran_code): topup = topup_tran_codes[tran.tran_code] bal_amt += topup.sub_total bal_amt = round(bal_amt, 2) data.append({ 'carRegNo': car.reg_no, 'date': DateTime.to_date_string(topup.tran_date), 'description': topup.tran_code, 'dbAmt': topup.sub_total, 'crAmt': 0, 'balAmt': bal_amt }) elif charge_tran_codes.has_key(tran.tran_code): charge = charge_tran_codes[tran.tran_code] bal_amt -= charge.sub_total bal_amt = round(bal_amt, 2) data.append({ 'carRegNo': car.reg_no, 'date': DateTime.to_date_string(charge.tran_date), 'description': charge.tran_code, 'dbAmt': 0, 'crAmt': charge.sub_total, 'balAmt': bal_amt }) json_values['returnStatus'] = True json_values['data'] = data except Exception, ex: json_values['returnStatus'] = False json_values['returnMessage'] = str(ex)
def create(self, vm, charge_time=None): try: if charge_time is None: vm.charge_time = DateTime.malaysia_now() else: vm.charge_time = charge_time self.validate_tran_date(vm) self.validate_closing(vm) self.__validate_attendant_code(vm) self.__validate_lot_no(vm) self.__validate_car_reg_no(vm) # get attendance comm attendant_da = AttendantDataAccess() attendant = attendant_da.get(vm.attendant_code) if attendant is None: raise Exception('Attendant not found.') vm.comm_per = attendant.comm_per # get system setting system_da = SystemSettingDataAccess() system_setting = system_da.get() # save charge da = ChargeDataAccess() # get charge q = Charge.query(ancestor=da.get_key(vm.tran_date)) q = q.filter(Charge.lot_no==vm.lot_no, Charge.car_reg_no==vm.car_reg_no, Charge.ended==False, Charge.void==False) charge = q.get() if charge is None: # new charge vm.start_time = vm.charge_time vm.last_charge_time = None else: # continue old charge vm.tran_code = charge.tran_code vm.start_time = charge.start_time vm.last_charge_time = charge.charge_time # if idle duration > 2hrs, end this charge # and create a new charge idle_duration = vm.idle_duration() if idle_duration > system_setting.reset_duration: # end charge da.end(vm) # create new charge vm.tran_code = '' vm.start_time = vm.charge_time vm.last_charge_time = None vm.cal_duration() vm.cal_sub_total() vm.cal_comm_amt() vm.cal_amt() if len(vm.tran_code) < 1: da.create(vm) else: da.update_charge(vm) except Exception, ex: audit_da = AttendantAuditTrailDataAccess() audit_da.create(vm.attendant_code, 'Create Charge', 'Fail. Error=%s' % str(ex)) raise ex