数据库版本:mysql5.6, 表皆为InnoDB,事务操作内容:售卖信息提交,同步库存表和财务流水表。
public String save(@Param("..") final SundryIncome sundryIncome, final HttpSession session, final String saveType, final String receiptAwlId) {
if (sundryIncome == null) {
return "0";
}
try {
Trans.exec(Connection.TRANSACTION_SERIALIZABLE, new Atom() {
public void run() {
Manager manager = (Manager) session.getAttribute(UserSession.USER_SESSION);
StudentInfo stuinfo = dao.fetch(StudentInfo.class, sundryIncome.getStudentUuid());
SundryIncome _f0 = getLastedSundryIncome(manager.getSchoolUuid());// 最近一条sundryIncome,取得dateSequenceId
String dateSequence = null;
if (_f0 == null) {
dateSequence = "";
} else {
dateSequence = _f0.getDateSequence();
}
DateSequenceGenerator g = new DateSequenceGenerator();
// 生成新的日期串号
String next_dateSequence = g.getNextDateSequence(DateSequenceGenerator.PREFIX_TYPE_SUNDRYFEE, dateSequence);// 日期串号前缀为:ZF
if (stuinfo == null) {
TeacherInfo teainfo = dao.fetch(TeacherInfo.class, sundryIncome.getStudentUuid());
if (teainfo == null) {// 如果学生信息没有,则去教师信息表查,可能是自销
throw new NullPointerException("不存在的学生信息或教师信息,请重新录入!!");
} else {
sundryIncome.setTeacherUuid(teainfo.getUuid());
sundryIncome.setTeacherName(teainfo.getName());
sundryIncome.setMemo(sundryIncome.getMemo() + "[自销]");
sundryIncome.setClassUuid("-NONE-");
sundryIncome.setClassName("-NONE-");
}
} else {
sundryIncome.setTeacherUuid(stuinfo.getTeacherUuid());
sundryIncome.setTeacherName(stuinfo.getTeacherName());
sundryIncome.setClassUuid(stuinfo.getClassUuid());
sundryIncome.setClassName(stuinfo.getClassName());
}
sundryIncome.setDateSequence(next_dateSequence);// 新的日期串号
sundryIncome.setSchoolName(manager.getSchoolName());
sundryIncome.setSchoolUuid(manager.getSchoolUuid());
sundryIncome.setAccountant(manager.getRealName());
sundryIncome.setAccountantUuid(manager.getRealNameUuid());
sundryIncome.setCreateTime(new Date(new java.util.Date().getTime()));
// savetype==1:出票
if ("1".equals(saveType)) {
sundryIncome.setIsIssued((byte) 1);// 是否出票,值为1,表示已出票
sundryIncome.setIssuedDate(new Date(new java.util.Date().getTime()));
ReceiptBox rbox = new ReceiptBox();
String uuid = null;
if (StringUtils.isBlank(receiptAwlId)) {
uuid = UUID.randomUUID().toString();
} else {
// 出票并打印,这时发送来一个receiptAwlId,uuid无须自动生成
uuid = receiptAwlId;
}
rbox.setAwlId(uuid);
sundryIncome.setReceiptAwlId(uuid);
rbox.setTransactionType(CONST.RECEIPT_TRANSACTION_SUNDRY_TYPE);// 票据注明:杂费收入
ReceiptBox lastReceiptBox = getLastedReceiptBox(manager.getSchoolUuid());
String receiptBox_dateSequence = (lastReceiptBox == null) ? "" : lastReceiptBox.getDateSequence();
String receiptBox_next_dateSequence = g.getNextDateSequence(DateSequenceGenerator.PREFIX_TYPE_TUITIONFEE, receiptBox_dateSequence);
rbox.setDateSequence(receiptBox_next_dateSequence);
// sundryIncome.setReceiptBoxDateSequence(receiptBox_next_dateSequence);
rbox.setAccountant(manager.getRealName());
rbox.setAccountantUuid(manager.getRealNameUuid());
rbox.setStudentName(sundryIncome.getStudentName());
rbox.setStudentUuid(sundryIncome.getStudentUuid());
rbox.setSchoolName(manager.getSchoolName());
rbox.setSchoolUuid(manager.getSchoolUuid());
rbox.setAmount(sundryIncome.getAmount());// 票据总额
rbox.setMemo(sundryIncome.getMemo());
List<SundryIncome> sundryincomes = new ArrayList<SundryIncome>(1);
sundryincomes.add(sundryIncome);
rbox.setJsonContent(Json.toJson(sundryincomes));// 把票据项目作为json字串存储
Long paramTime = new java.util.Date().getTime();
rbox.setCreateTime(new Date(paramTime));
rbox.setIssuedReceiptDate(new java.sql.Date(paramTime));// 出票日期
rbox.setIssuedReceiptTime(new java.sql.Time(paramTime));// 出票时间
dao.insert(rbox);// 存储票据信息
}
dao.insert(sundryIncome);// 保存杂费数据
// >>>>>>>>>>>>>>>库存要减
Inventory inventoryItem = dao.fetch(Inventory.class, sundryIncome.getInventoryId());
if (inventoryItem != null) {
Double nowQuantity = inventoryItem.getQuantity();// 现在的库存量
Double saleQuantity = sundryIncome.getTotal();// 正在售出的数量
BigDecimal _b_nowQuantity = new BigDecimal(nowQuantity);// 现在的库存量bigDecimal类型
BigDecimal _b_saleQuantity = new BigDecimal(saleQuantity);// 正在售出的数量bigDecimal类型
Double newQuantity = _b_nowQuantity.subtract(_b_saleQuantity).doubleValue();// 现在的库存减去售出的数量=新的库存量
if (newQuantity < 0) {
try {
throw new Exception("库存不足");
} catch (Exception e) {
e.printStackTrace();
}
}
inventoryItem.setQuantity(newQuantity);// 更新库存量
inventoryItem.setUpdateTime(new Date(new java.util.Date().getTime()));// 库存本项目更新日期
dao.update(inventoryItem);
} else {
try {
throw new Exception("没有找到相应的库存!SundryIncomeAction.save(...)");
} catch (Exception e) {
e.printStackTrace();
}
}
// >>>>>>>>>>>>>>>>同步数据到t_journalaccount
if (sundryIncome.getIsSyn() == 1) {
try {
synToJournalAccount(manager, sundryIncome);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
} catch (Exception e) {
return e.toString();
}
return "1";
}