Commit fd681728 by guanghui.cui

折扣数据分摊到每个商品中

parent e0d8d887
...@@ -16,8 +16,9 @@ password=123 ...@@ -16,8 +16,9 @@ password=123
pos=bifengtang pos=bifengtang
#pos server ip #pos server ip
serverip=192.168.0.102 serverip=192.168.0.102
#商户从平台拿到百分比(总订单金额) #优惠类型
percent=90 discountTypeShop=600
discountTypePlantform=601
#守护进程脚本 #守护进程脚本
[SH] [SH]
......
...@@ -31,11 +31,14 @@ COrderInfo::COrderInfo() ...@@ -31,11 +31,14 @@ COrderInfo::COrderInfo()
strMonitorDeamon = ZIni::readString("SH","monitor", "",strIniPath.c_str()); strMonitorDeamon = ZIni::readString("SH","monitor", "",strIniPath.c_str());
strTakeoutDeamon = ZIni::readString("SH","takeout", "",strIniPath.c_str()); strTakeoutDeamon = ZIni::readString("SH","takeout", "",strIniPath.c_str());
_percent = ZIni::readInt("SYS","percent", 0,strIniPath.c_str()); _merchantDisId = ZIni::readInt("SYS","discountTypeShop", 0,strIniPath.c_str());
if(_percent<=0){ if(_merchantDisId<=0){
LOG(ERROR)<<"get percent failed,percent:"<<_percent; LOG(ERROR)<<"get discountTypeShop failed,discountTypeShop:"<<_merchantDisId;
}
_plantformFeeId = ZIni::readInt("SYS","discountTypePlantform", 0,strIniPath.c_str());
if(_plantformFeeId<=0){
LOG(ERROR)<<"get discountTypePlantform failed,discountTypePlantform:"<<_plantformFeeId;
} }
LOG(INFO)<<"percent is:"<<_percent;
} }
pthread_mutex_init(&mutex, NULL); //初始化互斥锁 pthread_mutex_init(&mutex, NULL); //初始化互斥锁
} }
...@@ -93,6 +96,15 @@ int COrderInfo::Init() ...@@ -93,6 +96,15 @@ int COrderInfo::Init()
} }
mysql_free_result(mysql->_result); mysql_free_result(mysql->_result);
} }
//添加优惠类型(平台服务费,商家优惠)
nErrCode = AddDiscountType();
if(nErrCode != EC_OK)
{
pthread_mutex_unlock(&mutex); //解锁
return EC_EX_SQL_FAILED;
}
nErrCode = AddFMTable(); nErrCode = AddFMTable();
CleanOldData(); CleanOldData();
pthread_mutex_unlock(&mutex); //解锁 pthread_mutex_unlock(&mutex); //解锁
...@@ -309,14 +321,42 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order) ...@@ -309,14 +321,42 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order)
} }
//订单商品写入“订单详情表” //订单商品写入“订单详情表”
float disRate=(order.service_fee*1.00)/(order.products_fee); //平台服务费率
float fBoxRateFee=disRate*(order.package_fee/100.00); //餐盒需要承担的平台服务费,由于没有餐盒这个商品,所以把这个费用分摊到其它商品中
bool bDisShop=false; //是否已经计算商户优惠金额
bool bBoxRateFee=false; //是否已经计算餐盒平台服务费
int iDisShopIndex=0; //写入商户优惠的商品索引
float fRateProShop=0.00; //使用商户优惠商品的折扣率;
int itemIndex=0; int itemIndex=0;
for(auto dish:vecDish){ for(auto dish:vecDish){
itemIndex++; itemIndex++;
float discount_product=0.00; //折扣金额
float price_product=dish.original_price/100.00; //商品单价 float price_product=dish.original_price/100.00; //商品单价
float discount_product=price_product*disRate; //折扣金额
float tot_product=dish.original_price*dish.consume_num/100.00; //商品总额 float tot_product=dish.original_price*dish.consume_num/100.00; //商品总额
std::string dish_name=""; std::string dish_name="";
int print1=0,print2=0,print3=0,print4=0,print5=0,cat=0,dept=0; int print1=0,print2=0,print3=0,print4=0,print5=0,cat=0,dept=0;
if(!bDisShop&&price_product>(discount_product+order.dis_shop_fee/100.00)){
discount_product+=order.dis_shop_fee/100.00;
iDisShopIndex=itemIndex;
fRateProShop=order.dis_shop_fee*1.00/dish.original_price;
bDisShop=true;
//写入商品折扣表
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE, "INSERT INTO tbl_DiscOnItem(`Date`,`Check`,Outlet, DiscIdx,ItemIdx,DiscTot) \
VALUES('%s',%d,%d,2,%d,%.2f)", strDate.data(),orderId,iOutlet,itemIndex,-order.dis_shop_fee/100.00);
nErrCode = mysql->insert();
if(nErrCode != EC_OK)
{
rollbackData(orderIdStr);
pthread_mutex_unlock(&mutex); //解锁
return EC_EX_SQL_FAILED;
}
}
if(!bBoxRateFee&&price_product>(discount_product+fBoxRateFee)){
discount_product+=fBoxRateFee;
bBoxRateFee=true;
}
//查询商品详细信息 //查询商品详细信息
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"SELECT Name3,PrintQ1,PrintQ2,PrintQ3,PrintQ4,PrintQ5,Category,Dept FROM tbl_Menu WHERE Item='%s';",dish.pid.data()); snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"SELECT Name3,PrintQ1,PrintQ2,PrintQ3,PrintQ4,PrintQ5,Category,Dept FROM tbl_Menu WHERE Item='%s';",dish.pid.data());
...@@ -344,8 +384,20 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order) ...@@ -344,8 +384,20 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order)
DiscBefore,UnitQty,Qty,ItemTot,RvDiscBefore,RvItemTot,Dept,Category,IsItemModified,LastModifiedTime,ParentItemIdx,ChildCount)\ DiscBefore,UnitQty,Qty,ItemTot,RvDiscBefore,RvItemTot,Dept,Category,IsItemModified,LastModifiedTime,ParentItemIdx,ChildCount)\
VALUES('%s',%d,%d,%d,'%s','%s','%s',%d,'%s',99,4,%d,%d,%d,%d,%d,'%s',4,'1',1,%.2f,%.2f,1,-1,%.2f,1,%d,%.2f,%.2f,%.2f,%d,%d,1,'%s',%d,%d)", \ VALUES('%s',%d,%d,%d,'%s','%s','%s',%d,'%s',99,4,%d,%d,%d,%d,%d,'%s',4,'1',1,%.2f,%.2f,1,-1,%.2f,1,%d,%.2f,%.2f,%.2f,%d,%d,1,'%s',%d,%d)", \
strDate.data(),orderId,iOutlet,itemIndex,dish.pid.data(),dish_name.data(),dish_name.data(),dish.type,chTime,print1,print2,print3,print4,\ strDate.data(),orderId,iOutlet,itemIndex,dish.pid.data(),dish_name.data(),dish_name.data(),dish.type,chTime,print1,print2,print3,print4,\
print5,dish.isPrintCheck.data(),price_product,price_product,discount_product,\ print5,dish.isPrintCheck.data(),price_product,price_product,-discount_product,\
dish.consume_num,tot_product,discount_product,tot_product,dept,cat,chTime,dish.index,dish.childCount); dish.consume_num,tot_product,-discount_product,tot_product,dept,cat,chTime,dish.index,dish.childCount);
nErrCode = mysql->insert();
if(nErrCode != EC_OK)
{
rollbackData(orderIdStr);
pthread_mutex_unlock(&mutex); //解锁
return EC_EX_SQL_FAILED;
}
//商品折扣列表
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE, "INSERT INTO tbl_DiscOnItem(`Date`,`Check`,Outlet, DiscIdx,ItemIdx,DiscTot) \
VALUES('%s',%d,%d,1,%d,%.2f)", \
strDate.data(),orderId,iOutlet,itemIndex,-price_product*disRate);
nErrCode = mysql->insert(); nErrCode = mysql->insert();
if(nErrCode != EC_OK) if(nErrCode != EC_OK)
{ {
...@@ -371,11 +423,11 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order) ...@@ -371,11 +423,11 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order)
//写入折扣信息表 //写入折扣信息表
//VALUES('2017-10-26',1234567,1390,1,7,'takeout discount','takeout discount','ÍâÂôÕÛ¿Û',2,0,15,'2017-10-26 18:23:08',99,2,'1',-0.1000,-20.08); //VALUES('2017-10-26',1234567,1390,1,7,'takeout discount','takeout discount','ÍâÂôÕÛ¿Û',2,0,15,'2017-10-26 18:23:08',99,2,'1',-0.1000,-20.08);
float disRate=(order.service_fee+order.dis_shop_fee*1.00)/order.products_fee; float dis_platform=(order.service_fee)/100.00; //折扣金额
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE, "INSERT INTO tbl_Disc(`Date`,`Check`,Outlet,DiscIdx,DiscNo,Name1,Name2,Name3,ItemIdx,StartLevel,EndLevel,ApplyTime,\ snprintf(mysql->sqlStr,SQL_BUFFER_SIZE, "INSERT INTO tbl_Disc(`Date`,`Check`,Outlet,DiscIdx,DiscNo,Name1,Name2,Name3,ItemIdx,StartLevel,EndLevel,ApplyTime,\
ApplyStation,ApplyEmp,IsDiscBefore,Rate,DiscTot)\ ApplyStation,ApplyEmp,IsDiscBefore,Rate,DiscTot)\
VALUES('%s',%d,%d,1,0,'takeout discount','takeout discount','ÍâÂôÕÛ¿Û',%d,0,15,'%s',99,2,'1',%.2f,%.2f)", \ VALUES('%s',%d,%d,1,%d,'plantform fee','plantform fee','ƽ̨·þÎñ·Ñ',%d,0,15,'%s',99,2,'1',%.2f,%.2f)", \
strDate.data(),orderId,iOutlet,itemIndex,chTime,-disRate,-discount); strDate.data(),orderId,iOutlet,_plantformFeeId,itemIndex,chTime,-disRate,-dis_platform);
LOG(INFO)<<"======disc info insert:"<<mysql->sqlStr; LOG(INFO)<<"======disc info insert:"<<mysql->sqlStr;
nErrCode = mysql->insert(); nErrCode = mysql->insert();
if(nErrCode != EC_OK) if(nErrCode != EC_OK)
...@@ -385,6 +437,23 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order) ...@@ -385,6 +437,23 @@ int COrderInfo::SetTakeawayOrder(TakeawayOrder &order)
return EC_EX_SQL_FAILED; return EC_EX_SQL_FAILED;
} }
if(order.dis_shop_fee>0){
float dis_shop=(order.service_fee)/100.00; //折扣金额
float shopRate=(order.dis_shop_fee*1.00)/order.products_fee;
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE, "INSERT INTO tbl_Disc(`Date`,`Check`,Outlet,DiscIdx,DiscNo,Name1,Name2,Name3,ItemIdx,StartLevel,EndLevel,ApplyTime,\
ApplyStation,ApplyEmp,IsDiscBefore,Rate,DiscTot)\
VALUES('%s',%d,%d,2,%d,'merchant discount','merchant discount','ÉÌ»§ÓÅ»Ý',%d,0,15,'%s',99,2,'1',%.2f,%.2f)", \
strDate.data(),orderId,iOutlet,_merchantDisId,iDisShopIndex,chTime,-fRateProShop,-dis_shop);
LOG(INFO)<<"======disc info insert:"<<mysql->sqlStr;
nErrCode = mysql->insert();
if(nErrCode != EC_OK)
{
rollbackData(orderIdStr);
pthread_mutex_unlock(&mutex); //解锁
return EC_EX_SQL_FAILED;
}
}
pthread_mutex_unlock(&mutex); //解锁 pthread_mutex_unlock(&mutex); //解锁
return 0; return 0;
} }
...@@ -468,6 +537,8 @@ int COrderInfo::AddFMTable() ...@@ -468,6 +537,8 @@ int COrderInfo::AddFMTable()
insert_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\ insert_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\
order_status INT DEFAULT 0)"); order_status INT DEFAULT 0)");
nErrCode = mysql->exec_proc(); nErrCode = mysql->exec_proc();
if(0!=nErrCode)
return nErrCode;
//添加fmTakeaway_products表 //添加fmTakeaway_products表
snprintf(mysql->sqlStr, SQL_BUFFER_SIZE,"CREATE TABLE IF NOT EXISTS fmTakeaway_products (\ snprintf(mysql->sqlStr, SQL_BUFFER_SIZE,"CREATE TABLE IF NOT EXISTS fmTakeaway_products (\
orderid VARCHAR(15) NOT NULL ,\ orderid VARCHAR(15) NOT NULL ,\
...@@ -475,7 +546,9 @@ int COrderInfo::AddFMTable() ...@@ -475,7 +546,9 @@ int COrderInfo::AddFMTable()
original_price INT,\ original_price INT,\
pid VARCHAR(20),\ pid VARCHAR(20),\
insert_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)"); insert_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)");
nErrCode = mysql->exec_proc(); nErrCode = mysql->exec_proc();
if(0!=nErrCode)
return nErrCode;
//设置“非码外卖按钮” //设置“非码外卖按钮”
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"UPDATE tbl_FuncType SET Name1='fmTakeout',Name2='·ÇÂëÍâÂô',Name3='·ÇÂëÍâÂô',Param1='%s' WHERE FuncNo=28;",strTakeoutDeamon.data()); snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"UPDATE tbl_FuncType SET Name1='fmTakeout',Name2='·ÇÂëÍâÂô',Name3='·ÇÂëÍâÂô',Param1='%s' WHERE FuncNo=28;",strTakeoutDeamon.data());
...@@ -519,32 +592,6 @@ void COrderInfo::SetTakewayTypes(std::vector<fmPOSType> &vecTakeway) ...@@ -519,32 +592,6 @@ void COrderInfo::SetTakewayTypes(std::vector<fmPOSType> &vecTakeway)
vecTakewayTypes.assign(vecTakeway.begin(), vecTakeway.end()); vecTakewayTypes.assign(vecTakeway.begin(), vecTakeway.end());
} }
char* COrderInfo::UTF8ToLatin1(const char *str)
{
if(_tempLatin1!=NULL){
delete[] _tempLatin1;
_tempLatin1=NULL;
}
iconv_t cd;
size_t inLen, outLen;
char *_str = (char*)str;
if (!str)
return NULL;
inLen = strlen(str)+1;
_tempLatin1=new char[inLen];
outLen = inLen;
cd = iconv_open("ISO-8859-1", "UTF-8");
size_t convertedLen = iconv(cd, &_str, &inLen, &_tempLatin1, &outLen);
LOG(INFO)<<"Converted Length:"<< convertedLen << "Error: " << errno;
iconv_close(cd);
LOG(INFO)<<"_tempLatin1:"<<_tempLatin1;
return _tempLatin1;
}
bool COrderInfo::GetMenuPrinter() bool COrderInfo::GetMenuPrinter()
{ {
LOG(INFO)<<"---------------------get printer service--------------------"; LOG(INFO)<<"---------------------get printer service--------------------";
...@@ -769,6 +816,52 @@ std::string COrderInfo::charset_u2g(const std::string& utf8) ...@@ -769,6 +816,52 @@ std::string COrderInfo::charset_u2g(const std::string& utf8)
return gbk; return gbk;
} }
int COrderInfo::AddDiscountType()
{
int nErrCode = EC_OK;
//添加商户优惠类型
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"SELECT * FROM tbl_DiscType where DiscNo=%d",_merchantDisId);
nErrCode = mysql->query();
if(nErrCode != EC_OK){
return EC_EX_SQL_FAILED;
}
if (NULL == (mysql->_row = mysql_fetch_row(mysql->_result))){
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"INSERT INTO tbl_DiscType(DiscNo,SeqNo,Name1,Name2,Name3,StartLevel,EndLevel,\
EmpDiscLevel,IsDisabled,`Type`,IsOpenDisc,IsDiscBefore,Rate,FixAmount)\
VALUES(%d,%d,'merchant discount','merchant discount','ÉÌ»§ÓÅ»Ý',0,99,9,0,1,'1','0',0,0)",\
_merchantDisId, _merchantDisId);
nErrCode = mysql->insert();
if(nErrCode != EC_OK)
{
LOG(ERROR) << "insert error"<<mysql->sqlStr;
return EC_EX_SQL_FAILED;
}
}
mysql_free_result(mysql->_result);
//添加平台服务费类型
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"SELECT * FROM tbl_DiscType where DiscNo=%d",_plantformFeeId);
nErrCode = mysql->query();
if(nErrCode != EC_OK){
return EC_EX_SQL_FAILED;
}
if (NULL == (mysql->_row = mysql_fetch_row(mysql->_result))){
snprintf(mysql->sqlStr,SQL_BUFFER_SIZE,"INSERT INTO tbl_DiscType(DiscNo,SeqNo,Name1,Name2,Name3,StartLevel,EndLevel,\
EmpDiscLevel,IsDisabled,`Type`,IsOpenDisc,IsDiscBefore,Rate,FixAmount)\
VALUES(%d,%d,'plantform fee','plantform fee','ƽ̨·þÎñ·Ñ',0,99,9,0,1,'1','0',0,0)",\
_plantformFeeId, _plantformFeeId);
nErrCode = mysql->insert();
if(nErrCode != EC_OK)
{
LOG(ERROR) << "insert error"<<mysql->sqlStr;
return EC_EX_SQL_FAILED;
}
}
mysql_free_result(mysql->_result);
return nErrCode;
}
//需要导出的函数,即用户在外部可以调用的接口 //需要导出的函数,即用户在外部可以调用的接口
bool GetOperationObject(void** _RtObject) bool GetOperationObject(void** _RtObject)
{ {
......
...@@ -89,7 +89,6 @@ private: ...@@ -89,7 +89,6 @@ private:
std::vector<std::string> vecSetMenu; //菜单中是套餐的项目 std::vector<std::string> vecSetMenu; //菜单中是套餐的项目
std::map<std::string, std::vector<SetMenuItem>> mapSetMenu; //套餐(key:套餐id、value:套餐条目) std::map<std::string, std::vector<SetMenuItem>> mapSetMenu; //套餐(key:套餐id、value:套餐条目)
char* UTF8ToLatin1(const char *str);
void CleanOldData(); //清理超过六个月数据 void CleanOldData(); //清理超过六个月数据
int charset_convert(const char* charset_from, const char* charset_to int charset_convert(const char* charset_from, const char* charset_to
...@@ -107,6 +106,7 @@ private: ...@@ -107,6 +106,7 @@ private:
//向数据库总添加:fmTemp表 | 触发器 | fmTakeaway表 | fmTakeaway_products表 //向数据库总添加:fmTemp表 | 触发器 | fmTakeaway表 | fmTakeaway_products表
int AddFMTable(); int AddFMTable();
void rollbackData(std::string orderid); void rollbackData(std::string orderid);
int AddDiscountType(); //添加优惠类型
pthread_mutex_t mutex; //互斥锁 pthread_mutex_t mutex; //互斥锁
MySQLService *mysql; MySQLService *mysql;
...@@ -114,7 +114,9 @@ private: ...@@ -114,7 +114,9 @@ private:
char* _tempLatin1; //utf8转换为Latin1后,存放在此变量中 char* _tempLatin1; //utf8转换为Latin1后,存放在此变量中
std::string strMonitorDeamon; //monitor守护进程脚本文件路径 std::string strMonitorDeamon; //monitor守护进程脚本文件路径
std::string strTakeoutDeamon; //takeout守护进程脚本文件路径 std::string strTakeoutDeamon; //takeout守护进程脚本文件路径
int _percent; //商户从平台拿到百分比(总订单金额)
int _merchantDisId; //优惠类型id(商户减免)
int _plantformFeeId; //优惠类型id(平台服务费)
}; };
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment