新版订单消耗系统

OrderIncome.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2021/4/6
  6. * Time: 17:21
  7. */
  8. namespace App\Models;
  9. use App\RedisModel;
  10. use Illuminate\Database\Eloquent\Model;
  11. use Illuminate\Support\Facades\DB;
  12. class OrderIncome extends Model
  13. {
  14. protected $table = 'order_income';
  15. public $timestamps = false;
  16. protected static $unguarded = true;
  17. const ORDER_INCOME_MONTH_PRE = 'OrderConsumption.OrderIncomeMonthRds_';
  18. const COLUMNS = [
  19. 'task_id',
  20. 'order_id',
  21. 'order_part_id',
  22. 'month',
  23. 'advertiser_account',
  24. 'advertiser_id',
  25. 'salesman',
  26. 'saler_id',
  27. 'handler',
  28. 'handlers_id',
  29. 'customer_name',
  30. 'final_customer_name',
  31. 'origin_customer_name',
  32. 'project_name',
  33. 'launch_start_date',
  34. 'launch_end_date',
  35. 'launch_platform',
  36. 'media_name',
  37. 'cost_company_name',
  38. 'cost_owner',
  39. 'amount',
  40. 'origin_amount',
  41. 'internet_celebrity_name',
  42. 'internet_celebrity_id',
  43. 'estimated_amount',
  44. 'final_amount',
  45. 'document_number_first',
  46. 'billing_date_first',
  47. 'invoice_amount_first',
  48. 'billing_date_second',
  49. 'invoice_amount_second',
  50. 'confirmation_certificate_first',
  51. 'customer_rebate',
  52. 'actual_consumption_rebate_ratio',
  53. 'link_fee_rebate_ratio',
  54. 'flow_boost_back_point_ratio',
  55. 'top_fee_rebate_proportion',
  56. 'actual_consumption_before',
  57. 'link_fee_before',
  58. 'flow_boost_back_point_before',
  59. 'top_fee_before',
  60. 'before_folding_combined',
  61. 'actual_consumption_after',
  62. 'link_fee_after',
  63. 'flow_boost_back_point_after',
  64. 'top_fee_after',
  65. 'discounted_combined',
  66. 'rebate_spending_amount',
  67. 'final_rebate_spending_amount',
  68. 'document_number_seven',
  69. 'confirmation_certificate_seven',
  70. 'rebate_expense_receiving_unit',
  71. 'income_red_invoice_date',
  72. 'income_red_invoice_amount',
  73. 'statements_status',
  74. 'invoice_num',
  75. 'saler_note',
  76. 'is_pdd_refund',
  77. //'traffic_boost_sum',
  78. //'cost',
  79. 'if_public',
  80. 'payment_date',
  81. 'uninvoiced_amount',
  82. 'cpa_unit_income_price',
  83. 'cpa_single_quantity',
  84. 'verify_status',
  85. 'company',
  86. 'customer_rebate_amount',
  87. 'cpa_actual_consumption',
  88. 'rebate_spending_ratio'
  89. ];
  90. /**
  91. * 为私单自动生成任务id
  92. * @param $prefix
  93. * @return string
  94. */
  95. public static function createTaskId($prefix)
  96. {
  97. return $prefix.'-'.time().rand(1000, 9999);
  98. }
  99. /*
  100. * 废弃
  101. */
  102. public static function setIncomeRds($month)
  103. {
  104. $sql = "select
  105. id,
  106. month,
  107. task_id,
  108. order_part_id,
  109. order_id,
  110. company,
  111. advertiser_account,
  112. advertiser_id,
  113. salesman,
  114. saler_id,
  115. handler,
  116. handlers_id,
  117. customer_name,
  118. final_customer_name,
  119. origin_customer_name,
  120. project_name,
  121. launch_start_date,
  122. launch_end_date,
  123. launch_platform,
  124. media_name,
  125. cost_company_name,
  126. cost_owner,
  127. amount,
  128. final_amount,
  129. internet_celebrity_name,
  130. internet_celebrity_id,
  131. customer_rebate,
  132. actual_consumption_rebate_ratio,
  133. link_fee_rebate_ratio,
  134. flow_boost_back_point_ratio,
  135. top_fee_rebate_proportion,
  136. actual_consumption_before,
  137. link_fee_before,
  138. flow_boost_back_point_before,
  139. top_fee_before,
  140. before_folding_combined,
  141. actual_consumption_after,
  142. link_fee_after,
  143. flow_boost_back_point_after,
  144. top_fee_after,
  145. discounted_combined,
  146. rebate_spending_amount,
  147. rebate_spending_ratio,
  148. rebate_expense_receiving_unit,
  149. income_red_invoice_date,
  150. income_red_invoice_amount,
  151. statements_status,
  152. invoice_status,
  153. invoice_num,
  154. saler_note,
  155. is_pdd_refund,
  156. if_public,
  157. payment_date,
  158. cpa_unit_income_price,
  159. cpa_single_quantity,
  160. cpa_actual_consumption,
  161. verify_status,
  162. customer_rebate_amount,
  163. status
  164. from order_income
  165. where enable=1 and month='{$month}'
  166. order by id desc ";
  167. $data = DB::select($sql);
  168. $data = json_decode(json_encode($data), true);
  169. $orderIds = array_column($data, 'order_id');
  170. $invoiceData = CustomerInvoices::invoiceHistory($orderIds);
  171. #数据处理
  172. foreach($data as &$v){
  173. $v['task_id'] .= '';
  174. $item = [
  175. 'first_invoice_amount' => 0,
  176. 'first_invoice_date' => null,
  177. 'invoice_num' => 0,
  178. 'second_invoice_amount' => 0,
  179. 'second_invoice_date' => null,
  180. 'uninvoiced_amount' => $v['final_amount'],
  181. ];
  182. //处理超长小数百分比显示问题
  183. $v['actual_consumption_rebate_ratio'] = Order::longDecimalFormat($v['actual_consumption_rebate_ratio']);
  184. $v['link_fee_rebate_ratio'] = Order::longDecimalFormat($v['link_fee_rebate_ratio']);
  185. $v['flow_boost_back_point_ratio'] = Order::longDecimalFormat($v['flow_boost_back_point_ratio']);
  186. $v['top_fee_rebate_proportion'] = Order::longDecimalFormat($v['top_fee_rebate_proportion']);
  187. $v['rebate_spending_ratio'] = Order::longDecimalFormat($v['rebate_spending_ratio']);
  188. $v['month'] = substr($v['month'], 0, 7);
  189. #拼接开票信息
  190. if (isset($invoiceData[$v['order_id']])) {
  191. $invoiceInfo = $invoiceData[$v['order_id']];
  192. } else {
  193. $invoiceInfo = $item;
  194. }
  195. $v = array_merge($v, $invoiceInfo);
  196. $v['if_public'] = $v['if_public'] == 1 ? '是' : '';
  197. $v['is_pdd_refund'] = $v['is_pdd_refund'] == 1 ? '是' : '';
  198. }
  199. $rk = static::ORDER_INCOME_MONTH_PRE . $month;
  200. RedisModel::set($rk, json_encode($data));
  201. RedisModel::expire($rk, 86400);
  202. return $data;
  203. }
  204. /*
  205. * 根据检索条件获取收入表数据
  206. */
  207. public static function getSearchIncomeQuery($search, $user)
  208. {
  209. return OrderIncome::where('enable', 1)
  210. ->where(function($query) use($search, $user) {
  211. if (isset($search['is_statements']) && 1 == $search['is_statements']) {
  212. if($user->role_id == 11) {
  213. $query->whereRaw('(handlers_id='.$user->id.')');
  214. }
  215. } else {
  216. if($user->role_id == 11) {
  217. $query->whereRaw('(saler_id='.$user->id.' or handlers_id='.$user->id.')');
  218. }
  219. }
  220. if(isset($search['company']) && !empty($search['company'])) {
  221. if( count($search['company']) == 1){
  222. $query->where('company', current($search['company']) );
  223. } else {
  224. $query->whereIn('company', $search['company']);
  225. }
  226. }
  227. if(isset($search['advertiser_account']) && !empty($search['advertiser_account'])) {
  228. if ( count($search['advertiser_account']) == 1) {
  229. $query->where('advertiser_account', current($search['advertiser_account']));
  230. } else {
  231. $query->whereIn('advertiser_account', $search['advertiser_account']);
  232. }
  233. }
  234. if(isset($search['media_name']) && !empty($search['media_name'])) {
  235. if ( count($search['media_name']) == 1) {
  236. $query->where('media_name', current($search['media_name']));
  237. } else {
  238. $query->whereIn('media_name', $search['media_name']);
  239. }
  240. }
  241. if(isset($search['internet_celebrity_name']) && !empty($search['internet_celebrity_name'])) {
  242. if ( count($search['internet_celebrity_name']) == 1) {
  243. $query->where('internet_celebrity_name', current($search['internet_celebrity_name']));
  244. } else {
  245. $query->whereIn('internet_celebrity_name', $search['internet_celebrity_name']);
  246. }
  247. }
  248. if(isset($search['handler']) && !empty($search['handler'])) {
  249. if ( count($search['handler']) == 1) {
  250. $query->where('handler', current($search['handler']));
  251. } else {
  252. $query->whereIn('handler', $search['handler']);
  253. }
  254. }
  255. if(isset($search['customer_name']) && !empty($search['customer_name'])) {
  256. if ( count($search['customer_name']) == 1) {
  257. $query->where('customer_name', current($search['customer_name']));
  258. } else {
  259. $query->whereIn('customer_name', $search['customer_name']);
  260. }
  261. }
  262. if(isset($search['project_name']) && !empty($search['project_name'])) {
  263. if( count($search['project_name']) == 1) {
  264. $query->where('project_name', current($search['project_name']));
  265. } else {
  266. $query->whereIn('project_name', $search['project_name']);
  267. }
  268. }
  269. if(isset($search['cost_company_name']) && !empty($search['cost_company_name'])) {
  270. if(count($search['cost_company_name']) == 1) {
  271. $query->where('cost_company_name', current($search['cost_company_name']));
  272. } else {
  273. $query->whereIn('cost_company_name', $search['cost_company_name']);
  274. }
  275. }
  276. if(isset($search['cost_owner']) && !empty($search['cost_owner'])) {
  277. if (count($search['cost_owner']) == 1) {
  278. $query->where('cost_owner', current($search['cost_owner']));
  279. } else {
  280. $query->whereIn('cost_owner', $search['cost_owner']);
  281. }
  282. }
  283. if(isset($search['rebate_expense_receiving_unit']) && !empty($search['rebate_expense_receiving_unit'])) {
  284. if ( count($search['rebate_expense_receiving_unit']) == 1) {
  285. $query->where('rebate_expense_receiving_unit', current($search['rebate_expense_receiving_unit']));
  286. } else {
  287. $query->whereIn('rebate_expense_receiving_unit', $search['rebate_expense_receiving_unit']);
  288. }
  289. }
  290. if(isset($search['advertiser_id']) && !empty($search['advertiser_id'])) {
  291. if (count($search['advertiser_id']) == 1) {
  292. $query->where('advertiser_id', current($search['advertiser_id']));
  293. } else {
  294. $query->whereIn('advertiser_id', $search['advertiser_id']);
  295. }
  296. }
  297. if(isset($search['if_public'])) $query->where('if_public', (int)$search['if_public']);
  298. if(isset($search['launch_platform']) && !empty($search['launch_platform'])) {
  299. if(count($search['launch_platform']) == 1) {
  300. $query->where('launch_platform', current($search['launch_platform']));
  301. } else {
  302. $query->whereIn('launch_platform', $search['launch_platform']);
  303. }
  304. }
  305. if(isset($search['month']) && $search['month']) {
  306. if(count($search['month']) == 1) {
  307. $query->where('month', '=', date('Y-m-01', strtotime(current($search['month'])) ) );
  308. } else {
  309. $monthList = array_map(function($value){
  310. return date('Y-m-01', strtotime($value));
  311. }, $search['month']);
  312. $query->whereIn('month', $monthList);
  313. }
  314. }
  315. if(isset($search['launch_end_date_start']) && $search['launch_end_date_start']) $query->where('launch_end_date', '>=', $search['launch_end_date_start']);
  316. if(isset($search['launch_end_date_end']) && $search['launch_end_date_end']) $query->where('launch_end_date', '<=', $search['launch_end_date_end']);
  317. if(isset($search['amount_min']) && $search['amount_min']) $query->where('amount', '>=', $search['amount_min']);
  318. if(isset($search['amount_max']) && $search['amount_max']) $query->where('amount', '<=', $search['amount_max']);
  319. if(isset($search['final_amount_min']) && $search['final_amount_min']) $query->where('final_amount', '>=', $search['final_amount_min']);
  320. if(isset($search['final_amount_max']) && $search['final_amount_max']) $query->where('final_amount', '<=', $search['final_amount_max']);
  321. // if(isset($search['cost_min']) && $search['cost_min']) $query->where('cost', '>=', $search['cost_min']);
  322. // if(isset($search['cost_max']) && $search['cost_max']) $query->where('cost', '<=', $search['cost_max']);
  323. if(isset($search['income_red_invoice_date_start']) && $search['income_red_invoice_date_start']) $query->where('income_red_invoice_date', '>=', $search['income_red_invoice_date_start']);
  324. if(isset($search['income_red_invoice_date_end']) && $search['income_red_invoice_date_end']) $query->where('income_red_invoice_date', '<=', $search['income_red_invoice_date_end']);
  325. if(isset($search['income_red_invoice_amount_min']) && $search['income_red_invoice_amount_min']) $query->where('income_red_invoice_amount', '>=', $search['income_red_invoice_amount_min']);
  326. if(isset($search['income_red_invoice_amount_max']) && $search['income_red_invoice_amount_max']) $query->where('income_red_invoice_amount', '<=', $search['income_red_invoice_amount_max']);
  327. if(isset($search['verify_status']) && $search['verify_status']>0){
  328. $status = $search['verify_status'];
  329. switch ($status) {
  330. case 1:
  331. $status = [1, 2];
  332. break;
  333. case 2:
  334. $status = [4];
  335. break;
  336. case 3:
  337. $status = [3, 5];
  338. break;
  339. default:
  340. $status = [];
  341. break;
  342. }
  343. if(!empty($status)) $query->whereIn('verify_status', $status);
  344. }
  345. if(!empty($search['statements_status'])) {
  346. if(-1 == $search['statements_status']) {
  347. $search['statements_status'] = 0;
  348. }
  349. $query->where('statements_status', $search['statements_status']);
  350. }
  351. if(!empty($search['task_id'])) $query->where('task_id', 'like', '%'.$search['task_id'].'%');
  352. if(!empty($search['order_part_id'])) $query->where('order_part_id', 'like', '%'.$search['order_part_id'].'%');
  353. // $query->where('order_id', '>', '');
  354. });
  355. }
  356. }