新版订单消耗系统

OrderCost.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. use App\RedisModel;
  6. class OrderCost extends Model
  7. {
  8. protected $table = 'order_cost';
  9. public $timestamps = false;
  10. const ORDER_COST_MONTH_PRE = 'OrderConsumption.OrderCostMonthRds_';
  11. const ORDER_OFFLINE_MONTH_PRE = 'OrderConsumption.OrderOfflineMonthRds_';
  12. const COLUMNS = [
  13. 'task_id',
  14. 'order_part_id',
  15. 'month',
  16. 'cost',
  17. 'traffic_boost_money',
  18. 'traffic_boost_refund',
  19. 'traffic_boost_sum',
  20. 'top_fee',
  21. 'actual_cost_sum',
  22. 'estimate_cost',
  23. 'estimate_traffic_boost_refund',
  24. 'cost_invoice_date',
  25. 'confirmed_cost_includes_tax',
  26. 'confirmed_cost',
  27. 'cost_owner',
  28. 'offline_subsidy_company',
  29. 'offline_subsidy_amount',
  30. 'rebate',
  31. 'offline_invoice_date',
  32. 'offline_confirmed_cost_includes_tax',
  33. 'offline_confirmed_cost',
  34. 'offline_celeb_name',
  35. 'offline_pay_date',
  36. 'ratio_of_consumption_to_top_rebate',
  37. 'ratio_of_consumption_to_top_income',
  38. 'final_ratio_of_consumption_to_top_rebate',
  39. 'final_ratio_of_consumption_to_top_income',
  40. 'flow_boost_rebate_ratio',
  41. 'flow_boost_rebate_income',
  42. 'final_flow_boost_rebate_ratio',
  43. 'final_flow_boost_rebate_income',
  44. 'red_invoice_date',
  45. 'refund_invoice_date',
  46. 'estimate_rebate_total',
  47. 'rebate_total',
  48. 'document_number_second',
  49. 'document_number_third',
  50. 'confirmation_certificate_third',
  51. 'document_number_four',
  52. 'confirmation_certificate_four',
  53. 'document_number_five',
  54. 'document_number_six',
  55. 'confirmation_certificate_five',
  56. 'confirmation_certificate_six',
  57. 'order_id',
  58. 'if_public',
  59. 'is_pdd_refund',
  60. 'cost_verify_status',
  61. 'note',
  62. 'is_cash',
  63. 'cost_sign_company'
  64. ];
  65. /**
  66. * 生成制定月份缓存
  67. */
  68. public static function setCostRds($month)
  69. {
  70. $sql = "select oc.*,
  71. oi.company,
  72. oi.advertiser_account,
  73. oi.advertiser_id,
  74. oi.salesman,
  75. oi.saler_id,
  76. oi.handler,
  77. oi.handlers_id,
  78. oi.customer_name,
  79. oi.final_customer_name,
  80. oi.origin_customer_name,
  81. oi.project_name,
  82. oi.launch_start_date,
  83. oi.launch_end_date,
  84. oi.launch_platform,
  85. oi.media_name,
  86. oi.cost_company_name,
  87. oi.cost_owner,
  88. oi.amount,
  89. oi.internet_celebrity_name,
  90. oi.internet_celebrity_id
  91. from order_cost as oc
  92. left join order_income oi on oi.order_id = oc.order_id
  93. where oc.enable=1 and oc.month='{$month}'
  94. order by oc.id desc ";
  95. $data = DB::select($sql);
  96. $data = json_decode(json_encode($data), true);
  97. #数据处理
  98. foreach($data as &$v){
  99. $v['task_id'] .= '';
  100. //处理超长小数百分比显示问题
  101. $v['flow_boost_rebate_ratio'] = Order::longDecimalFormat($v['flow_boost_rebate_ratio']);
  102. $v['ratio_of_consumption_to_top_rebate'] = Order::longDecimalFormat($v['ratio_of_consumption_to_top_rebate']);
  103. $v['final_flow_boost_rebate_ratio'] = Order::longDecimalFormat($v['final_flow_boost_rebate_ratio']);
  104. $v['final_ratio_of_consumption_to_top_rebate'] = Order::longDecimalFormat($v['final_ratio_of_consumption_to_top_rebate']);
  105. $v['month'] = substr($v['month'], 0, 7);
  106. $v['if_public'] = $v['if_public'] == 1 ? '是' : '';
  107. $v['is_pdd_refund'] = $v['is_pdd_refund'] == 1 ? '是' : '';
  108. }
  109. $rk = static::ORDER_COST_MONTH_PRE . $month;
  110. RedisModel::set($rk, json_encode($data));
  111. RedisModel::expire($rk, 86400);
  112. return $data;
  113. }
  114. /**
  115. * 生成制定月份缓存线下报表
  116. */
  117. public static function setOfflineRds($month)
  118. {
  119. $sql = "select oc.id,
  120. oc.task_id,
  121. oc.month,
  122. oc.order_part_id,
  123. oc.order_id,
  124. oc.offline_subsidy_company,
  125. oc.offline_subsidy_amount,
  126. oc.offline_celeb_name,
  127. oc.offline_pay_date,
  128. oc.status,
  129. oc.cost_verify_status,
  130. oi.company,
  131. oi.advertiser_account,
  132. oi.advertiser_id,
  133. oi.salesman,
  134. oi.saler_id,
  135. oi.handler,
  136. oi.handlers_id,
  137. oi.customer_name,
  138. oi.final_customer_name,
  139. oi.origin_customer_name,
  140. oi.project_name,
  141. oi.launch_start_date,
  142. oi.launch_end_date,
  143. oi.launch_platform,
  144. oi.media_name,
  145. oi.cost_company_name,
  146. oi.cost_owner,
  147. oi.amount,
  148. oi.internet_celebrity_name,
  149. oi.internet_celebrity_id
  150. from order_cost as oc
  151. left join order_income oi on oi.order_id = oc.order_id
  152. where oc.enable=1 and oc.month='{$month}' and offline_subsidy_amount>0
  153. order by oc.id desc ";
  154. $data = DB::select($sql);
  155. $data = json_decode(json_encode($data), true);
  156. #数据处理
  157. foreach($data as &$v){
  158. $v['task_id'] .= '';
  159. //处理超长小数百分比显示问题
  160. $v['month'] = substr($v['month'], 0, 7);
  161. }
  162. $rk = static::ORDER_OFFLINE_MONTH_PRE . $month;
  163. RedisModel::set($rk, json_encode($data));
  164. RedisModel::expire($rk, 86400);
  165. return $data;
  166. }
  167. /**
  168. * 添加订单
  169. */
  170. public static function addData($params)
  171. {
  172. $id = DB::table('order_cost')->insertGetId($params);
  173. self::orderCalculateSave($id);
  174. return $id;
  175. }
  176. /**
  177. * 订单补充/汇总计算数据
  178. */
  179. public static function orderCalculateSave($id)
  180. {
  181. $order = self::where('id', $id)->first()->toArray();
  182. $data = [];
  183. self::orderCalculate($order, $data);
  184. if( empty($data) ){
  185. return false;
  186. }
  187. $res = self::where('id', $id)->update($data);
  188. return $res;
  189. }
  190. /**
  191. * 订单计算公式-统一
  192. *
  193. */
  194. public static function orderCalculate($order = [], &$params = [], $status = 0)
  195. {
  196. if(empty($order)){
  197. $order = [
  198. 'cost' => null,
  199. 'top_fee' => null,
  200. 'traffic_boost_refund' => null,
  201. 'offline_subsidy_amount' => null,
  202. 'traffic_boost_money' => null,
  203. 'sr_rate' => null,
  204. 'ratio_of_consumption_to_top_rebate' => null,
  205. 'flow_boost_rebate_ratio' => null,
  206. 'offline_confirmed_cost_includes_tax' => null,
  207. 'final_ratio_of_consumption_to_top_rebate' => null,
  208. 'final_flow_boost_rebate_ratio' => null,
  209. ];
  210. }
  211. $cost = isset($params['cost']) ? $params['cost'] : $order['cost'];
  212. $top_fee = isset($params['top_fee']) ? $params['top_fee'] : $order['top_fee'];
  213. $traffic_boost_refund = isset($params['traffic_boost_refund']) ? $params['traffic_boost_refund'] : $order['traffic_boost_refund'];
  214. $offline_subsidy_amount = isset($params['offline_subsidy_amount']) ? $params['offline_subsidy_amount'] : $order['offline_subsidy_amount'];
  215. $traffic_boost_money = isset($params['traffic_boost_money']) ? $params['traffic_boost_money'] : $order['traffic_boost_money'];
  216. $sr_rate = isset($params['sr_rate']) ? $params['sr_rate'] : $order['sr_rate'];
  217. #新加 媒体名称影响返点
  218. $ratio_of_consumption_to_top_rebate = isset($params['ratio_of_consumption_to_top_rebate']) ? $params['ratio_of_consumption_to_top_rebate'] : $order['ratio_of_consumption_to_top_rebate'];
  219. $flow_boost_rebate_ratio = isset($params['flow_boost_rebate_ratio']) ? $params['flow_boost_rebate_ratio'] : $order['flow_boost_rebate_ratio'];
  220. $offline_confirmed_cost_includes_tax = isset($params['offline_confirmed_cost_includes_tax']) ? $params['offline_confirmed_cost_includes_tax'] : $order['offline_confirmed_cost_includes_tax'];
  221. #上传成本,返货公式
  222. $params['actual_cost_sum'] = $cost + $top_fee; //后台实付成本合计金额 = 订单成本金额消耗+置顶费
  223. $params['rebate'] = $offline_subsidy_amount * $sr_rate; //上饶一笑金额 = 线下补款金额 * 比例
  224. if($status<4){
  225. //财务锁单后暂估,当月数据不再变,只更新最终数据
  226. $params['estimate_cost'] = $cost;
  227. $params['estimate_traffic_boost_refund'] = $traffic_boost_refund;
  228. $params['ratio_of_consumption_to_top_income'] = round($params['actual_cost_sum'] * $ratio_of_consumption_to_top_rebate, 2); //消耗与置顶返点收入
  229. $params['flow_boost_rebate_income'] = round($traffic_boost_money * $flow_boost_rebate_ratio, 2); //流量助推返点收入
  230. $params['estimate_rebate_total'] = $params['flow_boost_rebate_income'] + $params['ratio_of_consumption_to_top_income'];//暂估返点收入=消耗与置顶返点收入+流量助推返点收入
  231. }
  232. #点确认返点,更新逻辑
  233. //TODO 不确定
  234. // if( isset($params['final_ratio_of_consumption_to_top_rebate']) || isset($params['final_flow_boost_rebate_ratio']) ){
  235. // $final_ratio_of_consumption_to_top_rebate = isset($params['final_ratio_of_consumption_to_top_rebate']) ? $params['final_ratio_of_consumption_to_top_rebate'] : $order['final_ratio_of_consumption_to_top_rebate'];
  236. // $final_flow_boost_rebate_ratio = isset($params['final_flow_boost_rebate_ratio']) ? $params['final_flow_boost_rebate_ratio'] : $order['final_flow_boost_rebate_ratio'];
  237. //
  238. // $params['final_ratio_of_consumption_to_top_income'] = round($params['actual_cost_sum'] * $final_ratio_of_consumption_to_top_rebate, 2); //消耗与置顶返点-确认收入
  239. // $params['final_flow_boost_rebate_income'] = round($traffic_boost_money * $final_flow_boost_rebate_ratio, 2); //流量助推返点-确认收入
  240. // $params['rebate_total'] = $params['final_ratio_of_consumption_to_top_income'] + $params['final_flow_boost_rebate_income'];//确认返点收入=消耗与置顶返点收入+流量助推返点收入
  241. // }
  242. $params['offline_confirmed_cost'] = round($offline_confirmed_cost_includes_tax/1.06, 2); //线下不含税
  243. return $params;
  244. }
  245. /**
  246. * 拼接条件语句
  247. */
  248. public static function getSearchQuery($search)
  249. {
  250. return DB::table('order_cost as oc')
  251. ->leftJoin('order_income as oi', 'oi.order_id', '=', 'oc.order_id')
  252. ->where('oc.enable', 1)
  253. ->where(function($query) use($search) {
  254. //if(isset($search['saler_id']) && $search['saler_id']>0) $query->whereRaw('(saler_id='.$user->id.' or handlers_id='.$user->id.')');
  255. if(isset($search['internet_celebrity_name']) && !empty($search['internet_celebrity_name']) ){
  256. if( count($search['internet_celebrity_name']) == 1){
  257. $query->where('oi.internet_celebrity_name', current($search['internet_celebrity_name']) );
  258. } else {
  259. $query->whereIn('oi.internet_celebrity_name', $search['internet_celebrity_name']);
  260. }
  261. }
  262. if(isset($search['company']) && !empty($search['company']) ){
  263. if( count($search['company']) == 1){
  264. $query->where('oi.company', current($search['company']) );
  265. } else {
  266. $query->whereIn('oi.company', $search['company']);
  267. }
  268. }
  269. if(isset($search['project_name']) && !empty($search['project_name']) ){
  270. if( count($search['project_name']) == 1){
  271. $query->where('oi.project_name', current($search['project_name']) );
  272. } else {
  273. $query->whereIn('oi.project_name', $search['project_name']);
  274. }
  275. }
  276. if(isset($search['salesman']) && !empty($search['salesman']) ){
  277. if( count($search['salesman']) == 1){
  278. $query->where('oi.salesman', current($search['salesman']) );
  279. } else {
  280. $query->whereIn('oi.salesman', $search['salesman']);
  281. }
  282. }
  283. if(isset($search['handler']) && !empty($search['handler']) ){
  284. if( count($search['handler']) == 1){
  285. $query->where('oi.handler', current($search['handler']) );
  286. } else {
  287. $query->whereIn('oi.handler', $search['handler']);
  288. }
  289. }
  290. if(isset($search['customer_name']) && !empty($search['customer_name']) ){
  291. if( count($search['customer_name']) == 1){
  292. $query->where('oi.customer_name', current($search['customer_name']) );
  293. } else {
  294. $query->whereIn('oi.customer_name', $search['customer_name']);
  295. }
  296. }
  297. if(isset($search['launch_platform']) && !empty($search['launch_platform']) ){
  298. if( count($search['launch_platform']) == 1){
  299. $query->where('oi.launch_platform', current($search['launch_platform']) );
  300. } else {
  301. $query->whereIn('oi.launch_platform', $search['launch_platform']);
  302. }
  303. }
  304. if(isset($search['cost_company_name']) && !empty($search['cost_company_name']) ){
  305. if( count($search['cost_company_name']) == 1){
  306. $query->where('oi.cost_company_name', current($search['cost_company_name']) );
  307. } else {
  308. $query->whereIn('oi.cost_company_name', $search['cost_company_name']);
  309. }
  310. }
  311. if(isset($search['cost_owner']) && !empty($search['cost_owner']) ){
  312. if( count($search['cost_owner']) == 1){
  313. $query->where('oc.cost_owner', current($search['cost_owner']) );
  314. } else {
  315. $query->whereIn('oc.cost_owner', $search['cost_owner']);
  316. }
  317. }
  318. if(isset($search['offline_subsidy_company']) && !empty($search['offline_subsidy_company']) ){
  319. if( count($search['offline_subsidy_company']) == 1){
  320. $query->where('oc.offline_subsidy_company', current($search['offline_subsidy_company']) );
  321. } else {
  322. $query->whereIn('oc.offline_subsidy_company', $search['offline_subsidy_company']);
  323. }
  324. }
  325. if(isset($search['offline_celeb_name']) && !empty($search['offline_celeb_name']) ){
  326. if( count($search['offline_celeb_name']) == 1){
  327. $query->where('oc.offline_celeb_name', current($search['offline_celeb_name']) );
  328. } else {
  329. $query->whereIn('oc.offline_celeb_name', $search['offline_celeb_name']);
  330. }
  331. }
  332. if(isset($search['offline_pay_date']) && !empty($search['offline_pay_date']) ){
  333. if( count($search['offline_pay_date']) == 1){
  334. $query->where('oc.offline_pay_date', current($search['offline_pay_date']) );
  335. } else {
  336. $query->whereIn('oc.offline_pay_date', $search['offline_pay_date']);
  337. }
  338. }
  339. if(isset($search['advertiser_account']) && !empty($search['advertiser_account']) ){
  340. if( count($search['advertiser_account']) == 1){
  341. $query->where('oi.advertiser_account', current($search['advertiser_account']) );
  342. } else {
  343. $query->whereIn('oi.advertiser_account', $search['advertiser_account']);
  344. }
  345. }
  346. if(isset($search['advertiser_id']) && !empty($search['advertiser_id']) ){
  347. if( count($search['advertiser_id']) == 1){
  348. $query->where('oi.advertiser_id', current($search['advertiser_id']) );
  349. } else {
  350. $query->whereIn('oi.advertiser_id', $search['advertiser_id']);
  351. }
  352. }
  353. if(isset($search['media_name']) && !empty($search['media_name']) ){
  354. if( count($search['media_name']) == 1){
  355. $query->where('oi.media_name', current($search['media_name']) );
  356. } else {
  357. $query->whereIn('oi.media_name', $search['media_name']);
  358. }
  359. }
  360. if(isset($search['ratio_of_consumption_to_top_rebate']) && !empty($search['ratio_of_consumption_to_top_rebate']) ){
  361. if( count($search['ratio_of_consumption_to_top_rebate']) == 1){
  362. $query->where('oc.ratio_of_consumption_to_top_rebate', current($search['ratio_of_consumption_to_top_rebate']) );
  363. } else {
  364. $query->whereIn('oc.ratio_of_consumption_to_top_rebate', $search['ratio_of_consumption_to_top_rebate']);
  365. }
  366. }
  367. if(isset($search['flow_boost_rebate_ratio']) && !empty($search['flow_boost_rebate_ratio']) ){
  368. if( count($search['flow_boost_rebate_ratio']) == 1){
  369. $query->where('oc.flow_boost_rebate_ratio', current($search['flow_boost_rebate_ratio']) );
  370. } else {
  371. $query->whereIn('oc.flow_boost_rebate_ratio', $search['flow_boost_rebate_ratio']);
  372. }
  373. }
  374. if(isset($search['task_id']) && !empty($search['task_id'])) $query->where('oc.task_id', 'like', $search['task_id'].'%');
  375. if(isset($search['is_refund'])) $query->where('oc.is_pdd_refund', $search['is_refund']);
  376. if(isset($search['if_public'])) $query->where('oc.if_public', $search['if_public']);
  377. if(isset($search['month'])) $query->where('oc.month', $search['month']);
  378. if(isset($search['order_part_id'])) $query->where('oi.order_part_id', $search['order_part_id']);
  379. });
  380. }
  381. }