TPlusServerService.php 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\Depart;
  5. use App\Model\Employee;
  6. use App\Model\EmployeeDepartPermission;
  7. use App\Model\EmployeeIndex;
  8. use App\Model\Product;
  9. use App\Model\RevenueCost;
  10. use App\Model\RevenueCostTotal;
  11. use App\Model\SalaryEmployee;
  12. use Illuminate\Database\Schema\Blueprint;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Schema;
  15. class TPlusServerService extends Service
  16. {
  17. /**
  18. * @var TPlusDatabaseServerService
  19. */
  20. protected $databaseService;
  21. /**
  22. * @var string|null
  23. */
  24. protected $error;
  25. /**
  26. * TPlusServerService constructor.
  27. */
  28. public function __construct()
  29. {
  30. $service = new TPlusDatabaseServerService();
  31. $this->databaseService = $service->db;
  32. $this->error = $service->error;
  33. }
  34. /**
  35. * 获取错误信息
  36. *
  37. * @return string|null
  38. */
  39. public function getError()
  40. {
  41. return $this->error;
  42. }
  43. private $table = "tmp_revenue_cost_data";
  44. private $table_2 = "tmp_salary_employee";
  45. /**
  46. * 同步人员部门
  47. *
  48. * @param array $data
  49. * @param array $user
  50. * @return array
  51. */
  52. public function synPersonDepart($data, $user)
  53. {
  54. try {
  55. $this->databaseService->table('AA_Department')
  56. ->select('id','idparent as parent_id','name as title','code','disabled as is_use')
  57. ->chunkById(100, function ($data) {
  58. DB::transaction(function () use ($data) {
  59. $dataArray = Collect($data)->map(function ($object) {
  60. return (array)$object;
  61. })->toArray();
  62. $d_id = Depart::whereIn('id', array_column($dataArray,'id'))
  63. ->pluck('id')
  64. ->toArray();
  65. $insert = $update = [];
  66. foreach ($dataArray as $value){
  67. $is_use = $value['is_use'] ? 0 : 1;
  68. if(in_array($value['id'], $d_id)){
  69. $update[] = [
  70. 'id' => $value['id'],
  71. 'parent_id' => $value['parent_id'],
  72. 'title' => $value['title'],
  73. 'code' => $value['code'],
  74. 'is_use' => $is_use
  75. ];
  76. }else{
  77. $insert[] = [
  78. 'id' => $value['id'],
  79. 'parent_id' => $value['parent_id'],
  80. 'title' => $value['title'],
  81. 'code' => $value['code'],
  82. 'is_use' => $is_use
  83. ];
  84. }
  85. }
  86. if(! empty($insert)) Depart::insert($insert);
  87. if(! empty($update)) {
  88. foreach ($update as $value){
  89. Depart::where('id', $value['id'])
  90. ->update($value);
  91. }
  92. }
  93. });
  94. });
  95. $this->databaseService->table('AA_Person')
  96. ->select('id','code as number','name as emp_name','mobilePhoneNo as mobile','iddepartment as depart_id','disabled as state')
  97. ->chunkById(100, function ($data) {
  98. DB::transaction(function () use ($data) {
  99. $dataArray = Collect($data)->map(function ($object) {
  100. return (array)$object;
  101. })->toArray();
  102. $employee_id = Employee::whereIn('id', array_column($dataArray,'id'))
  103. ->pluck('id')
  104. ->toArray();
  105. $insert = $update = $depart_update = [];
  106. foreach ($dataArray as $value){
  107. $state = $value['state'] ? Employee::NOT_USE : Employee::USE;
  108. if(in_array($value['id'], $employee_id)){
  109. $update[] = [
  110. 'id' => $value['id'],
  111. 'number' => $value['number'],
  112. 'emp_name' => $value['emp_name'],
  113. 'mobile' => $value['mobile'],
  114. 'state' => $state
  115. ];
  116. }else{
  117. $insert[] = [
  118. 'id' => $value['id'],
  119. 'number' => $value['number'],
  120. 'emp_name' => $value['emp_name'],
  121. 'mobile' => $value['mobile'],
  122. 'state' => $state
  123. ];
  124. }
  125. $depart_update[] = [
  126. 'employee_id' => $value['id'],
  127. 'depart_id' => $value['depart_id']
  128. ];
  129. }
  130. if(! empty($insert)) Employee::insert($insert);
  131. if(! empty($update)) {
  132. foreach ($update as $value){
  133. Employee::where('id', $value['id'])
  134. ->update($value);
  135. }
  136. }
  137. if(! empty($depart_update)){
  138. EmployeeDepartPermission::whereIn('employee_id',array_column($depart_update,'employee_id'))->delete();
  139. EmployeeDepartPermission::insert($depart_update);
  140. }
  141. });
  142. });
  143. } catch (\Throwable $e) {
  144. return [false, $e->getMessage()];
  145. }
  146. return [true, ''];
  147. }
  148. /**
  149. * 收入成本统计同步
  150. *
  151. * @param array $data
  152. * @param array $user
  153. * @return array
  154. */
  155. public function synRevenueCost($data, $user){
  156. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
  157. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
  158. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
  159. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  160. if(! $bool) return [false, $bool_msg];
  161. $data['start_timeStamp'] = $start_time;
  162. $data['end_timeStamp'] = $end_time;
  163. $start = date('Y-m-d H:i:s.000', $start_time);
  164. $end = date('Y-m-d H:i:s.000', $end_time);
  165. $data['start_time'] = $start;
  166. $data['end_time'] = $end;
  167. $data['operation_time'] = time();
  168. if(empty($data['type'])) return [false, '同步类型不能为空'];
  169. if(in_array($data['type'],[1,2,3,4])){
  170. list($status,$msg) = $this->limitingSendRequest($this->table);
  171. if(! $status) return [false, '收入成本相关信息同步正在后台运行,请稍后'];
  172. // //同步
  173. // list($status, $msg) = $this->synRevenueCostFromTPlus($data, $user);
  174. // if(! $status) {
  175. // $this->clearTmpTable();
  176. // $this->dellimitingSendRequest($this->table);
  177. // return [false, $msg];
  178. // }
  179. //队列
  180. ProcessDataJob::dispatch($data, $user)->onQueue(RevenueCost::job);
  181. }else{
  182. return [false, '同步类型错误'];
  183. }
  184. return [true, '收入成本相关信息同步已进入后台任务'];
  185. }
  186. public function synRevenueCostFromTPlus($data, $user){
  187. //创建临时表 如果不存在
  188. $this->createTmpTable();
  189. //清理临时表 如果内容不为空
  190. $this->clearTmpTable();
  191. $type = $data['type'];
  192. //写入临时数据
  193. if($type == 1){
  194. list($status, $msg) = $this->xhdTPlus($data, $user);
  195. if(! $status) return [false, $msg];
  196. list($status, $msg) = $this->xsfpTPlus($data, $user);
  197. if(! $status) return [false, $msg];
  198. list($status, $msg) = $this->hkdTPlus($data, $user);
  199. if(! $status) return [false, $msg];
  200. }elseif ($type == 2){
  201. list($status, $msg) = $this->xhdTPlus($data, $user);
  202. if(! $status) return [false, $msg];
  203. }elseif ($type == 3){
  204. list($status, $msg) = $this->xsfpTPlus($data, $user);
  205. if(! $status) return [false, $msg];
  206. }elseif ($type == 4){
  207. list($status, $msg) = $this->hkdTPlus($data, $user);
  208. if(! $status) return [false, $msg];
  209. }
  210. //更新数据
  211. list($status,$msg) = $this->updateRevenueCost($data);
  212. if(! $status) return [false, $msg];
  213. // //更新主表数据
  214. // list($status,$msg) = $this->updateRevenueCostTotal($data);
  215. // if(! $status) return [false, $msg];
  216. //都成功后 清理临时表
  217. $this->clearTmpTable();
  218. //释放redis
  219. $this->delTableKey();
  220. return [true, '同步成功'];
  221. }
  222. private function xhdTPlus($data, $user){
  223. try {
  224. $table = $this->table;
  225. $limit = 500;
  226. $lastId = 0;
  227. do {
  228. $rows = $this->databaseService->table('SA_SaleDelivery_b as sd_b')
  229. ->join('SA_SaleDelivery as sd', 'sd_b.idSaleDeliveryDTO', '=', 'sd.ID')
  230. ->leftJoin('AA_Partner as pn', 'sd.idsettlecustomer', '=', 'pn.ID')
  231. ->leftJoin('AA_Person as ps', 'sd.idclerk', '=', 'ps.ID')
  232. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  233. ->leftJoin('AA_Inventory as it', 'sd_b.idinventory', '=', 'it.ID')
  234. ->leftJoin('AA_Unit as ui', 'sd_b.idbaseunit', '=', 'ui.ID')
  235. ->where('sd.voucherdate', '>=', $data['start_time'])
  236. ->where('sd.voucherdate', '<=', $data['end_time'])
  237. ->where('sd_b.ID', '>', $lastId) // 用真实字段
  238. ->orderBy('sd_b.ID')
  239. ->limit($limit)
  240. ->selectRaw("
  241. COALESCE(sd.ID, 0) as order_id,
  242. COALESCE(sd.code, '') as order_number,
  243. sd.voucherdate as order_time,
  244. sd.voucherState as order_state,
  245. COALESCE(ps.name, '') as employee_id_1_title,
  246. COALESCE(sd.idclerk, 0) as employee_id_1,
  247. COALESCE(ps2.name, '') as employee_id_2_title,
  248. COALESCE(pn.idsaleman, 0) as employee_id_2,
  249. COALESCE(pn.code, '') as customer_code,
  250. COALESCE(pn.name, '') as customer_title,
  251. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  252. COALESCE(sd.pubuserdefnvc11, '') as channel_finance,
  253. COALESCE(sd.pubuserdefnvc12, '') as channel_details,
  254. COALESCE(it.code, '') as product_code,
  255. COALESCE(it.name, '') as product_title,
  256. COALESCE(it.specification, '') as product_size,
  257. COALESCE(ui.name, '') as unit,
  258. COALESCE(sd_b.quantity, 0) as quantity,
  259. COALESCE(sd_b.taxPrice, 0) as price_3,
  260. COALESCE(sd_b.taxAmount, 0) as price_3_total,
  261. COALESCE(sd_b.ID, 0) as id_detail,
  262. COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  263. ")
  264. ->get();
  265. if ($rows->isEmpty()) break;
  266. $dataArray = Collect($rows)->map(function ($object) {
  267. return (array)$object;
  268. })->toArray();
  269. //存货档案
  270. $product = Product::where('del_time', 0)
  271. ->whereIn('code', array_unique(array_column($dataArray, 'product_code')))
  272. ->select('code', 'write_off_price', 'freight_price', 'business_cost')
  273. ->get()->toArray();
  274. $product_map = array_column($product, null, 'code');
  275. //组织数据
  276. foreach ($dataArray as $key => $value) {
  277. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  278. if(is_numeric($customer_profit_rate)){
  279. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  280. }else{
  281. $customer_profit_rate = 0;
  282. }
  283. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  284. $p_tmp = $product_map[$value['product_code']] ?? [];
  285. $dataArray[$key]['order_type'] = RevenueCost::ORDER_ONE;
  286. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  287. $write_off_price = $p_tmp['write_off_price'] ?? 0;
  288. $dataArray[$key]['price_1'] = $write_off_price;
  289. $dataArray[$key]['price_1_total'] = bcmul($write_off_price, $value['quantity'], 2);
  290. $freight_price = $p_tmp['freight_price'] ?? 0;
  291. $dataArray[$key]['price_2'] = $freight_price;
  292. $dataArray[$key]['price_2_total'] = bcmul($freight_price, $value['quantity'], 2);
  293. $business_cost = $p_tmp['business_cost'] ?? 0;
  294. $dataArray[$key]['price_4'] = $business_cost;
  295. $price_4_total = bcmul($business_cost, $value['quantity'], 2);
  296. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'], 2);
  297. $profit = bcsub($value['price_3_total'], $price_4_total, 2);
  298. $dataArray[$key]['profit'] = $profit;
  299. $dataArray[$key]['profit_rate'] = $value['price_3_total'] > 0 ? bcdiv($profit, $value['price_3_total'], 2) : 0;
  300. }
  301. DB::table($table)->insert($dataArray);
  302. // 更新 lastId 继续下一批
  303. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  304. } while (count($rows) === $limit);
  305. }catch (\Throwable $exception){
  306. return [false, "销货单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  307. }
  308. return [true, ''];
  309. }
  310. private function xsfpTPlus($data, $user){
  311. try {
  312. $table = $this->table;
  313. $limit = 500;
  314. $lastId = 0;
  315. do {
  316. $rows = $this->databaseService->table('SA_SaleInvoice_b as si_b')
  317. ->join('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  318. ->leftJoin('SA_SaleDelivery_b as sd_b', 'si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  319. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  320. ->leftJoin('AA_Person as ps', 'si.idclerk', '=', 'ps.ID')
  321. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  322. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  323. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  324. ->where('si.voucherdate','>=',$data['start_time'])
  325. ->where('si.voucherdate','<=',$data['end_time'])
  326. ->where('si_b.ID', '>', $lastId) // 用真实字段
  327. ->orderBy('si_b.ID')
  328. ->limit($limit)
  329. ->selectRaw("
  330. COALESCE(si.ID, 0) as order_id,
  331. COALESCE(si.code, '') as order_number,
  332. si.voucherdate as order_time,
  333. COALESCE(pn.code, '') as customer_code,
  334. COALESCE(pn.name, '') as customer_title,
  335. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  336. COALESCE(si.idclerk, 0) as employee_id_1,
  337. COALESCE(ps.name, '') as employee_id_1_title,
  338. COALESCE(pn.idsaleman, 0) as employee_id_2,
  339. COALESCE(ps2.name, '') as employee_id_2_title,
  340. COALESCE(it.code, '') as product_code,
  341. COALESCE(it.name, '') as product_title,
  342. COALESCE(it.specification, '') as product_size,
  343. COALESCE(ui.name, '') as unit,
  344. COALESCE(si_b.quantity, 0) as quantity,
  345. COALESCE(si_b.taxPrice, 0) as price_1,
  346. COALESCE(si_b.taxAmount, 0) as price_1_total,
  347. COALESCE(si_b.ID, 0) as id_detail,
  348. COALESCE(si_b.sourceVoucherDetailId, 0) as id_detail_upstream,
  349. COALESCE(si_b.sourceVoucherCode, '') as order_number_upstream,
  350. COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  351. ")
  352. ->get();
  353. if ($rows->isEmpty()) break;
  354. $dataArray = Collect($rows)->map(function ($object) {
  355. return (array)$object;
  356. })->toArray();
  357. //存货档案
  358. $product = Product::where('del_time',0)
  359. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  360. ->select('code','business_cost')
  361. ->get()->toArray();
  362. $product_map = array_column($product,null,'code');
  363. //组织数据
  364. foreach ($dataArray as $key => $value){
  365. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  366. if(is_numeric($customer_profit_rate)){
  367. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  368. }else{
  369. $customer_profit_rate = 0;
  370. }
  371. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  372. $p_tmp = $product_map[$value['product_code']] ?? [];
  373. $dataArray[$key]['order_type'] = RevenueCost::ORDER_TWO;
  374. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  375. $business_cost = $p_tmp['business_cost'] ?? 0;
  376. $dataArray[$key]['price_4'] = $business_cost;
  377. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  378. $dataArray[$key]['price_4_total'] = bcmul($business_cost, $value['quantity'],2);
  379. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  380. $dataArray[$key]['profit'] = $profit;
  381. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  382. }
  383. DB::table($table)->insert($dataArray);
  384. // 更新 lastId 继续下一批
  385. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  386. } while (count($rows) === $limit);
  387. }catch (\Throwable $exception){
  388. return [false, "销售发票同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  389. }
  390. return [true, ''];
  391. }
  392. private function hkdTPlus($data, $user){
  393. try{
  394. $table = $this->table;
  395. $limit = 500;
  396. $lastId = 0;
  397. do {
  398. $rows = $this->databaseService->table('ARAP_ReceivePayment_b as rp_b')
  399. ->join('ARAP_ReceivePayment as rp', 'rp_b.idArapReceivePaymentDTO', '=', 'rp.ID')
  400. // ->leftJoin('SA_SaleInvoice_b as si_b', 'rp_b.voucherDetailID', '=', 'si_b.ID')
  401. // ->leftJoin('SA_SaleInvoice as si', 'si_b.idSaleInvoiceDTO', '=', 'si.ID')
  402. // ->leftJoin('SA_SaleDelivery_b as sd_b', 'si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  403. // 发票子表关联(仅限 idvouchertype = 20)
  404. ->leftJoin('SA_SaleInvoice_b as si_b', function ($join) {
  405. $join->on('rp_b.voucherDetailID', '=', 'si_b.ID')
  406. ->where('rp_b.idvouchertype', '=', 20);
  407. })
  408. ->leftJoin('SA_SaleInvoice as si', function ($join) {
  409. $join->on('si_b.idSaleInvoiceDTO', '=', 'si.ID')
  410. ->where('rp_b.idvouchertype', '=', 20);
  411. })
  412. ->leftJoin('SA_SaleDelivery_b as sd_b', function ($join) {
  413. $join->on('si_b.sourceVoucherDetailId', '=', 'sd_b.ID')
  414. ->where('rp_b.idvouchertype', '=', 20);
  415. })
  416. ->leftJoin('AA_Partner as pn', 'si.idsettlecustomer', '=', 'pn.ID')
  417. ->leftJoin('AA_Person as ps', 'rp.idperson', '=', 'ps.ID')
  418. ->leftJoin('AA_Person as ps2', 'pn.idsaleman', '=', 'ps2.ID')
  419. ->leftJoin('AA_Inventory as it', 'si_b.idinventory', '=', 'it.ID')
  420. ->leftJoin('AA_Unit as ui', 'si_b.idbaseunit', '=', 'ui.ID')
  421. ->where('rp.voucherdate','>=',$data['start_time'])
  422. ->where('rp.voucherdate','<=',$data['end_time'])
  423. // ->where('rp_b.idvouchertype','=', 20) // 销售发票
  424. ->where('rp.isReceiveFlag','=', 1)
  425. ->where('rp_b.ID', '>', $lastId)
  426. ->orderBy('rp_b.ID')
  427. ->limit($limit)
  428. // ->selectRaw("
  429. // COALESCE(rp.ID, 0) as order_id,
  430. // COALESCE(rp.code, '') as order_number,
  431. // rp.voucherdate as order_time,
  432. // COALESCE(pn.code, '') as customer_code,
  433. // COALESCE(pn.name, '') as customer_title,
  434. // COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  435. // COALESCE(it.code, '') as product_code,
  436. // COALESCE(it.name, '') as product_title,
  437. // COALESCE(rp.idperson, 0) as employee_id_1,
  438. // COALESCE(ps.name, '') as employee_id_1_title,
  439. // COALESCE(pn.idsaleman, 0) as employee_id_2,
  440. // COALESCE(ps2.name, '') as employee_id_2_title,
  441. // COALESCE(it.specification, '') as product_size,
  442. // COALESCE(ui.name, '') as unit,
  443. // COALESCE(si_b.quantity, 0) as quantity,
  444. // COALESCE(si_b.taxPrice, 0) as price_1,
  445. // COALESCE(si_b.taxAmount, 0) as price_1_total,
  446. // COALESCE(rp_b.amount, 0) as payment_amount,
  447. // COALESCE(rp_b.ID, 0) as id_detail,
  448. // COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  449. // COALESCE(rp_b.voucherCode, '') as order_number_upstream,
  450. // COALESCE(sd_b.pubuserdefdecm9, 0) as is_activity
  451. // ")
  452. ->selectRaw("
  453. COALESCE(rp.ID, 0) as order_id,
  454. COALESCE(rp.code, '') as order_number,
  455. rp.voucherdate as order_time,
  456. COALESCE(pn.code, '') as customer_code,
  457. COALESCE(pn.name, '') as customer_title,
  458. COALESCE(pn.priuserdefnvc14, '') as customer_profit_rate,
  459. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.code, '') ELSE '' END as product_code,
  460. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.name, '') ELSE '' END as product_title,
  461. COALESCE(rp.idperson, 0) as employee_id_1,
  462. COALESCE(ps.name, '') as employee_id_1_title,
  463. COALESCE(pn.idsaleman, 0) as employee_id_2,
  464. COALESCE(ps2.name, '') as employee_id_2_title,
  465. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(it.specification, '') ELSE '' END as product_size,
  466. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(ui.name, '') ELSE '' END as unit,
  467. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.quantity, 0) ELSE 0 END as quantity,
  468. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxPrice, 0) ELSE 0 END as price_1,
  469. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(si_b.taxAmount, 0) ELSE 0 END as price_1_total,
  470. COALESCE(rp_b.amount, 0) as payment_amount,
  471. COALESCE(rp_b.ID, 0) as id_detail,
  472. COALESCE(rp_b.voucherDetailID, 0) as id_detail_upstream,
  473. COALESCE(rp_b.voucherCode, '') as order_number_upstream,
  474. CASE WHEN rp_b.idvouchertype = 20 THEN COALESCE(sd_b.pubuserdefdecm9, 0) ELSE 0 END as is_activity,
  475. rp_b.idvouchertype as voucher_type
  476. ")
  477. ->get();
  478. if ($rows->isEmpty()) break;
  479. $dataArray = Collect($rows)->map(function ($object) {
  480. return (array)$object;
  481. })->toArray();
  482. //存货档案
  483. $product = Product::where('del_time',0)
  484. ->whereIn('code', array_unique(array_column($dataArray,'product_code')))
  485. ->select('code','business_cost')
  486. ->get()->toArray();
  487. $product_map = array_column($product,null,'code');
  488. //组织数据
  489. foreach ($dataArray as $key => $value){
  490. $customer_profit_rate = rtrim($value['customer_profit_rate'],'%');
  491. if(is_numeric($customer_profit_rate)){
  492. $customer_profit_rate = bcdiv($customer_profit_rate,100,3);
  493. }else{
  494. $customer_profit_rate = 0;
  495. }
  496. $dataArray[$key]['customer_profit_rate'] = $customer_profit_rate;
  497. $p_tmp = $product_map[$value['product_code']] ?? [];
  498. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  499. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  500. $business_cost = $p_tmp['business_cost'] ?? 0;
  501. $dataArray[$key]['price_4'] = $business_cost;
  502. $price_4_total = bcmul($business_cost, $value['quantity'],2);
  503. $dataArray[$key]['price_4_total'] = $price_4_total;
  504. $profit = bcsub($value['price_1_total'], $price_4_total,2);
  505. $dataArray[$key]['profit'] = $profit;
  506. $dataArray[$key]['profit_rate'] = $value['price_1_total'] > 0 ? bcdiv($profit, $value['price_1_total'],2) : 0;
  507. }
  508. DB::table($table)->insert($dataArray);
  509. // 更新 lastId 继续下一批
  510. $lastId = end($dataArray)['id_detail'] ?? $lastId;
  511. } while (count($rows) === $limit);
  512. }catch (\Throwable $exception){
  513. return [false, "回款单同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  514. }
  515. return [true, ''];
  516. }
  517. private function updateRevenueCost($data){
  518. try {
  519. $start_timeStamp = $data['start_timeStamp'];
  520. $end_timeStamp = $data['end_timeStamp'];
  521. $tmpTable = $this->table;
  522. $time = time();
  523. $ergs = $data;
  524. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
  525. // 1. 先软删除旧数据(你已有)
  526. RevenueCost::where('del_time', 0)
  527. ->where('order_time', '>=', $start_timeStamp)
  528. ->where('order_time', '<=', $end_timeStamp)
  529. ->update(['del_time' => $time]);
  530. // 2. 分批从临时表插入新数据
  531. $batchSize = 500;
  532. $lastId = 0;
  533. do {
  534. $chunk = DB::table($tmpTable)
  535. ->where('id', '>', $lastId)
  536. ->orderBy('id')
  537. ->limit($batchSize)
  538. ->get();
  539. if ($chunk->isEmpty()) {
  540. break;
  541. }
  542. $data = $chunk->map(function ($item) use($time){
  543. return [
  544. 'order_id' => $item->order_id,
  545. 'order_number' => $item->order_number,
  546. 'order_time' => $item->order_time,
  547. 'order_state' => $item->order_state,
  548. 'employee_id_1_title' => $item->employee_id_1_title,
  549. 'employee_id_1' => $item->employee_id_1 ?? 0,
  550. 'employee_id_2' => $item->employee_id_2 ?? 0,
  551. 'employee_id_2_title' => $item->employee_id_2_title ?? "",
  552. 'customer_code' => $item->customer_code,
  553. 'customer_title' => $item->customer_title,
  554. 'channel_finance' => $item->channel_finance,
  555. 'channel_details' => $item->channel_details,
  556. 'product_code' => $item->product_code,
  557. 'product_title' => $item->product_title,
  558. 'product_size' => $item->product_size,
  559. 'unit' => $item->unit,
  560. 'quantity' => $item->quantity,
  561. 'price_1' => $item->price_1,
  562. 'price_1_total' => $item->price_1_total,
  563. 'price_2' => $item->price_2,
  564. 'price_2_total' => $item->price_2_total,
  565. 'price_3' => $item->price_3,
  566. 'price_3_total' => $item->price_3_total,
  567. 'price_4' => $item->price_4,
  568. 'price_4_total' => $item->price_4_total,
  569. 'profit' => $item->profit,
  570. 'profit_rate' => $item->profit_rate,
  571. 'id_detail' => $item->id_detail,
  572. 'order_type' => $item->order_type,
  573. 'payment_amount' => $item->payment_amount ?? 0,
  574. 'id_detail_upstream' => $item->id_detail_upstream?? 0,
  575. 'order_number_upstream' => $item->order_number_upstream ?? "",
  576. 'is_activity' => $item->is_activity ?? 0,
  577. 'customer_profit_rate' => $item->customer_profit_rate ?? '',
  578. 'voucher_type' => $item->voucher_type ?? 0,
  579. 'crt_time' => $time,
  580. ];
  581. })->toArray();
  582. // 每批单独插入(可选:加小事务)
  583. RevenueCost::insert($data);
  584. // 更新 lastId
  585. $lastId = $chunk->last()->id;
  586. } while ($chunk->count() == $batchSize);
  587. // 3. 更新主表
  588. list($status, $msg) = $this->updateRevenueCostTotal($ergs);
  589. if (! $status) {
  590. throw new \Exception($msg);
  591. }
  592. });
  593. }catch (\Throwable $exception){
  594. return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  595. }
  596. return [true, ''];
  597. }
  598. private function updateRevenueCostTotal($data){
  599. try {
  600. $start_timeStamp = $data['start_timeStamp'];
  601. $end_timeStamp = $data['end_timeStamp'];
  602. $time = time();
  603. //组织写入数据
  604. $return = [];
  605. $update_stamp = [];
  606. DB::table('revenue_cost')
  607. ->where('del_time',0)
  608. ->where('order_time', '>=', $start_timeStamp)
  609. ->where('order_time', '<=', $end_timeStamp)
  610. ->select(RevenueCost::$field)
  611. ->chunkById(100, function ($data) use(&$return,&$update_stamp){
  612. $dataArray = Collect($data)->map(function ($object){
  613. return (array)$object;
  614. })->toArray();
  615. foreach ($dataArray as $value){
  616. //变成每个月第一天的时间戳
  617. $time = date("Y-m-01", $value['order_time']);
  618. $stamp = strtotime($time);
  619. if(! in_array($stamp, $update_stamp)) $update_stamp[] = $stamp;
  620. if($value['order_type'] == RevenueCost::ORDER_ONE){
  621. $income = $value['price_3_total'];
  622. }elseif ($value['order_type'] == RevenueCost::ORDER_TWO){
  623. $income = $value['price_1_total'];
  624. }else{
  625. $income = $value['payment_amount'];
  626. }
  627. $adjust = $income < 0 ? $income : 0;
  628. $business = $value['price_4_total'];
  629. if(isset($return[$stamp][$value['order_type']][$value['employee_id_1']])){
  630. $income_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['income'], $income,2);
  631. $adjust_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'], $adjust,2);
  632. $business_total = bcadd($return[$stamp][$value['order_type']][$value['employee_id_1']]['business'], $business,2);
  633. $return[$stamp][$value['order_type']][$value['employee_id_1']]['income'] = $income_total;
  634. $return[$stamp][$value['order_type']][$value['employee_id_1']]['adjust'] = $adjust_total;
  635. $return[$stamp][$value['order_type']][$value['employee_id_1']]['business'] = $business_total;
  636. }else{
  637. $return[$stamp][$value['order_type']][$value['employee_id_1']] = [
  638. 'income' => $income,
  639. 'adjust' => $adjust,
  640. 'business' => $business,
  641. 'order_time' => $stamp,
  642. 'employee_id_1_title' => $value['employee_id_1_title'],
  643. ];
  644. }
  645. }
  646. });
  647. $insert = [];
  648. foreach ($return as $value){
  649. foreach ($value as $order_type => $val){
  650. foreach ($val as $employee_id => $v){
  651. $profit = bcsub($v['income'], $v['business'],2);
  652. $profit_rate = $v['income'] > 0 ? bcdiv($profit, $v['income'],2) : 0;
  653. $v['profit'] = $profit;
  654. $v['profit_rate'] = $profit_rate;
  655. $v['order_type'] = $order_type;
  656. $v['employee_id_1'] = $employee_id;
  657. $v['crt_time'] = $time;
  658. $insert[] = $v;
  659. }
  660. }
  661. }
  662. RevenueCostTotal::where('del_time', 0)
  663. ->whereIn('order_time', $update_stamp)
  664. ->update(['del_time' => $time]);
  665. RevenueCostTotal::insert($insert);
  666. }catch (\Throwable $exception){
  667. return [false, "主表同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  668. }
  669. return [true, ''];
  670. }
  671. private function createTmpTable(){
  672. $table = $this->table;
  673. if (! Schema::hasTable($table)) {
  674. // 可以通过 migration 创建,或程序启动时检查
  675. Schema::create($table, function (Blueprint $table) {
  676. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  677. $table->integer('order_type')->default(0);
  678. $table->bigInteger('order_id')->default(0);
  679. $table->string('order_number', 50)->default('');
  680. $table->integer('order_time')->nullable();
  681. $table->integer('order_state')->default(0);
  682. $table->string('employee_id_1_title', 100)->default('');
  683. $table->bigInteger('employee_id_1')->default(0);
  684. $table->bigInteger('employee_id_2')->default(0);
  685. $table->string('employee_id_2_title', 100)->default('');
  686. $table->string('customer_code', 50)->default('');
  687. $table->string('customer_title', 100)->default('');
  688. $table->string('channel_finance', 50)->nullable();
  689. $table->string('channel_details', 50)->nullable();
  690. $table->string('product_code', 50)->default('');
  691. $table->string('product_title', 100)->default('');
  692. $table->string('product_size', 100)->nullable();
  693. $table->string('unit', 20)->nullable();
  694. $table->decimal('quantity', 12, 2)->default(0);
  695. $table->decimal('price_1', 12, 2)->default(0); // 销项成本
  696. $table->decimal('price_1_total', 12, 2)->default(0);
  697. $table->decimal('price_2', 12, 2)->default(0); // 运费
  698. $table->decimal('price_2_total', 12, 2)->default(0);
  699. $table->decimal('price_3', 12, 2)->default(0); // 含税单价
  700. $table->decimal('price_3_total', 12, 2)->default(0); // 含税金额
  701. $table->decimal('price_4', 12, 2)->default(0); // 业务成本
  702. $table->decimal('price_4_total', 12, 2)->default(0);
  703. $table->decimal('profit', 12, 2)->default(0);
  704. $table->decimal('payment_amount', 12, 2)->default(0);
  705. $table->decimal('profit_rate', 10, 3)->default(0);
  706. $table->bigInteger('id_detail')->default(0);
  707. $table->bigInteger('id_detail_upstream')->default(0);
  708. $table->string('order_number_upstream', 100)->nullable();
  709. $table->decimal('is_activity', 2, 0)->default(0);
  710. $table->string('customer_profit_rate', 20)->default('');
  711. $table->bigInteger('voucher_type')->default(0);
  712. });
  713. }
  714. }
  715. public function clearTmpTable(){
  716. if (Schema::hasTable($this->table)) DB::table($this->table)->truncate();
  717. }
  718. public function delTableKey($type = 1){
  719. $key = $this->table;
  720. if($type == 2) $key = $this->table_2;
  721. $this->dellimitingSendRequest($key);
  722. }
  723. public function synSalaryEmployee($data, $user){
  724. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '同步时间不能为空'];
  725. list($start_time, $end_time) = $this->changeDateToTimeStampAboutRange($data['crt_time'],false);
  726. if ($start_time === null || $end_time === null || $start_time > $end_time) return [false, "同步时间:时间区间无效"];
  727. list($bool, $bool_msg) = $this->isOverThreeMonths($start_time, $end_time);
  728. if(! $bool) return [false, $bool_msg];
  729. $data['start_timeStamp'] = $start_time;
  730. $data['end_timeStamp'] = $end_time;
  731. $data['start_time'] = strtotime(date("Y-m-01",$data['start_timeStamp']));
  732. $data['end_time'] = strtotime(date("Y-m-01"),$data['end_timeStamp']);
  733. $data['operation_time'] = time();
  734. list($status,$msg) = $this->limitingSendRequest($this->table_2);
  735. if(! $status) return [false, '业务员工资同步正在后台运行,请稍后'];
  736. //同步
  737. // list($status, $msg) = $this->synSalaryEmployeeFromMine($data, $user);
  738. // if(! $status) {
  739. // $this->dellimitingSendRequest($this->table_2);
  740. // return [false, $msg];
  741. // }
  742. //队列
  743. ProcessDataJob::dispatch($data, $user, 2)->onQueue(RevenueCost::job2);
  744. return [true, '业务员工资相关信息同步已进入后台任务'];
  745. }
  746. public function synSalaryEmployeeFromMine($data, $user){
  747. //创建临时表 如果不存在
  748. $this->createTmpTable2();
  749. //清理临时表 如果内容不为空
  750. $this->clearTmpTable2();
  751. //写入临时表
  752. DB::table('revenue_cost')
  753. ->where('del_time', 0)
  754. ->where('order_type', RevenueCost::ORDER_THREE)
  755. ->where('order_time','>=',$data['start_timeStamp'])
  756. ->where('order_time','<=',$data['end_timeStamp'])
  757. ->select([
  758. 'employee_id_1',
  759. 'employee_id_1_title',
  760. 'order_time as time',
  761. DB::raw("DATE_FORMAT(FROM_UNIXTIME(order_time), '%Y-%m-01') as order_time"),
  762. DB::raw("SUM(payment_amount) as payment_amount"),
  763. DB::raw("SUM(CASE WHEN is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_not_include_activity"),
  764. DB::raw("SUM(CASE WHEN is_activity = 1 THEN payment_amount ELSE 0 END) as payment_amount_activity"),
  765. DB::raw("SUM(CASE WHEN profit_rate < customer_profit_rate and is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_lower_than_rate"),
  766. DB::raw("SUM(CASE WHEN profit_rate >= customer_profit_rate and is_activity = 0 THEN payment_amount ELSE 0 END) as payment_amount_greater_than_rate"),
  767. DB::raw("SUM(CASE WHEN profit_rate >= customer_profit_rate and is_activity = 0 THEN price_4_total ELSE 0 END) as business"),
  768. DB::raw("ROW_NUMBER() OVER (ORDER BY MIN(id)) as fake_id")
  769. ])
  770. ->groupBy('employee_id_1', DB::raw("DATE_FORMAT(FROM_UNIXTIME(order_time), '%Y-%m-01')"))
  771. ->orderBy('order_time', 'desc')
  772. ->chunkById(500, function ($data){
  773. $dataArray = Collect($data)->map(function ($object){
  774. return (array)$object;
  775. })->toArray();
  776. $indexes = $this->getEmployeeIndex($dataArray);
  777. foreach ($dataArray as $key => $value){
  778. $value['index_' . EmployeeIndex::TYPE_ONE] = 0;
  779. $value['index_' . EmployeeIndex::TYPE_SIX] = 0;
  780. $value['index_' . EmployeeIndex::TYPE_EIGHT] = 0;
  781. $this->findIndex($indexes, $value);
  782. $dataArray[$key]['order_type'] = RevenueCost::ORDER_THREE;
  783. $dataArray[$key]['order_time'] = strtotime($value['order_time']);
  784. $dataArray[$key]['sale_bonus'] = $value['index_' . EmployeeIndex::TYPE_EIGHT];
  785. $dataArray[$key]['index_' . EmployeeIndex::TYPE_ONE] = $value['index_' . EmployeeIndex::TYPE_ONE];
  786. $rate = bcdiv($value['index_' . EmployeeIndex::TYPE_ONE],100,2);
  787. $pay_in_advance = bcmul($rate, $value['payment_amount_greater_than_rate'],2);
  788. $dataArray[$key]['pay_in_advance'] = $pay_in_advance;
  789. $dataArray[$key]['basic_salary'] = $value['index_' . EmployeeIndex::TYPE_SIX];
  790. $dataArray[$key]['should_pay'] = bcadd(bcadd($value['index_' . EmployeeIndex::TYPE_EIGHT], $pay_in_advance,2),$value['index_' . EmployeeIndex::TYPE_SIX],2);
  791. unset($dataArray[$key]['fake_id']);
  792. unset($dataArray[$key]['time']);
  793. }
  794. DB::table($this->table_2)->insert($dataArray);
  795. }, 'fake_id');
  796. //更新数据
  797. list($status,$msg) = $this->updateSalaryEmployee($data);
  798. if(! $status) return [false, $msg];
  799. //清理临时表 如果内容不为空
  800. $this->clearTmpTable2();
  801. //释放redis
  802. $this->delTableKey(2);
  803. return [true, '同步成功'];
  804. }
  805. private function getEmployeeIndex($existingData)
  806. {
  807. if (empty($existingData)) {
  808. return collect();
  809. }
  810. // 取出所有涉及的 employee_id 和时间区间
  811. $employeeIds = array_column($existingData, 'employee_id_1');
  812. $time = array_column($existingData, 'time');
  813. $minStart = ! empty($time) ? min($time) : 0;
  814. $maxEnd = ! empty($time) ? max($time) : 0;
  815. // 一次性查出这些员工在最大区间范围内的所有指标
  816. $results = EmployeeIndex::where('del_time', 0)
  817. ->whereIn('type', [EmployeeIndex::TYPE_ONE, EmployeeIndex::TYPE_EIGHT, EmployeeIndex::TYPE_SIX])
  818. ->whereIn('employee_id', $employeeIds)
  819. ->where('start_time', '<=', $maxEnd)
  820. ->where('end_time', '>=', $minStart)
  821. ->select('start_time','end_time','employee_id','index','type')
  822. ->get();
  823. return $results;
  824. }
  825. private function findIndex($indexes, &$value){
  826. // 找到所有符合条件的 index(可能多个 type)
  827. $matchedIndexes = $indexes->filter(function ($item) use ($value) {
  828. return $item['employee_id'] == $value['employee_id_1']
  829. && $item['start_time'] <= $value['time']
  830. && $item['end_time'] >= $value['time'];
  831. });
  832. // 按 type 去重,只保留第一次出现的
  833. $uniqueByType = [];
  834. foreach ($matchedIndexes as $item) {
  835. $index = "index_" . $item['type'];
  836. if (! isset($uniqueByType[$index])) {
  837. $uniqueByType[$index] = $item['index'];
  838. }
  839. }
  840. $value = array_merge($value, $uniqueByType);
  841. }
  842. private function createTmpTable2(){
  843. $table = $this->table_2;
  844. if (! Schema::hasTable($table)) {
  845. // 可以通过 migration 创建,或程序启动时检查
  846. Schema::create($table, function (Blueprint $table) {
  847. $table->bigIncrements('id'); // BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
  848. $table->integer('order_type')->default(0);
  849. $table->integer('order_time')->default(0);
  850. $table->string('employee_id_1_title', 100)->default('');
  851. $table->bigInteger('employee_id_1')->default(0);
  852. $table->decimal('payment_amount', 12, 2)->default(0);
  853. $table->decimal('payment_amount_not_include_activity', 12, 2)->default(0);
  854. $table->decimal('payment_amount_activity', 12, 2)->default(0);
  855. $table->decimal('payment_amount_lower_than_rate', 12, 2)->default(0);
  856. $table->decimal('payment_amount_greater_than_rate', 12, 2)->default(0);
  857. $table->decimal('business', 12, 2)->default(0);
  858. $table->decimal('sale_bonus', 12, 2)->default(0);
  859. $table->decimal('index_1', 10, 2)->default(0);
  860. $table->decimal('pay_in_advance', 12, 2)->default(0);
  861. $table->decimal('basic_salary', 12, 2)->default(0);
  862. $table->decimal('should_pay', 12, 2)->default(0);
  863. });
  864. }
  865. }
  866. public function clearTmpTable2(){
  867. if (Schema::hasTable($this->table_2)) DB::table($this->table_2)->truncate();
  868. }
  869. private function updateSalaryEmployee($data){
  870. try {
  871. $start_timeStamp = $data['start_time'];
  872. $end_timeStamp = $data['end_time'];
  873. $tmpTable = $this->table_2;
  874. $time = time();
  875. $ergs = $data;
  876. DB::transaction(function () use ($start_timeStamp, $end_timeStamp, $tmpTable,$time, $ergs) {
  877. // 1. 先软删除旧数据(你已有)
  878. SalaryEmployee::where('del_time', 0)
  879. ->where('order_time', '>=', $start_timeStamp)
  880. ->where('order_time', '<=', $end_timeStamp)
  881. ->update(['del_time' => $time]);
  882. // 2. 分批从临时表插入新数据
  883. $batchSize = 500;
  884. $lastId = 0;
  885. do {
  886. $chunk = DB::table($tmpTable)
  887. ->where('id', '>', $lastId)
  888. ->orderBy('id')
  889. ->limit($batchSize)
  890. ->get();
  891. if ($chunk->isEmpty()) {
  892. break;
  893. }
  894. $data = $chunk->map(function ($item) use($time){
  895. return [
  896. 'order_type' => $item->order_type,
  897. 'order_time' => $item->order_time,
  898. 'employee_id_1_title' => $item->employee_id_1_title,
  899. 'employee_id_1' => $item->employee_id_1 ?? 0,
  900. 'payment_amount' => $item->payment_amount,
  901. 'payment_amount_not_include_activity' => $item->payment_amount_not_include_activity,
  902. 'payment_amount_activity' => $item->payment_amount_activity,
  903. 'payment_amount_lower_than_rate' => $item->payment_amount_lower_than_rate,
  904. 'payment_amount_greater_than_rate' => $item->payment_amount_greater_than_rate,
  905. 'business' => $item->business,
  906. 'sale_bonus' => $item->sale_bonus,
  907. 'index_1' => $item->index_1,
  908. 'pay_in_advance' => $item->pay_in_advance,
  909. 'basic_salary' => $item->basic_salary,
  910. 'should_pay' => $item->should_pay,
  911. 'crt_time' => $time,
  912. ];
  913. })->toArray();
  914. // 每批单独插入(可选:加小事务)
  915. SalaryEmployee::insert($data);
  916. // 更新 lastId
  917. $lastId = $chunk->last()->id;
  918. } while ($chunk->count() == $batchSize);
  919. });
  920. }catch (\Throwable $exception){
  921. return [false, "单据明细同步异常" . $exception->getMessage() . "|" . $exception->getLine() . "|" . $exception->getFile()];
  922. }
  923. return [true, ''];
  924. }
  925. }